1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* The arguments given to this program are all the C and Lisp source files
21 of GNU Emacs. .elc and .el and .c files are allowed.
22 A .o file can also be specified; the .c file it was made from is used.
23 This helps the makefile pass the correct list of files.
25 The results, which go to standard output or to a file
26 specified with -a or -o (-a to append, -o to start from nothing),
27 are entries containing function or variable names and their documentation.
28 Each entry starts with a ^_ character.
29 Then comes F for a function or V for a variable.
30 Then comes the function or variable name, terminated with a newline.
31 Then comes the documentation for that function or variable.
40 #define READ_TEXT "rt"
41 #define READ_BINARY "rb"
44 #define READ_BINARY "r"
45 #endif /* not MSDOS */
57 _fmode
= O_BINARY
; /* all of files are treated as binary files */
58 (stdout
)->_flag
&= ~_IOTEXT
;
59 _setmode (fileno (stdout
), O_BINARY
);
63 /* If first two args are -o FILE, output to FILE. */
65 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
67 outfile
= fopen (argv
[i
+ 1], "w");
70 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
72 outfile
= fopen (argv
[i
+ 1], "a");
75 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
82 err_count
+= scan_file (argv
[i
]); /* err_count seems to be {mis,un}used */
84 exit (err_count
); /* see below - shane */
88 /* Read file FILENAME and output its doc strings to outfile. */
89 /* Return 1 if file is not found, 0 if it is found. */
94 int len
= strlen (filename
);
95 if (!strcmp (filename
+ len
- 4, ".elc"))
96 return scan_lisp_file (filename
, READ_BINARY
);
97 else if (!strcmp (filename
+ len
- 3, ".el"))
98 return scan_lisp_file (filename
, READ_TEXT
);
100 return scan_c_file (filename
, READ_TEXT
);
105 /* Skip a C string from INFILE,
106 and return the character that follows the closing ".
107 If printflag is positive, output string contents to outfile.
108 If it is negative, store contents in buf.
109 Convert escape sequences \n and \t to newline and tab;
110 discard \ followed by newline. */
112 read_c_string (infile
, printflag
)
122 while (c
!= '"' && c
!= EOF
)
139 else if (printflag
< 0)
146 /* If we had a "", concatenate the two strings. */
156 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
157 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
159 write_c_args (out
, func
, buf
, minargs
, maxargs
)
162 int minargs
, maxargs
;
169 fprintf (out
, "(%s", func
);
174 for (p
= buf
; *p
; p
++)
179 /* Notice when we start printing a new identifier. */
180 if ((('A' <= c
&& c
<= 'Z')
181 || ('a' <= c
&& c
<= 'z')
182 || ('0' <= c
&& c
<= '9')
194 if (minargs
== 0 && maxargs
> 0)
195 fprintf (out
, "&optional ");
205 /* Print the C argument list as it would appear in lisp:
206 print underscores as hyphens, and print commas as spaces.
207 Collapse adjacent spaces into one. */
208 if (c
== '_') c
= '-';
209 if (c
== ',') c
= ' ';
211 /* In C code, `default' is a reserved word, so we spell it
212 `defalt'; unmangle that here. */
214 && strncmp (p
, "defalt", 6) == 0
215 && ! (('A' <= p
[6] && p
[6] <= 'Z')
216 || ('a' <= p
[6] && p
[6] <= 'z')
217 || ('0' <= p
[6] && p
[6] <= '9')
220 fprintf (out
, "default");
225 else if (c
!= ' ' || ! just_spaced
)
227 if (c
>= 'a' && c
<= 'z')
228 /* Upcase the letter. */
233 just_spaced
= (c
== ' ');
238 /* Read through a c file. If a .o file is named,
239 the corresponding .c file is read instead.
240 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
241 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
243 scan_c_file (filename
, mode
)
244 char *filename
, *mode
;
249 register int defunflag
;
250 register int defvarperbufferflag
;
251 register int defvarflag
;
252 int minargs
, maxargs
;
254 if (filename
[strlen (filename
) - 1] == 'o')
255 filename
[strlen (filename
) - 1] = 'c';
257 infile
= fopen (filename
, mode
);
259 /* No error if non-ex input file */
267 while (!feof (infile
))
304 defvarperbufferflag
= (c
== 'P');
317 defunflag
= c
== 'U';
332 c
= read_c_string (infile
, -1);
336 else if (defvarperbufferflag
)
340 else /* For DEFSIMPLE and DEFPRED */
348 if (defunflag
&& (commas
== 1 || commas
== 2))
352 while (c
== ' ' || c
== '\n' || c
== '\t');
356 if (commas
== 2) /* pick up minargs */
357 fscanf (infile
, "%d", &minargs
);
358 else /* pick up maxargs */
359 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
362 fscanf (infile
, "%d", &maxargs
);
369 while (c
== ' ' || c
== '\n' || c
== '\t')
372 c
= read_c_string (infile
, 0);
376 while (c
== ' ' || c
== '\n' || c
== '\t')
382 putc (defvarflag
? 'V' : 'F', outfile
);
383 fprintf (outfile
, "%s\n", buf
);
384 c
= read_c_string (infile
, 1);
386 /* If this is a defun, find the arguments and print them. If
387 this function takes MANY or UNEVALLED args, then the C source
388 won't give the names of the arguments, so we shouldn't bother
389 trying to find them. */
390 if (defunflag
&& maxargs
!= -1)
392 char argbuf
[1024], *p
= argbuf
;
399 /* Skip into arguments. */
406 /* Copy arguments into ARGBUF. */
409 *p
++ = c
= getc (infile
);
413 fprintf (outfile
, "\n\n");
414 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
423 /* Read a file of Lisp code, compiled or interpreted.
425 (defun NAME ARGS DOCSTRING ...)
426 (defmacro NAME ARGS DOCSTRING ...)
427 (autoload (quote NAME) FILE DOCSTRING ...)
428 (defvar NAME VALUE DOCSTRING)
429 (defconst NAME VALUE DOCSTRING)
430 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
431 (fset (quote NAME) #[... DOCSTRING ...])
432 (defalias (quote NAME) #[... DOCSTRING ...])
433 starting in column zero.
434 (quote NAME) may appear as 'NAME as well.
435 For defun, defmacro, and autoload, we know how to skip over the arglist.
436 For defvar, defconst, and fset we skip to the docstring with a kludgy
437 formatting convention: all docstrings must appear on the same line as the
438 initial open-paren (the one in column zero) and must contain a backslash
439 and a double-quote immediately after the initial double-quote. No newlines
440 must appear between the beginning of the form and the first double-quote.
441 The only source file that must follow this convention is loaddefs.el; aside
442 from that, it is always the .elc file that we look at, and they are no
443 problem because byte-compiler output follows this convention.
444 The NAME and DOCSTRING are output.
445 NAME is preceded by `F' for a function or `V' for a variable.
446 An entry is output only if DOCSTRING has \ newline just after the opening "
454 while (c
== ' ' || c
== '\t' || c
== '\n')
460 read_lisp_symbol (infile
, buffer
)
465 char *fillp
= buffer
;
472 *(++fillp
) = getc (infile
);
473 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '(' || c
== ')')
484 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
490 scan_lisp_file (filename
, mode
)
491 char *filename
, *mode
;
496 infile
= fopen (filename
, mode
);
500 return 0; /* No error */
504 while (!feof (infile
))
506 char buffer
[BUFSIZ
];
507 char *fillp
= buffer
;
519 read_lisp_symbol (infile
, buffer
);
521 if (! strcmp (buffer
, "defun") ||
522 ! strcmp (buffer
, "defmacro"))
525 read_lisp_symbol (infile
, buffer
);
527 /* Skip the arguments: either "nil" or a list in parens */
530 if (c
== 'n') /* nil */
532 if ((c
= getc (infile
)) != 'i' ||
533 (c
= getc (infile
)) != 'l')
535 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
542 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
551 /* If the next three characters aren't `dquote bslash newline'
552 then we're not reading a docstring.
554 if ((c
= getc (infile
)) != '"' ||
555 (c
= getc (infile
)) != '\\' ||
556 (c
= getc (infile
)) != '\n')
559 fprintf (stderr
, "## non-docstring in %s (%s)\n",
566 else if (! strcmp (buffer
, "defvar") ||
567 ! strcmp (buffer
, "defconst"))
571 read_lisp_symbol (infile
, buffer
);
573 /* Skip until the first newline; remember the two previous chars. */
574 while (c
!= '\n' && c
>= 0)
581 /* If two previous characters were " and \,
582 this is a doc string. Otherwise, there is none. */
583 if (c2
!= '"' || c1
!= '\\')
586 fprintf (stderr
, "## non-docstring in %s (%s)\n",
593 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
600 read_lisp_symbol (infile
, buffer
);
605 fprintf (stderr
, "## unparsable name in fset in %s\n",
609 read_lisp_symbol (infile
, buffer
);
610 if (strcmp (buffer
, "quote"))
612 fprintf (stderr
, "## unparsable name in fset in %s\n",
616 read_lisp_symbol (infile
, buffer
);
621 "## unparsable quoted name in fset in %s\n",
627 /* Skip until the first newline; remember the two previous chars. */
628 while (c
!= '\n' && c
>= 0)
635 /* If two previous characters were " and \,
636 this is a doc string. Otherwise, there is none. */
637 if (c2
!= '"' || c1
!= '\\')
640 fprintf (stderr
, "## non-docstring in %s (%s)\n",
647 else if (! strcmp (buffer
, "autoload"))
652 read_lisp_symbol (infile
, buffer
);
657 fprintf (stderr
, "## unparsable name in autoload in %s\n",
661 read_lisp_symbol (infile
, buffer
);
662 if (strcmp (buffer
, "quote"))
664 fprintf (stderr
, "## unparsable name in autoload in %s\n",
668 read_lisp_symbol (infile
, buffer
);
673 "## unparsable quoted name in autoload in %s\n",
679 if ((c
= getc (infile
)) != '\"')
681 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
685 read_c_string (infile
, 0);
688 /* If the next three characters aren't `dquote bslash newline'
689 then we're not reading a docstring.
691 if ((c
= getc (infile
)) != '"' ||
692 (c
= getc (infile
)) != '\\' ||
693 (c
= getc (infile
)) != '\n')
696 fprintf (stderr
, "## non-docstring in %s (%s)\n",
704 else if (! strcmp (buffer
, "if") ||
705 ! strcmp (buffer
, "byte-code"))
712 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
718 /* At this point, there is a docstring that we should gobble.
719 The opening quote (and leading backslash-newline) have already
723 putc (type
, outfile
);
724 fprintf (outfile
, "%s\n", buffer
);
725 read_c_string (infile
, 1);