184b8f5a19310ca2a42814acb8e1ba3013e3139a
[ccan] / tools / ccanlint / ccanlint.c
1 /*
2  * ccanlint: assorted checks and advice for a ccan package
3  * Copyright (C) 2008 Rusty Russell
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
29 static unsigned int verbose = 0;
30 static LIST_HEAD(tests);
31 static LIST_HEAD(finished_tests);
32
33 static void usage(const char *name)
34 {
35         fprintf(stderr, "Usage: %s [-s] [-v] [-d <dirname>]\n"
36                 "   -v: verbose mode\n"
37                 "   -s: simply give one line per FAIL and total score\n"
38                 "   -d: use this directory instead of the current one\n",
39                 name);
40         exit(1);
41 }
42
43 static void indent_print(const char *string)
44 {
45         while (*string) {
46                 unsigned int line = strcspn(string, "\n");
47                 printf("\t%.*s", line, string);
48                 if (string[line] == '\n') {
49                         printf("\n");
50                         line++;
51                 }
52                 string += line;
53         }
54 }
55
56 bool ask(const char *question)
57 {
58         char reply[2];
59
60         printf("%s ", question);
61         fflush(stdout);
62
63         return fgets(reply, sizeof(reply), stdin) != NULL
64                 && toupper(reply[0]) == 'Y';
65 }
66
67 static bool run_test(const struct ccanlint *i,
68                      bool summary,
69                      unsigned int *score,
70                      unsigned int *total_score,
71                      struct manifest *m)
72 {
73         void *result;
74         unsigned int this_score;
75
76         if (i->total_score)
77                 *total_score += i->total_score;
78
79         result = i->check(m);
80         if (!result) {
81                 if (verbose)
82                         printf("  %s: OK\n", i->name);
83                 if (i->total_score)
84                         *score += i->total_score;
85                 return true;
86         }
87
88         if (i->score)
89                 this_score = i->score(m, result);
90         else
91                 this_score = 0;
92
93         *score += this_score;
94         if (summary) {
95                 printf("%s FAILED (%u/%u)\n",
96                        i->name, this_score, i->total_score);
97
98                 if (verbose)
99                         indent_print(i->describe(m, result));
100                 return false;
101         }
102
103         printf("%s\n", i->describe(m, result));
104
105         if (i->handle)
106                 i->handle(m, result);
107
108         return false;
109 }
110
111 static void register_test(struct ccanlint *test, ...)
112 {
113         va_list ap;
114         struct ccanlint *depends; 
115         struct dependent *dchild;
116
117         list_add(&tests, &test->list);
118         va_start(ap, test);
119         /* Careful: we might have been initialized by a dependent. */
120         if (test->dependencies.n.next == NULL)
121                 list_head_init(&test->dependencies);
122
123         //dependant(s) args (if any), last one is NULL
124         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
125                 dchild = malloc(sizeof(*dchild));
126                 dchild->dependent = test;
127                 /* The thing we depend on might not be initialized yet! */
128                 if (depends->dependencies.n.next == NULL)
129                         list_head_init(&depends->dependencies);
130                 list_add_tail(&depends->dependencies, &dchild->node);
131                 test->num_depends++;
132         }
133         va_end(ap);
134 }
135
136 static void init_tests(void)
137 {
138         const struct ccanlint *i;
139
140 #undef REGISTER_TEST
141 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
142 #include "generated-init-tests"
143
144         if (!verbose)
145                 return;
146
147         list_for_each(&tests, i, list) {
148                 printf("%s depends on %u others\n", i->name, i->num_depends);
149                 if (!list_empty(&i->dependencies)) {
150                         const struct dependent *d;
151                         printf("These depend on us:\n");
152                         list_for_each(&i->dependencies, d, node)
153                                 printf("\t%s\n", d->dependent->name);
154                 }
155         }
156 }
157
158 int main(int argc, char *argv[])
159 {
160         int c;
161         bool summary = false;
162         unsigned int score, total_score;
163         struct manifest *m;
164         const struct ccanlint *i;
165
166         /* I'd love to use long options, but that's not standard. */
167         /* FIXME: getopt_long ccan package? */
168         while ((c = getopt(argc, argv, "sd:v")) != -1) {
169                 switch (c) {
170                 case 'd':
171                         if (chdir(optarg) != 0)
172                                 err(1, "Changing into directory '%s'", optarg);
173                         break;
174                 case 's':
175                         summary = true;
176                         break;
177                 case 'v':
178                         verbose++;
179                         break;
180                 default:
181                         usage(argv[0]);
182                 }
183         }
184
185         if (optind < argc)
186                 usage(argv[0]);
187
188         m = get_manifest();
189
190         init_tests();
191
192         /* If you don't pass the compulsory tests, you don't even get a score */
193         if (verbose)
194                 printf("Compulsory tests:\n");
195         list_for_each(&tests, i, list)
196                 if (!i->total_score)
197                         if (!run_test(i, summary, &score, &total_score, m))
198                                 exit(1);
199
200         if (verbose)
201                 printf("\nNormal tests:\n");
202         score = total_score = 0;
203         list_for_each(&tests, i, list)
204                 if (i->total_score)
205                         run_test(i, summary, &score, &total_score, m);
206
207         printf("Total score: %u/%u\n", score, total_score);
208
209         return 0;
210 }