docproc.c 9.5 KB

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