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 of the License, or
11 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
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.
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 #define DIRECTORY_SEP '/'
66 #ifndef IS_DIRECTORY_SEP
67 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
71 int scan_lisp_file ();
75 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
76 file where that function is defined. */
84 /* Stdio stream for output to the DOC file. */
87 /* Name this program was invoked with. */
90 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
97 fprintf (stderr
, "%s: ", progname
);
98 fprintf (stderr
, s1
, s2
);
99 fprintf (stderr
, "\n");
102 /* Print error message and exit. */
113 /* Like malloc but get fatal error if memory is exhausted. */
119 void *result
= (void *) malloc (size
);
121 fatal ("virtual memory exhausted", 0);
138 /* Don't put CRs in the DOC file. */
141 #if 0 /* Suspicion is that this causes hanging.
142 So instead we require people to use -o on MSDOS. */
143 (stdout
)->_flag
&= ~_IOTEXT
;
144 _setmode (fileno (stdout
), O_BINARY
);
150 _setmode (fileno (stdout
), O_BINARY
);
151 #endif /* WINDOWSNT */
153 /* If first two args are -o FILE, output to FILE. */
155 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
157 outfile
= fopen (argv
[i
+ 1], "w");
160 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
162 outfile
= fopen (argv
[i
+ 1], "a");
165 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
172 fatal ("No output file specified", "");
175 for (; i
< argc
; i
++)
178 /* Don't process one file twice. */
179 for (j
= first_infile
; j
< i
; j
++)
180 if (! strcmp (argv
[i
], argv
[j
]))
183 err_count
+= scan_file (argv
[i
]);
185 return (err_count
> 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
188 /* Add a source file name boundary marker in the output file. */
190 put_filename (filename
)
195 for (tmp
= filename
; *tmp
; tmp
++)
197 if (IS_DIRECTORY_SEP(*tmp
))
203 fprintf (outfile
, "%s\n", filename
);
206 /* Read file FILENAME and output its doc strings to outfile. */
207 /* Return 1 if file is not found, 0 if it is found. */
213 int len
= strlen (filename
);
215 put_filename (filename
);
216 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
217 return scan_lisp_file (filename
, READ_BINARY
);
218 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
219 return scan_lisp_file (filename
, READ_TEXT
);
221 return scan_c_file (filename
, READ_TEXT
);
226 /* Some state during the execution of `read_c_string_or_comment'. */
229 /* A count of spaces and newlines that have been read, but not output. */
230 unsigned pending_spaces
, pending_newlines
;
232 /* Where we're reading from. */
235 /* If non-zero, a buffer into which to copy characters. */
237 /* If non-zero, a file into which to copy characters. */
240 /* A keyword we look for at the beginning of lines. If found, it is
241 not copied, and SAW_KEYWORD is set to true. */
243 /* The current point we've reached in an occurance of KEYWORD in
245 char *cur_keyword_ptr
;
246 /* Set to true if we saw an occurance of KEYWORD. */
250 /* Output CH to the file or buffer in STATE. Any pending newlines or
251 spaces are output first. */
256 struct rcsoc_state
*state
;
261 if (state
->pending_newlines
> 0)
263 state
->pending_newlines
--;
266 else if (state
->pending_spaces
> 0)
268 state
->pending_spaces
--;
275 putc (out_ch
, state
->out_file
);
277 *state
->buf_ptr
++ = out_ch
;
279 while (out_ch
!= ch
);
282 /* If in the middle of scanning a keyword, continue scanning with
283 character CH, otherwise output CH to the file or buffer in STATE.
284 Any pending newlines or spaces are output first, as well as any
285 previously scanned characters that were thought to be part of a
286 keyword, but were in fact not. */
289 scan_keyword_or_put_char (ch
, state
)
291 struct rcsoc_state
*state
;
294 && *state
->cur_keyword_ptr
== ch
295 && (state
->cur_keyword_ptr
> state
->keyword
296 || state
->pending_newlines
> 0))
297 /* We might be looking at STATE->keyword at some point.
298 Keep looking until we know for sure. */
300 if (*++state
->cur_keyword_ptr
== '\0')
301 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
303 state
->saw_keyword
= 1;
305 /* Reset the scanning pointer. */
306 state
->cur_keyword_ptr
= state
->keyword
;
308 /* Canonicalize whitespace preceding a usage string. */
309 state
->pending_newlines
= 2;
310 state
->pending_spaces
= 0;
312 /* Skip any whitespace between the keyword and the
315 ch
= getc (state
->in_file
);
316 while (ch
== ' ' || ch
== '\n');
318 /* Output the open-paren we just read. */
319 put_char (ch
, state
);
321 /* Skip the function name and replace it with `fn'. */
323 ch
= getc (state
->in_file
);
324 while (ch
!= ' ' && ch
!= ')');
325 put_char ('f', state
);
326 put_char ('n', state
);
328 /* Put back the last character. */
329 ungetc (ch
, state
->in_file
);
334 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
335 /* We scanned the beginning of a potential usage
336 keyword, but it was a false alarm. Output the
341 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
342 put_char (*p
, state
);
344 state
->cur_keyword_ptr
= state
->keyword
;
347 put_char (ch
, state
);
352 /* Skip a C string or C-style comment from INFILE, and return the
353 character that follows. COMMENT non-zero means skip a comment. If
354 PRINTFLAG is positive, output string contents to outfile. If it is
355 negative, store contents in buf. Convert escape sequences \n and
356 \t to newline and tab; discard \ followed by newline.
357 If SAW_USAGE is non-zero, then any occurances of the string `usage:'
358 at the beginning of a line will be removed, and *SAW_USAGE set to
359 true if any were encountered. */
362 read_c_string_or_comment (infile
, printflag
, comment
, saw_usage
)
369 struct rcsoc_state state
;
371 state
.in_file
= infile
;
372 state
.buf_ptr
= (printflag
< 0 ? buf
: 0);
373 state
.out_file
= (printflag
> 0 ? outfile
: 0);
374 state
.pending_spaces
= 0;
375 state
.pending_newlines
= 0;
376 state
.keyword
= (saw_usage
? "usage:" : 0);
377 state
.cur_keyword_ptr
= state
.keyword
;
378 state
.saw_keyword
= 0;
382 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
387 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
392 if (c
== '\n' || c
== '\r')
404 state
.pending_spaces
++;
407 state
.pending_newlines
++;
408 state
.pending_spaces
= 0;
411 scan_keyword_or_put_char (c
, &state
);
427 scan_keyword_or_put_char ('*', &state
);
434 /* If we had a "", concatenate the two strings. */
443 *saw_usage
= state
.saw_keyword
;
450 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
451 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
454 write_c_args (out
, func
, buf
, minargs
, maxargs
)
457 int minargs
, maxargs
;
464 fprintf (out
, "(fn");
469 for (p
= buf
; *p
; p
++)
474 /* Notice when we start printing a new identifier. */
475 if ((('A' <= c
&& c
<= 'Z')
476 || ('a' <= c
&& c
<= 'z')
477 || ('0' <= c
&& c
<= '9')
489 if (minargs
== 0 && maxargs
> 0)
490 fprintf (out
, "&optional ");
500 /* Print the C argument list as it would appear in lisp:
501 print underscores as hyphens, and print commas and newlines
502 as spaces. Collapse adjacent spaces into one. */
505 else if (c
== ',' || c
== '\n')
508 /* In C code, `default' is a reserved word, so we spell it
509 `defalt'; unmangle that here. */
511 && strncmp (p
, "defalt", 6) == 0
512 && ! (('A' <= p
[6] && p
[6] <= 'Z')
513 || ('a' <= p
[6] && p
[6] <= 'z')
514 || ('0' <= p
[6] && p
[6] <= '9')
517 fprintf (out
, "DEFAULT");
522 else if (c
!= ' ' || !just_spaced
)
524 if (c
>= 'a' && c
<= 'z')
525 /* Upcase the letter. */
530 just_spaced
= c
== ' ';
535 /* Read through a c file. If a .o file is named,
536 the corresponding .c or .m file is read instead.
537 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
538 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
541 scan_c_file (filename
, mode
)
542 char *filename
, *mode
;
547 register int defunflag
;
548 register int defvarperbufferflag
;
549 register int defvarflag
;
550 int minargs
, maxargs
;
551 int extension
= filename
[strlen (filename
) - 1];
553 if (extension
== 'o')
554 filename
[strlen (filename
) - 1] = 'c';
556 infile
= fopen (filename
, mode
);
558 if (infile
== NULL
&& extension
== 'o')
561 filename
[strlen (filename
) - 1] = 'm';
562 infile
= fopen (filename
, mode
);
564 filename
[strlen (filename
) - 1] = 'c'; /* don't confuse people */
567 /* No error if non-ex input file */
574 /* Reset extension to be able to detect duplicate files. */
575 filename
[strlen (filename
) - 1] = extension
;
578 while (!feof (infile
))
582 if (c
!= '\n' && c
!= '\r')
617 defvarperbufferflag
= (c
== 'P');
630 defunflag
= c
== 'U';
632 defvarperbufferflag
= 0;
643 /* Lisp variable or function name. */
647 c
= read_c_string_or_comment (infile
, -1, 0, 0);
649 /* DEFVAR_LISP ("name", addr, "doc")
650 DEFVAR_LISP ("name", addr /\* doc *\/)
651 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
655 else if (defvarperbufferflag
)
659 else /* For DEFSIMPLE and DEFPRED */
668 if (defunflag
&& (commas
== 1 || commas
== 2))
672 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
676 if (commas
== 2) /* pick up minargs */
677 fscanf (infile
, "%d", &minargs
);
678 else /* pick up maxargs */
679 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
682 fscanf (infile
, "%d", &maxargs
);
691 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
695 c
= read_c_string_or_comment (infile
, 0, 0, 0);
697 while (c
!= EOF
&& c
!= ',' && c
!= '/')
702 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
704 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
710 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
717 && (c
= getc (infile
),
721 int comment
= c
!= '"';
725 putc (defvarflag
? 'V' : 'F', outfile
);
726 fprintf (outfile
, "%s\n", buf
);
729 getc (infile
); /* Skip past `*' */
730 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
732 /* If this is a defun, find the arguments and print them. If
733 this function takes MANY or UNEVALLED args, then the C source
734 won't give the names of the arguments, so we shouldn't bother
737 Various doc-string styles:
738 0: DEFUN (..., "DOC") (args) [!comment]
739 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
740 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
742 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
744 char argbuf
[1024], *p
= argbuf
;
746 if (!comment
|| doc_keyword
)
754 /* Skip into arguments. */
761 /* Copy arguments into ARGBUF. */
764 *p
++ = c
= getc (infile
);
768 fprintf (outfile
, "\n\n");
769 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
771 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
772 /* The DOC should provide the usage form. */
773 fprintf (stderr
, "Missing `usage' for function `%s'.\n", buf
);
781 /* Read a file of Lisp code, compiled or interpreted.
783 (defun NAME ARGS DOCSTRING ...)
784 (defmacro NAME ARGS DOCSTRING ...)
785 (defsubst NAME ARGS DOCSTRING ...)
786 (autoload (quote NAME) FILE DOCSTRING ...)
787 (defvar NAME VALUE DOCSTRING)
788 (defconst NAME VALUE DOCSTRING)
789 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
790 (fset (quote NAME) #[... DOCSTRING ...])
791 (defalias (quote NAME) #[... DOCSTRING ...])
792 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
793 starting in column zero.
794 (quote NAME) may appear as 'NAME as well.
796 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
797 When we find that, we save it for the following defining-form,
798 and we use that instead of reading a doc string within that defining-form.
800 For defvar, defconst, and fset we skip to the docstring with a kludgy
801 formatting convention: all docstrings must appear on the same line as the
802 initial open-paren (the one in column zero) and must contain a backslash
803 and a newline immediately after the initial double-quote. No newlines
804 must appear between the beginning of the form and the first double-quote.
805 For defun, defmacro, and autoload, we know how to skip over the
806 arglist, but the doc string must still have a backslash and newline
807 immediately after the double quote.
808 The only source files that must follow this convention are preloaded
809 uncompiled ones like loaddefs.el and bindings.el; aside
810 from that, it is always the .elc file that we look at, and they are no
811 problem because byte-compiler output follows this convention.
812 The NAME and DOCSTRING are output.
813 NAME is preceded by `F' for a function or `V' for a variable.
814 An entry is output only if DOCSTRING has \ newline just after the opening "
822 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
828 read_lisp_symbol (infile
, buffer
)
833 char *fillp
= buffer
;
840 *(++fillp
) = getc (infile
);
841 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
852 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
858 scan_lisp_file (filename
, mode
)
859 char *filename
, *mode
;
863 char *saved_string
= 0;
865 infile
= fopen (filename
, mode
);
869 return 0; /* No error */
873 while (!feof (infile
))
878 /* If not at end of line, skip till we get to one. */
879 if (c
!= '\n' && c
!= '\r')
884 /* Skip the line break. */
885 while (c
== '\n' || c
== '\r')
887 /* Detect a dynamic doc string and save it for the next expression. */
896 /* Read the length. */
897 while ((c
= getc (infile
),
898 c
>= '0' && c
<= '9'))
904 /* The next character is a space that is counted in the length
905 but not part of the doc string.
906 We already read it, so just ignore it. */
909 /* Read in the contents. */
910 if (saved_string
!= 0)
912 saved_string
= (char *) malloc (length
);
913 for (i
= 0; i
< length
; i
++)
914 saved_string
[i
] = getc (infile
);
915 /* The last character is a ^_.
916 That is needed in the .elc file
917 but it is redundant in DOC. So get rid of it here. */
918 saved_string
[length
- 1] = 0;
919 /* Skip the line break. */
920 while (c
== '\n' && c
== '\r')
922 /* Skip the following line. */
923 while (c
!= '\n' && c
!= '\r')
932 read_lisp_symbol (infile
, buffer
);
934 if (! strcmp (buffer
, "defun")
935 || ! strcmp (buffer
, "defmacro")
936 || ! strcmp (buffer
, "defsubst"))
939 read_lisp_symbol (infile
, buffer
);
941 /* Skip the arguments: either "nil" or a list in parens */
944 if (c
== 'n') /* nil */
946 if ((c
= getc (infile
)) != 'i'
947 || (c
= getc (infile
)) != 'l')
949 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
956 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
965 /* If the next three characters aren't `dquote bslash newline'
966 then we're not reading a docstring.
968 if ((c
= getc (infile
)) != '"'
969 || (c
= getc (infile
)) != '\\'
970 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
973 fprintf (stderr
, "## non-docstring in %s (%s)\n",
980 else if (! strcmp (buffer
, "defvar")
981 || ! strcmp (buffer
, "defconst"))
985 read_lisp_symbol (infile
, buffer
);
987 if (saved_string
== 0)
990 /* Skip until the end of line; remember two previous chars. */
991 while (c
!= '\n' && c
!= '\r' && c
>= 0)
998 /* If two previous characters were " and \,
999 this is a doc string. Otherwise, there is none. */
1000 if (c2
!= '"' || c1
!= '\\')
1003 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1011 else if (! strcmp (buffer
, "custom-declare-variable"))
1013 char c1
= 0, c2
= 0;
1018 read_lisp_symbol (infile
, buffer
);
1024 "## unparsable name in custom-declare-variable in %s\n",
1028 read_lisp_symbol (infile
, buffer
);
1029 if (strcmp (buffer
, "quote"))
1032 "## unparsable name in custom-declare-variable in %s\n",
1036 read_lisp_symbol (infile
, buffer
);
1041 "## unparsable quoted name in custom-declare-variable in %s\n",
1047 if (saved_string
== 0)
1049 /* Skip to end of line; remember the two previous chars. */
1050 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1057 /* If two previous characters were " and \,
1058 this is a doc string. Otherwise, there is none. */
1059 if (c2
!= '"' || c1
!= '\\')
1062 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1070 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1072 char c1
= 0, c2
= 0;
1077 read_lisp_symbol (infile
, buffer
);
1082 fprintf (stderr
, "## unparsable name in fset in %s\n",
1086 read_lisp_symbol (infile
, buffer
);
1087 if (strcmp (buffer
, "quote"))
1089 fprintf (stderr
, "## unparsable name in fset in %s\n",
1093 read_lisp_symbol (infile
, buffer
);
1098 "## unparsable quoted name in fset in %s\n",
1104 if (saved_string
== 0)
1106 /* Skip to end of line; remember the two previous chars. */
1107 while (c
!= '\n' && c
!= '\r' && c
>= 0)
1114 /* If two previous characters were " and \,
1115 this is a doc string. Otherwise, there is none. */
1116 if (c2
!= '"' || c1
!= '\\')
1119 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1127 else if (! strcmp (buffer
, "autoload"))
1132 read_lisp_symbol (infile
, buffer
);
1137 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1141 read_lisp_symbol (infile
, buffer
);
1142 if (strcmp (buffer
, "quote"))
1144 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1148 read_lisp_symbol (infile
, buffer
);
1153 "## unparsable quoted name in autoload in %s\n",
1158 skip_white (infile
);
1159 if ((c
= getc (infile
)) != '\"')
1161 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1165 read_c_string_or_comment (infile
, 0, 0, 0);
1166 skip_white (infile
);
1168 if (saved_string
== 0)
1170 /* If the next three characters aren't `dquote bslash newline'
1171 then we're not reading a docstring. */
1172 if ((c
= getc (infile
)) != '"'
1173 || (c
= getc (infile
)) != '\\'
1174 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1177 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1186 else if (! strcmp (buffer
, "if")
1187 || ! strcmp (buffer
, "byte-code"))
1194 fprintf (stderr
, "## unrecognized top-level form, %s (%s)\n",
1200 /* At this point, we should either use the previous
1201 dynamic doc string in saved_string
1202 or gobble a doc string from the input file.
1204 In the latter case, the opening quote (and leading
1205 backslash-newline) have already been read. */
1207 putc (037, outfile
);
1208 putc (type
, outfile
);
1209 fprintf (outfile
, "%s\n", buffer
);
1212 fputs (saved_string
, outfile
);
1213 /* Don't use one dynamic doc string twice. */
1214 free (saved_string
);
1218 read_c_string_or_comment (infile
, 1, 0, 0);
1224 /* arch-tag: f7203aaf-991a-4238-acb5-601db56f2894
1225 (do not change this comment) */
1227 /* make-docfile.c ends here */