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 UNNEEDED,
42 struct list_head *list;
44 /* No requirements on public domain. */
45 if (m->license == LICENSE_PUBLIC_DOMAIN
46 || m->license == LICENSE_UNKNOWN) {
48 score->score = score->total;
52 foreach_ptr(list, &m->c_files, &m->h_files) {
55 list_for_each(list, f, list) {
57 char **lines = get_ccan_file_lines(f);
58 struct line_info *info = get_ccan_line_info(f);
59 bool found_license = false, found_flavor = false;
61 for (i = 0; lines[i]; i++) {
62 if (info[i].type == CODE_LINE)
64 if (strstr(lines[i], "LICENSE"))
66 if (line_has_license_flavour(lines[i],
67 licenses[m->license].shortname))
70 if ((!found_license || !found_flavor)
71 && !find_boilerplate(f, m->license)) {
72 score_file_error(score, f, lines[i] ? i : 0,
73 "No reference to license"
79 if (list_empty(&score->per_file_errors)) {
81 score->score = score->total;
85 static void add_license_comment(struct manifest *m, struct score *score)
88 const char *license_desc = get_license_oneliner(score, m->license);
89 char *files = tal_strdup(score, ""), *q;
91 list_for_each(&score->per_file_errors, e, list)
92 tal_append_fmt(&files, " %s\n", e->file->name);
94 q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
95 "Should I prepend '%s'?", files, license_desc);
99 list_for_each(&score->per_file_errors, e, list) {
104 tmpname = temp_file(score, ".licensed", e->file->name);
105 out = fopen(tmpname, "w");
107 err(1, "Opening %s", tmpname);
108 if (fprintf(out, "%s\n", license_desc) < 0)
109 err(1, "Writing %s", tmpname);
111 for (i = 0; e->file->lines[i]; i++)
112 if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
113 err(1, "Writing %s", tmpname);
115 if (fclose(out) != 0)
116 err(1, "Closing %s", tmpname);
118 if (!move_file(tmpname, e->file->fullname))
119 err(1, "Moving %s to %s", tmpname, e->file->fullname);
123 struct ccanlint license_comment = {
124 .key = "license_comment",
125 .name = "Source and header files refer to LICENSE",
126 .check = check_license_comment,
127 .handle = add_license_comment,
128 .needs = "license_exists"
130 REGISTER_TEST(license_comment);