info_documentation_exists.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. REGISTER_TEST(info_documentation_exists);
  18. static void create_info_template_doc(struct manifest *m, struct score *score)
  19. {
  20. int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
  21. FILE *new;
  22. char *oldcontents;
  23. if (fd < 0 || !(new = fdopen(fd, "w")))
  24. err(1, "Creating _info.new to insert documentation");
  25. if (fprintf(new,
  26. "/**\n"
  27. " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
  28. " *\n"
  29. " * Paragraphs why %s exists and where to use it.\n"
  30. " *\n"
  31. " * Followed by an Example: section with a standalone\n"
  32. " * (trivial and usually useless) program\n"
  33. " */\n", m->basename, m->basename) < 0) {
  34. unlink_noerr("_info.new");
  35. err(1, "Writing to _info.new to insert documentation");
  36. }
  37. oldcontents = grab_file(m, "_info", NULL);
  38. if (!oldcontents) {
  39. unlink_noerr("_info.new");
  40. err(1, "Reading _info");
  41. }
  42. if (fprintf(new, "%s", oldcontents) < 0) {
  43. unlink_noerr("_info.new");
  44. err(1, "Appending _info to _info.new");
  45. }
  46. if (fclose(new) != 0) {
  47. unlink_noerr("_info.new");
  48. err(1, "Closing _info.new");
  49. }
  50. if (!move_file("_info.new", "_info")) {
  51. unlink_noerr("_info.new");
  52. err(1, "Renaming _info.new to _info");
  53. }
  54. }
  55. static void check_info_documentation_exists(struct manifest *m,
  56. bool keep,
  57. unsigned int *timeleft,
  58. struct score *score)
  59. {
  60. struct list_head *infodocs = get_ccan_file_docs(m->info_file);
  61. struct doc_section *d;
  62. bool summary = false, description = false;
  63. /* We don't fail ccanlint for this. */
  64. score->pass = true;
  65. list_for_each(infodocs, d, list) {
  66. if (!streq(d->function, m->basename))
  67. continue;
  68. if (streq(d->type, "summary"))
  69. summary = true;
  70. if (streq(d->type, "description"))
  71. description = true;
  72. }
  73. if (summary && description) {
  74. score->score = score->total;
  75. } else if (!summary) {
  76. score->error = talloc_strdup(score,
  77. "_info file has no module documentation.\n\n"
  78. "CCAN modules use /**-style comments for documentation: the\n"
  79. "overall documentation belongs in the _info metafile.\n");
  80. info_documentation_exists.handle = create_info_template_doc;
  81. } else if (!description) {
  82. score->error = talloc_strdup(score,
  83. "_info file has no module description.\n\n"
  84. "The lines after the first summary line in the _info file\n"
  85. "documentation should describe the purpose and use of the\n"
  86. "overall package\n");
  87. }
  88. }
  89. struct ccanlint info_documentation_exists = {
  90. .key = "info_documentation_exists",
  91. .name = "Module has documentation in _info",
  92. .check = check_info_documentation_exists,
  93. .needs = "info_exists"
  94. };