docproc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include <unistd.h>
  41. #include <limits.h>
  42. #include <sys/types.h>
  43. #include <sys/wait.h>
  44. /* exitstatus is used to keep track of any failing calls to kernel-doc,
  45. * but execution continues. */
  46. int exitstatus = 0;
  47. typedef void DFL(char *);
  48. DFL *defaultline;
  49. typedef void FILEONLY(char * file);
  50. FILEONLY *internalfunctions;
  51. FILEONLY *externalfunctions;
  52. FILEONLY *symbolsonly;
  53. typedef void FILELINE(char * file, char * line);
  54. FILELINE * singlefunctions;
  55. FILELINE * entity_system;
  56. FILELINE * docsection;
  57. #define MAXLINESZ 2048
  58. #define MAXFILES 250
  59. #define KERNELDOCPATH "scripts/"
  60. #define KERNELDOC "kernel-doc"
  61. #define DOCBOOK "-docbook"
  62. #define FUNCTION "-function"
  63. #define NOFUNCTION "-nofunction"
  64. #define NODOCSECTIONS "-no-doc-sections"
  65. static char *srctree, *kernsrctree;
  66. void usage (void)
  67. {
  68. fprintf(stderr, "Usage: docproc {doc|depend} file\n");
  69. fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
  70. fprintf(stderr, "doc: frontend when generating kernel documentation\n");
  71. fprintf(stderr, "depend: generate list of files referenced within file\n");
  72. fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n");
  73. fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n");
  74. }
  75. /*
  76. * Execute kernel-doc with parameters given in svec
  77. */
  78. void exec_kernel_doc(char **svec)
  79. {
  80. pid_t pid;
  81. int ret;
  82. char real_filename[PATH_MAX + 1];
  83. /* Make sure output generated so far are flushed */
  84. fflush(stdout);
  85. switch (pid=fork()) {
  86. case -1:
  87. perror("fork");
  88. exit(1);
  89. case 0:
  90. memset(real_filename, 0, sizeof(real_filename));
  91. strncat(real_filename, kernsrctree, PATH_MAX);
  92. strncat(real_filename, "/" KERNELDOCPATH KERNELDOC,
  93. PATH_MAX - strlen(real_filename));
  94. execvp(real_filename, svec);
  95. fprintf(stderr, "exec ");
  96. perror(real_filename);
  97. exit(1);
  98. default:
  99. waitpid(pid, &ret ,0);
  100. }
  101. if (WIFEXITED(ret))
  102. exitstatus |= WEXITSTATUS(ret);
  103. else
  104. exitstatus = 0xff;
  105. }
  106. /* Types used to create list of all exported symbols in a number of files */
  107. struct symbols
  108. {
  109. char *name;
  110. };
  111. struct symfile
  112. {
  113. char *filename;
  114. struct symbols *symbollist;
  115. int symbolcnt;
  116. };
  117. struct symfile symfilelist[MAXFILES];
  118. int symfilecnt = 0;
  119. void add_new_symbol(struct symfile *sym, char * symname)
  120. {
  121. sym->symbollist =
  122. realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
  123. sym->symbollist[sym->symbolcnt++].name = strdup(symname);
  124. }
  125. /* Add a filename to the list */
  126. struct symfile * add_new_file(char * filename)
  127. {
  128. symfilelist[symfilecnt++].filename = strdup(filename);
  129. return &symfilelist[symfilecnt - 1];
  130. }
  131. /* Check if file already are present in the list */
  132. struct symfile * filename_exist(char * filename)
  133. {
  134. int i;
  135. for (i=0; i < symfilecnt; i++)
  136. if (strcmp(symfilelist[i].filename, filename) == 0)
  137. return &symfilelist[i];
  138. return NULL;
  139. }
  140. /*
  141. * List all files referenced within the template file.
  142. * Files are separated by tabs.
  143. */
  144. void adddep(char * file) { printf("\t%s", file); }
  145. void adddep2(char * file, char * line) { line = line; adddep(file); }
  146. void noaction(char * line) { line = line; }
  147. void noaction2(char * file, char * line) { file = file; line = line; }
  148. /* Echo the line without further action */
  149. void printline(char * line) { printf("%s", line); }
  150. /*
  151. * Find all symbols in filename that are exported with EXPORT_SYMBOL &
  152. * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly).
  153. * All symbols located are stored in symfilelist.
  154. */
  155. void find_export_symbols(char * filename)
  156. {
  157. FILE * fp;
  158. struct symfile *sym;
  159. char line[MAXLINESZ];
  160. if (filename_exist(filename) == NULL) {
  161. char real_filename[PATH_MAX + 1];
  162. memset(real_filename, 0, sizeof(real_filename));
  163. strncat(real_filename, srctree, PATH_MAX);
  164. strncat(real_filename, "/", PATH_MAX - strlen(real_filename));
  165. strncat(real_filename, filename,
  166. PATH_MAX - strlen(real_filename));
  167. sym = add_new_file(filename);
  168. fp = fopen(real_filename, "r");
  169. if (fp == NULL)
  170. {
  171. fprintf(stderr, "docproc: ");
  172. perror(real_filename);
  173. exit(1);
  174. }
  175. while (fgets(line, MAXLINESZ, fp)) {
  176. char *p;
  177. char *e;
  178. if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) ||
  179. ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) {
  180. /* Skip EXPORT_SYMBOL{_GPL} */
  181. while (isalnum(*p) || *p == '_')
  182. p++;
  183. /* Remove parentheses & additional whitespace */
  184. while (isspace(*p))
  185. p++;
  186. if (*p != '(')
  187. continue; /* Syntax error? */
  188. else
  189. p++;
  190. while (isspace(*p))
  191. p++;
  192. e = p;
  193. while (isalnum(*e) || *e == '_')
  194. e++;
  195. *e = '\0';
  196. add_new_symbol(sym, p);
  197. }
  198. }
  199. fclose(fp);
  200. }
  201. }
  202. /*
  203. * Document all external or internal functions in a file.
  204. * Call kernel-doc with following parameters:
  205. * kernel-doc -docbook -nofunction function_name1 filename
  206. * Function names are obtained from all the src files
  207. * by find_export_symbols.
  208. * intfunc uses -nofunction
  209. * extfunc uses -function
  210. */
  211. void docfunctions(char * filename, char * type)
  212. {
  213. int i,j;
  214. int symcnt = 0;
  215. int idx = 0;
  216. char **vec;
  217. for (i=0; i <= symfilecnt; i++)
  218. symcnt += symfilelist[i].symbolcnt;
  219. vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *));
  220. if (vec == NULL) {
  221. perror("docproc: ");
  222. exit(1);
  223. }
  224. vec[idx++] = KERNELDOC;
  225. vec[idx++] = DOCBOOK;
  226. vec[idx++] = NODOCSECTIONS;
  227. for (i=0; i < symfilecnt; i++) {
  228. struct symfile * sym = &symfilelist[i];
  229. for (j=0; j < sym->symbolcnt; j++) {
  230. vec[idx++] = type;
  231. vec[idx++] = sym->symbollist[j].name;
  232. }
  233. }
  234. vec[idx++] = filename;
  235. vec[idx] = NULL;
  236. printf("<!-- %s -->\n", filename);
  237. exec_kernel_doc(vec);
  238. fflush(stdout);
  239. free(vec);
  240. }
  241. void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
  242. void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
  243. /*
  244. * Document specific function(s) in a file.
  245. * Call kernel-doc with the following parameters:
  246. * kernel-doc -docbook -function function1 [-function function2]
  247. */
  248. void singfunc(char * filename, char * line)
  249. {
  250. char *vec[200]; /* Enough for specific functions */
  251. int i, idx = 0;
  252. int startofsym = 1;
  253. vec[idx++] = KERNELDOC;
  254. vec[idx++] = DOCBOOK;
  255. /* Split line up in individual parameters preceded by FUNCTION */
  256. for (i=0; line[i]; i++) {
  257. if (isspace(line[i])) {
  258. line[i] = '\0';
  259. startofsym = 1;
  260. continue;
  261. }
  262. if (startofsym) {
  263. startofsym = 0;
  264. vec[idx++] = FUNCTION;
  265. vec[idx++] = &line[i];
  266. }
  267. }
  268. vec[idx++] = filename;
  269. vec[idx] = NULL;
  270. exec_kernel_doc(vec);
  271. }
  272. /*
  273. * Insert specific documentation section from a file.
  274. * Call kernel-doc with the following parameters:
  275. * kernel-doc -docbook -function "doc section" filename
  276. */
  277. void docsect(char *filename, char *line)
  278. {
  279. char *vec[6]; /* kerneldoc -docbook -function "section" file NULL */
  280. char *s;
  281. for (s = line; *s; s++)
  282. if (*s == '\n')
  283. *s = '\0';
  284. vec[0] = KERNELDOC;
  285. vec[1] = DOCBOOK;
  286. vec[2] = FUNCTION;
  287. vec[3] = line;
  288. vec[4] = filename;
  289. vec[5] = NULL;
  290. exec_kernel_doc(vec);
  291. }
  292. /*
  293. * Parse file, calling action specific functions for:
  294. * 1) Lines containing !E
  295. * 2) Lines containing !I
  296. * 3) Lines containing !D
  297. * 4) Lines containing !F
  298. * 5) Lines containing !P
  299. * 6) Default lines - lines not matching the above
  300. */
  301. void parse_file(FILE *infile)
  302. {
  303. char line[MAXLINESZ];
  304. char * s;
  305. while (fgets(line, MAXLINESZ, infile)) {
  306. if (line[0] == '!') {
  307. s = line + 2;
  308. switch (line[1]) {
  309. case 'E':
  310. while (*s && !isspace(*s)) s++;
  311. *s = '\0';
  312. externalfunctions(line+2);
  313. break;
  314. case 'I':
  315. while (*s && !isspace(*s)) s++;
  316. *s = '\0';
  317. internalfunctions(line+2);
  318. break;
  319. case 'D':
  320. while (*s && !isspace(*s)) s++;
  321. *s = '\0';
  322. symbolsonly(line+2);
  323. break;
  324. case 'F':
  325. /* filename */
  326. while (*s && !isspace(*s)) s++;
  327. *s++ = '\0';
  328. /* function names */
  329. while (isspace(*s))
  330. s++;
  331. singlefunctions(line +2, s);
  332. break;
  333. case 'P':
  334. /* filename */
  335. while (*s && !isspace(*s)) s++;
  336. *s++ = '\0';
  337. /* DOC: section name */
  338. while (isspace(*s))
  339. s++;
  340. docsection(line + 2, s);
  341. break;
  342. default:
  343. defaultline(line);
  344. }
  345. }
  346. else {
  347. defaultline(line);
  348. }
  349. }
  350. fflush(stdout);
  351. }
  352. int main(int argc, char *argv[])
  353. {
  354. FILE * infile;
  355. srctree = getenv("SRCTREE");
  356. if (!srctree)
  357. srctree = getcwd(NULL, 0);
  358. kernsrctree = getenv("KBUILD_SRC");
  359. if (!kernsrctree)
  360. kernsrctree = srctree;
  361. if (argc != 3) {
  362. usage();
  363. exit(1);
  364. }
  365. /* Open file, exit on error */
  366. infile = fopen(argv[2], "r");
  367. if (infile == NULL) {
  368. fprintf(stderr, "docproc: ");
  369. perror(argv[2]);
  370. exit(2);
  371. }
  372. if (strcmp("doc", argv[1]) == 0)
  373. {
  374. /* Need to do this in two passes.
  375. * First pass is used to collect all symbols exported
  376. * in the various files;
  377. * Second pass generate the documentation.
  378. * This is required because some functions are declared
  379. * and exported in different files :-((
  380. */
  381. /* Collect symbols */
  382. defaultline = noaction;
  383. internalfunctions = find_export_symbols;
  384. externalfunctions = find_export_symbols;
  385. symbolsonly = find_export_symbols;
  386. singlefunctions = noaction2;
  387. docsection = noaction2;
  388. parse_file(infile);
  389. /* Rewind to start from beginning of file again */
  390. fseek(infile, 0, SEEK_SET);
  391. defaultline = printline;
  392. internalfunctions = intfunc;
  393. externalfunctions = extfunc;
  394. symbolsonly = printline;
  395. singlefunctions = singfunc;
  396. docsection = docsect;
  397. parse_file(infile);
  398. }
  399. else if (strcmp("depend", argv[1]) == 0)
  400. {
  401. /* Create first part of dependency chain
  402. * file.tmpl */
  403. printf("%s\t", argv[2]);
  404. defaultline = noaction;
  405. internalfunctions = adddep;
  406. externalfunctions = adddep;
  407. symbolsonly = adddep;
  408. singlefunctions = adddep2;
  409. docsection = adddep2;
  410. parse_file(infile);
  411. printf("\n");
  412. }
  413. else
  414. {
  415. fprintf(stderr, "Unknown option: %s\n", argv[1]);
  416. exit(1);
  417. }
  418. fclose(infile);
  419. fflush(stdout);
  420. return exitstatus;
  421. }