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.
40 #define READ_TEXT "rt"
41 #define READ_BINARY "rb"
44 #define READ_BINARY "r"
45 #endif /* not MSDOS */
48 int scan_lisp_file ();
62 _fmode
= O_BINARY
; /* all of files are treated as binary files */
63 (stdout
)->_flag
&= ~_IOTEXT
;
64 _setmode (fileno (stdout
), O_BINARY
);
68 /* If first two args are -o FILE, output to FILE. */
70 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
72 outfile
= fopen (argv
[i
+ 1], "w");
75 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
77 outfile
= fopen (argv
[i
+ 1], "a");
80 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
87 err_count
+= scan_file (argv
[i
]); /* err_count seems to be {mis,un}used */
89 exit (err_count
); /* see below - shane */
94 /* Read file FILENAME and output its doc strings to outfile. */
95 /* Return 1 if file is not found, 0 if it is found. */
101 int len
= strlen (filename
);
102 if (!strcmp (filename
+ len
- 4, ".elc"))
103 return scan_lisp_file (filename
, READ_BINARY
);
104 else if (!strcmp (filename
+ len
- 3, ".el"))
105 return scan_lisp_file (filename
, READ_TEXT
);
107 return scan_c_file (filename
, READ_TEXT
);
112 /* Skip a C string from INFILE,
113 and return the character that follows the closing ".
114 If printflag is positive, output string contents to outfile.
115 If it is negative, store contents in buf.
116 Convert escape sequences \n and \t to newline and tab;
117 discard \ followed by newline. */
120 read_c_string (infile
, printflag
)
130 while (c
!= '"' && c
!= EOF
)
147 else if (printflag
< 0)
154 /* If we had a "", concatenate the two strings. */
164 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
165 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
168 write_c_args (out
, func
, buf
, minargs
, maxargs
)
171 int minargs
, maxargs
;
178 fprintf (out
, "(%s", func
);
183 for (p
= buf
; *p
; p
++)
188 /* Notice when we start printing a new identifier. */
189 if ((('A' <= c
&& c
<= 'Z')
190 || ('a' <= c
&& c
<= 'z')
191 || ('0' <= c
&& c
<= '9')
203 if (minargs
== 0 && maxargs
> 0)
204 fprintf (out
, "&optional ");
214 /* Print the C argument list as it would appear in lisp:
215 print underscores as hyphens, and print commas as spaces.
216 Collapse adjacent spaces into one. */
217 if (c
== '_') c
= '-';
218 if (c
== ',') c
= ' ';
220 /* In C code, `default' is a reserved word, so we spell it
221 `defalt'; unmangle that here. */
223 && strncmp (p
, "defalt", 6) == 0
224 && ! (('A' <= p
[6] && p
[6] <= 'Z')
225 || ('a' <= p
[6] && p
[6] <= 'z')
226 || ('0' <= p
[6] && p
[6] <= '9')
229 fprintf (out
, "DEFAULT");
234 else if (c
!= ' ' || ! just_spaced
)
236 if (c
>= 'a' && c
<= 'z')
237 /* Upcase the letter. */
242 just_spaced
= (c
== ' ');
247 /* Read through a c file. If a .o file is named,
248 the corresponding .c file is read instead.
249 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
250 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
253 scan_c_file (filename
, mode
)
254 char *filename
, *mode
;
259 register int defunflag
;
260 register int defvarperbufferflag
;
261 register int defvarflag
;
262 int minargs
, maxargs
;
264 if (filename
[strlen (filename
) - 1] == 'o')
265 filename
[strlen (filename
) - 1] = 'c';
267 infile
= fopen (filename
, mode
);
269 /* No error if non-ex input file */
277 while (!feof (infile
))
314 defvarperbufferflag
= (c
== 'P');
327 defunflag
= c
== 'U';
342 c
= read_c_string (infile
, -1);
346 else if (defvarperbufferflag
)
350 else /* For DEFSIMPLE and DEFPRED */
358 if (defunflag
&& (commas
== 1 || commas
== 2))
362 while (c
== ' ' || c
== '\n' || c
== '\t');
366 if (commas
== 2) /* pick up minargs */
367 fscanf (infile
, "%d", &minargs
);
368 else /* pick up maxargs */
369 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
372 fscanf (infile
, "%d", &maxargs
);
379 while (c
== ' ' || c
== '\n' || c
== '\t')
382 c
= read_c_string (infile
, 0);
386 while (c
== ' ' || c
== '\n' || c
== '\t')
392 putc (defvarflag
? 'V' : 'F', outfile
);
393 fprintf (outfile
, "%s\n", buf
);
394 c
= read_c_string (infile
, 1);
396 /* If this is a defun, find the arguments and print them. If
397 this function takes MANY or UNEVALLED args, then the C source
398 won't give the names of the arguments, so we shouldn't bother
399 trying to find them. */
400 if (defunflag
&& maxargs
!= -1)
402 char argbuf
[1024], *p
= argbuf
;
409 /* Skip into arguments. */
416 /* Copy arguments into ARGBUF. */
419 *p
++ = c
= getc (infile
);
423 fprintf (outfile
, "\n\n");
424 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
433 /* Read a file of Lisp code, compiled or interpreted.
435 (defun NAME ARGS DOCSTRING ...)
436 (defmacro NAME ARGS DOCSTRING ...)
437 (autoload (quote NAME) FILE DOCSTRING ...)
438 (defvar NAME VALUE DOCSTRING)
439 (defconst NAME VALUE DOCSTRING)
440 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
441 (fset (quote NAME) #[... DOCSTRING ...])
442 (defalias (quote NAME) #[... DOCSTRING ...])
443 starting in column zero.
444 (quote NAME) may appear as 'NAME as well.
445 For defun, defmacro, and autoload, we know how to skip over the arglist.
446 For defvar, defconst, and fset we skip to the docstring with a kludgy
447 formatting convention: all docstrings must appear on the same line as the
448 initial open-paren (the one in column zero) and must contain a backslash
449 and a double-quote immediately after the initial double-quote. No newlines
450 must appear between the beginning of the form and the first double-quote.
451 The only source file that must follow this convention is loaddefs.el; aside
452 from that, it is always the .elc file that we look at, and they are no
453 problem because byte-compiler output follows this convention.
454 The NAME and DOCSTRING are output.
455 NAME is preceded by `F' for a function or `V' for a variable.
456 An entry is output only if DOCSTRING has \ newline just after the opening "
464 while (c
== ' ' || c
== '\t' || c
== '\n')
470 read_lisp_symbol (infile
, buffer
)
475 char *fillp
= buffer
;
482 *(++fillp
) = getc (infile
);
483 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '(' || c
== ')')
494 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
500 scan_lisp_file (filename
, mode
)
501 char *filename
, *mode
;
506 infile
= fopen (filename
, mode
);
510 return 0; /* No error */
514 while (!feof (infile
))
516 char buffer
[BUFSIZ
];
528 read_lisp_symbol (infile
, buffer
);
530 if (! strcmp (buffer
, "defun") ||
531 ! strcmp (buffer
, "defmacro"))
534 read_lisp_symbol (infile
, buffer
);
536 /* Skip the arguments: either "nil" or a list in parens */
539 if (c
== 'n') /* nil */
541 if ((c
= getc (infile
)) != 'i' ||
542 (c
= getc (infile
)) != 'l')
544 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
551 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
560 /* If the next three characters aren't `dquote bslash newline'
561 then we're not reading a docstring.
563 if ((c
= getc (infile
)) != '"' ||
564 (c
= getc (infile
)) != '\\' ||
565 (c
= getc (infile
)) != '\n')
568 fprintf (stderr
, "## non-docstring in %s (%s)\n",
575 else if (! strcmp (buffer
, "defvar") ||
576 ! strcmp (buffer
, "defconst"))
580 read_lisp_symbol (infile
, buffer
);
582 /* Skip until the first newline; remember the two previous chars. */
583 while (c
!= '\n' && c
>= 0)
590 /* If two previous characters were " and \,
591 this is a doc string. Otherwise, there is none. */
592 if (c2
!= '"' || c1
!= '\\')
595 fprintf (stderr
, "## non-docstring in %s (%s)\n",
602 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
609 read_lisp_symbol (infile
, buffer
);
614 fprintf (stderr
, "## unparsable name in fset in %s\n",
618 read_lisp_symbol (infile
, buffer
);
619 if (strcmp (buffer
, "quote"))
621 fprintf (stderr
, "## unparsable name in fset in %s\n",
625 read_lisp_symbol (infile
, buffer
);
630 "## unparsable quoted name in fset in %s\n",
636 /* Skip until the first newline; remember the two previous chars. */
637 while (c
!= '\n' && c
>= 0)
644 /* If two previous characters were " and \,
645 this is a doc string. Otherwise, there is none. */
646 if (c2
!= '"' || c1
!= '\\')
649 fprintf (stderr
, "## non-docstring in %s (%s)\n",
656 else if (! strcmp (buffer
, "autoload"))
661 read_lisp_symbol (infile
, buffer
);
666 fprintf (stderr
, "## unparsable name in autoload in %s\n",
670 read_lisp_symbol (infile
, buffer
);
671 if (strcmp (buffer
, "quote"))
673 fprintf (stderr
, "## unparsable name in autoload in %s\n",
677 read_lisp_symbol (infile
, buffer
);
682 "## unparsable quoted name in autoload in %s\n",
688 if ((c
= getc (infile
)) != '\"')
690 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
694 read_c_string (infile
, 0);
697 /* If the next three characters aren't `dquote bslash newline'
698 then we're not reading a docstring.
700 if ((c
= getc (infile
)) != '"' ||
701 (c
= getc (infile
)) != '\\' ||
702 (c
= getc (infile
)) != '\n')
705 fprintf (stderr
, "## non-docstring in %s (%s)\n",
713 else if (! strcmp (buffer
, "if") ||
714 ! strcmp (buffer
, "byte-code"))
721 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
727 /* At this point, there is a docstring that we should gobble.
728 The opening quote (and leading backslash-newline) have already
732 putc (type
, outfile
);
733 fprintf (outfile
, "%s\n", buffer
);
734 read_c_string (infile
, 1);