docproc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. if (asprintf(&s, "DOC: %s", line) < 0) {
  309. perror("asprintf");
  310. exit(1);
  311. }
  312. consume_symbol(s);
  313. free(s);
  314. vec[0] = KERNELDOC;
  315. vec[1] = DOCBOOK;
  316. vec[2] = FUNCTION;
  317. vec[3] = line;
  318. vec[4] = filename;
  319. vec[5] = NULL;
  320. exec_kernel_doc(vec);
  321. }
  322. static void find_all_symbols(char *filename)
  323. {
  324. char *vec[4]; /* kerneldoc -list file NULL */
  325. pid_t pid;
  326. int ret, i, count, start;
  327. char real_filename[PATH_MAX + 1];
  328. int pipefd[2];
  329. char *data, *str;
  330. size_t data_len = 0;
  331. vec[0] = KERNELDOC;
  332. vec[1] = LIST;
  333. vec[2] = filename;
  334. vec[3] = NULL;
  335. if (pipe(pipefd)) {
  336. perror("pipe");
  337. exit(1);
  338. }
  339. switch (pid=fork()) {
  340. case -1:
  341. perror("fork");
  342. exit(1);
  343. case 0:
  344. close(pipefd[0]);
  345. dup2(pipefd[1], 1);
  346. memset(real_filename, 0, sizeof(real_filename));
  347. strncat(real_filename, kernsrctree, PATH_MAX);
  348. strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
  349. PATH_MAX - strlen(real_filename));
  350. execvp(real_filename, vec);
  351. fprintf(stderr, "exec ");
  352. perror(real_filename);
  353. exit(1);
  354. default:
  355. close(pipefd[1]);
  356. data = malloc(4096);
  357. do {
  358. while ((ret = read(pipefd[0],
  359. data + data_len,
  360. 4096)) > 0) {
  361. data_len += ret;
  362. data = realloc(data, data_len + 4096);
  363. }
  364. } while (ret == -EAGAIN);
  365. if (ret != 0) {
  366. perror("read");
  367. exit(1);
  368. }
  369. waitpid(pid, &ret ,0);
  370. }
  371. if (WIFEXITED(ret))
  372. exitstatus |= WEXITSTATUS(ret);
  373. else
  374. exitstatus = 0xff;
  375. count = 0;
  376. /* poor man's strtok, but with counting */
  377. for (i = 0; i < data_len; i++) {
  378. if (data[i] == '\n') {
  379. count++;
  380. data[i] = '\0';
  381. }
  382. }
  383. start = all_list_len;
  384. all_list_len += count;
  385. all_list = realloc(all_list, sizeof(char *) * all_list_len);
  386. str = data;
  387. for (i = 0; i < data_len && start != all_list_len; i++) {
  388. if (data[i] == '\0') {
  389. all_list[start] = str;
  390. str = data + i + 1;
  391. start++;
  392. }
  393. }
  394. }
  395. /*
  396. * Parse file, calling action specific functions for:
  397. * 1) Lines containing !E
  398. * 2) Lines containing !I
  399. * 3) Lines containing !D
  400. * 4) Lines containing !F
  401. * 5) Lines containing !P
  402. * 6) Lines containing !C
  403. * 7) Default lines - lines not matching the above
  404. */
  405. static void parse_file(FILE *infile)
  406. {
  407. char line[MAXLINESZ];
  408. char * s;
  409. while (fgets(line, MAXLINESZ, infile)) {
  410. if (line[0] == '!') {
  411. s = line + 2;
  412. switch (line[1]) {
  413. case 'E':
  414. while (*s && !isspace(*s)) s++;
  415. *s = '\0';
  416. externalfunctions(line+2);
  417. break;
  418. case 'I':
  419. while (*s && !isspace(*s)) s++;
  420. *s = '\0';
  421. internalfunctions(line+2);
  422. break;
  423. case 'D':
  424. while (*s && !isspace(*s)) s++;
  425. *s = '\0';
  426. symbolsonly(line+2);
  427. break;
  428. case 'F':
  429. /* filename */
  430. while (*s && !isspace(*s)) s++;
  431. *s++ = '\0';
  432. /* function names */
  433. while (isspace(*s))
  434. s++;
  435. singlefunctions(line +2, s);
  436. break;
  437. case 'P':
  438. /* filename */
  439. while (*s && !isspace(*s)) s++;
  440. *s++ = '\0';
  441. /* DOC: section name */
  442. while (isspace(*s))
  443. s++;
  444. docsection(line + 2, s);
  445. break;
  446. case 'C':
  447. while (*s && !isspace(*s)) s++;
  448. *s = '\0';
  449. if (findall)
  450. findall(line+2);
  451. break;
  452. default:
  453. defaultline(line);
  454. }
  455. }
  456. else {
  457. defaultline(line);
  458. }
  459. }
  460. fflush(stdout);
  461. }
  462. int main(int argc, char *argv[])
  463. {
  464. FILE * infile;
  465. int i;
  466. srctree = getenv("SRCTREE");
  467. if (!srctree)
  468. srctree = getcwd(NULL, 0);
  469. kernsrctree = getenv("KBUILD_SRC");
  470. if (!kernsrctree || !*kernsrctree)
  471. kernsrctree = srctree;
  472. if (argc != 3) {
  473. usage();
  474. exit(1);
  475. }
  476. /* Open file, exit on error */
  477. infile = fopen(argv[2], "r");
  478. if (infile == NULL) {
  479. fprintf(stderr, "docproc: ");
  480. perror(argv[2]);
  481. exit(2);
  482. }
  483. if (strcmp("doc", argv[1]) == 0)
  484. {
  485. /* Need to do this in two passes.
  486. * First pass is used to collect all symbols exported
  487. * in the various files;
  488. * Second pass generate the documentation.
  489. * This is required because some functions are declared
  490. * and exported in different files :-((
  491. */
  492. /* Collect symbols */
  493. defaultline = noaction;
  494. internalfunctions = find_export_symbols;
  495. externalfunctions = find_export_symbols;
  496. symbolsonly = find_export_symbols;
  497. singlefunctions = noaction2;
  498. docsection = noaction2;
  499. findall = find_all_symbols;
  500. parse_file(infile);
  501. /* Rewind to start from beginning of file again */
  502. fseek(infile, 0, SEEK_SET);
  503. defaultline = printline;
  504. internalfunctions = intfunc;
  505. externalfunctions = extfunc;
  506. symbolsonly = printline;
  507. singlefunctions = singfunc;
  508. docsection = docsect;
  509. findall = NULL;
  510. parse_file(infile);
  511. for (i = 0; i < all_list_len; i++) {
  512. if (!all_list[i])
  513. continue;
  514. fprintf(stderr, "Warning: didn't use docs for %s\n",
  515. all_list[i]);
  516. }
  517. }
  518. else if (strcmp("depend", argv[1]) == 0)
  519. {
  520. /* Create first part of dependency chain
  521. * file.tmpl */
  522. printf("%s\t", argv[2]);
  523. defaultline = noaction;
  524. internalfunctions = adddep;
  525. externalfunctions = adddep;
  526. symbolsonly = adddep;
  527. singlefunctions = adddep2;
  528. docsection = adddep2;
  529. findall = adddep;
  530. parse_file(infile);
  531. printf("\n");
  532. }
  533. else
  534. {
  535. fprintf(stderr, "Unknown option: %s\n", argv[1]);
  536. exit(1);
  537. }
  538. fclose(infile);
  539. fflush(stdout);
  540. return exitstatus;
  541. }