license_comment.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, struct score *score)
  37. {
  38. struct list_head *list;
  39. /* No requirements on public domain. */
  40. if (m->license == LICENSE_PUBLIC_DOMAIN
  41. || m->license == LICENSE_UNKNOWN) {
  42. score->pass = true;
  43. score->score = score->total;
  44. return;
  45. }
  46. foreach_ptr(list, &m->c_files, &m->h_files) {
  47. struct ccan_file *f;
  48. list_for_each(list, f, list) {
  49. unsigned int i;
  50. char **lines = get_ccan_file_lines(f);
  51. struct line_info *info = get_ccan_line_info(f);
  52. bool found_license = false, found_flavor = false;
  53. for (i = 0; lines[i]; i++) {
  54. if (info[i].type == CODE_LINE)
  55. break;
  56. if (strstr(lines[i], "LICENSE"))
  57. found_license = true;
  58. if (line_has_license_flavour(lines[i],
  59. licenses[m->license].shortname))
  60. found_flavor = true;
  61. }
  62. if ((!found_license || !found_flavor)
  63. && !find_boilerplate(f, m->license)) {
  64. score_file_error(score, f, lines[i] ? i : 0,
  65. "No reference to license"
  66. " found");
  67. }
  68. }
  69. }
  70. if (list_empty(&score->per_file_errors)) {
  71. score->pass = true;
  72. score->score = score->total;
  73. }
  74. }
  75. static void add_license_comment(struct manifest *m, struct score *score)
  76. {
  77. struct file_error *e;
  78. const char *license_desc = get_license_oneliner(score, m->license);
  79. char *files = tal_strdup(score, ""), *q;
  80. list_for_each(&score->per_file_errors, e, list)
  81. tal_append_fmt(&files, " %s\n", e->file->name);
  82. q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
  83. "Should I prepend '%s'?", files, license_desc);
  84. if (!ask(q))
  85. return;
  86. list_for_each(&score->per_file_errors, e, list) {
  87. char *tmpname;
  88. FILE *out;
  89. unsigned int i;
  90. tmpname = temp_file(score, ".licensed", e->file->name);
  91. out = fopen(tmpname, "w");
  92. if (!out)
  93. err(1, "Opening %s", tmpname);
  94. if (fprintf(out, "%s\n", license_desc) < 0)
  95. err(1, "Writing %s", tmpname);
  96. for (i = 0; e->file->lines[i]; i++)
  97. if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
  98. err(1, "Writing %s", tmpname);
  99. if (fclose(out) != 0)
  100. err(1, "Closing %s", tmpname);
  101. if (!move_file(tmpname, e->file->fullname))
  102. err(1, "Moving %s to %s", tmpname, e->file->fullname);
  103. }
  104. }
  105. struct ccanlint license_comment = {
  106. .key = "license_comment",
  107. .name = "Source and header files refer to LICENSE",
  108. .check = check_license_comment,
  109. .handle = add_license_comment,
  110. .needs = "license_exists"
  111. };
  112. REGISTER_TEST(license_comment);