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 are scanned for
14 * any exported sympols "EXPORT_SYMBOL()" statements.
15 * This is used to create proper -function and
16 * -nofunction arguments in calls to kernel-doc.
17 * Usage: docproc doc file.tmpl
19 * dependency-generator:
20 * Scans the template file and list all files
21 * referenced in a format recognized by make.
22 * Usage: docproc depend file.tmpl
23 * Writes dependency information to stdout
24 * in the following format:
25 * file.tmpl src.c src2.c
26 * The filenames are obtained from the following constructs:
40 #include <sys/types.h>
43 /* exitstatus is used to keep track of any failing calls to kernel-doc,
44 * but execution continues. */
47 typedef void DFL(char *);
50 typedef void FILEONLY(char * file);
51 FILEONLY *internalfunctions;
52 FILEONLY *externalfunctions;
53 FILEONLY *symbolsonly;
55 typedef void FILELINE(char * file, char * line);
56 FILELINE * singlefunctions;
57 FILELINE * entity_system;
59 #define MAXLINESZ 2048
61 #define KERNELDOCPATH "scripts/"
62 #define KERNELDOC "kernel-doc"
63 #define DOCBOOK "-docbook"
64 #define FUNCTION "-function"
65 #define NOFUNCTION "-nofunction"
69 fprintf(stderr, "Usage: docproc {doc|depend} file\n");
70 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
71 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
72 fprintf(stderr, "depend: generate list of files referenced within file\n");
76 * Execute kernel-doc with parameters givin in svec
78 void exec_kernel_doc(char **svec)
85 /* Make sure output generated so far are flushed */
92 rflen = strlen(getenv("SRCTREE"));
93 rflen += strlen(KERNELDOCPATH KERNELDOC);
94 real_filename = alloca(rflen + 1);
95 strcpy(real_filename, getenv("SRCTREE"));
96 strcat(real_filename, KERNELDOCPATH KERNELDOC);
97 execvp(real_filename, svec);
98 fprintf(stderr, "exec ");
99 perror(real_filename);
102 waitpid(pid, &ret ,0);
105 exitstatus |= WEXITSTATUS(ret);
110 /* Types used to create list of all exported symbols in a number of files */
119 struct symbols *symbollist;
123 struct symfile symfilelist[MAXFILES];
126 void add_new_symbol(struct symfile *sym, char * symname)
129 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
130 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
133 /* Add a filename to the list */
134 struct symfile * add_new_file(char * filename)
136 symfilelist[symfilecnt++].filename = strdup(filename);
137 return &symfilelist[symfilecnt - 1];
139 /* Check if file already are present in the list */
140 struct symfile * filename_exist(char * filename)
143 for (i=0; i < symfilecnt; i++)
144 if (strcmp(symfilelist[i].filename, filename) == 0)
145 return &symfilelist[i];
150 * List all files referenced within the template file.
151 * Files are separated by tabs.
153 void adddep(char * file) { printf("\t%s", file); }
154 void adddep2(char * file, char * line) { line = line; adddep(file); }
155 void noaction(char * line) { line = line; }
156 void noaction2(char * file, char * line) { file = file; line = line; }
158 /* Echo the line without further action */
159 void printline(char * line) { printf("%s", line); }
162 * Find all symbols exported with EXPORT_SYMBOL and EXPORT_SYMBOL_GPL
164 * All symbols located are stored in symfilelist.
166 void find_export_symbols(char * filename)
170 char line[MAXLINESZ];
171 if (filename_exist(filename) == NULL) {
172 int rflen = strlen(getenv("SRCTREE")) + strlen(filename);
173 char *real_filename = alloca(rflen + 1);
174 strcpy(real_filename, getenv("SRCTREE"));
175 strcat(real_filename, filename);
176 sym = add_new_file(filename);
177 fp = fopen(real_filename, "r");
180 fprintf(stderr, "docproc: ");
181 perror(real_filename);
183 while (fgets(line, MAXLINESZ, fp)) {
186 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != 0) ||
187 ((p = strstr(line, "EXPORT_SYMBOL")) != 0)) {
188 /* Skip EXPORT_SYMBOL{_GPL} */
189 while (isalnum(*p) || *p == '_')
191 /* Remove paranteses and additional ws */
195 continue; /* Syntax error? */
201 while (isalnum(*e) || *e == '_')
204 add_new_symbol(sym, p);
212 * Document all external or internal functions in a file.
213 * Call kernel-doc with following parameters:
214 * kernel-doc -docbook -nofunction function_name1 filename
215 * function names are obtained from all the the src files
216 * by find_export_symbols.
217 * intfunc uses -nofunction
218 * extfunc uses -function
220 void docfunctions(char * filename, char * type)
227 for (i=0; i <= symfilecnt; i++)
228 symcnt += symfilelist[i].symbolcnt;
229 vec = malloc((2 + 2 * symcnt + 2) * sizeof(char*));
234 vec[idx++] = KERNELDOC;
235 vec[idx++] = DOCBOOK;
236 for (i=0; i < symfilecnt; i++) {
237 struct symfile * sym = &symfilelist[i];
238 for (j=0; j < sym->symbolcnt; j++) {
240 vec[idx++] = sym->symbollist[j].name;
243 vec[idx++] = filename;
245 printf("<!-- %s -->\n", filename);
246 exec_kernel_doc(vec);
250 void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
251 void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
254 * Document specific function(s) in a file.
255 * Call kernel-doc with the following parameters:
256 * kernel-doc -docbook -function function1 [-function function2]
258 void singfunc(char * filename, char * line)
260 char *vec[200]; /* Enough for specific functions */
263 vec[idx++] = KERNELDOC;
264 vec[idx++] = DOCBOOK;
266 /* Split line up in individual parameters preceeded by FUNCTION */
267 for (i=0; line[i]; i++) {
268 if (isspace(line[i])) {
275 vec[idx++] = FUNCTION;
276 vec[idx++] = &line[i];
279 vec[idx++] = filename;
281 exec_kernel_doc(vec);
285 * Parse file, calling action specific functions for:
286 * 1) Lines containing !E
287 * 2) Lines containing !I
288 * 3) Lines containing !D
289 * 4) Lines containing !F
290 * 5) Default lines - lines not matching the above
292 void parse_file(FILE *infile)
294 char line[MAXLINESZ];
296 while (fgets(line, MAXLINESZ, infile)) {
297 if (line[0] == '!') {
301 while (*s && !isspace(*s)) s++;
303 externalfunctions(line+2);
306 while (*s && !isspace(*s)) s++;
308 internalfunctions(line+2);
311 while (*s && !isspace(*s)) s++;
317 while (*s && !isspace(*s)) s++;
322 singlefunctions(line +2, s);
336 int main(int argc, char **argv)
343 /* Open file, exit on error */
344 infile = fopen(argv[2], "r");
345 if (infile == NULL) {
346 fprintf(stderr, "docproc: ");
351 if (strcmp("doc", argv[1]) == 0)
353 /* Need to do this in two passes.
354 * First pass is used to collect all symbols exported
355 * in the various files.
356 * Second pass generate the documentation.
357 * This is required because function are declared
358 * and exported in different files :-((
360 /* Collect symbols */
361 defaultline = noaction;
362 internalfunctions = find_export_symbols;
363 externalfunctions = find_export_symbols;
364 symbolsonly = find_export_symbols;
365 singlefunctions = noaction2;
368 /* Rewind to start from beginning of file again */
369 fseek(infile, 0, SEEK_SET);
370 defaultline = printline;
371 internalfunctions = intfunc;
372 externalfunctions = extfunc;
373 symbolsonly = printline;
374 singlefunctions = singfunc;
378 else if (strcmp("depend", argv[1]) == 0)
380 /* Create first part of dependency chain
382 printf("%s\t", argv[2]);
383 defaultline = noaction;
384 internalfunctions = adddep;
385 externalfunctions = adddep;
386 symbolsonly = adddep;
387 singlefunctions = adddep2;
393 fprintf(stderr, "Unknown option: %s\n", argv[1]);