2 #include "get_file_lines.h"
3 #include <talloc/talloc.h>
5 #include <str_talloc/str_talloc.h>
6 #include <grab_file/grab_file.h>
7 #include <noerr/noerr.h>
16 char **get_ccan_file_lines(struct ccan_file *f)
19 char *buffer = grab_file(f, f->name, NULL);
21 err(1, "Getting file %s", f->name);
22 f->lines = strsplit(f, buffer, "\n", &f->num_lines);
27 static void add_files(struct manifest *m, const char *dir)
37 err(1, "Opening directory %s", dir[0] ? dir : ".");
39 while ((ent = readdir(d)) != NULL) {
42 struct list_head *dest;
45 if (ent->d_name[0] == '.')
48 f = talloc(m, struct ccan_file);
50 f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
51 if (lstat(f->name, &st) != 0)
52 err(1, "lstat %s", f->name);
54 if (S_ISDIR(st.st_mode)) {
55 f->name = talloc_append_string(f->name, "/");
56 add_files(m, f->name);
59 if (!S_ISREG(st.st_mode)) {
64 if (streq(f->name, "_info.c")) {
69 is_c_src = strends(f->name, ".c");
70 if (!is_c_src && !strends(f->name, ".h"))
71 dest = &m->other_files;
72 else if (!strchr(f->name, '/')) {
77 } else if (strstarts(f->name, "test/")) {
79 if (strstarts(f->name, "test/run"))
81 else if (strstarts(f->name, "test/compile_ok"))
82 dest = &m->compile_ok_tests;
83 else if (strstarts(f->name, "test/compile_fail"))
84 dest = &m->compile_fail_tests;
86 dest = &m->other_test_files;
88 dest = &m->other_test_files;
90 dest = &m->other_files;
92 list_add(dest, &f->list);
97 char *report_on_lines(struct list_head *files,
98 char *(*report)(const char *),
103 list_for_each(files, f, list) {
105 char **lines = get_ccan_file_lines(f);
107 for (i = 0; i < f->num_lines; i++) {
108 char *r = report(lines[i]);
112 sofar = talloc_asprintf_append(sofar,
121 struct manifest *get_manifest(void)
123 struct manifest *m = talloc(NULL, struct manifest);
127 list_head_init(&m->c_files);
128 list_head_init(&m->h_files);
129 list_head_init(&m->run_tests);
130 list_head_init(&m->compile_ok_tests);
131 list_head_init(&m->compile_fail_tests);
132 list_head_init(&m->other_test_files);
133 list_head_init(&m->other_files);
135 /* *This* is why people hate C. */
137 m->basename = talloc_array(m, char, len);
138 while (!getcwd(m->basename, len)) {
140 err(1, "Getting current directory");
141 m->basename = talloc_realloc(m, m->basename, char, len *= 2);
144 len = strlen(m->basename);
145 while (len && m->basename[len-1] == '/')
146 m->basename[--len] = '\0';
148 m->basename = strrchr(m->basename, '/');
150 errx(1, "I don't expect to be run from the root directory");