ccanlint - avoid e.g. GPL vs LGPL mismatch in license checking
[ccan] / tools / ccanlint / tests / license_comment.c
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
14 static char *xstrdup(const char *s) {
15         char * ret = strdup(s);
16         if (ret == NULL) {
17                 perror("strdup");
18                 abort();
19         }
20         return ret;
21 }
22
23 /**
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
29  */
30 static bool line_has_license_flavour(const char *line, const char *flavour) {
31         char *strtok_line, *strtok_tmp, *token;
32         bool ret = false;
33         const size_t flavour_len = strlen(flavour);
34
35         strtok_line = strtok_tmp = xstrdup(line);
36         while((token = strtok(strtok_tmp, " \t-:")) != NULL) {
37                 if (!strncmp(token, flavour, flavour_len)) {
38                         ret = true;
39                         break;
40                 }
41                 strtok_tmp = NULL;
42         }
43         free(strtok_line);
44
45         return ret;
46 }
47
48 static void check_license_comment(struct manifest *m,
49                                   unsigned int *timeleft, struct score *score)
50 {
51         struct list_head *list;
52
53         /* No requirements on public domain. */
54         if (m->license == LICENSE_PUBLIC_DOMAIN
55             || m->license == LICENSE_UNKNOWN) {
56                 score->pass = true;
57                 score->score = score->total;
58                 return;
59         }
60
61         foreach_ptr(list, &m->c_files, &m->h_files) {
62                 struct ccan_file *f;
63
64                 list_for_each(list, f, list) {
65                         unsigned int i;
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;
69
70                         for (i = 0; lines[i]; i++) {
71                                 if (info[i].type == CODE_LINE)
72                                         break;
73                                 if (strstr(lines[i], "LICENSE"))
74                                         found_license = true;
75                                 if (line_has_license_flavour(lines[i],
76                                                              licenses[m->license].shortname))
77                                         found_flavor = true;
78                         }
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"
83                                                  " found");
84                         }
85                 }
86         }
87
88         if (list_empty(&score->per_file_errors)) {
89                 score->pass = true;
90                 score->score = score->total;
91         }
92 }
93
94 static void add_license_comment(struct manifest *m, struct score *score)
95 {
96         struct file_error *e;
97         const char *license_desc = get_license_oneliner(score, m->license);
98         char *files = tal_strdup(score, ""), *q;
99
100         list_for_each(&score->per_file_errors, e, list)
101                 tal_append_fmt(&files, "  %s\n", e->file->name);
102
103         q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
104                     "Should I prepend '%s'?", files, license_desc);
105         if (!ask(q))
106                 return;
107
108         list_for_each(&score->per_file_errors, e, list) {
109                 char *tmpname;
110                 FILE *out;
111                 unsigned int i;
112
113                 tmpname = temp_file(score, ".licensed", e->file->name);
114                 out = fopen(tmpname, "w");
115                 if (!out)
116                         err(1, "Opening %s", tmpname);
117                 if (fprintf(out, "%s\n", license_desc) < 0)
118                         err(1, "Writing %s", tmpname);
119
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);
123
124                 if (fclose(out) != 0)
125                         err(1, "Closing %s", tmpname);
126
127                 if (!move_file(tmpname, e->file->fullname))
128                         err(1, "Moving %s to %s", tmpname, e->file->fullname);
129         }
130 }
131
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"
138 };
139 REGISTER_TEST(license_comment);