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 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 3, 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 */
65 #define DIRECTORY_SEP ':'
66 #else /* not MAC_OS8 */
67 #define DIRECTORY_SEP '/'
68 #endif /* not MAC_OS8 */
71 #ifndef IS_DIRECTORY_SEP
72 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
76 int scan_lisp_file ();
80 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
81 file where that function is defined. */
89 /* Stdio stream for output to the DOC file. */
92 /* Name this program was invoked with. */
95 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
102 fprintf (stderr
, "%s: ", progname
);
103 fprintf (stderr
, s1
, s2
);
104 fprintf (stderr
, "\n");
107 /* Print error message and exit. */
118 /* Like malloc but get fatal error if memory is exhausted. */
124 void *result
= (void *) malloc (size
);
126 fatal ("virtual memory exhausted", 0);
143 /* Don't put CRs in the DOC file. */
146 #if 0 /* Suspicion is that this causes hanging.
147 So instead we require people to use -o on MSDOS. */
148 (stdout
)->_flag
&= ~_IOTEXT
;
149 _setmode (fileno (stdout
), O_BINARY
);
155 _setmode (fileno (stdout
), O_BINARY
);
156 #endif /* WINDOWSNT */
158 /* If first two args are -o FILE, output to FILE. */
160 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
162 outfile
= fopen (argv
[i
+ 1], "w");
165 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
167 outfile
= fopen (argv
[i
+ 1], "a");
170 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
177 fatal ("No output file specified", "");
180 for (; i
< argc
; i
++)
183 /* Don't process one file twice. */
184 for (j
= first_infile
; j
< i
; j
++)
185 if (! strcmp (argv
[i
], argv
[j
]))
188 err_count
+= scan_file (argv
[i
]);
190 return (err_count
> 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
193 /* Add a source file name boundary marker in the output file. */
195 put_filename (filename
)
200 for (tmp
= filename
; *tmp
; tmp
++)
202 if (IS_DIRECTORY_SEP(*tmp
))
208 fprintf (outfile
, "%s\n", filename
);
211 /* Read file FILENAME and output its doc strings to outfile. */
212 /* Return 1 if file is not found, 0 if it is found. */
218 int len
= strlen (filename
);
220 put_filename (filename
);
221 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
222 return scan_lisp_file (filename
, READ_BINARY
);
223 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
224 return scan_lisp_file (filename
, READ_TEXT
);
226 return scan_c_file (filename
, READ_TEXT
);
231 /* Some state during the execution of `read_c_string_or_comment'. */
234 /* A count of spaces and newlines that have been read, but not output. */
235 unsigned pending_spaces
, pending_newlines
;
237 /* Where we're reading from. */
240 /* If non-zero, a buffer into which to copy characters. */
242 /* If non-zero, a file into which to copy characters. */
245 /* A keyword we look for at the beginning of lines. If found, it is
246 not copied, and SAW_KEYWORD is set to true. */
248 /* The current point we've reached in an occurance of KEYWORD in
250 char *cur_keyword_ptr
;
251 /* Set to true if we saw an occurance of KEYWORD. */
255 /* Output CH to the file or buffer in STATE. Any pending newlines or
256 spaces are output first. */
261 struct rcsoc_state
*state
;
266 if (state
->pending_newlines
> 0)
268 state
->pending_newlines
--;
271 else if (state
->pending_spaces
> 0)
273 state
->pending_spaces
--;
280 putc (out_ch
, state
->out_file
);
282 *state
->buf_ptr
++ = out_ch
;
284 while (out_ch
!= ch
);
287 /* If in the middle of scanning a keyword, continue scanning with
288 character CH, otherwise output CH to the file or buffer in STATE.
289 Any pending newlines or spaces are output first, as well as any
290 previously scanned characters that were thought to be part of a
291 keyword, but were in fact not. */
294 scan_keyword_or_put_char (ch
, state
)
296 struct rcsoc_state
*state
;
299 && *state
->cur_keyword_ptr
== ch
300 && (state
->cur_keyword_ptr
> state
->keyword
301 || state
->pending_newlines
> 0))
302 /* We might be looking at STATE->keyword at some point.
303 Keep looking until we know for sure. */
305 if (*++state
->cur_keyword_ptr
== '\0')
306 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
308 state
->saw_keyword
= 1;
310 /* Reset the scanning pointer. */
311 state
->cur_keyword_ptr
= state
->keyword
;
313 /* Canonicalize whitespace preceding a usage string. */
314 state
->pending_newlines
= 2;
315 state
->pending_spaces
= 0;
317 /* Skip any whitespace between the keyword and the
320 ch
= getc (state
->in_file
);
321 while (ch
== ' ' || ch
== '\n');
323 /* Output the open-paren we just read. */
324 put_char (ch
, state
);
326 /* Skip the function name and replace it with `fn'. */
328 ch
= getc (state
->in_file
);
329 while (ch
!= ' ' && ch
!= ')');
330 put_char ('f', state
);
331 put_char ('n', state
);
333 /* Put back the last character. */
334 ungetc (ch
, state
->in_file
);
339 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
340 /* We scanned the beginning of a potential usage
341 keyword, but it was a false alarm. Output the
346 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
347 put_char (*p
, state
);
349 state
->cur_keyword_ptr
= state
->keyword
;
352 put_char (ch
, state
);
357 /* Skip a C string or C-style comment from INFILE, and return the
358 character that follows. COMMENT non-zero means skip a comment. If
359 PRINTFLAG is positive, output string contents to outfile. If it is
360 negative, store contents in buf. Convert escape sequences \n and
361 \t to newline and tab; discard \ followed by newline.
362 If SAW_USAGE is non-zero, then any occurances of the string `usage:'
363 at the beginning of a line will be removed, and *SAW_USAGE set to
364 true if any were encountered. */
367 read_c_string_or_comment (infile
, printflag
, comment
, saw_usage
)
374 struct rcsoc_state state
;
376 state
.in_file
= infile
;
377 state
.buf_ptr
= (printflag
< 0 ? buf
: 0);
378 state
.out_file
= (printflag
> 0 ? outfile
: 0);
379 state
.pending_spaces
= 0;
380 state
.pending_newlines
= 0;
381 state
.keyword
= (saw_usage
? "usage:" : 0);
382 state
.cur_keyword_ptr
= state
.keyword
;
383 state
.saw_keyword
= 0;
387 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
392 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
397 if (c
== '\n' || c
== '\r')
409 state
.pending_spaces
++;
412 state
.pending_newlines
++;
413 state
.pending_spaces
= 0;
416 scan_keyword_or_put_char (c
, &state
);
432 scan_keyword_or_put_char ('*', &state
);
439 /* If we had a "", concatenate the two strings. */
448 *saw_usage
= state
.saw_keyword
;
455 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
456 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
459 write_c_args (out
, func
, buf
, minargs
, maxargs
)
462 int minargs
, maxargs
;
469 fprintf (out
, "(fn");
474 for (p
= buf
; *p
; p
++)
479 /* Notice when we start printing a new identifier. */
480 if ((('A' <= c
&& c
<= 'Z')
481 || ('a' <= c
&& c
<= 'z')
482 || ('0' <= c
&& c
<= '9')
494 if (minargs
== 0 && maxargs
> 0)
495 fprintf (out
, "&optional ");
505 /* Print the C argument list as it would appear in lisp:
506 print underscores as hyphens, and print commas and newlines
507 as spaces. Collapse adjacent spaces into one. */
510 else if (c
== ',' || c
== '\n')
513 /* In C code, `default' is a reserved word, so we spell it
514 `defalt'; unmangle that here. */
516 && strncmp (p
, "defalt", 6) == 0
517 && ! (('A' <= p
[6] && p
[6] <= 'Z')
518 || ('a' <= p
[6] && p
[6] <= 'z')
519 || ('0' <= p
[6] && p
[6] <= '9')
522 fprintf (out
, "DEFAULT");
527 else if (c
!= ' ' || !just_spaced
)
529 if (c
>= 'a' && c
<= 'z')
530 /* Upcase the letter. */
535 just_spaced
= c
== ' ';
540 /* Read through a c file. If a .o file is named,
541 the corresponding .c file is read instead.
542 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
543 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
546 scan_c_file (filename
, mode
)
547 char *filename
, *mode
;
552 register int defunflag
;
553 register int defvarperbufferflag
;
554 register int defvarflag
;
555 int minargs
, maxargs
;
556 int extension
= filename
[strlen (filename
) - 1];
558 if (extension
== 'o')
559 filename
[strlen (filename
) - 1] = 'c';
561 infile
= fopen (filename
, mode
);
563 /* No error if non-ex input file */
570 /* Reset extension to be able to detect duplicate files. */
571 filename
[strlen (filename
) - 1] = extension
;
574 while (!feof (infile
))
578 if (c
!= '\n' && c
!= '\r')
613 defvarperbufferflag
= (c
== 'P');
626 defunflag
= c
== 'U';
628 defvarperbufferflag
= 0;
639 /* Lisp variable or function name. */
643 c
= read_c_string_or_comment (infile
, -1, 0, 0);
645 /* DEFVAR_LISP ("name", addr, "doc")
646 DEFVAR_LISP ("name", addr /\* doc *\/)
647 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
651 else if (defvarperbufferflag
)
655 else /* For DEFSIMPLE and DEFPRED */
664 if (defunflag
&& (commas
== 1 || commas
== 2))
668 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
672 if (commas
== 2) /* pick up minargs */
673 fscanf (infile
, "%d", &minargs
);
674 else /* pick up maxargs */
675 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
678 fscanf (infile
, "%d", &maxargs
);
687 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
691 c
= read_c_string_or_comment (infile
, 0, 0, 0);
693 while (c
!= EOF
&& c
!= ',' && c
!= '/')
698 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
700 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
706 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
713 && (c
= getc (infile
),
717 int comment
= c
!= '"';
721 putc (defvarflag
? 'V' : 'F', outfile
);
722 fprintf (outfile
, "%s\n", buf
);
725 getc (infile
); /* Skip past `*' */
726 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
728 /* If this is a defun, find the arguments and print them. If
729 this function takes MANY or UNEVALLED args, then the C source
730 won't give the names of the arguments, so we shouldn't bother
733 Various doc-string styles:
734 0: DEFUN (..., "DOC") (args) [!comment]
735 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
736 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
738 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
740 char argbuf
[1024], *p
= argbuf
;
742 if (!comment
|| doc_keyword
)
750 /* Skip into arguments. */
757 /* Copy arguments into ARGBUF. */
760 *p
++ = c
= getc (infile
);
764 fprintf (outfile
, "\n\n");
765 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
767 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
768 /* The DOC should provide the usage form. */
769 fprintf (stderr
, "Missing `usage' for function `%s'.\n", buf
);
777 /* Read a file of Lisp code, compiled or interpreted.
779 (defun NAME ARGS DOCSTRING ...)
780 (defmacro NAME ARGS DOCSTRING ...)
781 (defsubst NAME ARGS DOCSTRING ...)
782 (autoload (quote NAME) FILE DOCSTRING ...)
783 (defvar NAME VALUE DOCSTRING)
784 (defconst NAME VALUE DOCSTRING)
785 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
786 (fset (quote NAME) #[... DOCSTRING ...])
787 (defalias (quote NAME) #[... DOCSTRING ...])
788 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
789 starting in column zero.
790 (quote NAME) may appear as 'NAME as well.
792 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
793 When we find that, we save it for the following defining-form,
794 and we use that instead of reading a doc string within that defining-form.
796 For defvar, defconst, and fset we skip to the docstring with a kludgy
797 formatting convention: all docstrings must appear on the same line as the
798 initial open-paren (the one in column zero) and must contain a backslash
799 and a newline immediately after the initial double-quote. No newlines
800 must appear between the beginning of the form and the first double-quote.
801 For defun, defmacro, and autoload, we know how to skip over the
802 arglist, but the doc string must still have a backslash and newline
803 immediately after the double quote.
804 The only source files that must follow this convention are preloaded
805 uncompiled ones like loaddefs.el and bindings.el; aside
806 from that, it is always the .elc file that we look at, and they are no
807 problem because byte-compiler output follows this convention.
808 The NAME and DOCSTRING are output.
809 NAME is preceded by `F' for a function or `V' for a variable.
810 An entry is output only if DOCSTRING has \ newline just after the opening "
818 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
824 read_lisp_symbol (infile
, buffer
)
829 char *fillp
= buffer
;
836 *(++fillp
) = getc (infile
);
837 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
848 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
854 scan_lisp_file (filename
, mode
)
855 char *filename
, *mode
;
859 char *saved_string
= 0;
861 infile
= fopen (filename
, mode
);
865 return 0; /* No error */
869 while (!feof (infile
))
874 /* If not at end of line, skip till we get to one. */
875 if (c
!= '\n' && c
!= '\r')
880 /* Skip the line break. */
881 while (c
== '\n' || c
== '\r')
883 /* Detect a dynamic doc string and save it for the next expression. */
892 /* Read the length. */
893 while ((c
= getc (infile
),
894 c
>= '0' && c
<= '9'))
900 /* The next character is a space that is counted in the length
901 but not part of the doc string.
902 We already read it, so just ignore it. */
905 /* Read in the contents. */
906 if (saved_string
!= 0)
908 saved_string
= (char *) malloc (length
);
909 for (i
= 0; i
< length
; i
++)
910 saved_string
[i
] = getc (infile
);
911 /* The last character is a ^_.
912 That is needed in the .elc file
913 but it is redundant in DOC. So get rid of it here. */
914 saved_string
[length
- 1] = 0;
915 /* Skip the line break. */
916 while (c
== '\n' && c
== '\r')
918 /* Skip the following line. */
919 while (c
!= '\n' && c
!= '\r')
928 read_lisp_symbol (infile
, buffer
);
930 if (! strcmp (buffer
, "defun")
931 || ! strcmp (buffer
, "defmacro")
932 || ! strcmp (buffer
, "defsubst"))
935 read_lisp_symbol (infile
, buffer
);
937 /* Skip the arguments: either "nil" or a list in parens */
940 if (c
== 'n') /* nil */
942 if ((c
= getc (infile
)) != 'i'
943 || (c
= getc (infile
)) != 'l')
945 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
952 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
961 /* If the next three characters aren't `dquote bslash newline'
962 then we're not reading a docstring.
964 if ((c
= getc (infile
)) != '"'
965 || (c
= getc (infile
)) != '\\'
966 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
969 fprintf (stderr
, "## non-docstring in %s (%s)\n",
976 else if (! strcmp (buffer
, "defvar")
977 || ! strcmp (buffer
, "defconst"))
981 read_lisp_symbol (infile
, buffer
);
983 if (saved_string
== 0)
986 /* Skip until the end of line; remember two previous chars. */
987 while (c
!= '\n' && c
!= '\r' && c
>= 0)
994 /* If two previous characters were " and \,
995 this is a doc string. Otherwise, there is none. */
996 if (c2
!= '"' || c1
!= '\\')
999 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1007 else if (! strcmp (buffer
, "custom-declare-variable"))
1009 char c1
= 0, c2
= 0;
1014 read_lisp_symbol (infile
, buffer
);
1020 "## unparsable name in custom-declare-variable in %s\n",
1024 read_lisp_symbol (infile
, buffer
);
1025 if (strcmp (buffer
, "quote"))
1028 "## unparsable name in custom-declare-variable in %s\n",
1032 read_lisp_symbol (infile
, buffer
);
1037 "## unparsable quoted name in custom-declare-variable in %s\n",
1043 if (saved_string
== 0)
1045 /* Skip to end of line; remember the two previous chars. */
1046 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1053 /* If two previous characters were " and \,
1054 this is a doc string. Otherwise, there is none. */
1055 if (c2
!= '"' || c1
!= '\\')
1058 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1066 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1068 char c1
= 0, c2
= 0;
1073 read_lisp_symbol (infile
, buffer
);
1078 fprintf (stderr
, "## unparsable name in fset in %s\n",
1082 read_lisp_symbol (infile
, buffer
);
1083 if (strcmp (buffer
, "quote"))
1085 fprintf (stderr
, "## unparsable name in fset in %s\n",
1089 read_lisp_symbol (infile
, buffer
);
1094 "## unparsable quoted name in fset in %s\n",
1100 if (saved_string
== 0)
1102 /* Skip to end of line; remember the two previous chars. */
1103 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1110 /* If two previous characters were " and \,
1111 this is a doc string. Otherwise, there is none. */
1112 if (c2
!= '"' || c1
!= '\\')
1115 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1123 else if (! strcmp (buffer
, "autoload"))
1128 read_lisp_symbol (infile
, buffer
);
1133 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1137 read_lisp_symbol (infile
, buffer
);
1138 if (strcmp (buffer
, "quote"))
1140 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1144 read_lisp_symbol (infile
, buffer
);
1149 "## unparsable quoted name in autoload in %s\n",
1154 skip_white (infile
);
1155 if ((c
= getc (infile
)) != '\"')
1157 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1161 read_c_string_or_comment (infile
, 0, 0, 0);
1162 skip_white (infile
);
1164 if (saved_string
== 0)
1166 /* If the next three characters aren't `dquote bslash newline'
1167 then we're not reading a docstring. */
1168 if ((c
= getc (infile
)) != '"'
1169 || (c
= getc (infile
)) != '\\'
1170 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1173 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1182 else if (! strcmp (buffer
, "if")
1183 || ! strcmp (buffer
, "byte-code"))
1190 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
1196 /* At this point, we should either use the previous
1197 dynamic doc string in saved_string
1198 or gobble a doc string from the input file.
1200 In the latter case, the opening quote (and leading
1201 backslash-newline) have already been read. */
1203 putc (037, outfile
);
1204 putc (type
, outfile
);
1205 fprintf (outfile
, "%s\n", buffer
);
1208 fputs (saved_string
, outfile
);
1209 /* Don't use one dynamic doc string twice. */
1210 free (saved_string
);
1214 read_c_string_or_comment (infile
, 1, 0, 0);
1220 /* arch-tag: f7203aaf-991a-4238-acb5-601db56f2894
1221 (do not change this comment) */
1223 /* make-docfile.c ends here */