Cleanup random differences to mips64 equivalent.
[linux-2.6/linux-mips.git] / scripts / docproc.c
blob18e1d45a7ff9fd95ea2218bd27329966e18333a3
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 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:
27 * !Efilename
28 * !Ifilename
29 * !Dfilename
30 * !Ffilename
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <limits.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
43 /* exitstatus is used to keep track of any failing calls to kernel-doc,
44 * but execution continues. */
45 int exitstatus = 0;
47 typedef void DFL(char *);
48 DFL *defaultline;
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
60 #define MAXFILES 250
61 #define KERNELDOCPATH "scripts/"
62 #define KERNELDOC "kernel-doc"
63 #define DOCBOOK "-docbook"
64 #define FUNCTION "-function"
65 #define NOFUNCTION "-nofunction"
67 void usage (void)
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)
80 pid_t pid;
81 int ret;
82 /* Make sure output generated so far are flushed */
83 fflush(stdout);
84 switch(pid=fork()) {
85 case -1:
86 perror("fork");
87 exit(1);
88 case 0:
89 execvp(KERNELDOCPATH KERNELDOC, svec);
90 perror("exec " KERNELDOCPATH KERNELDOC);
91 exit(1);
92 default:
93 waitpid(pid, &ret ,0);
95 if (WIFEXITED(ret))
96 exitstatus |= WEXITSTATUS(ret);
97 else
98 exitstatus = 0xff;
101 /* Types used to create list of all exported symbols in a number of files */
102 struct symbols
104 char *name;
107 struct symfile
109 char *filename;
110 struct symbols *symbollist;
111 int symbolcnt;
114 struct symfile symfilelist[MAXFILES];
115 int symfilecnt = 0;
117 void add_new_symbol(struct symfile *sym, char * symname)
119 sym->symbollist =
120 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
121 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
124 /* Add a filename to the list */
125 struct symfile * add_new_file(char * filename)
127 symfilelist[symfilecnt++].filename = strdup(filename);
128 return &symfilelist[symfilecnt - 1];
130 /* Check if file already are present in the list */
131 struct symfile * filename_exist(char * filename)
133 int i;
134 for (i=0; i < symfilecnt; i++)
135 if (strcmp(symfilelist[i].filename, filename) == 0)
136 return &symfilelist[i];
137 return NULL;
141 * List all files referenced within the template file.
142 * Files are separated by tabs.
144 void adddep(char * file) { printf("\t%s", file); }
145 void adddep2(char * file, char * line) { line = line; adddep(file); }
146 void noaction(char * line) { line = line; }
147 void noaction2(char * file, char * line) { file = file; line = line; }
149 /* Echo the line without further action */
150 void printline(char * line) { printf("%s", line); }
153 * Find all symbols exported with EXPORT_SYMBOL and EXPORT_SYMBOL_GPL
154 * in filename.
155 * All symbols located are stored in symfilelist.
157 void find_export_symbols(char * filename)
159 FILE * fp;
160 struct symfile *sym;
161 char line[MAXLINESZ];
162 if (filename_exist(filename) == NULL) {
163 sym = add_new_file(filename);
164 fp = fopen(filename, "r");
165 if (fp == NULL)
167 fprintf(stderr, "docproc: ");
168 perror(filename);
170 while(fgets(line, MAXLINESZ, fp)) {
171 char *p;
172 char *e;
173 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != 0) ||
174 ((p = strstr(line, "EXPORT_SYMBOL")) != 0)) {
175 /* Skip EXPORT_SYMBOL{_GPL} */
176 while (isalnum(*p) || *p == '_')
177 p++;
178 /* Remove paranteses and additional ws */
179 while (isspace(*p))
180 p++;
181 if (*p != '(')
182 continue; /* Syntax error? */
183 else
184 p++;
185 while (isspace(*p))
186 p++;
187 e = p;
188 while (isalnum(*e) || *e == '_')
189 e++;
190 *e = '\0';
191 add_new_symbol(sym, p);
194 fclose(fp);
199 * Document all external or internal functions in a file.
200 * Call kernel-doc with following parameters:
201 * kernel-doc -docbook -nofunction function_name1 filename
202 * function names are obtained from all the the src files
203 * by find_export_symbols.
204 * intfunc uses -nofunction
205 * extfunc uses -function
207 void docfunctions(char * filename, char * type)
209 int i,j;
210 int symcnt = 0;
211 int idx = 0;
212 char **vec;
214 for (i=0; i <= symfilecnt; i++)
215 symcnt += symfilelist[i].symbolcnt;
216 vec = malloc((2 + 2 * symcnt + 2) * sizeof(char*));
217 if (vec == NULL) {
218 perror("docproc: ");
219 exit(1);
221 vec[idx++] = KERNELDOC;
222 vec[idx++] = DOCBOOK;
223 for (i=0; i < symfilecnt; i++) {
224 struct symfile * sym = &symfilelist[i];
225 for (j=0; j < sym->symbolcnt; j++) {
226 vec[idx++] = type;
227 vec[idx++] = sym->symbollist[j].name;
230 vec[idx++] = filename;
231 vec[idx] = NULL;
232 printf("<!-- %s -->\n", filename);
233 exec_kernel_doc(vec);
234 fflush(stdout);
235 free(vec);
237 void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
238 void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
241 * Document spÄecific function(s) in a file.
242 * Call kernel-doc with the following parameters:
243 * kernel-doc -docbook -function function1 [-function function2]
245 void singfunc(char * filename, char * line)
247 char *vec[200]; /* Enough for specific functions */
248 int i, idx = 0;
249 int startofsym = 1;
250 vec[idx++] = KERNELDOC;
251 vec[idx++] = DOCBOOK;
253 /* Split line up in individual parameters preceeded by FUNCTION */
254 for (i=0; line[i]; i++) {
255 if (isspace(line[i])) {
256 line[i] = '\0';
257 startofsym = 1;
258 continue;
260 if (startofsym) {
261 startofsym = 0;
262 vec[idx++] = FUNCTION;
263 vec[idx++] = &line[i];
266 vec[idx++] = filename;
267 vec[idx] = NULL;
268 exec_kernel_doc(vec);
272 * Parse file, calling action specific functions for:
273 * 1) Lines containing !E
274 * 2) Lines containing !I
275 * 3) Lines containing !D
276 * 4) Lines containing !F
277 * 5) Default lines - lines not matching the above
279 void parse_file(FILE *infile)
281 char line[MAXLINESZ];
282 char * s;
283 while(fgets(line, MAXLINESZ, infile)) {
284 if (line[0] == '!') {
285 s = line + 2;
286 switch (line[1]) {
287 case 'E':
288 while (*s && !isspace(*s)) s++;
289 *s = '\0';
290 externalfunctions(line+2);
291 break;
292 case 'I':
293 while (*s && !isspace(*s)) s++;
294 *s = '\0';
295 internalfunctions(line+2);
296 break;
297 case 'D':
298 while (*s && !isspace(*s)) s++;
299 *s = '\0';
300 symbolsonly(line+2);
301 break;
302 case 'F':
303 /* filename */
304 while (*s && !isspace(*s)) s++;
305 *s++ = '\0';
306 /* function names */
307 while (isspace(*s))
308 s++;
309 singlefunctions(line +2, s);
310 break;
311 default:
312 defaultline(line);
315 else {
316 defaultline(line);
319 fflush(stdout);
323 int main(int argc, char *argv[])
325 FILE * infile;
326 if (argc != 3) {
327 usage();
328 exit(1);
330 /* Open file, exit on error */
331 infile = fopen(argv[2], "r");
332 if (infile == NULL) {
333 fprintf(stderr, "docproc: ");
334 perror(argv[2]);
335 exit(2);
338 if (strcmp("doc", argv[1]) == 0)
340 /* Need to do this in two passes.
341 * First pass is used to collect all symbols exported
342 * in the various files.
343 * Second pass generate the documentation.
344 * This is required because function are declared
345 * and exported in different files :-((
347 /* Collect symbols */
348 defaultline = noaction;
349 internalfunctions = find_export_symbols;
350 externalfunctions = find_export_symbols;
351 symbolsonly = find_export_symbols;
352 singlefunctions = noaction2;
353 parse_file(infile);
355 /* Rewind to start from beginning of file again */
356 fseek(infile, 0, SEEK_SET);
357 defaultline = printline;
358 internalfunctions = intfunc;
359 externalfunctions = extfunc;
360 symbolsonly = printline;
361 singlefunctions = singfunc;
363 parse_file(infile);
365 else if (strcmp("depend", argv[1]) == 0)
367 /* Create first part of dependency chain
368 * file.tmpl */
369 printf("%s\t", argv[2]);
370 defaultline = noaction;
371 internalfunctions = adddep;
372 externalfunctions = adddep;
373 symbolsonly = adddep;
374 singlefunctions = adddep2;
375 parse_file(infile);
376 printf("\n");
378 else
380 fprintf(stderr, "Unknown option: %s\n", argv[1]);
381 exit(1);
383 fclose(infile);
384 fflush(stdout);
385 return exitstatus;