info_documentation_exists.c 3.0 KB

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