has_info_documentation.c 2.7 KB

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