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, signed 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)
82 char real_filename[PATH_MAX + 1];
83 /* Make sure output generated so far are flushed */
90 memset(real_filename, 0, sizeof(real_filename));
91 strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
92 strncat(real_filename, KERNELDOCPATH KERNELDOC,
93 PATH_MAX - strlen(real_filename));
94 execvp(real_filename, svec);
95 fprintf(stderr, "exec ");
96 perror(real_filename);
99 waitpid(pid, &ret ,0);
102 exitstatus |= WEXITSTATUS(ret);
107 /* Types used to create list of all exported symbols in a number of files */
116 struct symbols *symbollist;
120 struct symfile symfilelist[MAXFILES];
123 void add_new_symbol(struct symfile *sym, char * symname)
126 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
127 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
130 /* Add a filename to the list */
131 struct symfile * add_new_file(char * filename)
133 symfilelist[symfilecnt++].filename = strdup(filename);
134 return &symfilelist[symfilecnt - 1];
136 /* Check if file already are present in the list */
137 struct symfile * filename_exist(char * filename)
140 for (i=0; i < symfilecnt; i++)
141 if (strcmp(symfilelist[i].filename, filename) == 0)
142 return &symfilelist[i];
147 * List all files referenced within the template file.
148 * Files are separated by tabs.
150 void adddep(char * file) { printf("\t%s", file); }
151 void adddep2(char * file, signed char * line) { line = line; adddep(file); }
152 void noaction(char * line) { line = line; }
153 void noaction2(char * file, signed char * line) { file = file; line = line; }
155 /* Echo the line without further action */
156 void printline(char * line) { printf("%s", line); }
159 * Find all symbols exported with EXPORT_SYMBOL and EXPORT_SYMBOL_GPL
161 * All symbols located are stored in symfilelist.
163 void find_export_symbols(char * filename)
167 char line[MAXLINESZ];
168 if (filename_exist(filename) == NULL) {
169 char real_filename[PATH_MAX + 1];
170 memset(real_filename, 0, sizeof(real_filename));
171 strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
172 strncat(real_filename, filename,
173 PATH_MAX - strlen(real_filename));
174 sym = add_new_file(filename);
175 fp = fopen(real_filename, "r");
178 fprintf(stderr, "docproc: ");
179 perror(real_filename);
181 while(fgets(line, MAXLINESZ, fp)) {
184 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != 0) ||
185 ((p = strstr(line, "EXPORT_SYMBOL")) != 0)) {
186 /* Skip EXPORT_SYMBOL{_GPL} */
187 while (isalnum(*p) || *p == '_')
189 /* Remove paranteses and additional ws */
193 continue; /* Syntax error? */
199 while (isalnum(*e) || *e == '_')
202 add_new_symbol(sym, p);
210 * Document all external or internal functions in a file.
211 * Call kernel-doc with following parameters:
212 * kernel-doc -docbook -nofunction function_name1 filename
213 * function names are obtained from all the the src files
214 * by find_export_symbols.
215 * intfunc uses -nofunction
216 * extfunc uses -function
218 void docfunctions(char * filename, char * type)
225 for (i=0; i <= symfilecnt; i++)
226 symcnt += symfilelist[i].symbolcnt;
227 vec = malloc((2 + 2 * symcnt + 2) * sizeof(char*));
232 vec[idx++] = KERNELDOC;
233 vec[idx++] = DOCBOOK;
234 for (i=0; i < symfilecnt; i++) {
235 struct symfile * sym = &symfilelist[i];
236 for (j=0; j < sym->symbolcnt; j++) {
238 vec[idx++] = sym->symbollist[j].name;
241 vec[idx++] = filename;
243 printf("<!-- %s -->\n", filename);
244 exec_kernel_doc(vec);
248 void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
249 void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
252 * Document spÄecific function(s) in a file.
253 * Call kernel-doc with the following parameters:
254 * kernel-doc -docbook -function function1 [-function function2]
256 void singfunc(char * filename, signed char * line)
258 char *vec[200]; /* Enough for specific functions */
261 vec[idx++] = KERNELDOC;
262 vec[idx++] = DOCBOOK;
264 /* Split line up in individual parameters preceeded by FUNCTION */
265 for (i=0; line[i]; i++) {
266 if (isspace(line[i])) {
273 vec[idx++] = FUNCTION;
274 vec[idx++] = &line[i];
277 vec[idx++] = filename;
279 exec_kernel_doc(vec);
283 * Parse file, calling action specific functions for:
284 * 1) Lines containing !E
285 * 2) Lines containing !I
286 * 3) Lines containing !D
287 * 4) Lines containing !F
288 * 5) Default lines - lines not matching the above
290 void parse_file(FILE *infile)
292 char line[MAXLINESZ];
294 while(fgets(line, MAXLINESZ, infile)) {
295 if (line[0] == '!') {
299 while (*s && !isspace(*s)) s++;
301 externalfunctions(line+2);
304 while (*s && !isspace(*s)) s++;
306 internalfunctions(line+2);
309 while (*s && !isspace(*s)) s++;
315 while (*s && !isspace(*s)) s++;
320 singlefunctions(line +2, s);
334 int main(int argc, char *argv[])
341 /* Open file, exit on error */
342 infile = fopen(argv[2], "r");
343 if (infile == NULL) {
344 fprintf(stderr, "docproc: ");
349 if (strcmp("doc", argv[1]) == 0)
351 /* Need to do this in two passes.
352 * First pass is used to collect all symbols exported
353 * in the various files.
354 * Second pass generate the documentation.
355 * This is required because function are declared
356 * and exported in different files :-((
358 /* Collect symbols */
359 defaultline = noaction;
360 internalfunctions = find_export_symbols;
361 externalfunctions = find_export_symbols;
362 symbolsonly = find_export_symbols;
363 singlefunctions = noaction2;
366 /* Rewind to start from beginning of file again */
367 fseek(infile, 0, SEEK_SET);
368 defaultline = printline;
369 internalfunctions = intfunc;
370 externalfunctions = extfunc;
371 symbolsonly = printline;
372 singlefunctions = singfunc;
376 else if (strcmp("depend", argv[1]) == 0)
378 /* Create first part of dependency chain
380 printf("%s\t", argv[2]);
381 defaultline = noaction;
382 internalfunctions = adddep;
383 externalfunctions = adddep;
384 symbolsonly = adddep;
385 singlefunctions = adddep2;
391 fprintf(stderr, "Unknown option: %s\n", argv[1]);