]>
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 */
38 /* defined to be emacs_main, sys_fopen, etc. in config.h */
51 #endif /* WINDOWSNT */
54 #define READ_TEXT "rt"
55 #define READ_BINARY "rb"
56 #else /* not DOS_NT */
58 #define READ_BINARY "r"
59 #endif /* not DOS_NT */
62 int scan_lisp_file ();
66 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
67 file where that function is defined. */
79 /* Stdio stream for output to the DOC file. */
82 /* Name this program was invoked with. */
85 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
92 fprintf (stderr
, "%s: ", progname
);
93 fprintf (stderr
, s1
, s2
);
94 fprintf (stderr
, "\n");
97 /* Print error message and exit. */
108 /* Like malloc but get fatal error if memory is exhausted. */
114 long *result
= (long *) malloc (size
);
116 fatal ("virtual memory exhausted", 0);
133 /* Don't put CRs in the DOC file. */
136 #if 0 /* Suspicion is that this causes hanging.
137 So instead we require people to use -o on MSDOS. */
138 (stdout
)->_flag
&= ~_IOTEXT
;
139 _setmode (fileno (stdout
), O_BINARY
);
145 _setmode (fileno (stdout
), O_BINARY
);
146 #endif /* WINDOWSNT */
148 /* If first two args are -o FILE, output to FILE. */
150 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
152 outfile
= fopen (argv
[i
+ 1], "w");
155 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
157 outfile
= fopen (argv
[i
+ 1], "a");
160 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
167 fatal ("No output file specified", "");
170 for (; i
< argc
; i
++)
173 /* Don't process one file twice. */
174 for (j
= first_infile
; j
< i
; j
++)
175 if (! strcmp (argv
[i
], argv
[j
]))
178 err_count
+= scan_file (argv
[i
]);
181 exit (err_count
> 0);
183 return err_count
> 0;
186 /* Read file FILENAME and output its doc strings to outfile. */
187 /* Return 1 if file is not found, 0 if it is found. */
193 int len
= strlen (filename
);
194 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
195 return scan_lisp_file (filename
, READ_BINARY
);
196 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
197 return scan_lisp_file (filename
, READ_TEXT
);
199 return scan_c_file (filename
, READ_TEXT
);
204 /* Skip a C string from INFILE,
205 and return the character that follows the closing ".
206 If printflag is positive, output string contents to outfile.
207 If it is negative, store contents in buf.
208 Convert escape sequences \n and \t to newline and tab;
209 discard \ followed by newline. */
212 read_c_string (infile
, printflag
)
222 while (c
!= '"' && c
!= EOF
)
227 if (c
== '\n' || c
== '\r')
239 else if (printflag
< 0)
246 /* If we had a "", concatenate the two strings. */
256 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
257 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
260 write_c_args (out
, func
, buf
, minargs
, maxargs
)
263 int minargs
, maxargs
;
270 fprintf (out
, "(%s", func
);
275 for (p
= buf
; *p
; p
++)
280 /* Notice when we start printing a new identifier. */
281 if ((('A' <= c
&& c
<= 'Z')
282 || ('a' <= c
&& c
<= 'z')
283 || ('0' <= c
&& c
<= '9')
295 if (minargs
== 0 && maxargs
> 0)
296 fprintf (out
, "&optional ");
306 /* Print the C argument list as it would appear in lisp:
307 print underscores as hyphens, and print commas as spaces.
308 Collapse adjacent spaces into one. */
309 if (c
== '_') c
= '-';
310 if (c
== ',') c
= ' ';
312 /* In C code, `default' is a reserved word, so we spell it
313 `defalt'; unmangle that here. */
315 && strncmp (p
, "defalt", 6) == 0
316 && ! (('A' <= p
[6] && p
[6] <= 'Z')
317 || ('a' <= p
[6] && p
[6] <= 'z')
318 || ('0' <= p
[6] && p
[6] <= '9')
321 fprintf (out
, "DEFAULT");
326 else if (c
!= ' ' || ! just_spaced
)
328 if (c
>= 'a' && c
<= 'z')
329 /* Upcase the letter. */
334 just_spaced
= (c
== ' ');
339 /* Read through a c file. If a .o file is named,
340 the corresponding .c file is read instead.
341 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
342 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
345 scan_c_file (filename
, mode
)
346 char *filename
, *mode
;
351 register int defunflag
;
352 register int defvarperbufferflag
;
353 register int defvarflag
;
354 int minargs
, maxargs
;
355 int extension
= filename
[strlen (filename
) - 1];
357 if (extension
== 'o')
358 filename
[strlen (filename
) - 1] = 'c';
360 infile
= fopen (filename
, mode
);
362 /* No error if non-ex input file */
369 /* Reset extension to be able to detect duplicate files. */
370 filename
[strlen (filename
) - 1] = extension
;
373 while (!feof (infile
))
375 if (c
!= '\n' && c
!= '\r')
410 defvarperbufferflag
= (c
== 'P');
423 defunflag
= c
== 'U';
438 c
= read_c_string (infile
, -1);
442 else if (defvarperbufferflag
)
446 else /* For DEFSIMPLE and DEFPRED */
454 if (defunflag
&& (commas
== 1 || commas
== 2))
458 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
462 if (commas
== 2) /* pick up minargs */
463 fscanf (infile
, "%d", &minargs
);
464 else /* pick up maxargs */
465 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
468 fscanf (infile
, "%d", &maxargs
);
475 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
478 c
= read_c_string (infile
, 0);
482 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
488 putc (defvarflag
? 'V' : 'F', outfile
);
489 fprintf (outfile
, "%s\n", buf
);
490 c
= read_c_string (infile
, 1);
492 /* If this is a defun, find the arguments and print them. If
493 this function takes MANY or UNEVALLED args, then the C source
494 won't give the names of the arguments, so we shouldn't bother
495 trying to find them. */
496 if (defunflag
&& maxargs
!= -1)
498 char argbuf
[1024], *p
= argbuf
;
505 /* Skip into arguments. */
512 /* Copy arguments into ARGBUF. */
515 *p
++ = c
= getc (infile
);
519 fprintf (outfile
, "\n\n");
520 write_c_args (outfile
, buf
, argbuf
, minargs
, maxargs
);
529 /* Read a file of Lisp code, compiled or interpreted.
531 (defun NAME ARGS DOCSTRING ...)
532 (defmacro NAME ARGS DOCSTRING ...)
533 (autoload (quote NAME) FILE DOCSTRING ...)
534 (defvar NAME VALUE DOCSTRING)
535 (defconst NAME VALUE DOCSTRING)
536 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
537 (fset (quote NAME) #[... DOCSTRING ...])
538 (defalias (quote NAME) #[... DOCSTRING ...])
539 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
540 starting in column zero.
541 (quote NAME) may appear as 'NAME as well.
543 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
544 When we find that, we save it for the following defining-form,
545 and we use that instead of reading a doc string within that defining-form.
547 For defvar, defconst, and fset we skip to the docstring with a kludgy
548 formatting convention: all docstrings must appear on the same line as the
549 initial open-paren (the one in column zero) and must contain a backslash
550 and a newline immediately after the initial double-quote. No newlines
551 must appear between the beginning of the form and the first double-quote.
552 For defun, defmacro, and autoload, we know how to skip over the
553 arglist, but the doc string must still have a backslash and newline
554 immediately after the double quote.
555 The only source files that must follow this convention are preloaded
556 uncompiled ones like loaddefs.el and bindings.el; aside
557 from that, it is always the .elc file that we look at, and they are no
558 problem because byte-compiler output follows this convention.
559 The NAME and DOCSTRING are output.
560 NAME is preceded by `F' for a function or `V' for a variable.
561 An entry is output only if DOCSTRING has \ newline just after the opening "
569 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
575 read_lisp_symbol (infile
, buffer
)
580 char *fillp
= buffer
;
587 *(++fillp
) = getc (infile
);
588 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
599 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
605 scan_lisp_file (filename
, mode
)
606 char *filename
, *mode
;
610 char *saved_string
= 0;
612 infile
= fopen (filename
, mode
);
616 return 0; /* No error */
620 while (!feof (infile
))
625 /* If not at end of line, skip till we get to one. */
626 if (c
!= '\n' && c
!= '\r')
631 /* Skip the line break. */
632 while (c
== '\n' || c
== '\r')
634 /* Detect a dynamic doc string and save it for the next expression. */
643 /* Read the length. */
644 while ((c
= getc (infile
),
645 c
>= '0' && c
<= '9'))
651 /* The next character is a space that is counted in the length
652 but not part of the doc string.
653 We already read it, so just ignore it. */
656 /* Read in the contents. */
657 if (saved_string
!= 0)
659 saved_string
= (char *) malloc (length
);
660 for (i
= 0; i
< length
; i
++)
661 saved_string
[i
] = getc (infile
);
662 /* The last character is a ^_.
663 That is needed in the .elc file
664 but it is redundant in DOC. So get rid of it here. */
665 saved_string
[length
- 1] = 0;
666 /* Skip the line break. */
667 while (c
== '\n' && c
== '\r')
669 /* Skip the following line. */
670 while (c
!= '\n' && c
!= '\r')
679 read_lisp_symbol (infile
, buffer
);
681 if (! strcmp (buffer
, "defun")
682 || ! strcmp (buffer
, "defmacro"))
685 read_lisp_symbol (infile
, buffer
);
687 /* Skip the arguments: either "nil" or a list in parens */
690 if (c
== 'n') /* nil */
692 if ((c
= getc (infile
)) != 'i'
693 || (c
= getc (infile
)) != 'l')
695 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
702 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
711 /* If the next three characters aren't `dquote bslash newline'
712 then we're not reading a docstring.
714 if ((c
= getc (infile
)) != '"'
715 || (c
= getc (infile
)) != '\\'
716 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
719 fprintf (stderr
, "## non-docstring in %s (%s)\n",
726 else if (! strcmp (buffer
, "defvar")
727 || ! strcmp (buffer
, "defconst"))
731 read_lisp_symbol (infile
, buffer
);
733 if (saved_string
== 0)
736 /* Skip until the end of line; remember two previous chars. */
737 while (c
!= '\n' && c
!= '\r' && c
>= 0)
744 /* If two previous characters were " and \,
745 this is a doc string. Otherwise, there is none. */
746 if (c2
!= '"' || c1
!= '\\')
749 fprintf (stderr
, "## non-docstring in %s (%s)\n",
757 else if (! strcmp (buffer
, "custom-declare-variable"))
764 read_lisp_symbol (infile
, buffer
);
770 "## unparsable name in custom-declare-variable in %s\n",
774 read_lisp_symbol (infile
, buffer
);
775 if (strcmp (buffer
, "quote"))
778 "## unparsable name in custom-declare-variable in %s\n",
782 read_lisp_symbol (infile
, buffer
);
787 "## unparsable quoted name in custom-declare-variable in %s\n",
793 if (saved_string
== 0)
795 /* Skip to end of line; remember the two previous chars. */
796 while (c
!= '\n' && c
!= '\r' && c
>= 0)
803 /* If two previous characters were " and \,
804 this is a doc string. Otherwise, there is none. */
805 if (c2
!= '"' || c1
!= '\\')
808 fprintf (stderr
, "## non-docstring in %s (%s)\n",
816 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
823 read_lisp_symbol (infile
, buffer
);
828 fprintf (stderr
, "## unparsable name in fset in %s\n",
832 read_lisp_symbol (infile
, buffer
);
833 if (strcmp (buffer
, "quote"))
835 fprintf (stderr
, "## unparsable name in fset in %s\n",
839 read_lisp_symbol (infile
, buffer
);
844 "## unparsable quoted name in fset in %s\n",
850 if (saved_string
== 0)
852 /* Skip to end of line; remember the two previous chars. */
853 while (c
!= '\n' && c
!= '\r' && c
>= 0)
860 /* If two previous characters were " and \,
861 this is a doc string. Otherwise, there is none. */
862 if (c2
!= '"' || c1
!= '\\')
865 fprintf (stderr
, "## non-docstring in %s (%s)\n",
873 else if (! strcmp (buffer
, "autoload"))
878 read_lisp_symbol (infile
, buffer
);
883 fprintf (stderr
, "## unparsable name in autoload in %s\n",
887 read_lisp_symbol (infile
, buffer
);
888 if (strcmp (buffer
, "quote"))
890 fprintf (stderr
, "## unparsable name in autoload in %s\n",
894 read_lisp_symbol (infile
, buffer
);
899 "## unparsable quoted name in autoload in %s\n",
905 if ((c
= getc (infile
)) != '\"')
907 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
911 read_c_string (infile
, 0);
914 if (saved_string
== 0)
916 /* If the next three characters aren't `dquote bslash newline'
917 then we're not reading a docstring. */
918 if ((c
= getc (infile
)) != '"'
919 || (c
= getc (infile
)) != '\\'
920 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
923 fprintf (stderr
, "## non-docstring in %s (%s)\n",
932 else if (! strcmp (buffer
, "if")
933 || ! strcmp (buffer
, "byte-code"))
940 fprintf (stderr
, "## unrecognised top-level form, %s (%s)\n",
946 /* At this point, we should either use the previous
947 dynamic doc string in saved_string
948 or gobble a doc string from the input file.
950 In the latter case, the opening quote (and leading
951 backslash-newline) have already been read. */
954 putc (type
, outfile
);
955 fprintf (outfile
, "%s\n", buffer
);
958 fputs (saved_string
, outfile
);
959 /* Don't use one dynamic doc string twice. */
964 read_c_string (infile
, 1);