1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 86, 92, 93, 94, 97, 1999, 2000, 01, 2004
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 */
63 #ifndef IS_DIRECTORY_SEP
64 #define IS_DIRECTORY_SEP(_c_) ((_c_) == '/')
68 int scan_lisp_file ();
72 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
73 file where that function is defined. */
81 /* Stdio stream for output to the DOC file. */
84 /* Name this program was invoked with. */
87 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
94 fprintf (stderr
, "%s: ", progname
);
95 fprintf (stderr
, s1
, s2
);
96 fprintf (stderr
, "\n");
99 /* Print error message and exit. */
110 /* Like malloc but get fatal error if memory is exhausted. */
116 void *result
= (void *) malloc (size
);
118 fatal ("virtual memory exhausted", 0);
135 /* Don't put CRs in the DOC file. */
138 #if 0 /* Suspicion is that this causes hanging.
139 So instead we require people to use -o on MSDOS. */
140 (stdout
)->_flag
&= ~_IOTEXT
;
141 _setmode (fileno (stdout
), O_BINARY
);
147 _setmode (fileno (stdout
), O_BINARY
);
148 #endif /* WINDOWSNT */
150 /* If first two args are -o FILE, output to FILE. */
152 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
154 outfile
= fopen (argv
[i
+ 1], "w");
157 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
159 outfile
= fopen (argv
[i
+ 1], "a");
162 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
169 fatal ("No output file specified", "");
172 for (; i
< argc
; i
++)
175 /* Don't process one file twice. */
176 for (j
= first_infile
; j
< i
; j
++)
177 if (! strcmp (argv
[i
], argv
[j
]))
180 err_count
+= scan_file (argv
[i
]);
182 return (err_count
> 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
185 /* Add a source file name boundary marker in the output file. */
187 put_filename (filename
)
192 for (tmp
= filename
; *tmp
; tmp
++)
194 if (IS_DIRECTORY_SEP(*tmp
))
200 fprintf (outfile
, "%s\n", filename
);
203 /* Read file FILENAME and output its doc strings to outfile. */
204 /* Return 1 if file is not found, 0 if it is found. */
210 int len
= strlen (filename
);
212 put_filename (filename
);
213 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
214 return scan_lisp_file (filename
, READ_BINARY
);
215 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
216 return scan_lisp_file (filename
, READ_TEXT
);
218 return scan_c_file (filename
, READ_TEXT
);
223 /* Some state during the execution of `read_c_string_or_comment'. */
226 /* A count of spaces and newlines that have been read, but not output. */
227 unsigned pending_spaces
, pending_newlines
;
229 /* Where we're reading from. */
232 /* If non-zero, a buffer into which to copy characters. */
234 /* If non-zero, a file into which to copy characters. */
237 /* A keyword we look for at the beginning of lines. If found, it is
238 not copied, and SAW_KEYWORD is set to true. */
240 /* The current point we've reached in an occurance of KEYWORD in
242 char *cur_keyword_ptr
;
243 /* Set to true if we saw an occurance of KEYWORD. */
247 /* Output CH to the file or buffer in STATE. Any pending newlines or
248 spaces are output first. */
253 struct rcsoc_state
*state
;
258 if (state
->pending_newlines
> 0)
260 state
->pending_newlines
--;
263 else if (state
->pending_spaces
> 0)
265 state
->pending_spaces
--;
272 putc (out_ch
, state
->out_file
);
274 *state
->buf_ptr
++ = out_ch
;
276 while (out_ch
!= ch
);
279 /* If in the middle of scanning a keyword, continue scanning with
280 character CH, otherwise output CH to the file or buffer in STATE.
281 Any pending newlines or spaces are output first, as well as any
282 previously scanned characters that were thought to be part of a
283 keyword, but were in fact not. */
286 scan_keyword_or_put_char (ch
, state
)
288 struct rcsoc_state
*state
;
291 && *state
->cur_keyword_ptr
== ch
292 && (state
->cur_keyword_ptr
> state
->keyword
293 || state
->pending_newlines
> 0))
294 /* We might be looking at STATE->keyword at some point.
295 Keep looking until we know for sure. */
297 if (*++state
->cur_keyword_ptr
== '\0')
298 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
300 state
->saw_keyword
= 1;
302 /* Reset the scanning pointer. */
303 state
->cur_keyword_ptr
= state
->keyword
;
305 /* Canonicalize whitespace preceding a usage string. */
306 state
->pending_newlines
= 2;
307 state
->pending_spaces
= 0;
309 /* Skip any whitespace between the keyword and the
312 ch
= getc (state
->in_file
);
313 while (ch
== ' ' || ch
== '\n');
315 /* Output the open-paren we just read. */
316 put_char (ch
, state
);
318 /* Skip the function name and replace it with `fn'. */
320 ch
= getc (state
->in_file
);
321 while (ch
!= ' ' && ch
!= ')');
322 put_char ('f', state
);
323 put_char ('n', state
);
325 /* Put back the last character. */
326 ungetc (ch
, state
->in_file
);
331 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
332 /* We scanned the beginning of a potential usage
333 keyword, but it was a false alarm. Output the
338 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
339 put_char (*p
, state
);
341 state
->cur_keyword_ptr
= state
->keyword
;
344 put_char (ch
, state
);
349 /* Skip a C string or C-style comment from INFILE, and return the
350 character that follows. COMMENT non-zero means skip a comment. If
351 PRINTFLAG is positive, output string contents to outfile. If it is
352 negative, store contents in buf. Convert escape sequences \n and
353 \t to newline and tab; discard \ followed by newline.
354 If SAW_USAGE is non-zero, then any occurances of the string `usage:'
355 at the beginning of a line will be removed, and *SAW_USAGE set to
356 true if any were encountered. */
359 read_c_string_or_comment (infile
, printflag
, comment
, saw_usage
)
366 struct rcsoc_state state
;
368 state
.in_file
= infile
;
369 state
.buf_ptr
= (printflag
< 0 ? buf
: 0);
370 state
.out_file
= (printflag
> 0 ? outfile
: 0);
371 state
.pending_spaces
= 0;
372 state
.pending_newlines
= 0;
373 state
.keyword
= (saw_usage
? "usage:" : 0);
374 state
.cur_keyword_ptr
= state
.keyword
;
375 state
.saw_keyword
= 0;
379 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
384 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
389 if (c
== '\n' || c
== '\r')
401 state
.pending_spaces
++;
404 state
.pending_newlines
++;
405 state
.pending_spaces
= 0;
408 scan_keyword_or_put_char (c
, &state
);
424 scan_keyword_or_put_char ('*', &state
);
431 /* If we had a "", concatenate the two strings. */
440 *saw_usage
= state
.saw_keyword
;
447 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
448 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
451 write_c_args (out
, func
, buf
, minargs
, maxargs
)
454 int minargs
, maxargs
;
461 fprintf (out
, "(fn");
466 for (p
= buf
; *p
; p
++)
471 /* Notice when we start printing a new identifier. */
472 if ((('A' <= c
&& c
<= 'Z')
473 || ('a' <= c
&& c
<= 'z')
474 || ('0' <= c
&& c
<= '9')
486 if (minargs
== 0 && maxargs
> 0)
487 fprintf (out
, "&optional ");
497 /* Print the C argument list as it would appear in lisp:
498 print underscores as hyphens, and print commas and newlines
499 as spaces. Collapse adjacent spaces into one. */
502 else if (c
== ',' || c
== '\n')
505 /* In C code, `default' is a reserved word, so we spell it
506 `defalt'; unmangle that here. */
508 && strncmp (p
, "defalt", 6) == 0
509 && ! (('A' <= p
[6] && p
[6] <= 'Z')
510 || ('a' <= p
[6] && p
[6] <= 'z')
511 || ('0' <= p
[6] && p
[6] <= '9')
514 fprintf (out
, "DEFAULT");
519 else if (c
!= ' ' || !just_spaced
)
521 if (c
>= 'a' && c
<= 'z')
522 /* Upcase the letter. */
527 just_spaced
= c
== ' ';
532 /* Read through a c file. If a .o file is named,
533 the corresponding .c file is read instead.
534 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
535 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
538 scan_c_file (filename
, mode
)
539 char *filename
, *mode
;
544 register int defunflag
;
545 register int defvarperbufferflag
;
546 register int defvarflag
;
547 int minargs
, maxargs
;
548 int extension
= filename
[strlen (filename
) - 1];
550 if (extension
== 'o')
551 filename
[strlen (filename
) - 1] = 'c';
553 infile
= fopen (filename
, mode
);
555 /* No error if non-ex input file */
562 /* Reset extension to be able to detect duplicate files. */
563 filename
[strlen (filename
) - 1] = extension
;
566 while (!feof (infile
))
570 if (c
!= '\n' && c
!= '\r')
605 defvarperbufferflag
= (c
== 'P');
618 defunflag
= c
== 'U';
630 /* Lisp variable or function name. */
634 c
= read_c_string_or_comment (infile
, -1, 0, 0);
636 /* DEFVAR_LISP ("name", addr, "doc")
637 DEFVAR_LISP ("name", addr /\* doc *\/)
638 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
642 else if (defvarperbufferflag
)
646 else /* For DEFSIMPLE and DEFPRED */
655 if (defunflag
&& (commas
== 1 || commas
== 2))
659 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
663 if (commas
== 2) /* pick up minargs */
664 fscanf (infile
, "%d", &minargs
);
665 else /* pick up maxargs */
666 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
669 fscanf (infile
, "%d", &maxargs
);
678 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
682 c
= read_c_string_or_comment (infile
, 0, 0, 0);
684 while (c
!= EOF
&& c
!= ',' && c
!= '/')
689 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
691 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
697 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
704 && (c
= getc (infile
),
708 int comment
= c
!= '"';
712 putc (defvarflag
? 'V' : 'F', outfile
);
713 fprintf (outfile
, "%s\n", buf
);
716 getc (infile
); /* Skip past `*' */
717 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
719 /* If this is a defun, find the arguments and print them. If
720 this function takes MANY or UNEVALLED args, then the C source
721 won't give the names of the arguments, so we shouldn't bother
724 Various doc-string styles:
725 0: DEFUN (..., "DOC") (args) [!comment]
726 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
727 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
729 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
731 char argbuf
[1024], *p
= argbuf
;
733 if (!comment
|| doc_keyword
)
741 /* Skip into arguments. */
748 /* Copy arguments into ARGBUF. */
751 *p
++ = c
= getc (infile
);
755 fprintf (outfile
, "\n\n");
756 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
758 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
759 /* The DOC should provide the usage form. */
760 fprintf (stderr
, "Missing `usage' for function `%s'.\n", buf
);
768 /* Read a file of Lisp code, compiled or interpreted.
770 (defun NAME ARGS DOCSTRING ...)
771 (defmacro NAME ARGS DOCSTRING ...)
772 (defsubst NAME ARGS DOCSTRING ...)
773 (autoload (quote NAME) FILE DOCSTRING ...)
774 (defvar NAME VALUE DOCSTRING)
775 (defconst NAME VALUE DOCSTRING)
776 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
777 (fset (quote NAME) #[... DOCSTRING ...])
778 (defalias (quote NAME) #[... DOCSTRING ...])
779 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
780 starting in column zero.
781 (quote NAME) may appear as 'NAME as well.
783 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
784 When we find that, we save it for the following defining-form,
785 and we use that instead of reading a doc string within that defining-form.
787 For defvar, defconst, and fset we skip to the docstring with a kludgy
788 formatting convention: all docstrings must appear on the same line as the
789 initial open-paren (the one in column zero) and must contain a backslash
790 and a newline immediately after the initial double-quote. No newlines
791 must appear between the beginning of the form and the first double-quote.
792 For defun, defmacro, and autoload, we know how to skip over the
793 arglist, but the doc string must still have a backslash and newline
794 immediately after the double quote.
795 The only source files that must follow this convention are preloaded
796 uncompiled ones like loaddefs.el and bindings.el; aside
797 from that, it is always the .elc file that we look at, and they are no
798 problem because byte-compiler output follows this convention.
799 The NAME and DOCSTRING are output.
800 NAME is preceded by `F' for a function or `V' for a variable.
801 An entry is output only if DOCSTRING has \ newline just after the opening "
809 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
815 read_lisp_symbol (infile
, buffer
)
820 char *fillp
= buffer
;
827 *(++fillp
) = getc (infile
);
828 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
839 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
845 scan_lisp_file (filename
, mode
)
846 char *filename
, *mode
;
850 char *saved_string
= 0;
852 infile
= fopen (filename
, mode
);
856 return 0; /* No error */
860 while (!feof (infile
))
865 /* If not at end of line, skip till we get to one. */
866 if (c
!= '\n' && c
!= '\r')
871 /* Skip the line break. */
872 while (c
== '\n' || c
== '\r')
874 /* Detect a dynamic doc string and save it for the next expression. */
883 /* Read the length. */
884 while ((c
= getc (infile
),
885 c
>= '0' && c
<= '9'))
891 /* The next character is a space that is counted in the length
892 but not part of the doc string.
893 We already read it, so just ignore it. */
896 /* Read in the contents. */
897 if (saved_string
!= 0)
899 saved_string
= (char *) malloc (length
);
900 for (i
= 0; i
< length
; i
++)
901 saved_string
[i
] = getc (infile
);
902 /* The last character is a ^_.
903 That is needed in the .elc file
904 but it is redundant in DOC. So get rid of it here. */
905 saved_string
[length
- 1] = 0;
906 /* Skip the line break. */
907 while (c
== '\n' && c
== '\r')
909 /* Skip the following line. */
910 while (c
!= '\n' && c
!= '\r')
919 read_lisp_symbol (infile
, buffer
);
921 if (! strcmp (buffer
, "defun")
922 || ! strcmp (buffer
, "defmacro")
923 || ! strcmp (buffer
, "defsubst"))
926 read_lisp_symbol (infile
, buffer
);
928 /* Skip the arguments: either "nil" or a list in parens */
931 if (c
== 'n') /* nil */
933 if ((c
= getc (infile
)) != 'i'
934 || (c
= getc (infile
)) != 'l')
936 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
943 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
952 /* If the next three characters aren't `dquote bslash newline'
953 then we're not reading a docstring.
955 if ((c
= getc (infile
)) != '"'
956 || (c
= getc (infile
)) != '\\'
957 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
960 fprintf (stderr
, "## non-docstring in %s (%s)\n",
967 else if (! strcmp (buffer
, "defvar")
968 || ! strcmp (buffer
, "defconst"))
972 read_lisp_symbol (infile
, buffer
);
974 if (saved_string
== 0)
977 /* Skip until the end of line; remember two previous chars. */
978 while (c
!= '\n' && c
!= '\r' && c
>= 0)
985 /* If two previous characters were " and \,
986 this is a doc string. Otherwise, there is none. */
987 if (c2
!= '"' || c1
!= '\\')
990 fprintf (stderr
, "## non-docstring in %s (%s)\n",
998 else if (! strcmp (buffer
, "custom-declare-variable"))
1000 char c1
= 0, c2
= 0;
1005 read_lisp_symbol (infile
, buffer
);
1011 "## unparsable name in custom-declare-variable in %s\n",
1015 read_lisp_symbol (infile
, buffer
);
1016 if (strcmp (buffer
, "quote"))
1019 "## unparsable name in custom-declare-variable in %s\n",
1023 read_lisp_symbol (infile
, buffer
);
1028 "## unparsable quoted name in custom-declare-variable in %s\n",
1034 if (saved_string
== 0)
1036 /* Skip to end of line; remember the two previous chars. */
1037 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1044 /* If two previous characters were " and \,
1045 this is a doc string. Otherwise, there is none. */
1046 if (c2
!= '"' || c1
!= '\\')
1049 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1057 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1059 char c1
= 0, c2
= 0;
1064 read_lisp_symbol (infile
, buffer
);
1069 fprintf (stderr
, "## unparsable name in fset in %s\n",
1073 read_lisp_symbol (infile
, buffer
);
1074 if (strcmp (buffer
, "quote"))
1076 fprintf (stderr
, "## unparsable name in fset in %s\n",
1080 read_lisp_symbol (infile
, buffer
);
1085 "## unparsable quoted name in fset in %s\n",
1091 if (saved_string
== 0)
1093 /* Skip to end of line; remember the two previous chars. */
1094 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1101 /* If two previous characters were " and \,
1102 this is a doc string. Otherwise, there is none. */
1103 if (c2
!= '"' || c1
!= '\\')
1106 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1114 else if (! strcmp (buffer
, "autoload"))
1119 read_lisp_symbol (infile
, buffer
);
1124 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1128 read_lisp_symbol (infile
, buffer
);
1129 if (strcmp (buffer
, "quote"))
1131 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1135 read_lisp_symbol (infile
, buffer
);
1140 "## unparsable quoted name in autoload in %s\n",
1145 skip_white (infile
);
1146 if ((c
= getc (infile
)) != '\"')
1148 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1152 read_c_string_or_comment (infile
, 0, 0, 0);
1153 skip_white (infile
);
1155 if (saved_string
== 0)
1157 /* If the next three characters aren't `dquote bslash newline'
1158 then we're not reading a docstring. */
1159 if ((c
= getc (infile
)) != '"'
1160 || (c
= getc (infile
)) != '\\'
1161 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1164 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1173 else if (! strcmp (buffer
, "if")
1174 || ! strcmp (buffer
, "byte-code"))
1181 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
1187 /* At this point, we should either use the previous
1188 dynamic doc string in saved_string
1189 or gobble a doc string from the input file.
1191 In the latter case, the opening quote (and leading
1192 backslash-newline) have already been read. */
1194 putc (037, outfile
);
1195 putc (type
, outfile
);
1196 fprintf (outfile
, "%s\n", buffer
);
1199 fputs (saved_string
, outfile
);
1200 /* Don't use one dynamic doc string twice. */
1201 free (saved_string
);
1205 read_c_string_or_comment (infile
, 1, 0, 0);
1211 /* arch-tag: f7203aaf-991a-4238-acb5-601db56f2894
1212 (do not change this comment) */
1214 /* make-docfile.c ends here */