1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/foreach/foreach.h>
12 #include <ccan/str/str.h>
13 #include <ccan/tal/str/str.h>
16 * line_has_license_flavour - returns true if line contains a <flavour> license
17 * @line: line to look for license in
18 * @shortname: license to find
19 * @note ("LGPLv2.0","LGPL") returns true
20 * @note ("LGPLv2.0","GPL") returns false
22 static bool line_has_license_flavour(const char *line, const char *shortname)
24 char **toks = tal_strsplit(NULL, line, " \t-:", STR_NO_EMPTY);
28 for (i = 0; toks[i] != NULL; i++) {
29 if (strstarts(toks[i], shortname)) {
38 static void check_license_comment(struct manifest *m,
39 unsigned int *timeleft, struct score *score)
41 struct list_head *list;
43 /* No requirements on public domain. */
44 if (m->license == LICENSE_PUBLIC_DOMAIN
45 || m->license == LICENSE_UNKNOWN) {
47 score->score = score->total;
51 foreach_ptr(list, &m->c_files, &m->h_files) {
54 list_for_each(list, f, list) {
56 char **lines = get_ccan_file_lines(f);
57 struct line_info *info = get_ccan_line_info(f);
58 bool found_license = false, found_flavor = false;
60 for (i = 0; lines[i]; i++) {
61 if (info[i].type == CODE_LINE)
63 if (strstr(lines[i], "LICENSE"))
65 if (line_has_license_flavour(lines[i],
66 licenses[m->license].shortname))
69 if ((!found_license || !found_flavor)
70 && !find_boilerplate(f, m->license)) {
71 score_file_error(score, f, lines[i] ? i : 0,
72 "No reference to license"
78 if (list_empty(&score->per_file_errors)) {
80 score->score = score->total;
84 static void add_license_comment(struct manifest *m, struct score *score)
87 const char *license_desc = get_license_oneliner(score, m->license);
88 char *files = tal_strdup(score, ""), *q;
90 list_for_each(&score->per_file_errors, e, list)
91 tal_append_fmt(&files, " %s\n", e->file->name);
93 q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
94 "Should I prepend '%s'?", files, license_desc);
98 list_for_each(&score->per_file_errors, e, list) {
103 tmpname = temp_file(score, ".licensed", e->file->name);
104 out = fopen(tmpname, "w");
106 err(1, "Opening %s", tmpname);
107 if (fprintf(out, "%s\n", license_desc) < 0)
108 err(1, "Writing %s", tmpname);
110 for (i = 0; e->file->lines[i]; i++)
111 if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
112 err(1, "Writing %s", tmpname);
114 if (fclose(out) != 0)
115 err(1, "Closing %s", tmpname);
117 if (!move_file(tmpname, e->file->fullname))
118 err(1, "Moving %s to %s", tmpname, e->file->fullname);
122 struct ccanlint license_comment = {
123 .key = "license_comment",
124 .name = "Source and header files refer to LICENSE",
125 .check = check_license_comment,
126 .handle = add_license_comment,
127 .needs = "license_exists"
129 REGISTER_TEST(license_comment);