make-docfile.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /* Generate doc-string file for GNU Emacs from source files.
  2. Copyright (C) 1985-1986, 1992-1994, 1997, 1999-2012
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Emacs.
  5. GNU Emacs is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. GNU Emacs is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  15. /* The arguments given to this program are all the C and Lisp source files
  16. of GNU Emacs. .elc and .el and .c files are allowed.
  17. A .o file can also be specified; the .c file it was made from is used.
  18. This helps the makefile pass the correct list of files.
  19. Option -d DIR means change to DIR before looking for files.
  20. The results, which go to standard output or to a file
  21. specified with -a or -o (-a to append, -o to start from nothing),
  22. are entries containing function or variable names and their documentation.
  23. Each entry starts with a ^_ character.
  24. Then comes F for a function or V for a variable.
  25. Then comes the function or variable name, terminated with a newline.
  26. Then comes the documentation for that function or variable.
  27. */
  28. #include <config.h>
  29. /* defined to be emacs_main, sys_fopen, etc. in config.h */
  30. #undef main
  31. #undef fopen
  32. #undef chdir
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #ifdef MSDOS
  36. #include <fcntl.h>
  37. #endif /* MSDOS */
  38. #ifdef WINDOWSNT
  39. #include <fcntl.h>
  40. #include <direct.h>
  41. #endif /* WINDOWSNT */
  42. #ifdef DOS_NT
  43. #define READ_TEXT "rt"
  44. #define READ_BINARY "rb"
  45. #else /* not DOS_NT */
  46. #define READ_TEXT "r"
  47. #define READ_BINARY "r"
  48. #endif /* not DOS_NT */
  49. #ifndef DIRECTORY_SEP
  50. #define DIRECTORY_SEP '/'
  51. #endif
  52. #ifndef IS_DIRECTORY_SEP
  53. #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
  54. #endif
  55. /* Use this to suppress gcc's `...may be used before initialized' warnings. */
  56. #ifdef lint
  57. # define IF_LINT(Code) Code
  58. #else
  59. # define IF_LINT(Code) /* empty */
  60. #endif
  61. static int scan_file (char *filename);
  62. static int scan_lisp_file (const char *filename, const char *mode);
  63. static int scan_c_file (char *filename, const char *mode);
  64. static void fatal (const char *s1, const char *s2) NO_RETURN;
  65. static void start_globals (void);
  66. static void write_globals (void);
  67. #ifdef MSDOS
  68. /* s/msdos.h defines this as sys_chdir, but we're not linking with the
  69. file where that function is defined. */
  70. #undef chdir
  71. #endif
  72. #include <unistd.h>
  73. /* Stdio stream for output to the DOC file. */
  74. FILE *outfile;
  75. /* Name this program was invoked with. */
  76. char *progname;
  77. /* Nonzero if this invocation is generating globals.h. */
  78. int generate_globals;
  79. /* Print error message. `s1' is printf control string, `s2' is arg for it. */
  80. /* VARARGS1 */
  81. static void
  82. error (const char *s1, const char *s2)
  83. {
  84. fprintf (stderr, "%s: ", progname);
  85. fprintf (stderr, s1, s2);
  86. fprintf (stderr, "\n");
  87. }
  88. /* Print error message and exit. */
  89. /* VARARGS1 */
  90. static void
  91. fatal (const char *s1, const char *s2)
  92. {
  93. error (s1, s2);
  94. exit (EXIT_FAILURE);
  95. }
  96. /* Like malloc but get fatal error if memory is exhausted. */
  97. static void *
  98. xmalloc (unsigned int size)
  99. {
  100. void *result = (void *) malloc (size);
  101. if (result == NULL)
  102. fatal ("virtual memory exhausted", 0);
  103. return result;
  104. }
  105. /* Like realloc but get fatal error if memory is exhausted. */
  106. static void *
  107. xrealloc (void *arg, unsigned int size)
  108. {
  109. void *result = (void *) realloc (arg, size);
  110. if (result == NULL)
  111. fatal ("virtual memory exhausted", 0);
  112. return result;
  113. }
  114. int
  115. main (int argc, char **argv)
  116. {
  117. int i;
  118. int err_count = 0;
  119. int first_infile;
  120. progname = argv[0];
  121. outfile = stdout;
  122. /* Don't put CRs in the DOC file. */
  123. #ifdef MSDOS
  124. _fmode = O_BINARY;
  125. #if 0 /* Suspicion is that this causes hanging.
  126. So instead we require people to use -o on MSDOS. */
  127. (stdout)->_flag &= ~_IOTEXT;
  128. _setmode (fileno (stdout), O_BINARY);
  129. #endif
  130. outfile = 0;
  131. #endif /* MSDOS */
  132. #ifdef WINDOWSNT
  133. _fmode = O_BINARY;
  134. _setmode (fileno (stdout), O_BINARY);
  135. #endif /* WINDOWSNT */
  136. /* If first two args are -o FILE, output to FILE. */
  137. i = 1;
  138. if (argc > i + 1 && !strcmp (argv[i], "-o"))
  139. {
  140. outfile = fopen (argv[i + 1], "w");
  141. i += 2;
  142. }
  143. if (argc > i + 1 && !strcmp (argv[i], "-a"))
  144. {
  145. outfile = fopen (argv[i + 1], "a");
  146. i += 2;
  147. }
  148. if (argc > i + 1 && !strcmp (argv[i], "-d"))
  149. {
  150. if (chdir (argv[i + 1]) != 0)
  151. {
  152. perror (argv[i + 1]);
  153. return EXIT_FAILURE;
  154. }
  155. i += 2;
  156. }
  157. if (argc > i && !strcmp (argv[i], "-g"))
  158. {
  159. generate_globals = 1;
  160. ++i;
  161. }
  162. if (outfile == 0)
  163. fatal ("No output file specified", "");
  164. if (generate_globals)
  165. start_globals ();
  166. first_infile = i;
  167. for (; i < argc; i++)
  168. {
  169. int j;
  170. /* Don't process one file twice. */
  171. for (j = first_infile; j < i; j++)
  172. if (! strcmp (argv[i], argv[j]))
  173. break;
  174. if (j == i)
  175. err_count += scan_file (argv[i]);
  176. }
  177. if (err_count == 0 && generate_globals)
  178. write_globals ();
  179. return (err_count > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
  180. }
  181. /* Add a source file name boundary marker in the output file. */
  182. static void
  183. put_filename (char *filename)
  184. {
  185. char *tmp;
  186. for (tmp = filename; *tmp; tmp++)
  187. {
  188. if (IS_DIRECTORY_SEP(*tmp))
  189. filename = tmp + 1;
  190. }
  191. putc (037, outfile);
  192. putc ('S', outfile);
  193. fprintf (outfile, "%s\n", filename);
  194. }
  195. /* Read file FILENAME and output its doc strings to outfile. */
  196. /* Return 1 if file is not found, 0 if it is found. */
  197. static int
  198. scan_file (char *filename)
  199. {
  200. size_t len = strlen (filename);
  201. if (!generate_globals)
  202. put_filename (filename);
  203. if (len > 4 && !strcmp (filename + len - 4, ".elc"))
  204. return scan_lisp_file (filename, READ_BINARY);
  205. else if (len > 3 && !strcmp (filename + len - 3, ".el"))
  206. return scan_lisp_file (filename, READ_TEXT);
  207. else
  208. return scan_c_file (filename, READ_TEXT);
  209. }
  210. static void
  211. start_globals (void)
  212. {
  213. fprintf (outfile, "/* This file was auto-generated by make-docfile. */\n");
  214. fprintf (outfile, "/* DO NOT EDIT. */\n");
  215. fprintf (outfile, "struct emacs_globals {\n");
  216. }
  217. static char input_buffer[128];
  218. /* Some state during the execution of `read_c_string_or_comment'. */
  219. struct rcsoc_state
  220. {
  221. /* A count of spaces and newlines that have been read, but not output. */
  222. unsigned pending_spaces, pending_newlines;
  223. /* Where we're reading from. */
  224. FILE *in_file;
  225. /* If non-zero, a buffer into which to copy characters. */
  226. char *buf_ptr;
  227. /* If non-zero, a file into which to copy characters. */
  228. FILE *out_file;
  229. /* A keyword we look for at the beginning of lines. If found, it is
  230. not copied, and SAW_KEYWORD is set to true. */
  231. const char *keyword;
  232. /* The current point we've reached in an occurrence of KEYWORD in
  233. the input stream. */
  234. const char *cur_keyword_ptr;
  235. /* Set to true if we saw an occurrence of KEYWORD. */
  236. int saw_keyword;
  237. };
  238. /* Output CH to the file or buffer in STATE. Any pending newlines or
  239. spaces are output first. */
  240. static inline void
  241. put_char (int ch, struct rcsoc_state *state)
  242. {
  243. int out_ch;
  244. do
  245. {
  246. if (state->pending_newlines > 0)
  247. {
  248. state->pending_newlines--;
  249. out_ch = '\n';
  250. }
  251. else if (state->pending_spaces > 0)
  252. {
  253. state->pending_spaces--;
  254. out_ch = ' ';
  255. }
  256. else
  257. out_ch = ch;
  258. if (state->out_file)
  259. putc (out_ch, state->out_file);
  260. if (state->buf_ptr)
  261. *state->buf_ptr++ = out_ch;
  262. }
  263. while (out_ch != ch);
  264. }
  265. /* If in the middle of scanning a keyword, continue scanning with
  266. character CH, otherwise output CH to the file or buffer in STATE.
  267. Any pending newlines or spaces are output first, as well as any
  268. previously scanned characters that were thought to be part of a
  269. keyword, but were in fact not. */
  270. static void
  271. scan_keyword_or_put_char (int ch, struct rcsoc_state *state)
  272. {
  273. if (state->keyword
  274. && *state->cur_keyword_ptr == ch
  275. && (state->cur_keyword_ptr > state->keyword
  276. || state->pending_newlines > 0))
  277. /* We might be looking at STATE->keyword at some point.
  278. Keep looking until we know for sure. */
  279. {
  280. if (*++state->cur_keyword_ptr == '\0')
  281. /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
  282. {
  283. state->saw_keyword = 1;
  284. /* Reset the scanning pointer. */
  285. state->cur_keyword_ptr = state->keyword;
  286. /* Canonicalize whitespace preceding a usage string. */
  287. state->pending_newlines = 2;
  288. state->pending_spaces = 0;
  289. /* Skip any whitespace between the keyword and the
  290. usage string. */
  291. do
  292. ch = getc (state->in_file);
  293. while (ch == ' ' || ch == '\n');
  294. /* Output the open-paren we just read. */
  295. put_char (ch, state);
  296. /* Skip the function name and replace it with `fn'. */
  297. do
  298. ch = getc (state->in_file);
  299. while (ch != ' ' && ch != ')');
  300. put_char ('f', state);
  301. put_char ('n', state);
  302. /* Put back the last character. */
  303. ungetc (ch, state->in_file);
  304. }
  305. }
  306. else
  307. {
  308. if (state->keyword && state->cur_keyword_ptr > state->keyword)
  309. /* We scanned the beginning of a potential usage
  310. keyword, but it was a false alarm. Output the
  311. part we scanned. */
  312. {
  313. const char *p;
  314. for (p = state->keyword; p < state->cur_keyword_ptr; p++)
  315. put_char (*p, state);
  316. state->cur_keyword_ptr = state->keyword;
  317. }
  318. put_char (ch, state);
  319. }
  320. }
  321. /* Skip a C string or C-style comment from INFILE, and return the
  322. character that follows. COMMENT non-zero means skip a comment. If
  323. PRINTFLAG is positive, output string contents to outfile. If it is
  324. negative, store contents in buf. Convert escape sequences \n and
  325. \t to newline and tab; discard \ followed by newline.
  326. If SAW_USAGE is non-zero, then any occurrences of the string `usage:'
  327. at the beginning of a line will be removed, and *SAW_USAGE set to
  328. true if any were encountered. */
  329. static int
  330. read_c_string_or_comment (FILE *infile, int printflag, int comment, int *saw_usage)
  331. {
  332. register int c;
  333. struct rcsoc_state state;
  334. state.in_file = infile;
  335. state.buf_ptr = (printflag < 0 ? input_buffer : 0);
  336. state.out_file = (printflag > 0 ? outfile : 0);
  337. state.pending_spaces = 0;
  338. state.pending_newlines = 0;
  339. state.keyword = (saw_usage ? "usage:" : 0);
  340. state.cur_keyword_ptr = state.keyword;
  341. state.saw_keyword = 0;
  342. c = getc (infile);
  343. if (comment)
  344. while (c == '\n' || c == '\r' || c == '\t' || c == ' ')
  345. c = getc (infile);
  346. while (c != EOF)
  347. {
  348. while (c != EOF && (comment ? c != '*' : c != '"'))
  349. {
  350. if (c == '\\')
  351. {
  352. c = getc (infile);
  353. if (c == '\n' || c == '\r')
  354. {
  355. c = getc (infile);
  356. continue;
  357. }
  358. if (c == 'n')
  359. c = '\n';
  360. if (c == 't')
  361. c = '\t';
  362. }
  363. if (c == ' ')
  364. state.pending_spaces++;
  365. else if (c == '\n')
  366. {
  367. state.pending_newlines++;
  368. state.pending_spaces = 0;
  369. }
  370. else
  371. scan_keyword_or_put_char (c, &state);
  372. c = getc (infile);
  373. }
  374. if (c != EOF)
  375. c = getc (infile);
  376. if (comment)
  377. {
  378. if (c == '/')
  379. {
  380. c = getc (infile);
  381. break;
  382. }
  383. scan_keyword_or_put_char ('*', &state);
  384. }
  385. else
  386. {
  387. if (c != '"')
  388. break;
  389. /* If we had a "", concatenate the two strings. */
  390. c = getc (infile);
  391. }
  392. }
  393. if (printflag < 0)
  394. *state.buf_ptr = 0;
  395. if (saw_usage)
  396. *saw_usage = state.saw_keyword;
  397. return c;
  398. }
  399. /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
  400. MINARGS and MAXARGS are the minimum and maximum number of arguments. */
  401. static void
  402. write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs)
  403. {
  404. register char *p;
  405. int in_ident = 0;
  406. char *ident_start IF_LINT (= NULL);
  407. size_t ident_length = 0;
  408. fprintf (out, "(fn");
  409. if (*buf == '(')
  410. ++buf;
  411. for (p = buf; *p; p++)
  412. {
  413. char c = *p;
  414. /* Notice when a new identifier starts. */
  415. if ((('A' <= c && c <= 'Z')
  416. || ('a' <= c && c <= 'z')
  417. || ('0' <= c && c <= '9')
  418. || c == '_')
  419. != in_ident)
  420. {
  421. if (!in_ident)
  422. {
  423. in_ident = 1;
  424. ident_start = p;
  425. }
  426. else
  427. {
  428. in_ident = 0;
  429. ident_length = p - ident_start;
  430. }
  431. }
  432. /* Found the end of an argument, write out the last seen
  433. identifier. */
  434. if (c == ',' || c == ')')
  435. {
  436. if (ident_length == 0)
  437. {
  438. error ("empty arg list for `%s' should be (void), not ()", func);
  439. continue;
  440. }
  441. if (strncmp (ident_start, "void", ident_length) == 0)
  442. continue;
  443. putc (' ', out);
  444. if (minargs == 0 && maxargs > 0)
  445. fprintf (out, "&optional ");
  446. minargs--;
  447. maxargs--;
  448. /* In C code, `default' is a reserved word, so we spell it
  449. `defalt'; demangle that here. */
  450. if (ident_length == 6 && strncmp (ident_start, "defalt", 6) == 0)
  451. fprintf (out, "DEFAULT");
  452. else
  453. while (ident_length-- > 0)
  454. {
  455. c = *ident_start++;
  456. if (c >= 'a' && c <= 'z')
  457. /* Upcase the letter. */
  458. c += 'A' - 'a';
  459. else if (c == '_')
  460. /* Print underscore as hyphen. */
  461. c = '-';
  462. putc (c, out);
  463. }
  464. }
  465. }
  466. putc (')', out);
  467. }
  468. /* The types of globals. */
  469. enum global_type
  470. {
  471. EMACS_INTEGER,
  472. BOOLEAN,
  473. LISP_OBJECT,
  474. INVALID
  475. };
  476. /* A single global. */
  477. struct global
  478. {
  479. enum global_type type;
  480. char *name;
  481. };
  482. /* All the variable names we saw while scanning C sources in `-g'
  483. mode. */
  484. int num_globals;
  485. int num_globals_allocated;
  486. struct global *globals;
  487. static void
  488. add_global (enum global_type type, char *name)
  489. {
  490. /* Ignore the one non-symbol that can occur. */
  491. if (strcmp (name, "..."))
  492. {
  493. ++num_globals;
  494. if (num_globals_allocated == 0)
  495. {
  496. num_globals_allocated = 100;
  497. globals = xmalloc (num_globals_allocated * sizeof (struct global));
  498. }
  499. else if (num_globals == num_globals_allocated)
  500. {
  501. num_globals_allocated *= 2;
  502. globals = xrealloc (globals,
  503. num_globals_allocated * sizeof (struct global));
  504. }
  505. globals[num_globals - 1].type = type;
  506. globals[num_globals - 1].name = name;
  507. }
  508. }
  509. static int
  510. compare_globals (const void *a, const void *b)
  511. {
  512. const struct global *ga = a;
  513. const struct global *gb = b;
  514. return strcmp (ga->name, gb->name);
  515. }
  516. static void
  517. write_globals (void)
  518. {
  519. int i;
  520. qsort (globals, num_globals, sizeof (struct global), compare_globals);
  521. for (i = 0; i < num_globals; ++i)
  522. {
  523. char const *type;
  524. switch (globals[i].type)
  525. {
  526. case EMACS_INTEGER:
  527. type = "EMACS_INT";
  528. break;
  529. case BOOLEAN:
  530. type = "int";
  531. break;
  532. case LISP_OBJECT:
  533. type = "Lisp_Object";
  534. break;
  535. default:
  536. fatal ("not a recognized DEFVAR_", 0);
  537. }
  538. fprintf (outfile, " %s f_%s;\n", type, globals[i].name);
  539. fprintf (outfile, "#define %s globals.f_%s\n",
  540. globals[i].name, globals[i].name);
  541. while (i + 1 < num_globals
  542. && !strcmp (globals[i].name, globals[i + 1].name))
  543. ++i;
  544. }
  545. fprintf (outfile, "};\n");
  546. fprintf (outfile, "extern struct emacs_globals globals;\n");
  547. }
  548. /* Read through a c file. If a .o file is named,
  549. the corresponding .c or .m file is read instead.
  550. Looks for DEFUN constructs such as are defined in ../src/lisp.h.
  551. Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
  552. static int
  553. scan_c_file (char *filename, const char *mode)
  554. {
  555. FILE *infile;
  556. register int c;
  557. register int commas;
  558. int minargs, maxargs;
  559. int extension = filename[strlen (filename) - 1];
  560. if (extension == 'o')
  561. filename[strlen (filename) - 1] = 'c';
  562. infile = fopen (filename, mode);
  563. if (infile == NULL && extension == 'o')
  564. {
  565. /* try .m */
  566. filename[strlen (filename) - 1] = 'm';
  567. infile = fopen (filename, mode);
  568. if (infile == NULL)
  569. filename[strlen (filename) - 1] = 'c'; /* don't confuse people */
  570. }
  571. /* No error if non-ex input file */
  572. if (infile == NULL)
  573. {
  574. perror (filename);
  575. return 0;
  576. }
  577. /* Reset extension to be able to detect duplicate files. */
  578. filename[strlen (filename) - 1] = extension;
  579. c = '\n';
  580. while (!feof (infile))
  581. {
  582. int doc_keyword = 0;
  583. int defunflag = 0;
  584. int defvarperbufferflag = 0;
  585. int defvarflag = 0;
  586. enum global_type type = INVALID;
  587. if (c != '\n' && c != '\r')
  588. {
  589. c = getc (infile);
  590. continue;
  591. }
  592. c = getc (infile);
  593. if (c == ' ')
  594. {
  595. while (c == ' ')
  596. c = getc (infile);
  597. if (c != 'D')
  598. continue;
  599. c = getc (infile);
  600. if (c != 'E')
  601. continue;
  602. c = getc (infile);
  603. if (c != 'F')
  604. continue;
  605. c = getc (infile);
  606. if (c != 'V')
  607. continue;
  608. c = getc (infile);
  609. if (c != 'A')
  610. continue;
  611. c = getc (infile);
  612. if (c != 'R')
  613. continue;
  614. c = getc (infile);
  615. if (c != '_')
  616. continue;
  617. defvarflag = 1;
  618. c = getc (infile);
  619. defvarperbufferflag = (c == 'P');
  620. if (generate_globals)
  621. {
  622. if (c == 'I')
  623. type = EMACS_INTEGER;
  624. else if (c == 'L')
  625. type = LISP_OBJECT;
  626. else if (c == 'B')
  627. type = BOOLEAN;
  628. }
  629. c = getc (infile);
  630. /* We need to distinguish between DEFVAR_BOOL and
  631. DEFVAR_BUFFER_DEFAULTS. */
  632. if (generate_globals && type == BOOLEAN && c != 'O')
  633. type = INVALID;
  634. }
  635. else if (c == 'D')
  636. {
  637. c = getc (infile);
  638. if (c != 'E')
  639. continue;
  640. c = getc (infile);
  641. if (c != 'F')
  642. continue;
  643. c = getc (infile);
  644. defunflag = c == 'U';
  645. }
  646. else continue;
  647. if (generate_globals && (!defvarflag || defvarperbufferflag
  648. || type == INVALID))
  649. continue;
  650. while (c != '(')
  651. {
  652. if (c < 0)
  653. goto eof;
  654. c = getc (infile);
  655. }
  656. /* Lisp variable or function name. */
  657. c = getc (infile);
  658. if (c != '"')
  659. continue;
  660. c = read_c_string_or_comment (infile, -1, 0, 0);
  661. if (generate_globals)
  662. {
  663. int i = 0;
  664. char *name;
  665. /* Skip "," and whitespace. */
  666. do
  667. {
  668. c = getc (infile);
  669. }
  670. while (c == ',' || c == ' ' || c == '\t' || c == '\n' || c == '\r');
  671. /* Read in the identifier. */
  672. do
  673. {
  674. input_buffer[i++] = c;
  675. c = getc (infile);
  676. }
  677. while (! (c == ',' || c == ' ' || c == '\t' ||
  678. c == '\n' || c == '\r'));
  679. input_buffer[i] = '\0';
  680. name = xmalloc (i + 1);
  681. memcpy (name, input_buffer, i + 1);
  682. add_global (type, name);
  683. continue;
  684. }
  685. /* DEFVAR_LISP ("name", addr, "doc")
  686. DEFVAR_LISP ("name", addr /\* doc *\/)
  687. DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
  688. if (defunflag)
  689. commas = 5;
  690. else if (defvarperbufferflag)
  691. commas = 3;
  692. else if (defvarflag)
  693. commas = 1;
  694. else /* For DEFSIMPLE and DEFPRED */
  695. commas = 2;
  696. while (commas)
  697. {
  698. if (c == ',')
  699. {
  700. commas--;
  701. if (defunflag && (commas == 1 || commas == 2))
  702. {
  703. int scanned = 0;
  704. do
  705. c = getc (infile);
  706. while (c == ' ' || c == '\n' || c == '\r' || c == '\t');
  707. if (c < 0)
  708. goto eof;
  709. ungetc (c, infile);
  710. if (commas == 2) /* pick up minargs */
  711. scanned = fscanf (infile, "%d", &minargs);
  712. else /* pick up maxargs */
  713. if (c == 'M' || c == 'U') /* MANY || UNEVALLED */
  714. maxargs = -1;
  715. else
  716. scanned = fscanf (infile, "%d", &maxargs);
  717. if (scanned < 0)
  718. goto eof;
  719. }
  720. }
  721. if (c == EOF)
  722. goto eof;
  723. c = getc (infile);
  724. }
  725. while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
  726. c = getc (infile);
  727. if (c == '"')
  728. c = read_c_string_or_comment (infile, 0, 0, 0);
  729. while (c != EOF && c != ',' && c != '/')
  730. c = getc (infile);
  731. if (c == ',')
  732. {
  733. c = getc (infile);
  734. while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
  735. c = getc (infile);
  736. while ((c >= 'a' && c <= 'z') || (c >= 'Z' && c <= 'Z'))
  737. c = getc (infile);
  738. if (c == ':')
  739. {
  740. doc_keyword = 1;
  741. c = getc (infile);
  742. while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
  743. c = getc (infile);
  744. }
  745. }
  746. if (c == '"'
  747. || (c == '/'
  748. && (c = getc (infile),
  749. ungetc (c, infile),
  750. c == '*')))
  751. {
  752. int comment = c != '"';
  753. int saw_usage;
  754. putc (037, outfile);
  755. putc (defvarflag ? 'V' : 'F', outfile);
  756. fprintf (outfile, "%s\n", input_buffer);
  757. if (comment)
  758. getc (infile); /* Skip past `*' */
  759. c = read_c_string_or_comment (infile, 1, comment, &saw_usage);
  760. /* If this is a defun, find the arguments and print them. If
  761. this function takes MANY or UNEVALLED args, then the C source
  762. won't give the names of the arguments, so we shouldn't bother
  763. trying to find them.
  764. Various doc-string styles:
  765. 0: DEFUN (..., "DOC") (args) [!comment]
  766. 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
  767. 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
  768. */
  769. if (defunflag && maxargs != -1 && !saw_usage)
  770. {
  771. char argbuf[1024], *p = argbuf;
  772. if (!comment || doc_keyword)
  773. while (c != ')')
  774. {
  775. if (c < 0)
  776. goto eof;
  777. c = getc (infile);
  778. }
  779. /* Skip into arguments. */
  780. while (c != '(')
  781. {
  782. if (c < 0)
  783. goto eof;
  784. c = getc (infile);
  785. }
  786. /* Copy arguments into ARGBUF. */
  787. *p++ = c;
  788. do
  789. *p++ = c = getc (infile);
  790. while (c != ')');
  791. *p = '\0';
  792. /* Output them. */
  793. fprintf (outfile, "\n\n");
  794. write_c_args (outfile, input_buffer, argbuf, minargs, maxargs);
  795. }
  796. else if (defunflag && maxargs == -1 && !saw_usage)
  797. /* The DOC should provide the usage form. */
  798. fprintf (stderr, "Missing `usage' for function `%s'.\n",
  799. input_buffer);
  800. }
  801. }
  802. eof:
  803. fclose (infile);
  804. return 0;
  805. }
  806. /* Read a file of Lisp code, compiled or interpreted.
  807. Looks for
  808. (defun NAME ARGS DOCSTRING ...)
  809. (defmacro NAME ARGS DOCSTRING ...)
  810. (defsubst NAME ARGS DOCSTRING ...)
  811. (autoload (quote NAME) FILE DOCSTRING ...)
  812. (defvar NAME VALUE DOCSTRING)
  813. (defconst NAME VALUE DOCSTRING)
  814. (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
  815. (fset (quote NAME) #[... DOCSTRING ...])
  816. (defalias (quote NAME) #[... DOCSTRING ...])
  817. (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
  818. starting in column zero.
  819. (quote NAME) may appear as 'NAME as well.
  820. We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
  821. When we find that, we save it for the following defining-form,
  822. and we use that instead of reading a doc string within that defining-form.
  823. For defvar, defconst, and fset we skip to the docstring with a kludgy
  824. formatting convention: all docstrings must appear on the same line as the
  825. initial open-paren (the one in column zero) and must contain a backslash
  826. and a newline immediately after the initial double-quote. No newlines
  827. must appear between the beginning of the form and the first double-quote.
  828. For defun, defmacro, and autoload, we know how to skip over the
  829. arglist, but the doc string must still have a backslash and newline
  830. immediately after the double quote.
  831. The only source files that must follow this convention are preloaded
  832. uncompiled ones like loaddefs.el and bindings.el; aside
  833. from that, it is always the .elc file that we look at, and they are no
  834. problem because byte-compiler output follows this convention.
  835. The NAME and DOCSTRING are output.
  836. NAME is preceded by `F' for a function or `V' for a variable.
  837. An entry is output only if DOCSTRING has \ newline just after the opening "
  838. */
  839. static void
  840. skip_white (FILE *infile)
  841. {
  842. char c = ' ';
  843. while (c == ' ' || c == '\t' || c == '\n' || c == '\r')
  844. c = getc (infile);
  845. ungetc (c, infile);
  846. }
  847. static void
  848. read_lisp_symbol (FILE *infile, char *buffer)
  849. {
  850. char c;
  851. char *fillp = buffer;
  852. skip_white (infile);
  853. while (1)
  854. {
  855. c = getc (infile);
  856. if (c == '\\')
  857. *(++fillp) = getc (infile);
  858. else if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '(' || c == ')')
  859. {
  860. ungetc (c, infile);
  861. *fillp = 0;
  862. break;
  863. }
  864. else
  865. *fillp++ = c;
  866. }
  867. if (! buffer[0])
  868. fprintf (stderr, "## expected a symbol, got '%c'\n", c);
  869. skip_white (infile);
  870. }
  871. static int
  872. scan_lisp_file (const char *filename, const char *mode)
  873. {
  874. FILE *infile;
  875. register int c;
  876. char *saved_string = 0;
  877. if (generate_globals)
  878. fatal ("scanning lisp file when -g specified", 0);
  879. infile = fopen (filename, mode);
  880. if (infile == NULL)
  881. {
  882. perror (filename);
  883. return 0; /* No error */
  884. }
  885. c = '\n';
  886. while (!feof (infile))
  887. {
  888. char buffer[BUFSIZ];
  889. char type;
  890. /* If not at end of line, skip till we get to one. */
  891. if (c != '\n' && c != '\r')
  892. {
  893. c = getc (infile);
  894. continue;
  895. }
  896. /* Skip the line break. */
  897. while (c == '\n' || c == '\r')
  898. c = getc (infile);
  899. /* Detect a dynamic doc string and save it for the next expression. */
  900. if (c == '#')
  901. {
  902. c = getc (infile);
  903. if (c == '@')
  904. {
  905. size_t length = 0;
  906. size_t i;
  907. /* Read the length. */
  908. while ((c = getc (infile),
  909. c >= '0' && c <= '9'))
  910. {
  911. length *= 10;
  912. length += c - '0';
  913. }
  914. if (length <= 1)
  915. fatal ("invalid dynamic doc string length", "");
  916. if (c != ' ')
  917. fatal ("space not found after dynamic doc string length", "");
  918. /* The next character is a space that is counted in the length
  919. but not part of the doc string.
  920. We already read it, so just ignore it. */
  921. length--;
  922. /* Read in the contents. */
  923. free (saved_string);
  924. saved_string = (char *) xmalloc (length);
  925. for (i = 0; i < length; i++)
  926. saved_string[i] = getc (infile);
  927. /* The last character is a ^_.
  928. That is needed in the .elc file
  929. but it is redundant in DOC. So get rid of it here. */
  930. saved_string[length - 1] = 0;
  931. /* Skip the line break. */
  932. while (c == '\n' || c == '\r')
  933. c = getc (infile);
  934. /* Skip the following line. */
  935. while (c != '\n' && c != '\r')
  936. c = getc (infile);
  937. }
  938. continue;
  939. }
  940. if (c != '(')
  941. continue;
  942. read_lisp_symbol (infile, buffer);
  943. if (! strcmp (buffer, "defun")
  944. || ! strcmp (buffer, "defmacro")
  945. || ! strcmp (buffer, "defsubst"))
  946. {
  947. type = 'F';
  948. read_lisp_symbol (infile, buffer);
  949. /* Skip the arguments: either "nil" or a list in parens */
  950. c = getc (infile);
  951. if (c == 'n') /* nil */
  952. {
  953. if ((c = getc (infile)) != 'i'
  954. || (c = getc (infile)) != 'l')
  955. {
  956. fprintf (stderr, "## unparsable arglist in %s (%s)\n",
  957. buffer, filename);
  958. continue;
  959. }
  960. }
  961. else if (c != '(')
  962. {
  963. fprintf (stderr, "## unparsable arglist in %s (%s)\n",
  964. buffer, filename);
  965. continue;
  966. }
  967. else
  968. while (c != ')')
  969. c = getc (infile);
  970. skip_white (infile);
  971. /* If the next three characters aren't `dquote bslash newline'
  972. then we're not reading a docstring.
  973. */
  974. if ((c = getc (infile)) != '"'
  975. || (c = getc (infile)) != '\\'
  976. || ((c = getc (infile)) != '\n' && c != '\r'))
  977. {
  978. #ifdef DEBUG
  979. fprintf (stderr, "## non-docstring in %s (%s)\n",
  980. buffer, filename);
  981. #endif
  982. continue;
  983. }
  984. }
  985. /* defcustom can only occur in uncompiled Lisp files. */
  986. else if (! strcmp (buffer, "defvar")
  987. || ! strcmp (buffer, "defconst")
  988. || ! strcmp (buffer, "defcustom"))
  989. {
  990. char c1 = 0, c2 = 0;
  991. type = 'V';
  992. read_lisp_symbol (infile, buffer);
  993. if (saved_string == 0)
  994. {
  995. /* Skip until the end of line; remember two previous chars. */
  996. while (c != '\n' && c != '\r' && c >= 0)
  997. {
  998. c2 = c1;
  999. c1 = c;
  1000. c = getc (infile);
  1001. }
  1002. /* If two previous characters were " and \,
  1003. this is a doc string. Otherwise, there is none. */
  1004. if (c2 != '"' || c1 != '\\')
  1005. {
  1006. #ifdef DEBUG
  1007. fprintf (stderr, "## non-docstring in %s (%s)\n",
  1008. buffer, filename);
  1009. #endif
  1010. continue;
  1011. }
  1012. }
  1013. }
  1014. else if (! strcmp (buffer, "custom-declare-variable")
  1015. || ! strcmp (buffer, "defvaralias")
  1016. )
  1017. {
  1018. char c1 = 0, c2 = 0;
  1019. type = 'V';
  1020. c = getc (infile);
  1021. if (c == '\'')
  1022. read_lisp_symbol (infile, buffer);
  1023. else
  1024. {
  1025. if (c != '(')
  1026. {
  1027. fprintf (stderr,
  1028. "## unparsable name in custom-declare-variable in %s\n",
  1029. filename);
  1030. continue;
  1031. }
  1032. read_lisp_symbol (infile, buffer);
  1033. if (strcmp (buffer, "quote"))
  1034. {
  1035. fprintf (stderr,
  1036. "## unparsable name in custom-declare-variable in %s\n",
  1037. filename);
  1038. continue;
  1039. }
  1040. read_lisp_symbol (infile, buffer);
  1041. c = getc (infile);
  1042. if (c != ')')
  1043. {
  1044. fprintf (stderr,
  1045. "## unparsable quoted name in custom-declare-variable in %s\n",
  1046. filename);
  1047. continue;
  1048. }
  1049. }
  1050. if (saved_string == 0)
  1051. {
  1052. /* Skip to end of line; remember the two previous chars. */
  1053. while (c != '\n' && c != '\r' && c >= 0)
  1054. {
  1055. c2 = c1;
  1056. c1 = c;
  1057. c = getc (infile);
  1058. }
  1059. /* If two previous characters were " and \,
  1060. this is a doc string. Otherwise, there is none. */
  1061. if (c2 != '"' || c1 != '\\')
  1062. {
  1063. #ifdef DEBUG
  1064. fprintf (stderr, "## non-docstring in %s (%s)\n",
  1065. buffer, filename);
  1066. #endif
  1067. continue;
  1068. }
  1069. }
  1070. }
  1071. else if (! strcmp (buffer, "fset") || ! strcmp (buffer, "defalias"))
  1072. {
  1073. char c1 = 0, c2 = 0;
  1074. type = 'F';
  1075. c = getc (infile);
  1076. if (c == '\'')
  1077. read_lisp_symbol (infile, buffer);
  1078. else
  1079. {
  1080. if (c != '(')
  1081. {
  1082. fprintf (stderr, "## unparsable name in fset in %s\n",
  1083. filename);
  1084. continue;
  1085. }
  1086. read_lisp_symbol (infile, buffer);
  1087. if (strcmp (buffer, "quote"))
  1088. {
  1089. fprintf (stderr, "## unparsable name in fset in %s\n",
  1090. filename);
  1091. continue;
  1092. }
  1093. read_lisp_symbol (infile, buffer);
  1094. c = getc (infile);
  1095. if (c != ')')
  1096. {
  1097. fprintf (stderr,
  1098. "## unparsable quoted name in fset in %s\n",
  1099. filename);
  1100. continue;
  1101. }
  1102. }
  1103. if (saved_string == 0)
  1104. {
  1105. /* Skip to end of line; remember the two previous chars. */
  1106. while (c != '\n' && c != '\r' && c >= 0)
  1107. {
  1108. c2 = c1;
  1109. c1 = c;
  1110. c = getc (infile);
  1111. }
  1112. /* If two previous characters were " and \,
  1113. this is a doc string. Otherwise, there is none. */
  1114. if (c2 != '"' || c1 != '\\')
  1115. {
  1116. #ifdef DEBUG
  1117. fprintf (stderr, "## non-docstring in %s (%s)\n",
  1118. buffer, filename);
  1119. #endif
  1120. continue;
  1121. }
  1122. }
  1123. }
  1124. else if (! strcmp (buffer, "autoload"))
  1125. {
  1126. type = 'F';
  1127. c = getc (infile);
  1128. if (c == '\'')
  1129. read_lisp_symbol (infile, buffer);
  1130. else
  1131. {
  1132. if (c != '(')
  1133. {
  1134. fprintf (stderr, "## unparsable name in autoload in %s\n",
  1135. filename);
  1136. continue;
  1137. }
  1138. read_lisp_symbol (infile, buffer);
  1139. if (strcmp (buffer, "quote"))
  1140. {
  1141. fprintf (stderr, "## unparsable name in autoload in %s\n",
  1142. filename);
  1143. continue;
  1144. }
  1145. read_lisp_symbol (infile, buffer);
  1146. c = getc (infile);
  1147. if (c != ')')
  1148. {
  1149. fprintf (stderr,
  1150. "## unparsable quoted name in autoload in %s\n",
  1151. filename);
  1152. continue;
  1153. }
  1154. }
  1155. skip_white (infile);
  1156. if ((c = getc (infile)) != '\"')
  1157. {
  1158. fprintf (stderr, "## autoload of %s unparsable (%s)\n",
  1159. buffer, filename);
  1160. continue;
  1161. }
  1162. read_c_string_or_comment (infile, 0, 0, 0);
  1163. skip_white (infile);
  1164. if (saved_string == 0)
  1165. {
  1166. /* If the next three characters aren't `dquote bslash newline'
  1167. then we're not reading a docstring. */
  1168. if ((c = getc (infile)) != '"'
  1169. || (c = getc (infile)) != '\\'
  1170. || ((c = getc (infile)) != '\n' && c != '\r'))
  1171. {
  1172. #ifdef DEBUG
  1173. fprintf (stderr, "## non-docstring in %s (%s)\n",
  1174. buffer, filename);
  1175. #endif
  1176. continue;
  1177. }
  1178. }
  1179. }
  1180. #ifdef DEBUG
  1181. else if (! strcmp (buffer, "if")
  1182. || ! strcmp (buffer, "byte-code"))
  1183. ;
  1184. #endif
  1185. else
  1186. {
  1187. #ifdef DEBUG
  1188. fprintf (stderr, "## unrecognized top-level form, %s (%s)\n",
  1189. buffer, filename);
  1190. #endif
  1191. continue;
  1192. }
  1193. /* At this point, we should either use the previous
  1194. dynamic doc string in saved_string
  1195. or gobble a doc string from the input file.
  1196. In the latter case, the opening quote (and leading
  1197. backslash-newline) have already been read. */
  1198. putc (037, outfile);
  1199. putc (type, outfile);
  1200. fprintf (outfile, "%s\n", buffer);
  1201. if (saved_string)
  1202. {
  1203. fputs (saved_string, outfile);
  1204. /* Don't use one dynamic doc string twice. */
  1205. free (saved_string);
  1206. saved_string = 0;
  1207. }
  1208. else
  1209. read_c_string_or_comment (infile, 1, 0, 0);
  1210. }
  1211. fclose (infile);
  1212. return 0;
  1213. }
  1214. /* make-docfile.c ends here */