3
0

docproc.c 9.6 KB

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