license_comment.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. static void check_license_comment(struct manifest *m,
  14. unsigned int *timeleft, struct score *score)
  15. {
  16. struct list_head *list;
  17. /* No requirements on public domain. */
  18. if (m->license == LICENSE_PUBLIC_DOMAIN
  19. || m->license == LICENSE_UNKNOWN) {
  20. score->pass = true;
  21. score->score = score->total;
  22. return;
  23. }
  24. foreach_ptr(list, &m->c_files, &m->h_files) {
  25. struct ccan_file *f;
  26. list_for_each(list, f, list) {
  27. unsigned int i;
  28. char **lines = get_ccan_file_lines(f);
  29. struct line_info *info = get_ccan_line_info(f);
  30. bool found_license = false, found_flavor = false;
  31. for (i = 0; lines[i]; i++) {
  32. if (info[i].type == CODE_LINE)
  33. break;
  34. if (strstr(lines[i], "LICENSE"))
  35. found_license = true;
  36. if (strstr(lines[i],
  37. licenses[m->license].shortname))
  38. found_flavor = true;
  39. }
  40. if ((!found_license || !found_flavor)
  41. && !find_boilerplate(f, m->license)) {
  42. score_file_error(score, f, lines[i] ? i : 0,
  43. "No reference to license"
  44. " found");
  45. }
  46. }
  47. }
  48. if (list_empty(&score->per_file_errors)) {
  49. score->pass = true;
  50. score->score = score->total;
  51. }
  52. }
  53. static void add_license_comment(struct manifest *m, struct score *score)
  54. {
  55. struct file_error *e;
  56. const char *license_desc = get_license_oneliner(score, m->license);
  57. char *files = tal_strdup(score, ""), *q;
  58. list_for_each(&score->per_file_errors, e, list)
  59. tal_append_fmt(&files, " %s\n", e->file->name);
  60. q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
  61. "Should I prepend '%s'?", files, license_desc);
  62. if (!ask(q))
  63. return;
  64. list_for_each(&score->per_file_errors, e, list) {
  65. char *tmpname;
  66. FILE *out;
  67. unsigned int i;
  68. tmpname = temp_file(score, ".licensed", e->file->name);
  69. out = fopen(tmpname, "w");
  70. if (!out)
  71. err(1, "Opening %s", tmpname);
  72. if (fprintf(out, "%s\n", license_desc) < 0)
  73. err(1, "Writing %s", tmpname);
  74. for (i = 0; e->file->lines[i]; i++)
  75. if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
  76. err(1, "Writing %s", tmpname);
  77. if (fclose(out) != 0)
  78. err(1, "Closing %s", tmpname);
  79. if (!move_file(tmpname, e->file->fullname))
  80. err(1, "Moving %s to %s", tmpname, e->file->fullname);
  81. }
  82. }
  83. struct ccanlint license_comment = {
  84. .key = "license_comment",
  85. .name = "Source and header files refer to LICENSE",
  86. .check = check_license_comment,
  87. .handle = add_license_comment,
  88. .needs = "license_exists"
  89. };
  90. REGISTER_TEST(license_comment);