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