has_info_documentation.c 3.4 KB

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