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
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
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:
45 #include <sys/types.h>
48 /* exitstatus is used to keep track of any failing calls to kernel-doc,
49 * but execution continues. */
52 typedef void DFL(char *);
55 typedef void FILEONLY(char * file);
56 FILEONLY *internalfunctions;
57 FILEONLY *externalfunctions;
58 FILEONLY *symbolsonly;
61 typedef void FILELINE(char * file, char * line);
62 FILELINE * singlefunctions;
63 FILELINE * entity_system;
64 FILELINE * docsection;
66 #define MAXLINESZ 2048
68 #define KERNELDOCPATH "scripts/"
69 #define KERNELDOC "kernel-doc"
70 #define DOCBOOK "-docbook"
72 #define FUNCTION "-function"
73 #define NOFUNCTION "-nofunction"
74 #define NODOCSECTIONS "-no-doc-sections"
76 static char *srctree, *kernsrctree;
78 static char **all_list = NULL;
79 static int all_list_len = 0;
81 static void consume_symbol(const char *sym)
85 for (i = 0; i < all_list_len; i++) {
88 if (strcmp(sym, all_list[i]))
95 static void usage (void)
97 fprintf(stderr, "Usage: docproc {doc|depend} file\n");
98 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
99 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
100 fprintf(stderr, "depend: generate list of files referenced within file\n");
101 fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n");
102 fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n");
106 * Execute kernel-doc with parameters given in svec
108 static void exec_kernel_doc(char **svec)
112 char real_filename[PATH_MAX + 1];
113 /* Make sure output generated so far are flushed */
115 switch (pid=fork()) {
120 memset(real_filename, 0, sizeof(real_filename));
121 strncat(real_filename, kernsrctree, PATH_MAX);
122 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
123 PATH_MAX - strlen(real_filename));
124 execvp(real_filename, svec);
125 fprintf(stderr, "exec ");
126 perror(real_filename);
129 waitpid(pid, &ret ,0);
132 exitstatus |= WEXITSTATUS(ret);
137 /* Types used to create list of all exported symbols in a number of files */
146 struct symbols *symbollist;
150 struct symfile symfilelist[MAXFILES];
153 static void add_new_symbol(struct symfile *sym, char * symname)
156 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
157 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
160 /* Add a filename to the list */
161 static struct symfile * add_new_file(char * filename)
163 symfilelist[symfilecnt++].filename = strdup(filename);
164 return &symfilelist[symfilecnt - 1];
167 /* Check if file already are present in the list */
168 static struct symfile * filename_exist(char * filename)
171 for (i=0; i < symfilecnt; i++)
172 if (strcmp(symfilelist[i].filename, filename) == 0)
173 return &symfilelist[i];
178 * List all files referenced within the template file.
179 * Files are separated by tabs.
181 static void adddep(char * file) { printf("\t%s", file); }
182 static void adddep2(char * file, char * line) { line = line; adddep(file); }
183 static void noaction(char * line) { line = line; }
184 static void noaction2(char * file, char * line) { file = file; line = line; }
186 /* Echo the line without further action */
187 static void printline(char * line) { printf("%s", line); }
190 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
191 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
192 * All symbols located are stored in symfilelist.
194 static void find_export_symbols(char * filename)
198 char line[MAXLINESZ];
199 if (filename_exist(filename) == NULL) {
200 char real_filename[PATH_MAX + 1];
201 memset(real_filename, 0, sizeof(real_filename));
202 strncat(real_filename, srctree, PATH_MAX);
203 strncat(real_filename, "/", PATH_MAX - strlen(real_filename));
204 strncat(real_filename, filename,
205 PATH_MAX - strlen(real_filename));
206 sym = add_new_file(filename);
207 fp = fopen(real_filename, "r");
209 fprintf(stderr, "docproc: ");
210 perror(real_filename);
213 while (fgets(line, MAXLINESZ, fp)) {
216 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
217 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
218 /* Skip EXPORT_SYMBOL{_GPL} */
219 while (isalnum(*p) || *p == '_')
221 /* Remove parentheses & additional whitespace */
225 continue; /* Syntax error? */
231 while (isalnum(*e) || *e == '_')
234 add_new_symbol(sym, p);
242 * Document all external or internal functions in a file.
243 * Call kernel-doc with following parameters:
244 * kernel-doc -docbook -nofunction function_name1 filename
245 * Function names are obtained from all the src files
246 * by find_export_symbols.
247 * intfunc uses -nofunction
248 * extfunc uses -function
250 static void docfunctions(char * filename, char * type)
257 for (i=0; i <= symfilecnt; i++)
258 symcnt += symfilelist[i].symbolcnt;
259 vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
264 vec[idx++] = KERNELDOC;
265 vec[idx++] = DOCBOOK;
266 vec[idx++] = NODOCSECTIONS;
267 for (i=0; i < symfilecnt; i++) {
268 struct symfile * sym = &symfilelist[i];
269 for (j=0; j < sym->symbolcnt; j++) {
271 consume_symbol(sym->symbollist[j].name);
272 vec[idx++] = sym->symbollist[j].name;
275 vec[idx++] = filename;
277 printf("<!-- %s -->\n", filename);
278 exec_kernel_doc(vec);
282 static void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
283 static void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
286 * Document specific function(s) in a file.
287 * Call kernel-doc with the following parameters:
288 * kernel-doc -docbook -function function1 [-function function2]
290 static void singfunc(char * filename, char * line)
292 char *vec[200]; /* Enough for specific functions */
295 vec[idx++] = KERNELDOC;
296 vec[idx++] = DOCBOOK;
298 /* Split line up in individual parameters preceded by FUNCTION */
299 for (i=0; line[i]; i++) {
300 if (isspace(line[i])) {
307 vec[idx++] = FUNCTION;
308 vec[idx++] = &line[i];
311 for (i = 0; i < idx; i++) {
312 if (strcmp(vec[i], FUNCTION))
314 consume_symbol(vec[i + 1]);
316 vec[idx++] = filename;
318 exec_kernel_doc(vec);
322 * Insert specific documentation section from a file.
323 * Call kernel-doc with the following parameters:
324 * kernel-doc -docbook -function "doc section" filename
326 static void docsect(char *filename, char *line)
328 char *vec[6]; /* kerneldoc -docbook -function "section" file NULL */
331 for (s = line; *s; s++)
335 if (asprintf(&s, "DOC: %s", line) < 0) {
348 exec_kernel_doc(vec);
351 static void find_all_symbols(char *filename)
353 char *vec[4]; /* kerneldoc -list file NULL */
355 int ret, i, count, start;
356 char real_filename[PATH_MAX + 1];
371 switch (pid=fork()) {
378 memset(real_filename, 0, sizeof(real_filename));
379 strncat(real_filename, kernsrctree, PATH_MAX);
380 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
381 PATH_MAX - strlen(real_filename));
382 execvp(real_filename, vec);
383 fprintf(stderr, "exec ");
384 perror(real_filename);
390 while ((ret = read(pipefd[0],
394 data = realloc(data, data_len + 4096);
396 } while (ret == -EAGAIN);
401 waitpid(pid, &ret ,0);
404 exitstatus |= WEXITSTATUS(ret);
409 /* poor man's strtok, but with counting */
410 for (i = 0; i < data_len; i++) {
411 if (data[i] == '\n') {
416 start = all_list_len;
417 all_list_len += count;
418 all_list = realloc(all_list, sizeof(char *) * all_list_len);
420 for (i = 0; i < data_len && start != all_list_len; i++) {
421 if (data[i] == '\0') {
422 all_list[start] = str;
430 * Parse file, calling action specific functions for:
431 * 1) Lines containing !E
432 * 2) Lines containing !I
433 * 3) Lines containing !D
434 * 4) Lines containing !F
435 * 5) Lines containing !P
436 * 6) Lines containing !C
437 * 7) Default lines - lines not matching the above
439 static void parse_file(FILE *infile)
441 char line[MAXLINESZ];
443 while (fgets(line, MAXLINESZ, infile)) {
444 if (line[0] == '!') {
448 while (*s && !isspace(*s)) s++;
450 externalfunctions(line+2);
453 while (*s && !isspace(*s)) s++;
455 internalfunctions(line+2);
458 while (*s && !isspace(*s)) s++;
464 while (*s && !isspace(*s)) s++;
469 singlefunctions(line +2, s);
473 while (*s && !isspace(*s)) s++;
475 /* DOC: section name */
478 docsection(line + 2, s);
481 while (*s && !isspace(*s)) s++;
497 int main(int argc, char *argv[])
502 srctree = getenv("SRCTREE");
504 srctree = getcwd(NULL, 0);
505 kernsrctree = getenv("KBUILD_SRC");
506 if (!kernsrctree || !*kernsrctree)
507 kernsrctree = srctree;
512 /* Open file, exit on error */
513 infile = fopen(argv[2], "r");
514 if (infile == NULL) {
515 fprintf(stderr, "docproc: ");
520 if (strcmp("doc", argv[1]) == 0) {
521 /* Need to do this in two passes.
522 * First pass is used to collect all symbols exported
523 * in the various files;
524 * Second pass generate the documentation.
525 * This is required because some functions are declared
526 * and exported in different files :-((
528 /* Collect symbols */
529 defaultline = noaction;
530 internalfunctions = find_export_symbols;
531 externalfunctions = find_export_symbols;
532 symbolsonly = find_export_symbols;
533 singlefunctions = noaction2;
534 docsection = noaction2;
535 findall = find_all_symbols;
538 /* Rewind to start from beginning of file again */
539 fseek(infile, 0, SEEK_SET);
540 defaultline = printline;
541 internalfunctions = intfunc;
542 externalfunctions = extfunc;
543 symbolsonly = printline;
544 singlefunctions = singfunc;
545 docsection = docsect;
550 for (i = 0; i < all_list_len; i++) {
553 fprintf(stderr, "Warning: didn't use docs for %s\n",
556 } else if (strcmp("depend", argv[1]) == 0) {
557 /* Create first part of dependency chain
559 printf("%s\t", argv[2]);
560 defaultline = noaction;
561 internalfunctions = adddep;
562 externalfunctions = adddep;
563 symbolsonly = adddep;
564 singlefunctions = adddep2;
565 docsection = adddep2;
570 fprintf(stderr, "Unknown option: %s\n", argv[1]);