1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 86, 92, 93, 94, 97, 1999, 2000, 2001
3 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 2, 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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.
27 The results, which go to standard output or to a file
28 specified with -a or -o (-a to append, -o to start from nothing),
29 are entries containing function or variable names and their documentation.
30 Each entry starts with a ^_ character.
31 Then comes F for a function or V for a variable.
32 Then comes the function or variable name, terminated with a newline.
33 Then comes the documentation for that function or variable.
36 #define NO_SHORTNAMES /* Tell config not to load remap.h */
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 int scan_lisp_file ();
67 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
68 file where that function is defined. */
76 /* Stdio stream for output to the DOC file. */
79 /* Name this program was invoked with. */
82 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
89 fprintf (stderr
, "%s: ", progname
);
90 fprintf (stderr
, s1
, s2
);
91 fprintf (stderr
, "\n");
94 /* Print error message and exit. */
105 /* Like malloc but get fatal error if memory is exhausted. */
111 long *result
= (long *) malloc (size
);
113 fatal ("virtual memory exhausted", 0);
130 /* Don't put CRs in the DOC file. */
133 #if 0 /* Suspicion is that this causes hanging.
134 So instead we require people to use -o on MSDOS. */
135 (stdout
)->_flag
&= ~_IOTEXT
;
136 _setmode (fileno (stdout
), O_BINARY
);
142 _setmode (fileno (stdout
), O_BINARY
);
143 #endif /* WINDOWSNT */
145 /* If first two args are -o FILE, output to FILE. */
147 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
149 outfile
= fopen (argv
[i
+ 1], "w");
152 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
154 outfile
= fopen (argv
[i
+ 1], "a");
157 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
164 fatal ("No output file specified", "");
167 for (; i
< argc
; i
++)
170 /* Don't process one file twice. */
171 for (j
= first_infile
; j
< i
; j
++)
172 if (! strcmp (argv
[i
], argv
[j
]))
175 err_count
+= scan_file (argv
[i
]);
178 exit (err_count
> 0);
180 return err_count
> 0;
183 /* Read file FILENAME and output its doc strings to outfile. */
184 /* Return 1 if file is not found, 0 if it is found. */
190 int len
= strlen (filename
);
191 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
192 return scan_lisp_file (filename
, READ_BINARY
);
193 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
194 return scan_lisp_file (filename
, READ_TEXT
);
196 return scan_c_file (filename
, READ_TEXT
);
201 /* Skip a C string or C-style comment from INFILE, and return the
202 character that follows. COMMENT non-zero means skip a comment. If
203 PRINTFLAG is positive, output string contents to outfile. If it is
204 negative, store contents in buf. Convert escape sequences \n and
205 \t to newline and tab; discard \ followed by newline. */
208 read_c_string_or_comment (infile
, printflag
, comment
)
217 while ((c
= getc (infile
)) != EOF
218 && (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' '))
226 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
231 if (c
== '\n' || c
== '\r')
244 else if (printflag
< 0)
264 /* If we had a "", concatenate the two strings. */
277 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
278 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
281 write_c_args (out
, func
, buf
, minargs
, maxargs
)
284 int minargs
, maxargs
;
291 fprintf (out
, "(%s", func
);
296 for (p
= buf
; *p
; p
++)
301 /* Notice when we start printing a new identifier. */
302 if ((('A' <= c
&& c
<= 'Z')
303 || ('a' <= c
&& c
<= 'z')
304 || ('0' <= c
&& c
<= '9')
316 if (minargs
== 0 && maxargs
> 0)
317 fprintf (out
, "&optional ");
327 /* Print the C argument list as it would appear in lisp:
328 print underscores as hyphens, and print commas and newlines
329 as spaces. Collapse adjacent spaces into one. */
332 else if (c
== ',' || c
== '\n')
335 /* In C code, `default' is a reserved word, so we spell it
336 `defalt'; unmangle that here. */
338 && strncmp (p
, "defalt", 6) == 0
339 && ! (('A' <= p
[6] && p
[6] <= 'Z')
340 || ('a' <= p
[6] && p
[6] <= 'z')
341 || ('0' <= p
[6] && p
[6] <= '9')
344 fprintf (out
, "DEFAULT");
349 else if (c
!= ' ' || !just_spaced
)
351 if (c
>= 'a' && c
<= 'z')
352 /* Upcase the letter. */
357 just_spaced
= c
== ' ';
362 /* Read through a c file. If a .o file is named,
363 the corresponding .c file is read instead.
364 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
365 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
368 scan_c_file (filename
, mode
)
369 char *filename
, *mode
;
374 register int defunflag
;
375 register int defvarperbufferflag
;
376 register int defvarflag
;
377 int minargs
, maxargs
;
378 int extension
= filename
[strlen (filename
) - 1];
380 if (extension
== 'o')
381 filename
[strlen (filename
) - 1] = 'c';
383 infile
= fopen (filename
, mode
);
385 /* No error if non-ex input file */
392 /* Reset extension to be able to detect duplicate files. */
393 filename
[strlen (filename
) - 1] = extension
;
396 while (!feof (infile
))
398 if (c
!= '\n' && c
!= '\r')
433 defvarperbufferflag
= (c
== 'P');
446 defunflag
= c
== 'U';
458 /* Lisp variable or function name. */
462 c
= read_c_string_or_comment (infile
, -1, 0);
464 /* DEFVAR_LISP ("name", addr /\* doc *\/)
465 DEFVAR_LISP ("name", addr, doc) */
469 else if (defvarperbufferflag
)
473 else /* For DEFSIMPLE and DEFPRED */
482 if (defunflag
&& (commas
== 1 || commas
== 2))
486 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
490 if (commas
== 2) /* pick up minargs */
491 fscanf (infile
, "%d", &minargs
);
492 else /* pick up maxargs */
493 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
496 fscanf (infile
, "%d", &maxargs
);
505 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
509 c
= read_c_string_or_comment (infile
, 0, 0);
511 while (c
!= EOF
&& c
!= ',' && c
!= '/')
516 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
522 && (c
= getc (infile
),
526 int comment
= c
!= '"';
529 putc (defvarflag
? 'V' : 'F', outfile
);
530 fprintf (outfile
, "%s\n", buf
);
533 getc (infile
); /* Skip past `*' */
534 c
= read_c_string_or_comment (infile
, 1, comment
);
536 /* If this is a defun, find the arguments and print them. If
537 this function takes MANY or UNEVALLED args, then the C source
538 won't give the names of the arguments, so we shouldn't bother
541 Old: DEFUN (..., "DOC") (args)
542 New: DEFUN (..., /\* DOC *\/ (args)) */
543 if (defunflag
&& maxargs
!= -1)
545 char argbuf
[1024], *p
= argbuf
;
555 /* Skip into arguments. */
562 /* Copy arguments into ARGBUF. */
565 *p
++ = c
= getc (infile
);
569 fprintf (outfile
, "\n\n");
570 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
579 /* Read a file of Lisp code, compiled or interpreted.
581 (defun NAME ARGS DOCSTRING ...)
582 (defmacro NAME ARGS DOCSTRING ...)
583 (defsubst NAME ARGS DOCSTRING ...)
584 (autoload (quote NAME) FILE DOCSTRING ...)
585 (defvar NAME VALUE DOCSTRING)
586 (defconst NAME VALUE DOCSTRING)
587 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
588 (fset (quote NAME) #[... DOCSTRING ...])
589 (defalias (quote NAME) #[... DOCSTRING ...])
590 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
591 starting in column zero.
592 (quote NAME) may appear as 'NAME as well.
594 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
595 When we find that, we save it for the following defining-form,
596 and we use that instead of reading a doc string within that defining-form.
598 For defvar, defconst, and fset we skip to the docstring with a kludgy
599 formatting convention: all docstrings must appear on the same line as the
600 initial open-paren (the one in column zero) and must contain a backslash
601 and a newline immediately after the initial double-quote. No newlines
602 must appear between the beginning of the form and the first double-quote.
603 For defun, defmacro, and autoload, we know how to skip over the
604 arglist, but the doc string must still have a backslash and newline
605 immediately after the double quote.
606 The only source files that must follow this convention are preloaded
607 uncompiled ones like loaddefs.el and bindings.el; aside
608 from that, it is always the .elc file that we look at, and they are no
609 problem because byte-compiler output follows this convention.
610 The NAME and DOCSTRING are output.
611 NAME is preceded by `F' for a function or `V' for a variable.
612 An entry is output only if DOCSTRING has \ newline just after the opening "
620 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
626 read_lisp_symbol (infile
, buffer
)
631 char *fillp
= buffer
;
638 *(++fillp
) = getc (infile
);
639 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
650 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
656 scan_lisp_file (filename
, mode
)
657 char *filename
, *mode
;
661 char *saved_string
= 0;
663 infile
= fopen (filename
, mode
);
667 return 0; /* No error */
671 while (!feof (infile
))
676 /* If not at end of line, skip till we get to one. */
677 if (c
!= '\n' && c
!= '\r')
682 /* Skip the line break. */
683 while (c
== '\n' || c
== '\r')
685 /* Detect a dynamic doc string and save it for the next expression. */
694 /* Read the length. */
695 while ((c
= getc (infile
),
696 c
>= '0' && c
<= '9'))
702 /* The next character is a space that is counted in the length
703 but not part of the doc string.
704 We already read it, so just ignore it. */
707 /* Read in the contents. */
708 if (saved_string
!= 0)
710 saved_string
= (char *) malloc (length
);
711 for (i
= 0; i
< length
; i
++)
712 saved_string
[i
] = getc (infile
);
713 /* The last character is a ^_.
714 That is needed in the .elc file
715 but it is redundant in DOC. So get rid of it here. */
716 saved_string
[length
- 1] = 0;
717 /* Skip the line break. */
718 while (c
== '\n' && c
== '\r')
720 /* Skip the following line. */
721 while (c
!= '\n' && c
!= '\r')
730 read_lisp_symbol (infile
, buffer
);
732 if (! strcmp (buffer
, "defun")
733 || ! strcmp (buffer
, "defmacro")
734 || ! strcmp (buffer
, "defsubst"))
737 read_lisp_symbol (infile
, buffer
);
739 /* Skip the arguments: either "nil" or a list in parens */
742 if (c
== 'n') /* nil */
744 if ((c
= getc (infile
)) != 'i'
745 || (c
= getc (infile
)) != 'l')
747 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
754 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
763 /* If the next three characters aren't `dquote bslash newline'
764 then we're not reading a docstring.
766 if ((c
= getc (infile
)) != '"'
767 || (c
= getc (infile
)) != '\\'
768 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
771 fprintf (stderr
, "## non-docstring in %s (%s)\n",
778 else if (! strcmp (buffer
, "defvar")
779 || ! strcmp (buffer
, "defconst"))
783 read_lisp_symbol (infile
, buffer
);
785 if (saved_string
== 0)
788 /* Skip until the end of line; remember two previous chars. */
789 while (c
!= '\n' && c
!= '\r' && c
>= 0)
796 /* If two previous characters were " and \,
797 this is a doc string. Otherwise, there is none. */
798 if (c2
!= '"' || c1
!= '\\')
801 fprintf (stderr
, "## non-docstring in %s (%s)\n",
809 else if (! strcmp (buffer
, "custom-declare-variable"))
816 read_lisp_symbol (infile
, buffer
);
822 "## unparsable name in custom-declare-variable in %s\n",
826 read_lisp_symbol (infile
, buffer
);
827 if (strcmp (buffer
, "quote"))
830 "## unparsable name in custom-declare-variable in %s\n",
834 read_lisp_symbol (infile
, buffer
);
839 "## unparsable quoted name in custom-declare-variable in %s\n",
845 if (saved_string
== 0)
847 /* Skip to end of line; remember the two previous chars. */
848 while (c
!= '\n' && c
!= '\r' && c
>= 0)
855 /* If two previous characters were " and \,
856 this is a doc string. Otherwise, there is none. */
857 if (c2
!= '"' || c1
!= '\\')
860 fprintf (stderr
, "## non-docstring in %s (%s)\n",
868 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
875 read_lisp_symbol (infile
, buffer
);
880 fprintf (stderr
, "## unparsable name in fset in %s\n",
884 read_lisp_symbol (infile
, buffer
);
885 if (strcmp (buffer
, "quote"))
887 fprintf (stderr
, "## unparsable name in fset in %s\n",
891 read_lisp_symbol (infile
, buffer
);
896 "## unparsable quoted name in fset in %s\n",
902 if (saved_string
== 0)
904 /* Skip to end of line; remember the two previous chars. */
905 while (c
!= '\n' && c
!= '\r' && c
>= 0)
912 /* If two previous characters were " and \,
913 this is a doc string. Otherwise, there is none. */
914 if (c2
!= '"' || c1
!= '\\')
917 fprintf (stderr
, "## non-docstring in %s (%s)\n",
925 else if (! strcmp (buffer
, "autoload"))
930 read_lisp_symbol (infile
, buffer
);
935 fprintf (stderr
, "## unparsable name in autoload in %s\n",
939 read_lisp_symbol (infile
, buffer
);
940 if (strcmp (buffer
, "quote"))
942 fprintf (stderr
, "## unparsable name in autoload in %s\n",
946 read_lisp_symbol (infile
, buffer
);
951 "## unparsable quoted name in autoload in %s\n",
957 if ((c
= getc (infile
)) != '\"')
959 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
963 read_c_string_or_comment (infile
, 0, 0);
966 if (saved_string
== 0)
968 /* If the next three characters aren't `dquote bslash newline'
969 then we're not reading a docstring. */
970 if ((c
= getc (infile
)) != '"'
971 || (c
= getc (infile
)) != '\\'
972 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
975 fprintf (stderr
, "## non-docstring in %s (%s)\n",
984 else if (! strcmp (buffer
, "if")
985 || ! strcmp (buffer
, "byte-code"))
992 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
998 /* At this point, we should either use the previous
999 dynamic doc string in saved_string
1000 or gobble a doc string from the input file.
1002 In the latter case, the opening quote (and leading
1003 backslash-newline) have already been read. */
1005 putc (037, outfile
);
1006 putc (type
, outfile
);
1007 fprintf (outfile
, "%s\n", buffer
);
1010 fputs (saved_string
, outfile
);
1011 /* Don't use one dynamic doc string twice. */
1012 free (saved_string
);
1016 read_c_string_or_comment (infile
, 1, 0);