blob: 99ca7a698687a5a0558b987b05ffb6c581c01141 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
8 * are kept together.
9 *
10 * documentation-frontend
11 * Scans the template file and call kernel-doc for
12 * all occurrences of ![EIF]file
Randy Dunlap6dd16f42007-09-04 21:23:22 -070013 * 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()
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * This is used to create proper -function and
18 * -nofunction arguments in calls to kernel-doc.
19 * Usage: docproc doc file.tmpl
20 *
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:
29 * !Efilename
30 * !Ifilename
31 * !Dfilename
32 * !Ffilename
Johannes Berge662af42007-10-24 15:08:48 -070033 * !Pfilename
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <ctype.h>
41#include <unistd.h>
42#include <limits.h>
43#include <sys/types.h>
44#include <sys/wait.h>
45
46/* exitstatus is used to keep track of any failing calls to kernel-doc,
47 * but execution continues. */
48int exitstatus = 0;
49
50typedef void DFL(char *);
51DFL *defaultline;
52
53typedef void FILEONLY(char * file);
54FILEONLY *internalfunctions;
55FILEONLY *externalfunctions;
56FILEONLY *symbolsonly;
57
J.A. Magallon48b9d032005-06-25 14:59:22 -070058typedef void FILELINE(char * file, char * line);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059FILELINE * singlefunctions;
60FILELINE * entity_system;
Johannes Berge662af42007-10-24 15:08:48 -070061FILELINE * docsection;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define MAXLINESZ 2048
64#define MAXFILES 250
65#define KERNELDOCPATH "scripts/"
66#define KERNELDOC "kernel-doc"
67#define DOCBOOK "-docbook"
68#define FUNCTION "-function"
69#define NOFUNCTION "-nofunction"
Johannes Berg2e959722007-10-24 15:08:48 -070070#define NODOCSECTIONS "-no-doc-sections"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jiri Slaby2d510052009-05-31 18:05:34 +020072static char *srctree, *kernsrctree;
Rob Landleybb13be52007-10-09 01:25:18 -050073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074void usage (void)
75{
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");
Jiri Slaby2d510052009-05-31 18:05:34 +020080 fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n");
81 fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84/*
Randy Dunlap6dd16f42007-09-04 21:23:22 -070085 * Execute kernel-doc with parameters given in svec
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87void exec_kernel_doc(char **svec)
88{
89 pid_t pid;
90 int ret;
91 char real_filename[PATH_MAX + 1];
92 /* Make sure output generated so far are flushed */
93 fflush(stdout);
Randy Dunlap6dd16f42007-09-04 21:23:22 -070094 switch (pid=fork()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 case -1:
96 perror("fork");
97 exit(1);
98 case 0:
99 memset(real_filename, 0, sizeof(real_filename));
Jiri Slaby2d510052009-05-31 18:05:34 +0200100 strncat(real_filename, kernsrctree, PATH_MAX);
101 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 PATH_MAX - strlen(real_filename));
103 execvp(real_filename, svec);
104 fprintf(stderr, "exec ");
105 perror(real_filename);
106 exit(1);
107 default:
108 waitpid(pid, &ret ,0);
109 }
110 if (WIFEXITED(ret))
111 exitstatus |= WEXITSTATUS(ret);
112 else
113 exitstatus = 0xff;
114}
115
116/* Types used to create list of all exported symbols in a number of files */
117struct symbols
118{
119 char *name;
120};
121
122struct symfile
123{
124 char *filename;
125 struct symbols *symbollist;
126 int symbolcnt;
127};
128
129struct symfile symfilelist[MAXFILES];
130int symfilecnt = 0;
131
132void add_new_symbol(struct symfile *sym, char * symname)
133{
134 sym->symbollist =
135 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
136 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
137}
138
139/* Add a filename to the list */
140struct symfile * add_new_file(char * filename)
141{
142 symfilelist[symfilecnt++].filename = strdup(filename);
143 return &symfilelist[symfilecnt - 1];
144}
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* Check if file already are present in the list */
147struct symfile * filename_exist(char * filename)
148{
149 int i;
150 for (i=0; i < symfilecnt; i++)
151 if (strcmp(symfilelist[i].filename, filename) == 0)
152 return &symfilelist[i];
153 return NULL;
154}
155
156/*
157 * List all files referenced within the template file.
158 * Files are separated by tabs.
159 */
160void adddep(char * file) { printf("\t%s", file); }
J.A. Magallon48b9d032005-06-25 14:59:22 -0700161void adddep2(char * file, char * line) { line = line; adddep(file); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162void noaction(char * line) { line = line; }
J.A. Magallon48b9d032005-06-25 14:59:22 -0700163void noaction2(char * file, char * line) { file = file; line = line; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* Echo the line without further action */
166void printline(char * line) { printf("%s", line); }
167
168/*
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700169 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
170 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * All symbols located are stored in symfilelist.
172 */
173void find_export_symbols(char * filename)
174{
175 FILE * fp;
176 struct symfile *sym;
177 char line[MAXLINESZ];
178 if (filename_exist(filename) == NULL) {
179 char real_filename[PATH_MAX + 1];
180 memset(real_filename, 0, sizeof(real_filename));
Rob Landleybb13be52007-10-09 01:25:18 -0500181 strncat(real_filename, srctree, PATH_MAX);
Jiri Slaby2d510052009-05-31 18:05:34 +0200182 strncat(real_filename, "/", PATH_MAX - strlen(real_filename));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 strncat(real_filename, filename,
184 PATH_MAX - strlen(real_filename));
185 sym = add_new_file(filename);
186 fp = fopen(real_filename, "r");
187 if (fp == NULL)
188 {
189 fprintf(stderr, "docproc: ");
190 perror(real_filename);
Henrik Kretzschmar074a5dd2006-09-29 02:00:56 -0700191 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700193 while (fgets(line, MAXLINESZ, fp)) {
J.A. Magallon48b9d032005-06-25 14:59:22 -0700194 char *p;
195 char *e;
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700196 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
197 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Skip EXPORT_SYMBOL{_GPL} */
199 while (isalnum(*p) || *p == '_')
200 p++;
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700201 /* Remove parentheses & additional whitespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 while (isspace(*p))
203 p++;
204 if (*p != '(')
205 continue; /* Syntax error? */
206 else
207 p++;
208 while (isspace(*p))
209 p++;
210 e = p;
211 while (isalnum(*e) || *e == '_')
212 e++;
213 *e = '\0';
214 add_new_symbol(sym, p);
215 }
216 }
217 fclose(fp);
218 }
219}
220
221/*
222 * Document all external or internal functions in a file.
223 * Call kernel-doc with following parameters:
224 * kernel-doc -docbook -nofunction function_name1 filename
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700225 * Function names are obtained from all the src files
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * by find_export_symbols.
227 * intfunc uses -nofunction
228 * extfunc uses -function
229 */
230void docfunctions(char * filename, char * type)
231{
232 int i,j;
233 int symcnt = 0;
234 int idx = 0;
235 char **vec;
236
237 for (i=0; i <= symfilecnt; i++)
238 symcnt += symfilelist[i].symbolcnt;
Johannes Berg2e959722007-10-24 15:08:48 -0700239 vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (vec == NULL) {
241 perror("docproc: ");
242 exit(1);
243 }
244 vec[idx++] = KERNELDOC;
245 vec[idx++] = DOCBOOK;
Johannes Berg2e959722007-10-24 15:08:48 -0700246 vec[idx++] = NODOCSECTIONS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 for (i=0; i < symfilecnt; i++) {
248 struct symfile * sym = &symfilelist[i];
249 for (j=0; j < sym->symbolcnt; j++) {
250 vec[idx++] = type;
251 vec[idx++] = sym->symbollist[j].name;
252 }
253 }
254 vec[idx++] = filename;
255 vec[idx] = NULL;
256 printf("<!-- %s -->\n", filename);
257 exec_kernel_doc(vec);
258 fflush(stdout);
259 free(vec);
260}
261void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
262void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
263
264/*
Randy Dunlapc6120932006-11-02 22:07:01 -0800265 * Document specific function(s) in a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * Call kernel-doc with the following parameters:
267 * kernel-doc -docbook -function function1 [-function function2]
268 */
J.A. Magallon48b9d032005-06-25 14:59:22 -0700269void singfunc(char * filename, char * line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 char *vec[200]; /* Enough for specific functions */
272 int i, idx = 0;
273 int startofsym = 1;
274 vec[idx++] = KERNELDOC;
275 vec[idx++] = DOCBOOK;
276
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700277 /* Split line up in individual parameters preceded by FUNCTION */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 for (i=0; line[i]; i++) {
279 if (isspace(line[i])) {
280 line[i] = '\0';
281 startofsym = 1;
282 continue;
283 }
284 if (startofsym) {
285 startofsym = 0;
286 vec[idx++] = FUNCTION;
287 vec[idx++] = &line[i];
288 }
289 }
290 vec[idx++] = filename;
291 vec[idx] = NULL;
292 exec_kernel_doc(vec);
293}
294
295/*
Johannes Berge662af42007-10-24 15:08:48 -0700296 * Insert specific documentation section from a file.
297 * Call kernel-doc with the following parameters:
298 * kernel-doc -docbook -function "doc section" filename
299 */
300void docsect(char *filename, char *line)
301{
302 char *vec[6]; /* kerneldoc -docbook -function "section" file NULL */
303 char *s;
304
305 for (s = line; *s; s++)
306 if (*s == '\n')
307 *s = '\0';
308
309 vec[0] = KERNELDOC;
310 vec[1] = DOCBOOK;
311 vec[2] = FUNCTION;
312 vec[3] = line;
313 vec[4] = filename;
314 vec[5] = NULL;
315 exec_kernel_doc(vec);
316}
317
318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * Parse file, calling action specific functions for:
320 * 1) Lines containing !E
321 * 2) Lines containing !I
322 * 3) Lines containing !D
323 * 4) Lines containing !F
Johannes Berge662af42007-10-24 15:08:48 -0700324 * 5) Lines containing !P
325 * 6) Default lines - lines not matching the above
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327void parse_file(FILE *infile)
328{
329 char line[MAXLINESZ];
J.A. Magallon48b9d032005-06-25 14:59:22 -0700330 char * s;
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700331 while (fgets(line, MAXLINESZ, infile)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (line[0] == '!') {
333 s = line + 2;
334 switch (line[1]) {
335 case 'E':
336 while (*s && !isspace(*s)) s++;
337 *s = '\0';
338 externalfunctions(line+2);
339 break;
340 case 'I':
341 while (*s && !isspace(*s)) s++;
342 *s = '\0';
343 internalfunctions(line+2);
344 break;
345 case 'D':
346 while (*s && !isspace(*s)) s++;
347 *s = '\0';
348 symbolsonly(line+2);
349 break;
350 case 'F':
351 /* filename */
352 while (*s && !isspace(*s)) s++;
353 *s++ = '\0';
354 /* function names */
355 while (isspace(*s))
356 s++;
357 singlefunctions(line +2, s);
358 break;
Johannes Berge662af42007-10-24 15:08:48 -0700359 case 'P':
360 /* filename */
361 while (*s && !isspace(*s)) s++;
362 *s++ = '\0';
363 /* DOC: section name */
364 while (isspace(*s))
365 s++;
366 docsection(line + 2, s);
367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 default:
369 defaultline(line);
370 }
371 }
372 else {
373 defaultline(line);
374 }
375 }
376 fflush(stdout);
377}
378
379
380int main(int argc, char *argv[])
381{
382 FILE * infile;
Rob Landleybb13be52007-10-09 01:25:18 -0500383
384 srctree = getenv("SRCTREE");
385 if (!srctree)
386 srctree = getcwd(NULL, 0);
Jiri Slaby2d510052009-05-31 18:05:34 +0200387 kernsrctree = getenv("KBUILD_SRC");
Amerigo Wangb767b902009-06-19 03:06:54 -0400388 if (!kernsrctree || !*kernsrctree)
Jiri Slaby2d510052009-05-31 18:05:34 +0200389 kernsrctree = srctree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (argc != 3) {
391 usage();
392 exit(1);
393 }
394 /* Open file, exit on error */
395 infile = fopen(argv[2], "r");
396 if (infile == NULL) {
397 fprintf(stderr, "docproc: ");
398 perror(argv[2]);
399 exit(2);
400 }
401
402 if (strcmp("doc", argv[1]) == 0)
403 {
404 /* Need to do this in two passes.
405 * First pass is used to collect all symbols exported
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700406 * in the various files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * Second pass generate the documentation.
Randy Dunlap6dd16f42007-09-04 21:23:22 -0700408 * This is required because some functions are declared
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * and exported in different files :-((
410 */
411 /* Collect symbols */
412 defaultline = noaction;
413 internalfunctions = find_export_symbols;
414 externalfunctions = find_export_symbols;
415 symbolsonly = find_export_symbols;
416 singlefunctions = noaction2;
Johannes Berge662af42007-10-24 15:08:48 -0700417 docsection = noaction2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 parse_file(infile);
419
420 /* Rewind to start from beginning of file again */
421 fseek(infile, 0, SEEK_SET);
422 defaultline = printline;
423 internalfunctions = intfunc;
424 externalfunctions = extfunc;
425 symbolsonly = printline;
426 singlefunctions = singfunc;
Johannes Berge662af42007-10-24 15:08:48 -0700427 docsection = docsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 parse_file(infile);
430 }
431 else if (strcmp("depend", argv[1]) == 0)
432 {
433 /* Create first part of dependency chain
434 * file.tmpl */
435 printf("%s\t", argv[2]);
436 defaultline = noaction;
437 internalfunctions = adddep;
438 externalfunctions = adddep;
439 symbolsonly = adddep;
440 singlefunctions = adddep2;
Johannes Berge662af42007-10-24 15:08:48 -0700441 docsection = adddep2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 parse_file(infile);
443 printf("\n");
444 }
445 else
446 {
447 fprintf(stderr, "Unknown option: %s\n", argv[1]);
448 exit(1);
449 }
450 fclose(infile);
451 fflush(stdout);
452 return exitstatus;
453}