tdb: use tdb_nest_lock() for active lock.
[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 "../tools.h"
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <err.h>
28 #include <ctype.h>
29 #include <ccan/talloc/talloc.h>
30
31 static unsigned int verbose = 0;
32 static LIST_HEAD(compulsory_tests);
33 static LIST_HEAD(normal_tests);
34 static LIST_HEAD(finished_tests);
35 bool safe_mode = false;
36
37 static void usage(const char *name)
38 {
39         fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
40                 "   -v: verbose mode\n"
41                 "   -s: simply give one line summary\n"
42                 "   -d: use this directory instead of the current one\n"
43                 "   -n: do not compile anything\n",
44                 name);
45         exit(1);
46 }
47
48 #if 0
49 static void indent_print(const char *string)
50 {
51         while (*string) {
52                 unsigned int line = strcspn(string, "\n");
53                 printf("\t%.*s", line, string);
54                 if (string[line] == '\n') {
55                         printf("\n");
56                         line++;
57                 }
58                 string += line;
59         }
60 }
61 #endif
62
63 bool ask(const char *question)
64 {
65         char reply[2];
66
67         printf("%s ", question);
68         fflush(stdout);
69
70         return fgets(reply, sizeof(reply), stdin) != NULL
71                 && toupper(reply[0]) == 'Y';
72 }
73
74 static const char *should_skip(struct manifest *m, struct ccanlint *i)
75 {
76         if (i->skip_fail)
77                 return "dependency failed";
78
79         if (i->skip)
80                 return "dependency was skipped";
81
82         if (i->can_run)
83                 return i->can_run(m);
84         return NULL;
85 }
86
87 static bool run_test(struct ccanlint *i,
88                      bool quiet,
89                      unsigned int *score,
90                      unsigned int *total_score,
91                      struct manifest *m)
92 {
93         void *result;
94         unsigned int this_score;
95         const struct dependent *d;
96         const char *skip;
97
98         //one less test to run through
99         list_for_each(&i->dependencies, d, node)
100                 d->dependent->num_depends--;
101
102         skip = should_skip(m, i);
103         if (skip) {
104                 if (verbose)
105                         printf("  %s: skipped (%s)\n", i->name, skip);
106
107                 /* If we're skipping this because a prereq failed, we fail. */
108                 if (i->skip_fail)
109                         *total_score += i->total_score;
110                         
111                 list_del(&i->list);
112                 list_add_tail(&finished_tests, &i->list);
113                 list_for_each(&i->dependencies, d, node) {
114                         d->dependent->skip = true;
115                         d->dependent->skip_fail = i->skip_fail;
116                 }
117                 return true;
118         }
119
120         result = i->check(m);
121         if (!result) {
122                 if (verbose) {
123                         printf("  %s: OK", i->name);
124                         if (i->total_score)
125                                 printf(" (+%u/%u)",
126                                        i->total_score, i->total_score);
127                         printf("\n");
128                 }
129                 if (i->total_score) {
130                         *score += i->total_score;
131                         *total_score += i->total_score;
132                 }
133
134                 list_del(&i->list);
135                 list_add_tail(&finished_tests, &i->list);
136                 return true;
137         }
138
139         if (i->score)
140                 this_score = i->score(m, result);
141         else
142                 this_score = 0;
143
144         list_del(&i->list);
145         list_add_tail(&finished_tests, &i->list);
146
147         *total_score += i->total_score;
148         *score += this_score;
149         if (verbose) {
150                 printf("  %s: FAIL (+%u/%u)\n",
151                        i->name, this_score, i->total_score);
152         }
153         if (!quiet) {
154                 printf("%s\n", i->describe(m, result));
155
156                 if (i->handle)
157                         i->handle(m, result);
158         }
159
160         /* Skip any tests which depend on this one. */
161         list_for_each(&i->dependencies, d, node) {
162                 d->dependent->skip = true;
163                 d->dependent->skip_fail = true;
164         }
165
166         return false;
167 }
168
169 static void register_test(struct list_head *h, struct ccanlint *test, ...)
170 {
171         va_list ap;
172         struct ccanlint *depends;
173         struct dependent *dchild;
174
175         list_add(h, &test->list);
176
177         va_start(ap, test);
178         /* Careful: we might have been initialized by a dependent. */
179         if (test->dependencies.n.next == NULL)
180                 list_head_init(&test->dependencies);
181
182         //dependent(s) args (if any), last one is NULL
183         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
184                 dchild = malloc(sizeof(*dchild));
185                 dchild->dependent = test;
186                 /* The thing we depend on might not be initialized yet! */
187                 if (depends->dependencies.n.next == NULL)
188                         list_head_init(&depends->dependencies);
189                 list_add_tail(&depends->dependencies, &dchild->node);
190                 test->num_depends++;
191         }
192         va_end(ap);
193 }
194
195 /**
196  * get_next_test - retrieves the next test to be processed
197  **/
198 static inline struct ccanlint *get_next_test(struct list_head *test)
199 {
200         struct ccanlint *i;
201
202         if (list_empty(test))
203                 return NULL;
204
205         list_for_each(test, i, list) {
206                 if (i->num_depends == 0)
207                         return i;
208         }
209         errx(1, "Can't make process; test dependency cycle");
210 }
211
212 static void init_tests(void)
213 {
214         const struct ccanlint *i;
215
216 #undef REGISTER_TEST
217 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
218 #include "generated-normal-tests"
219 #undef REGISTER_TEST
220 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
221 #include "generated-compulsory-tests"
222
223         if (!verbose)
224                 return;
225
226         printf("\nCompulsory Tests\n");
227         list_for_each(&compulsory_tests, i, list) {
228                 printf("%s depends on %u others\n", i->name, i->num_depends);
229                 if (!list_empty(&i->dependencies)) {
230                         const struct dependent *d;
231                         printf("These depend on us:\n");
232                         list_for_each(&i->dependencies, d, node)
233                                 printf("\t%s\n", d->dependent->name);
234                 }
235         }
236
237         printf("\nNormal Tests\n");
238         list_for_each(&normal_tests, i, list) {
239                 printf("%s depends on %u others\n", i->name, i->num_depends);
240                 if (!list_empty(&i->dependencies)) {
241                         const struct dependent *d;
242                         printf("These depend on us:\n");
243                         list_for_each(&i->dependencies, d, node)
244                                 printf("\t%s\n", d->dependent->name);
245                 }
246         }
247 }
248
249 int main(int argc, char *argv[])
250 {
251         int c;
252         bool summary = false;
253         unsigned int score = 0, total_score = 0;
254         struct manifest *m;
255         struct ccanlint *i;
256         const char *prefix = "", *dir = ".";
257
258         /* I'd love to use long options, but that's not standard. */
259         /* FIXME: getopt_long ccan package? */
260         while ((c = getopt(argc, argv, "sd:vn")) != -1) {
261                 switch (c) {
262                 case 'd':
263                         dir = optarg;
264                         prefix = talloc_append_string(talloc_basename(NULL,
265                                                                       optarg),
266                                                       ": ");
267                         break;
268                 case 's':
269                         summary = true;
270                         break;
271                 case 'v':
272                         verbose++;
273                         break;
274                 case 'n':
275                         safe_mode = true;
276                         break;
277                 default:
278                         usage(argv[0]);
279                 }
280         }
281
282         if (optind < argc)
283                 usage(argv[0]);
284
285         m = get_manifest(talloc_autofree_context(), dir);
286
287         init_tests();
288
289         /* If you don't pass the compulsory tests, you don't even get a score */
290         if (verbose)
291                 printf("Compulsory tests:\n");
292
293         while ((i = get_next_test(&compulsory_tests)) != NULL) {
294                 if (!run_test(i, summary, &score, &total_score, m)) {
295                         errx(1, "%s%s failed", prefix, i->name);
296                 }
297         }
298
299         if (verbose)
300                 printf("\nNormal tests:\n");
301         while ((i = get_next_test(&normal_tests)) != NULL)
302                 run_test(i, summary, &score, &total_score, m);
303
304         printf("%sTotal score: %u/%u\n", prefix, score, total_score);
305         return 0;
306 }