docproc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. fprintf(stderr, "docproc: ");
  189. perror(real_filename);
  190. exit(1);
  191. }
  192. while (fgets(line, MAXLINESZ, fp)) {
  193. char *p;
  194. char *e;
  195. if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
  196. ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
  197. /* Skip EXPORT_SYMBOL{_GPL} */
  198. while (isalnum(*p) || *p == '_')
  199. p++;
  200. /* Remove parentheses & additional whitespace */
  201. while (isspace(*p))
  202. p++;
  203. if (*p != '(')
  204. continue; /* Syntax error? */
  205. else
  206. p++;
  207. while (isspace(*p))
  208. p++;
  209. e = p;
  210. while (isalnum(*e) || *e == '_')
  211. e++;
  212. *e = '\0';
  213. add_new_symbol(sym, p);
  214. }
  215. }
  216. fclose(fp);
  217. }
  218. }
  219. /*
  220. * Document all external or internal functions in a file.
  221. * Call kernel-doc with following parameters:
  222. * kernel-doc -docbook -nofunction function_name1 filename
  223. * Function names are obtained from all the src files
  224. * by find_export_symbols.
  225. * intfunc uses -nofunction
  226. * extfunc uses -function
  227. */
  228. static void docfunctions(char * filename, char * type)
  229. {
  230. int i,j;
  231. int symcnt = 0;
  232. int idx = 0;
  233. char **vec;
  234. for (i=0; i <= symfilecnt; i++)
  235. symcnt += symfilelist[i].symbolcnt;
  236. vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
  237. if (vec == NULL) {
  238. perror("docproc: ");
  239. exit(1);
  240. }
  241. vec[idx++] = KERNELDOC;
  242. vec[idx++] = DOCBOOK;
  243. vec[idx++] = NODOCSECTIONS;
  244. for (i=0; i < symfilecnt; i++) {
  245. struct symfile * sym = &symfilelist[i];
  246. for (j=0; j < sym->symbolcnt; j++) {
  247. vec[idx++] = type;
  248. consume_symbol(sym->symbollist[j].name);
  249. vec[idx++] = sym->symbollist[j].name;
  250. }
  251. }
  252. vec[idx++] = filename;
  253. vec[idx] = NULL;
  254. printf("<!-- %s -->\n", filename);
  255. exec_kernel_doc(vec);
  256. fflush(stdout);
  257. free(vec);
  258. }
  259. static void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
  260. static void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
  261. /*
  262. * Document specific function(s) in a file.
  263. * Call kernel-doc with the following parameters:
  264. * kernel-doc -docbook -function function1 [-function function2]
  265. */
  266. static void singfunc(char * filename, char * line)
  267. {
  268. char *vec[200]; /* Enough for specific functions */
  269. int i, idx = 0;
  270. int startofsym = 1;
  271. vec[idx++] = KERNELDOC;
  272. vec[idx++] = DOCBOOK;
  273. /* Split line up in individual parameters preceded by FUNCTION */
  274. for (i=0; line[i]; i++) {
  275. if (isspace(line[i])) {
  276. line[i] = '\0';
  277. startofsym = 1;
  278. continue;
  279. }
  280. if (startofsym) {
  281. startofsym = 0;
  282. vec[idx++] = FUNCTION;
  283. vec[idx++] = &line[i];
  284. }
  285. }
  286. for (i = 0; i < idx; i++) {
  287. if (strcmp(vec[i], FUNCTION))
  288. continue;
  289. consume_symbol(vec[i + 1]);
  290. }
  291. vec[idx++] = filename;
  292. vec[idx] = NULL;
  293. exec_kernel_doc(vec);
  294. }
  295. /*
  296. * Insert specific documentation section from a file.
  297. * Call kernel-doc with the following parameters:
  298. * kernel-doc -docbook -function "doc section" filename
  299. */
  300. static void docsect(char *filename, char *line)
  301. {
  302. char *vec[6]; /* kerneldoc -docbook -function "section" file NULL */
  303. char *s;
  304. for (s = line; *s; s++)
  305. if (*s == '\n')
  306. *s = '\0';
  307. if (asprintf(&s, "DOC: %s", line) < 0) {
  308. perror("asprintf");
  309. exit(1);
  310. }
  311. consume_symbol(s);
  312. free(s);
  313. vec[0] = KERNELDOC;
  314. vec[1] = DOCBOOK;
  315. vec[2] = FUNCTION;
  316. vec[3] = line;
  317. vec[4] = filename;
  318. vec[5] = NULL;
  319. exec_kernel_doc(vec);
  320. }
  321. static void find_all_symbols(char *filename)
  322. {
  323. char *vec[4]; /* kerneldoc -list file NULL */
  324. pid_t pid;
  325. int ret, i, count, start;
  326. char real_filename[PATH_MAX + 1];
  327. int pipefd[2];
  328. char *data, *str;
  329. size_t data_len = 0;
  330. vec[0] = KERNELDOC;
  331. vec[1] = LIST;
  332. vec[2] = filename;
  333. vec[3] = NULL;
  334. if (pipe(pipefd)) {
  335. perror("pipe");
  336. exit(1);
  337. }
  338. switch (pid=fork()) {
  339. case -1:
  340. perror("fork");
  341. exit(1);
  342. case 0:
  343. close(pipefd[0]);
  344. dup2(pipefd[1], 1);
  345. memset(real_filename, 0, sizeof(real_filename));
  346. strncat(real_filename, kernsrctree, PATH_MAX);
  347. strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
  348. PATH_MAX - strlen(real_filename));
  349. execvp(real_filename, vec);
  350. fprintf(stderr, "exec ");
  351. perror(real_filename);
  352. exit(1);
  353. default:
  354. close(pipefd[1]);
  355. data = malloc(4096);
  356. do {
  357. while ((ret = read(pipefd[0],
  358. data + data_len,
  359. 4096)) > 0) {
  360. data_len += ret;
  361. data = realloc(data, data_len + 4096);
  362. }
  363. } while (ret == -EAGAIN);
  364. if (ret != 0) {
  365. perror("read");
  366. exit(1);
  367. }
  368. waitpid(pid, &ret ,0);
  369. }
  370. if (WIFEXITED(ret))
  371. exitstatus |= WEXITSTATUS(ret);
  372. else
  373. exitstatus = 0xff;
  374. count = 0;
  375. /* poor man's strtok, but with counting */
  376. for (i = 0; i < data_len; i++) {
  377. if (data[i] == '\n') {
  378. count++;
  379. data[i] = '\0';
  380. }
  381. }
  382. start = all_list_len;
  383. all_list_len += count;
  384. all_list = realloc(all_list, sizeof(char *) * all_list_len);
  385. str = data;
  386. for (i = 0; i < data_len && start != all_list_len; i++) {
  387. if (data[i] == '\0') {
  388. all_list[start] = str;
  389. str = data + i + 1;
  390. start++;
  391. }
  392. }
  393. }
  394. /*
  395. * Parse file, calling action specific functions for:
  396. * 1) Lines containing !E
  397. * 2) Lines containing !I
  398. * 3) Lines containing !D
  399. * 4) Lines containing !F
  400. * 5) Lines containing !P
  401. * 6) Lines containing !C
  402. * 7) Default lines - lines not matching the above
  403. */
  404. static void parse_file(FILE *infile)
  405. {
  406. char line[MAXLINESZ];
  407. char * s;
  408. while (fgets(line, MAXLINESZ, infile)) {
  409. if (line[0] == '!') {
  410. s = line + 2;
  411. switch (line[1]) {
  412. case 'E':
  413. while (*s && !isspace(*s)) s++;
  414. *s = '\0';
  415. externalfunctions(line+2);
  416. break;
  417. case 'I':
  418. while (*s && !isspace(*s)) s++;
  419. *s = '\0';
  420. internalfunctions(line+2);
  421. break;
  422. case 'D':
  423. while (*s && !isspace(*s)) s++;
  424. *s = '\0';
  425. symbolsonly(line+2);
  426. break;
  427. case 'F':
  428. /* filename */
  429. while (*s && !isspace(*s)) s++;
  430. *s++ = '\0';
  431. /* function names */
  432. while (isspace(*s))
  433. s++;
  434. singlefunctions(line +2, s);
  435. break;
  436. case 'P':
  437. /* filename */
  438. while (*s && !isspace(*s)) s++;
  439. *s++ = '\0';
  440. /* DOC: section name */
  441. while (isspace(*s))
  442. s++;
  443. docsection(line + 2, s);
  444. break;
  445. case 'C':
  446. while (*s && !isspace(*s)) s++;
  447. *s = '\0';
  448. if (findall)
  449. findall(line+2);
  450. break;
  451. default:
  452. defaultline(line);
  453. }
  454. } else {
  455. defaultline(line);
  456. }
  457. }
  458. fflush(stdout);
  459. }
  460. int main(int argc, char *argv[])
  461. {
  462. FILE * infile;
  463. int i;
  464. srctree = getenv("SRCTREE");
  465. if (!srctree)
  466. srctree = getcwd(NULL, 0);
  467. kernsrctree = getenv("KBUILD_SRC");
  468. if (!kernsrctree || !*kernsrctree)
  469. kernsrctree = srctree;
  470. if (argc != 3) {
  471. usage();
  472. exit(1);
  473. }
  474. /* Open file, exit on error */
  475. infile = fopen(argv[2], "r");
  476. if (infile == NULL) {
  477. fprintf(stderr, "docproc: ");
  478. perror(argv[2]);
  479. exit(2);
  480. }
  481. if (strcmp("doc", argv[1]) == 0) {
  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. } else if (strcmp("depend", argv[1]) == 0) {
  515. /* Create first part of dependency chain
  516. * file.tmpl */
  517. printf("%s\t", argv[2]);
  518. defaultline = noaction;
  519. internalfunctions = adddep;
  520. externalfunctions = adddep;
  521. symbolsonly = adddep;
  522. singlefunctions = adddep2;
  523. docsection = adddep2;
  524. findall = adddep;
  525. parse_file(infile);
  526. printf("\n");
  527. } else {
  528. fprintf(stderr, "Unknown option: %s\n", argv[1]);
  529. exit(1);
  530. }
  531. fclose(infile);
  532. fflush(stdout);
  533. return exitstatus;
  534. }