Blame view
scripts/docproc.c
14.1 KB
1da177e4c
|
1 2 3 4 5 6 7 8 9 10 11 12 |
/* * docproc is a simple preprocessor for the template files * used as placeholders for the kernel internal documentation. * docproc is used for documentation-frontend and * dependency-generator. * The two usages have in common that they require * some knowledge of the .tmpl syntax, therefore they * are kept together. * * documentation-frontend * Scans the template file and call kernel-doc for * all occurrences of ![EIF]file |
6dd16f44a
|
13 14 15 16 |
* Beforehand each referenced file is scanned for * any symbols that are exported via these macros: * EXPORT_SYMBOL(), EXPORT_SYMBOL_GPL(), & * EXPORT_SYMBOL_GPL_FUTURE() |
1da177e4c
|
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
* This is used to create proper -function and * -nofunction arguments in calls to kernel-doc. * Usage: docproc doc file.tmpl * * dependency-generator: * Scans the template file and list all files * referenced in a format recognized by make. * Usage: docproc depend file.tmpl * Writes dependency information to stdout * in the following format: * file.tmpl src.c src2.c * The filenames are obtained from the following constructs: * !Efilename * !Ifilename * !Dfilename * !Ffilename |
e662af428
|
33 |
* !Pfilename |
1da177e4c
|
34 35 |
* */ |
eda603f6c
|
36 |
#define _GNU_SOURCE |
1da177e4c
|
37 38 39 40 41 42 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <limits.h> |
eda603f6c
|
43 |
#include <errno.h> |
1da177e4c
|
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <sys/types.h> #include <sys/wait.h> /* exitstatus is used to keep track of any failing calls to kernel-doc, * but execution continues. */ int exitstatus = 0; typedef void DFL(char *); DFL *defaultline; typedef void FILEONLY(char * file); FILEONLY *internalfunctions; FILEONLY *externalfunctions; FILEONLY *symbolsonly; |
eda603f6c
|
58 |
FILEONLY *findall; |
1da177e4c
|
59 |
|
48b9d03c5
|
60 |
typedef void FILELINE(char * file, char * line); |
1da177e4c
|
61 62 |
FILELINE * singlefunctions; FILELINE * entity_system; |
e662af428
|
63 |
FILELINE * docsection; |
1da177e4c
|
64 65 66 67 68 69 |
#define MAXLINESZ 2048 #define MAXFILES 250 #define KERNELDOCPATH "scripts/" #define KERNELDOC "kernel-doc" #define DOCBOOK "-docbook" |
eda603f6c
|
70 |
#define LIST "-list" |
1da177e4c
|
71 72 |
#define FUNCTION "-function" #define NOFUNCTION "-nofunction" |
2e95972c4
|
73 |
#define NODOCSECTIONS "-no-doc-sections" |
1da177e4c
|
74 |
|
2d51005c2
|
75 |
static char *srctree, *kernsrctree; |
bb13be514
|
76 |
|
eda603f6c
|
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
static char **all_list = NULL; static int all_list_len = 0; static void consume_symbol(const char *sym) { int i; for (i = 0; i < all_list_len; i++) { if (!all_list[i]) continue; if (strcmp(sym, all_list[i])) continue; all_list[i] = NULL; break; } } |
4356f4890
|
93 |
static void usage (void) |
1da177e4c
|
94 95 96 97 98 99 100 101 102 |
{ fprintf(stderr, "Usage: docproc {doc|depend} file "); fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout "); fprintf(stderr, "doc: frontend when generating kernel documentation "); fprintf(stderr, "depend: generate list of files referenced within file "); |
2d51005c2
|
103 104 105 106 |
fprintf(stderr, "Environment variable SRCTREE: absolute path to sources. "); fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree. "); |
1da177e4c
|
107 108 109 |
} /* |
6dd16f44a
|
110 |
* Execute kernel-doc with parameters given in svec |
1da177e4c
|
111 |
*/ |
4356f4890
|
112 |
static void exec_kernel_doc(char **svec) |
1da177e4c
|
113 114 115 116 117 118 |
{ pid_t pid; int ret; char real_filename[PATH_MAX + 1]; /* Make sure output generated so far are flushed */ fflush(stdout); |
6dd16f44a
|
119 |
switch (pid=fork()) { |
1da177e4c
|
120 121 122 123 124 |
case -1: perror("fork"); exit(1); case 0: memset(real_filename, 0, sizeof(real_filename)); |
2d51005c2
|
125 126 |
strncat(real_filename, kernsrctree, PATH_MAX); strncat(real_filename, "/" KERNELDOCPATH KERNELDOC, |
1da177e4c
|
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
PATH_MAX - strlen(real_filename)); execvp(real_filename, svec); fprintf(stderr, "exec "); perror(real_filename); exit(1); default: waitpid(pid, &ret ,0); } if (WIFEXITED(ret)) exitstatus |= WEXITSTATUS(ret); else exitstatus = 0xff; } /* Types used to create list of all exported symbols in a number of files */ struct symbols { char *name; }; struct symfile { char *filename; struct symbols *symbollist; int symbolcnt; }; struct symfile symfilelist[MAXFILES]; int symfilecnt = 0; |
4356f4890
|
156 |
static void add_new_symbol(struct symfile *sym, char * symname) |
1da177e4c
|
157 158 159 160 161 162 163 |
{ sym->symbollist = realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *)); sym->symbollist[sym->symbolcnt++].name = strdup(symname); } /* Add a filename to the list */ |
4356f4890
|
164 |
static struct symfile * add_new_file(char * filename) |
1da177e4c
|
165 166 167 168 |
{ symfilelist[symfilecnt++].filename = strdup(filename); return &symfilelist[symfilecnt - 1]; } |
6dd16f44a
|
169 |
|
1da177e4c
|
170 |
/* Check if file already are present in the list */ |
4356f4890
|
171 |
static struct symfile * filename_exist(char * filename) |
1da177e4c
|
172 173 174 175 176 177 178 179 180 181 182 183 |
{ int i; for (i=0; i < symfilecnt; i++) if (strcmp(symfilelist[i].filename, filename) == 0) return &symfilelist[i]; return NULL; } /* * List all files referenced within the template file. * Files are separated by tabs. */ |
4356f4890
|
184 185 186 187 |
static void adddep(char * file) { printf("\t%s", file); } static void adddep2(char * file, char * line) { line = line; adddep(file); } static void noaction(char * line) { line = line; } static void noaction2(char * file, char * line) { file = file; line = line; } |
1da177e4c
|
188 189 |
/* Echo the line without further action */ |
4356f4890
|
190 |
static void printline(char * line) { printf("%s", line); } |
1da177e4c
|
191 192 |
/* |
6dd16f44a
|
193 194 |
* Find all symbols in filename that are exported with EXPORT_SYMBOL & * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly). |
1da177e4c
|
195 196 |
* All symbols located are stored in symfilelist. */ |
4356f4890
|
197 |
static void find_export_symbols(char * filename) |
1da177e4c
|
198 199 200 201 202 203 204 |
{ FILE * fp; struct symfile *sym; char line[MAXLINESZ]; if (filename_exist(filename) == NULL) { char real_filename[PATH_MAX + 1]; memset(real_filename, 0, sizeof(real_filename)); |
bb13be514
|
205 |
strncat(real_filename, srctree, PATH_MAX); |
2d51005c2
|
206 |
strncat(real_filename, "/", PATH_MAX - strlen(real_filename)); |
1da177e4c
|
207 208 209 210 |
strncat(real_filename, filename, PATH_MAX - strlen(real_filename)); sym = add_new_file(filename); fp = fopen(real_filename, "r"); |
f0f3ca8d9
|
211 |
if (fp == NULL) { |
1da177e4c
|
212 213 |
fprintf(stderr, "docproc: "); perror(real_filename); |
074a5dde0
|
214 |
exit(1); |
1da177e4c
|
215 |
} |
6dd16f44a
|
216 |
while (fgets(line, MAXLINESZ, fp)) { |
48b9d03c5
|
217 218 |
char *p; char *e; |
6dd16f44a
|
219 220 |
if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) || ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) { |
1da177e4c
|
221 222 223 |
/* Skip EXPORT_SYMBOL{_GPL} */ while (isalnum(*p) || *p == '_') p++; |
6dd16f44a
|
224 |
/* Remove parentheses & additional whitespace */ |
1da177e4c
|
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
while (isspace(*p)) p++; if (*p != '(') continue; /* Syntax error? */ else p++; while (isspace(*p)) p++; e = p; while (isalnum(*e) || *e == '_') e++; *e = '\0'; add_new_symbol(sym, p); } } fclose(fp); } } /* * Document all external or internal functions in a file. * Call kernel-doc with following parameters: * kernel-doc -docbook -nofunction function_name1 filename |
6dd16f44a
|
248 |
* Function names are obtained from all the src files |
1da177e4c
|
249 250 251 252 |
* by find_export_symbols. * intfunc uses -nofunction * extfunc uses -function */ |
4356f4890
|
253 |
static void docfunctions(char * filename, char * type) |
1da177e4c
|
254 255 256 257 258 259 260 261 |
{ int i,j; int symcnt = 0; int idx = 0; char **vec; for (i=0; i <= symfilecnt; i++) symcnt += symfilelist[i].symbolcnt; |
2e95972c4
|
262 |
vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *)); |
1da177e4c
|
263 264 265 266 267 268 |
if (vec == NULL) { perror("docproc: "); exit(1); } vec[idx++] = KERNELDOC; vec[idx++] = DOCBOOK; |
2e95972c4
|
269 |
vec[idx++] = NODOCSECTIONS; |
1da177e4c
|
270 271 272 273 |
for (i=0; i < symfilecnt; i++) { struct symfile * sym = &symfilelist[i]; for (j=0; j < sym->symbolcnt; j++) { vec[idx++] = type; |
eda603f6c
|
274 |
consume_symbol(sym->symbollist[j].name); |
1da177e4c
|
275 276 277 278 279 280 281 282 283 284 285 |
vec[idx++] = sym->symbollist[j].name; } } vec[idx++] = filename; vec[idx] = NULL; printf("<!-- %s --> ", filename); exec_kernel_doc(vec); fflush(stdout); free(vec); } |
4356f4890
|
286 287 |
static void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); } static void extfunc(char * filename) { docfunctions(filename, FUNCTION); } |
1da177e4c
|
288 289 |
/* |
c61209383
|
290 |
* Document specific function(s) in a file. |
1da177e4c
|
291 292 293 |
* Call kernel-doc with the following parameters: * kernel-doc -docbook -function function1 [-function function2] */ |
4356f4890
|
294 |
static void singfunc(char * filename, char * line) |
1da177e4c
|
295 296 297 298 299 300 |
{ char *vec[200]; /* Enough for specific functions */ int i, idx = 0; int startofsym = 1; vec[idx++] = KERNELDOC; vec[idx++] = DOCBOOK; |
6dd16f44a
|
301 |
/* Split line up in individual parameters preceded by FUNCTION */ |
1da177e4c
|
302 303 304 305 306 307 308 309 310 311 312 313 |
for (i=0; line[i]; i++) { if (isspace(line[i])) { line[i] = '\0'; startofsym = 1; continue; } if (startofsym) { startofsym = 0; vec[idx++] = FUNCTION; vec[idx++] = &line[i]; } } |
eda603f6c
|
314 315 316 317 318 |
for (i = 0; i < idx; i++) { if (strcmp(vec[i], FUNCTION)) continue; consume_symbol(vec[i + 1]); } |
1da177e4c
|
319 320 321 322 323 324 |
vec[idx++] = filename; vec[idx] = NULL; exec_kernel_doc(vec); } /* |
e662af428
|
325 326 327 328 |
* Insert specific documentation section from a file. * Call kernel-doc with the following parameters: * kernel-doc -docbook -function "doc section" filename */ |
4356f4890
|
329 |
static void docsect(char *filename, char *line) |
e662af428
|
330 331 332 333 334 335 336 337 |
{ char *vec[6]; /* kerneldoc -docbook -function "section" file NULL */ char *s; for (s = line; *s; s++) if (*s == ' ') *s = '\0'; |
d0f95c782
|
338 339 340 341 |
if (asprintf(&s, "DOC: %s", line) < 0) { perror("asprintf"); exit(1); } |
eda603f6c
|
342 343 |
consume_symbol(s); free(s); |
e662af428
|
344 345 346 347 348 349 350 351 |
vec[0] = KERNELDOC; vec[1] = DOCBOOK; vec[2] = FUNCTION; vec[3] = line; vec[4] = filename; vec[5] = NULL; exec_kernel_doc(vec); } |
eda603f6c
|
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
static void find_all_symbols(char *filename) { char *vec[4]; /* kerneldoc -list file NULL */ pid_t pid; int ret, i, count, start; char real_filename[PATH_MAX + 1]; int pipefd[2]; char *data, *str; size_t data_len = 0; vec[0] = KERNELDOC; vec[1] = LIST; vec[2] = filename; vec[3] = NULL; if (pipe(pipefd)) { perror("pipe"); exit(1); } switch (pid=fork()) { case -1: perror("fork"); exit(1); case 0: close(pipefd[0]); dup2(pipefd[1], 1); memset(real_filename, 0, sizeof(real_filename)); strncat(real_filename, kernsrctree, PATH_MAX); strncat(real_filename, "/" KERNELDOCPATH KERNELDOC, PATH_MAX - strlen(real_filename)); execvp(real_filename, vec); fprintf(stderr, "exec "); perror(real_filename); exit(1); default: close(pipefd[1]); data = malloc(4096); do { while ((ret = read(pipefd[0], data + data_len, 4096)) > 0) { data_len += ret; data = realloc(data, data_len + 4096); } } while (ret == -EAGAIN); if (ret != 0) { perror("read"); exit(1); } waitpid(pid, &ret ,0); } if (WIFEXITED(ret)) exitstatus |= WEXITSTATUS(ret); else exitstatus = 0xff; count = 0; /* poor man's strtok, but with counting */ for (i = 0; i < data_len; i++) { if (data[i] == ' ') { count++; data[i] = '\0'; } } start = all_list_len; all_list_len += count; all_list = realloc(all_list, sizeof(char *) * all_list_len); str = data; for (i = 0; i < data_len && start != all_list_len; i++) { if (data[i] == '\0') { all_list[start] = str; str = data + i + 1; start++; } } } |
e662af428
|
430 |
/* |
1da177e4c
|
431 432 433 434 435 |
* Parse file, calling action specific functions for: * 1) Lines containing !E * 2) Lines containing !I * 3) Lines containing !D * 4) Lines containing !F |
e662af428
|
436 |
* 5) Lines containing !P |
eda603f6c
|
437 438 |
* 6) Lines containing !C * 7) Default lines - lines not matching the above |
1da177e4c
|
439 |
*/ |
4356f4890
|
440 |
static void parse_file(FILE *infile) |
1da177e4c
|
441 442 |
{ char line[MAXLINESZ]; |
48b9d03c5
|
443 |
char * s; |
6dd16f44a
|
444 |
while (fgets(line, MAXLINESZ, infile)) { |
1da177e4c
|
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
if (line[0] == '!') { s = line + 2; switch (line[1]) { case 'E': while (*s && !isspace(*s)) s++; *s = '\0'; externalfunctions(line+2); break; case 'I': while (*s && !isspace(*s)) s++; *s = '\0'; internalfunctions(line+2); break; case 'D': while (*s && !isspace(*s)) s++; *s = '\0'; symbolsonly(line+2); break; case 'F': /* filename */ while (*s && !isspace(*s)) s++; *s++ = '\0'; /* function names */ while (isspace(*s)) s++; singlefunctions(line +2, s); break; |
e662af428
|
472 473 474 475 476 477 478 479 480 |
case 'P': /* filename */ while (*s && !isspace(*s)) s++; *s++ = '\0'; /* DOC: section name */ while (isspace(*s)) s++; docsection(line + 2, s); break; |
eda603f6c
|
481 482 483 484 485 486 |
case 'C': while (*s && !isspace(*s)) s++; *s = '\0'; if (findall) findall(line+2); break; |
1da177e4c
|
487 488 489 |
default: defaultline(line); } |
f0f3ca8d9
|
490 |
} else { |
1da177e4c
|
491 492 493 494 495 496 497 498 499 500 |
defaultline(line); } } fflush(stdout); } int main(int argc, char *argv[]) { FILE * infile; |
eda603f6c
|
501 |
int i; |
bb13be514
|
502 503 504 505 |
srctree = getenv("SRCTREE"); if (!srctree) srctree = getcwd(NULL, 0); |
2d51005c2
|
506 |
kernsrctree = getenv("KBUILD_SRC"); |
b767b9059
|
507 |
if (!kernsrctree || !*kernsrctree) |
2d51005c2
|
508 |
kernsrctree = srctree; |
1da177e4c
|
509 510 511 512 513 514 515 516 517 518 519 |
if (argc != 3) { usage(); exit(1); } /* Open file, exit on error */ infile = fopen(argv[2], "r"); if (infile == NULL) { fprintf(stderr, "docproc: "); perror(argv[2]); exit(2); } |
f0f3ca8d9
|
520 |
if (strcmp("doc", argv[1]) == 0) { |
1da177e4c
|
521 522 |
/* Need to do this in two passes. * First pass is used to collect all symbols exported |
6dd16f44a
|
523 |
* in the various files; |
1da177e4c
|
524 |
* Second pass generate the documentation. |
6dd16f44a
|
525 |
* This is required because some functions are declared |
1da177e4c
|
526 527 528 529 530 531 532 533 |
* and exported in different files :-(( */ /* Collect symbols */ defaultline = noaction; internalfunctions = find_export_symbols; externalfunctions = find_export_symbols; symbolsonly = find_export_symbols; singlefunctions = noaction2; |
e662af428
|
534 |
docsection = noaction2; |
eda603f6c
|
535 |
findall = find_all_symbols; |
1da177e4c
|
536 537 538 539 540 541 542 543 544 |
parse_file(infile); /* Rewind to start from beginning of file again */ fseek(infile, 0, SEEK_SET); defaultline = printline; internalfunctions = intfunc; externalfunctions = extfunc; symbolsonly = printline; singlefunctions = singfunc; |
e662af428
|
545 |
docsection = docsect; |
eda603f6c
|
546 |
findall = NULL; |
1da177e4c
|
547 548 |
parse_file(infile); |
eda603f6c
|
549 550 551 552 553 554 555 556 |
for (i = 0; i < all_list_len; i++) { if (!all_list[i]) continue; fprintf(stderr, "Warning: didn't use docs for %s ", all_list[i]); } |
f0f3ca8d9
|
557 |
} else if (strcmp("depend", argv[1]) == 0) { |
1da177e4c
|
558 559 560 561 562 563 564 565 |
/* Create first part of dependency chain * file.tmpl */ printf("%s\t", argv[2]); defaultline = noaction; internalfunctions = adddep; externalfunctions = adddep; symbolsonly = adddep; singlefunctions = adddep2; |
e662af428
|
566 |
docsection = adddep2; |
eda603f6c
|
567 |
findall = adddep; |
1da177e4c
|
568 569 570 |
parse_file(infile); printf(" "); |
f0f3ca8d9
|
571 |
} else { |
1da177e4c
|
572 573 574 575 576 577 578 579 |
fprintf(stderr, "Unknown option: %s ", argv[1]); exit(1); } fclose(infile); fflush(stdout); return exitstatus; } |