ccanlint.c 5.9 KB

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