9b252735f8187c3f2c3fb554bccf160f61f2a3bc
[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         *score += this_score;
104         if (summary) {
105                 printf("%s FAILED (%u/%u)\n",
106                        i->name, this_score, i->total_score);
107
108                 if (verbose)
109                         indent_print(i->describe(m, result));
110                 list_del(&i->list);
111                 list_add_tail(&finished_tests, &i->list);
112                 return false;
113         }
114
115         printf("%s\n", i->describe(m, result));
116
117         if (i->handle)
118                 i->handle(m, result);
119         list_del(&i->list);
120         list_add_tail(&finished_tests, &i->list);
121         return false;
122 }
123
124 static void register_test(struct ccanlint *test, ...)
125 {
126         va_list ap;
127         struct ccanlint *depends;
128         struct dependent *dchild;
129
130         if (!test->total_score)
131                 list_add(&compulsory_tests, &test->list);
132         else
133                 list_add(&normal_tests, &test->list);
134
135         va_start(ap, test);
136         /* Careful: we might have been initialized by a dependent. */
137         if (test->dependencies.n.next == NULL)
138                 list_head_init(&test->dependencies);
139
140         //dependent(s) args (if any), last one is NULL
141         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
142                 dchild = malloc(sizeof(*dchild));
143                 dchild->dependent = test;
144                 /* The thing we depend on might not be initialized yet! */
145                 if (depends->dependencies.n.next == NULL)
146                         list_head_init(&depends->dependencies);
147                 list_add_tail(&depends->dependencies, &dchild->node);
148                 test->num_depends++;
149         }
150         va_end(ap);
151 }
152
153 /**
154  * get_next_test - retrieves the next test to be processed
155  **/
156 static inline struct ccanlint *get_next_test(struct list_head *test)
157 {
158         struct ccanlint *i;
159
160         if (list_empty(test))
161                 return NULL;
162
163         list_for_each(test, i, list) {
164                 if (i->num_depends == 0)
165                         return i;
166         }
167         errx(1, "Can't make process; test dependency cycle");
168 }
169
170 static void init_tests(void)
171 {
172         const struct ccanlint *i;
173
174 #undef REGISTER_TEST
175 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
176 #include "generated-init-tests"
177
178         if (!verbose)
179                 return;
180
181         printf("\nCompulsory Tests\n");
182         list_for_each(&compulsory_tests, i, list) {
183                 printf("%s depends on %u others\n", i->name, i->num_depends);
184                 if (!list_empty(&i->dependencies)) {
185                         const struct dependent *d;
186                         printf("These depend on us:\n");
187                         list_for_each(&i->dependencies, d, node)
188                                 printf("\t%s\n", d->dependent->name);
189                 }
190         }
191
192         printf("\nNormal Tests\n");
193         list_for_each(&normal_tests, i, list) {
194                 printf("%s depends on %u others\n", i->name, i->num_depends);
195                 if (!list_empty(&i->dependencies)) {
196                         const struct dependent *d;
197                         printf("These depend on us:\n");
198                         list_for_each(&i->dependencies, d, node)
199                                 printf("\t%s\n", d->dependent->name);
200                 }
201         }
202 }
203
204 int main(int argc, char *argv[])
205 {
206         int c;
207         bool summary = false;
208         unsigned int score, total_score;
209         struct manifest *m;
210         struct ccanlint *i;
211
212         /* I'd love to use long options, but that's not standard. */
213         /* FIXME: getopt_long ccan package? */
214         while ((c = getopt(argc, argv, "sd:vn")) != -1) {
215                 switch (c) {
216                 case 'd':
217                         if (chdir(optarg) != 0)
218                                 err(1, "Changing into directory '%s'", optarg);
219                         break;
220                 case 's':
221                         summary = true;
222                         break;
223                 case 'v':
224                         verbose++;
225                         break;
226                 case 'n':
227                         safe_mode = true;
228                         break;
229                 default:
230                         usage(argv[0]);
231                 }
232         }
233
234         if (optind < argc)
235                 usage(argv[0]);
236
237         m = get_manifest(talloc_autofree_context());
238
239         init_tests();
240
241         /* If you don't pass the compulsory tests, you don't even get a score */
242         if (verbose)
243                 printf("Compulsory tests:\n");
244
245         while ((i = get_next_test(&compulsory_tests)) != NULL) {
246                 if (!run_test(i, summary, &score, &total_score, m))
247                         exit(1);
248         }
249
250         if (verbose)
251                 printf("\nNormal tests:\n");
252         score = total_score = 0;
253         while ((i = get_next_test(&normal_tests)) != NULL) {
254                 if (i->total_score)
255                         run_test(i, summary, &score, &total_score, m);
256         }
257         printf("Total score: %u/%u\n", score, total_score);
258         return 0;
259 }