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"
75 #define SHOWNOTFOUND "-show-not-found"
77 static char *srctree
, *kernsrctree
;
79 static char **all_list
= NULL
;
80 static int all_list_len
= 0;
82 static void consume_symbol(const char *sym
)
86 for (i
= 0; i
< all_list_len
; i
++) {
89 if (strcmp(sym
, all_list
[i
]))
96 static void usage (void)
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");
107 * Execute kernel-doc with parameters given in svec
109 static void exec_kernel_doc(char **svec
)
113 char real_filename
[PATH_MAX
+ 1];
114 /* Make sure output generated so far are flushed */
116 switch (pid
=fork()) {
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
);
130 waitpid(pid
, &ret
,0);
133 exitstatus
|= WEXITSTATUS(ret
);
138 /* Types used to create list of all exported symbols in a number of files */
147 struct symbols
*symbollist
;
151 struct symfile symfilelist
[MAXFILES
];
154 static void add_new_symbol(struct symfile
*sym
, char * symname
)
157 realloc(sym
->symbollist
, (sym
->symbolcnt
+ 1) * sizeof(char *));
158 sym
->symbollist
[sym
->symbolcnt
++].name
= strdup(symname
);
161 /* Add a filename to the list */
162 static struct symfile
* add_new_file(char * filename
)
164 symfilelist
[symfilecnt
++].filename
= strdup(filename
);
165 return &symfilelist
[symfilecnt
- 1];
168 /* Check if file already are present in the list */
169 static struct symfile
* filename_exist(char * filename
)
172 for (i
=0; i
< symfilecnt
; i
++)
173 if (strcmp(symfilelist
[i
].filename
, filename
) == 0)
174 return &symfilelist
[i
];
179 * List all files referenced within the template file.
180 * Files are separated by tabs.
182 static void adddep(char * file
) { printf("\t%s", file
); }
183 static void adddep2(char * file
, char * line
) { line
= line
; adddep(file
); }
184 static void noaction(char * line
) { line
= line
; }
185 static void noaction2(char * file
, char * line
) { file
= file
; line
= line
; }
187 /* Echo the line without further action */
188 static void printline(char * line
) { printf("%s", line
); }
191 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
192 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
193 * All symbols located are stored in symfilelist.
195 static void find_export_symbols(char * filename
)
199 char line
[MAXLINESZ
];
200 if (filename_exist(filename
) == NULL
) {
201 char real_filename
[PATH_MAX
+ 1];
202 memset(real_filename
, 0, sizeof(real_filename
));
203 strncat(real_filename
, srctree
, PATH_MAX
);
204 strncat(real_filename
, "/", PATH_MAX
- strlen(real_filename
));
205 strncat(real_filename
, filename
,
206 PATH_MAX
- strlen(real_filename
));
207 sym
= add_new_file(filename
);
208 fp
= fopen(real_filename
, "r");
210 fprintf(stderr
, "docproc: ");
211 perror(real_filename
);
214 while (fgets(line
, MAXLINESZ
, fp
)) {
217 if (((p
= strstr(line
, "EXPORT_SYMBOL_GPL")) != NULL
) ||
218 ((p
= strstr(line
, "EXPORT_SYMBOL")) != NULL
)) {
219 /* Skip EXPORT_SYMBOL{_GPL} */
220 while (isalnum(*p
) || *p
== '_')
222 /* Remove parentheses & additional whitespace */
226 continue; /* Syntax error? */
232 while (isalnum(*e
) || *e
== '_')
235 add_new_symbol(sym
, p
);
243 * Document all external or internal functions in a file.
244 * Call kernel-doc with following parameters:
245 * kernel-doc -docbook -nofunction function_name1 filename
246 * Function names are obtained from all the src files
247 * by find_export_symbols.
248 * intfunc uses -nofunction
249 * extfunc uses -function
251 static void docfunctions(char * filename
, char * type
)
258 for (i
=0; i
<= symfilecnt
; i
++)
259 symcnt
+= symfilelist
[i
].symbolcnt
;
260 vec
= malloc((2 + 2 * symcnt
+ 3) * sizeof(char *));
265 vec
[idx
++] = KERNELDOC
;
266 vec
[idx
++] = DOCBOOK
;
267 vec
[idx
++] = NODOCSECTIONS
;
268 for (i
=0; i
< symfilecnt
; i
++) {
269 struct symfile
* sym
= &symfilelist
[i
];
270 for (j
=0; j
< sym
->symbolcnt
; j
++) {
272 consume_symbol(sym
->symbollist
[j
].name
);
273 vec
[idx
++] = sym
->symbollist
[j
].name
;
276 vec
[idx
++] = filename
;
278 printf("<!-- %s -->\n", filename
);
279 exec_kernel_doc(vec
);
283 static void intfunc(char * filename
) { docfunctions(filename
, NOFUNCTION
); }
284 static void extfunc(char * filename
) { docfunctions(filename
, FUNCTION
); }
287 * Document specific function(s) in a file.
288 * Call kernel-doc with the following parameters:
289 * kernel-doc -docbook -function function1 [-function function2]
291 static void singfunc(char * filename
, char * line
)
293 char *vec
[200]; /* Enough for specific functions */
296 vec
[idx
++] = KERNELDOC
;
297 vec
[idx
++] = DOCBOOK
;
298 vec
[idx
++] = SHOWNOTFOUND
;
300 /* Split line up in individual parameters preceded by FUNCTION */
301 for (i
=0; line
[i
]; i
++) {
302 if (isspace(line
[i
])) {
309 vec
[idx
++] = FUNCTION
;
310 vec
[idx
++] = &line
[i
];
313 for (i
= 0; i
< idx
; i
++) {
314 if (strcmp(vec
[i
], FUNCTION
))
316 consume_symbol(vec
[i
+ 1]);
318 vec
[idx
++] = filename
;
320 exec_kernel_doc(vec
);
324 * Insert specific documentation section from a file.
325 * Call kernel-doc with the following parameters:
326 * kernel-doc -docbook -function "doc section" filename
328 static void docsect(char *filename
, char *line
)
330 /* kerneldoc -docbook -show-not-found -function "section" file NULL */
334 for (s
= line
; *s
; s
++)
338 if (asprintf(&s
, "DOC: %s", line
) < 0) {
347 vec
[2] = SHOWNOTFOUND
;
352 exec_kernel_doc(vec
);
355 static void find_all_symbols(char *filename
)
357 char *vec
[4]; /* kerneldoc -list file NULL */
359 int ret
, i
, count
, start
;
360 char real_filename
[PATH_MAX
+ 1];
375 switch (pid
=fork()) {
382 memset(real_filename
, 0, sizeof(real_filename
));
383 strncat(real_filename
, kernsrctree
, PATH_MAX
);
384 strncat(real_filename
, "/" KERNELDOCPATH KERNELDOC
,
385 PATH_MAX
- strlen(real_filename
));
386 execvp(real_filename
, vec
);
387 fprintf(stderr
, "exec ");
388 perror(real_filename
);
394 while ((ret
= read(pipefd
[0],
398 data
= realloc(data
, data_len
+ 4096);
400 } while (ret
== -EAGAIN
);
405 waitpid(pid
, &ret
,0);
408 exitstatus
|= WEXITSTATUS(ret
);
413 /* poor man's strtok, but with counting */
414 for (i
= 0; i
< data_len
; i
++) {
415 if (data
[i
] == '\n') {
420 start
= all_list_len
;
421 all_list_len
+= count
;
422 all_list
= realloc(all_list
, sizeof(char *) * all_list_len
);
424 for (i
= 0; i
< data_len
&& start
!= all_list_len
; i
++) {
425 if (data
[i
] == '\0') {
426 all_list
[start
] = str
;
434 * Parse file, calling action specific functions for:
435 * 1) Lines containing !E
436 * 2) Lines containing !I
437 * 3) Lines containing !D
438 * 4) Lines containing !F
439 * 5) Lines containing !P
440 * 6) Lines containing !C
441 * 7) Default lines - lines not matching the above
443 static void parse_file(FILE *infile
)
445 char line
[MAXLINESZ
];
447 while (fgets(line
, MAXLINESZ
, infile
)) {
448 if (line
[0] == '!') {
452 while (*s
&& !isspace(*s
)) s
++;
454 externalfunctions(line
+2);
457 while (*s
&& !isspace(*s
)) s
++;
459 internalfunctions(line
+2);
462 while (*s
&& !isspace(*s
)) s
++;
468 while (*s
&& !isspace(*s
)) s
++;
473 singlefunctions(line
+2, s
);
477 while (*s
&& !isspace(*s
)) s
++;
479 /* DOC: section name */
482 docsection(line
+ 2, s
);
485 while (*s
&& !isspace(*s
)) s
++;
501 int main(int argc
, char *argv
[])
506 srctree
= getenv("SRCTREE");
508 srctree
= getcwd(NULL
, 0);
509 kernsrctree
= getenv("KBUILD_SRC");
510 if (!kernsrctree
|| !*kernsrctree
)
511 kernsrctree
= srctree
;
516 /* Open file, exit on error */
517 infile
= fopen(argv
[2], "r");
518 if (infile
== NULL
) {
519 fprintf(stderr
, "docproc: ");
524 if (strcmp("doc", argv
[1]) == 0) {
525 /* Need to do this in two passes.
526 * First pass is used to collect all symbols exported
527 * in the various files;
528 * Second pass generate the documentation.
529 * This is required because some functions are declared
530 * and exported in different files :-((
532 /* Collect symbols */
533 defaultline
= noaction
;
534 internalfunctions
= find_export_symbols
;
535 externalfunctions
= find_export_symbols
;
536 symbolsonly
= find_export_symbols
;
537 singlefunctions
= noaction2
;
538 docsection
= noaction2
;
539 findall
= find_all_symbols
;
542 /* Rewind to start from beginning of file again */
543 fseek(infile
, 0, SEEK_SET
);
544 defaultline
= printline
;
545 internalfunctions
= intfunc
;
546 externalfunctions
= extfunc
;
547 symbolsonly
= printline
;
548 singlefunctions
= singfunc
;
549 docsection
= docsect
;
554 for (i
= 0; i
< all_list_len
; i
++) {
557 fprintf(stderr
, "Warning: didn't use docs for %s\n",
560 } else if (strcmp("depend", argv
[1]) == 0) {
561 /* Create first part of dependency chain
563 printf("%s\t", argv
[2]);
564 defaultline
= noaction
;
565 internalfunctions
= adddep
;
566 externalfunctions
= adddep
;
567 symbolsonly
= adddep
;
568 singlefunctions
= adddep2
;
569 docsection
= adddep2
;
574 fprintf(stderr
, "Unknown option: %s\n", argv
[1]);