docproc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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.
  9. *
  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
  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
  33. * !Pfilename
  34. *
  35. */
  36. #define _GNU_SOURCE
  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 <errno.h>
  44. #include <sys/types.h>
  45. #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;
  49. typedef void DFL(char *);
  50. DFL *defaultline;
  51. typedef void FILEONLY(char * file);
  52. FILEONLY *internalfunctions;
  53. FILEONLY *externalfunctions;
  54. FILEONLY *symbolsonly;
  55. FILEONLY *findall;
  56. typedef void FILELINE(char * file, char * line);
  57. FILELINE * singlefunctions;
  58. FILELINE * entity_system;
  59. FILELINE * docsection;
  60. #define MAXLINESZ 2048
  61. #define MAXFILES 250
  62. #define KERNELDOCPATH "scripts/"
  63. #define KERNELDOC "kernel-doc"
  64. #define DOCBOOK "-docbook"
  65. #define LIST "-list"
  66. #define FUNCTION "-function"
  67. #define NOFUNCTION "-nofunction"
  68. #define NODOCSECTIONS "-no-doc-sections"
  69. static char *srctree, *kernsrctree;
  70. static char **all_list = NULL;
  71. static int all_list_len = 0;
  72. static void consume_symbol(const char *sym)
  73. {
  74. int i;
  75. for (i = 0; i < all_list_len; i++) {
  76. if (!all_list[i])
  77. continue;
  78. if (strcmp(sym, all_list[i]))
  79. continue;
  80. all_list[i] = NULL;
  81. break;
  82. }
  83. }
  84. static void usage (void)
  85. {
  86. fprintf(stderr, "Usage: docproc {doc|depend} file\n");
  87. fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
  88. fprintf(stderr, "doc: frontend when generating kernel documentation\n");
  89. fprintf(stderr, "depend: generate list of files referenced within file\n");
  90. fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n");
  91. fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n");
  92. }
  93. /*
  94. * Execute kernel-doc with parameters given in svec
  95. */
  96. static void exec_kernel_doc(char **svec)
  97. {
  98. pid_t pid;
  99. int ret;
  100. char real_filename[PATH_MAX + 1];
  101. /* Make sure output generated so far are flushed */
  102. fflush(stdout);
  103. switch (pid=fork()) {
  104. case -1:
  105. perror("fork");
  106. exit(1);
  107. case 0:
  108. memset(real_filename, 0, sizeof(real_filename));
  109. strncat(real_filename, kernsrctree, PATH_MAX);
  110. strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
  111. PATH_MAX - strlen(real_filename));
  112. execvp(real_filename, svec);
  113. fprintf(stderr, "exec ");
  114. perror(real_filename);
  115. exit(1);
  116. default:
  117. waitpid(pid, &ret ,0);
  118. }
  119. if (WIFEXITED(ret))
  120. exitstatus |= WEXITSTATUS(ret);
  121. else
  122. exitstatus = 0xff;
  123. }
  124. /* Types used to create list of all exported symbols in a number of files */
  125. struct symbols
  126. {
  127. char *name;
  128. };
  129. struct symfile
  130. {
  131. char *filename;
  132. struct symbols *symbollist;
  133. int symbolcnt;
  134. };
  135. struct symfile symfilelist[MAXFILES];
  136. int symfilecnt = 0;
  137. static void add_new_symbol(struct symfile *sym, char * symname)
  138. {
  139. sym->symbollist =
  140. realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
  141. sym->symbollist[sym->symbolcnt++].name = strdup(symname);
  142. }
  143. /* Add a filename to the list */
  144. static struct symfile * add_new_file(char * filename)
  145. {
  146. symfilelist[symfilecnt++].filename = strdup(filename);
  147. return &symfilelist[symfilecnt - 1];
  148. }
  149. /* Check if file already are present in the list */
  150. static struct symfile * filename_exist(char * filename)
  151. {
  152. int i;
  153. for (i=0; i < symfilecnt; i++)
  154. if (strcmp(symfilelist[i].filename, filename) == 0)
  155. return &symfilelist[i];
  156. return NULL;
  157. }
  158. /*
  159. * List all files referenced within the template file.
  160. * Files are separated by tabs.
  161. */
  162. static void adddep(char * file) { printf("\t%s", file); }
  163. static void adddep2(char * file, char * line) { line = line; adddep(file); }
  164. static void noaction(char * line) { line = line; }
  165. static void noaction2(char * file, char * line) { file = file; line = line; }
  166. /* Echo the line without further action */
  167. static void printline(char * line) { printf("%s", line); }
  168. /*
  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.
  172. */
  173. static void 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));
  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)
  188. {
  189. fprintf(stderr, "docproc: ");
  190. perror(real_filename);
  191. exit(1);
  192. }
  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);
  215. }
  216. }
  217. fclose(fp);
  218. }
  219. }
  220. /*
  221. * Document all external or internal functions in a file.
  222. * Call kernel-doc with following parameters:
  223. * kernel-doc -docbook -nofunction function_name1 filename
  224. * Function names are obtained from all the src files
  225. * by find_export_symbols.
  226. * intfunc uses -nofunction
  227. * extfunc uses -function
  228. */
  229. static void docfunctions(char * filename, char * type)
  230. {
  231. int i,j;
  232. int symcnt = 0;
  233. int idx = 0;
  234. char **vec;
  235. for (i=0; i <= symfilecnt; i++)
  236. symcnt += symfilelist[i].symbolcnt;
  237. vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
  238. if (vec == NULL) {
  239. perror("docproc: ");
  240. exit(1);
  241. }
  242. vec[idx++] = KERNELDOC;
  243. vec[idx++] = DOCBOOK;
  244. vec[idx++] = NODOCSECTIONS;
  245. for (i=0; i < symfilecnt; i++) {
  246. struct symfile * sym = &symfilelist[i];
  247. for (j=0; j < sym->symbolcnt; j++) {
  248. vec[idx++] = type;
  249. consume_symbol(sym->symbollist[j].name);
  250. vec[idx++] = sym->symbollist[j].name;
  251. }
  252. }
  253. vec[idx++] = filename;
  254. vec[idx] = NULL;
  255. printf("<!-- %s -->\n", filename);
  256. exec_kernel_doc(vec);
  257. fflush(stdout);
  258. free(vec);
  259. }
  260. static void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
  261. static void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
  262. /*
  263. * Document specific function(s) in a file.
  264. * Call kernel-doc with the following parameters:
  265. * kernel-doc -docbook -function function1 [-function function2]
  266. */
  267. static void singfunc(char * filename, char * line)
  268. {
  269. char *vec[200]; /* Enough for specific functions */
  270. int i, idx = 0;
  271. int startofsym = 1;
  272. vec[idx++] = KERNELDOC;
  273. vec[idx++] = DOCBOOK;
  274. /* Split line up in individual parameters preceded by FUNCTION */
  275. for (i=0; line[i]; i++) {
  276. if (isspace(line[i])) {
  277. line[i] = '\0';
  278. startofsym = 1;
  279. continue;
  280. }
  281. if (startofsym) {
  282. startofsym = 0;
  283. vec[idx++] = FUNCTION;
  284. vec[idx++] = &line[i];
  285. }
  286. }
  287. for (i = 0; i < idx; i++) {
  288. if (strcmp(vec[i], FUNCTION))
  289. continue;
  290. consume_symbol(vec[i + 1]);
  291. }
  292. vec[idx++] = filename;
  293. vec[idx] = NULL;
  294. exec_kernel_doc(vec);
  295. }
  296. /*
  297. * Insert specific documentation section from a file.
  298. * Call kernel-doc with the following parameters:
  299. * kernel-doc -docbook -function "doc section" filename
  300. */
  301. static void docsect(char *filename, char *line)
  302. {
  303. char *vec[6]; /* kerneldoc -docbook -function "section" file NULL */
  304. char *s;
  305. for (s = line; *s; s++)
  306. if (*s == '\n')
  307. *s = '\0';
  308. asprintf(&s, "DOC: %s", line);
  309. consume_symbol(s);
  310. free(s);
  311. vec[0] = KERNELDOC;
  312. vec[1] = DOCBOOK;
  313. vec[2] = FUNCTION;
  314. vec[3] = line;
  315. vec[4] = filename;
  316. vec[5] = NULL;
  317. exec_kernel_doc(vec);
  318. }
  319. static void find_all_symbols(char *filename)
  320. {
  321. char *vec[4]; /* kerneldoc -list file NULL */
  322. pid_t pid;
  323. int ret, i, count, start;
  324. char real_filename[PATH_MAX + 1];
  325. int pipefd[2];
  326. char *data, *str;
  327. size_t data_len = 0;
  328. vec[0] = KERNELDOC;
  329. vec[1] = LIST;
  330. vec[2] = filename;
  331. vec[3] = NULL;
  332. if (pipe(pipefd)) {
  333. perror("pipe");
  334. exit(1);
  335. }
  336. switch (pid=fork()) {
  337. case -1:
  338. perror("fork");
  339. exit(1);
  340. case 0:
  341. close(pipefd[0]);
  342. dup2(pipefd[1], 1);
  343. memset(real_filename, 0, sizeof(real_filename));
  344. strncat(real_filename, kernsrctree, PATH_MAX);
  345. strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
  346. PATH_MAX - strlen(real_filename));
  347. execvp(real_filename, vec);
  348. fprintf(stderr, "exec ");
  349. perror(real_filename);
  350. exit(1);
  351. default:
  352. close(pipefd[1]);
  353. data = malloc(4096);
  354. do {
  355. while ((ret = read(pipefd[0],
  356. data + data_len,
  357. 4096)) > 0) {
  358. data_len += ret;
  359. data = realloc(data, data_len + 4096);
  360. }
  361. } while (ret == -EAGAIN);
  362. if (ret != 0) {
  363. perror("read");
  364. exit(1);
  365. }
  366. waitpid(pid, &ret ,0);
  367. }
  368. if (WIFEXITED(ret))
  369. exitstatus |= WEXITSTATUS(ret);
  370. else
  371. exitstatus = 0xff;
  372. count = 0;
  373. /* poor man's strtok, but with counting */
  374. for (i = 0; i < data_len; i++) {
  375. if (data[i] == '\n') {
  376. count++;
  377. data[i] = '\0';
  378. }
  379. }
  380. start = all_list_len;
  381. all_list_len += count;
  382. all_list = realloc(all_list, sizeof(char *) * all_list_len);
  383. str = data;
  384. for (i = 0; i < data_len && start != all_list_len; i++) {
  385. if (data[i] == '\0') {
  386. all_list[start] = str;
  387. str = data + i + 1;
  388. start++;
  389. }
  390. }
  391. }
  392. /*
  393. * Parse file, calling action specific functions for:
  394. * 1) Lines containing !E
  395. * 2) Lines containing !I
  396. * 3) Lines containing !D
  397. * 4) Lines containing !F
  398. * 5) Lines containing !P
  399. * 6) Lines containing !C
  400. * 7) Default lines - lines not matching the above
  401. */
  402. static void parse_file(FILE *infile)
  403. {
  404. char line[MAXLINESZ];
  405. char * s;
  406. while (fgets(line, MAXLINESZ, infile)) {
  407. if (line[0] == '!') {
  408. s = line + 2;
  409. switch (line[1]) {
  410. case 'E':
  411. while (*s && !isspace(*s)) s++;
  412. *s = '\0';
  413. externalfunctions(line+2);
  414. break;
  415. case 'I':
  416. while (*s && !isspace(*s)) s++;
  417. *s = '\0';
  418. internalfunctions(line+2);
  419. break;
  420. case 'D':
  421. while (*s && !isspace(*s)) s++;
  422. *s = '\0';
  423. symbolsonly(line+2);
  424. break;
  425. case 'F':
  426. /* filename */
  427. while (*s && !isspace(*s)) s++;
  428. *s++ = '\0';
  429. /* function names */
  430. while (isspace(*s))
  431. s++;
  432. singlefunctions(line +2, s);
  433. break;
  434. case 'P':
  435. /* filename */
  436. while (*s && !isspace(*s)) s++;
  437. *s++ = '\0';
  438. /* DOC: section name */
  439. while (isspace(*s))
  440. s++;
  441. docsection(line + 2, s);
  442. break;
  443. case 'C':
  444. while (*s && !isspace(*s)) s++;
  445. *s = '\0';
  446. if (findall)
  447. findall(line+2);
  448. break;
  449. default:
  450. defaultline(line);
  451. }
  452. }
  453. else {
  454. defaultline(line);
  455. }
  456. }
  457. fflush(stdout);
  458. }
  459. int main(int argc, char *argv[])
  460. {
  461. FILE * infile;
  462. int i;
  463. srctree = getenv("SRCTREE");
  464. if (!srctree)
  465. srctree = getcwd(NULL, 0);
  466. kernsrctree = getenv("KBUILD_SRC");
  467. if (!kernsrctree || !*kernsrctree)
  468. kernsrctree = srctree;
  469. if (argc != 3) {
  470. usage();
  471. exit(1);
  472. }
  473. /* Open file, exit on error */
  474. infile = fopen(argv[2], "r");
  475. if (infile == NULL) {
  476. fprintf(stderr, "docproc: ");
  477. perror(argv[2]);
  478. exit(2);
  479. }
  480. if (strcmp("doc", argv[1]) == 0)
  481. {
  482. /* Need to do this in two passes.
  483. * First pass is used to collect all symbols exported
  484. * in the various files;
  485. * Second pass generate the documentation.
  486. * This is required because some functions are declared
  487. * and exported in different files :-((
  488. */
  489. /* Collect symbols */
  490. defaultline = noaction;
  491. internalfunctions = find_export_symbols;
  492. externalfunctions = find_export_symbols;
  493. symbolsonly = find_export_symbols;
  494. singlefunctions = noaction2;
  495. docsection = noaction2;
  496. findall = find_all_symbols;
  497. parse_file(infile);
  498. /* Rewind to start from beginning of file again */
  499. fseek(infile, 0, SEEK_SET);
  500. defaultline = printline;
  501. internalfunctions = intfunc;
  502. externalfunctions = extfunc;
  503. symbolsonly = printline;
  504. singlefunctions = singfunc;
  505. docsection = docsect;
  506. findall = NULL;
  507. parse_file(infile);
  508. for (i = 0; i < all_list_len; i++) {
  509. if (!all_list[i])
  510. continue;
  511. fprintf(stderr, "Warning: didn't use docs for %s\n",
  512. all_list[i]);
  513. }
  514. }
  515. else if (strcmp("depend", argv[1]) == 0)
  516. {
  517. /* Create first part of dependency chain
  518. * file.tmpl */
  519. printf("%s\t", argv[2]);
  520. defaultline = noaction;
  521. internalfunctions = adddep;
  522. externalfunctions = adddep;
  523. symbolsonly = adddep;
  524. singlefunctions = adddep2;
  525. docsection = adddep2;
  526. findall = adddep;
  527. parse_file(infile);
  528. printf("\n");
  529. }
  530. else
  531. {
  532. fprintf(stderr, "Unknown option: %s\n", argv[1]);
  533. exit(1);
  534. }
  535. fclose(infile);
  536. fflush(stdout);
  537. return exitstatus;
  538. }