1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 1986, 1992, 1993, 1994, 1997, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* The arguments given to this program are all the C and Lisp source files
24 of GNU Emacs. .elc and .el and .c files are allowed.
25 A .o file can also be specified; the .c file it was made from is used.
26 This helps the makefile pass the correct list of files.
27 Option -d DIR means change to DIR before looking for files.
29 The results, which go to standard output or to a file
30 specified with -a or -o (-a to append, -o to start from nothing),
31 are entries containing function or variable names and their documentation.
32 Each entry starts with a ^_ character.
33 Then comes F for a function or V for a variable.
34 Then comes the function or variable name, terminated with a newline.
35 Then comes the documentation for that function or variable.
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 #define DIRECTORY_SEP '/'
67 #ifndef IS_DIRECTORY_SEP
68 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
72 int scan_lisp_file ();
76 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
77 file where that function is defined. */
85 /* Stdio stream for output to the DOC file. */
88 /* Name this program was invoked with. */
91 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
98 fprintf (stderr
, "%s: ", progname
);
99 fprintf (stderr
, s1
, s2
);
100 fprintf (stderr
, "\n");
103 /* Print error message and exit. */
114 /* Like malloc but get fatal error if memory is exhausted. */
120 void *result
= (void *) malloc (size
);
122 fatal ("virtual memory exhausted", 0);
139 /* Don't put CRs in the DOC file. */
142 #if 0 /* Suspicion is that this causes hanging.
143 So instead we require people to use -o on MSDOS. */
144 (stdout
)->_flag
&= ~_IOTEXT
;
145 _setmode (fileno (stdout
), O_BINARY
);
151 _setmode (fileno (stdout
), O_BINARY
);
152 #endif /* WINDOWSNT */
154 /* If first two args are -o FILE, output to FILE. */
156 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
158 outfile
= fopen (argv
[i
+ 1], "w");
161 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
163 outfile
= fopen (argv
[i
+ 1], "a");
166 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
173 fatal ("No output file specified", "");
176 for (; i
< argc
; i
++)
179 /* Don't process one file twice. */
180 for (j
= first_infile
; j
< i
; j
++)
181 if (! strcmp (argv
[i
], argv
[j
]))
184 err_count
+= scan_file (argv
[i
]);
186 return (err_count
> 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
189 /* Add a source file name boundary marker in the output file. */
191 put_filename (filename
)
196 for (tmp
= filename
; *tmp
; tmp
++)
198 if (IS_DIRECTORY_SEP(*tmp
))
204 fprintf (outfile
, "%s\n", filename
);
207 /* Read file FILENAME and output its doc strings to outfile. */
208 /* Return 1 if file is not found, 0 if it is found. */
214 int len
= strlen (filename
);
216 put_filename (filename
);
217 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
218 return scan_lisp_file (filename
, READ_BINARY
);
219 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
220 return scan_lisp_file (filename
, READ_TEXT
);
222 return scan_c_file (filename
, READ_TEXT
);
227 /* Some state during the execution of `read_c_string_or_comment'. */
230 /* A count of spaces and newlines that have been read, but not output. */
231 unsigned pending_spaces
, pending_newlines
;
233 /* Where we're reading from. */
236 /* If non-zero, a buffer into which to copy characters. */
238 /* If non-zero, a file into which to copy characters. */
241 /* A keyword we look for at the beginning of lines. If found, it is
242 not copied, and SAW_KEYWORD is set to true. */
244 /* The current point we've reached in an occurance of KEYWORD in
246 char *cur_keyword_ptr
;
247 /* Set to true if we saw an occurance of KEYWORD. */
251 /* Output CH to the file or buffer in STATE. Any pending newlines or
252 spaces are output first. */
257 struct rcsoc_state
*state
;
262 if (state
->pending_newlines
> 0)
264 state
->pending_newlines
--;
267 else if (state
->pending_spaces
> 0)
269 state
->pending_spaces
--;
276 putc (out_ch
, state
->out_file
);
278 *state
->buf_ptr
++ = out_ch
;
280 while (out_ch
!= ch
);
283 /* If in the middle of scanning a keyword, continue scanning with
284 character CH, otherwise output CH to the file or buffer in STATE.
285 Any pending newlines or spaces are output first, as well as any
286 previously scanned characters that were thought to be part of a
287 keyword, but were in fact not. */
290 scan_keyword_or_put_char (ch
, state
)
292 struct rcsoc_state
*state
;
295 && *state
->cur_keyword_ptr
== ch
296 && (state
->cur_keyword_ptr
> state
->keyword
297 || state
->pending_newlines
> 0))
298 /* We might be looking at STATE->keyword at some point.
299 Keep looking until we know for sure. */
301 if (*++state
->cur_keyword_ptr
== '\0')
302 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
304 state
->saw_keyword
= 1;
306 /* Reset the scanning pointer. */
307 state
->cur_keyword_ptr
= state
->keyword
;
309 /* Canonicalize whitespace preceding a usage string. */
310 state
->pending_newlines
= 2;
311 state
->pending_spaces
= 0;
313 /* Skip any whitespace between the keyword and the
316 ch
= getc (state
->in_file
);
317 while (ch
== ' ' || ch
== '\n');
319 /* Output the open-paren we just read. */
320 put_char (ch
, state
);
322 /* Skip the function name and replace it with `fn'. */
324 ch
= getc (state
->in_file
);
325 while (ch
!= ' ' && ch
!= ')');
326 put_char ('f', state
);
327 put_char ('n', state
);
329 /* Put back the last character. */
330 ungetc (ch
, state
->in_file
);
335 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
336 /* We scanned the beginning of a potential usage
337 keyword, but it was a false alarm. Output the
342 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
343 put_char (*p
, state
);
345 state
->cur_keyword_ptr
= state
->keyword
;
348 put_char (ch
, state
);
353 /* Skip a C string or C-style comment from INFILE, and return the
354 character that follows. COMMENT non-zero means skip a comment. If
355 PRINTFLAG is positive, output string contents to outfile. If it is
356 negative, store contents in buf. Convert escape sequences \n and
357 \t to newline and tab; discard \ followed by newline.
358 If SAW_USAGE is non-zero, then any occurances of the string `usage:'
359 at the beginning of a line will be removed, and *SAW_USAGE set to
360 true if any were encountered. */
363 read_c_string_or_comment (infile
, printflag
, comment
, saw_usage
)
370 struct rcsoc_state state
;
372 state
.in_file
= infile
;
373 state
.buf_ptr
= (printflag
< 0 ? buf
: 0);
374 state
.out_file
= (printflag
> 0 ? outfile
: 0);
375 state
.pending_spaces
= 0;
376 state
.pending_newlines
= 0;
377 state
.keyword
= (saw_usage
? "usage:" : 0);
378 state
.cur_keyword_ptr
= state
.keyword
;
379 state
.saw_keyword
= 0;
383 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
388 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
393 if (c
== '\n' || c
== '\r')
405 state
.pending_spaces
++;
408 state
.pending_newlines
++;
409 state
.pending_spaces
= 0;
412 scan_keyword_or_put_char (c
, &state
);
428 scan_keyword_or_put_char ('*', &state
);
435 /* If we had a "", concatenate the two strings. */
444 *saw_usage
= state
.saw_keyword
;
451 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
452 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
455 write_c_args (out
, func
, buf
, minargs
, maxargs
)
458 int minargs
, maxargs
;
465 fprintf (out
, "(fn");
470 for (p
= buf
; *p
; p
++)
475 /* Notice when we start printing a new identifier. */
476 if ((('A' <= c
&& c
<= 'Z')
477 || ('a' <= c
&& c
<= 'z')
478 || ('0' <= c
&& c
<= '9')
490 if (minargs
== 0 && maxargs
> 0)
491 fprintf (out
, "&optional ");
501 /* Print the C argument list as it would appear in lisp:
502 print underscores as hyphens, and print commas and newlines
503 as spaces. Collapse adjacent spaces into one. */
506 else if (c
== ',' || c
== '\n')
509 /* In C code, `default' is a reserved word, so we spell it
510 `defalt'; unmangle that here. */
512 && strncmp (p
, "defalt", 6) == 0
513 && ! (('A' <= p
[6] && p
[6] <= 'Z')
514 || ('a' <= p
[6] && p
[6] <= 'z')
515 || ('0' <= p
[6] && p
[6] <= '9')
518 fprintf (out
, "DEFAULT");
523 else if (c
!= ' ' || !just_spaced
)
525 if (c
>= 'a' && c
<= 'z')
526 /* Upcase the letter. */
531 just_spaced
= c
== ' ';
536 /* Read through a c file. If a .o file is named,
537 the corresponding .c file is read instead.
538 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
539 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
542 scan_c_file (filename
, mode
)
543 char *filename
, *mode
;
548 register int defunflag
;
549 register int defvarperbufferflag
;
550 register int defvarflag
;
551 int minargs
, maxargs
;
552 int extension
= filename
[strlen (filename
) - 1];
554 if (extension
== 'o')
555 filename
[strlen (filename
) - 1] = 'c';
557 infile
= fopen (filename
, mode
);
559 /* No error if non-ex input file */
566 /* Reset extension to be able to detect duplicate files. */
567 filename
[strlen (filename
) - 1] = extension
;
570 while (!feof (infile
))
574 if (c
!= '\n' && c
!= '\r')
609 defvarperbufferflag
= (c
== 'P');
622 defunflag
= c
== 'U';
624 defvarperbufferflag
= 0;
635 /* Lisp variable or function name. */
639 c
= read_c_string_or_comment (infile
, -1, 0, 0);
641 /* DEFVAR_LISP ("name", addr, "doc")
642 DEFVAR_LISP ("name", addr /\* doc *\/)
643 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
647 else if (defvarperbufferflag
)
651 else /* For DEFSIMPLE and DEFPRED */
660 if (defunflag
&& (commas
== 1 || commas
== 2))
664 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
668 if (commas
== 2) /* pick up minargs */
669 fscanf (infile
, "%d", &minargs
);
670 else /* pick up maxargs */
671 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
674 fscanf (infile
, "%d", &maxargs
);
683 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
687 c
= read_c_string_or_comment (infile
, 0, 0, 0);
689 while (c
!= EOF
&& c
!= ',' && c
!= '/')
694 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
696 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
702 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
709 && (c
= getc (infile
),
713 int comment
= c
!= '"';
717 putc (defvarflag
? 'V' : 'F', outfile
);
718 fprintf (outfile
, "%s\n", buf
);
721 getc (infile
); /* Skip past `*' */
722 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
724 /* If this is a defun, find the arguments and print them. If
725 this function takes MANY or UNEVALLED args, then the C source
726 won't give the names of the arguments, so we shouldn't bother
729 Various doc-string styles:
730 0: DEFUN (..., "DOC") (args) [!comment]
731 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
732 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
734 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
736 char argbuf
[1024], *p
= argbuf
;
738 if (!comment
|| doc_keyword
)
746 /* Skip into arguments. */
753 /* Copy arguments into ARGBUF. */
756 *p
++ = c
= getc (infile
);
760 fprintf (outfile
, "\n\n");
761 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
763 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
764 /* The DOC should provide the usage form. */
765 fprintf (stderr
, "Missing `usage' for function `%s'.\n", buf
);
773 /* Read a file of Lisp code, compiled or interpreted.
775 (defun NAME ARGS DOCSTRING ...)
776 (defmacro NAME ARGS DOCSTRING ...)
777 (defsubst NAME ARGS DOCSTRING ...)
778 (autoload (quote NAME) FILE DOCSTRING ...)
779 (defvar NAME VALUE DOCSTRING)
780 (defconst NAME VALUE DOCSTRING)
781 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
782 (fset (quote NAME) #[... DOCSTRING ...])
783 (defalias (quote NAME) #[... DOCSTRING ...])
784 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
785 starting in column zero.
786 (quote NAME) may appear as 'NAME as well.
788 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
789 When we find that, we save it for the following defining-form,
790 and we use that instead of reading a doc string within that defining-form.
792 For defvar, defconst, and fset we skip to the docstring with a kludgy
793 formatting convention: all docstrings must appear on the same line as the
794 initial open-paren (the one in column zero) and must contain a backslash
795 and a newline immediately after the initial double-quote. No newlines
796 must appear between the beginning of the form and the first double-quote.
797 For defun, defmacro, and autoload, we know how to skip over the
798 arglist, but the doc string must still have a backslash and newline
799 immediately after the double quote.
800 The only source files that must follow this convention are preloaded
801 uncompiled ones like loaddefs.el and bindings.el; aside
802 from that, it is always the .elc file that we look at, and they are no
803 problem because byte-compiler output follows this convention.
804 The NAME and DOCSTRING are output.
805 NAME is preceded by `F' for a function or `V' for a variable.
806 An entry is output only if DOCSTRING has \ newline just after the opening "
814 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
820 read_lisp_symbol (infile
, buffer
)
825 char *fillp
= buffer
;
832 *(++fillp
) = getc (infile
);
833 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
844 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
850 scan_lisp_file (filename
, mode
)
851 char *filename
, *mode
;
855 char *saved_string
= 0;
857 infile
= fopen (filename
, mode
);
861 return 0; /* No error */
865 while (!feof (infile
))
870 /* If not at end of line, skip till we get to one. */
871 if (c
!= '\n' && c
!= '\r')
876 /* Skip the line break. */
877 while (c
== '\n' || c
== '\r')
879 /* Detect a dynamic doc string and save it for the next expression. */
888 /* Read the length. */
889 while ((c
= getc (infile
),
890 c
>= '0' && c
<= '9'))
896 /* The next character is a space that is counted in the length
897 but not part of the doc string.
898 We already read it, so just ignore it. */
901 /* Read in the contents. */
902 if (saved_string
!= 0)
904 saved_string
= (char *) malloc (length
);
905 for (i
= 0; i
< length
; i
++)
906 saved_string
[i
] = getc (infile
);
907 /* The last character is a ^_.
908 That is needed in the .elc file
909 but it is redundant in DOC. So get rid of it here. */
910 saved_string
[length
- 1] = 0;
911 /* Skip the line break. */
912 while (c
== '\n' && c
== '\r')
914 /* Skip the following line. */
915 while (c
!= '\n' && c
!= '\r')
924 read_lisp_symbol (infile
, buffer
);
926 if (! strcmp (buffer
, "defun")
927 || ! strcmp (buffer
, "defmacro")
928 || ! strcmp (buffer
, "defsubst"))
931 read_lisp_symbol (infile
, buffer
);
933 /* Skip the arguments: either "nil" or a list in parens */
936 if (c
== 'n') /* nil */
938 if ((c
= getc (infile
)) != 'i'
939 || (c
= getc (infile
)) != 'l')
941 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
948 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
957 /* If the next three characters aren't `dquote bslash newline'
958 then we're not reading a docstring.
960 if ((c
= getc (infile
)) != '"'
961 || (c
= getc (infile
)) != '\\'
962 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
965 fprintf (stderr
, "## non-docstring in %s (%s)\n",
972 else if (! strcmp (buffer
, "defvar")
973 || ! strcmp (buffer
, "defconst"))
977 read_lisp_symbol (infile
, buffer
);
979 if (saved_string
== 0)
982 /* Skip until the end of line; remember two previous chars. */
983 while (c
!= '\n' && c
!= '\r' && c
>= 0)
990 /* If two previous characters were " and \,
991 this is a doc string. Otherwise, there is none. */
992 if (c2
!= '"' || c1
!= '\\')
995 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1003 else if (! strcmp (buffer
, "custom-declare-variable"))
1005 char c1
= 0, c2
= 0;
1010 read_lisp_symbol (infile
, buffer
);
1016 "## unparsable name in custom-declare-variable in %s\n",
1020 read_lisp_symbol (infile
, buffer
);
1021 if (strcmp (buffer
, "quote"))
1024 "## unparsable name in custom-declare-variable in %s\n",
1028 read_lisp_symbol (infile
, buffer
);
1033 "## unparsable quoted name in custom-declare-variable in %s\n",
1039 if (saved_string
== 0)
1041 /* Skip to end of line; remember the two previous chars. */
1042 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1049 /* If two previous characters were " and \,
1050 this is a doc string. Otherwise, there is none. */
1051 if (c2
!= '"' || c1
!= '\\')
1054 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1062 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1064 char c1
= 0, c2
= 0;
1069 read_lisp_symbol (infile
, buffer
);
1074 fprintf (stderr
, "## unparsable name in fset in %s\n",
1078 read_lisp_symbol (infile
, buffer
);
1079 if (strcmp (buffer
, "quote"))
1081 fprintf (stderr
, "## unparsable name in fset in %s\n",
1085 read_lisp_symbol (infile
, buffer
);
1090 "## unparsable quoted name in fset in %s\n",
1096 if (saved_string
== 0)
1098 /* Skip to end of line; remember the two previous chars. */
1099 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1106 /* If two previous characters were " and \,
1107 this is a doc string. Otherwise, there is none. */
1108 if (c2
!= '"' || c1
!= '\\')
1111 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1119 else if (! strcmp (buffer
, "autoload"))
1124 read_lisp_symbol (infile
, buffer
);
1129 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1133 read_lisp_symbol (infile
, buffer
);
1134 if (strcmp (buffer
, "quote"))
1136 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1140 read_lisp_symbol (infile
, buffer
);
1145 "## unparsable quoted name in autoload in %s\n",
1150 skip_white (infile
);
1151 if ((c
= getc (infile
)) != '\"')
1153 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1157 read_c_string_or_comment (infile
, 0, 0, 0);
1158 skip_white (infile
);
1160 if (saved_string
== 0)
1162 /* If the next three characters aren't `dquote bslash newline'
1163 then we're not reading a docstring. */
1164 if ((c
= getc (infile
)) != '"'
1165 || (c
= getc (infile
)) != '\\'
1166 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1169 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1178 else if (! strcmp (buffer
, "if")
1179 || ! strcmp (buffer
, "byte-code"))
1186 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
1192 /* At this point, we should either use the previous
1193 dynamic doc string in saved_string
1194 or gobble a doc string from the input file.
1196 In the latter case, the opening quote (and leading
1197 backslash-newline) have already been read. */
1199 putc (037, outfile
);
1200 putc (type
, outfile
);
1201 fprintf (outfile
, "%s\n", buffer
);
1204 fputs (saved_string
, outfile
);
1205 /* Don't use one dynamic doc string twice. */
1206 free (saved_string
);
1210 read_c_string_or_comment (infile
, 1, 0, 0);
1216 /* arch-tag: f7203aaf-991a-4238-acb5-601db56f2894
1217 (do not change this comment) */
1219 /* make-docfile.c ends here */