1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 86, 92, 93, 94, 97, 1999 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 */
38 /* defined to be emacs_main, sys_fopen, etc. in config.h */
51 #endif /* WINDOWSNT */
54 #define READ_TEXT "rt"
55 #define READ_BINARY "rb"
56 #else /* not DOS_NT */
58 #define READ_BINARY "r"
59 #endif /* not DOS_NT */
62 int scan_lisp_file ();
66 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
67 file where that function is defined. */
75 /* Stdio stream for output to the DOC file. */
78 /* Name this program was invoked with. */
81 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
88 fprintf (stderr
, "%s: ", progname
);
89 fprintf (stderr
, s1
, s2
);
90 fprintf (stderr
, "\n");
93 /* Print error message and exit. */
104 /* Like malloc but get fatal error if memory is exhausted. */
110 long *result
= (long *) malloc (size
);
112 fatal ("virtual memory exhausted", 0);
129 /* Don't put CRs in the DOC file. */
132 #if 0 /* Suspicion is that this causes hanging.
133 So instead we require people to use -o on MSDOS. */
134 (stdout
)->_flag
&= ~_IOTEXT
;
135 _setmode (fileno (stdout
), O_BINARY
);
141 _setmode (fileno (stdout
), O_BINARY
);
142 #endif /* WINDOWSNT */
144 /* If first two args are -o FILE, output to FILE. */
146 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
148 outfile
= fopen (argv
[i
+ 1], "w");
151 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
153 outfile
= fopen (argv
[i
+ 1], "a");
156 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
163 fatal ("No output file specified", "");
166 for (; i
< argc
; i
++)
169 /* Don't process one file twice. */
170 for (j
= first_infile
; j
< i
; j
++)
171 if (! strcmp (argv
[i
], argv
[j
]))
174 err_count
+= scan_file (argv
[i
]);
177 exit (err_count
> 0);
179 return err_count
> 0;
182 /* Read file FILENAME and output its doc strings to outfile. */
183 /* Return 1 if file is not found, 0 if it is found. */
189 int len
= strlen (filename
);
190 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
191 return scan_lisp_file (filename
, READ_BINARY
);
192 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
193 return scan_lisp_file (filename
, READ_TEXT
);
195 return scan_c_file (filename
, READ_TEXT
);
200 /* Skip a C string from INFILE,
201 and return the character that follows the closing ".
202 If printflag is positive, output string contents to outfile.
203 If it is negative, store contents in buf.
204 Convert escape sequences \n and \t to newline and tab;
205 discard \ followed by newline. */
208 read_c_string (infile
, printflag
)
218 while (c
!= '"' && c
!= EOF
)
223 if (c
== '\n' || c
== '\r')
235 else if (printflag
< 0)
242 /* If we had a "", concatenate the two strings. */
252 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
253 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
256 write_c_args (out
, func
, buf
, minargs
, maxargs
)
259 int minargs
, maxargs
;
266 fprintf (out
, "(%s", func
);
271 for (p
= buf
; *p
; p
++)
276 /* Notice when we start printing a new identifier. */
277 if ((('A' <= c
&& c
<= 'Z')
278 || ('a' <= c
&& c
<= 'z')
279 || ('0' <= c
&& c
<= '9')
291 if (minargs
== 0 && maxargs
> 0)
292 fprintf (out
, "&optional ");
302 /* Print the C argument list as it would appear in lisp:
303 print underscores as hyphens, and print commas as spaces.
304 Collapse adjacent spaces into one. */
305 if (c
== '_') c
= '-';
306 if (c
== ',') c
= ' ';
308 /* In C code, `default' is a reserved word, so we spell it
309 `defalt'; unmangle that here. */
311 && strncmp (p
, "defalt", 6) == 0
312 && ! (('A' <= p
[6] && p
[6] <= 'Z')
313 || ('a' <= p
[6] && p
[6] <= 'z')
314 || ('0' <= p
[6] && p
[6] <= '9')
317 fprintf (out
, "DEFAULT");
322 else if (c
!= ' ' || ! just_spaced
)
324 if (c
>= 'a' && c
<= 'z')
325 /* Upcase the letter. */
330 just_spaced
= (c
== ' ');
335 /* Read through a c file. If a .o file is named,
336 the corresponding .c file is read instead.
337 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
338 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
341 scan_c_file (filename
, mode
)
342 char *filename
, *mode
;
347 register int defunflag
;
348 register int defvarperbufferflag
;
349 register int defvarflag
;
350 int minargs
, maxargs
;
351 int extension
= filename
[strlen (filename
) - 1];
353 if (extension
== 'o')
354 filename
[strlen (filename
) - 1] = 'c';
356 infile
= fopen (filename
, mode
);
358 /* No error if non-ex input file */
365 /* Reset extension to be able to detect duplicate files. */
366 filename
[strlen (filename
) - 1] = extension
;
369 while (!feof (infile
))
371 if (c
!= '\n' && c
!= '\r')
406 defvarperbufferflag
= (c
== 'P');
419 defunflag
= c
== 'U';
434 c
= read_c_string (infile
, -1);
438 else if (defvarperbufferflag
)
442 else /* For DEFSIMPLE and DEFPRED */
450 if (defunflag
&& (commas
== 1 || commas
== 2))
454 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
458 if (commas
== 2) /* pick up minargs */
459 fscanf (infile
, "%d", &minargs
);
460 else /* pick up maxargs */
461 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
464 fscanf (infile
, "%d", &maxargs
);
471 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
474 c
= read_c_string (infile
, 0);
478 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
484 putc (defvarflag
? 'V' : 'F', outfile
);
485 fprintf (outfile
, "%s\n", buf
);
486 c
= read_c_string (infile
, 1);
488 /* If this is a defun, find the arguments and print them. If
489 this function takes MANY or UNEVALLED args, then the C source
490 won't give the names of the arguments, so we shouldn't bother
491 trying to find them. */
492 if (defunflag
&& maxargs
!= -1)
494 char argbuf
[1024], *p
= argbuf
;
501 /* Skip into arguments. */
508 /* Copy arguments into ARGBUF. */
511 *p
++ = c
= getc (infile
);
515 fprintf (outfile
, "\n\n");
516 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
525 /* Read a file of Lisp code, compiled or interpreted.
527 (defun NAME ARGS DOCSTRING ...)
528 (defmacro NAME ARGS DOCSTRING ...)
529 (autoload (quote NAME) FILE DOCSTRING ...)
530 (defvar NAME VALUE DOCSTRING)
531 (defconst NAME VALUE DOCSTRING)
532 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
533 (fset (quote NAME) #[... DOCSTRING ...])
534 (defalias (quote NAME) #[... DOCSTRING ...])
535 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
536 starting in column zero.
537 (quote NAME) may appear as 'NAME as well.
539 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
540 When we find that, we save it for the following defining-form,
541 and we use that instead of reading a doc string within that defining-form.
543 For defvar, defconst, and fset we skip to the docstring with a kludgy
544 formatting convention: all docstrings must appear on the same line as the
545 initial open-paren (the one in column zero) and must contain a backslash
546 and a newline immediately after the initial double-quote. No newlines
547 must appear between the beginning of the form and the first double-quote.
548 For defun, defmacro, and autoload, we know how to skip over the
549 arglist, but the doc string must still have a backslash and newline
550 immediately after the double quote.
551 The only source files that must follow this convention are preloaded
552 uncompiled ones like loaddefs.el and bindings.el; aside
553 from that, it is always the .elc file that we look at, and they are no
554 problem because byte-compiler output follows this convention.
555 The NAME and DOCSTRING are output.
556 NAME is preceded by `F' for a function or `V' for a variable.
557 An entry is output only if DOCSTRING has \ newline just after the opening "
565 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
571 read_lisp_symbol (infile
, buffer
)
576 char *fillp
= buffer
;
583 *(++fillp
) = getc (infile
);
584 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
595 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
601 scan_lisp_file (filename
, mode
)
602 char *filename
, *mode
;
606 char *saved_string
= 0;
608 infile
= fopen (filename
, mode
);
612 return 0; /* No error */
616 while (!feof (infile
))
621 /* If not at end of line, skip till we get to one. */
622 if (c
!= '\n' && c
!= '\r')
627 /* Skip the line break. */
628 while (c
== '\n' || c
== '\r')
630 /* Detect a dynamic doc string and save it for the next expression. */
639 /* Read the length. */
640 while ((c
= getc (infile
),
641 c
>= '0' && c
<= '9'))
647 /* The next character is a space that is counted in the length
648 but not part of the doc string.
649 We already read it, so just ignore it. */
652 /* Read in the contents. */
653 if (saved_string
!= 0)
655 saved_string
= (char *) malloc (length
);
656 for (i
= 0; i
< length
; i
++)
657 saved_string
[i
] = getc (infile
);
658 /* The last character is a ^_.
659 That is needed in the .elc file
660 but it is redundant in DOC. So get rid of it here. */
661 saved_string
[length
- 1] = 0;
662 /* Skip the line break. */
663 while (c
== '\n' && c
== '\r')
665 /* Skip the following line. */
666 while (c
!= '\n' && c
!= '\r')
675 read_lisp_symbol (infile
, buffer
);
677 if (! strcmp (buffer
, "defun")
678 || ! strcmp (buffer
, "defmacro"))
681 read_lisp_symbol (infile
, buffer
);
683 /* Skip the arguments: either "nil" or a list in parens */
686 if (c
== 'n') /* nil */
688 if ((c
= getc (infile
)) != 'i'
689 || (c
= getc (infile
)) != 'l')
691 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
698 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
707 /* If the next three characters aren't `dquote bslash newline'
708 then we're not reading a docstring.
710 if ((c
= getc (infile
)) != '"'
711 || (c
= getc (infile
)) != '\\'
712 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
715 fprintf (stderr
, "## non-docstring in %s (%s)\n",
722 else if (! strcmp (buffer
, "defvar")
723 || ! strcmp (buffer
, "defconst"))
727 read_lisp_symbol (infile
, buffer
);
729 if (saved_string
== 0)
732 /* Skip until the end of line; remember two previous chars. */
733 while (c
!= '\n' && c
!= '\r' && c
>= 0)
740 /* If two previous characters were " and \,
741 this is a doc string. Otherwise, there is none. */
742 if (c2
!= '"' || c1
!= '\\')
745 fprintf (stderr
, "## non-docstring in %s (%s)\n",
753 else if (! strcmp (buffer
, "custom-declare-variable"))
760 read_lisp_symbol (infile
, buffer
);
766 "## unparsable name in custom-declare-variable in %s\n",
770 read_lisp_symbol (infile
, buffer
);
771 if (strcmp (buffer
, "quote"))
774 "## unparsable name in custom-declare-variable in %s\n",
778 read_lisp_symbol (infile
, buffer
);
783 "## unparsable quoted name in custom-declare-variable in %s\n",
789 if (saved_string
== 0)
791 /* Skip to end of line; remember the two previous chars. */
792 while (c
!= '\n' && c
!= '\r' && c
>= 0)
799 /* If two previous characters were " and \,
800 this is a doc string. Otherwise, there is none. */
801 if (c2
!= '"' || c1
!= '\\')
804 fprintf (stderr
, "## non-docstring in %s (%s)\n",
812 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
819 read_lisp_symbol (infile
, buffer
);
824 fprintf (stderr
, "## unparsable name in fset in %s\n",
828 read_lisp_symbol (infile
, buffer
);
829 if (strcmp (buffer
, "quote"))
831 fprintf (stderr
, "## unparsable name in fset in %s\n",
835 read_lisp_symbol (infile
, buffer
);
840 "## unparsable quoted name in fset in %s\n",
846 if (saved_string
== 0)
848 /* Skip to end of line; remember the two previous chars. */
849 while (c
!= '\n' && c
!= '\r' && c
>= 0)
856 /* If two previous characters were " and \,
857 this is a doc string. Otherwise, there is none. */
858 if (c2
!= '"' || c1
!= '\\')
861 fprintf (stderr
, "## non-docstring in %s (%s)\n",
869 else if (! strcmp (buffer
, "autoload"))
874 read_lisp_symbol (infile
, buffer
);
879 fprintf (stderr
, "## unparsable name in autoload in %s\n",
883 read_lisp_symbol (infile
, buffer
);
884 if (strcmp (buffer
, "quote"))
886 fprintf (stderr
, "## unparsable name in autoload in %s\n",
890 read_lisp_symbol (infile
, buffer
);
895 "## unparsable quoted name in autoload in %s\n",
901 if ((c
= getc (infile
)) != '\"')
903 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
907 read_c_string (infile
, 0);
910 if (saved_string
== 0)
912 /* If the next three characters aren't `dquote bslash newline'
913 then we're not reading a docstring. */
914 if ((c
= getc (infile
)) != '"'
915 || (c
= getc (infile
)) != '\\'
916 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
919 fprintf (stderr
, "## non-docstring in %s (%s)\n",
928 else if (! strcmp (buffer
, "if")
929 || ! strcmp (buffer
, "byte-code"))
936 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
942 /* At this point, we should either use the previous
943 dynamic doc string in saved_string
944 or gobble a doc string from the input file.
946 In the latter case, the opening quote (and leading
947 backslash-newline) have already been read. */
950 putc (type
, outfile
);
951 fprintf (outfile
, "%s\n", buffer
);
954 fputs (saved_string
, outfile
);
955 /* Don't use one dynamic doc string twice. */
960 read_c_string (infile
, 1);