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 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:
42 #include <sys/types.h>
45 /* exitstatus is used to keep track of any failing calls to kernel-doc,
46 * but execution continues. */
49 typedef void DFL(char *);
52 typedef void FILEONLY(char * file);
53 FILEONLY *internalfunctions;
54 FILEONLY *externalfunctions;
55 FILEONLY *symbolsonly;
57 typedef void FILELINE(char * file, char * line);
58 FILELINE * singlefunctions;
59 FILELINE * entity_system;
61 #define MAXLINESZ 2048
63 #define KERNELDOCPATH "scripts/"
64 #define KERNELDOC "kernel-doc"
65 #define DOCBOOK "-docbook"
66 #define FUNCTION "-function"
67 #define NOFUNCTION "-nofunction"
71 fprintf(stderr, "Usage: docproc {doc|depend} file\n");
72 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
73 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
74 fprintf(stderr, "depend: generate list of files referenced within file\n");
78 * Execute kernel-doc with parameters given in svec
80 void exec_kernel_doc(char **svec)
84 char real_filename[PATH_MAX + 1];
85 /* Make sure output generated so far are flushed */
92 memset(real_filename, 0, sizeof(real_filename));
93 strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
94 strncat(real_filename, KERNELDOCPATH KERNELDOC,
95 PATH_MAX - strlen(real_filename));
96 execvp(real_filename, svec);
97 fprintf(stderr, "exec ");
98 perror(real_filename);
101 waitpid(pid, &ret ,0);
104 exitstatus |= WEXITSTATUS(ret);
109 /* Types used to create list of all exported symbols in a number of files */
118 struct symbols *symbollist;
122 struct symfile symfilelist[MAXFILES];
125 void add_new_symbol(struct symfile *sym, char * symname)
128 realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
129 sym->symbollist[sym->symbolcnt++].name = strdup(symname);
132 /* Add a filename to the list */
133 struct symfile * add_new_file(char * filename)
135 symfilelist[symfilecnt++].filename = strdup(filename);
136 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 in filename that are exported with EXPORT_SYMBOL &
163 * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
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 char real_filename[PATH_MAX + 1];
173 memset(real_filename, 0, sizeof(real_filename));
174 strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
175 strncat(real_filename, filename,
176 PATH_MAX - strlen(real_filename));
177 sym = add_new_file(filename);
178 fp = fopen(real_filename, "r");
181 fprintf(stderr, "docproc: ");
182 perror(real_filename);
185 while (fgets(line, MAXLINESZ, fp)) {
188 if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
189 ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
190 /* Skip EXPORT_SYMBOL{_GPL} */
191 while (isalnum(*p) || *p == '_')
193 /* Remove parentheses & additional whitespace */
197 continue; /* Syntax error? */
203 while (isalnum(*e) || *e == '_')
206 add_new_symbol(sym, p);
214 * Document all external or internal functions in a file.
215 * Call kernel-doc with following parameters:
216 * kernel-doc -docbook -nofunction function_name1 filename
217 * Function names are obtained from all the src files
218 * by find_export_symbols.
219 * intfunc uses -nofunction
220 * extfunc uses -function
222 void docfunctions(char * filename, char * type)
229 for (i=0; i <= symfilecnt; i++)
230 symcnt += symfilelist[i].symbolcnt;
231 vec = malloc((2 + 2 * symcnt + 2) * sizeof(char*));
236 vec[idx++] = KERNELDOC;
237 vec[idx++] = DOCBOOK;
238 for (i=0; i < symfilecnt; i++) {
239 struct symfile * sym = &symfilelist[i];
240 for (j=0; j < sym->symbolcnt; j++) {
242 vec[idx++] = sym->symbollist[j].name;
245 vec[idx++] = filename;
247 printf("<!-- %s -->\n", filename);
248 exec_kernel_doc(vec);
252 void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
253 void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
256 * Document specific function(s) in a file.
257 * Call kernel-doc with the following parameters:
258 * kernel-doc -docbook -function function1 [-function function2]
260 void singfunc(char * filename, char * line)
262 char *vec[200]; /* Enough for specific functions */
265 vec[idx++] = KERNELDOC;
266 vec[idx++] = DOCBOOK;
268 /* Split line up in individual parameters preceded by FUNCTION */
269 for (i=0; line[i]; i++) {
270 if (isspace(line[i])) {
277 vec[idx++] = FUNCTION;
278 vec[idx++] = &line[i];
281 vec[idx++] = filename;
283 exec_kernel_doc(vec);
287 * Parse file, calling action specific functions for:
288 * 1) Lines containing !E
289 * 2) Lines containing !I
290 * 3) Lines containing !D
291 * 4) Lines containing !F
292 * 5) Default lines - lines not matching the above
294 void parse_file(FILE *infile)
296 char line[MAXLINESZ];
298 while (fgets(line, MAXLINESZ, infile)) {
299 if (line[0] == '!') {
303 while (*s && !isspace(*s)) s++;
305 externalfunctions(line+2);
308 while (*s && !isspace(*s)) s++;
310 internalfunctions(line+2);
313 while (*s && !isspace(*s)) s++;
319 while (*s && !isspace(*s)) s++;
324 singlefunctions(line +2, s);
338 int main(int argc, char *argv[])
345 /* Open file, exit on error */
346 infile = fopen(argv[2], "r");
347 if (infile == NULL) {
348 fprintf(stderr, "docproc: ");
353 if (strcmp("doc", argv[1]) == 0)
355 /* Need to do this in two passes.
356 * First pass is used to collect all symbols exported
357 * in the various files;
358 * Second pass generate the documentation.
359 * This is required because some functions are declared
360 * and exported in different files :-((
362 /* Collect symbols */
363 defaultline = noaction;
364 internalfunctions = find_export_symbols;
365 externalfunctions = find_export_symbols;
366 symbolsonly = find_export_symbols;
367 singlefunctions = noaction2;
370 /* Rewind to start from beginning of file again */
371 fseek(infile, 0, SEEK_SET);
372 defaultline = printline;
373 internalfunctions = intfunc;
374 externalfunctions = extfunc;
375 symbolsonly = printline;
376 singlefunctions = singfunc;
380 else if (strcmp("depend", argv[1]) == 0)
382 /* Create first part of dependency chain
384 printf("%s\t", argv[2]);
385 defaultline = noaction;
386 internalfunctions = adddep;
387 externalfunctions = adddep;
388 symbolsonly = adddep;
389 singlefunctions = adddep2;
395 fprintf(stderr, "Unknown option: %s\n", argv[1]);