Debian Codegraph @

lib/doc/bin/docproc.c

raw symbols SCIP
1 /*
2 * docproc is a simple preprocessor for the template files
3 * used as placeholders for the kernel internal documentation.
4 * docproc is used for documentation-frontend and
5 * dependency-generator.
6 * The two usages have in common that they require
7 * some knowledge of the .tmpl syntax, therefore they
8 * are kept together.
9 *
10 * documentation-frontend
11 * Scans the template file and call kernel-doc for
12 * all occurrences of ![EIF]file
13 * Beforehand each referenced file is scanned for
14 * any symbols that are exported via these macros:
15 * EXPORT_SYMBOL(), EXPORT_SYMBOL_GPL(), &
16 * EXPORT_SYMBOL_GPL_FUTURE()
17 * This is used to create proper -function and
18 * -nofunction arguments in calls to kernel-doc.
19 * Usage: docproc doc file.tmpl
20 *
21 * dependency-generator:
22 * Scans the template file and list all files
23 * referenced in a format recognized by make.
24 * Usage: docproc depend file.tmpl
25 * Writes dependency information to stdout
26 * in the following format:
27 * file.tmpl src.c src2.c
28 * The filenames are obtained from the following constructs:
29 * !Efilename
30 * !Ifilename
31 * !Dfilename
32 * !Ffilename
33 * !Pfilename
34 *
35 */
36
37 #define _GNU_SOURCE
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <unistd.h>
43 #include <limits.h>
44 #include <errno.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47
48 /* exitstatus is used to keep track of any failing calls to kernel-doc,
49 * but execution continues. */
50 int exitstatus = 0;
51
52 typedef void DFL(char *);
53 DFL *defaultline;
54
55 typedef void FILEONLY(char * file);
56 FILEONLY *internalfunctions;
57 FILEONLY *externalfunctions;
58 FILEONLY *symbolsonly;
59 FILEONLY *findall;
60
61 typedef void FILELINE(char * file, char * line);
62 FILELINE * singlefunctions;
63 FILELINE * entity_system;
64 FILELINE * docsection;
65
66 #define MAXLINESZ 2048
67 #define MAXFILES 250
68 #define KERNELDOCPATH "doc/bin/"
69 #define KERNELDOC "kernel-doc"
70 #define DOCBOOK "-docbook"
71 #define LIST "-list"
72 #define FUNCTION "-function"
73 #define NOFUNCTION "-nofunction"
74 #define NODOCSECTIONS "-no-doc-sections"
75 #define SHOWNOTFOUND "-show-not-found"
76
77 static char *srctree, *kernsrctree;
78
79 static char **all_list = NULL;
80 static size_t all_list_len = 0;
81
82 static void consume_symbol(const char *sym)
83 {
84 size_t i;
85
86 for (i = 0; i < all_list_len; i++) {
87 if (!all_list[i])
88 continue;
89 if (strcmp(sym, all_list[i]))
90 continue;
91 all_list[i] = NULL;
92 break;
93 }
94 }
95
96 static void usage (void)
97 {
98 fprintf(stderr, "Usage: docproc {doc|depend} file\n");
99 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
100 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
101 fprintf(stderr, "depend: generate list of files referenced within file\n");
102 fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n");
103 fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n");
104 }
105
106 /*
107 * Execute kernel-doc with parameters given in svec
108 */
109 static void exec_kernel_doc(char **svec)
110 {
111 pid_t pid;
112 int ret;
113 char real_filename[PATH_MAX + 1];
114 /* Make sure output generated so far are flushed */
115 fflush(stdout);
116 switch (pid=fork()) {
117 case -1:
118 perror("fork");
119 exit(1);
120 case 0:
121 memset(real_filename, 0, sizeof(real_filename));
122 strncat(real_filename, kernsrctree, PATH_MAX);
123 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
124 PATH_MAX - strlen(real_filename));
125 execvp(real_filename, svec);
126 fprintf(stderr, "exec ");
127 perror(real_filename);
128 exit(1);
129 default:
130 waitpid(pid, &ret ,0);
131 }
132 if (WIFEXITED(ret))
133 exitstatus |= WEXITSTATUS(ret);
134 else
135 exitstatus = 0xff;
136 }
137
138 /* Types used to create list of all exported symbols in a number of files */
139 struct symbols
140 {
141 char *name;
142 };
143
144 struct symfile
145 {
146 char *filename;
147 struct symbols *symbollist;
148 unsigned int symbolcnt;
149 };
150
151 struct symfile symfilelist[MAXFILES];
152 int symfilecnt = 0;
153
154 static void add_new_symbol(struct symfile *sym, char * symname)
155 {
156 sym->symbollist =
157 realloc(sym->symbollist,
158 (sym->symbolcnt + 1) * sizeof(struct symbols));
159 if (!sym->symbollist) {
160 perror("realloc");
161 exit(1);
162 }
163 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
164 }
165
166 /* Add a filename to the list */
167 static struct symfile * add_new_file(char * filename)
168 {
169 symfilelist[symfilecnt++].filename = strdup(filename);
170 return &symfilelist[symfilecnt - 1];
171 }
172
173 /* Check if file already are present in the list */
174 static struct symfile * filename_exist(char * filename)
175 {
176 int i;
177 for (i=0; i < symfilecnt; i++)
178 if (strcmp(symfilelist[i].filename, filename) == 0)
179 return &symfilelist[i];
180 return NULL;
181 }
182
183 /*
184 * List all files referenced within the template file.
185 * Files are separated by tabs.
186 */
187 static void adddep(char * file) { printf("\t%s", file); }
188 static void adddep2(char * file, char * line) { (void)line; adddep(file); }
189 static void noaction(char * line) { (void)line; }
190 static void noaction2(char * file, char * line) { (void)file; (void)line; }
191 /* Echo the line without further action */
192 static void printline(char * line) { printf("%s", line); }
193
194 /*
195 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
196 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
197 * All symbols located are stored in symfilelist.
198 */
199 static void find_export_symbols(char * filename)
200 {
201 FILE * fp;
202 struct symfile *sym;
203 char line[MAXLINESZ];
204 if (filename_exist(filename) == NULL) {
205 char real_filename[PATH_MAX + 1];
206 memset(real_filename, 0, sizeof(real_filename));
207 strncat(real_filename, srctree, PATH_MAX);
208 strncat(real_filename, "/", PATH_MAX - strlen(real_filename));
209 strncat(real_filename, filename,
210 PATH_MAX - strlen(real_filename));
211 sym = add_new_file(filename);
212 fp = fopen(real_filename, "r");
213 if (fp == NULL) {
214 fprintf(stderr, "docproc: ");
215 perror(real_filename);
216 exit(1);
217 }
218 while (fgets(line, MAXLINESZ, fp)) {
219 char *p;
220 char *e;
221 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
222 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
223 /* Skip EXPORT_SYMBOL{_GPL} */
224 while (isalnum(*p) || *p == '_')
225 p++;
226 /* Remove parentheses & additional whitespace */
227 while (isspace(*p))
228 p++;
229 if (*p != '(')
230 continue; /* Syntax error? */
231 else
232 p++;
233 while (isspace(*p))
234 p++;
235 e = p;
236 while (isalnum(*e) || *e == '_')
237 e++;
238 *e = '\0';
239 add_new_symbol(sym, p);
240 }
241 }
242 fclose(fp);
243 }
244 }
245
246 /*
247 * Document all external or internal functions in a file.
248 * Call kernel-doc with following parameters:
249 * kernel-doc -docbook -nofunction function_name1 filename
250 * Function names are obtained from all the src files
251 * by find_export_symbols.
252 * intfunc uses -nofunction
253 * extfunc uses -function
254 */
255 static void docfunctions(char * filename, char * type)
256 {
257 unsigned int j;
258 int i;
259 unsigned int symcnt = 0;
260 int idx = 0;
261 char **vec;
262
263 for (i=0; i <= symfilecnt; i++)
264 symcnt += symfilelist[i].symbolcnt;
265 vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
266 if (vec == NULL) {
267 perror("docproc: ");
268 exit(1);
269 }
270 vec[idx++] = KERNELDOC;
271 vec[idx++] = DOCBOOK;
272 vec[idx++] = NODOCSECTIONS;
273 for (i=0; i < symfilecnt; i++) {
274 struct symfile * sym = &symfilelist[i];
275 for (j=0; j < sym->symbolcnt; j++) {
276 vec[idx++] = type;
277 consume_symbol(sym->symbollist[j].name);
278 vec[idx++] = sym->symbollist[j].name;
279 }
280 }
281 vec[idx++] = filename;
282 vec[idx] = NULL;
283 printf("<!-- %s -->\n", filename);
284 exec_kernel_doc(vec);
285 fflush(stdout);
286 free(vec);
287 }
288 static void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
289 static void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
290
291 /*
292 * Document specific function(s) in a file.
293 * Call kernel-doc with the following parameters:
294 * kernel-doc -docbook -function function1 [-function function2]
295 */
296 static void singfunc(char * filename, char * line)
297 {
298 char *vec[200]; /* Enough for specific functions */
299 int i, idx = 0;
300 int startofsym = 1;
301 vec[idx++] = KERNELDOC;
302 vec[idx++] = DOCBOOK;
303 vec[idx++] = SHOWNOTFOUND;
304
305 /* Split line up in individual parameters preceded by FUNCTION */
306 for (i=0; line[i]; i++) {
307 if (isspace(line[i])) {
308 line[i] = '\0';
309 startofsym = 1;
310 continue;
311 }
312 if (startofsym) {
313 startofsym = 0;
314 vec[idx++] = FUNCTION;
315 vec[idx++] = &line[i];
316 }
317 }
318 for (i = 0; i < idx; i++) {
319 if (strcmp(vec[i], FUNCTION))
320 continue;
321 consume_symbol(vec[i + 1]);
322 }
323 vec[idx++] = filename;
324 vec[idx] = NULL;
325 exec_kernel_doc(vec);
326 }
327
328 /*
329 * Insert specific documentation section from a file.
330 * Call kernel-doc with the following parameters:
331 * kernel-doc -docbook -function "doc section" filename
332 */
333 static void docsect(char *filename, char *line)
334 {
335 /* kerneldoc -docbook -show-not-found -function "section" file NULL */
336 char *vec[7];
337 char *s;
338
339 for (s = line; *s; s++)
340 if (*s == '\n')
341 *s = '\0';
342
343 if (asprintf(&s, "DOC: %s", line) < 0) {
344 perror("asprintf");
345 exit(1);
346 }
347 consume_symbol(s);
348 free(s);
349
350 vec[0] = KERNELDOC;
351 vec[1] = DOCBOOK;
352 vec[2] = SHOWNOTFOUND;
353 vec[3] = FUNCTION;
354 vec[4] = line;
355 vec[5] = filename;
356 vec[6] = NULL;
357 exec_kernel_doc(vec);
358 }
359
360 static void find_all_symbols(char *filename)
361 {
362 char *vec[4]; /* kerneldoc -list file NULL */
363 pid_t pid;
364 ssize_t ret;
365 char real_filename[PATH_MAX + 1];
366 int pipefd[2];
367 char *data, *str;
368 size_t i, count, start, data_len = 0;
369
370 vec[0] = KERNELDOC;
371 vec[1] = LIST;
372 vec[2] = filename;
373 vec[3] = NULL;
374
375 if (pipe(pipefd)) {
376 perror("pipe");
377 exit(1);
378 }
379
380 switch (pid=fork()) {
381 case -1:
382 perror("fork");
383 exit(1);
384 case 0:
385 close(pipefd[0]);
386 dup2(pipefd[1], 1);
387 memset(real_filename, 0, sizeof(real_filename));
388 strncat(real_filename, kernsrctree, PATH_MAX);
389 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
390 PATH_MAX - strlen(real_filename));
391 execvp(real_filename, vec);
392 fprintf(stderr, "exec ");
393 perror(real_filename);
394 exit(1);
395 default:
396 close(pipefd[1]);
397 data = malloc(4096);
398 if (!data) {
399 perror("malloc");
400 exit(1);
401 }
402 do {
403 while ((ret = read(pipefd[0],
404 data + data_len,
405 4096)) > 0) {
406 data_len += (size_t)ret;
407 data = realloc(data, data_len + 4096);
408 if (!data) {
409 perror("realloc");
410 exit(1);
411 }
412 }
413 } while (ret == -EAGAIN);
414 if (ret != 0) {
415 perror("read");
416 exit(1);
417 }
418 waitpid(pid, (int *)&ret ,0);
419 }
420 if (WIFEXITED(ret))
421 exitstatus |= (int)WEXITSTATUS(ret);
422 else
423 exitstatus = 0xff;
424
425 count = 0;
426 /* poor man's strtok, but with counting */
427 for (i = 0; i < data_len; i++) {
428 if (data[i] == '\n') {
429 count++;
430 data[i] = '\0';
431 }
432 }
433 start = all_list_len;
434 all_list_len += count;
435 all_list = realloc(all_list, sizeof(char *) * all_list_len);
436 if (!all_list) {
437 perror("realloc");
438 exit(1);
439 }
440 str = data;
441 for (i = 0; i < data_len && start != all_list_len; i++) {
442 if (data[i] == '\0') {
443 all_list[start] = str;
444 str = data + i + 1;
445 start++;
446 }
447 }
448 free(data);
449 }
450
451 /*
452 * Parse file, calling action specific functions for:
453 * 1) Lines containing !E
454 * 2) Lines containing !I
455 * 3) Lines containing !D
456 * 4) Lines containing !F
457 * 5) Lines containing !P
458 * 6) Lines containing !C
459 * 7) Default lines - lines not matching the above
460 */
461 static void parse_file(FILE *infile)
462 {
463 char line[MAXLINESZ];
464 char * s;
465 while (fgets(line, MAXLINESZ, infile)) {
466 if (line[0] == '!') {
467 s = line + 2;
468 switch (line[1]) {
469 case 'E':
470 while (*s && !isspace(*s)) s++;
471 *s = '\0';
472 externalfunctions(line+2);
473 break;
474 case 'I':
475 while (*s && !isspace(*s)) s++;
476 *s = '\0';
477 internalfunctions(line+2);
478 break;
479 case 'D':
480 while (*s && !isspace(*s)) s++;
481 *s = '\0';
482 symbolsonly(line+2);
483 break;
484 case 'F':
485 /* filename */
486 while (*s && !isspace(*s)) s++;
487 *s++ = '\0';
488 /* function names */
489 while (isspace(*s))
490 s++;
491 singlefunctions(line +2, s);
492 break;
493 case 'P':
494 /* filename */
495 while (*s && !isspace(*s)) s++;
496 *s++ = '\0';
497 /* DOC: section name */
498 while (isspace(*s))
499 s++;
500 docsection(line + 2, s);
501 break;
502 case 'C':
503 while (*s && !isspace(*s)) s++;
504 *s = '\0';
505 if (findall)
506 findall(line+2);
507 break;
508 default:
509 defaultline(line);
510 }
511 } else {
512 defaultline(line);
513 }
514 }
515 fflush(stdout);
516 }
517
518
519 int main(int argc, char *argv[])
520 {
521 FILE * infile;
522 unsigned int i;
523
524 srctree = getenv("SRCTREE");
525 if (!srctree)
526 srctree = getcwd(NULL, 0);
527 kernsrctree = getenv("KBUILD_SRC");
528 if (!kernsrctree || !*kernsrctree)
529 kernsrctree = srctree;
530 if (argc != 3) {
531 usage();
532 exit(1);
533 }
534 /* Open file, exit on error */
535 infile = fopen(argv[2], "r");
536 if (infile == NULL) {
537 fprintf(stderr, "docproc: ");
538 perror(argv[2]);
539 exit(2);
540 }
541
542 if (strcmp("doc", argv[1]) == 0) {
543 /* Need to do this in two passes.
544 * First pass is used to collect all symbols exported
545 * in the various files;
546 * Second pass generate the documentation.
547 * This is required because some functions are declared
548 * and exported in different files :-((
549 */
550 /* Collect symbols */
551 defaultline = noaction;
552 internalfunctions = find_export_symbols;
553 externalfunctions = find_export_symbols;
554 symbolsonly = find_export_symbols;
555 singlefunctions = noaction2;
556 docsection = noaction2;
557 findall = find_all_symbols;
558 parse_file(infile);
559
560 /* Rewind to start from beginning of file again */
561 fseek(infile, 0, SEEK_SET);
562 defaultline = printline;
563 internalfunctions = intfunc;
564 externalfunctions = extfunc;
565 symbolsonly = printline;
566 singlefunctions = singfunc;
567 docsection = docsect;
568 findall = NULL;
569
570 parse_file(infile);
571
572 for (i = 0; i < all_list_len; i++) {
573 if (!all_list[i])
574 continue;
575 fprintf(stderr, "Warning: didn't use docs for %s\n",
576 all_list[i]);
577 }
578 } else if (strcmp("depend", argv[1]) == 0) {
579 /* Create first part of dependency chain
580 * file.tmpl */
581 printf("%s\t", argv[2]);
582 defaultline = noaction;
583 internalfunctions = adddep;
584 externalfunctions = adddep;
585 symbolsonly = adddep;
586 singlefunctions = adddep2;
587 docsection = adddep2;
588 findall = adddep;
589 parse_file(infile);
590 printf("\n");
591 } else {
592 fprintf(stderr, "Unknown option: %s\n", argv[1]);
593 exit(1);
594 }
595 fclose(infile);
596 fflush(stdout);
597 return exitstatus;
598 }