f88b58dc8e8ba3b416bb10a1469684258782c1bb
[ccan] / tools / ccanlint / ccanlint.c
1 /*
2  * ccanlint: assorted checks and advice for a ccan package
3  * Copyright (C) 2008 Rusty Russell, Idris Soule
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  *   This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #include "ccanlint.h"
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <err.h>
27 #include <ctype.h>
28 #include <ccan/talloc/talloc.h>
29
30 static unsigned int verbose = 0;
31 static LIST_HEAD(compulsory_tests);
32 static LIST_HEAD(normal_tests);
33 static LIST_HEAD(finished_tests);
34 bool safe_mode = false;
35
36 static void usage(const char *name)
37 {
38         fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
39                 "   -v: verbose mode\n"
40                 "   -s: simply give one line per FAIL and total score\n"
41                 "   -d: use this directory instead of the current one\n"
42                 "   -n: do not compile anything\n",
43                 name);
44         exit(1);
45 }
46
47 static void indent_print(const char *string)
48 {
49         while (*string) {
50                 unsigned int line = strcspn(string, "\n");
51                 printf("\t%.*s", line, string);
52                 if (string[line] == '\n') {
53                         printf("\n");
54                         line++;
55                 }
56                 string += line;
57         }
58 }
59
60 bool ask(const char *question)
61 {
62         char reply[2];
63
64         printf("%s ", question);
65         fflush(stdout);
66
67         return fgets(reply, sizeof(reply), stdin) != NULL
68                 && toupper(reply[0]) == 'Y';
69 }
70
71 static bool run_test(struct ccanlint *i,
72                      bool summary,
73                      unsigned int *score,
74                      unsigned int *total_score,
75                      struct manifest *m)
76 {
77         void *result;
78         unsigned int this_score;
79         const struct dependent *d;
80
81         if (i->total_score)
82                 *total_score += i->total_score;
83         //one less test to run through
84         list_for_each(&i->dependencies, d, node)
85                 d->dependent->num_depends--;
86         result = i->check(m);
87         if (!result) {
88                 if (verbose)
89                         printf("  %s: OK\n", i->name);
90                 if (i->total_score)
91                         *score += i->total_score;
92
93                 list_del(&i->list);
94                 list_add_tail(&finished_tests, &i->list);
95                 return true;
96         }
97
98         if (i->score)
99                 this_score = i->score(m, result);
100         else
101                 this_score = 0;
102
103         list_del(&i->list);
104         list_add_tail(&finished_tests, &i->list);
105
106         *score += this_score;
107         if (summary) {
108                 printf("%s FAILED (%u/%u)\n",
109                        i->name, this_score, i->total_score);
110
111                 if (verbose)
112                         indent_print(i->describe(m, result));
113         } else {
114                 printf("%s\n", i->describe(m, result));
115
116                 if (i->handle)
117                         i->handle(m, result);
118         }
119
120         /* Skip any tests which depend on this one. */
121         list_for_each(&i->dependencies, d, node) {
122                 list_del(&d->dependent->list);
123                 list_add(&finished_tests, &d->dependent->list);
124                 if (verbose)
125                         printf("  -> skipping %s\n", d->dependent->name);
126                 *total_score += d->dependent->total_score;
127         }
128
129         return false;
130 }
131
132 static void register_test(struct ccanlint *test, ...)
133 {
134         va_list ap;
135         struct ccanlint *depends;
136         struct dependent *dchild;
137
138         if (!test->total_score)
139                 list_add(&compulsory_tests, &test->list);
140         else
141                 list_add(&normal_tests, &test->list);
142
143         va_start(ap, test);
144         /* Careful: we might have been initialized by a dependent. */
145         if (test->dependencies.n.next == NULL)
146                 list_head_init(&test->dependencies);
147
148         //dependent(s) args (if any), last one is NULL
149         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
150                 dchild = malloc(sizeof(*dchild));
151                 dchild->dependent = test;
152                 /* The thing we depend on might not be initialized yet! */
153                 if (depends->dependencies.n.next == NULL)
154                         list_head_init(&depends->dependencies);
155                 list_add_tail(&depends->dependencies, &dchild->node);
156                 test->num_depends++;
157         }
158         va_end(ap);
159 }
160
161 /**
162  * get_next_test - retrieves the next test to be processed
163  **/
164 static inline struct ccanlint *get_next_test(struct list_head *test)
165 {
166         struct ccanlint *i;
167
168         if (list_empty(test))
169                 return NULL;
170
171         list_for_each(test, i, list) {
172                 if (i->num_depends == 0)
173                         return i;
174         }
175         errx(1, "Can't make process; test dependency cycle");
176 }
177
178 static void init_tests(void)
179 {
180         const struct ccanlint *i;
181
182 #undef REGISTER_TEST
183 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
184 #include "generated-init-tests"
185
186         if (!verbose)
187                 return;
188
189         printf("\nCompulsory Tests\n");
190         list_for_each(&compulsory_tests, i, list) {
191                 printf("%s depends on %u others\n", i->name, i->num_depends);
192                 if (!list_empty(&i->dependencies)) {
193                         const struct dependent *d;
194                         printf("These depend on us:\n");
195                         list_for_each(&i->dependencies, d, node)
196                                 printf("\t%s\n", d->dependent->name);
197                 }
198         }
199
200         printf("\nNormal Tests\n");
201         list_for_each(&normal_tests, i, list) {
202                 printf("%s depends on %u others\n", i->name, i->num_depends);
203                 if (!list_empty(&i->dependencies)) {
204                         const struct dependent *d;
205                         printf("These depend on us:\n");
206                         list_for_each(&i->dependencies, d, node)
207                                 printf("\t%s\n", d->dependent->name);
208                 }
209         }
210 }
211
212 int main(int argc, char *argv[])
213 {
214         int c;
215         bool summary = false;
216         unsigned int score, total_score;
217         struct manifest *m;
218         struct ccanlint *i;
219
220         /* I'd love to use long options, but that's not standard. */
221         /* FIXME: getopt_long ccan package? */
222         while ((c = getopt(argc, argv, "sd:vn")) != -1) {
223                 switch (c) {
224                 case 'd':
225                         if (chdir(optarg) != 0)
226                                 err(1, "Changing into directory '%s'", optarg);
227                         break;
228                 case 's':
229                         summary = true;
230                         break;
231                 case 'v':
232                         verbose++;
233                         break;
234                 case 'n':
235                         safe_mode = true;
236                         break;
237                 default:
238                         usage(argv[0]);
239                 }
240         }
241
242         if (optind < argc)
243                 usage(argv[0]);
244
245         m = get_manifest(talloc_autofree_context());
246
247         init_tests();
248
249         /* If you don't pass the compulsory tests, you don't even get a score */
250         if (verbose)
251                 printf("Compulsory tests:\n");
252
253         while ((i = get_next_test(&compulsory_tests)) != NULL) {
254                 if (!run_test(i, summary, &score, &total_score, m))
255                         exit(1);
256         }
257
258         if (verbose)
259                 printf("\nNormal tests:\n");
260         score = total_score = 0;
261         while ((i = get_next_test(&normal_tests)) != NULL) {
262                 if (i->total_score)
263                         run_test(i, summary, &score, &total_score, m);
264         }
265         printf("Total score: %u/%u\n", score, total_score);
266         return 0;
267 }