docproc.c 10 KB

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