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