info_documentation_exists.c 3.1 KB

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