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:
43 #include <sys/types.h>
46 /* exitstatus is used to keep track of any failing calls to kernel-doc,
47 * but execution continues. */
50 typedef void DFL(char *);
53 typedef void FILEONLY(char * file
);
54 FILEONLY
*internalfunctions
;
55 FILEONLY
*externalfunctions
;
56 FILEONLY
*symbolsonly
;
58 typedef void FILELINE(char * file
, char * line
);
59 FILELINE
* singlefunctions
;
60 FILELINE
* entity_system
;
61 FILELINE
* docsection
;
63 #define MAXLINESZ 2048
65 #define KERNELDOCPATH "scripts/"
66 #define KERNELDOC "kernel-doc"
67 #define DOCBOOK "-docbook"
68 #define FUNCTION "-function"
69 #define NOFUNCTION "-nofunction"
70 #define NODOCSECTIONS "-no-doc-sections"
72 static char *srctree
, *kernsrctree
;
74 static void usage (void)
76 fprintf(stderr
, "Usage: docproc {doc|depend} file\n");
77 fprintf(stderr
, "Input is read from file.tmpl. Output is sent to stdout\n");
78 fprintf(stderr
, "doc: frontend when generating kernel documentation\n");
79 fprintf(stderr
, "depend: generate list of files referenced within file\n");
80 fprintf(stderr
, "Environment variable SRCTREE: absolute path to sources.\n");
81 fprintf(stderr
, " KBUILD_SRC: absolute path to kernel source tree.\n");
85 * Execute kernel-doc with parameters given in svec
87 static void exec_kernel_doc(char **svec
)
91 char real_filename
[PATH_MAX
+ 1];
92 /* Make sure output generated so far are flushed */
99 memset(real_filename
, 0, sizeof(real_filename
));
100 strncat(real_filename
, kernsrctree
, PATH_MAX
);
101 strncat(real_filename
, "/" KERNELDOCPATH KERNELDOC
,
102 PATH_MAX
- strlen(real_filename
));
103 execvp(real_filename
, svec
);
104 fprintf(stderr
, "exec ");
105 perror(real_filename
);
108 waitpid(pid
, &ret
,0);
111 exitstatus
|= WEXITSTATUS(ret
);
116 /* Types used to create list of all exported symbols in a number of files */
125 struct symbols
*symbollist
;
129 struct symfile symfilelist
[MAXFILES
];
132 static void add_new_symbol(struct symfile
*sym
, char * symname
)
135 realloc(sym
->symbollist
, (sym
->symbolcnt
+ 1) * sizeof(char *));
136 sym
->symbollist
[sym
->symbolcnt
++].name
= strdup(symname
);
139 /* Add a filename to the list */
140 static struct symfile
* add_new_file(char * filename
)
142 symfilelist
[symfilecnt
++].filename
= strdup(filename
);
143 return &symfilelist
[symfilecnt
- 1];
146 /* Check if file already are present in the list */
147 static struct symfile
* filename_exist(char * filename
)
150 for (i
=0; i
< symfilecnt
; i
++)
151 if (strcmp(symfilelist
[i
].filename
, filename
) == 0)
152 return &symfilelist
[i
];
157 * List all files referenced within the template file.
158 * Files are separated by tabs.
160 static void adddep(char * file
) { printf("\t%s", file
); }
161 static void adddep2(char * file
, char * line
) { line
= line
; adddep(file
); }
162 static void noaction(char * line
) { line
= line
; }
163 static void noaction2(char * file
, char * line
) { file
= file
; line
= line
; }
165 /* Echo the line without further action */
166 static void printline(char * line
) { printf("%s", line
); }
169 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
170 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
171 * All symbols located are stored in symfilelist.
173 static void find_export_symbols(char * filename
)
177 char line
[MAXLINESZ
];
178 if (filename_exist(filename
) == NULL
) {
179 char real_filename
[PATH_MAX
+ 1];
180 memset(real_filename
, 0, sizeof(real_filename
));
181 strncat(real_filename
, srctree
, PATH_MAX
);
182 strncat(real_filename
, "/", PATH_MAX
- strlen(real_filename
));
183 strncat(real_filename
, filename
,
184 PATH_MAX
- strlen(real_filename
));
185 sym
= add_new_file(filename
);
186 fp
= fopen(real_filename
, "r");
189 fprintf(stderr
, "docproc: ");
190 perror(real_filename
);
193 while (fgets(line
, MAXLINESZ
, fp
)) {
196 if (((p
= strstr(line
, "EXPORT_SYMBOL_GPL")) != NULL
) ||
197 ((p
= strstr(line
, "EXPORT_SYMBOL")) != NULL
)) {
198 /* Skip EXPORT_SYMBOL{_GPL} */
199 while (isalnum(*p
) || *p
== '_')
201 /* Remove parentheses & additional whitespace */
205 continue; /* Syntax error? */
211 while (isalnum(*e
) || *e
== '_')
214 add_new_symbol(sym
, p
);
222 * Document all external or internal functions in a file.
223 * Call kernel-doc with following parameters:
224 * kernel-doc -docbook -nofunction function_name1 filename
225 * Function names are obtained from all the src files
226 * by find_export_symbols.
227 * intfunc uses -nofunction
228 * extfunc uses -function
230 static void docfunctions(char * filename
, char * type
)
237 for (i
=0; i
<= symfilecnt
; i
++)
238 symcnt
+= symfilelist
[i
].symbolcnt
;
239 vec
= malloc((2 + 2 * symcnt
+ 3) * sizeof(char *));
244 vec
[idx
++] = KERNELDOC
;
245 vec
[idx
++] = DOCBOOK
;
246 vec
[idx
++] = NODOCSECTIONS
;
247 for (i
=0; i
< symfilecnt
; i
++) {
248 struct symfile
* sym
= &symfilelist
[i
];
249 for (j
=0; j
< sym
->symbolcnt
; j
++) {
251 vec
[idx
++] = sym
->symbollist
[j
].name
;
254 vec
[idx
++] = filename
;
256 printf("<!-- %s -->\n", filename
);
257 exec_kernel_doc(vec
);
261 static void intfunc(char * filename
) { docfunctions(filename
, NOFUNCTION
); }
262 static void extfunc(char * filename
) { docfunctions(filename
, FUNCTION
); }
265 * Document specific function(s) in a file.
266 * Call kernel-doc with the following parameters:
267 * kernel-doc -docbook -function function1 [-function function2]
269 static void singfunc(char * filename
, char * line
)
271 char *vec
[200]; /* Enough for specific functions */
274 vec
[idx
++] = KERNELDOC
;
275 vec
[idx
++] = DOCBOOK
;
277 /* Split line up in individual parameters preceded by FUNCTION */
278 for (i
=0; line
[i
]; i
++) {
279 if (isspace(line
[i
])) {
286 vec
[idx
++] = FUNCTION
;
287 vec
[idx
++] = &line
[i
];
290 vec
[idx
++] = filename
;
292 exec_kernel_doc(vec
);
296 * Insert specific documentation section from a file.
297 * Call kernel-doc with the following parameters:
298 * kernel-doc -docbook -function "doc section" filename
300 static void docsect(char *filename
, char *line
)
302 char *vec
[6]; /* kerneldoc -docbook -function "section" file NULL */
305 for (s
= line
; *s
; s
++)
315 exec_kernel_doc(vec
);
319 * Parse file, calling action specific functions for:
320 * 1) Lines containing !E
321 * 2) Lines containing !I
322 * 3) Lines containing !D
323 * 4) Lines containing !F
324 * 5) Lines containing !P
325 * 6) Default lines - lines not matching the above
327 static void parse_file(FILE *infile
)
329 char line
[MAXLINESZ
];
331 while (fgets(line
, MAXLINESZ
, infile
)) {
332 if (line
[0] == '!') {
336 while (*s
&& !isspace(*s
)) s
++;
338 externalfunctions(line
+2);
341 while (*s
&& !isspace(*s
)) s
++;
343 internalfunctions(line
+2);
346 while (*s
&& !isspace(*s
)) s
++;
352 while (*s
&& !isspace(*s
)) s
++;
357 singlefunctions(line
+2, s
);
361 while (*s
&& !isspace(*s
)) s
++;
363 /* DOC: section name */
366 docsection(line
+ 2, s
);
380 int main(int argc
, char *argv
[])
384 srctree
= getenv("SRCTREE");
386 srctree
= getcwd(NULL
, 0);
387 kernsrctree
= getenv("KBUILD_SRC");
388 if (!kernsrctree
|| !*kernsrctree
)
389 kernsrctree
= srctree
;
394 /* Open file, exit on error */
395 infile
= fopen(argv
[2], "r");
396 if (infile
== NULL
) {
397 fprintf(stderr
, "docproc: ");
402 if (strcmp("doc", argv
[1]) == 0)
404 /* Need to do this in two passes.
405 * First pass is used to collect all symbols exported
406 * in the various files;
407 * Second pass generate the documentation.
408 * This is required because some functions are declared
409 * and exported in different files :-((
411 /* Collect symbols */
412 defaultline
= noaction
;
413 internalfunctions
= find_export_symbols
;
414 externalfunctions
= find_export_symbols
;
415 symbolsonly
= find_export_symbols
;
416 singlefunctions
= noaction2
;
417 docsection
= noaction2
;
420 /* Rewind to start from beginning of file again */
421 fseek(infile
, 0, SEEK_SET
);
422 defaultline
= printline
;
423 internalfunctions
= intfunc
;
424 externalfunctions
= extfunc
;
425 symbolsonly
= printline
;
426 singlefunctions
= singfunc
;
427 docsection
= docsect
;
431 else if (strcmp("depend", argv
[1]) == 0)
433 /* Create first part of dependency chain
435 printf("%s\t", argv
[2]);
436 defaultline
= noaction
;
437 internalfunctions
= adddep
;
438 externalfunctions
= adddep
;
439 symbolsonly
= adddep
;
440 singlefunctions
= adddep2
;
441 docsection
= adddep2
;
447 fprintf(stderr
, "Unknown option: %s\n", argv
[1]);