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"
68 #define NODOCSECTIONS "-no-doc-sections"
74 fprintf(stderr, "Usage: docproc {doc|depend} file\n");
75 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
76 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
77 fprintf(stderr, "depend: generate list of files referenced within file\n");
78 fprintf(stderr, "Environment variable SRCTREE: absolute path to kernel source tree.\n");
82 * Execute kernel-doc with parameters given in svec
84 void exec_kernel_doc(char **svec)
88 char real_filename[PATH_MAX + 1];
89 /* Make sure output generated so far are flushed */
96 memset(real_filename, 0, sizeof(real_filename));
97 strncat(real_filename, srctree, PATH_MAX);
98 strncat(real_filename, KERNELDOCPATH KERNELDOC,
99 PATH_MAX - strlen(real_filename));
100 execvp(real_filename, svec);
101 fprintf(stderr, "exec ");
102 perror(real_filename);
105 waitpid(pid, &ret ,0);
108 exitstatus |= WEXITSTATUS(ret);
113 /* Types used to create list of all exported symbols in a number of files */
122 struct symbols *symbollist;
126 struct symfile symfilelist[MAXFILES];
129 void add_new_symbol(struct symfile *sym, char * symname)
132 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
133 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
136 /* Add a filename to the list */
137 struct symfile * add_new_file(char * filename)
139 symfilelist[symfilecnt++].filename = strdup(filename);
140 return &symfilelist[symfilecnt - 1];
143 /* Check if file already are present in the list */
144 struct symfile * filename_exist(char * filename)
147 for (i=0; i < symfilecnt; i++)
148 if (strcmp(symfilelist[i].filename, filename) == 0)
149 return &symfilelist[i];
154 * List all files referenced within the template file.
155 * Files are separated by tabs.
157 void adddep(char * file) { printf("\t%s", file); }
158 void adddep2(char * file, char * line) { line = line; adddep(file); }
159 void noaction(char * line) { line = line; }
160 void noaction2(char * file, char * line) { file = file; line = line; }
162 /* Echo the line without further action */
163 void printline(char * line) { printf("%s", line); }
166 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
167 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
168 * All symbols located are stored in symfilelist.
170 void find_export_symbols(char * filename)
174 char line[MAXLINESZ];
175 if (filename_exist(filename) == NULL) {
176 char real_filename[PATH_MAX + 1];
177 memset(real_filename, 0, sizeof(real_filename));
178 strncat(real_filename, srctree, PATH_MAX);
179 strncat(real_filename, filename,
180 PATH_MAX - strlen(real_filename));
181 sym = add_new_file(filename);
182 fp = fopen(real_filename, "r");
185 fprintf(stderr, "docproc: ");
186 perror(real_filename);
189 while (fgets(line, MAXLINESZ, fp)) {
192 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
193 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
194 /* Skip EXPORT_SYMBOL{_GPL} */
195 while (isalnum(*p) || *p == '_')
197 /* Remove parentheses & additional whitespace */
201 continue; /* Syntax error? */
207 while (isalnum(*e) || *e == '_')
210 add_new_symbol(sym, p);
218 * Document all external or internal functions in a file.
219 * Call kernel-doc with following parameters:
220 * kernel-doc -docbook -nofunction function_name1 filename
221 * Function names are obtained from all the src files
222 * by find_export_symbols.
223 * intfunc uses -nofunction
224 * extfunc uses -function
226 void docfunctions(char * filename, char * type)
233 for (i=0; i <= symfilecnt; i++)
234 symcnt += symfilelist[i].symbolcnt;
235 vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
240 vec[idx++] = KERNELDOC;
241 vec[idx++] = DOCBOOK;
242 vec[idx++] = NODOCSECTIONS;
243 for (i=0; i < symfilecnt; i++) {
244 struct symfile * sym = &symfilelist[i];
245 for (j=0; j < sym->symbolcnt; j++) {
247 vec[idx++] = sym->symbollist[j].name;
250 vec[idx++] = filename;
252 printf("<!-- %s -->\n", filename);
253 exec_kernel_doc(vec);
257 void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
258 void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
261 * Document specific function(s) in a file.
262 * Call kernel-doc with the following parameters:
263 * kernel-doc -docbook -function function1 [-function function2]
265 void singfunc(char * filename, char * line)
267 char *vec[200]; /* Enough for specific functions */
270 vec[idx++] = KERNELDOC;
271 vec[idx++] = DOCBOOK;
273 /* Split line up in individual parameters preceded by FUNCTION */
274 for (i=0; line[i]; i++) {
275 if (isspace(line[i])) {
282 vec[idx++] = FUNCTION;
283 vec[idx++] = &line[i];
286 vec[idx++] = filename;
288 exec_kernel_doc(vec);
292 * Parse file, calling action specific functions for:
293 * 1) Lines containing !E
294 * 2) Lines containing !I
295 * 3) Lines containing !D
296 * 4) Lines containing !F
297 * 5) Default lines - lines not matching the above
299 void parse_file(FILE *infile)
301 char line[MAXLINESZ];
303 while (fgets(line, MAXLINESZ, infile)) {
304 if (line[0] == '!') {
308 while (*s && !isspace(*s)) s++;
310 externalfunctions(line+2);
313 while (*s && !isspace(*s)) s++;
315 internalfunctions(line+2);
318 while (*s && !isspace(*s)) s++;
324 while (*s && !isspace(*s)) s++;
329 singlefunctions(line +2, s);
343 int main(int argc, char *argv[])
347 srctree = getenv("SRCTREE");
349 srctree = getcwd(NULL, 0);
354 /* Open file, exit on error */
355 infile = fopen(argv[2], "r");
356 if (infile == NULL) {
357 fprintf(stderr, "docproc: ");
362 if (strcmp("doc", argv[1]) == 0)
364 /* Need to do this in two passes.
365 * First pass is used to collect all symbols exported
366 * in the various files;
367 * Second pass generate the documentation.
368 * This is required because some functions are declared
369 * and exported in different files :-((
371 /* Collect symbols */
372 defaultline = noaction;
373 internalfunctions = find_export_symbols;
374 externalfunctions = find_export_symbols;
375 symbolsonly = find_export_symbols;
376 singlefunctions = noaction2;
379 /* Rewind to start from beginning of file again */
380 fseek(infile, 0, SEEK_SET);
381 defaultline = printline;
382 internalfunctions = intfunc;
383 externalfunctions = extfunc;
384 symbolsonly = printline;
385 singlefunctions = singfunc;
389 else if (strcmp("depend", argv[1]) == 0)
391 /* Create first part of dependency chain
393 printf("%s\t", argv[2]);
394 defaultline = noaction;
395 internalfunctions = adddep;
396 externalfunctions = adddep;
397 symbolsonly = adddep;
398 singlefunctions = adddep2;
404 fprintf(stderr, "Unknown option: %s\n", argv[1]);