docproc.c 10 KB

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