json_out: make json_out_finished finish buffer.
[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 UNNEEDED,
40                                   struct score *score)
41 {
42         struct list_head *list;
43
44         /* No requirements on public domain. */
45         if (m->license == LICENSE_PUBLIC_DOMAIN
46             || m->license == LICENSE_UNKNOWN) {
47                 score->pass = true;
48                 score->score = score->total;
49                 return;
50         }
51
52         foreach_ptr(list, &m->c_files, &m->h_files) {
53                 struct ccan_file *f;
54
55                 list_for_each(list, f, list) {
56                         unsigned int i;
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;
60
61                         for (i = 0; lines[i]; i++) {
62                                 if (info[i].type == CODE_LINE)
63                                         break;
64                                 if (strstr(lines[i], "LICENSE"))
65                                         found_license = true;
66                                 if (line_has_license_flavour(lines[i],
67                                                              licenses[m->license].shortname))
68                                         found_flavor = true;
69                         }
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"
74                                                  " found");
75                         }
76                 }
77         }
78
79         if (list_empty(&score->per_file_errors)) {
80                 score->pass = true;
81                 score->score = score->total;
82         }
83 }
84
85 static void add_license_comment(struct manifest *m, struct score *score)
86 {
87         struct file_error *e;
88         const char *license_desc = get_license_oneliner(score, m->license);
89         char *files = tal_strdup(score, ""), *q;
90
91         list_for_each(&score->per_file_errors, e, list)
92                 tal_append_fmt(&files, "  %s\n", e->file->name);
93
94         q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
95                     "Should I prepend '%s'?", files, license_desc);
96         if (!ask(q))
97                 return;
98
99         list_for_each(&score->per_file_errors, e, list) {
100                 char *tmpname;
101                 FILE *out;
102                 unsigned int i;
103
104                 tmpname = temp_file(score, ".licensed", e->file->name);
105                 out = fopen(tmpname, "w");
106                 if (!out)
107                         err(1, "Opening %s", tmpname);
108                 if (fprintf(out, "%s\n", license_desc) < 0)
109                         err(1, "Writing %s", tmpname);
110
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);
114
115                 if (fclose(out) != 0)
116                         err(1, "Closing %s", tmpname);
117
118                 if (!move_file(tmpname, e->file->fullname))
119                         err(1, "Moving %s to %s", tmpname, e->file->fullname);
120         }
121 }
122
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"
129 };
130 REGISTER_TEST(license_comment);