1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 86, 92, 93, 94, 97, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; 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. */
22 /* The arguments given to this program are all the C and Lisp source files
23 of GNU Emacs. .elc and .el and .c files are allowed.
24 A .o file can also be specified; the .c file it was made from is used.
25 This helps the makefile pass the correct list of files.
27 The results, which go to standard output or to a file
28 specified with -a or -o (-a to append, -o to start from nothing),
29 are entries containing function or variable names and their documentation.
30 Each entry starts with a ^_ character.
31 Then comes F for a function or V for a variable.
32 Then comes the function or variable name, terminated with a newline.
33 Then comes the documentation for that function or variable.
36 #define NO_SHORTNAMES /* Tell config not to load remap.h */
39 /* defined to be emacs_main, sys_fopen, etc. in config.h */
52 #endif /* WINDOWSNT */
55 #define READ_TEXT "rt"
56 #define READ_BINARY "rb"
57 #else /* not DOS_NT */
59 #define READ_BINARY "r"
60 #endif /* not DOS_NT */
63 int scan_lisp_file ();
67 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
68 file where that function is defined. */
76 /* Stdio stream for output to the DOC file. */
79 /* Name this program was invoked with. */
82 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
89 fprintf (stderr
, "%s: ", progname
);
90 fprintf (stderr
, s1
, s2
);
91 fprintf (stderr
, "\n");
94 /* Print error message and exit. */
105 /* Like malloc but get fatal error if memory is exhausted. */
111 long *result
= (long *) malloc (size
);
113 fatal ("virtual memory exhausted", 0);
130 /* Don't put CRs in the DOC file. */
133 #if 0 /* Suspicion is that this causes hanging.
134 So instead we require people to use -o on MSDOS. */
135 (stdout
)->_flag
&= ~_IOTEXT
;
136 _setmode (fileno (stdout
), O_BINARY
);
142 _setmode (fileno (stdout
), O_BINARY
);
143 #endif /* WINDOWSNT */
145 /* If first two args are -o FILE, output to FILE. */
147 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
149 outfile
= fopen (argv
[i
+ 1], "w");
152 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
154 outfile
= fopen (argv
[i
+ 1], "a");
157 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
164 fatal ("No output file specified", "");
167 for (; i
< argc
; i
++)
170 /* Don't process one file twice. */
171 for (j
= first_infile
; j
< i
; j
++)
172 if (! strcmp (argv
[i
], argv
[j
]))
175 err_count
+= scan_file (argv
[i
]);
178 exit (err_count
> 0);
180 return err_count
> 0;
183 /* Read file FILENAME and output its doc strings to outfile. */
184 /* Return 1 if file is not found, 0 if it is found. */
190 int len
= strlen (filename
);
191 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
192 return scan_lisp_file (filename
, READ_BINARY
);
193 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
194 return scan_lisp_file (filename
, READ_TEXT
);
196 return scan_c_file (filename
, READ_TEXT
);
201 /* Some state during the execution of `read_c_string_or_comment'. */
204 /* A count of spaces and newlines that have been read, but not output. */
205 unsigned pending_spaces
, pending_newlines
;
207 /* Where we're reading from. */
210 /* If non-zero, a buffer into which to copy characters. */
212 /* If non-zero, a file into which to copy characters. */
215 /* A keyword we look for at the beginning of lines. If found, it is
216 not copied, and SAW_KEYWORD is set to true. */
218 /* The current point we've reached in an occurance of KEYWORD in
220 char *cur_keyword_ptr
;
221 /* Set to true if we saw an occurance of KEYWORD. */
225 /* Output CH to the file or buffer in STATE. Any pending newlines or
226 spaces are output first. */
231 struct rcsoc_state
*state
;
236 if (state
->pending_newlines
> 0)
238 state
->pending_newlines
--;
241 else if (state
->pending_spaces
> 0)
243 state
->pending_spaces
--;
250 putc (out_ch
, state
->out_file
);
252 *state
->buf_ptr
++ = out_ch
;
254 while (out_ch
!= ch
);
257 /* If in the middle of scanning a keyword, continue scanning with
258 character CH, otherwise output CH to the file or buffer in STATE.
259 Any pending newlines or spaces are output first, as well as any
260 previously scanned characters that were thought to be part of a
261 keyword, but were in fact not. */
264 scan_keyword_or_put_char (ch
, state
)
266 struct rcsoc_state
*state
;
269 && *state
->cur_keyword_ptr
== ch
270 && (state
->cur_keyword_ptr
> state
->keyword
271 || state
->pending_newlines
> 0))
272 /* We might be looking at STATE->keyword at some point.
273 Keep looking until we know for sure. */
275 if (*++state
->cur_keyword_ptr
== '\0')
276 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
278 state
->saw_keyword
= 1;
280 /* Reset the scanning pointer. */
281 state
->cur_keyword_ptr
= state
->keyword
;
283 /* Canonicalize whitespace preceding a usage string. */
284 state
->pending_newlines
= 2;
285 state
->pending_spaces
= 0;
287 /* Skip any whitespace between the keyword and the
290 ch
= getc (state
->in_file
);
291 while (ch
== ' ' || ch
== '\n');
293 /* Output the open-paren we just read. */
294 put_char (ch
, state
);
296 /* Skip the function name and replace it with `fn'. */
298 ch
= getc (state
->in_file
);
299 while (ch
!= ' ' && ch
!= ')');
300 put_char ('f', state
);
301 put_char ('n', state
);
303 /* Put back the last character. */
304 ungetc (ch
, state
->in_file
);
309 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
310 /* We scanned the beginning of a potential usage
311 keyword, but it was a false alarm. Output the
316 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
317 put_char (*p
, state
);
319 state
->cur_keyword_ptr
= state
->keyword
;
322 put_char (ch
, state
);
327 /* Skip a C string or C-style comment from INFILE, and return the
328 character that follows. COMMENT non-zero means skip a comment. If
329 PRINTFLAG is positive, output string contents to outfile. If it is
330 negative, store contents in buf. Convert escape sequences \n and
331 \t to newline and tab; discard \ followed by newline.
332 If SAW_USAGE is non-zero, then any occurances of the string `usage:'
333 at the beginning of a line will be removed, and *SAW_USAGE set to
334 true if any were encountered. */
337 read_c_string_or_comment (infile
, printflag
, comment
, saw_usage
)
344 struct rcsoc_state state
;
346 state
.in_file
= infile
;
347 state
.buf_ptr
= (printflag
< 0 ? buf
: 0);
348 state
.out_file
= (printflag
> 0 ? outfile
: 0);
349 state
.pending_spaces
= 0;
350 state
.pending_newlines
= 0;
351 state
.keyword
= (saw_usage
? "usage:" : 0);
352 state
.cur_keyword_ptr
= state
.keyword
;
353 state
.saw_keyword
= 0;
357 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
362 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
367 if (c
== '\n' || c
== '\r')
379 state
.pending_spaces
++;
382 state
.pending_newlines
++;
383 state
.pending_spaces
= 0;
386 scan_keyword_or_put_char (c
, &state
);
402 scan_keyword_or_put_char ('*', &state
);
409 /* If we had a "", concatenate the two strings. */
418 *saw_usage
= state
.saw_keyword
;
425 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
426 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
429 write_c_args (out
, func
, buf
, minargs
, maxargs
)
432 int minargs
, maxargs
;
439 fprintf (out
, "(fn");
444 for (p
= buf
; *p
; p
++)
449 /* Notice when we start printing a new identifier. */
450 if ((('A' <= c
&& c
<= 'Z')
451 || ('a' <= c
&& c
<= 'z')
452 || ('0' <= c
&& c
<= '9')
464 if (minargs
== 0 && maxargs
> 0)
465 fprintf (out
, "&optional ");
475 /* Print the C argument list as it would appear in lisp:
476 print underscores as hyphens, and print commas and newlines
477 as spaces. Collapse adjacent spaces into one. */
480 else if (c
== ',' || c
== '\n')
483 /* In C code, `default' is a reserved word, so we spell it
484 `defalt'; unmangle that here. */
486 && strncmp (p
, "defalt", 6) == 0
487 && ! (('A' <= p
[6] && p
[6] <= 'Z')
488 || ('a' <= p
[6] && p
[6] <= 'z')
489 || ('0' <= p
[6] && p
[6] <= '9')
492 fprintf (out
, "DEFAULT");
497 else if (c
!= ' ' || !just_spaced
)
499 if (c
>= 'a' && c
<= 'z')
500 /* Upcase the letter. */
505 just_spaced
= c
== ' ';
510 /* Read through a c file. If a .o file is named,
511 the corresponding .c file is read instead.
512 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
513 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
516 scan_c_file (filename
, mode
)
517 char *filename
, *mode
;
522 register int defunflag
;
523 register int defvarperbufferflag
;
524 register int defvarflag
;
525 int minargs
, maxargs
;
526 int extension
= filename
[strlen (filename
) - 1];
528 if (extension
== 'o')
529 filename
[strlen (filename
) - 1] = 'c';
531 infile
= fopen (filename
, mode
);
533 /* No error if non-ex input file */
540 /* Reset extension to be able to detect duplicate files. */
541 filename
[strlen (filename
) - 1] = extension
;
544 while (!feof (infile
))
548 if (c
!= '\n' && c
!= '\r')
583 defvarperbufferflag
= (c
== 'P');
596 defunflag
= c
== 'U';
608 /* Lisp variable or function name. */
612 c
= read_c_string_or_comment (infile
, -1, 0, 0);
614 /* DEFVAR_LISP ("name", addr, "doc")
615 DEFVAR_LISP ("name", addr /\* doc *\/)
616 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
620 else if (defvarperbufferflag
)
624 else /* For DEFSIMPLE and DEFPRED */
633 if (defunflag
&& (commas
== 1 || commas
== 2))
637 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
641 if (commas
== 2) /* pick up minargs */
642 fscanf (infile
, "%d", &minargs
);
643 else /* pick up maxargs */
644 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
647 fscanf (infile
, "%d", &maxargs
);
656 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
660 c
= read_c_string_or_comment (infile
, 0, 0, 0);
662 while (c
!= EOF
&& c
!= ',' && c
!= '/')
667 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
669 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
675 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
682 && (c
= getc (infile
),
686 int comment
= c
!= '"';
690 putc (defvarflag
? 'V' : 'F', outfile
);
691 fprintf (outfile
, "%s\n", buf
);
694 getc (infile
); /* Skip past `*' */
695 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
697 /* If this is a defun, find the arguments and print them. If
698 this function takes MANY or UNEVALLED args, then the C source
699 won't give the names of the arguments, so we shouldn't bother
702 Various doc-string styles:
703 0: DEFUN (..., "DOC") (args) [!comment]
704 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
705 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
707 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
709 char argbuf
[1024], *p
= argbuf
;
711 if (!comment
|| doc_keyword
)
719 /* Skip into arguments. */
726 /* Copy arguments into ARGBUF. */
729 *p
++ = c
= getc (infile
);
733 fprintf (outfile
, "\n\n");
734 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
736 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
737 /* The DOC should provide the usage form. */
738 fprintf (stderr
, "Missing `usage' for function `%s'.\n", buf
);
746 /* Read a file of Lisp code, compiled or interpreted.
748 (defun NAME ARGS DOCSTRING ...)
749 (defmacro NAME ARGS DOCSTRING ...)
750 (defsubst NAME ARGS DOCSTRING ...)
751 (autoload (quote NAME) FILE DOCSTRING ...)
752 (defvar NAME VALUE DOCSTRING)
753 (defconst NAME VALUE DOCSTRING)
754 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
755 (fset (quote NAME) #[... DOCSTRING ...])
756 (defalias (quote NAME) #[... DOCSTRING ...])
757 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
758 starting in column zero.
759 (quote NAME) may appear as 'NAME as well.
761 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
762 When we find that, we save it for the following defining-form,
763 and we use that instead of reading a doc string within that defining-form.
765 For defvar, defconst, and fset we skip to the docstring with a kludgy
766 formatting convention: all docstrings must appear on the same line as the
767 initial open-paren (the one in column zero) and must contain a backslash
768 and a newline immediately after the initial double-quote. No newlines
769 must appear between the beginning of the form and the first double-quote.
770 For defun, defmacro, and autoload, we know how to skip over the
771 arglist, but the doc string must still have a backslash and newline
772 immediately after the double quote.
773 The only source files that must follow this convention are preloaded
774 uncompiled ones like loaddefs.el and bindings.el; aside
775 from that, it is always the .elc file that we look at, and they are no
776 problem because byte-compiler output follows this convention.
777 The NAME and DOCSTRING are output.
778 NAME is preceded by `F' for a function or `V' for a variable.
779 An entry is output only if DOCSTRING has \ newline just after the opening "
787 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
793 read_lisp_symbol (infile
, buffer
)
798 char *fillp
= buffer
;
805 *(++fillp
) = getc (infile
);
806 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
817 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
823 scan_lisp_file (filename
, mode
)
824 char *filename
, *mode
;
828 char *saved_string
= 0;
830 infile
= fopen (filename
, mode
);
834 return 0; /* No error */
838 while (!feof (infile
))
843 /* If not at end of line, skip till we get to one. */
844 if (c
!= '\n' && c
!= '\r')
849 /* Skip the line break. */
850 while (c
== '\n' || c
== '\r')
852 /* Detect a dynamic doc string and save it for the next expression. */
861 /* Read the length. */
862 while ((c
= getc (infile
),
863 c
>= '0' && c
<= '9'))
869 /* The next character is a space that is counted in the length
870 but not part of the doc string.
871 We already read it, so just ignore it. */
874 /* Read in the contents. */
875 if (saved_string
!= 0)
877 saved_string
= (char *) malloc (length
);
878 for (i
= 0; i
< length
; i
++)
879 saved_string
[i
] = getc (infile
);
880 /* The last character is a ^_.
881 That is needed in the .elc file
882 but it is redundant in DOC. So get rid of it here. */
883 saved_string
[length
- 1] = 0;
884 /* Skip the line break. */
885 while (c
== '\n' && c
== '\r')
887 /* Skip the following line. */
888 while (c
!= '\n' && c
!= '\r')
897 read_lisp_symbol (infile
, buffer
);
899 if (! strcmp (buffer
, "defun")
900 || ! strcmp (buffer
, "defmacro")
901 || ! strcmp (buffer
, "defsubst"))
904 read_lisp_symbol (infile
, buffer
);
906 /* Skip the arguments: either "nil" or a list in parens */
909 if (c
== 'n') /* nil */
911 if ((c
= getc (infile
)) != 'i'
912 || (c
= getc (infile
)) != 'l')
914 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
921 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
930 /* If the next three characters aren't `dquote bslash newline'
931 then we're not reading a docstring.
933 if ((c
= getc (infile
)) != '"'
934 || (c
= getc (infile
)) != '\\'
935 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
938 fprintf (stderr
, "## non-docstring in %s (%s)\n",
945 else if (! strcmp (buffer
, "defvar")
946 || ! strcmp (buffer
, "defconst"))
950 read_lisp_symbol (infile
, buffer
);
952 if (saved_string
== 0)
955 /* Skip until the end of line; remember two previous chars. */
956 while (c
!= '\n' && c
!= '\r' && c
>= 0)
963 /* If two previous characters were " and \,
964 this is a doc string. Otherwise, there is none. */
965 if (c2
!= '"' || c1
!= '\\')
968 fprintf (stderr
, "## non-docstring in %s (%s)\n",
976 else if (! strcmp (buffer
, "custom-declare-variable"))
983 read_lisp_symbol (infile
, buffer
);
989 "## unparsable name in custom-declare-variable in %s\n",
993 read_lisp_symbol (infile
, buffer
);
994 if (strcmp (buffer
, "quote"))
997 "## unparsable name in custom-declare-variable in %s\n",
1001 read_lisp_symbol (infile
, buffer
);
1006 "## unparsable quoted name in custom-declare-variable in %s\n",
1012 if (saved_string
== 0)
1014 /* Skip to end of line; remember the two previous chars. */
1015 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1022 /* If two previous characters were " and \,
1023 this is a doc string. Otherwise, there is none. */
1024 if (c2
!= '"' || c1
!= '\\')
1027 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1035 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1037 char c1
= 0, c2
= 0;
1042 read_lisp_symbol (infile
, buffer
);
1047 fprintf (stderr
, "## unparsable name in fset in %s\n",
1051 read_lisp_symbol (infile
, buffer
);
1052 if (strcmp (buffer
, "quote"))
1054 fprintf (stderr
, "## unparsable name in fset in %s\n",
1058 read_lisp_symbol (infile
, buffer
);
1063 "## unparsable quoted name in fset in %s\n",
1069 if (saved_string
== 0)
1071 /* Skip to end of line; remember the two previous chars. */
1072 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1079 /* If two previous characters were " and \,
1080 this is a doc string. Otherwise, there is none. */
1081 if (c2
!= '"' || c1
!= '\\')
1084 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1092 else if (! strcmp (buffer
, "autoload"))
1097 read_lisp_symbol (infile
, buffer
);
1102 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1106 read_lisp_symbol (infile
, buffer
);
1107 if (strcmp (buffer
, "quote"))
1109 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1113 read_lisp_symbol (infile
, buffer
);
1118 "## unparsable quoted name in autoload in %s\n",
1123 skip_white (infile
);
1124 if ((c
= getc (infile
)) != '\"')
1126 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1130 read_c_string_or_comment (infile
, 0, 0, 0);
1131 skip_white (infile
);
1133 if (saved_string
== 0)
1135 /* If the next three characters aren't `dquote bslash newline'
1136 then we're not reading a docstring. */
1137 if ((c
= getc (infile
)) != '"'
1138 || (c
= getc (infile
)) != '\\'
1139 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1142 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1151 else if (! strcmp (buffer
, "if")
1152 || ! strcmp (buffer
, "byte-code"))
1159 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
1165 /* At this point, we should either use the previous
1166 dynamic doc string in saved_string
1167 or gobble a doc string from the input file.
1169 In the latter case, the opening quote (and leading
1170 backslash-newline) have already been read. */
1172 putc (037, outfile
);
1173 putc (type
, outfile
);
1174 fprintf (outfile
, "%s\n", buffer
);
1177 fputs (saved_string
, outfile
);
1178 /* Don't use one dynamic doc string twice. */
1179 free (saved_string
);
1183 read_c_string_or_comment (infile
, 1, 0, 0);