]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/license_comment.c
ccanlint: offer to insert license comments where they're missing.
[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 void check_license_comment(struct manifest *m,
15                                   unsigned int *timeleft, struct score *score)
16 {
17         struct list_head *list;
18
19         /* No requirements on public domain. */
20         if (m->license == LICENSE_PUBLIC_DOMAIN
21             || m->license == LICENSE_UNKNOWN) {
22                 score->pass = true;
23                 score->score = score->total;
24                 return;
25         }
26
27         foreach_ptr(list, &m->c_files, &m->h_files) {
28                 struct ccan_file *f;
29
30                 list_for_each(list, f, list) {
31                         unsigned int i;
32                         char **lines = get_ccan_file_lines(f);
33                         struct line_info *info = get_ccan_line_info(f);
34                         bool found_license = false, found_flavor = false;
35
36                         for (i = 0; lines[i]; i++) {
37                                 if (info[i].type == CODE_LINE)
38                                         break;
39                                 if (strstr(lines[i], "LICENSE"))
40                                         found_license = true;
41                                 if (strstr(lines[i],
42                                            licenses[m->license].shortname))
43                                         found_flavor = true;
44                         }
45                         if ((!found_license || !found_flavor)
46                             && !find_boilerplate(f, m->license)) {
47                                 score_file_error(score, f, lines[i] ? i : 0,
48                                                  "No reference to license"
49                                                  " found");
50                         }
51                 }
52         }
53
54         if (list_empty(&score->per_file_errors)) {
55                 score->pass = true;
56                 score->score = score->total;
57         }
58 }
59
60 static void add_license_comment(struct manifest *m, struct score *score)
61 {
62         struct file_error *e;
63         const char *license_desc = get_license_oneliner(score, m->license);
64         char *files = tal_strdup(score, ""), *q;
65
66         list_for_each(&score->per_file_errors, e, list)
67                 tal_append_fmt(&files, "  %s\n", e->file->name);
68
69         q = tal_fmt(score, "The following files don't have a comment:\n%s\n"
70                     "Should I prepend '%s'?", files, license_desc);
71         if (!ask(q))
72                 return;
73
74         list_for_each(&score->per_file_errors, e, list) {
75                 char *tmpname;
76                 FILE *out;
77                 unsigned int i;
78
79                 tmpname = temp_file(score, ".licensed", e->file->name);
80                 out = fopen(tmpname, "w");
81                 if (!out)
82                         err(1, "Opening %s", tmpname);
83                 if (fprintf(out, "%s\n", license_desc) < 0)
84                         err(1, "Writing %s", tmpname);
85
86                 for (i = 0; e->file->lines[i]; i++)
87                         if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
88                                 err(1, "Writing %s", tmpname);
89
90                 if (fclose(out) != 0)
91                         err(1, "Closing %s", tmpname);
92
93                 if (!move_file(tmpname, e->file->fullname))
94                         err(1, "Moving %s to %s", tmpname, e->file->fullname);
95         }
96 }
97
98 struct ccanlint license_comment = {
99         .key = "license_comment",
100         .name = "Source and header files refer to LICENSE",
101         .check = check_license_comment,
102         .handle = add_license_comment,
103         .needs = "license_exists"
104 };
105 REGISTER_TEST(license_comment);