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:
46 #include <sys/types.h>
50 /* exitstatus is used to keep track of any failing calls to kernel-doc,
51 * but execution continues. */
54 typedef void DFL(char *);
57 typedef void FILEONLY(char * file);
58 FILEONLY *internalfunctions;
59 FILEONLY *externalfunctions;
60 FILEONLY *symbolsonly;
63 typedef void FILELINE(char * file, char * line);
64 FILELINE * singlefunctions;
65 FILELINE * entity_system;
66 FILELINE * docsection;
68 #define MAXLINESZ 2048
70 #define KERNELDOCPATH "scripts/"
71 #define KERNELDOC "kernel-doc"
72 #define DOCBOOK "-docbook"
75 #define FUNCTION "-function"
76 #define NOFUNCTION "-nofunction"
77 #define NODOCSECTIONS "-no-doc-sections"
78 #define SHOWNOTFOUND "-show-not-found"
86 static enum file_format file_format = FORMAT_AUTO;
88 #define KERNELDOC_FORMAT (file_format == FORMAT_RST ? RST : DOCBOOK)
90 static char *srctree, *kernsrctree;
92 static char **all_list = NULL;
93 static int all_list_len = 0;
95 static void consume_symbol(const char *sym)
99 for (i = 0; i < all_list_len; i++) {
102 if (strcmp(sym, all_list[i]))
109 static void usage (void)
111 fprintf(stderr, "Usage: docproc [{--docbook|--rst}] {doc|depend} file\n");
112 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
113 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
114 fprintf(stderr, "depend: generate list of files referenced within file\n");
115 fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n");
116 fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n");
120 * Execute kernel-doc with parameters given in svec
122 static void exec_kernel_doc(char **svec)
126 char real_filename[PATH_MAX + 1];
127 /* Make sure output generated so far are flushed */
129 switch (pid=fork()) {
134 memset(real_filename, 0, sizeof(real_filename));
135 strncat(real_filename, kernsrctree, PATH_MAX);
136 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
137 PATH_MAX - strlen(real_filename));
138 execvp(real_filename, svec);
139 fprintf(stderr, "exec ");
140 perror(real_filename);
143 waitpid(pid, &ret ,0);
146 exitstatus |= WEXITSTATUS(ret);
151 /* Types used to create list of all exported symbols in a number of files */
160 struct symbols *symbollist;
164 struct symfile symfilelist[MAXFILES];
167 static void add_new_symbol(struct symfile *sym, char * symname)
170 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
171 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
174 /* Add a filename to the list */
175 static struct symfile * add_new_file(char * filename)
177 symfilelist[symfilecnt++].filename = strdup(filename);
178 return &symfilelist[symfilecnt - 1];
181 /* Check if file already are present in the list */
182 static struct symfile * filename_exist(char * filename)
185 for (i=0; i < symfilecnt; i++)
186 if (strcmp(symfilelist[i].filename, filename) == 0)
187 return &symfilelist[i];
192 * List all files referenced within the template file.
193 * Files are separated by tabs.
195 static void adddep(char * file) { printf("\t%s", file); }
196 static void adddep2(char * file, char * line) { line = line; adddep(file); }
197 static void noaction(char * line) { line = line; }
198 static void noaction2(char * file, char * line) { file = file; line = line; }
200 /* Echo the line without further action */
201 static void printline(char * line) { printf("%s", line); }
204 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
205 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
206 * All symbols located are stored in symfilelist.
208 static void find_export_symbols(char * filename)
212 char line[MAXLINESZ];
213 if (filename_exist(filename) == NULL) {
214 char real_filename[PATH_MAX + 1];
215 memset(real_filename, 0, sizeof(real_filename));
216 strncat(real_filename, srctree, PATH_MAX);
217 strncat(real_filename, "/", PATH_MAX - strlen(real_filename));
218 strncat(real_filename, filename,
219 PATH_MAX - strlen(real_filename));
220 sym = add_new_file(filename);
221 fp = fopen(real_filename, "r");
223 fprintf(stderr, "docproc: ");
224 perror(real_filename);
227 while (fgets(line, MAXLINESZ, fp)) {
230 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
231 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
232 /* Skip EXPORT_SYMBOL{_GPL} */
233 while (isalnum(*p) || *p == '_')
235 /* Remove parentheses & additional whitespace */
239 continue; /* Syntax error? */
245 while (isalnum(*e) || *e == '_')
248 add_new_symbol(sym, p);
256 * Document all external or internal functions in a file.
257 * Call kernel-doc with following parameters:
258 * kernel-doc [-docbook|-rst] -nofunction function_name1 filename
259 * Function names are obtained from all the src files
260 * by find_export_symbols.
261 * intfunc uses -nofunction
262 * extfunc uses -function
264 static void docfunctions(char * filename, char * type)
271 for (i=0; i <= symfilecnt; i++)
272 symcnt += symfilelist[i].symbolcnt;
273 vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
278 vec[idx++] = KERNELDOC;
279 vec[idx++] = KERNELDOC_FORMAT;
280 vec[idx++] = NODOCSECTIONS;
281 for (i=0; i < symfilecnt; i++) {
282 struct symfile * sym = &symfilelist[i];
283 for (j=0; j < sym->symbolcnt; j++) {
285 consume_symbol(sym->symbollist[j].name);
286 vec[idx++] = sym->symbollist[j].name;
289 vec[idx++] = filename;
291 if (file_format == FORMAT_RST)
292 printf(".. %s\n", filename);
294 printf("<!-- %s -->\n", filename);
295 exec_kernel_doc(vec);
299 static void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
300 static void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
303 * Document specific function(s) in a file.
304 * Call kernel-doc with the following parameters:
305 * kernel-doc -docbook -function function1 [-function function2]
307 static void singfunc(char * filename, char * line)
309 char *vec[200]; /* Enough for specific functions */
312 vec[idx++] = KERNELDOC;
313 vec[idx++] = KERNELDOC_FORMAT;
314 vec[idx++] = SHOWNOTFOUND;
316 /* Split line up in individual parameters preceded by FUNCTION */
317 for (i=0; line[i]; i++) {
318 if (isspace(line[i])) {
325 vec[idx++] = FUNCTION;
326 vec[idx++] = &line[i];
329 for (i = 0; i < idx; i++) {
330 if (strcmp(vec[i], FUNCTION))
332 consume_symbol(vec[i + 1]);
334 vec[idx++] = filename;
336 exec_kernel_doc(vec);
340 * Insert specific documentation section from a file.
341 * Call kernel-doc with the following parameters:
342 * kernel-doc -docbook -function "doc section" filename
344 static void docsect(char *filename, char *line)
346 /* kerneldoc -docbook -show-not-found -function "section" file NULL */
350 for (s = line; *s; s++)
354 if (asprintf(&s, "DOC: %s", line) < 0) {
362 vec[1] = KERNELDOC_FORMAT;
363 vec[2] = SHOWNOTFOUND;
368 exec_kernel_doc(vec);
371 static void find_all_symbols(char *filename)
373 char *vec[4]; /* kerneldoc -list file NULL */
375 int ret, i, count, start;
376 char real_filename[PATH_MAX + 1];
391 switch (pid=fork()) {
398 memset(real_filename, 0, sizeof(real_filename));
399 strncat(real_filename, kernsrctree, PATH_MAX);
400 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
401 PATH_MAX - strlen(real_filename));
402 execvp(real_filename, vec);
403 fprintf(stderr, "exec ");
404 perror(real_filename);
410 while ((ret = read(pipefd[0],
414 data = realloc(data, data_len + 4096);
416 } while (ret == -EAGAIN);
421 waitpid(pid, &ret ,0);
424 exitstatus |= WEXITSTATUS(ret);
429 /* poor man's strtok, but with counting */
430 for (i = 0; i < data_len; i++) {
431 if (data[i] == '\n') {
436 start = all_list_len;
437 all_list_len += count;
438 all_list = realloc(all_list, sizeof(char *) * all_list_len);
440 for (i = 0; i < data_len && start != all_list_len; i++) {
441 if (data[i] == '\0') {
442 all_list[start] = str;
450 * Terminate s at first space, if any. If there was a space, return pointer to
451 * the character after that. Otherwise, return pointer to the terminating NUL.
453 static char *chomp(char *s)
455 while (*s && !isspace(*s))
464 /* Return pointer to directive content, or NULL if not a directive. */
465 static char *is_directive(char *line)
467 if (file_format == FORMAT_DOCBOOK && line[0] == '!')
469 else if (file_format == FORMAT_RST && !strncmp(line, ".. !", 4))
476 * Parse file, calling action specific functions for:
477 * 1) Lines containing !E
478 * 2) Lines containing !I
479 * 3) Lines containing !D
480 * 4) Lines containing !F
481 * 5) Lines containing !P
482 * 6) Lines containing !C
483 * 7) Default lines - lines not matching the above
485 static void parse_file(FILE *infile)
487 char line[MAXLINESZ];
489 while (fgets(line, MAXLINESZ, infile)) {
490 p = is_directive(line);
499 externalfunctions(p);
503 internalfunctions(p);
515 singlefunctions(p, s);
520 /* DOC: section name */
538 * Is this a RestructuredText template? Answer the question by seeing if its
539 * name ends in ".rst".
541 static int is_rst(const char *file)
543 char *dot = strrchr(file, '.');
545 return dot && !strcmp(dot + 1, "rst");
554 int main(int argc, char *argv[])
556 const char *subcommand, *filename;
560 srctree = getenv("SRCTREE");
562 srctree = getcwd(NULL, 0);
563 kernsrctree = getenv("KBUILD_SRC");
564 if (!kernsrctree || !*kernsrctree)
565 kernsrctree = srctree;
569 struct option opts[] = {
570 { "docbook", no_argument, NULL, OPT_DOCBOOK },
571 { "rst", no_argument, NULL, OPT_RST },
572 { "help", no_argument, NULL, OPT_HELP },
576 c = getopt_long_only(argc, argv, "", opts, NULL);
582 file_format = FORMAT_DOCBOOK;
585 file_format = FORMAT_RST;
605 subcommand = argv[0];
608 if (file_format == FORMAT_AUTO)
609 file_format = is_rst(filename) ? FORMAT_RST : FORMAT_DOCBOOK;
611 /* Open file, exit on error */
612 infile = fopen(filename, "r");
613 if (infile == NULL) {
614 fprintf(stderr, "docproc: ");
619 if (strcmp("doc", subcommand) == 0) {
620 if (file_format == FORMAT_RST) {
621 time_t t = time(NULL);
622 printf(".. generated from %s by docproc %s\n",
623 filename, ctime(&t));
626 /* Need to do this in two passes.
627 * First pass is used to collect all symbols exported
628 * in the various files;
629 * Second pass generate the documentation.
630 * This is required because some functions are declared
631 * and exported in different files :-((
633 /* Collect symbols */
634 defaultline = noaction;
635 internalfunctions = find_export_symbols;
636 externalfunctions = find_export_symbols;
637 symbolsonly = find_export_symbols;
638 singlefunctions = noaction2;
639 docsection = noaction2;
640 findall = find_all_symbols;
643 /* Rewind to start from beginning of file again */
644 fseek(infile, 0, SEEK_SET);
645 defaultline = printline;
646 internalfunctions = intfunc;
647 externalfunctions = extfunc;
648 symbolsonly = printline;
649 singlefunctions = singfunc;
650 docsection = docsect;
655 for (i = 0; i < all_list_len; i++) {
658 fprintf(stderr, "Warning: didn't use docs for %s\n",
661 } else if (strcmp("depend", subcommand) == 0) {
662 /* Create first part of dependency chain
664 printf("%s\t", filename);
665 defaultline = noaction;
666 internalfunctions = adddep;
667 externalfunctions = adddep;
668 symbolsonly = adddep;
669 singlefunctions = adddep2;
670 docsection = adddep2;
675 fprintf(stderr, "Unknown option: %s\n", subcommand);