has_info_documentation.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. extern struct ccanlint has_info_documentation;
  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_has_info_documentation(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. list_for_each(infodocs, d, list) {
  64. if (!streq(d->function, m->basename))
  65. continue;
  66. if (streq(d->type, "summary"))
  67. summary = true;
  68. if (streq(d->type, "description"))
  69. description = true;
  70. }
  71. if (summary && description) {
  72. score->score = score->total;
  73. score->pass = true;
  74. } else if (!summary) {
  75. score->error = "_info file has no module documentation.\n\n"
  76. "CCAN modules use /**-style comments for documentation: the\n"
  77. "overall documentation belongs in the _info metafile.\n";
  78. has_info_documentation.handle = create_info_template_doc;
  79. } else if (!description) {
  80. score->error = "_info file has no module description.\n\n"
  81. "The lines after the first summary line in the _info file\n"
  82. "documentation should describe the purpose and use of the\n"
  83. "overall package\n";
  84. }
  85. }
  86. struct ccanlint has_info_documentation = {
  87. .key = "info_documentation_exists",
  88. .name = "Module has documentation in _info",
  89. .check = check_has_info_documentation,
  90. .needs = "info_exists"
  91. };
  92. REGISTER_TEST(has_info_documentation);