ccanlint.c 4.8 KB

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