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:
42 #include <sys/types.h>
45 /* exitstatus is used to keep track of any failing calls to kernel-doc,
46 * but execution continues. */
49 typedef void DFL(char *);
52 typedef void FILEONLY(char * file);
53 FILEONLY *internalfunctions;
54 FILEONLY *externalfunctions;
55 FILEONLY *symbolsonly;
57 typedef void FILELINE(char * file, char * line);
58 FILELINE * singlefunctions;
59 FILELINE * entity_system;
61 #define MAXLINESZ 2048
63 #define KERNELDOCPATH "scripts/"
64 #define KERNELDOC "kernel-doc"
65 #define DOCBOOK "-docbook"
66 #define FUNCTION "-function"
67 #define NOFUNCTION "-nofunction"
73 fprintf(stderr, "Usage: docproc {doc|depend} file\n");
74 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
75 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
76 fprintf(stderr, "depend: generate list of files referenced within file\n");
77 fprintf(stderr, "Environment variable SRCTREE: absolute path to kernel source tree.\n");
81 * Execute kernel-doc with parameters given in svec
83 void exec_kernel_doc(char **svec)
87 char real_filename[PATH_MAX + 1];
88 /* Make sure output generated so far are flushed */
95 memset(real_filename, 0, sizeof(real_filename));
96 strncat(real_filename, srctree, PATH_MAX);
97 strncat(real_filename, KERNELDOCPATH KERNELDOC,
98 PATH_MAX - strlen(real_filename));
99 execvp(real_filename, svec);
100 fprintf(stderr, "exec ");
101 perror(real_filename);
104 waitpid(pid, &ret ,0);
107 exitstatus |= WEXITSTATUS(ret);
112 /* Types used to create list of all exported symbols in a number of files */
121 struct symbols *symbollist;
125 struct symfile symfilelist[MAXFILES];
128 void add_new_symbol(struct symfile *sym, char * symname)
131 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
132 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
135 /* Add a filename to the list */
136 struct symfile * add_new_file(char * filename)
138 symfilelist[symfilecnt++].filename = strdup(filename);
139 return &symfilelist[symfilecnt - 1];
142 /* Check if file already are present in the list */
143 struct symfile * filename_exist(char * filename)
146 for (i=0; i < symfilecnt; i++)
147 if (strcmp(symfilelist[i].filename, filename) == 0)
148 return &symfilelist[i];
153 * List all files referenced within the template file.
154 * Files are separated by tabs.
156 void adddep(char * file) { printf("\t%s", file); }
157 void adddep2(char * file, char * line) { line = line; adddep(file); }
158 void noaction(char * line) { line = line; }
159 void noaction2(char * file, char * line) { file = file; line = line; }
161 /* Echo the line without further action */
162 void printline(char * line) { printf("%s", line); }
165 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
166 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
167 * All symbols located are stored in symfilelist.
169 void find_export_symbols(char * filename)
173 char line[MAXLINESZ];
174 if (filename_exist(filename) == NULL) {
175 char real_filename[PATH_MAX + 1];
176 memset(real_filename, 0, sizeof(real_filename));
177 strncat(real_filename, srctree, PATH_MAX);
178 strncat(real_filename, filename,
179 PATH_MAX - strlen(real_filename));
180 sym = add_new_file(filename);
181 fp = fopen(real_filename, "r");
184 fprintf(stderr, "docproc: ");
185 perror(real_filename);
188 while (fgets(line, MAXLINESZ, fp)) {
191 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
192 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
193 /* Skip EXPORT_SYMBOL{_GPL} */
194 while (isalnum(*p) || *p == '_')
196 /* Remove parentheses & additional whitespace */
200 continue; /* Syntax error? */
206 while (isalnum(*e) || *e == '_')
209 add_new_symbol(sym, p);
217 * Document all external or internal functions in a file.
218 * Call kernel-doc with following parameters:
219 * kernel-doc -docbook -nofunction function_name1 filename
220 * Function names are obtained from all the src files
221 * by find_export_symbols.
222 * intfunc uses -nofunction
223 * extfunc uses -function
225 void docfunctions(char * filename, char * type)
232 for (i=0; i <= symfilecnt; i++)
233 symcnt += symfilelist[i].symbolcnt;
234 vec = malloc((2 + 2 * symcnt + 2) * sizeof(char*));
239 vec[idx++] = KERNELDOC;
240 vec[idx++] = DOCBOOK;
241 for (i=0; i < symfilecnt; i++) {
242 struct symfile * sym = &symfilelist[i];
243 for (j=0; j < sym->symbolcnt; j++) {
245 vec[idx++] = sym->symbollist[j].name;
248 vec[idx++] = filename;
250 printf("<!-- %s -->\n", filename);
251 exec_kernel_doc(vec);
255 void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
256 void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
259 * Document specific function(s) in a file.
260 * Call kernel-doc with the following parameters:
261 * kernel-doc -docbook -function function1 [-function function2]
263 void singfunc(char * filename, char * line)
265 char *vec[200]; /* Enough for specific functions */
268 vec[idx++] = KERNELDOC;
269 vec[idx++] = DOCBOOK;
271 /* Split line up in individual parameters preceded by FUNCTION */
272 for (i=0; line[i]; i++) {
273 if (isspace(line[i])) {
280 vec[idx++] = FUNCTION;
281 vec[idx++] = &line[i];
284 vec[idx++] = filename;
286 exec_kernel_doc(vec);
290 * Parse file, calling action specific functions for:
291 * 1) Lines containing !E
292 * 2) Lines containing !I
293 * 3) Lines containing !D
294 * 4) Lines containing !F
295 * 5) Default lines - lines not matching the above
297 void parse_file(FILE *infile)
299 char line[MAXLINESZ];
301 while (fgets(line, MAXLINESZ, infile)) {
302 if (line[0] == '!') {
306 while (*s && !isspace(*s)) s++;
308 externalfunctions(line+2);
311 while (*s && !isspace(*s)) s++;
313 internalfunctions(line+2);
316 while (*s && !isspace(*s)) s++;
322 while (*s && !isspace(*s)) s++;
327 singlefunctions(line +2, s);
341 int main(int argc, char *argv[])
345 srctree = getenv("SRCTREE");
347 srctree = getcwd(NULL, 0);
352 /* Open file, exit on error */
353 infile = fopen(argv[2], "r");
354 if (infile == NULL) {
355 fprintf(stderr, "docproc: ");
360 if (strcmp("doc", argv[1]) == 0)
362 /* Need to do this in two passes.
363 * First pass is used to collect all symbols exported
364 * in the various files;
365 * Second pass generate the documentation.
366 * This is required because some functions are declared
367 * and exported in different files :-((
369 /* Collect symbols */
370 defaultline = noaction;
371 internalfunctions = find_export_symbols;
372 externalfunctions = find_export_symbols;
373 symbolsonly = find_export_symbols;
374 singlefunctions = noaction2;
377 /* Rewind to start from beginning of file again */
378 fseek(infile, 0, SEEK_SET);
379 defaultline = printline;
380 internalfunctions = intfunc;
381 externalfunctions = extfunc;
382 symbolsonly = printline;
383 singlefunctions = singfunc;
387 else if (strcmp("depend", argv[1]) == 0)
389 /* Create first part of dependency chain
391 printf("%s\t", argv[2]);
392 defaultline = noaction;
393 internalfunctions = adddep;
394 externalfunctions = adddep;
395 symbolsonly = adddep;
396 singlefunctions = adddep2;
402 fprintf(stderr, "Unknown option: %s\n", argv[1]);