2 #include "get_file_lines.h"
3 #include <talloc/talloc.h>
4 #include <string/string.h>
5 #include <noerr/noerr.h>
14 char **get_ccan_file_lines(struct ccan_file *f)
17 char *buffer = grab_file(f, f->name, NULL);
19 err(1, "Getting file %s", f->name);
20 f->lines = strsplit(f, buffer, "\n", &f->num_lines);
25 static void add_files(struct manifest *m, const char *dir)
35 err(1, "Opening directory %s", dir[0] ? dir : ".");
37 while ((ent = readdir(d)) != NULL) {
40 struct list_head *dest;
43 if (ent->d_name[0] == '.')
46 f = talloc(m, struct ccan_file);
48 f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
49 if (lstat(f->name, &st) != 0)
50 err(1, "lstat %s", f->name);
52 if (S_ISDIR(st.st_mode)) {
53 f->name = talloc_append_string(f->name, "/");
54 add_files(m, f->name);
57 if (!S_ISREG(st.st_mode)) {
62 if (streq(f->name, "_info.c")) {
67 is_c_src = strends(f->name, ".c");
68 if (!is_c_src && !strends(f->name, ".h"))
69 dest = &m->other_files;
70 else if (!strchr(f->name, '/')) {
75 } else if (strstarts(f->name, "test/")) {
77 if (strstarts(f->name, "test/run"))
79 else if (strstarts(f->name, "test/compile_ok"))
80 dest = &m->compile_ok_tests;
81 else if (strstarts(f->name, "test/compile_fail"))
82 dest = &m->compile_fail_tests;
84 dest = &m->other_test_files;
86 dest = &m->other_test_files;
88 dest = &m->other_files;
90 list_add(dest, &f->list);
95 char *report_on_lines(struct list_head *files,
96 char *(*report)(const char *),
101 list_for_each(files, f, list) {
103 char **lines = get_ccan_file_lines(f);
105 for (i = 0; i < f->num_lines; i++) {
106 char *r = report(lines[i]);
110 sofar = talloc_asprintf_append(sofar,
119 struct manifest *get_manifest(void)
121 struct manifest *m = talloc(NULL, struct manifest);
125 list_head_init(&m->c_files);
126 list_head_init(&m->h_files);
127 list_head_init(&m->run_tests);
128 list_head_init(&m->compile_ok_tests);
129 list_head_init(&m->compile_fail_tests);
130 list_head_init(&m->other_test_files);
131 list_head_init(&m->other_files);
133 /* *This* is why people hate C. */
135 m->basename = talloc_array(m, char, len);
136 while (!getcwd(m->basename, len)) {
138 err(1, "Getting current directory");
139 m->basename = talloc_realloc(m, m->basename, char, len *= 2);
142 len = strlen(m->basename);
143 while (len && m->basename[len-1] == '/')
144 m->basename[--len] = '\0';
146 m->basename = strrchr(m->basename, '/');
148 errx(1, "I don't expect to be run from the root directory");