exec: do not sleep in TASK_TRACED under ->cred_guard_mutex
[linux-2.6/mini2440.git] / scripts / basic / docproc.c
blob99ca7a698687a5a0558b987b05ffb6c581c01141
1 /*
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.
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:
29 * !Efilename
30 * !Ifilename
31 * !Dfilename
32 * !Ffilename
33 * !Pfilename
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>
46 /* exitstatus is used to keep track of any failing calls to kernel-doc,
47 * but execution continues. */
48 int exitstatus = 0;
50 typedef void DFL(char *);
51 DFL *defaultline;
53 typedef void FILEONLY(char * file);
54 FILEONLY *internalfunctions;
55 FILEONLY *externalfunctions;
56 FILEONLY *symbolsonly;
58 typedef void FILELINE(char * file, char * line);
59 FILELINE * singlefunctions;
60 FILELINE * entity_system;
61 FILELINE * docsection;
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"
70 #define NODOCSECTIONS "-no-doc-sections"
72 static char *srctree, *kernsrctree;
74 void usage (void)
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");
80 fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n");
81 fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n");
85 * Execute kernel-doc with parameters given in svec
87 void exec_kernel_doc(char **svec)
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);
94 switch (pid=fork()) {
95 case -1:
96 perror("fork");
97 exit(1);
98 case 0:
99 memset(real_filename, 0, sizeof(real_filename));
100 strncat(real_filename, kernsrctree, PATH_MAX);
101 strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
102 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);
110 if (WIFEXITED(ret))
111 exitstatus |= WEXITSTATUS(ret);
112 else
113 exitstatus = 0xff;
116 /* Types used to create list of all exported symbols in a number of files */
117 struct symbols
119 char *name;
122 struct symfile
124 char *filename;
125 struct symbols *symbollist;
126 int symbolcnt;
129 struct symfile symfilelist[MAXFILES];
130 int symfilecnt = 0;
132 void add_new_symbol(struct symfile *sym, char * symname)
134 sym->symbollist =
135 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
136 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
139 /* Add a filename to the list */
140 struct symfile * add_new_file(char * filename)
142 symfilelist[symfilecnt++].filename = strdup(filename);
143 return &symfilelist[symfilecnt - 1];
146 /* Check if file already are present in the list */
147 struct symfile * filename_exist(char * filename)
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;
157 * List all files referenced within the template file.
158 * Files are separated by tabs.
160 void adddep(char * file) { printf("\t%s", file); }
161 void adddep2(char * file, char * line) { line = line; adddep(file); }
162 void noaction(char * line) { line = line; }
163 void noaction2(char * file, char * line) { file = file; line = line; }
165 /* Echo the line without further action */
166 void printline(char * line) { printf("%s", line); }
169 * Find all symbols in filename that are exported with EXPORT_SYMBOL &
170 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
171 * All symbols located are stored in symfilelist.
173 void find_export_symbols(char * filename)
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));
181 strncat(real_filename, srctree, PATH_MAX);
182 strncat(real_filename, "/", PATH_MAX - strlen(real_filename));
183 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)
189 fprintf(stderr, "docproc: ");
190 perror(real_filename);
191 exit(1);
193 while (fgets(line, MAXLINESZ, fp)) {
194 char *p;
195 char *e;
196 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
197 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
198 /* Skip EXPORT_SYMBOL{_GPL} */
199 while (isalnum(*p) || *p == '_')
200 p++;
201 /* Remove parentheses & additional whitespace */
202 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);
217 fclose(fp);
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
225 * Function names are obtained from all the src files
226 * by find_export_symbols.
227 * intfunc uses -nofunction
228 * extfunc uses -function
230 void docfunctions(char * filename, char * type)
232 int i,j;
233 int symcnt = 0;
234 int idx = 0;
235 char **vec;
237 for (i=0; i <= symfilecnt; i++)
238 symcnt += symfilelist[i].symbolcnt;
239 vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
240 if (vec == NULL) {
241 perror("docproc: ");
242 exit(1);
244 vec[idx++] = KERNELDOC;
245 vec[idx++] = DOCBOOK;
246 vec[idx++] = NODOCSECTIONS;
247 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;
254 vec[idx++] = filename;
255 vec[idx] = NULL;
256 printf("<!-- %s -->\n", filename);
257 exec_kernel_doc(vec);
258 fflush(stdout);
259 free(vec);
261 void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
262 void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
265 * Document specific function(s) in a file.
266 * Call kernel-doc with the following parameters:
267 * kernel-doc -docbook -function function1 [-function function2]
269 void singfunc(char * filename, char * line)
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;
277 /* Split line up in individual parameters preceded by FUNCTION */
278 for (i=0; line[i]; i++) {
279 if (isspace(line[i])) {
280 line[i] = '\0';
281 startofsym = 1;
282 continue;
284 if (startofsym) {
285 startofsym = 0;
286 vec[idx++] = FUNCTION;
287 vec[idx++] = &line[i];
290 vec[idx++] = filename;
291 vec[idx] = NULL;
292 exec_kernel_doc(vec);
296 * Insert specific documentation section from a file.
297 * Call kernel-doc with the following parameters:
298 * kernel-doc -docbook -function "doc section" filename
300 void docsect(char *filename, char *line)
302 char *vec[6]; /* kerneldoc -docbook -function "section" file NULL */
303 char *s;
305 for (s = line; *s; s++)
306 if (*s == '\n')
307 *s = '\0';
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);
319 * 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
324 * 5) Lines containing !P
325 * 6) Default lines - lines not matching the above
327 void parse_file(FILE *infile)
329 char line[MAXLINESZ];
330 char * s;
331 while (fgets(line, MAXLINESZ, infile)) {
332 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;
359 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;
368 default:
369 defaultline(line);
372 else {
373 defaultline(line);
376 fflush(stdout);
380 int main(int argc, char *argv[])
382 FILE * infile;
384 srctree = getenv("SRCTREE");
385 if (!srctree)
386 srctree = getcwd(NULL, 0);
387 kernsrctree = getenv("KBUILD_SRC");
388 if (!kernsrctree || !*kernsrctree)
389 kernsrctree = srctree;
390 if (argc != 3) {
391 usage();
392 exit(1);
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);
402 if (strcmp("doc", argv[1]) == 0)
404 /* Need to do this in two passes.
405 * First pass is used to collect all symbols exported
406 * in the various files;
407 * Second pass generate the documentation.
408 * This is required because some functions are declared
409 * and exported in different files :-((
411 /* Collect symbols */
412 defaultline = noaction;
413 internalfunctions = find_export_symbols;
414 externalfunctions = find_export_symbols;
415 symbolsonly = find_export_symbols;
416 singlefunctions = noaction2;
417 docsection = noaction2;
418 parse_file(infile);
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;
427 docsection = docsect;
429 parse_file(infile);
431 else if (strcmp("depend", argv[1]) == 0)
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;
441 docsection = adddep2;
442 parse_file(infile);
443 printf("\n");
445 else
447 fprintf(stderr, "Unknown option: %s\n", argv[1]);
448 exit(1);
450 fclose(infile);
451 fflush(stdout);
452 return exitstatus;