has_info_documentation.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/doc_extract.h>
  3. #include <tools/tools.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <errno.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <err.h>
  13. #include <ccan/str/str.h>
  14. #include <ccan/talloc/talloc.h>
  15. #include <ccan/noerr/noerr.h>
  16. #include <ccan/grab_file/grab_file.h>
  17. struct info_docs
  18. {
  19. bool summary;
  20. bool description;
  21. bool example;
  22. };
  23. static void *check_has_info_documentation(struct manifest *m)
  24. {
  25. struct list_head *infodocs = get_ccan_file_docs(m->info_file);
  26. struct doc_section *d;
  27. struct info_docs id = { false, false, false };
  28. list_for_each(infodocs, d, list) {
  29. if (!streq(d->function, m->basename))
  30. continue;
  31. if (streq(d->type, "summary"))
  32. id.summary = true;
  33. if (streq(d->type, "description"))
  34. id.description = true;
  35. if (streq(d->type, "example"))
  36. id.example = true;
  37. }
  38. if (id.summary && id.description && id.example)
  39. return NULL;
  40. return talloc_memdup(m, &id, sizeof(id));
  41. }
  42. /* This is defined below. */
  43. extern struct ccanlint has_info_documentation;
  44. static void create_info_template_doc(struct manifest *m, void *check_result)
  45. {
  46. int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
  47. FILE *new;
  48. char *oldcontents;
  49. if (fd < 0 || !(new = fdopen(fd, "w")))
  50. err(1, "Creating _info.new to insert documentation");
  51. if (fprintf(new,
  52. "/**\n"
  53. " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
  54. " *\n"
  55. " * Paragraphs why %s exists and where to use it.\n"
  56. " *\n"
  57. " * Followed by an Example: section with a standalone\n"
  58. " * (trivial and usually useless) program\n"
  59. " */\n", m->basename, m->basename) < 0) {
  60. unlink_noerr("_info.new");
  61. err(1, "Writing to _info.new to insert documentation");
  62. }
  63. oldcontents = grab_file(m, "_info", NULL);
  64. if (!oldcontents) {
  65. unlink_noerr("_info.new");
  66. err(1, "Reading _info");
  67. }
  68. if (fprintf(new, "%s", oldcontents) < 0) {
  69. unlink_noerr("_info.new");
  70. err(1, "Appending _info to _info.new");
  71. }
  72. if (fclose(new) != 0) {
  73. unlink_noerr("_info.new");
  74. err(1, "Closing _info.new");
  75. }
  76. if (!move_file("_info.new", "_info")) {
  77. unlink_noerr("_info.new");
  78. err(1, "Renaming _info.new to _info");
  79. }
  80. }
  81. static const char *describe_has_info_documentation(struct manifest *m,
  82. void *check_result)
  83. {
  84. struct info_docs *id = check_result;
  85. char *reason = talloc_strdup(m, "");
  86. if (!id->summary) {
  87. has_info_documentation.handle = create_info_template_doc;
  88. reason = talloc_asprintf_append(reason,
  89. "Your _info file has no module documentation.\n\n"
  90. "CCAN modules use /**-style comments for documentation: the\n"
  91. "overall documentation belongs in the _info metafile.\n");
  92. }
  93. if (!id->description)
  94. reason = talloc_asprintf_append(reason,
  95. "Your _info file has no module description.\n\n"
  96. "The lines after the first summary line in the _info file\n"
  97. "documentation should describe the purpose and use of the\n"
  98. "overall package\n");
  99. if (!id->example)
  100. reason = talloc_asprintf_append(reason,
  101. "Your _info file has no module example.\n\n"
  102. "There should be an Example: section of the _info documentation\n"
  103. "which provides a concise toy program which uses your module\n");
  104. return reason;
  105. }
  106. static unsigned int has_info_documentation_score(struct manifest *m,
  107. void *check_result)
  108. {
  109. struct info_docs *id = check_result;
  110. return id->summary + id->description + id->example;
  111. }
  112. struct ccanlint has_info_documentation = {
  113. .name = "Documentation in _info file",
  114. .total_score = 3,
  115. .score = has_info_documentation_score,
  116. .check = check_has_info_documentation,
  117. .describe = describe_has_info_documentation,
  118. };
  119. REGISTER_TEST(has_info_documentation, NULL);