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.
26 Option -d DIR means change to DIR before looking for files.
28 The results, which go to standard output or to a file
29 specified with -a or -o (-a to append, -o to start from nothing),
30 are entries containing function or variable names and their documentation.
31 Each entry starts with a ^_ character.
32 Then comes F for a function or V for a variable.
33 Then comes the function or variable name, terminated with a newline.
34 Then comes the documentation for that function or variable.
37 #define NO_SHORTNAMES /* Tell config not to load remap.h */
40 /* defined to be emacs_main, sys_fopen, etc. in config.h */
53 #endif /* WINDOWSNT */
56 #define READ_TEXT "rt"
57 #define READ_BINARY "rb"
58 #else /* not DOS_NT */
60 #define READ_BINARY "r"
61 #endif /* not DOS_NT */
64 int scan_lisp_file ();
68 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
69 file where that function is defined. */
77 /* Stdio stream for output to the DOC file. */
80 /* Name this program was invoked with. */
83 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
90 fprintf (stderr
, "%s: ", progname
);
91 fprintf (stderr
, s1
, s2
);
92 fprintf (stderr
, "\n");
95 /* Print error message and exit. */
106 /* Like malloc but get fatal error if memory is exhausted. */
112 long *result
= (long *) malloc (size
);
114 fatal ("virtual memory exhausted", 0);
131 /* Don't put CRs in the DOC file. */
134 #if 0 /* Suspicion is that this causes hanging.
135 So instead we require people to use -o on MSDOS. */
136 (stdout
)->_flag
&= ~_IOTEXT
;
137 _setmode (fileno (stdout
), O_BINARY
);
143 _setmode (fileno (stdout
), O_BINARY
);
144 #endif /* WINDOWSNT */
146 /* If first two args are -o FILE, output to FILE. */
148 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
150 outfile
= fopen (argv
[i
+ 1], "w");
153 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
155 outfile
= fopen (argv
[i
+ 1], "a");
158 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
165 fatal ("No output file specified", "");
168 for (; i
< argc
; i
++)
171 /* Don't process one file twice. */
172 for (j
= first_infile
; j
< i
; j
++)
173 if (! strcmp (argv
[i
], argv
[j
]))
176 err_count
+= scan_file (argv
[i
]);
178 return (err_count
> 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
181 /* Read file FILENAME and output its doc strings to outfile. */
182 /* Return 1 if file is not found, 0 if it is found. */
188 int len
= strlen (filename
);
189 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
190 return scan_lisp_file (filename
, READ_BINARY
);
191 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
192 return scan_lisp_file (filename
, READ_TEXT
);
194 return scan_c_file (filename
, READ_TEXT
);
199 /* Some state during the execution of `read_c_string_or_comment'. */
202 /* A count of spaces and newlines that have been read, but not output. */
203 unsigned pending_spaces
, pending_newlines
;
205 /* Where we're reading from. */
208 /* If non-zero, a buffer into which to copy characters. */
210 /* If non-zero, a file into which to copy characters. */
213 /* A keyword we look for at the beginning of lines. If found, it is
214 not copied, and SAW_KEYWORD is set to true. */
216 /* The current point we've reached in an occurance of KEYWORD in
218 char *cur_keyword_ptr
;
219 /* Set to true if we saw an occurance of KEYWORD. */
223 /* Output CH to the file or buffer in STATE. Any pending newlines or
224 spaces are output first. */
229 struct rcsoc_state
*state
;
234 if (state
->pending_newlines
> 0)
236 state
->pending_newlines
--;
239 else if (state
->pending_spaces
> 0)
241 state
->pending_spaces
--;
248 putc (out_ch
, state
->out_file
);
250 *state
->buf_ptr
++ = out_ch
;
252 while (out_ch
!= ch
);
255 /* If in the middle of scanning a keyword, continue scanning with
256 character CH, otherwise output CH to the file or buffer in STATE.
257 Any pending newlines or spaces are output first, as well as any
258 previously scanned characters that were thought to be part of a
259 keyword, but were in fact not. */
262 scan_keyword_or_put_char (ch
, state
)
264 struct rcsoc_state
*state
;
267 && *state
->cur_keyword_ptr
== ch
268 && (state
->cur_keyword_ptr
> state
->keyword
269 || state
->pending_newlines
> 0))
270 /* We might be looking at STATE->keyword at some point.
271 Keep looking until we know for sure. */
273 if (*++state
->cur_keyword_ptr
== '\0')
274 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
276 state
->saw_keyword
= 1;
278 /* Reset the scanning pointer. */
279 state
->cur_keyword_ptr
= state
->keyword
;
281 /* Canonicalize whitespace preceding a usage string. */
282 state
->pending_newlines
= 2;
283 state
->pending_spaces
= 0;
285 /* Skip any whitespace between the keyword and the
288 ch
= getc (state
->in_file
);
289 while (ch
== ' ' || ch
== '\n');
291 /* Output the open-paren we just read. */
292 put_char (ch
, state
);
294 /* Skip the function name and replace it with `fn'. */
296 ch
= getc (state
->in_file
);
297 while (ch
!= ' ' && ch
!= ')');
298 put_char ('f', state
);
299 put_char ('n', state
);
301 /* Put back the last character. */
302 ungetc (ch
, state
->in_file
);
307 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
308 /* We scanned the beginning of a potential usage
309 keyword, but it was a false alarm. Output the
314 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
315 put_char (*p
, state
);
317 state
->cur_keyword_ptr
= state
->keyword
;
320 put_char (ch
, state
);
325 /* Skip a C string or C-style comment from INFILE, and return the
326 character that follows. COMMENT non-zero means skip a comment. If
327 PRINTFLAG is positive, output string contents to outfile. If it is
328 negative, store contents in buf. Convert escape sequences \n and
329 \t to newline and tab; discard \ followed by newline.
330 If SAW_USAGE is non-zero, then any occurances of the string `usage:'
331 at the beginning of a line will be removed, and *SAW_USAGE set to
332 true if any were encountered. */
335 read_c_string_or_comment (infile
, printflag
, comment
, saw_usage
)
342 struct rcsoc_state state
;
344 state
.in_file
= infile
;
345 state
.buf_ptr
= (printflag
< 0 ? buf
: 0);
346 state
.out_file
= (printflag
> 0 ? outfile
: 0);
347 state
.pending_spaces
= 0;
348 state
.pending_newlines
= 0;
349 state
.keyword
= (saw_usage
? "usage:" : 0);
350 state
.cur_keyword_ptr
= state
.keyword
;
351 state
.saw_keyword
= 0;
355 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
360 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
365 if (c
== '\n' || c
== '\r')
377 state
.pending_spaces
++;
380 state
.pending_newlines
++;
381 state
.pending_spaces
= 0;
384 scan_keyword_or_put_char (c
, &state
);
400 scan_keyword_or_put_char ('*', &state
);
407 /* If we had a "", concatenate the two strings. */
416 *saw_usage
= state
.saw_keyword
;
423 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
424 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
427 write_c_args (out
, func
, buf
, minargs
, maxargs
)
430 int minargs
, maxargs
;
437 fprintf (out
, "(fn");
442 for (p
= buf
; *p
; p
++)
447 /* Notice when we start printing a new identifier. */
448 if ((('A' <= c
&& c
<= 'Z')
449 || ('a' <= c
&& c
<= 'z')
450 || ('0' <= c
&& c
<= '9')
462 if (minargs
== 0 && maxargs
> 0)
463 fprintf (out
, "&optional ");
473 /* Print the C argument list as it would appear in lisp:
474 print underscores as hyphens, and print commas and newlines
475 as spaces. Collapse adjacent spaces into one. */
478 else if (c
== ',' || c
== '\n')
481 /* In C code, `default' is a reserved word, so we spell it
482 `defalt'; unmangle that here. */
484 && strncmp (p
, "defalt", 6) == 0
485 && ! (('A' <= p
[6] && p
[6] <= 'Z')
486 || ('a' <= p
[6] && p
[6] <= 'z')
487 || ('0' <= p
[6] && p
[6] <= '9')
490 fprintf (out
, "DEFAULT");
495 else if (c
!= ' ' || !just_spaced
)
497 if (c
>= 'a' && c
<= 'z')
498 /* Upcase the letter. */
503 just_spaced
= c
== ' ';
508 /* Read through a c file. If a .o file is named,
509 the corresponding .c file is read instead.
510 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
511 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
514 scan_c_file (filename
, mode
)
515 char *filename
, *mode
;
520 register int defunflag
;
521 register int defvarperbufferflag
;
522 register int defvarflag
;
523 int minargs
, maxargs
;
524 int extension
= filename
[strlen (filename
) - 1];
526 if (extension
== 'o')
527 filename
[strlen (filename
) - 1] = 'c';
529 infile
= fopen (filename
, mode
);
531 /* No error if non-ex input file */
538 /* Reset extension to be able to detect duplicate files. */
539 filename
[strlen (filename
) - 1] = extension
;
542 while (!feof (infile
))
546 if (c
!= '\n' && c
!= '\r')
581 defvarperbufferflag
= (c
== 'P');
594 defunflag
= c
== 'U';
606 /* Lisp variable or function name. */
610 c
= read_c_string_or_comment (infile
, -1, 0, 0);
612 /* DEFVAR_LISP ("name", addr, "doc")
613 DEFVAR_LISP ("name", addr /\* doc *\/)
614 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
618 else if (defvarperbufferflag
)
622 else /* For DEFSIMPLE and DEFPRED */
631 if (defunflag
&& (commas
== 1 || commas
== 2))
635 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
639 if (commas
== 2) /* pick up minargs */
640 fscanf (infile
, "%d", &minargs
);
641 else /* pick up maxargs */
642 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
645 fscanf (infile
, "%d", &maxargs
);
654 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
658 c
= read_c_string_or_comment (infile
, 0, 0, 0);
660 while (c
!= EOF
&& c
!= ',' && c
!= '/')
665 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
667 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
673 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
680 && (c
= getc (infile
),
684 int comment
= c
!= '"';
688 putc (defvarflag
? 'V' : 'F', outfile
);
689 fprintf (outfile
, "%s\n", buf
);
692 getc (infile
); /* Skip past `*' */
693 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
695 /* If this is a defun, find the arguments and print them. If
696 this function takes MANY or UNEVALLED args, then the C source
697 won't give the names of the arguments, so we shouldn't bother
700 Various doc-string styles:
701 0: DEFUN (..., "DOC") (args) [!comment]
702 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
703 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
705 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
707 char argbuf
[1024], *p
= argbuf
;
709 if (!comment
|| doc_keyword
)
717 /* Skip into arguments. */
724 /* Copy arguments into ARGBUF. */
727 *p
++ = c
= getc (infile
);
731 fprintf (outfile
, "\n\n");
732 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
734 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
735 /* The DOC should provide the usage form. */
736 fprintf (stderr
, "Missing `usage' for function `%s'.\n", buf
);
744 /* Read a file of Lisp code, compiled or interpreted.
746 (defun NAME ARGS DOCSTRING ...)
747 (defmacro NAME ARGS DOCSTRING ...)
748 (defsubst NAME ARGS DOCSTRING ...)
749 (autoload (quote NAME) FILE DOCSTRING ...)
750 (defvar NAME VALUE DOCSTRING)
751 (defconst NAME VALUE DOCSTRING)
752 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
753 (fset (quote NAME) #[... DOCSTRING ...])
754 (defalias (quote NAME) #[... DOCSTRING ...])
755 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
756 starting in column zero.
757 (quote NAME) may appear as 'NAME as well.
759 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
760 When we find that, we save it for the following defining-form,
761 and we use that instead of reading a doc string within that defining-form.
763 For defvar, defconst, and fset we skip to the docstring with a kludgy
764 formatting convention: all docstrings must appear on the same line as the
765 initial open-paren (the one in column zero) and must contain a backslash
766 and a newline immediately after the initial double-quote. No newlines
767 must appear between the beginning of the form and the first double-quote.
768 For defun, defmacro, and autoload, we know how to skip over the
769 arglist, but the doc string must still have a backslash and newline
770 immediately after the double quote.
771 The only source files that must follow this convention are preloaded
772 uncompiled ones like loaddefs.el and bindings.el; aside
773 from that, it is always the .elc file that we look at, and they are no
774 problem because byte-compiler output follows this convention.
775 The NAME and DOCSTRING are output.
776 NAME is preceded by `F' for a function or `V' for a variable.
777 An entry is output only if DOCSTRING has \ newline just after the opening "
785 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
791 read_lisp_symbol (infile
, buffer
)
796 char *fillp
= buffer
;
803 *(++fillp
) = getc (infile
);
804 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
815 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
821 scan_lisp_file (filename
, mode
)
822 char *filename
, *mode
;
826 char *saved_string
= 0;
828 infile
= fopen (filename
, mode
);
832 return 0; /* No error */
836 while (!feof (infile
))
841 /* If not at end of line, skip till we get to one. */
842 if (c
!= '\n' && c
!= '\r')
847 /* Skip the line break. */
848 while (c
== '\n' || c
== '\r')
850 /* Detect a dynamic doc string and save it for the next expression. */
859 /* Read the length. */
860 while ((c
= getc (infile
),
861 c
>= '0' && c
<= '9'))
867 /* The next character is a space that is counted in the length
868 but not part of the doc string.
869 We already read it, so just ignore it. */
872 /* Read in the contents. */
873 if (saved_string
!= 0)
875 saved_string
= (char *) malloc (length
);
876 for (i
= 0; i
< length
; i
++)
877 saved_string
[i
] = getc (infile
);
878 /* The last character is a ^_.
879 That is needed in the .elc file
880 but it is redundant in DOC. So get rid of it here. */
881 saved_string
[length
- 1] = 0;
882 /* Skip the line break. */
883 while (c
== '\n' && c
== '\r')
885 /* Skip the following line. */
886 while (c
!= '\n' && c
!= '\r')
895 read_lisp_symbol (infile
, buffer
);
897 if (! strcmp (buffer
, "defun")
898 || ! strcmp (buffer
, "defmacro")
899 || ! strcmp (buffer
, "defsubst"))
902 read_lisp_symbol (infile
, buffer
);
904 /* Skip the arguments: either "nil" or a list in parens */
907 if (c
== 'n') /* nil */
909 if ((c
= getc (infile
)) != 'i'
910 || (c
= getc (infile
)) != 'l')
912 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
919 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
928 /* If the next three characters aren't `dquote bslash newline'
929 then we're not reading a docstring.
931 if ((c
= getc (infile
)) != '"'
932 || (c
= getc (infile
)) != '\\'
933 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
936 fprintf (stderr
, "## non-docstring in %s (%s)\n",
943 else if (! strcmp (buffer
, "defvar")
944 || ! strcmp (buffer
, "defconst"))
948 read_lisp_symbol (infile
, buffer
);
950 if (saved_string
== 0)
953 /* Skip until the end of line; remember two previous chars. */
954 while (c
!= '\n' && c
!= '\r' && c
>= 0)
961 /* If two previous characters were " and \,
962 this is a doc string. Otherwise, there is none. */
963 if (c2
!= '"' || c1
!= '\\')
966 fprintf (stderr
, "## non-docstring in %s (%s)\n",
974 else if (! strcmp (buffer
, "custom-declare-variable"))
981 read_lisp_symbol (infile
, buffer
);
987 "## unparsable name in custom-declare-variable in %s\n",
991 read_lisp_symbol (infile
, buffer
);
992 if (strcmp (buffer
, "quote"))
995 "## unparsable name in custom-declare-variable in %s\n",
999 read_lisp_symbol (infile
, buffer
);
1004 "## unparsable quoted name in custom-declare-variable in %s\n",
1010 if (saved_string
== 0)
1012 /* Skip to end of line; remember the two previous chars. */
1013 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1020 /* If two previous characters were " and \,
1021 this is a doc string. Otherwise, there is none. */
1022 if (c2
!= '"' || c1
!= '\\')
1025 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1033 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1035 char c1
= 0, c2
= 0;
1040 read_lisp_symbol (infile
, buffer
);
1045 fprintf (stderr
, "## unparsable name in fset in %s\n",
1049 read_lisp_symbol (infile
, buffer
);
1050 if (strcmp (buffer
, "quote"))
1052 fprintf (stderr
, "## unparsable name in fset in %s\n",
1056 read_lisp_symbol (infile
, buffer
);
1061 "## unparsable quoted name in fset in %s\n",
1067 if (saved_string
== 0)
1069 /* Skip to end of line; remember the two previous chars. */
1070 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1077 /* If two previous characters were " and \,
1078 this is a doc string. Otherwise, there is none. */
1079 if (c2
!= '"' || c1
!= '\\')
1082 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1090 else if (! strcmp (buffer
, "autoload"))
1095 read_lisp_symbol (infile
, buffer
);
1100 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1104 read_lisp_symbol (infile
, buffer
);
1105 if (strcmp (buffer
, "quote"))
1107 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1111 read_lisp_symbol (infile
, buffer
);
1116 "## unparsable quoted name in autoload in %s\n",
1121 skip_white (infile
);
1122 if ((c
= getc (infile
)) != '\"')
1124 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1128 read_c_string_or_comment (infile
, 0, 0, 0);
1129 skip_white (infile
);
1131 if (saved_string
== 0)
1133 /* If the next three characters aren't `dquote bslash newline'
1134 then we're not reading a docstring. */
1135 if ((c
= getc (infile
)) != '"'
1136 || (c
= getc (infile
)) != '\\'
1137 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1140 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1149 else if (! strcmp (buffer
, "if")
1150 || ! strcmp (buffer
, "byte-code"))
1157 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
1163 /* At this point, we should either use the previous
1164 dynamic doc string in saved_string
1165 or gobble a doc string from the input file.
1167 In the latter case, the opening quote (and leading
1168 backslash-newline) have already been read. */
1170 putc (037, outfile
);
1171 putc (type
, outfile
);
1172 fprintf (outfile
, "%s\n", buffer
);
1175 fputs (saved_string
, outfile
);
1176 /* Don't use one dynamic doc string twice. */
1177 free (saved_string
);
1181 read_c_string_or_comment (infile
, 1, 0, 0);
1187 /* arch-tag: f7203aaf-991a-4238-acb5-601db56f2894
1188 (do not change this comment) */