2 * ccanlint: assorted checks and advice for a ccan package
3 * Copyright (C) 2008 Rusty Russell
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)
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
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.
28 static unsigned int verbose = 0;
29 static LIST_HEAD(tests);
31 static void init_tests(void)
33 #include "generated-init-tests"
36 static void usage(const char *name)
38 fprintf(stderr, "Usage: %s [-s] [-v] [-d <dirname>]\n"
40 " -s: simply give one line per FAIL and total score\n"
41 " -d: use this directory instead of the current one\n",
46 static void indent_print(const char *string)
49 unsigned int line = strcspn(string, "\n");
50 printf("\t%.*s", line, string);
51 if (string[line] == '\n') {
59 bool ask(const char *question)
63 printf("%s ", question);
66 return fgets(reply, sizeof(reply), stdin) != NULL
67 && toupper(reply[0]) == 'Y';
70 static bool run_test(const struct ccanlint *i,
73 unsigned int *total_score,
77 unsigned int this_score;
80 *total_score += i->total_score;
85 printf(" %s: OK\n", i->name);
87 *score += i->total_score;
92 this_score = i->score(m, result);
98 printf("%s FAILED (%u/%u)\n",
99 i->name, this_score, i->total_score);
102 indent_print(i->describe(m, result));
106 printf("%s\n", i->describe(m, result));
109 i->handle(m, result);
114 int main(int argc, char *argv[])
117 bool summary = false;
118 unsigned int score, total_score;
120 const struct ccanlint *i;
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) {
127 if (chdir(optarg) != 0)
128 err(1, "Changing into directory '%s'", optarg);
148 /* If you don't pass the compulsory tests, you don't even get a score */
150 printf("Compulsory tests:\n");
151 list_for_each(&tests, i, list)
153 if (!run_test(i, summary, &score, &total_score, m))
157 printf("\nNormal tests:\n");
158 score = total_score = 0;
159 list_for_each(&tests, i, list)
161 run_test(i, summary, &score, &total_score, m);
163 printf("Total score: %u/%u\n", score, total_score);