1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 1986, 92, 93, 94, 1997 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* The arguments given to this program are all the C and Lisp source files
22 of GNU Emacs. .elc and .el and .c files are allowed.
23 A .o file can also be specified; the .c file it was made from is used.
24 This helps the makefile pass the correct list of files.
26 The results, which go to standard output or to a file
27 specified with -a or -o (-a to append, -o to start from nothing),
28 are entries containing function or variable names and their documentation.
29 Each entry starts with a ^_ character.
30 Then comes F for a function or V for a variable.
31 Then comes the function or variable name, terminated with a newline.
32 Then comes the documentation for that function or variable.
35 #define NO_SHORTNAMES /* Tell config not to load remap.h */
36 #include <../src/config.h>
46 #endif /* WINDOWSNT */
49 #define READ_TEXT "rt"
50 #define READ_BINARY "rb"
51 #else /* not DOS_NT */
53 #define READ_BINARY "r"
54 #endif /* not DOS_NT */
57 int scan_lisp_file ();
61 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
62 file where that function is defined. */
70 /* Stdio stream for output to the DOC file. */
73 /* Name this program was invoked with. */
76 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
83 fprintf (stderr
, "%s: ", progname
);
84 fprintf (stderr
, s1
, s2
);
85 fprintf (stderr
, "\n");
88 /* Print error message and exit. */
99 /* Like malloc but get fatal error if memory is exhausted. */
105 long *result
= (long *) malloc (size
);
107 fatal ("virtual memory exhausted", 0);
124 /* Don't put CRs in the DOC file. */
127 #if 0 /* Suspicion is that this causes hanging.
128 So instead we require people to use -o on MSDOS. */
129 (stdout
)->_flag
&= ~_IOTEXT
;
130 _setmode (fileno (stdout
), O_BINARY
);
136 _setmode (fileno (stdout
), O_BINARY
);
137 #endif /* WINDOWSNT */
139 /* If first two args are -o FILE, output to FILE. */
141 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
143 outfile
= fopen (argv
[i
+ 1], "w");
146 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
148 outfile
= fopen (argv
[i
+ 1], "a");
151 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
158 fatal ("No output file specified", "");
161 for (; i
< argc
; i
++)
164 /* Don't process one file twice. */
165 for (j
= first_infile
; j
< i
; j
++)
166 if (! strcmp (argv
[i
], argv
[j
]))
169 err_count
+= scan_file (argv
[i
]);
172 exit (err_count
> 0);
174 return err_count
> 0;
177 /* Read file FILENAME and output its doc strings to outfile. */
178 /* Return 1 if file is not found, 0 if it is found. */
184 int len
= strlen (filename
);
185 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
186 return scan_lisp_file (filename
, READ_BINARY
);
187 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
188 return scan_lisp_file (filename
, READ_TEXT
);
190 return scan_c_file (filename
, READ_TEXT
);
195 /* Skip a C string from INFILE,
196 and return the character that follows the closing ".
197 If printflag is positive, output string contents to outfile.
198 If it is negative, store contents in buf.
199 Convert escape sequences \n and \t to newline and tab;
200 discard \ followed by newline. */
203 read_c_string (infile
, printflag
)
213 while (c
!= '"' && c
!= EOF
)
230 else if (printflag
< 0)
237 /* If we had a "", concatenate the two strings. */
247 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
248 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
251 write_c_args (out
, func
, buf
, minargs
, maxargs
)
254 int minargs
, maxargs
;
261 fprintf (out
, "(%s", func
);
266 for (p
= buf
; *p
; p
++)
271 /* Notice when we start printing a new identifier. */
272 if ((('A' <= c
&& c
<= 'Z')
273 || ('a' <= c
&& c
<= 'z')
274 || ('0' <= c
&& c
<= '9')
286 if (minargs
== 0 && maxargs
> 0)
287 fprintf (out
, "&optional ");
297 /* Print the C argument list as it would appear in lisp:
298 print underscores as hyphens, and print commas as spaces.
299 Collapse adjacent spaces into one. */
300 if (c
== '_') c
= '-';
301 if (c
== ',') c
= ' ';
303 /* In C code, `default' is a reserved word, so we spell it
304 `defalt'; unmangle that here. */
306 && strncmp (p
, "defalt", 6) == 0
307 && ! (('A' <= p
[6] && p
[6] <= 'Z')
308 || ('a' <= p
[6] && p
[6] <= 'z')
309 || ('0' <= p
[6] && p
[6] <= '9')
312 fprintf (out
, "DEFAULT");
317 else if (c
!= ' ' || ! just_spaced
)
319 if (c
>= 'a' && c
<= 'z')
320 /* Upcase the letter. */
325 just_spaced
= (c
== ' ');
330 /* Read through a c file. If a .o file is named,
331 the corresponding .c file is read instead.
332 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
333 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
336 scan_c_file (filename
, mode
)
337 char *filename
, *mode
;
342 register int defunflag
;
343 register int defvarperbufferflag
;
344 register int defvarflag
;
345 int minargs
, maxargs
;
346 int extension
= filename
[strlen (filename
) - 1];
348 if (extension
== 'o')
349 filename
[strlen (filename
) - 1] = 'c';
351 infile
= fopen (filename
, mode
);
353 /* No error if non-ex input file */
360 /* Reset extension to be able to detect duplicate files. */
361 filename
[strlen (filename
) - 1] = extension
;
364 while (!feof (infile
))
401 defvarperbufferflag
= (c
== 'P');
414 defunflag
= c
== 'U';
429 c
= read_c_string (infile
, -1);
433 else if (defvarperbufferflag
)
437 else /* For DEFSIMPLE and DEFPRED */
445 if (defunflag
&& (commas
== 1 || commas
== 2))
449 while (c
== ' ' || c
== '\n' || c
== '\t');
453 if (commas
== 2) /* pick up minargs */
454 fscanf (infile
, "%d", &minargs
);
455 else /* pick up maxargs */
456 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
459 fscanf (infile
, "%d", &maxargs
);
466 while (c
== ' ' || c
== '\n' || c
== '\t')
469 c
= read_c_string (infile
, 0);
473 while (c
== ' ' || c
== '\n' || c
== '\t')
479 putc (defvarflag
? 'V' : 'F', outfile
);
480 fprintf (outfile
, "%s\n", buf
);
481 c
= read_c_string (infile
, 1);
483 /* If this is a defun, find the arguments and print them. If
484 this function takes MANY or UNEVALLED args, then the C source
485 won't give the names of the arguments, so we shouldn't bother
486 trying to find them. */
487 if (defunflag
&& maxargs
!= -1)
489 char argbuf
[1024], *p
= argbuf
;
496 /* Skip into arguments. */
503 /* Copy arguments into ARGBUF. */
506 *p
++ = c
= getc (infile
);
510 fprintf (outfile
, "\n\n");
511 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
520 /* Read a file of Lisp code, compiled or interpreted.
522 (defun NAME ARGS DOCSTRING ...)
523 (defmacro NAME ARGS DOCSTRING ...)
524 (autoload (quote NAME) FILE DOCSTRING ...)
525 (defvar NAME VALUE DOCSTRING)
526 (defconst NAME VALUE DOCSTRING)
527 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
528 (fset (quote NAME) #[... DOCSTRING ...])
529 (defalias (quote NAME) #[... DOCSTRING ...])
530 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
531 starting in column zero.
532 (quote NAME) may appear as 'NAME as well.
534 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
535 When we find that, we save it for the following defining-form,
536 and we use that instead of reading a doc string within that defining-form.
538 For defun, defmacro, and autoload, we know how to skip over the arglist.
539 For defvar, defconst, and fset we skip to the docstring with a kludgy
540 formatting convention: all docstrings must appear on the same line as the
541 initial open-paren (the one in column zero) and must contain a backslash
542 and a double-quote immediately after the initial double-quote. No newlines
543 must appear between the beginning of the form and the first double-quote.
544 The only source file that must follow this convention is loaddefs.el; aside
545 from that, it is always the .elc file that we look at, and they are no
546 problem because byte-compiler output follows this convention.
547 The NAME and DOCSTRING are output.
548 NAME is preceded by `F' for a function or `V' for a variable.
549 An entry is output only if DOCSTRING has \ newline just after the opening "
557 while (c
== ' ' || c
== '\t' || c
== '\n')
563 read_lisp_symbol (infile
, buffer
)
568 char *fillp
= buffer
;
575 *(++fillp
) = getc (infile
);
576 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '(' || c
== ')')
587 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
593 scan_lisp_file (filename
, mode
)
594 char *filename
, *mode
;
598 char *saved_string
= 0;
600 infile
= fopen (filename
, mode
);
604 return 0; /* No error */
608 while (!feof (infile
))
619 /* Detect a dynamic doc string and save it for the next expression. */
628 /* Read the length. */
629 while ((c
= getc (infile
),
630 c
>= '0' && c
<= '9'))
636 /* The next character is a space that is counted in the length
637 but not part of the doc string.
638 We already read it, so just ignore it. */
641 /* Read in the contents. */
642 if (saved_string
!= 0)
644 saved_string
= (char *) malloc (length
);
645 for (i
= 0; i
< length
; i
++)
646 saved_string
[i
] = getc (infile
);
647 /* The last character is a ^_.
648 That is needed in the .elc file
649 but it is redundant in DOC. So get rid of it here. */
650 saved_string
[length
- 1] = 0;
651 /* Skip the newline. */
662 read_lisp_symbol (infile
, buffer
);
664 if (! strcmp (buffer
, "defun") ||
665 ! strcmp (buffer
, "defmacro"))
668 read_lisp_symbol (infile
, buffer
);
670 /* Skip the arguments: either "nil" or a list in parens */
673 if (c
== 'n') /* nil */
675 if ((c
= getc (infile
)) != 'i' ||
676 (c
= getc (infile
)) != 'l')
678 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
685 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
694 /* If the next three characters aren't `dquote bslash newline'
695 then we're not reading a docstring.
697 if ((c
= getc (infile
)) != '"' ||
698 (c
= getc (infile
)) != '\\' ||
699 (c
= getc (infile
)) != '\n')
702 fprintf (stderr
, "## non-docstring in %s (%s)\n",
709 else if (! strcmp (buffer
, "defvar") ||
710 ! strcmp (buffer
, "defconst"))
714 read_lisp_symbol (infile
, buffer
);
716 if (saved_string
== 0)
719 /* Skip until the first newline; remember the two previous chars. */
720 while (c
!= '\n' && c
>= 0)
727 /* If two previous characters were " and \,
728 this is a doc string. Otherwise, there is none. */
729 if (c2
!= '"' || c1
!= '\\')
732 fprintf (stderr
, "## non-docstring in %s (%s)\n",
740 else if (! strcmp (buffer
, "custom-declare-variable"))
747 read_lisp_symbol (infile
, buffer
);
753 "## unparsable name in custom-declare-variable in %s\n",
757 read_lisp_symbol (infile
, buffer
);
758 if (strcmp (buffer
, "quote"))
761 "## unparsable name in custom-declare-variable in %s\n",
765 read_lisp_symbol (infile
, buffer
);
770 "## unparsable quoted name in custom-declare-variable in %s\n",
776 if (saved_string
== 0)
778 /* Skip until the first newline; remember the two previous
780 while (c
!= '\n' && c
>= 0)
787 /* If two previous characters were " and \,
788 this is a doc string. Otherwise, there is none. */
789 if (c2
!= '"' || c1
!= '\\')
792 fprintf (stderr
, "## non-docstring in %s (%s)\n",
800 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
807 read_lisp_symbol (infile
, buffer
);
812 fprintf (stderr
, "## unparsable name in fset in %s\n",
816 read_lisp_symbol (infile
, buffer
);
817 if (strcmp (buffer
, "quote"))
819 fprintf (stderr
, "## unparsable name in fset in %s\n",
823 read_lisp_symbol (infile
, buffer
);
828 "## unparsable quoted name in fset in %s\n",
834 if (saved_string
== 0)
836 /* Skip until the first newline; remember the two previous chars. */
837 while (c
!= '\n' && c
>= 0)
844 /* If two previous characters were " and \,
845 this is a doc string. Otherwise, there is none. */
846 if (c2
!= '"' || c1
!= '\\')
849 fprintf (stderr
, "## non-docstring in %s (%s)\n",
857 else if (! strcmp (buffer
, "autoload"))
862 read_lisp_symbol (infile
, buffer
);
867 fprintf (stderr
, "## unparsable name in autoload in %s\n",
871 read_lisp_symbol (infile
, buffer
);
872 if (strcmp (buffer
, "quote"))
874 fprintf (stderr
, "## unparsable name in autoload in %s\n",
878 read_lisp_symbol (infile
, buffer
);
883 "## unparsable quoted name in autoload in %s\n",
889 if ((c
= getc (infile
)) != '\"')
891 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
895 read_c_string (infile
, 0);
898 if (saved_string
== 0)
900 /* If the next three characters aren't `dquote bslash newline'
901 then we're not reading a docstring. */
902 if ((c
= getc (infile
)) != '"' ||
903 (c
= getc (infile
)) != '\\' ||
904 (c
= getc (infile
)) != '\n')
907 fprintf (stderr
, "## non-docstring in %s (%s)\n",
916 else if (! strcmp (buffer
, "if") ||
917 ! strcmp (buffer
, "byte-code"))
924 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
930 /* At this point, we should either use the previous
931 dynamic doc string in saved_string
932 or gobble a doc string from the input file.
934 In the latter case, the opening quote (and leading
935 backslash-newline) have already been read. */
938 putc (type
, outfile
);
939 fprintf (outfile
, "%s\n", buffer
);
942 fputs (saved_string
, outfile
);
943 /* Don't use one dynamic doc string twice. */
948 read_c_string (infile
, 1);