docproc.c 11 KB

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