1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/foreach/foreach.h>
12 #include <ccan/str/str.h>
14 static char *xstrdup(const char *s) {
15 char * ret = strdup(s);
24 * line_has_license_flavour - returns true if line contains a <flavour> license
25 * @line: line to look for license in
26 * @shortname: license to find
27 * @note ("LGPLv2.0","LGPL") returns true
28 * @note ("LGPLv2.0","GPL") returns false
30 static bool line_has_license_flavour(const char *line, const char *flavour) {
31 char *strtok_line, *strtok_tmp, *token;
33 const size_t flavour_len = strlen(flavour);
35 strtok_line = strtok_tmp = xstrdup(line);
36 while((token = strtok(strtok_tmp, " \t-:")) != NULL) {
37 if (!strncmp(token, flavour, flavour_len)) {
48 static void check_license_comment(struct manifest *m,
49 unsigned int *timeleft, struct score *score)
51 struct list_head *list;
53 /* No requirements on public domain. */
54 if (m->license == LICENSE_PUBLIC_DOMAIN
55 || m->license == LICENSE_UNKNOWN) {
57 score->score = score->total;
61 foreach_ptr(list, &m->c_files, &m->h_files) {
64 list_for_each(list, f, list) {
66 char **lines = get_ccan_file_lines(f);
67 struct line_info *info = get_ccan_line_info(f);
68 bool found_license = false, found_flavor = false;
70 for (i = 0; lines[i]; i++) {
71 if (info[i].type == CODE_LINE)
73 if (strstr(lines[i], "LICENSE"))
75 if (line_has_license_flavour(lines[i],
76 licenses[m->license].shortname))
79 if ((!found_license || !found_flavor)
80 && !find_boilerplate(f, m->license)) {
81 score_file_error(score, f, lines[i] ? i : 0,
82 "No reference to license"
88 if (list_empty(&score->per_file_errors)) {
90 score->score = score->total;
94 static void add_license_comment(struct manifest *m, struct score *score)
97 const char *license_desc = get_license_oneliner(score, m->license);
98 char *files = tal_strdup(score, ""), *q;
100 list_for_each(&score->per_file_errors, e, list)
101 tal_append_fmt(&files, " %s\n", e->file->name);
103 q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
104 "Should I prepend '%s'?", files, license_desc);
108 list_for_each(&score->per_file_errors, e, list) {
113 tmpname = temp_file(score, ".licensed", e->file->name);
114 out = fopen(tmpname, "w");
116 err(1, "Opening %s", tmpname);
117 if (fprintf(out, "%s\n", license_desc) < 0)
118 err(1, "Writing %s", tmpname);
120 for (i = 0; e->file->lines[i]; i++)
121 if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
122 err(1, "Writing %s", tmpname);
124 if (fclose(out) != 0)
125 err(1, "Closing %s", tmpname);
127 if (!move_file(tmpname, e->file->fullname))
128 err(1, "Moving %s to %s", tmpname, e->file->fullname);
132 struct ccanlint license_comment = {
133 .key = "license_comment",
134 .name = "Source and header files refer to LICENSE",
135 .check = check_license_comment,
136 .handle = add_license_comment,
137 .needs = "license_exists"
139 REGISTER_TEST(license_comment);