make-docfile.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. /* Generate doc-string file for GNU Emacs from source files.
  2. Copyright (C) 1985-1986, 1992-1994, 1997, 1999-2016 Free Software
  3. 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 (at
  8. 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. #include <stdarg.h>
  30. #include <stdbool.h>
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <stdlib.h> /* config.h unconditionally includes this anyway */
  35. #ifdef WINDOWSNT
  36. /* Defined to be sys_fopen in ms-w32.h, but only #ifdef emacs, so this
  37. is really just insurance. */
  38. #undef fopen
  39. #include <direct.h>
  40. #endif /* WINDOWSNT */
  41. #include <binary-io.h>
  42. #include <intprops.h>
  43. #include <min-max.h>
  44. #ifdef DOS_NT
  45. /* Defined to be sys_chdir in ms-w32.h, but only #ifdef emacs, so this
  46. is really just insurance.
  47. Similarly, msdos defines this as sys_chdir, but we're not linking with the
  48. file where that function is defined. */
  49. #undef chdir
  50. #define IS_SLASH(c) ((c) == '/' || (c) == '\\' || (c) == ':')
  51. #else /* not DOS_NT */
  52. #define IS_SLASH(c) ((c) == '/')
  53. #endif /* not DOS_NT */
  54. static void scan_file (char *filename);
  55. static void scan_lisp_file (const char *filename, const char *mode);
  56. static void scan_c_file (char *filename, const char *mode);
  57. static void scan_c_stream (FILE *infile);
  58. static void start_globals (void);
  59. static void write_globals (void);
  60. #include <unistd.h>
  61. /* Name this program was invoked with. */
  62. static char *progname;
  63. /* True if this invocation is generating globals.h. */
  64. static bool generate_globals;
  65. /* Print error message. Args are like vprintf. */
  66. static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
  67. verror (char const *m, va_list ap)
  68. {
  69. fprintf (stderr, "%s: ", progname);
  70. vfprintf (stderr, m, ap);
  71. fprintf (stderr, "\n");
  72. }
  73. /* Print error message. Args are like printf. */
  74. static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
  75. error (char const *m, ...)
  76. {
  77. va_list ap;
  78. va_start (ap, m);
  79. verror (m, ap);
  80. va_end (ap);
  81. }
  82. /* Print error message and exit. Args are like printf. */
  83. static _Noreturn void ATTRIBUTE_FORMAT_PRINTF (1, 2)
  84. fatal (char const *m, ...)
  85. {
  86. va_list ap;
  87. va_start (ap, m);
  88. verror (m, ap);
  89. va_end (ap);
  90. exit (EXIT_FAILURE);
  91. }
  92. static _Noreturn void
  93. memory_exhausted (void)
  94. {
  95. fatal ("virtual memory exhausted");
  96. }
  97. /* Like malloc but get fatal error if memory is exhausted. */
  98. static void *
  99. xmalloc (ptrdiff_t size)
  100. {
  101. void *result = malloc (size);
  102. if (result == NULL)
  103. memory_exhausted ();
  104. return result;
  105. }
  106. /* Like realloc but get fatal error if memory is exhausted. */
  107. static void *
  108. xrealloc (void *arg, ptrdiff_t size)
  109. {
  110. void *result = realloc (arg, size);
  111. if (result == NULL)
  112. memory_exhausted ();
  113. return result;
  114. }
  115. int
  116. main (int argc, char **argv)
  117. {
  118. int i;
  119. progname = argv[0];
  120. /* If first two args are -o FILE, output to FILE. */
  121. i = 1;
  122. if (argc > i + 1 && !strcmp (argv[i], "-o"))
  123. {
  124. if (! freopen (argv[i + 1], "w", stdout))
  125. {
  126. perror (argv[i + 1]);
  127. return EXIT_FAILURE;
  128. }
  129. i += 2;
  130. }
  131. if (argc > i + 1 && !strcmp (argv[i], "-a"))
  132. {
  133. if (! freopen (argv[i + 1], "a", stdout))
  134. {
  135. perror (argv[i + 1]);
  136. return EXIT_FAILURE;
  137. }
  138. i += 2;
  139. }
  140. if (argc > i + 1 && !strcmp (argv[i], "-d"))
  141. {
  142. if (chdir (argv[i + 1]) != 0)
  143. {
  144. perror (argv[i + 1]);
  145. return EXIT_FAILURE;
  146. }
  147. i += 2;
  148. }
  149. if (argc > i && !strcmp (argv[i], "-g"))
  150. {
  151. generate_globals = true;
  152. ++i;
  153. }
  154. set_binary_mode (fileno (stdout), O_BINARY);
  155. if (generate_globals)
  156. start_globals ();
  157. if (argc <= i)
  158. scan_c_stream (stdin);
  159. else
  160. {
  161. int first_infile = i;
  162. for (; i < argc; i++)
  163. {
  164. int j;
  165. /* Don't process one file twice. */
  166. for (j = first_infile; j < i; j++)
  167. if (strcmp (argv[i], argv[j]) == 0)
  168. break;
  169. if (j == i)
  170. scan_file (argv[i]);
  171. }
  172. }
  173. if (generate_globals)
  174. write_globals ();
  175. if (ferror (stdout) || fclose (stdout) != 0)
  176. fatal ("write error");
  177. return EXIT_SUCCESS;
  178. }
  179. /* Add a source file name boundary marker in the output file. */
  180. static void
  181. put_filename (char *filename)
  182. {
  183. char *tmp;
  184. for (tmp = filename; *tmp; tmp++)
  185. {
  186. if (IS_DIRECTORY_SEP (*tmp))
  187. filename = tmp + 1;
  188. }
  189. printf ("\037S%s\n", filename);
  190. }
  191. /* Read file FILENAME and output its doc strings to stdout.
  192. Return true if file is found, false otherwise. */
  193. static void
  194. scan_file (char *filename)
  195. {
  196. ptrdiff_t len = strlen (filename);
  197. if (!generate_globals)
  198. put_filename (filename);
  199. if (len > 4 && !strcmp (filename + len - 4, ".elc"))
  200. scan_lisp_file (filename, "rb");
  201. else if (len > 3 && !strcmp (filename + len - 3, ".el"))
  202. scan_lisp_file (filename, "r");
  203. else
  204. scan_c_file (filename, "r");
  205. }
  206. static void
  207. start_globals (void)
  208. {
  209. puts ("/* This file was auto-generated by make-docfile. */");
  210. puts ("/* DO NOT EDIT. */");
  211. puts ("struct emacs_globals {");
  212. }
  213. static char input_buffer[128];
  214. /* Some state during the execution of `read_c_string_or_comment'. */
  215. struct rcsoc_state
  216. {
  217. /* A count of spaces and newlines that have been read, but not output. */
  218. intmax_t pending_spaces, pending_newlines;
  219. /* Where we're reading from. */
  220. FILE *in_file;
  221. /* If non-zero, a buffer into which to copy characters. */
  222. char *buf_ptr;
  223. /* If non-zero, a file into which to copy characters. */
  224. FILE *out_file;
  225. /* A keyword we look for at the beginning of lines. If found, it is
  226. not copied, and SAW_KEYWORD is set to true. */
  227. const char *keyword;
  228. /* The current point we've reached in an occurrence of KEYWORD in
  229. the input stream. */
  230. const char *cur_keyword_ptr;
  231. /* Set to true if we saw an occurrence of KEYWORD. */
  232. bool saw_keyword;
  233. };
  234. /* Output CH to the file or buffer in STATE. Any pending newlines or
  235. spaces are output first. */
  236. static void
  237. put_char (char ch, struct rcsoc_state *state)
  238. {
  239. char out_ch;
  240. do
  241. {
  242. if (state->pending_newlines > 0)
  243. {
  244. state->pending_newlines--;
  245. out_ch = '\n';
  246. }
  247. else if (state->pending_spaces > 0)
  248. {
  249. state->pending_spaces--;
  250. out_ch = ' ';
  251. }
  252. else
  253. out_ch = ch;
  254. if (state->out_file)
  255. putc (out_ch, state->out_file);
  256. if (state->buf_ptr)
  257. *state->buf_ptr++ = out_ch;
  258. }
  259. while (out_ch != ch);
  260. }
  261. /* If in the middle of scanning a keyword, continue scanning with
  262. character CH, otherwise output CH to the file or buffer in STATE.
  263. Any pending newlines or spaces are output first, as well as any
  264. previously scanned characters that were thought to be part of a
  265. keyword, but were in fact not. */
  266. static void
  267. scan_keyword_or_put_char (char ch, struct rcsoc_state *state)
  268. {
  269. if (state->keyword
  270. && *state->cur_keyword_ptr == ch
  271. && (state->cur_keyword_ptr > state->keyword
  272. || state->pending_newlines > 0))
  273. /* We might be looking at STATE->keyword at some point.
  274. Keep looking until we know for sure. */
  275. {
  276. if (*++state->cur_keyword_ptr == '\0')
  277. /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
  278. {
  279. state->saw_keyword = true;
  280. /* Reset the scanning pointer. */
  281. state->cur_keyword_ptr = state->keyword;
  282. /* Canonicalize whitespace preceding a usage string. */
  283. state->pending_newlines = 2;
  284. state->pending_spaces = 0;
  285. /* Skip any whitespace between the keyword and the
  286. usage string. */
  287. int c;
  288. do
  289. c = getc (state->in_file);
  290. while (c == ' ' || c == '\n');
  291. /* Output the open-paren we just read. */
  292. if (c != '(')
  293. fatal ("Missing '(' after keyword");
  294. put_char (c, state);
  295. /* Skip the function name and replace it with `fn'. */
  296. do
  297. {
  298. c = getc (state->in_file);
  299. if (c == EOF)
  300. fatal ("Unexpected EOF after keyword");
  301. }
  302. while (c != ' ' && c != ')');
  303. put_char ('f', state);
  304. put_char ('n', state);
  305. /* Put back the last character. */
  306. ungetc (c, state->in_file);
  307. }
  308. }
  309. else
  310. {
  311. if (state->keyword && state->cur_keyword_ptr > state->keyword)
  312. /* We scanned the beginning of a potential usage
  313. keyword, but it was a false alarm. Output the
  314. part we scanned. */
  315. {
  316. const char *p;
  317. for (p = state->keyword; p < state->cur_keyword_ptr; p++)
  318. put_char (*p, state);
  319. state->cur_keyword_ptr = state->keyword;
  320. }
  321. put_char (ch, state);
  322. }
  323. }
  324. /* Skip a C string or C-style comment from INFILE, and return the
  325. byte that follows, or EOF. COMMENT means skip a comment. If
  326. PRINTFLAG is positive, output string contents to stdout. If it is
  327. negative, store contents in buf. Convert escape sequences \n and
  328. \t to newline and tab; discard \ followed by newline.
  329. If SAW_USAGE is non-null, then any occurrences of the string "usage:"
  330. at the beginning of a line will be removed, and *SAW_USAGE set to
  331. true if any were encountered. */
  332. static int
  333. read_c_string_or_comment (FILE *infile, int printflag, bool comment,
  334. bool *saw_usage)
  335. {
  336. int c;
  337. struct rcsoc_state state;
  338. state.in_file = infile;
  339. state.buf_ptr = (printflag < 0 ? input_buffer : 0);
  340. state.out_file = (printflag > 0 ? stdout : 0);
  341. state.pending_spaces = 0;
  342. state.pending_newlines = 0;
  343. state.keyword = (saw_usage ? "usage:" : 0);
  344. state.cur_keyword_ptr = state.keyword;
  345. state.saw_keyword = false;
  346. c = getc (infile);
  347. if (comment)
  348. while (c == '\n' || c == '\r' || c == '\t' || c == ' ')
  349. c = getc (infile);
  350. while (c != EOF)
  351. {
  352. while (c != EOF && (comment ? c != '*' : c != '"'))
  353. {
  354. if (c == '\\')
  355. {
  356. c = getc (infile);
  357. if (c == '\n' || c == '\r')
  358. {
  359. c = getc (infile);
  360. continue;
  361. }
  362. if (c == 'n')
  363. c = '\n';
  364. if (c == 't')
  365. c = '\t';
  366. }
  367. if (c == ' ')
  368. state.pending_spaces++;
  369. else if (c == '\n')
  370. {
  371. state.pending_newlines++;
  372. state.pending_spaces = 0;
  373. }
  374. else
  375. scan_keyword_or_put_char (c, &state);
  376. c = getc (infile);
  377. }
  378. if (c != EOF)
  379. c = getc (infile);
  380. if (comment)
  381. {
  382. if (c == '/')
  383. {
  384. c = getc (infile);
  385. break;
  386. }
  387. scan_keyword_or_put_char ('*', &state);
  388. }
  389. else
  390. {
  391. if (c != '"')
  392. break;
  393. /* If we had a "", concatenate the two strings. */
  394. c = getc (infile);
  395. }
  396. }
  397. if (printflag < 0)
  398. *state.buf_ptr = 0;
  399. if (saw_usage)
  400. *saw_usage = state.saw_keyword;
  401. return c;
  402. }
  403. /* Write to stdout the argument names of function FUNC, whose text is in BUF.
  404. MINARGS and MAXARGS are the minimum and maximum number of arguments. */
  405. static void
  406. write_c_args (char *func, char *buf, int minargs, int maxargs)
  407. {
  408. char *p;
  409. bool in_ident = false;
  410. char *ident_start IF_LINT (= NULL);
  411. ptrdiff_t ident_length = 0;
  412. fputs ("(fn", stdout);
  413. if (*buf == '(')
  414. ++buf;
  415. for (p = buf; *p; p++)
  416. {
  417. char c = *p;
  418. /* Notice when a new identifier starts. */
  419. if ((('A' <= c && c <= 'Z')
  420. || ('a' <= c && c <= 'z')
  421. || ('0' <= c && c <= '9')
  422. || c == '_')
  423. != in_ident)
  424. {
  425. if (!in_ident)
  426. {
  427. in_ident = true;
  428. ident_start = p;
  429. }
  430. else
  431. {
  432. in_ident = false;
  433. ident_length = p - ident_start;
  434. }
  435. }
  436. /* Found the end of an argument, write out the last seen
  437. identifier. */
  438. if (c == ',' || c == ')')
  439. {
  440. if (ident_length == 0)
  441. {
  442. error ("empty arg list for '%s' should be (void), not ()", func);
  443. continue;
  444. }
  445. if (strncmp (ident_start, "void", ident_length) == 0)
  446. continue;
  447. putchar (' ');
  448. if (minargs == 0 && maxargs > 0)
  449. fputs ("&optional ", stdout);
  450. minargs--;
  451. maxargs--;
  452. /* In C code, `default' is a reserved word, so we spell it
  453. `defalt'; demangle that here. */
  454. if (ident_length == 6 && memcmp (ident_start, "defalt", 6) == 0)
  455. fputs ("DEFAULT", stdout);
  456. else
  457. while (ident_length-- > 0)
  458. {
  459. c = *ident_start++;
  460. if (c >= 'a' && c <= 'z')
  461. /* Upcase the letter. */
  462. c += 'A' - 'a';
  463. else if (c == '_')
  464. /* Print underscore as hyphen. */
  465. c = '-';
  466. putchar (c);
  467. }
  468. }
  469. }
  470. putchar (')');
  471. }
  472. /* The types of globals. These are sorted roughly in decreasing alignment
  473. order to avoid allocation gaps, except that symbols and functions
  474. are last. */
  475. enum global_type
  476. {
  477. INVALID,
  478. LISP_OBJECT,
  479. EMACS_INTEGER,
  480. BOOLEAN,
  481. SYMBOL,
  482. FUNCTION
  483. };
  484. /* A single global. */
  485. struct global
  486. {
  487. enum global_type type;
  488. char *name;
  489. int flags;
  490. union
  491. {
  492. int value;
  493. char const *svalue;
  494. } v;
  495. };
  496. /* Bit values for FLAGS field from the above. Applied for DEFUNs only. */
  497. enum { DEFUN_noreturn = 1, DEFUN_const = 2 };
  498. /* All the variable names we saw while scanning C sources in `-g'
  499. mode. */
  500. static ptrdiff_t num_globals;
  501. static ptrdiff_t num_globals_allocated;
  502. static struct global *globals;
  503. static struct global *
  504. add_global (enum global_type type, char const *name, int value,
  505. char const *svalue)
  506. {
  507. /* Ignore the one non-symbol that can occur. */
  508. if (strcmp (name, "..."))
  509. {
  510. if (num_globals == num_globals_allocated)
  511. {
  512. ptrdiff_t num_globals_max = (min (PTRDIFF_MAX, SIZE_MAX)
  513. / sizeof *globals);
  514. if (num_globals_allocated == num_globals_max)
  515. memory_exhausted ();
  516. if (num_globals_allocated < num_globals_max / 2)
  517. num_globals_allocated = 2 * num_globals_allocated + 1;
  518. else
  519. num_globals_allocated = num_globals_max;
  520. globals = xrealloc (globals, num_globals_allocated * sizeof *globals);
  521. }
  522. ++num_globals;
  523. ptrdiff_t namesize = strlen (name) + 1;
  524. char *buf = xmalloc (namesize + (svalue ? strlen (svalue) + 1 : 0));
  525. globals[num_globals - 1].type = type;
  526. globals[num_globals - 1].name = strcpy (buf, name);
  527. if (svalue)
  528. globals[num_globals - 1].v.svalue = strcpy (buf + namesize, svalue);
  529. else
  530. globals[num_globals - 1].v.value = value;
  531. globals[num_globals - 1].flags = 0;
  532. return globals + num_globals - 1;
  533. }
  534. return NULL;
  535. }
  536. static int
  537. compare_globals (const void *a, const void *b)
  538. {
  539. const struct global *ga = a;
  540. const struct global *gb = b;
  541. if (ga->type != gb->type)
  542. return ga->type - gb->type;
  543. /* Consider "nil" to be the least, so that iQnil is zero. That
  544. way, Qnil's internal representation is zero, which is a bit faster. */
  545. if (ga->type == SYMBOL)
  546. {
  547. bool a_nil = strcmp (ga->name, "Qnil") == 0;
  548. bool b_nil = strcmp (gb->name, "Qnil") == 0;
  549. if (a_nil | b_nil)
  550. return b_nil - a_nil;
  551. }
  552. return strcmp (ga->name, gb->name);
  553. }
  554. static void
  555. close_emacs_globals (ptrdiff_t num_symbols)
  556. {
  557. printf (("};\n"
  558. "extern struct emacs_globals globals;\n"
  559. "\n"
  560. "#ifndef DEFINE_SYMBOLS\n"
  561. "extern\n"
  562. "#endif\n"
  563. "struct Lisp_Symbol alignas (GCALIGNMENT) lispsym[%td];\n"),
  564. num_symbols);
  565. }
  566. static void
  567. write_globals (void)
  568. {
  569. ptrdiff_t i, j;
  570. bool seen_defun = false;
  571. ptrdiff_t symnum = 0;
  572. ptrdiff_t num_symbols = 0;
  573. qsort (globals, num_globals, sizeof (struct global), compare_globals);
  574. j = 0;
  575. for (i = 0; i < num_globals; i++)
  576. {
  577. while (i + 1 < num_globals
  578. && strcmp (globals[i].name, globals[i + 1].name) == 0)
  579. {
  580. if (globals[i].type == FUNCTION
  581. && globals[i].v.value != globals[i + 1].v.value)
  582. error ("function '%s' defined twice with differing signatures",
  583. globals[i].name);
  584. free (globals[i].name);
  585. i++;
  586. }
  587. num_symbols += globals[i].type == SYMBOL;
  588. globals[j++] = globals[i];
  589. }
  590. num_globals = j;
  591. for (i = 0; i < num_globals; ++i)
  592. {
  593. char const *type = 0;
  594. switch (globals[i].type)
  595. {
  596. case EMACS_INTEGER:
  597. type = "EMACS_INT";
  598. break;
  599. case BOOLEAN:
  600. type = "bool";
  601. break;
  602. case LISP_OBJECT:
  603. type = "Lisp_Object";
  604. break;
  605. case SYMBOL:
  606. case FUNCTION:
  607. if (!seen_defun)
  608. {
  609. close_emacs_globals (num_symbols);
  610. putchar ('\n');
  611. seen_defun = true;
  612. }
  613. break;
  614. default:
  615. fatal ("not a recognized DEFVAR_");
  616. }
  617. if (type)
  618. {
  619. printf (" %s f_%s;\n", type, globals[i].name);
  620. printf ("#define %s globals.f_%s\n",
  621. globals[i].name, globals[i].name);
  622. }
  623. else if (globals[i].type == SYMBOL)
  624. printf (("#define i%s %td\n"
  625. "DEFINE_LISP_SYMBOL (%s)\n"),
  626. globals[i].name, symnum++, globals[i].name);
  627. else
  628. {
  629. if (globals[i].flags & DEFUN_noreturn)
  630. fputs ("_Noreturn ", stdout);
  631. printf ("EXFUN (%s, ", globals[i].name);
  632. if (globals[i].v.value == -1)
  633. fputs ("MANY", stdout);
  634. else if (globals[i].v.value == -2)
  635. fputs ("UNEVALLED", stdout);
  636. else
  637. printf ("%d", globals[i].v.value);
  638. putchar (')');
  639. if (globals[i].flags & DEFUN_const)
  640. fputs (" ATTRIBUTE_CONST", stdout);
  641. puts (";");
  642. }
  643. }
  644. if (!seen_defun)
  645. close_emacs_globals (num_symbols);
  646. puts ("#ifdef DEFINE_SYMBOLS");
  647. puts ("static char const *const defsym_name[] = {");
  648. for (ptrdiff_t i = 0; i < num_globals; i++)
  649. if (globals[i].type == SYMBOL)
  650. printf ("\t\"%s\",\n", globals[i].v.svalue);
  651. puts ("};");
  652. puts ("#endif");
  653. puts ("#define Qnil builtin_lisp_symbol (0)");
  654. puts ("#if DEFINE_NON_NIL_Q_SYMBOL_MACROS");
  655. num_symbols = 0;
  656. for (ptrdiff_t i = 0; i < num_globals; i++)
  657. if (globals[i].type == SYMBOL && num_symbols++ != 0)
  658. printf ("# define %s builtin_lisp_symbol (%td)\n",
  659. globals[i].name, num_symbols - 1);
  660. puts ("#endif");
  661. }
  662. /* Read through a c file. If a .o file is named,
  663. the corresponding .c or .m file is read instead.
  664. Looks for DEFUN constructs such as are defined in ../src/lisp.h.
  665. Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
  666. static void
  667. scan_c_file (char *filename, const char *mode)
  668. {
  669. FILE *infile;
  670. char extension = filename[strlen (filename) - 1];
  671. if (extension == 'o')
  672. filename[strlen (filename) - 1] = 'c';
  673. infile = fopen (filename, mode);
  674. if (infile == NULL && extension == 'o')
  675. {
  676. /* Try .m. */
  677. filename[strlen (filename) - 1] = 'm';
  678. infile = fopen (filename, mode);
  679. if (infile == NULL)
  680. filename[strlen (filename) - 1] = 'c'; /* Don't confuse people. */
  681. }
  682. if (infile == NULL)
  683. {
  684. perror (filename);
  685. exit (EXIT_FAILURE);
  686. }
  687. /* Reset extension to be able to detect duplicate files. */
  688. filename[strlen (filename) - 1] = extension;
  689. scan_c_stream (infile);
  690. }
  691. /* Return 1 if next input from INFILE is equal to P, -1 if EOF,
  692. 0 if input doesn't match. */
  693. static int
  694. stream_match (FILE *infile, const char *p)
  695. {
  696. for (; *p; p++)
  697. {
  698. int c = getc (infile);
  699. if (c == EOF)
  700. return -1;
  701. if (c != *p)
  702. return 0;
  703. }
  704. return 1;
  705. }
  706. static void
  707. scan_c_stream (FILE *infile)
  708. {
  709. int commas, minargs, maxargs;
  710. int c = '\n';
  711. while (!feof (infile))
  712. {
  713. bool doc_keyword = false;
  714. bool defunflag = false;
  715. bool defvarperbufferflag = false;
  716. bool defvarflag = false;
  717. enum global_type type = INVALID;
  718. static char *name;
  719. static ptrdiff_t name_size;
  720. if (c != '\n' && c != '\r')
  721. {
  722. c = getc (infile);
  723. continue;
  724. }
  725. c = getc (infile);
  726. if (c == ' ')
  727. {
  728. while (c == ' ')
  729. c = getc (infile);
  730. if (c != 'D')
  731. continue;
  732. c = getc (infile);
  733. if (c != 'E')
  734. continue;
  735. c = getc (infile);
  736. if (c != 'F')
  737. continue;
  738. c = getc (infile);
  739. if (c == 'S')
  740. {
  741. c = getc (infile);
  742. if (c != 'Y')
  743. continue;
  744. c = getc (infile);
  745. if (c != 'M')
  746. continue;
  747. c = getc (infile);
  748. if (c != ' ' && c != '\t' && c != '(')
  749. continue;
  750. type = SYMBOL;
  751. }
  752. else if (c == 'V')
  753. {
  754. c = getc (infile);
  755. if (c != 'A')
  756. continue;
  757. c = getc (infile);
  758. if (c != 'R')
  759. continue;
  760. c = getc (infile);
  761. if (c != '_')
  762. continue;
  763. defvarflag = true;
  764. c = getc (infile);
  765. defvarperbufferflag = (c == 'P');
  766. if (generate_globals)
  767. {
  768. if (c == 'I')
  769. type = EMACS_INTEGER;
  770. else if (c == 'L')
  771. type = LISP_OBJECT;
  772. else if (c == 'B')
  773. type = BOOLEAN;
  774. }
  775. c = getc (infile);
  776. /* We need to distinguish between DEFVAR_BOOL and
  777. DEFVAR_BUFFER_DEFAULTS. */
  778. if (generate_globals && type == BOOLEAN && c != 'O')
  779. type = INVALID;
  780. }
  781. else
  782. continue;
  783. }
  784. else if (c == 'D')
  785. {
  786. c = getc (infile);
  787. if (c != 'E')
  788. continue;
  789. c = getc (infile);
  790. if (c != 'F')
  791. continue;
  792. c = getc (infile);
  793. defunflag = c == 'U';
  794. }
  795. else continue;
  796. if (generate_globals
  797. && (!defvarflag || defvarperbufferflag || type == INVALID)
  798. && !defunflag && type != SYMBOL)
  799. continue;
  800. while (c != '(')
  801. {
  802. if (c < 0)
  803. goto eof;
  804. c = getc (infile);
  805. }
  806. if (type != SYMBOL)
  807. {
  808. /* Lisp variable or function name. */
  809. c = getc (infile);
  810. if (c != '"')
  811. continue;
  812. c = read_c_string_or_comment (infile, -1, false, 0);
  813. }
  814. if (generate_globals)
  815. {
  816. ptrdiff_t i = 0;
  817. char const *svalue = 0;
  818. /* Skip "," and whitespace. */
  819. do
  820. {
  821. c = getc (infile);
  822. }
  823. while (c == ',' || c == ' ' || c == '\t' || c == '\n' || c == '\r');
  824. /* Read in the identifier. */
  825. do
  826. {
  827. if (c < 0)
  828. goto eof;
  829. input_buffer[i++] = c;
  830. c = getc (infile);
  831. }
  832. while (! (c == ',' || c == ' ' || c == '\t'
  833. || c == '\n' || c == '\r'));
  834. input_buffer[i] = '\0';
  835. if (name_size <= i)
  836. {
  837. free (name);
  838. name_size = i + 1;
  839. ptrdiff_t doubled;
  840. if (! INT_MULTIPLY_WRAPV (name_size, 2, &doubled)
  841. && doubled <= SIZE_MAX)
  842. name_size = doubled;
  843. name = xmalloc (name_size);
  844. }
  845. memcpy (name, input_buffer, i + 1);
  846. if (type == SYMBOL)
  847. {
  848. do
  849. c = getc (infile);
  850. while (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  851. if (c != '"')
  852. continue;
  853. c = read_c_string_or_comment (infile, -1, false, 0);
  854. svalue = input_buffer;
  855. }
  856. if (!defunflag)
  857. {
  858. add_global (type, name, 0, svalue);
  859. continue;
  860. }
  861. }
  862. if (type == SYMBOL)
  863. continue;
  864. /* DEFVAR_LISP ("name", addr, "doc")
  865. DEFVAR_LISP ("name", addr /\* doc *\/)
  866. DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
  867. if (defunflag)
  868. commas = generate_globals ? 4 : 5;
  869. else if (defvarperbufferflag)
  870. commas = 3;
  871. else if (defvarflag)
  872. commas = 1;
  873. else /* For DEFSIMPLE and DEFPRED. */
  874. commas = 2;
  875. while (commas)
  876. {
  877. if (c == ',')
  878. {
  879. commas--;
  880. if (defunflag && (commas == 1 || commas == 2))
  881. {
  882. int scanned = 0;
  883. do
  884. c = getc (infile);
  885. while (c == ' ' || c == '\n' || c == '\r' || c == '\t');
  886. if (c < 0)
  887. goto eof;
  888. ungetc (c, infile);
  889. if (commas == 2) /* Pick up minargs. */
  890. scanned = fscanf (infile, "%d", &minargs);
  891. else /* Pick up maxargs. */
  892. if (c == 'M' || c == 'U') /* MANY || UNEVALLED */
  893. {
  894. if (generate_globals)
  895. maxargs = (c == 'M') ? -1 : -2;
  896. else
  897. maxargs = -1;
  898. }
  899. else
  900. scanned = fscanf (infile, "%d", &maxargs);
  901. if (scanned < 0)
  902. goto eof;
  903. }
  904. }
  905. if (c == EOF)
  906. goto eof;
  907. c = getc (infile);
  908. }
  909. if (generate_globals)
  910. {
  911. struct global *g = add_global (FUNCTION, name, maxargs, 0);
  912. if (!g)
  913. continue;
  914. /* The following code tries to recognize function attributes
  915. specified after the docstring, e.g.:
  916. DEFUN ("foo", Ffoo, Sfoo, X, Y, Z,
  917. doc: /\* doc *\/
  918. attributes: attribute1 attribute2 ...)
  919. (Lisp_Object arg...)
  920. Now only 'noreturn' and 'const' attributes are used. */
  921. /* Advance to the end of docstring. */
  922. c = getc (infile);
  923. if (c == EOF)
  924. goto eof;
  925. int d = getc (infile);
  926. if (d == EOF)
  927. goto eof;
  928. while (1)
  929. {
  930. if (c == '*' && d == '/')
  931. break;
  932. c = d, d = getc (infile);
  933. if (d == EOF)
  934. goto eof;
  935. }
  936. /* Skip spaces, if any. */
  937. do
  938. {
  939. c = getc (infile);
  940. if (c == EOF)
  941. goto eof;
  942. }
  943. while (c == ' ' || c == '\n' || c == '\r' || c == '\t');
  944. /* Check for 'attributes:' token. */
  945. if (c == 'a' && stream_match (infile, "ttributes:"))
  946. {
  947. char *p = input_buffer;
  948. /* Collect attributes up to ')'. */
  949. while (1)
  950. {
  951. c = getc (infile);
  952. if (c == EOF)
  953. goto eof;
  954. if (c == ')')
  955. break;
  956. if (p - input_buffer > sizeof (input_buffer))
  957. abort ();
  958. *p++ = c;
  959. }
  960. *p = 0;
  961. if (strstr (input_buffer, "noreturn"))
  962. g->flags |= DEFUN_noreturn;
  963. if (strstr (input_buffer, "const"))
  964. g->flags |= DEFUN_const;
  965. }
  966. continue;
  967. }
  968. while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
  969. c = getc (infile);
  970. if (c == '"')
  971. c = read_c_string_or_comment (infile, 0, false, 0);
  972. while (c != EOF && c != ',' && c != '/')
  973. c = getc (infile);
  974. if (c == ',')
  975. {
  976. c = getc (infile);
  977. while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
  978. c = getc (infile);
  979. while ((c >= 'a' && c <= 'z') || (c >= 'Z' && c <= 'Z'))
  980. c = getc (infile);
  981. if (c == ':')
  982. {
  983. doc_keyword = true;
  984. c = getc (infile);
  985. while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
  986. c = getc (infile);
  987. }
  988. }
  989. if (c == '"'
  990. || (c == '/'
  991. && (c = getc (infile),
  992. ungetc (c, infile),
  993. c == '*')))
  994. {
  995. bool comment = c != '"';
  996. bool saw_usage;
  997. printf ("\037%c%s\n", defvarflag ? 'V' : 'F', input_buffer);
  998. if (comment)
  999. getc (infile); /* Skip past `*'. */
  1000. c = read_c_string_or_comment (infile, 1, comment, &saw_usage);
  1001. /* If this is a defun, find the arguments and print them. If
  1002. this function takes MANY or UNEVALLED args, then the C source
  1003. won't give the names of the arguments, so we shouldn't bother
  1004. trying to find them.
  1005. Various doc-string styles:
  1006. 0: DEFUN (..., "DOC") (args) [!comment]
  1007. 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
  1008. 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
  1009. */
  1010. if (defunflag && maxargs != -1 && !saw_usage)
  1011. {
  1012. char argbuf[1024], *p = argbuf;
  1013. if (!comment || doc_keyword)
  1014. while (c != ')')
  1015. {
  1016. if (c < 0)
  1017. goto eof;
  1018. c = getc (infile);
  1019. }
  1020. /* Skip into arguments. */
  1021. while (c != '(')
  1022. {
  1023. if (c < 0)
  1024. goto eof;
  1025. c = getc (infile);
  1026. }
  1027. /* Copy arguments into ARGBUF. */
  1028. *p++ = c;
  1029. do
  1030. *p++ = c = getc (infile);
  1031. while (c != ')');
  1032. *p = '\0';
  1033. /* Output them. */
  1034. fputs ("\n\n", stdout);
  1035. write_c_args (input_buffer, argbuf, minargs, maxargs);
  1036. }
  1037. else if (defunflag && maxargs == -1 && !saw_usage)
  1038. /* The DOC should provide the usage form. */
  1039. fprintf (stderr, "Missing 'usage' for function '%s'.\n",
  1040. input_buffer);
  1041. }
  1042. }
  1043. eof:
  1044. if (ferror (infile) || fclose (infile) != 0)
  1045. fatal ("read error");
  1046. }
  1047. /* Read a file of Lisp code, compiled or interpreted.
  1048. Looks for
  1049. (defun NAME ARGS DOCSTRING ...)
  1050. (defmacro NAME ARGS DOCSTRING ...)
  1051. (defsubst NAME ARGS DOCSTRING ...)
  1052. (autoload (quote NAME) FILE DOCSTRING ...)
  1053. (defvar NAME VALUE DOCSTRING)
  1054. (defconst NAME VALUE DOCSTRING)
  1055. (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
  1056. (fset (quote NAME) #[... DOCSTRING ...])
  1057. (defalias (quote NAME) #[... DOCSTRING ...])
  1058. (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
  1059. starting in column zero.
  1060. (quote NAME) may appear as 'NAME as well.
  1061. We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
  1062. When we find that, we save it for the following defining-form,
  1063. and we use that instead of reading a doc string within that defining-form.
  1064. For defvar, defconst, and fset we skip to the docstring with a kludgy
  1065. formatting convention: all docstrings must appear on the same line as the
  1066. initial open-paren (the one in column zero) and must contain a backslash
  1067. and a newline immediately after the initial double-quote. No newlines
  1068. must appear between the beginning of the form and the first double-quote.
  1069. For defun, defmacro, and autoload, we know how to skip over the
  1070. arglist, but the doc string must still have a backslash and newline
  1071. immediately after the double quote.
  1072. The only source files that must follow this convention are preloaded
  1073. uncompiled ones like loaddefs.el; aside from that, it is always the .elc
  1074. file that we should look at, and they are no problem because byte-compiler
  1075. output follows this convention.
  1076. The NAME and DOCSTRING are output.
  1077. NAME is preceded by `F' for a function or `V' for a variable.
  1078. An entry is output only if DOCSTRING has \ newline just after the opening ".
  1079. */
  1080. static void
  1081. skip_white (FILE *infile)
  1082. {
  1083. char c = ' ';
  1084. while (c == ' ' || c == '\t' || c == '\n' || c == '\r')
  1085. c = getc (infile);
  1086. ungetc (c, infile);
  1087. }
  1088. static void
  1089. read_lisp_symbol (FILE *infile, char *buffer)
  1090. {
  1091. char c;
  1092. char *fillp = buffer;
  1093. skip_white (infile);
  1094. while (1)
  1095. {
  1096. c = getc (infile);
  1097. if (c == '\\')
  1098. *(++fillp) = getc (infile);
  1099. else if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '(' || c == ')')
  1100. {
  1101. ungetc (c, infile);
  1102. *fillp = 0;
  1103. break;
  1104. }
  1105. else
  1106. *fillp++ = c;
  1107. }
  1108. if (! buffer[0])
  1109. fprintf (stderr, "## expected a symbol, got '%c'\n", c);
  1110. skip_white (infile);
  1111. }
  1112. static bool
  1113. search_lisp_doc_at_eol (FILE *infile)
  1114. {
  1115. int c = 0, c1 = 0, c2 = 0;
  1116. /* Skip until the end of line; remember two previous chars. */
  1117. while (c != '\n' && c != '\r' && c != EOF)
  1118. {
  1119. c2 = c1;
  1120. c1 = c;
  1121. c = getc (infile);
  1122. }
  1123. /* If two previous characters were " and \,
  1124. this is a doc string. Otherwise, there is none. */
  1125. if (c2 != '"' || c1 != '\\')
  1126. {
  1127. #ifdef DEBUG
  1128. fprintf (stderr, "## non-docstring found\n");
  1129. #endif
  1130. ungetc (c, infile);
  1131. return false;
  1132. }
  1133. return true;
  1134. }
  1135. #define DEF_ELISP_FILE(fn) { #fn, sizeof(#fn) - 1 }
  1136. static void
  1137. scan_lisp_file (const char *filename, const char *mode)
  1138. {
  1139. FILE *infile;
  1140. int c;
  1141. char *saved_string = 0;
  1142. /* These are the only files that are loaded uncompiled, and must
  1143. follow the conventions of the doc strings expected by this
  1144. function. These conventions are automatically followed by the
  1145. byte compiler when it produces the .elc files. */
  1146. static struct {
  1147. const char *fn;
  1148. int fl;
  1149. } const uncompiled[] = {
  1150. DEF_ELISP_FILE (loaddefs.el),
  1151. DEF_ELISP_FILE (loadup.el),
  1152. DEF_ELISP_FILE (charprop.el),
  1153. DEF_ELISP_FILE (cp51932.el),
  1154. DEF_ELISP_FILE (eucjp-ms.el)
  1155. };
  1156. int i;
  1157. int flen = strlen (filename);
  1158. if (generate_globals)
  1159. fatal ("scanning lisp file when -g specified");
  1160. if (flen > 3 && !strcmp (filename + flen - 3, ".el"))
  1161. {
  1162. bool match = false;
  1163. for (i = 0; i < sizeof (uncompiled) / sizeof (uncompiled[0]); i++)
  1164. {
  1165. if (uncompiled[i].fl <= flen
  1166. && !strcmp (filename + flen - uncompiled[i].fl, uncompiled[i].fn)
  1167. && (flen == uncompiled[i].fl
  1168. || IS_SLASH (filename[flen - uncompiled[i].fl - 1])))
  1169. {
  1170. match = true;
  1171. break;
  1172. }
  1173. }
  1174. if (!match)
  1175. fatal ("uncompiled lisp file %s is not supported", filename);
  1176. }
  1177. infile = fopen (filename, mode);
  1178. if (infile == NULL)
  1179. {
  1180. perror (filename);
  1181. exit (EXIT_FAILURE);
  1182. }
  1183. c = '\n';
  1184. while (!feof (infile))
  1185. {
  1186. char buffer[BUFSIZ];
  1187. char type;
  1188. /* If not at end of line, skip till we get to one. */
  1189. if (c != '\n' && c != '\r')
  1190. {
  1191. c = getc (infile);
  1192. continue;
  1193. }
  1194. /* Skip the line break. */
  1195. while (c == '\n' || c == '\r')
  1196. c = getc (infile);
  1197. /* Detect a dynamic doc string and save it for the next expression. */
  1198. if (c == '#')
  1199. {
  1200. c = getc (infile);
  1201. if (c == '@')
  1202. {
  1203. ptrdiff_t length = 0;
  1204. ptrdiff_t i;
  1205. /* Read the length. */
  1206. while ((c = getc (infile),
  1207. c >= '0' && c <= '9'))
  1208. {
  1209. if (INT_MULTIPLY_WRAPV (length, 10, &length)
  1210. || INT_ADD_WRAPV (length, c - '0', &length)
  1211. || SIZE_MAX < length)
  1212. memory_exhausted ();
  1213. }
  1214. if (length <= 1)
  1215. fatal ("invalid dynamic doc string length");
  1216. if (c != ' ')
  1217. fatal ("space not found after dynamic doc string length");
  1218. /* The next character is a space that is counted in the length
  1219. but not part of the doc string.
  1220. We already read it, so just ignore it. */
  1221. length--;
  1222. /* Read in the contents. */
  1223. free (saved_string);
  1224. saved_string = xmalloc (length);
  1225. for (i = 0; i < length; i++)
  1226. saved_string[i] = getc (infile);
  1227. /* The last character is a ^_.
  1228. That is needed in the .elc file
  1229. but it is redundant in DOC. So get rid of it here. */
  1230. saved_string[length - 1] = 0;
  1231. /* Skip the line break. */
  1232. while (c == '\n' || c == '\r')
  1233. c = getc (infile);
  1234. /* Skip the following line. */
  1235. while (c != '\n' && c != '\r')
  1236. c = getc (infile);
  1237. }
  1238. continue;
  1239. }
  1240. if (c != '(')
  1241. continue;
  1242. read_lisp_symbol (infile, buffer);
  1243. if (! strcmp (buffer, "defun")
  1244. || ! strcmp (buffer, "defmacro")
  1245. || ! strcmp (buffer, "defsubst"))
  1246. {
  1247. type = 'F';
  1248. read_lisp_symbol (infile, buffer);
  1249. /* Skip the arguments: either "nil" or a list in parens. */
  1250. c = getc (infile);
  1251. if (c == 'n') /* nil */
  1252. {
  1253. if ((c = getc (infile)) != 'i'
  1254. || (c = getc (infile)) != 'l')
  1255. {
  1256. fprintf (stderr, "## unparsable arglist in %s (%s)\n",
  1257. buffer, filename);
  1258. continue;
  1259. }
  1260. }
  1261. else if (c != '(')
  1262. {
  1263. fprintf (stderr, "## unparsable arglist in %s (%s)\n",
  1264. buffer, filename);
  1265. continue;
  1266. }
  1267. else
  1268. while (c != ')')
  1269. c = getc (infile);
  1270. skip_white (infile);
  1271. /* If the next three characters aren't `dquote bslash newline'
  1272. then we're not reading a docstring.
  1273. */
  1274. if ((c = getc (infile)) != '"'
  1275. || (c = getc (infile)) != '\\'
  1276. || ((c = getc (infile)) != '\n' && c != '\r'))
  1277. {
  1278. #ifdef DEBUG
  1279. fprintf (stderr, "## non-docstring in %s (%s)\n",
  1280. buffer, filename);
  1281. #endif
  1282. continue;
  1283. }
  1284. }
  1285. /* defcustom can only occur in uncompiled Lisp files. */
  1286. else if (! strcmp (buffer, "defvar")
  1287. || ! strcmp (buffer, "defconst")
  1288. || ! strcmp (buffer, "defcustom"))
  1289. {
  1290. type = 'V';
  1291. read_lisp_symbol (infile, buffer);
  1292. if (saved_string == 0)
  1293. if (!search_lisp_doc_at_eol (infile))
  1294. continue;
  1295. }
  1296. else if (! strcmp (buffer, "custom-declare-variable")
  1297. || ! strcmp (buffer, "defvaralias")
  1298. )
  1299. {
  1300. type = 'V';
  1301. c = getc (infile);
  1302. if (c == '\'')
  1303. read_lisp_symbol (infile, buffer);
  1304. else
  1305. {
  1306. if (c != '(')
  1307. {
  1308. fprintf (stderr,
  1309. "## unparsable name in custom-declare-variable in %s\n",
  1310. filename);
  1311. continue;
  1312. }
  1313. read_lisp_symbol (infile, buffer);
  1314. if (strcmp (buffer, "quote"))
  1315. {
  1316. fprintf (stderr,
  1317. "## unparsable name in custom-declare-variable in %s\n",
  1318. filename);
  1319. continue;
  1320. }
  1321. read_lisp_symbol (infile, buffer);
  1322. c = getc (infile);
  1323. if (c != ')')
  1324. {
  1325. fprintf (stderr,
  1326. "## unparsable quoted name in custom-declare-variable in %s\n",
  1327. filename);
  1328. continue;
  1329. }
  1330. }
  1331. if (saved_string == 0)
  1332. if (!search_lisp_doc_at_eol (infile))
  1333. continue;
  1334. }
  1335. else if (! strcmp (buffer, "fset") || ! strcmp (buffer, "defalias"))
  1336. {
  1337. type = 'F';
  1338. c = getc (infile);
  1339. if (c == '\'')
  1340. read_lisp_symbol (infile, buffer);
  1341. else
  1342. {
  1343. if (c != '(')
  1344. {
  1345. fprintf (stderr, "## unparsable name in fset in %s\n",
  1346. filename);
  1347. continue;
  1348. }
  1349. read_lisp_symbol (infile, buffer);
  1350. if (strcmp (buffer, "quote"))
  1351. {
  1352. fprintf (stderr, "## unparsable name in fset in %s\n",
  1353. filename);
  1354. continue;
  1355. }
  1356. read_lisp_symbol (infile, buffer);
  1357. c = getc (infile);
  1358. if (c != ')')
  1359. {
  1360. fprintf (stderr,
  1361. "## unparsable quoted name in fset in %s\n",
  1362. filename);
  1363. continue;
  1364. }
  1365. }
  1366. if (saved_string == 0)
  1367. if (!search_lisp_doc_at_eol (infile))
  1368. continue;
  1369. }
  1370. else if (! strcmp (buffer, "autoload"))
  1371. {
  1372. type = 'F';
  1373. c = getc (infile);
  1374. if (c == '\'')
  1375. read_lisp_symbol (infile, buffer);
  1376. else
  1377. {
  1378. if (c != '(')
  1379. {
  1380. fprintf (stderr, "## unparsable name in autoload in %s\n",
  1381. filename);
  1382. continue;
  1383. }
  1384. read_lisp_symbol (infile, buffer);
  1385. if (strcmp (buffer, "quote"))
  1386. {
  1387. fprintf (stderr, "## unparsable name in autoload in %s\n",
  1388. filename);
  1389. continue;
  1390. }
  1391. read_lisp_symbol (infile, buffer);
  1392. c = getc (infile);
  1393. if (c != ')')
  1394. {
  1395. fprintf (stderr,
  1396. "## unparsable quoted name in autoload in %s\n",
  1397. filename);
  1398. continue;
  1399. }
  1400. }
  1401. skip_white (infile);
  1402. if ((c = getc (infile)) != '\"')
  1403. {
  1404. fprintf (stderr, "## autoload of %s unparsable (%s)\n",
  1405. buffer, filename);
  1406. continue;
  1407. }
  1408. read_c_string_or_comment (infile, 0, false, 0);
  1409. if (saved_string == 0)
  1410. if (!search_lisp_doc_at_eol (infile))
  1411. continue;
  1412. }
  1413. #ifdef DEBUG
  1414. else if (! strcmp (buffer, "if")
  1415. || ! strcmp (buffer, "byte-code"))
  1416. continue;
  1417. #endif
  1418. else
  1419. {
  1420. #ifdef DEBUG
  1421. fprintf (stderr, "## unrecognized top-level form, %s (%s)\n",
  1422. buffer, filename);
  1423. #endif
  1424. continue;
  1425. }
  1426. /* At this point, we should either use the previous dynamic doc string in
  1427. saved_string or gobble a doc string from the input file.
  1428. In the latter case, the opening quote (and leading backslash-newline)
  1429. have already been read. */
  1430. printf ("\037%c%s\n", type, buffer);
  1431. if (saved_string)
  1432. {
  1433. fputs (saved_string, stdout);
  1434. /* Don't use one dynamic doc string twice. */
  1435. free (saved_string);
  1436. saved_string = 0;
  1437. }
  1438. else
  1439. read_c_string_or_comment (infile, 1, false, 0);
  1440. }
  1441. free (saved_string);
  1442. if (ferror (infile) || fclose (infile) != 0)
  1443. fatal ("%s: read error", filename);
  1444. }
  1445. /* make-docfile.c ends here */