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
)
343 struct rcsoc_state state
;
345 state
.in_file
= infile
;
346 state
.buf_ptr
= (printflag
< 0 ? buf
: 0);
347 state
.out_file
= (printflag
> 0 ? outfile
: 0);
348 state
.pending_spaces
= 0;
349 state
.pending_newlines
= 0;
350 state
.keyword
= (saw_usage
? "usage:" : 0);
351 state
.cur_keyword_ptr
= state
.keyword
;
352 state
.saw_keyword
= 0;
356 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
361 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
366 if (c
== '\n' || c
== '\r')
378 state
.pending_spaces
++;
381 state
.pending_newlines
++;
382 state
.pending_spaces
= 0;
385 scan_keyword_or_put_char (c
, &state
);
401 scan_keyword_or_put_char ('*', &state
);
408 /* If we had a "", concatenate the two strings. */
417 *saw_usage
= state
.saw_keyword
;
424 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
425 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
428 write_c_args (out
, func
, buf
, minargs
, maxargs
)
431 int minargs
, maxargs
;
438 fprintf (out
, "(fn");
443 for (p
= buf
; *p
; p
++)
448 /* Notice when we start printing a new identifier. */
449 if ((('A' <= c
&& c
<= 'Z')
450 || ('a' <= c
&& c
<= 'z')
451 || ('0' <= c
&& c
<= '9')
463 if (minargs
== 0 && maxargs
> 0)
464 fprintf (out
, "&optional ");
474 /* Print the C argument list as it would appear in lisp:
475 print underscores as hyphens, and print commas and newlines
476 as spaces. Collapse adjacent spaces into one. */
479 else if (c
== ',' || c
== '\n')
482 /* In C code, `default' is a reserved word, so we spell it
483 `defalt'; unmangle that here. */
485 && strncmp (p
, "defalt", 6) == 0
486 && ! (('A' <= p
[6] && p
[6] <= 'Z')
487 || ('a' <= p
[6] && p
[6] <= 'z')
488 || ('0' <= p
[6] && p
[6] <= '9')
491 fprintf (out
, "DEFAULT");
496 else if (c
!= ' ' || !just_spaced
)
498 if (c
>= 'a' && c
<= 'z')
499 /* Upcase the letter. */
504 just_spaced
= c
== ' ';
509 /* Read through a c file. If a .o file is named,
510 the corresponding .c file is read instead.
511 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
512 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
515 scan_c_file (filename
, mode
)
516 char *filename
, *mode
;
521 register int defunflag
;
522 register int defvarperbufferflag
;
523 register int defvarflag
;
524 int minargs
, maxargs
;
525 int extension
= filename
[strlen (filename
) - 1];
527 if (extension
== 'o')
528 filename
[strlen (filename
) - 1] = 'c';
530 infile
= fopen (filename
, mode
);
532 /* No error if non-ex input file */
539 /* Reset extension to be able to detect duplicate files. */
540 filename
[strlen (filename
) - 1] = extension
;
543 while (!feof (infile
))
547 if (c
!= '\n' && c
!= '\r')
582 defvarperbufferflag
= (c
== 'P');
595 defunflag
= c
== 'U';
607 /* Lisp variable or function name. */
611 c
= read_c_string_or_comment (infile
, -1, 0, 0);
613 /* DEFVAR_LISP ("name", addr, "doc")
614 DEFVAR_LISP ("name", addr /\* doc *\/)
615 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
619 else if (defvarperbufferflag
)
623 else /* For DEFSIMPLE and DEFPRED */
632 if (defunflag
&& (commas
== 1 || commas
== 2))
636 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
640 if (commas
== 2) /* pick up minargs */
641 fscanf (infile
, "%d", &minargs
);
642 else /* pick up maxargs */
643 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
646 fscanf (infile
, "%d", &maxargs
);
655 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
659 c
= read_c_string_or_comment (infile
, 0, 0, 0);
661 while (c
!= EOF
&& c
!= ',' && c
!= '/')
666 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
668 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
674 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
681 && (c
= getc (infile
),
685 int comment
= c
!= '"';
689 putc (defvarflag
? 'V' : 'F', outfile
);
690 fprintf (outfile
, "%s\n", buf
);
693 getc (infile
); /* Skip past `*' */
694 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
696 /* If this is a defun, find the arguments and print them. If
697 this function takes MANY or UNEVALLED args, then the C source
698 won't give the names of the arguments, so we shouldn't bother
701 Various doc-string styles:
702 0: DEFUN (..., "DOC") (args) [!comment]
703 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
704 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
706 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
708 char argbuf
[1024], *p
= argbuf
;
710 if (!comment
|| doc_keyword
)
718 /* Skip into arguments. */
725 /* Copy arguments into ARGBUF. */
728 *p
++ = c
= getc (infile
);
732 fprintf (outfile
, "\n\n");
733 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
735 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
736 /* The DOC should provide the usage form. */
737 fprintf (stderr
, "Missing `usage' for function `%s'.\n", buf
);
745 /* Read a file of Lisp code, compiled or interpreted.
747 (defun NAME ARGS DOCSTRING ...)
748 (defmacro NAME ARGS DOCSTRING ...)
749 (defsubst NAME ARGS DOCSTRING ...)
750 (autoload (quote NAME) FILE DOCSTRING ...)
751 (defvar NAME VALUE DOCSTRING)
752 (defconst NAME VALUE DOCSTRING)
753 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
754 (fset (quote NAME) #[... DOCSTRING ...])
755 (defalias (quote NAME) #[... DOCSTRING ...])
756 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
757 starting in column zero.
758 (quote NAME) may appear as 'NAME as well.
760 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
761 When we find that, we save it for the following defining-form,
762 and we use that instead of reading a doc string within that defining-form.
764 For defvar, defconst, and fset we skip to the docstring with a kludgy
765 formatting convention: all docstrings must appear on the same line as the
766 initial open-paren (the one in column zero) and must contain a backslash
767 and a newline immediately after the initial double-quote. No newlines
768 must appear between the beginning of the form and the first double-quote.
769 For defun, defmacro, and autoload, we know how to skip over the
770 arglist, but the doc string must still have a backslash and newline
771 immediately after the double quote.
772 The only source files that must follow this convention are preloaded
773 uncompiled ones like loaddefs.el and bindings.el; aside
774 from that, it is always the .elc file that we look at, and they are no
775 problem because byte-compiler output follows this convention.
776 The NAME and DOCSTRING are output.
777 NAME is preceded by `F' for a function or `V' for a variable.
778 An entry is output only if DOCSTRING has \ newline just after the opening "
786 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
792 read_lisp_symbol (infile
, buffer
)
797 char *fillp
= buffer
;
804 *(++fillp
) = getc (infile
);
805 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
816 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
822 scan_lisp_file (filename
, mode
)
823 char *filename
, *mode
;
827 char *saved_string
= 0;
829 infile
= fopen (filename
, mode
);
833 return 0; /* No error */
837 while (!feof (infile
))
842 /* If not at end of line, skip till we get to one. */
843 if (c
!= '\n' && c
!= '\r')
848 /* Skip the line break. */
849 while (c
== '\n' || c
== '\r')
851 /* Detect a dynamic doc string and save it for the next expression. */
860 /* Read the length. */
861 while ((c
= getc (infile
),
862 c
>= '0' && c
<= '9'))
868 /* The next character is a space that is counted in the length
869 but not part of the doc string.
870 We already read it, so just ignore it. */
873 /* Read in the contents. */
874 if (saved_string
!= 0)
876 saved_string
= (char *) malloc (length
);
877 for (i
= 0; i
< length
; i
++)
878 saved_string
[i
] = getc (infile
);
879 /* The last character is a ^_.
880 That is needed in the .elc file
881 but it is redundant in DOC. So get rid of it here. */
882 saved_string
[length
- 1] = 0;
883 /* Skip the line break. */
884 while (c
== '\n' && c
== '\r')
886 /* Skip the following line. */
887 while (c
!= '\n' && c
!= '\r')
896 read_lisp_symbol (infile
, buffer
);
898 if (! strcmp (buffer
, "defun")
899 || ! strcmp (buffer
, "defmacro")
900 || ! strcmp (buffer
, "defsubst"))
903 read_lisp_symbol (infile
, buffer
);
905 /* Skip the arguments: either "nil" or a list in parens */
908 if (c
== 'n') /* nil */
910 if ((c
= getc (infile
)) != 'i'
911 || (c
= getc (infile
)) != 'l')
913 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
920 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
929 /* If the next three characters aren't `dquote bslash newline'
930 then we're not reading a docstring.
932 if ((c
= getc (infile
)) != '"'
933 || (c
= getc (infile
)) != '\\'
934 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
937 fprintf (stderr
, "## non-docstring in %s (%s)\n",
944 else if (! strcmp (buffer
, "defvar")
945 || ! strcmp (buffer
, "defconst"))
949 read_lisp_symbol (infile
, buffer
);
951 if (saved_string
== 0)
954 /* Skip until the end of line; remember two previous chars. */
955 while (c
!= '\n' && c
!= '\r' && c
>= 0)
962 /* If two previous characters were " and \,
963 this is a doc string. Otherwise, there is none. */
964 if (c2
!= '"' || c1
!= '\\')
967 fprintf (stderr
, "## non-docstring in %s (%s)\n",
975 else if (! strcmp (buffer
, "custom-declare-variable"))
982 read_lisp_symbol (infile
, buffer
);
988 "## unparsable name in custom-declare-variable in %s\n",
992 read_lisp_symbol (infile
, buffer
);
993 if (strcmp (buffer
, "quote"))
996 "## unparsable name in custom-declare-variable in %s\n",
1000 read_lisp_symbol (infile
, buffer
);
1005 "## unparsable quoted name in custom-declare-variable in %s\n",
1011 if (saved_string
== 0)
1013 /* Skip to end of line; remember the two previous chars. */
1014 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1021 /* If two previous characters were " and \,
1022 this is a doc string. Otherwise, there is none. */
1023 if (c2
!= '"' || c1
!= '\\')
1026 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1034 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1036 char c1
= 0, c2
= 0;
1041 read_lisp_symbol (infile
, buffer
);
1046 fprintf (stderr
, "## unparsable name in fset in %s\n",
1050 read_lisp_symbol (infile
, buffer
);
1051 if (strcmp (buffer
, "quote"))
1053 fprintf (stderr
, "## unparsable name in fset in %s\n",
1057 read_lisp_symbol (infile
, buffer
);
1062 "## unparsable quoted name in fset in %s\n",
1068 if (saved_string
== 0)
1070 /* Skip to end of line; remember the two previous chars. */
1071 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1078 /* If two previous characters were " and \,
1079 this is a doc string. Otherwise, there is none. */
1080 if (c2
!= '"' || c1
!= '\\')
1083 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1091 else if (! strcmp (buffer
, "autoload"))
1096 read_lisp_symbol (infile
, buffer
);
1101 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1105 read_lisp_symbol (infile
, buffer
);
1106 if (strcmp (buffer
, "quote"))
1108 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1112 read_lisp_symbol (infile
, buffer
);
1117 "## unparsable quoted name in autoload in %s\n",
1122 skip_white (infile
);
1123 if ((c
= getc (infile
)) != '\"')
1125 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1129 read_c_string_or_comment (infile
, 0, 0, 0);
1130 skip_white (infile
);
1132 if (saved_string
== 0)
1134 /* If the next three characters aren't `dquote bslash newline'
1135 then we're not reading a docstring. */
1136 if ((c
= getc (infile
)) != '"'
1137 || (c
= getc (infile
)) != '\\'
1138 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1141 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1150 else if (! strcmp (buffer
, "if")
1151 || ! strcmp (buffer
, "byte-code"))
1158 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
1164 /* At this point, we should either use the previous
1165 dynamic doc string in saved_string
1166 or gobble a doc string from the input file.
1168 In the latter case, the opening quote (and leading
1169 backslash-newline) have already been read. */
1171 putc (037, outfile
);
1172 putc (type
, outfile
);
1173 fprintf (outfile
, "%s\n", buffer
);
1176 fputs (saved_string
, outfile
);
1177 /* Don't use one dynamic doc string twice. */
1178 free (saved_string
);
1182 read_c_string_or_comment (infile
, 1, 0, 0);