info_documentation_exists.c 3.1 KB

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