Removed grab_file.c
[ccan] / tools / ccanlint / file_analysis.c
1 #include "ccanlint.h"
2 #include "get_file_lines.h"
3 #include <talloc/talloc.h>
4 #include <string/string.h>
5 #include <noerr/noerr.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <err.h>
11 #include <errno.h>
12 #include <dirent.h>
13
14 char **get_ccan_file_lines(struct ccan_file *f)
15 {
16         if (!f->lines)
17                 f->lines = get_file_lines(f, f->name, &f->num_lines);
18         return f->lines;
19 }
20
21 static void add_files(struct manifest *m, const char *dir)
22 {
23         DIR *d;
24         struct dirent *ent;
25
26         if (dir[0])
27                 d = opendir(dir);
28         else
29                 d = opendir(".");
30         if (!d)
31                 err(1, "Opening directory %s", dir[0] ? dir : ".");
32
33         while ((ent = readdir(d)) != NULL) {
34                 struct stat st;
35                 struct ccan_file *f;
36                 struct list_head *dest;
37                 bool is_c_src;
38
39                 if (ent->d_name[0] == '.')
40                         continue;
41
42                 f = talloc(m, struct ccan_file);
43                 f->lines = NULL;
44                 f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
45                 if (lstat(f->name, &st) != 0)
46                         err(1, "lstat %s", f->name);
47
48                 if (S_ISDIR(st.st_mode)) {
49                         f->name = talloc_append_string(f->name, "/");
50                         add_files(m, f->name);
51                         continue;
52                 }
53                 if (!S_ISREG(st.st_mode)) {
54                         talloc_free(f);
55                         continue;
56                 }
57
58                 if (streq(f->name, "_info.c")) {
59                         m->info_file = f;
60                         continue;
61                 }
62
63                 is_c_src = strends(f->name, ".c");
64                 if (!is_c_src && !strends(f->name, ".h"))
65                         dest = &m->other_files;
66                 else if (!strchr(f->name, '/')) {
67                         if (is_c_src)
68                                 dest = &m->c_files;
69                         else
70                                 dest = &m->h_files;
71                 } else if (strstarts(f->name, "test/")) {
72                         if (is_c_src) {
73                                 if (strstarts(f->name, "test/run"))
74                                         dest = &m->run_tests;
75                                 else if (strstarts(f->name, "test/compile_ok"))
76                                         dest = &m->compile_ok_tests;
77                                 else if (strstarts(f->name, "test/compile_fail"))
78                                         dest = &m->compile_fail_tests;
79                                 else
80                                         dest = &m->other_test_files;
81                         } else
82                                 dest = &m->other_test_files;
83                 } else
84                         dest = &m->other_files;
85
86                 list_add(dest, &f->list);
87         }
88         closedir(d);
89 }
90
91 char *report_on_lines(struct list_head *files,
92                       char *(*report)(const char *),
93                       char *sofar)
94 {
95         struct ccan_file *f;
96
97         list_for_each(files, f, list) {
98                 unsigned int i;
99                 char **lines = get_ccan_file_lines(f);
100
101                 for (i = 0; i < f->num_lines; i++) {
102                         char *r = report(lines[i]);
103                         if (!r)
104                                 continue;
105
106                         sofar = talloc_asprintf_append(sofar,
107                                                        "%s:%u:%s\n",
108                                                        f->name, i+1, r);
109                         talloc_free(r);
110                 }
111         }
112         return sofar;
113 }
114
115 struct manifest *get_manifest(void)
116 {
117         struct manifest *m = talloc(NULL, struct manifest);
118         unsigned int len;
119
120         m->info_file = NULL;
121         list_head_init(&m->c_files);
122         list_head_init(&m->h_files);
123         list_head_init(&m->run_tests);
124         list_head_init(&m->compile_ok_tests);
125         list_head_init(&m->compile_fail_tests);
126         list_head_init(&m->other_test_files);
127         list_head_init(&m->other_files);
128
129         /* *This* is why people hate C. */
130         len = 32;
131         m->basename = talloc_array(m, char, len);
132         while (!getcwd(m->basename, len)) {
133                 if (errno != ERANGE)
134                         err(1, "Getting current directory");
135                 m->basename = talloc_realloc(m, m->basename, char, len *= 2);
136         }
137
138         len = strlen(m->basename);
139         while (len && m->basename[len-1] == '/')
140                 m->basename[--len] = '\0';
141
142         m->basename = strrchr(m->basename, '/');
143         if (!m->basename)
144                 errx(1, "I don't expect to be run from the root directory");
145         m->basename++;
146
147         add_files(m, "");
148         return m;
149 }