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 ();
63 _fmode
= O_BINARY
; /* all of files are treated as binary files */
64 (stdout
)->_flag
&= ~_IOTEXT
;
65 _setmode (fileno (stdout
), O_BINARY
);
69 /* If first two args are -o FILE, output to FILE. */
71 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
73 outfile
= fopen (argv
[i
+ 1], "w");
76 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
78 outfile
= fopen (argv
[i
+ 1], "a");
81 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
91 /* Don't process one file twice. */
92 for (j
= first_infile
; j
< i
; j
++)
93 if (! strcmp (argv
[i
], argv
[j
]))
96 err_count
+= scan_file (argv
[i
]);
101 return err_count
> 0;
104 /* Read file FILENAME and output its doc strings to outfile. */
105 /* Return 1 if file is not found, 0 if it is found. */
111 int len
= strlen (filename
);
112 if (!strcmp (filename
+ len
- 4, ".elc"))
113 return scan_lisp_file (filename
, READ_BINARY
);
114 else if (!strcmp (filename
+ len
- 3, ".el"))
115 return scan_lisp_file (filename
, READ_TEXT
);
117 return scan_c_file (filename
, READ_TEXT
);
122 /* Skip a C string from INFILE,
123 and return the character that follows the closing ".
124 If printflag is positive, output string contents to outfile.
125 If it is negative, store contents in buf.
126 Convert escape sequences \n and \t to newline and tab;
127 discard \ followed by newline. */
130 read_c_string (infile
, printflag
)
140 while (c
!= '"' && c
!= EOF
)
157 else if (printflag
< 0)
164 /* If we had a "", concatenate the two strings. */
174 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
175 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
178 write_c_args (out
, func
, buf
, minargs
, maxargs
)
181 int minargs
, maxargs
;
188 fprintf (out
, "(%s", func
);
193 for (p
= buf
; *p
; p
++)
198 /* Notice when we start printing a new identifier. */
199 if ((('A' <= c
&& c
<= 'Z')
200 || ('a' <= c
&& c
<= 'z')
201 || ('0' <= c
&& c
<= '9')
213 if (minargs
== 0 && maxargs
> 0)
214 fprintf (out
, "&optional ");
224 /* Print the C argument list as it would appear in lisp:
225 print underscores as hyphens, and print commas as spaces.
226 Collapse adjacent spaces into one. */
227 if (c
== '_') c
= '-';
228 if (c
== ',') c
= ' ';
230 /* In C code, `default' is a reserved word, so we spell it
231 `defalt'; unmangle that here. */
233 && strncmp (p
, "defalt", 6) == 0
234 && ! (('A' <= p
[6] && p
[6] <= 'Z')
235 || ('a' <= p
[6] && p
[6] <= 'z')
236 || ('0' <= p
[6] && p
[6] <= '9')
239 fprintf (out
, "DEFAULT");
244 else if (c
!= ' ' || ! just_spaced
)
246 if (c
>= 'a' && c
<= 'z')
247 /* Upcase the letter. */
252 just_spaced
= (c
== ' ');
257 /* Read through a c file. If a .o file is named,
258 the corresponding .c file is read instead.
259 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
260 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
263 scan_c_file (filename
, mode
)
264 char *filename
, *mode
;
269 register int defunflag
;
270 register int defvarperbufferflag
;
271 register int defvarflag
;
272 int minargs
, maxargs
;
274 if (filename
[strlen (filename
) - 1] == 'o')
275 filename
[strlen (filename
) - 1] = 'c';
277 infile
= fopen (filename
, mode
);
279 /* No error if non-ex input file */
287 while (!feof (infile
))
324 defvarperbufferflag
= (c
== 'P');
337 defunflag
= c
== 'U';
352 c
= read_c_string (infile
, -1);
356 else if (defvarperbufferflag
)
360 else /* For DEFSIMPLE and DEFPRED */
368 if (defunflag
&& (commas
== 1 || commas
== 2))
372 while (c
== ' ' || c
== '\n' || c
== '\t');
376 if (commas
== 2) /* pick up minargs */
377 fscanf (infile
, "%d", &minargs
);
378 else /* pick up maxargs */
379 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
382 fscanf (infile
, "%d", &maxargs
);
389 while (c
== ' ' || c
== '\n' || c
== '\t')
392 c
= read_c_string (infile
, 0);
396 while (c
== ' ' || c
== '\n' || c
== '\t')
402 putc (defvarflag
? 'V' : 'F', outfile
);
403 fprintf (outfile
, "%s\n", buf
);
404 c
= read_c_string (infile
, 1);
406 /* If this is a defun, find the arguments and print them. If
407 this function takes MANY or UNEVALLED args, then the C source
408 won't give the names of the arguments, so we shouldn't bother
409 trying to find them. */
410 if (defunflag
&& maxargs
!= -1)
412 char argbuf
[1024], *p
= argbuf
;
419 /* Skip into arguments. */
426 /* Copy arguments into ARGBUF. */
429 *p
++ = c
= getc (infile
);
433 fprintf (outfile
, "\n\n");
434 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
443 /* Read a file of Lisp code, compiled or interpreted.
445 (defun NAME ARGS DOCSTRING ...)
446 (defmacro NAME ARGS DOCSTRING ...)
447 (autoload (quote NAME) FILE DOCSTRING ...)
448 (defvar NAME VALUE DOCSTRING)
449 (defconst NAME VALUE DOCSTRING)
450 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
451 (fset (quote NAME) #[... DOCSTRING ...])
452 (defalias (quote NAME) #[... DOCSTRING ...])
453 starting in column zero.
454 (quote NAME) may appear as 'NAME as well.
455 For defun, defmacro, and autoload, we know how to skip over the arglist.
456 For defvar, defconst, and fset we skip to the docstring with a kludgy
457 formatting convention: all docstrings must appear on the same line as the
458 initial open-paren (the one in column zero) and must contain a backslash
459 and a double-quote immediately after the initial double-quote. No newlines
460 must appear between the beginning of the form and the first double-quote.
461 The only source file that must follow this convention is loaddefs.el; aside
462 from that, it is always the .elc file that we look at, and they are no
463 problem because byte-compiler output follows this convention.
464 The NAME and DOCSTRING are output.
465 NAME is preceded by `F' for a function or `V' for a variable.
466 An entry is output only if DOCSTRING has \ newline just after the opening "
474 while (c
== ' ' || c
== '\t' || c
== '\n')
480 read_lisp_symbol (infile
, buffer
)
485 char *fillp
= buffer
;
492 *(++fillp
) = getc (infile
);
493 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '(' || c
== ')')
504 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
510 scan_lisp_file (filename
, mode
)
511 char *filename
, *mode
;
516 infile
= fopen (filename
, mode
);
520 return 0; /* No error */
524 while (!feof (infile
))
526 char buffer
[BUFSIZ
];
538 read_lisp_symbol (infile
, buffer
);
540 if (! strcmp (buffer
, "defun") ||
541 ! strcmp (buffer
, "defmacro"))
544 read_lisp_symbol (infile
, buffer
);
546 /* Skip the arguments: either "nil" or a list in parens */
549 if (c
== 'n') /* nil */
551 if ((c
= getc (infile
)) != 'i' ||
552 (c
= getc (infile
)) != 'l')
554 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
561 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
570 /* If the next three characters aren't `dquote bslash newline'
571 then we're not reading a docstring.
573 if ((c
= getc (infile
)) != '"' ||
574 (c
= getc (infile
)) != '\\' ||
575 (c
= getc (infile
)) != '\n')
578 fprintf (stderr
, "## non-docstring in %s (%s)\n",
585 else if (! strcmp (buffer
, "defvar") ||
586 ! strcmp (buffer
, "defconst"))
590 read_lisp_symbol (infile
, buffer
);
592 /* Skip until the first newline; remember the two previous chars. */
593 while (c
!= '\n' && c
>= 0)
600 /* If two previous characters were " and \,
601 this is a doc string. Otherwise, there is none. */
602 if (c2
!= '"' || c1
!= '\\')
605 fprintf (stderr
, "## non-docstring in %s (%s)\n",
612 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
619 read_lisp_symbol (infile
, buffer
);
624 fprintf (stderr
, "## unparsable name in fset in %s\n",
628 read_lisp_symbol (infile
, buffer
);
629 if (strcmp (buffer
, "quote"))
631 fprintf (stderr
, "## unparsable name in fset in %s\n",
635 read_lisp_symbol (infile
, buffer
);
640 "## unparsable quoted name in fset in %s\n",
646 /* Skip until the first newline; remember the two previous chars. */
647 while (c
!= '\n' && c
>= 0)
654 /* If two previous characters were " and \,
655 this is a doc string. Otherwise, there is none. */
656 if (c2
!= '"' || c1
!= '\\')
659 fprintf (stderr
, "## non-docstring in %s (%s)\n",
666 else if (! strcmp (buffer
, "autoload"))
671 read_lisp_symbol (infile
, buffer
);
676 fprintf (stderr
, "## unparsable name in autoload in %s\n",
680 read_lisp_symbol (infile
, buffer
);
681 if (strcmp (buffer
, "quote"))
683 fprintf (stderr
, "## unparsable name in autoload in %s\n",
687 read_lisp_symbol (infile
, buffer
);
692 "## unparsable quoted name in autoload in %s\n",
698 if ((c
= getc (infile
)) != '\"')
700 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
704 read_c_string (infile
, 0);
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')
715 fprintf (stderr
, "## non-docstring in %s (%s)\n",
723 else if (! strcmp (buffer
, "if") ||
724 ! strcmp (buffer
, "byte-code"))
731 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
737 /* At this point, there is a docstring that we should gobble.
738 The opening quote (and leading backslash-newline) have already
742 putc (type
, outfile
);
743 fprintf (outfile
, "%s\n", buffer
);
744 read_c_string (infile
, 1);