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"
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 kernel source tree.\n");
84 * Execute kernel-doc with parameters given in svec
86 void exec_kernel_doc(char **svec
)
90 char real_filename
[PATH_MAX
+ 1];
91 /* Make sure output generated so far are flushed */
98 memset(real_filename
, 0, sizeof(real_filename
));
99 strncat(real_filename
, srctree
, PATH_MAX
);
100 strncat(real_filename
, KERNELDOCPATH KERNELDOC
,
101 PATH_MAX
- strlen(real_filename
));
102 execvp(real_filename
, svec
);
103 fprintf(stderr
, "exec ");
104 perror(real_filename
);
107 waitpid(pid
, &ret
,0);
110 exitstatus
|= WEXITSTATUS(ret
);
115 /* Types used to create list of all exported symbols in a number of files */
124 struct symbols
*symbollist
;
128 struct symfile symfilelist
[MAXFILES
];
131 void add_new_symbol(struct symfile
*sym
, char * symname
)
134 realloc(sym
->symbollist
, (sym
->symbolcnt
+ 1) * sizeof(char *));
135 sym
->symbollist
[sym
->symbolcnt
++].name
= strdup(symname
);
138 /* Add a filename to the list */
139 struct symfile
* add_new_file(char * filename
)
141 symfilelist
[symfilecnt
++].filename
= strdup(filename
);
142 return &symfilelist
[symfilecnt
- 1];
145 /* Check if file already are present in the list */
146 struct symfile
* filename_exist(char * filename
)
149 for (i
=0; i
< symfilecnt
; i
++)
150 if (strcmp(symfilelist
[i
].filename
, filename
) == 0)
151 return &symfilelist
[i
];
156 * List all files referenced within the template file.
157 * Files are separated by tabs.
159 void adddep(char * file
) { printf("\t%s", file
); }
160 void adddep2(char * file
, char * line
) { line
= line
; adddep(file
); }
161 void noaction(char * line
) { line
= line
; }
162 void noaction2(char * file
, char * line
) { file
= file
; line
= line
; }
164 /* Echo the line without further action */
165 void printline(char * line
) { printf("%s", line
); }
168 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
169 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
170 * All symbols located are stored in symfilelist.
172 void find_export_symbols(char * filename
)
176 char line
[MAXLINESZ
];
177 if (filename_exist(filename
) == NULL
) {
178 char real_filename
[PATH_MAX
+ 1];
179 memset(real_filename
, 0, sizeof(real_filename
));
180 strncat(real_filename
, srctree
, PATH_MAX
);
181 strncat(real_filename
, filename
,
182 PATH_MAX
- strlen(real_filename
));
183 sym
= add_new_file(filename
);
184 fp
= fopen(real_filename
, "r");
187 fprintf(stderr
, "docproc: ");
188 perror(real_filename
);
191 while (fgets(line
, MAXLINESZ
, fp
)) {
194 if (((p
= strstr(line
, "EXPORT_SYMBOL_GPL")) != NULL
) ||
195 ((p
= strstr(line
, "EXPORT_SYMBOL")) != NULL
)) {
196 /* Skip EXPORT_SYMBOL{_GPL} */
197 while (isalnum(*p
) || *p
== '_')
199 /* Remove parentheses & additional whitespace */
203 continue; /* Syntax error? */
209 while (isalnum(*e
) || *e
== '_')
212 add_new_symbol(sym
, p
);
220 * Document all external or internal functions in a file.
221 * Call kernel-doc with following parameters:
222 * kernel-doc -docbook -nofunction function_name1 filename
223 * Function names are obtained from all the src files
224 * by find_export_symbols.
225 * intfunc uses -nofunction
226 * extfunc uses -function
228 void docfunctions(char * filename
, char * type
)
235 for (i
=0; i
<= symfilecnt
; i
++)
236 symcnt
+= symfilelist
[i
].symbolcnt
;
237 vec
= malloc((2 + 2 * symcnt
+ 3) * sizeof(char *));
242 vec
[idx
++] = KERNELDOC
;
243 vec
[idx
++] = DOCBOOK
;
244 vec
[idx
++] = NODOCSECTIONS
;
245 for (i
=0; i
< symfilecnt
; i
++) {
246 struct symfile
* sym
= &symfilelist
[i
];
247 for (j
=0; j
< sym
->symbolcnt
; j
++) {
249 vec
[idx
++] = sym
->symbollist
[j
].name
;
252 vec
[idx
++] = filename
;
254 printf("<!-- %s -->\n", filename
);
255 exec_kernel_doc(vec
);
259 void intfunc(char * filename
) { docfunctions(filename
, NOFUNCTION
); }
260 void extfunc(char * filename
) { docfunctions(filename
, FUNCTION
); }
263 * Document specific function(s) in a file.
264 * Call kernel-doc with the following parameters:
265 * kernel-doc -docbook -function function1 [-function function2]
267 void singfunc(char * filename
, char * line
)
269 char *vec
[200]; /* Enough for specific functions */
272 vec
[idx
++] = KERNELDOC
;
273 vec
[idx
++] = DOCBOOK
;
275 /* Split line up in individual parameters preceded by FUNCTION */
276 for (i
=0; line
[i
]; i
++) {
277 if (isspace(line
[i
])) {
284 vec
[idx
++] = FUNCTION
;
285 vec
[idx
++] = &line
[i
];
288 vec
[idx
++] = filename
;
290 exec_kernel_doc(vec
);
294 * Insert specific documentation section from a file.
295 * Call kernel-doc with the following parameters:
296 * kernel-doc -docbook -function "doc section" filename
298 void docsect(char *filename
, char *line
)
300 char *vec
[6]; /* kerneldoc -docbook -function "section" file NULL */
303 for (s
= line
; *s
; s
++)
313 exec_kernel_doc(vec
);
317 * Parse file, calling action specific functions for:
318 * 1) Lines containing !E
319 * 2) Lines containing !I
320 * 3) Lines containing !D
321 * 4) Lines containing !F
322 * 5) Lines containing !P
323 * 6) Default lines - lines not matching the above
325 void parse_file(FILE *infile
)
327 char line
[MAXLINESZ
];
329 while (fgets(line
, MAXLINESZ
, infile
)) {
330 if (line
[0] == '!') {
334 while (*s
&& !isspace(*s
)) s
++;
336 externalfunctions(line
+2);
339 while (*s
&& !isspace(*s
)) s
++;
341 internalfunctions(line
+2);
344 while (*s
&& !isspace(*s
)) s
++;
350 while (*s
&& !isspace(*s
)) s
++;
355 singlefunctions(line
+2, s
);
359 while (*s
&& !isspace(*s
)) s
++;
361 /* DOC: section name */
364 docsection(line
+ 2, s
);
378 int main(int argc
, char *argv
[])
382 srctree
= getenv("SRCTREE");
384 srctree
= getcwd(NULL
, 0);
389 /* Open file, exit on error */
390 infile
= fopen(argv
[2], "r");
391 if (infile
== NULL
) {
392 fprintf(stderr
, "docproc: ");
397 if (strcmp("doc", argv
[1]) == 0)
399 /* Need to do this in two passes.
400 * First pass is used to collect all symbols exported
401 * in the various files;
402 * Second pass generate the documentation.
403 * This is required because some functions are declared
404 * and exported in different files :-((
406 /* Collect symbols */
407 defaultline
= noaction
;
408 internalfunctions
= find_export_symbols
;
409 externalfunctions
= find_export_symbols
;
410 symbolsonly
= find_export_symbols
;
411 singlefunctions
= noaction2
;
412 docsection
= noaction2
;
415 /* Rewind to start from beginning of file again */
416 fseek(infile
, 0, SEEK_SET
);
417 defaultline
= printline
;
418 internalfunctions
= intfunc
;
419 externalfunctions
= extfunc
;
420 symbolsonly
= printline
;
421 singlefunctions
= singfunc
;
422 docsection
= docsect
;
426 else if (strcmp("depend", argv
[1]) == 0)
428 /* Create first part of dependency chain
430 printf("%s\t", argv
[2]);
431 defaultline
= noaction
;
432 internalfunctions
= adddep
;
433 externalfunctions
= adddep
;
434 symbolsonly
= adddep
;
435 singlefunctions
= adddep2
;
436 docsection
= adddep2
;
442 fprintf(stderr
, "Unknown option: %s\n", argv
[1]);