1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 1986, 1992, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* The arguments given to this program are all the C and Lisp source files
21 of GNU Emacs. .elc and .el and .c files are allowed.
22 A .o file can also be specified; the .c file it was made from is used.
23 This helps the makefile pass the correct list of files.
25 The results, which go to standard output or to a file
26 specified with -a or -o (-a to append, -o to start from nothing),
27 are entries containing function or variable names and their documentation.
28 Each entry starts with a ^_ character.
29 Then comes F for a function or V for a variable.
30 Then comes the function or variable name, terminated with a newline.
31 Then comes the documentation for that function or variable.
42 #endif /* WINDOWSNT */
45 #define READ_TEXT "rt"
46 #define READ_BINARY "rb"
47 #else /* not DOS_NT */
49 #define READ_BINARY "r"
50 #endif /* not DOS_NT */
53 int scan_lisp_file ();
56 /* Stdio stream for output to the DOC file. */
59 /* Name this program was invoked with. */
62 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
69 fprintf (stderr
, "%s: ", progname
);
70 fprintf (stderr
, s1
, s2
);
71 fprintf (stderr
, "\n");
74 /* Print error message and exit. */
85 /* Like malloc but get fatal error if memory is exhausted. */
91 char *result
= (char *) malloc (size
);
93 fatal ("virtual memory exhausted", 0);
108 /* Don't put CRs in the DOC file. */
111 (stdout
)->_flag
&= ~_IOTEXT
;
112 _setmode (fileno (stdout
), O_BINARY
);
116 _setmode (fileno (stdout
), O_BINARY
);
117 #endif /* WINDOWSNT */
121 /* If first two args are -o FILE, output to FILE. */
123 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
125 outfile
= fopen (argv
[i
+ 1], "w");
128 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
130 outfile
= fopen (argv
[i
+ 1], "a");
133 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
140 for (; i
< argc
; i
++)
143 /* Don't process one file twice. */
144 for (j
= first_infile
; j
< i
; j
++)
145 if (! strcmp (argv
[i
], argv
[j
]))
148 err_count
+= scan_file (argv
[i
]);
151 exit (err_count
> 0);
153 return err_count
> 0;
156 /* Read file FILENAME and output its doc strings to outfile. */
157 /* Return 1 if file is not found, 0 if it is found. */
163 int len
= strlen (filename
);
164 if (!strcmp (filename
+ len
- 4, ".elc"))
165 return scan_lisp_file (filename
, READ_BINARY
);
166 else if (!strcmp (filename
+ len
- 3, ".el"))
167 return scan_lisp_file (filename
, READ_TEXT
);
169 return scan_c_file (filename
, READ_TEXT
);
174 /* Skip a C string from INFILE,
175 and return the character that follows the closing ".
176 If printflag is positive, output string contents to outfile.
177 If it is negative, store contents in buf.
178 Convert escape sequences \n and \t to newline and tab;
179 discard \ followed by newline. */
182 read_c_string (infile
, printflag
)
192 while (c
!= '"' && c
!= EOF
)
209 else if (printflag
< 0)
216 /* If we had a "", concatenate the two strings. */
226 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
227 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
230 write_c_args (out
, func
, buf
, minargs
, maxargs
)
233 int minargs
, maxargs
;
240 fprintf (out
, "(%s", func
);
245 for (p
= buf
; *p
; p
++)
250 /* Notice when we start printing a new identifier. */
251 if ((('A' <= c
&& c
<= 'Z')
252 || ('a' <= c
&& c
<= 'z')
253 || ('0' <= c
&& c
<= '9')
265 if (minargs
== 0 && maxargs
> 0)
266 fprintf (out
, "&optional ");
276 /* Print the C argument list as it would appear in lisp:
277 print underscores as hyphens, and print commas as spaces.
278 Collapse adjacent spaces into one. */
279 if (c
== '_') c
= '-';
280 if (c
== ',') c
= ' ';
282 /* In C code, `default' is a reserved word, so we spell it
283 `defalt'; unmangle that here. */
285 && strncmp (p
, "defalt", 6) == 0
286 && ! (('A' <= p
[6] && p
[6] <= 'Z')
287 || ('a' <= p
[6] && p
[6] <= 'z')
288 || ('0' <= p
[6] && p
[6] <= '9')
291 fprintf (out
, "DEFAULT");
296 else if (c
!= ' ' || ! just_spaced
)
298 if (c
>= 'a' && c
<= 'z')
299 /* Upcase the letter. */
304 just_spaced
= (c
== ' ');
309 /* Read through a c file. If a .o file is named,
310 the corresponding .c file is read instead.
311 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
312 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
315 scan_c_file (filename
, mode
)
316 char *filename
, *mode
;
321 register int defunflag
;
322 register int defvarperbufferflag
;
323 register int defvarflag
;
324 int minargs
, maxargs
;
326 if (filename
[strlen (filename
) - 1] == 'o')
327 filename
[strlen (filename
) - 1] = 'c';
329 infile
= fopen (filename
, mode
);
331 /* No error if non-ex input file */
339 while (!feof (infile
))
376 defvarperbufferflag
= (c
== 'P');
389 defunflag
= c
== 'U';
404 c
= read_c_string (infile
, -1);
408 else if (defvarperbufferflag
)
412 else /* For DEFSIMPLE and DEFPRED */
420 if (defunflag
&& (commas
== 1 || commas
== 2))
424 while (c
== ' ' || c
== '\n' || c
== '\t');
428 if (commas
== 2) /* pick up minargs */
429 fscanf (infile
, "%d", &minargs
);
430 else /* pick up maxargs */
431 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
434 fscanf (infile
, "%d", &maxargs
);
441 while (c
== ' ' || c
== '\n' || c
== '\t')
444 c
= read_c_string (infile
, 0);
448 while (c
== ' ' || c
== '\n' || c
== '\t')
454 putc (defvarflag
? 'V' : 'F', outfile
);
455 fprintf (outfile
, "%s\n", buf
);
456 c
= read_c_string (infile
, 1);
458 /* If this is a defun, find the arguments and print them. If
459 this function takes MANY or UNEVALLED args, then the C source
460 won't give the names of the arguments, so we shouldn't bother
461 trying to find them. */
462 if (defunflag
&& maxargs
!= -1)
464 char argbuf
[1024], *p
= argbuf
;
471 /* Skip into arguments. */
478 /* Copy arguments into ARGBUF. */
481 *p
++ = c
= getc (infile
);
485 fprintf (outfile
, "\n\n");
486 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
495 /* Read a file of Lisp code, compiled or interpreted.
497 (defun NAME ARGS DOCSTRING ...)
498 (defmacro NAME ARGS DOCSTRING ...)
499 (autoload (quote NAME) FILE DOCSTRING ...)
500 (defvar NAME VALUE DOCSTRING)
501 (defconst NAME VALUE DOCSTRING)
502 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
503 (fset (quote NAME) #[... DOCSTRING ...])
504 (defalias (quote NAME) #[... DOCSTRING ...])
505 starting in column zero.
506 (quote NAME) may appear as 'NAME as well.
508 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
509 When we find that, we save it for the following defining-form,
510 and we use that instead of reading a doc string within that defining-form.
512 For defun, defmacro, and autoload, we know how to skip over the arglist.
513 For defvar, defconst, and fset we skip to the docstring with a kludgy
514 formatting convention: all docstrings must appear on the same line as the
515 initial open-paren (the one in column zero) and must contain a backslash
516 and a double-quote immediately after the initial double-quote. No newlines
517 must appear between the beginning of the form and the first double-quote.
518 The only source file that must follow this convention is loaddefs.el; aside
519 from that, it is always the .elc file that we look at, and they are no
520 problem because byte-compiler output follows this convention.
521 The NAME and DOCSTRING are output.
522 NAME is preceded by `F' for a function or `V' for a variable.
523 An entry is output only if DOCSTRING has \ newline just after the opening "
531 while (c
== ' ' || c
== '\t' || c
== '\n')
537 read_lisp_symbol (infile
, buffer
)
542 char *fillp
= buffer
;
549 *(++fillp
) = getc (infile
);
550 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '(' || c
== ')')
561 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
567 scan_lisp_file (filename
, mode
)
568 char *filename
, *mode
;
572 char *saved_string
= 0;
574 infile
= fopen (filename
, mode
);
578 return 0; /* No error */
582 while (!feof (infile
))
593 /* Detect a dynamic doc string and save it for the next expression. */
602 /* Read the length. */
603 while ((c
= getc (infile
),
604 c
>= '0' && c
<= '9'))
610 /* The next character is a space that is counted in the length
611 but not part of the doc string.
612 We already read it, so just ignore it. */
615 /* Read in the contents. */
616 if (saved_string
!= 0)
618 saved_string
= (char *) malloc (length
);
619 for (i
= 0; i
< length
; i
++)
620 saved_string
[i
] = getc (infile
);
621 /* The last character is a ^_.
622 That is needed in the .elc file
623 but it is redundant in DOC. So get rid of it here. */
624 saved_string
[length
- 1] = 0;
625 /* Skip the newline. */
636 read_lisp_symbol (infile
, buffer
);
638 if (! strcmp (buffer
, "defun") ||
639 ! strcmp (buffer
, "defmacro"))
642 read_lisp_symbol (infile
, buffer
);
644 /* Skip the arguments: either "nil" or a list in parens */
647 if (c
== 'n') /* nil */
649 if ((c
= getc (infile
)) != 'i' ||
650 (c
= getc (infile
)) != 'l')
652 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
659 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
668 /* If the next three characters aren't `dquote bslash newline'
669 then we're not reading a docstring.
671 if ((c
= getc (infile
)) != '"' ||
672 (c
= getc (infile
)) != '\\' ||
673 (c
= getc (infile
)) != '\n')
676 fprintf (stderr
, "## non-docstring in %s (%s)\n",
683 else if (! strcmp (buffer
, "defvar") ||
684 ! strcmp (buffer
, "defconst"))
688 read_lisp_symbol (infile
, buffer
);
690 if (saved_string
== 0)
693 /* Skip until the first newline; remember the two previous chars. */
694 while (c
!= '\n' && c
>= 0)
701 /* If two previous characters were " and \,
702 this is a doc string. Otherwise, there is none. */
703 if (c2
!= '"' || c1
!= '\\')
706 fprintf (stderr
, "## non-docstring in %s (%s)\n",
714 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
721 read_lisp_symbol (infile
, buffer
);
726 fprintf (stderr
, "## unparsable name in fset in %s\n",
730 read_lisp_symbol (infile
, buffer
);
731 if (strcmp (buffer
, "quote"))
733 fprintf (stderr
, "## unparsable name in fset in %s\n",
737 read_lisp_symbol (infile
, buffer
);
742 "## unparsable quoted name in fset in %s\n",
748 if (saved_string
== 0)
750 /* Skip until the first newline; remember the two previous chars. */
751 while (c
!= '\n' && c
>= 0)
758 /* If two previous characters were " and \,
759 this is a doc string. Otherwise, there is none. */
760 if (c2
!= '"' || c1
!= '\\')
763 fprintf (stderr
, "## non-docstring in %s (%s)\n",
771 else if (! strcmp (buffer
, "autoload"))
776 read_lisp_symbol (infile
, buffer
);
781 fprintf (stderr
, "## unparsable name in autoload in %s\n",
785 read_lisp_symbol (infile
, buffer
);
786 if (strcmp (buffer
, "quote"))
788 fprintf (stderr
, "## unparsable name in autoload in %s\n",
792 read_lisp_symbol (infile
, buffer
);
797 "## unparsable quoted name in autoload in %s\n",
803 if ((c
= getc (infile
)) != '\"')
805 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
809 read_c_string (infile
, 0);
812 if (saved_string
== 0)
814 /* If the next three characters aren't `dquote bslash newline'
815 then we're not reading a docstring. */
816 if ((c
= getc (infile
)) != '"' ||
817 (c
= getc (infile
)) != '\\' ||
818 (c
= getc (infile
)) != '\n')
821 fprintf (stderr
, "## non-docstring in %s (%s)\n",
830 else if (! strcmp (buffer
, "if") ||
831 ! strcmp (buffer
, "byte-code"))
838 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
844 /* At this point, we should either use the previous
845 dynamic doc string in saved_string
846 or gobble a doc string from the input file.
848 In the latter case, the opening quote (and leading
849 backslash-newline) have already been read. */
852 putc (type
, outfile
);
853 fprintf (outfile
, "%s\n", buffer
);
856 fputs (saved_string
, outfile
);
857 /* Don't use one dynamic doc string twice. */
862 read_c_string (infile
, 1);