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