docproc.c 10 KB

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