]>
code.delx.au - gnu-emacs/blob - lib-src/make-docfile.c
1 /* Generate doc-string file for GNU Emacs from source files.
2 Copyright (C) 1985, 1986, 92, 93, 94, 1997 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 */
36 #include <../src/config.h>
46 #endif /* WINDOWSNT */
49 #define READ_TEXT "rt"
50 #define READ_BINARY "rb"
51 #else /* not DOS_NT */
53 #define READ_BINARY "r"
54 #endif /* not DOS_NT */
57 int scan_lisp_file ();
61 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
62 file where that function is defined. */
74 /* Stdio stream for output to the DOC file. */
77 /* Name this program was invoked with. */
80 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
87 fprintf (stderr
, "%s: ", progname
);
88 fprintf (stderr
, s1
, s2
);
89 fprintf (stderr
, "\n");
92 /* Print error message and exit. */
103 /* Like malloc but get fatal error if memory is exhausted. */
109 long *result
= (long *) malloc (size
);
111 fatal ("virtual memory exhausted", 0);
128 /* Don't put CRs in the DOC file. */
131 #if 0 /* Suspicion is that this causes hanging.
132 So instead we require people to use -o on MSDOS. */
133 (stdout
)->_flag
&= ~_IOTEXT
;
134 _setmode (fileno (stdout
), O_BINARY
);
140 _setmode (fileno (stdout
), O_BINARY
);
141 #endif /* WINDOWSNT */
143 /* If first two args are -o FILE, output to FILE. */
145 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
147 outfile
= fopen (argv
[i
+ 1], "w");
150 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
152 outfile
= fopen (argv
[i
+ 1], "a");
155 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
162 fatal ("No output file specified", "");
165 for (; i
< argc
; i
++)
168 /* Don't process one file twice. */
169 for (j
= first_infile
; j
< i
; j
++)
170 if (! strcmp (argv
[i
], argv
[j
]))
173 err_count
+= scan_file (argv
[i
]);
176 exit (err_count
> 0);
178 return err_count
> 0;
181 /* Read file FILENAME and output its doc strings to outfile. */
182 /* Return 1 if file is not found, 0 if it is found. */
188 int len
= strlen (filename
);
189 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
190 return scan_lisp_file (filename
, READ_BINARY
);
191 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
192 return scan_lisp_file (filename
, READ_TEXT
);
194 return scan_c_file (filename
, READ_TEXT
);
199 /* Skip a C string from INFILE,
200 and return the character that follows the closing ".
201 If printflag is positive, output string contents to outfile.
202 If it is negative, store contents in buf.
203 Convert escape sequences \n and \t to newline and tab;
204 discard \ followed by newline. */
207 read_c_string (infile
, printflag
)
217 while (c
!= '"' && c
!= EOF
)
234 else if (printflag
< 0)
241 /* If we had a "", concatenate the two strings. */
251 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
252 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
255 write_c_args (out
, func
, buf
, minargs
, maxargs
)
258 int minargs
, maxargs
;
265 fprintf (out
, "(%s", func
);
270 for (p
= buf
; *p
; p
++)
275 /* Notice when we start printing a new identifier. */
276 if ((('A' <= c
&& c
<= 'Z')
277 || ('a' <= c
&& c
<= 'z')
278 || ('0' <= c
&& c
<= '9')
290 if (minargs
== 0 && maxargs
> 0)
291 fprintf (out
, "&optional ");
301 /* Print the C argument list as it would appear in lisp:
302 print underscores as hyphens, and print commas as spaces.
303 Collapse adjacent spaces into one. */
304 if (c
== '_') c
= '-';
305 if (c
== ',') c
= ' ';
307 /* In C code, `default' is a reserved word, so we spell it
308 `defalt'; unmangle that here. */
310 && strncmp (p
, "defalt", 6) == 0
311 && ! (('A' <= p
[6] && p
[6] <= 'Z')
312 || ('a' <= p
[6] && p
[6] <= 'z')
313 || ('0' <= p
[6] && p
[6] <= '9')
316 fprintf (out
, "DEFAULT");
321 else if (c
!= ' ' || ! just_spaced
)
323 if (c
>= 'a' && c
<= 'z')
324 /* Upcase the letter. */
329 just_spaced
= (c
== ' ');
334 /* Read through a c file. If a .o file is named,
335 the corresponding .c file is read instead.
336 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
337 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
340 scan_c_file (filename
, mode
)
341 char *filename
, *mode
;
346 register int defunflag
;
347 register int defvarperbufferflag
;
348 register int defvarflag
;
349 int minargs
, maxargs
;
350 int extension
= filename
[strlen (filename
) - 1];
352 if (extension
== 'o')
353 filename
[strlen (filename
) - 1] = 'c';
355 infile
= fopen (filename
, mode
);
357 /* No error if non-ex input file */
364 /* Reset extension to be able to detect duplicate files. */
365 filename
[strlen (filename
) - 1] = extension
;
368 while (!feof (infile
))
405 defvarperbufferflag
= (c
== 'P');
418 defunflag
= c
== 'U';
433 c
= read_c_string (infile
, -1);
437 else if (defvarperbufferflag
)
441 else /* For DEFSIMPLE and DEFPRED */
449 if (defunflag
&& (commas
== 1 || commas
== 2))
453 while (c
== ' ' || c
== '\n' || c
== '\t');
457 if (commas
== 2) /* pick up minargs */
458 fscanf (infile
, "%d", &minargs
);
459 else /* pick up maxargs */
460 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
463 fscanf (infile
, "%d", &maxargs
);
470 while (c
== ' ' || c
== '\n' || c
== '\t')
473 c
= read_c_string (infile
, 0);
477 while (c
== ' ' || c
== '\n' || c
== '\t')
483 putc (defvarflag
? 'V' : 'F', outfile
);
484 fprintf (outfile
, "%s\n", buf
);
485 c
= read_c_string (infile
, 1);
487 /* If this is a defun, find the arguments and print them. If
488 this function takes MANY or UNEVALLED args, then the C source
489 won't give the names of the arguments, so we shouldn't bother
490 trying to find them. */
491 if (defunflag
&& maxargs
!= -1)
493 char argbuf
[1024], *p
= argbuf
;
500 /* Skip into arguments. */
507 /* Copy arguments into ARGBUF. */
510 *p
++ = c
= getc (infile
);
514 fprintf (outfile
, "\n\n");
515 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
524 /* Read a file of Lisp code, compiled or interpreted.
526 (defun NAME ARGS DOCSTRING ...)
527 (defmacro NAME ARGS DOCSTRING ...)
528 (autoload (quote NAME) FILE DOCSTRING ...)
529 (defvar NAME VALUE DOCSTRING)
530 (defconst NAME VALUE DOCSTRING)
531 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
532 (fset (quote NAME) #[... DOCSTRING ...])
533 (defalias (quote NAME) #[... DOCSTRING ...])
534 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
535 starting in column zero.
536 (quote NAME) may appear as 'NAME as well.
538 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
539 When we find that, we save it for the following defining-form,
540 and we use that instead of reading a doc string within that defining-form.
542 For defun, defmacro, and autoload, we know how to skip over the arglist.
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 double-quote immediately after the initial double-quote. No newlines
547 must appear between the beginning of the form and the first double-quote.
548 The only source file that must follow this convention is loaddefs.el; aside
549 from that, it is always the .elc file that we look at, and they are no
550 problem because byte-compiler output follows this convention.
551 The NAME and DOCSTRING are output.
552 NAME is preceded by `F' for a function or `V' for a variable.
553 An entry is output only if DOCSTRING has \ newline just after the opening "
561 while (c
== ' ' || c
== '\t' || c
== '\n')
567 read_lisp_symbol (infile
, buffer
)
572 char *fillp
= buffer
;
579 *(++fillp
) = getc (infile
);
580 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '(' || c
== ')')
591 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
597 scan_lisp_file (filename
, mode
)
598 char *filename
, *mode
;
602 char *saved_string
= 0;
604 infile
= fopen (filename
, mode
);
608 return 0; /* No error */
612 while (!feof (infile
))
623 /* Detect a dynamic doc string and save it for the next expression. */
632 /* Read the length. */
633 while ((c
= getc (infile
),
634 c
>= '0' && c
<= '9'))
640 /* The next character is a space that is counted in the length
641 but not part of the doc string.
642 We already read it, so just ignore it. */
645 /* Read in the contents. */
646 if (saved_string
!= 0)
648 saved_string
= (char *) malloc (length
);
649 for (i
= 0; i
< length
; i
++)
650 saved_string
[i
] = getc (infile
);
651 /* The last character is a ^_.
652 That is needed in the .elc file
653 but it is redundant in DOC. So get rid of it here. */
654 saved_string
[length
- 1] = 0;
655 /* Skip the newline. */
666 read_lisp_symbol (infile
, buffer
);
668 if (! strcmp (buffer
, "defun") ||
669 ! strcmp (buffer
, "defmacro"))
672 read_lisp_symbol (infile
, buffer
);
674 /* Skip the arguments: either "nil" or a list in parens */
677 if (c
== 'n') /* nil */
679 if ((c
= getc (infile
)) != 'i' ||
680 (c
= getc (infile
)) != 'l')
682 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
689 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
698 /* If the next three characters aren't `dquote bslash newline'
699 then we're not reading a docstring.
701 if ((c
= getc (infile
)) != '"' ||
702 (c
= getc (infile
)) != '\\' ||
703 (c
= getc (infile
)) != '\n')
706 fprintf (stderr
, "## non-docstring in %s (%s)\n",
713 else if (! strcmp (buffer
, "defvar") ||
714 ! strcmp (buffer
, "defconst"))
718 read_lisp_symbol (infile
, buffer
);
720 if (saved_string
== 0)
723 /* Skip until the first newline; remember the two previous chars. */
724 while (c
!= '\n' && c
>= 0)
731 /* If two previous characters were " and \,
732 this is a doc string. Otherwise, there is none. */
733 if (c2
!= '"' || c1
!= '\\')
736 fprintf (stderr
, "## non-docstring in %s (%s)\n",
744 else if (! strcmp (buffer
, "custom-declare-variable"))
751 read_lisp_symbol (infile
, buffer
);
757 "## unparsable name in custom-declare-variable in %s\n",
761 read_lisp_symbol (infile
, buffer
);
762 if (strcmp (buffer
, "quote"))
765 "## unparsable name in custom-declare-variable in %s\n",
769 read_lisp_symbol (infile
, buffer
);
774 "## unparsable quoted name in custom-declare-variable in %s\n",
780 if (saved_string
== 0)
782 /* Skip until the first newline; remember the two previous
784 while (c
!= '\n' && c
>= 0)
791 /* If two previous characters were " and \,
792 this is a doc string. Otherwise, there is none. */
793 if (c2
!= '"' || c1
!= '\\')
796 fprintf (stderr
, "## non-docstring in %s (%s)\n",
804 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
811 read_lisp_symbol (infile
, buffer
);
816 fprintf (stderr
, "## unparsable name in fset in %s\n",
820 read_lisp_symbol (infile
, buffer
);
821 if (strcmp (buffer
, "quote"))
823 fprintf (stderr
, "## unparsable name in fset in %s\n",
827 read_lisp_symbol (infile
, buffer
);
832 "## unparsable quoted name in fset in %s\n",
838 if (saved_string
== 0)
840 /* Skip until the first newline; remember the two previous chars. */
841 while (c
!= '\n' && c
>= 0)
848 /* If two previous characters were " and \,
849 this is a doc string. Otherwise, there is none. */
850 if (c2
!= '"' || c1
!= '\\')
853 fprintf (stderr
, "## non-docstring in %s (%s)\n",
861 else if (! strcmp (buffer
, "autoload"))
866 read_lisp_symbol (infile
, buffer
);
871 fprintf (stderr
, "## unparsable name in autoload in %s\n",
875 read_lisp_symbol (infile
, buffer
);
876 if (strcmp (buffer
, "quote"))
878 fprintf (stderr
, "## unparsable name in autoload in %s\n",
882 read_lisp_symbol (infile
, buffer
);
887 "## unparsable quoted name in autoload in %s\n",
893 if ((c
= getc (infile
)) != '\"')
895 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
899 read_c_string (infile
, 0);
902 if (saved_string
== 0)
904 /* If the next three characters aren't `dquote bslash newline'
905 then we're not reading a docstring. */
906 if ((c
= getc (infile
)) != '"' ||
907 (c
= getc (infile
)) != '\\' ||
908 (c
= getc (infile
)) != '\n')
911 fprintf (stderr
, "## non-docstring in %s (%s)\n",
920 else if (! strcmp (buffer
, "if") ||
921 ! strcmp (buffer
, "byte-code"))
928 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
934 /* At this point, we should either use the previous
935 dynamic doc string in saved_string
936 or gobble a doc string from the input file.
938 In the latter case, the opening quote (and leading
939 backslash-newline) have already been read. */
942 putc (type
, outfile
);
943 fprintf (outfile
, "%s\n", buffer
);
946 fputs (saved_string
, outfile
);
947 /* Don't use one dynamic doc string twice. */
952 read_c_string (infile
, 1);