1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/foreach/foreach.h>
12 #include <ccan/str/str.h>
14 static void check_license_comment(struct manifest *m,
15 unsigned int *timeleft, struct score *score)
17 struct list_head *list;
19 /* No requirements on public domain. */
20 if (m->license == LICENSE_PUBLIC_DOMAIN
21 || m->license == LICENSE_UNKNOWN) {
23 score->score = score->total;
27 foreach_ptr(list, &m->c_files, &m->h_files) {
30 list_for_each(list, f, list) {
32 char **lines = get_ccan_file_lines(f);
33 struct line_info *info = get_ccan_line_info(f);
34 bool found_license = false, found_flavor = false;
36 for (i = 0; lines[i]; i++) {
37 if (info[i].type == CODE_LINE)
39 if (strstr(lines[i], "LICENSE"))
42 licenses[m->license].shortname))
45 if ((!found_license || !found_flavor)
46 && !find_boilerplate(f, m->license)) {
47 score_file_error(score, f, lines[i] ? i : 0,
48 "No reference to license"
54 if (list_empty(&score->per_file_errors)) {
56 score->score = score->total;
60 static void add_license_comment(struct manifest *m, struct score *score)
63 const char *license_desc = get_license_oneliner(score, m->license);
64 char *files = tal_strdup(score, ""), *q;
66 list_for_each(&score->per_file_errors, e, list)
67 tal_append_fmt(&files, " %s\n", e->file->name);
69 q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
70 "Should I prepend '%s'?", files, license_desc);
74 list_for_each(&score->per_file_errors, e, list) {
79 tmpname = temp_file(score, ".licensed", e->file->name);
80 out = fopen(tmpname, "w");
82 err(1, "Opening %s", tmpname);
83 if (fprintf(out, "%s\n", license_desc) < 0)
84 err(1, "Writing %s", tmpname);
86 for (i = 0; e->file->lines[i]; i++)
87 if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
88 err(1, "Writing %s", tmpname);
91 err(1, "Closing %s", tmpname);
93 if (!move_file(tmpname, e->file->fullname))
94 err(1, "Moving %s to %s", tmpname, e->file->fullname);
98 struct ccanlint license_comment = {
99 .key = "license_comment",
100 .name = "Source and header files refer to LICENSE",
101 .check = check_license_comment,
102 .handle = add_license_comment,
103 .needs = "license_exists"
105 REGISTER_TEST(license_comment);