license_comment.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <ccan/foreach/foreach.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <limits.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <err.h>
  12. #include <ccan/str/str.h>
  13. #include <ccan/tal/str/str.h>
  14. /**
  15. * line_has_license_flavour - returns true if line contains a <flavour> license
  16. * @line: line to look for license in
  17. * @shortname: license to find
  18. * @note ("LGPLv2.0","LGPL") returns true
  19. * @note ("LGPLv2.0","GPL") returns false
  20. */
  21. static bool line_has_license_flavour(const char *line, const char *shortname)
  22. {
  23. char **toks = tal_strsplit(NULL, line, " \t-:", STR_NO_EMPTY);
  24. size_t i;
  25. bool ret = false;
  26. for (i = 0; toks[i] != NULL; i++) {
  27. if (strstarts(toks[i], shortname)) {
  28. ret = true;
  29. break;
  30. }
  31. }
  32. tal_free(toks);
  33. return ret;
  34. }
  35. static void check_license_comment(struct manifest *m,
  36. unsigned int *timeleft UNNEEDED,
  37. struct score *score)
  38. {
  39. struct list_head *list;
  40. /* No requirements on public domain. */
  41. if (m->license == LICENSE_PUBLIC_DOMAIN
  42. || m->license == LICENSE_UNKNOWN) {
  43. score->pass = true;
  44. score->score = score->total;
  45. return;
  46. }
  47. foreach_ptr(list, &m->c_files, &m->h_files) {
  48. struct ccan_file *f;
  49. list_for_each(list, f, list) {
  50. unsigned int i;
  51. char **lines = get_ccan_file_lines(f);
  52. struct line_info *info = get_ccan_line_info(f);
  53. bool found_license = false, found_flavor = false;
  54. for (i = 0; lines[i]; i++) {
  55. if (info[i].type == CODE_LINE)
  56. break;
  57. if (strstr(lines[i], "LICENSE"))
  58. found_license = true;
  59. if (line_has_license_flavour(lines[i],
  60. licenses[m->license].shortname))
  61. found_flavor = true;
  62. }
  63. if ((!found_license || !found_flavor)
  64. && !find_boilerplate(f, m->license)) {
  65. score_file_error(score, f, lines[i] ? i : 0,
  66. "No reference to license"
  67. " found");
  68. }
  69. }
  70. }
  71. if (list_empty(&score->per_file_errors)) {
  72. score->pass = true;
  73. score->score = score->total;
  74. }
  75. }
  76. static void add_license_comment(struct manifest *m, struct score *score)
  77. {
  78. struct file_error *e;
  79. const char *license_desc = get_license_oneliner(score, m->license);
  80. char *files = tal_strdup(score, ""), *q;
  81. list_for_each(&score->per_file_errors, e, list)
  82. tal_append_fmt(&files, " %s\n", e->file->name);
  83. q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
  84. "Should I prepend '%s'?", files, license_desc);
  85. if (!ask(q))
  86. return;
  87. list_for_each(&score->per_file_errors, e, list) {
  88. char *tmpname;
  89. FILE *out;
  90. unsigned int i;
  91. tmpname = temp_file(score, ".licensed", e->file->name);
  92. out = fopen(tmpname, "w");
  93. if (!out)
  94. err(1, "Opening %s", tmpname);
  95. if (fprintf(out, "%s\n", license_desc) < 0)
  96. err(1, "Writing %s", tmpname);
  97. for (i = 0; e->file->lines[i]; i++)
  98. if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
  99. err(1, "Writing %s", tmpname);
  100. if (fclose(out) != 0)
  101. err(1, "Closing %s", tmpname);
  102. if (!move_file(tmpname, e->file->fullname))
  103. err(1, "Moving %s to %s", tmpname, e->file->fullname);
  104. }
  105. }
  106. struct ccanlint license_comment = {
  107. .key = "license_comment",
  108. .name = "Source and header files refer to LICENSE",
  109. .check = check_license_comment,
  110. .handle = add_license_comment,
  111. .needs = "license_exists"
  112. };
  113. REGISTER_TEST(license_comment);