Another Joey fix:
[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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <err.h>
26 #include <ctype.h>
27
28 static unsigned int verbose = 0;
29 static LIST_HEAD(tests);
30
31 static void init_tests(void)
32 {
33 #include "generated-init-tests" 
34 }
35
36 static void usage(const char *name)
37 {
38         fprintf(stderr, "Usage: %s [-s] [-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                 name);
43         exit(1);
44 }
45
46 static void indent_print(const char *string)
47 {
48         while (*string) {
49                 unsigned int line = strcspn(string, "\n");
50                 printf("\t%.*s", line, string);
51                 if (string[line] == '\n') {
52                         printf("\n");
53                         line++;
54                 }
55                 string += line;
56         }
57 }
58
59 bool ask(const char *question)
60 {
61         char reply[2];
62
63         printf("%s ", question);
64         fflush(stdout);
65
66         return fgets(reply, sizeof(reply), stdin) != NULL
67                 && toupper(reply[0]) == 'Y';
68 }
69
70 static bool run_test(const struct ccanlint *i,
71                      bool summary,
72                      unsigned int *score,
73                      unsigned int *total_score,
74                      struct manifest *m)
75 {
76         void *result;
77         unsigned int this_score;
78
79         if (i->total_score)
80                 *total_score += i->total_score;
81
82         result = i->check(m);
83         if (!result) {
84                 if (verbose)
85                         printf("  %s: OK\n", i->name);
86                 if (i->total_score)
87                         *score += i->total_score;
88                 return true;
89         }
90
91         if (i->score)
92                 this_score = i->score(m, result);
93         else
94                 this_score = 0;
95
96         *score += this_score;
97         if (summary) {
98                 printf("%s FAILED (%u/%u)\n",
99                        i->name, this_score, i->total_score);
100
101                 if (verbose)
102                         indent_print(i->describe(m, result));
103                 return false;
104         }
105
106         printf("%s\n", i->describe(m, result));
107
108         if (i->handle)
109                 i->handle(m, result);
110
111         return false;
112 }
113
114 int main(int argc, char *argv[])
115 {
116         int c;
117         bool summary = false;
118         unsigned int score, total_score;
119         struct manifest *m;
120         const struct ccanlint *i;
121
122         /* I'd love to use long options, but that's not standard. */
123         /* FIXME: getopt_long ccan package? */
124         while ((c = getopt(argc, argv, "sd:v")) != -1) {
125                 switch (c) {
126                 case 'd':
127                         if (chdir(optarg) != 0)
128                                 err(1, "Changing into directory '%s'", optarg);
129                         break;
130                 case 's':
131                         summary = true;
132                         break;
133                 case 'v':
134                         verbose++;
135                         break;
136                 default:
137                         usage(argv[0]);
138                 }
139         }
140
141         if (optind < argc)
142                 usage(argv[0]);
143
144         m = get_manifest();
145
146         init_tests();
147
148         /* If you don't pass the compulsory tests, you don't even get a score */
149         if (verbose)
150                 printf("Compulsory tests:\n");
151         list_for_each(&tests, i, list)
152                 if (!i->total_score)
153                         if (!run_test(i, summary, &score, &total_score, m))
154                                 exit(1);
155
156         if (verbose)
157                 printf("\nNormal tests:\n");
158         score = total_score = 0;
159         list_for_each(&tests, i, list)
160                 if (i->total_score)
161                         run_test(i, summary, &score, &total_score, m);
162
163         printf("Total score: %u/%u\n", score, total_score);
164
165         return 0;
166 }