]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/license_comment.c
ccanlint: Add cflags support to _info
[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 #include <ccan/tal/str/str.h>
14
15 /**
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
21  */
22 static bool line_has_license_flavour(const char *line, const char *shortname)
23 {
24         char **toks = tal_strsplit(NULL, line, " \t-:", STR_NO_EMPTY);
25         size_t i;
26         bool ret = false;
27
28         for (i = 0; toks[i] != NULL; i++) {
29                 if (strstarts(toks[i], shortname)) {
30                         ret = true;
31                         break;
32                 }
33         }
34         tal_free(toks);
35         return ret;
36 }
37
38 static void check_license_comment(struct manifest *m,
39                                   unsigned int *timeleft, struct score *score)
40 {
41         struct list_head *list;
42
43         /* No requirements on public domain. */
44         if (m->license == LICENSE_PUBLIC_DOMAIN
45             || m->license == LICENSE_UNKNOWN) {
46                 score->pass = true;
47                 score->score = score->total;
48                 return;
49         }
50
51         foreach_ptr(list, &m->c_files, &m->h_files) {
52                 struct ccan_file *f;
53
54                 list_for_each(list, f, list) {
55                         unsigned int i;
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;
59
60                         for (i = 0; lines[i]; i++) {
61                                 if (info[i].type == CODE_LINE)
62                                         break;
63                                 if (strstr(lines[i], "LICENSE"))
64                                         found_license = true;
65                                 if (line_has_license_flavour(lines[i],
66                                                              licenses[m->license].shortname))
67                                         found_flavor = true;
68                         }
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"
73                                                  " found");
74                         }
75                 }
76         }
77
78         if (list_empty(&score->per_file_errors)) {
79                 score->pass = true;
80                 score->score = score->total;
81         }
82 }
83
84 static void add_license_comment(struct manifest *m, struct score *score)
85 {
86         struct file_error *e;
87         const char *license_desc = get_license_oneliner(score, m->license);
88         char *files = tal_strdup(score, ""), *q;
89
90         list_for_each(&score->per_file_errors, e, list)
91                 tal_append_fmt(&files, "  %s\n", e->file->name);
92
93         q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
94                     "Should I prepend '%s'?", files, license_desc);
95         if (!ask(q))
96                 return;
97
98         list_for_each(&score->per_file_errors, e, list) {
99                 char *tmpname;
100                 FILE *out;
101                 unsigned int i;
102
103                 tmpname = temp_file(score, ".licensed", e->file->name);
104                 out = fopen(tmpname, "w");
105                 if (!out)
106                         err(1, "Opening %s", tmpname);
107                 if (fprintf(out, "%s\n", license_desc) < 0)
108                         err(1, "Writing %s", tmpname);
109
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);
113
114                 if (fclose(out) != 0)
115                         err(1, "Closing %s", tmpname);
116
117                 if (!move_file(tmpname, e->file->fullname))
118                         err(1, "Moving %s to %s", tmpname, e->file->fullname);
119         }
120 }
121
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"
128 };
129 REGISTER_TEST(license_comment);