info_documentation_exists.c 3.1 KB

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