+ if (streq(testname, "all")) {
+ struct list_head *list;
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, i, list)
+ i->keep_results = true;
+ }
+ } else {
+ i = find_test(testname);
+ if (!i)
+ errx(1, "No test %s to --keep", testname);
+ i->keep_results = true;
+ }
+
+ /* Don't automatically destroy temporary dir. */
+ talloc_set_destructor(temp_dir(NULL), show_tmpdir);
+ return NULL;
+}
+
+static char *skip_test(const char *testname, void *unused)
+{
+ btree_insert(cmdline_exclude, testname);
+ return NULL;
+}
+
+static void print_tests(struct list_head *tests, const char *type)
+{
+ struct ccanlint *i;
+
+ printf("%s tests:\n", type);
+ /* This makes them print in topological order. */
+ while ((i = get_next_test(tests)) != NULL) {
+ const struct dependent *d;
+ printf(" %-25s %s\n", i->key, i->name);
+ list_del(&i->list);
+ list_for_each(&i->dependencies, d, node)
+ d->dependent->num_depends--;
+ }
+}
+
+static char *list_tests(void *arg)
+{
+ print_tests(&compulsory_tests, "Compulsory");
+ print_tests(&normal_tests, "Normal");
+ exit(0);
+}
+
+static void test_dgraph_vertices(struct list_head *tests, const char *style)
+{
+ const struct ccanlint *i;
+
+ list_for_each(tests, i, list) {
+ /*
+ * todo: escape labels in case ccanlint test keys have
+ * characters interpreted as GraphViz syntax.
+ */
+ printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
+ }
+}
+
+static void test_dgraph_edges(struct list_head *tests)
+{
+ const struct ccanlint *i;
+ const struct dependent *d;
+
+ list_for_each(tests, i, list)
+ list_for_each(&i->dependencies, d, node)
+ printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
+}
+
+static char *test_dependency_graph(void *arg)
+{
+ puts("digraph G {");
+
+ test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
+ test_dgraph_vertices(&normal_tests, "");
+
+ test_dgraph_edges(&compulsory_tests);
+ test_dgraph_edges(&normal_tests);
+
+ puts("}");
+
+ exit(0);
+}
+
+/* Remove empty lines. */
+static char **collapse(char **lines, unsigned int *nump)
+{
+ unsigned int i, j;
+ for (i = j = 0; lines[i]; i++) {
+ if (lines[i][0])
+ lines[j++] = lines[i];
+ }
+ lines[j] = NULL;
+ if (nump)
+ *nump = j;
+ return lines;
+}
+
+
+static void add_options(struct ccanlint *test, char **options,
+ unsigned int num_options)
+{
+ unsigned int num;
+
+ if (!test->options)
+ num = 0;
+ else
+ /* -1, because last one is NULL. */
+ num = talloc_array_length(test->options) - 1;
+
+ test->options = talloc_realloc(NULL, test->options,
+ char *,
+ num + num_options + 1);
+ memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
+}
+
+static void add_info_options(struct ccan_file *info, bool mark_fails)
+{
+ struct doc_section *d;
+ unsigned int i;
+ struct ccanlint *test;
+
+ list_for_each(get_ccan_file_docs(info), d, list) {
+ if (!streq(d->type, "ccanlint"))
+ continue;
+
+ for (i = 0; i < d->num_lines; i++) {
+ unsigned int num_words;
+ char **words = collapse(strsplit(d, d->lines[i], " \t"),
+ &num_words);
+ if (num_words == 0)
+ continue;
+
+ if (strncmp(words[0], "//", 2) == 0)
+ continue;
+
+ test = find_test(words[0]);
+ if (!test) {
+ warnx("%s: unknown ccanlint test '%s'",
+ info->fullname, words[0]);
+ continue;
+ }
+
+ if (!words[1]) {
+ warnx("%s: no argument to test '%s'",
+ info->fullname, words[0]);
+ continue;
+ }
+
+ /* Known failure? */
+ if (strcasecmp(words[1], "FAIL") == 0) {
+ if (mark_fails)
+ btree_insert(info_exclude, words[0]);
+ } else {
+ if (!test->takes_options)
+ warnx("%s: %s doesn't take options",
+ info->fullname, words[0]);
+ add_options(test, words+1, num_words-1);
+ }
+ }
+ }
+}
+
+/* If options are of form "filename:<option>" they only apply to that file */
+char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
+{
+ char **ret;
+ unsigned int i, j = 0;
+
+ /* Fast path. */
+ if (!test->options[0])
+ return test->options;
+
+ ret = talloc_array(f, char *, talloc_array_length(test->options));
+ for (i = 0; test->options[i]; i++) {
+ char *optname;
+
+ if (!test->options[i] || !strchr(test->options[i], ':')) {
+ optname = test->options[i];
+ } else if (strstarts(test->options[i], f->name)
+ && test->options[i][strlen(f->name)] == ':') {
+ optname = test->options[i] + strlen(f->name) + 1;
+ } else
+ continue;
+
+ /* FAIL overrides anything else. */
+ if (streq(optname, "FAIL")) {
+ ret = talloc_array(f, char *, 2);
+ ret[0] = (char *)"FAIL";
+ ret[1] = NULL;
+ return ret;
+ }
+ ret[j++] = optname;
+ }
+ ret[j] = NULL;
+
+ /* Shrink it to size so talloc_array_length() works as expected. */
+ return talloc_realloc(NULL, ret, char *, j + 1);
+}
+
+static bool depends_on(struct ccanlint *i, struct ccanlint *target)
+{
+ const struct dependent *d;
+
+ if (i == target)
+ return true;
+
+ list_for_each(&i->dependencies, d, node) {
+ if (depends_on(d->dependent, target))
+ return true;
+ }
+ return false;
+}
+
+/* O(N^2), who cares? */
+static void skip_unrelated_tests(struct ccanlint *target)
+{
+ struct ccanlint *i;
+ struct list_head *list;
+
+ foreach_ptr(list, &compulsory_tests, &normal_tests)
+ list_for_each(list, i, list)
+ if (!depends_on(i, target))
+ i->skip = "not relevant to target";
+}
+
+static char *demangle_string(char *string)
+{
+ unsigned int i;
+ const char mapfrom[] = "abfnrtv";
+ const char mapto[] = "\a\b\f\n\r\t\v";
+
+ if (!strchr(string, '"'))
+ return NULL;
+ string = strchr(string, '"') + 1;
+ if (!strrchr(string, '"'))
+ return NULL;
+ *strrchr(string, '"') = '\0';
+
+ for (i = 0; i < strlen(string); i++) {
+ if (string[i] == '\\') {
+ char repl;
+ unsigned len = 0;
+ const char *p = strchr(mapfrom, string[i+1]);
+ if (p) {
+ repl = mapto[p - mapfrom];
+ len = 1;
+ } else if (strlen(string+i+1) >= 3) {
+ if (string[i+1] == 'x') {
+ repl = (string[i+2]-'0')*16
+ + string[i+3]-'0';
+ len = 3;
+ } else if (cisdigit(string[i+1])) {
+ repl = (string[i+2]-'0')*8*8
+ + (string[i+3]-'0')*8
+ + (string[i+4]-'0');
+ len = 3;
+ }
+ }
+ if (len == 0) {
+ repl = string[i+1];
+ len = 1;
+ }
+
+ string[i] = repl;
+ memmove(string + i + 1, string + i + len + 1,
+ strlen(string + i + len + 1) + 1);