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