(type-break-mode): remove :version field from defcustom declaration.
[emacs.git] / lib-src / make-docfile.c
blob82e7f7c4efd62160ee4176108757bb4ff509ff85
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)
9 any later version.
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>
38 #include <stdio.h>
39 #ifdef MSDOS
40 #include <fcntl.h>
41 #endif /* MSDOS */
42 #ifdef WINDOWSNT
43 #include <stdlib.h>
44 #include <fcntl.h>
45 #include <direct.h>
46 #endif /* WINDOWSNT */
48 #ifdef DOS_NT
49 #define READ_TEXT "rt"
50 #define READ_BINARY "rb"
51 #else /* not DOS_NT */
52 #define READ_TEXT "r"
53 #define READ_BINARY "r"
54 #endif /* not DOS_NT */
56 int scan_file ();
57 int scan_lisp_file ();
58 int scan_c_file ();
60 #ifdef MSDOS
61 /* s/msdos.h defines this as sys_chdir, but we're not linking with the
62 file where that function is defined. */
63 #undef chdir
64 #endif
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
70 /* Stdio stream for output to the DOC file. */
71 FILE *outfile;
73 /* Name this program was invoked with. */
74 char *progname;
76 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
78 /* VARARGS1 */
79 void
80 error (s1, s2)
81 char *s1, *s2;
83 fprintf (stderr, "%s: ", progname);
84 fprintf (stderr, s1, s2);
85 fprintf (stderr, "\n");
88 /* Print error message and exit. */
90 /* VARARGS1 */
91 void
92 fatal (s1, s2)
93 char *s1, *s2;
95 error (s1, s2);
96 exit (1);
99 /* Like malloc but get fatal error if memory is exhausted. */
101 long *
102 xmalloc (size)
103 unsigned int size;
105 long *result = (long *) malloc (size);
106 if (result == NULL)
107 fatal ("virtual memory exhausted", 0);
108 return result;
112 main (argc, argv)
113 int argc;
114 char **argv;
116 int i;
117 int err_count = 0;
118 int first_infile;
120 progname = argv[0];
122 outfile = stdout;
124 /* Don't put CRs in the DOC file. */
125 #ifdef MSDOS
126 _fmode = O_BINARY;
127 #if 0 /* Suspicion is that this causes hanging.
128 So instead we require people to use -o on MSDOS. */
129 (stdout)->_flag &= ~_IOTEXT;
130 _setmode (fileno (stdout), O_BINARY);
131 #endif
132 outfile = 0;
133 #endif /* MSDOS */
134 #ifdef WINDOWSNT
135 _fmode = O_BINARY;
136 _setmode (fileno (stdout), O_BINARY);
137 #endif /* WINDOWSNT */
139 /* If first two args are -o FILE, output to FILE. */
140 i = 1;
141 if (argc > i + 1 && !strcmp (argv[i], "-o"))
143 outfile = fopen (argv[i + 1], "w");
144 i += 2;
146 if (argc > i + 1 && !strcmp (argv[i], "-a"))
148 outfile = fopen (argv[i + 1], "a");
149 i += 2;
151 if (argc > i + 1 && !strcmp (argv[i], "-d"))
153 chdir (argv[i + 1]);
154 i += 2;
157 if (outfile == 0)
158 fatal ("No output file specified", "");
160 first_infile = i;
161 for (; i < argc; i++)
163 int j;
164 /* Don't process one file twice. */
165 for (j = first_infile; j < i; j++)
166 if (! strcmp (argv[i], argv[j]))
167 break;
168 if (j == i)
169 err_count += scan_file (argv[i]);
171 #ifndef VMS
172 exit (err_count > 0);
173 #endif /* VMS */
174 return err_count > 0;
177 /* Read file FILENAME and output its doc strings to outfile. */
178 /* Return 1 if file is not found, 0 if it is found. */
181 scan_file (filename)
182 char *filename;
184 int len = strlen (filename);
185 if (len > 4 && !strcmp (filename + len - 4, ".elc"))
186 return scan_lisp_file (filename, READ_BINARY);
187 else if (len > 3 && !strcmp (filename + len - 3, ".el"))
188 return scan_lisp_file (filename, READ_TEXT);
189 else
190 return scan_c_file (filename, READ_TEXT);
193 char buf[128];
195 /* Skip a C string from INFILE,
196 and return the character that follows the closing ".
197 If printflag is positive, output string contents to outfile.
198 If it is negative, store contents in buf.
199 Convert escape sequences \n and \t to newline and tab;
200 discard \ followed by newline. */
203 read_c_string (infile, printflag)
204 FILE *infile;
205 int printflag;
207 register int c;
208 char *p = buf;
210 c = getc (infile);
211 while (c != EOF)
213 while (c != '"' && c != EOF)
215 if (c == '\\')
217 c = getc (infile);
218 if (c == '\n')
220 c = getc (infile);
221 continue;
223 if (c == 'n')
224 c = '\n';
225 if (c == 't')
226 c = '\t';
228 if (printflag > 0)
229 putc (c, outfile);
230 else if (printflag < 0)
231 *p++ = c;
232 c = getc (infile);
234 c = getc (infile);
235 if (c != '"')
236 break;
237 /* If we had a "", concatenate the two strings. */
238 c = getc (infile);
241 if (printflag < 0)
242 *p = 0;
244 return c;
247 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
248 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
250 void
251 write_c_args (out, func, buf, minargs, maxargs)
252 FILE *out;
253 char *func, *buf;
254 int minargs, maxargs;
256 register char *p;
257 int in_ident = 0;
258 int just_spaced = 0;
259 int need_space = 1;
261 fprintf (out, "(%s", func);
263 if (*buf == '(')
264 ++buf;
266 for (p = buf; *p; p++)
268 char c = *p;
269 int ident_start = 0;
271 /* Notice when we start printing a new identifier. */
272 if ((('A' <= c && c <= 'Z')
273 || ('a' <= c && c <= 'z')
274 || ('0' <= c && c <= '9')
275 || c == '_')
276 != in_ident)
278 if (!in_ident)
280 in_ident = 1;
281 ident_start = 1;
283 if (need_space)
284 putc (' ', out);
286 if (minargs == 0 && maxargs > 0)
287 fprintf (out, "&optional ");
288 just_spaced = 1;
290 minargs--;
291 maxargs--;
293 else
294 in_ident = 0;
297 /* Print the C argument list as it would appear in lisp:
298 print underscores as hyphens, and print commas as spaces.
299 Collapse adjacent spaces into one. */
300 if (c == '_') c = '-';
301 if (c == ',') c = ' ';
303 /* In C code, `default' is a reserved word, so we spell it
304 `defalt'; unmangle that here. */
305 if (ident_start
306 && strncmp (p, "defalt", 6) == 0
307 && ! (('A' <= p[6] && p[6] <= 'Z')
308 || ('a' <= p[6] && p[6] <= 'z')
309 || ('0' <= p[6] && p[6] <= '9')
310 || p[6] == '_'))
312 fprintf (out, "DEFAULT");
313 p += 5;
314 in_ident = 0;
315 just_spaced = 0;
317 else if (c != ' ' || ! just_spaced)
319 if (c >= 'a' && c <= 'z')
320 /* Upcase the letter. */
321 c += 'A' - 'a';
322 putc (c, out);
325 just_spaced = (c == ' ');
326 need_space = 0;
330 /* Read through a c file. If a .o file is named,
331 the corresponding .c file is read instead.
332 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
333 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
336 scan_c_file (filename, mode)
337 char *filename, *mode;
339 FILE *infile;
340 register int c;
341 register int commas;
342 register int defunflag;
343 register int defvarperbufferflag;
344 register int defvarflag;
345 int minargs, maxargs;
346 int extension = filename[strlen (filename) - 1];
348 if (extension == 'o')
349 filename[strlen (filename) - 1] = 'c';
351 infile = fopen (filename, mode);
353 /* No error if non-ex input file */
354 if (infile == NULL)
356 perror (filename);
357 return 0;
360 /* Reset extension to be able to detect duplicate files. */
361 filename[strlen (filename) - 1] = extension;
363 c = '\n';
364 while (!feof (infile))
366 if (c != '\n')
368 c = getc (infile);
369 continue;
371 c = getc (infile);
372 if (c == ' ')
374 while (c == ' ')
375 c = getc (infile);
376 if (c != 'D')
377 continue;
378 c = getc (infile);
379 if (c != 'E')
380 continue;
381 c = getc (infile);
382 if (c != 'F')
383 continue;
384 c = getc (infile);
385 if (c != 'V')
386 continue;
387 c = getc (infile);
388 if (c != 'A')
389 continue;
390 c = getc (infile);
391 if (c != 'R')
392 continue;
393 c = getc (infile);
394 if (c != '_')
395 continue;
397 defvarflag = 1;
398 defunflag = 0;
400 c = getc (infile);
401 defvarperbufferflag = (c == 'P');
403 c = getc (infile);
405 else if (c == 'D')
407 c = getc (infile);
408 if (c != 'E')
409 continue;
410 c = getc (infile);
411 if (c != 'F')
412 continue;
413 c = getc (infile);
414 defunflag = c == 'U';
415 defvarflag = 0;
417 else continue;
419 while (c != '(')
421 if (c < 0)
422 goto eof;
423 c = getc (infile);
426 c = getc (infile);
427 if (c != '"')
428 continue;
429 c = read_c_string (infile, -1);
431 if (defunflag)
432 commas = 5;
433 else if (defvarperbufferflag)
434 commas = 2;
435 else if (defvarflag)
436 commas = 1;
437 else /* For DEFSIMPLE and DEFPRED */
438 commas = 2;
440 while (commas)
442 if (c == ',')
444 commas--;
445 if (defunflag && (commas == 1 || commas == 2))
448 c = getc (infile);
449 while (c == ' ' || c == '\n' || c == '\t');
450 if (c < 0)
451 goto eof;
452 ungetc (c, infile);
453 if (commas == 2) /* pick up minargs */
454 fscanf (infile, "%d", &minargs);
455 else /* pick up maxargs */
456 if (c == 'M' || c == 'U') /* MANY || UNEVALLED */
457 maxargs = -1;
458 else
459 fscanf (infile, "%d", &maxargs);
462 if (c < 0)
463 goto eof;
464 c = getc (infile);
466 while (c == ' ' || c == '\n' || c == '\t')
467 c = getc (infile);
468 if (c == '"')
469 c = read_c_string (infile, 0);
470 while (c != ',')
471 c = getc (infile);
472 c = getc (infile);
473 while (c == ' ' || c == '\n' || c == '\t')
474 c = getc (infile);
476 if (c == '"')
478 putc (037, outfile);
479 putc (defvarflag ? 'V' : 'F', outfile);
480 fprintf (outfile, "%s\n", buf);
481 c = read_c_string (infile, 1);
483 /* If this is a defun, find the arguments and print them. If
484 this function takes MANY or UNEVALLED args, then the C source
485 won't give the names of the arguments, so we shouldn't bother
486 trying to find them. */
487 if (defunflag && maxargs != -1)
489 char argbuf[1024], *p = argbuf;
490 while (c != ')')
492 if (c < 0)
493 goto eof;
494 c = getc (infile);
496 /* Skip into arguments. */
497 while (c != '(')
499 if (c < 0)
500 goto eof;
501 c = getc (infile);
503 /* Copy arguments into ARGBUF. */
504 *p++ = c;
506 *p++ = c = getc (infile);
507 while (c != ')');
508 *p = '\0';
509 /* Output them. */
510 fprintf (outfile, "\n\n");
511 write_c_args (outfile, buf, argbuf, minargs, maxargs);
515 eof:
516 fclose (infile);
517 return 0;
520 /* Read a file of Lisp code, compiled or interpreted.
521 Looks for
522 (defun NAME ARGS DOCSTRING ...)
523 (defmacro NAME ARGS DOCSTRING ...)
524 (autoload (quote NAME) FILE DOCSTRING ...)
525 (defvar NAME VALUE DOCSTRING)
526 (defconst NAME VALUE DOCSTRING)
527 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
528 (fset (quote NAME) #[... DOCSTRING ...])
529 (defalias (quote NAME) #[... DOCSTRING ...])
530 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
531 starting in column zero.
532 (quote NAME) may appear as 'NAME as well.
534 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
535 When we find that, we save it for the following defining-form,
536 and we use that instead of reading a doc string within that defining-form.
538 For defun, defmacro, and autoload, we know how to skip over the arglist.
539 For defvar, defconst, and fset we skip to the docstring with a kludgy
540 formatting convention: all docstrings must appear on the same line as the
541 initial open-paren (the one in column zero) and must contain a backslash
542 and a double-quote immediately after the initial double-quote. No newlines
543 must appear between the beginning of the form and the first double-quote.
544 The only source file that must follow this convention is loaddefs.el; aside
545 from that, it is always the .elc file that we look at, and they are no
546 problem because byte-compiler output follows this convention.
547 The NAME and DOCSTRING are output.
548 NAME is preceded by `F' for a function or `V' for a variable.
549 An entry is output only if DOCSTRING has \ newline just after the opening "
552 void
553 skip_white (infile)
554 FILE *infile;
556 char c = ' ';
557 while (c == ' ' || c == '\t' || c == '\n')
558 c = getc (infile);
559 ungetc (c, infile);
562 void
563 read_lisp_symbol (infile, buffer)
564 FILE *infile;
565 char *buffer;
567 char c;
568 char *fillp = buffer;
570 skip_white (infile);
571 while (1)
573 c = getc (infile);
574 if (c == '\\')
575 *(++fillp) = getc (infile);
576 else if (c == ' ' || c == '\t' || c == '\n' || c == '(' || c == ')')
578 ungetc (c, infile);
579 *fillp = 0;
580 break;
582 else
583 *fillp++ = c;
586 if (! buffer[0])
587 fprintf (stderr, "## expected a symbol, got '%c'\n", c);
589 skip_white (infile);
593 scan_lisp_file (filename, mode)
594 char *filename, *mode;
596 FILE *infile;
597 register int c;
598 char *saved_string = 0;
600 infile = fopen (filename, mode);
601 if (infile == NULL)
603 perror (filename);
604 return 0; /* No error */
607 c = '\n';
608 while (!feof (infile))
610 char buffer[BUFSIZ];
611 char type;
613 if (c != '\n')
615 c = getc (infile);
616 continue;
618 c = getc (infile);
619 /* Detect a dynamic doc string and save it for the next expression. */
620 if (c == '#')
622 c = getc (infile);
623 if (c == '@')
625 int length = 0;
626 int i;
628 /* Read the length. */
629 while ((c = getc (infile),
630 c >= '0' && c <= '9'))
632 length *= 10;
633 length += c - '0';
636 /* The next character is a space that is counted in the length
637 but not part of the doc string.
638 We already read it, so just ignore it. */
639 length--;
641 /* Read in the contents. */
642 if (saved_string != 0)
643 free (saved_string);
644 saved_string = (char *) malloc (length);
645 for (i = 0; i < length; i++)
646 saved_string[i] = getc (infile);
647 /* The last character is a ^_.
648 That is needed in the .elc file
649 but it is redundant in DOC. So get rid of it here. */
650 saved_string[length - 1] = 0;
651 /* Skip the newline. */
652 c = getc (infile);
653 while (c != '\n')
654 c = getc (infile);
656 continue;
659 if (c != '(')
660 continue;
662 read_lisp_symbol (infile, buffer);
664 if (! strcmp (buffer, "defun") ||
665 ! strcmp (buffer, "defmacro"))
667 type = 'F';
668 read_lisp_symbol (infile, buffer);
670 /* Skip the arguments: either "nil" or a list in parens */
672 c = getc (infile);
673 if (c == 'n') /* nil */
675 if ((c = getc (infile)) != 'i' ||
676 (c = getc (infile)) != 'l')
678 fprintf (stderr, "## unparsable arglist in %s (%s)\n",
679 buffer, filename);
680 continue;
683 else if (c != '(')
685 fprintf (stderr, "## unparsable arglist in %s (%s)\n",
686 buffer, filename);
687 continue;
689 else
690 while (c != ')')
691 c = getc (infile);
692 skip_white (infile);
694 /* If the next three characters aren't `dquote bslash newline'
695 then we're not reading a docstring.
697 if ((c = getc (infile)) != '"' ||
698 (c = getc (infile)) != '\\' ||
699 (c = getc (infile)) != '\n')
701 #ifdef DEBUG
702 fprintf (stderr, "## non-docstring in %s (%s)\n",
703 buffer, filename);
704 #endif
705 continue;
709 else if (! strcmp (buffer, "defvar") ||
710 ! strcmp (buffer, "defconst"))
712 char c1 = 0, c2 = 0;
713 type = 'V';
714 read_lisp_symbol (infile, buffer);
716 if (saved_string == 0)
719 /* Skip until the first newline; remember the two previous chars. */
720 while (c != '\n' && c >= 0)
722 c2 = c1;
723 c1 = c;
724 c = getc (infile);
727 /* If two previous characters were " and \,
728 this is a doc string. Otherwise, there is none. */
729 if (c2 != '"' || c1 != '\\')
731 #ifdef DEBUG
732 fprintf (stderr, "## non-docstring in %s (%s)\n",
733 buffer, filename);
734 #endif
735 continue;
740 else if (! strcmp (buffer, "custom-declare-variable"))
742 char c1 = 0, c2 = 0;
743 type = 'V';
745 c = getc (infile);
746 if (c == '\'')
747 read_lisp_symbol (infile, buffer);
748 else
750 if (c != '(')
752 fprintf (stderr,
753 "## unparsable name in custom-declare-variable in %s\n",
754 filename);
755 continue;
757 read_lisp_symbol (infile, buffer);
758 if (strcmp (buffer, "quote"))
760 fprintf (stderr,
761 "## unparsable name in custom-declare-variable in %s\n",
762 filename);
763 continue;
765 read_lisp_symbol (infile, buffer);
766 c = getc (infile);
767 if (c != ')')
769 fprintf (stderr,
770 "## unparsable quoted name in custom-declare-variable in %s\n",
771 filename);
772 continue;
776 if (saved_string == 0)
778 /* Skip until the first newline; remember the two previous
779 chars. */
780 while (c != '\n' && c >= 0)
782 c2 = c1;
783 c1 = c;
784 c = getc (infile);
787 /* If two previous characters were " and \,
788 this is a doc string. Otherwise, there is none. */
789 if (c2 != '"' || c1 != '\\')
791 #ifdef DEBUG
792 fprintf (stderr, "## non-docstring in %s (%s)\n",
793 buffer, filename);
794 #endif
795 continue;
800 else if (! strcmp (buffer, "fset") || ! strcmp (buffer, "defalias"))
802 char c1 = 0, c2 = 0;
803 type = 'F';
805 c = getc (infile);
806 if (c == '\'')
807 read_lisp_symbol (infile, buffer);
808 else
810 if (c != '(')
812 fprintf (stderr, "## unparsable name in fset in %s\n",
813 filename);
814 continue;
816 read_lisp_symbol (infile, buffer);
817 if (strcmp (buffer, "quote"))
819 fprintf (stderr, "## unparsable name in fset in %s\n",
820 filename);
821 continue;
823 read_lisp_symbol (infile, buffer);
824 c = getc (infile);
825 if (c != ')')
827 fprintf (stderr,
828 "## unparsable quoted name in fset in %s\n",
829 filename);
830 continue;
834 if (saved_string == 0)
836 /* Skip until the first newline; remember the two previous chars. */
837 while (c != '\n' && c >= 0)
839 c2 = c1;
840 c1 = c;
841 c = getc (infile);
844 /* If two previous characters were " and \,
845 this is a doc string. Otherwise, there is none. */
846 if (c2 != '"' || c1 != '\\')
848 #ifdef DEBUG
849 fprintf (stderr, "## non-docstring in %s (%s)\n",
850 buffer, filename);
851 #endif
852 continue;
857 else if (! strcmp (buffer, "autoload"))
859 type = 'F';
860 c = getc (infile);
861 if (c == '\'')
862 read_lisp_symbol (infile, buffer);
863 else
865 if (c != '(')
867 fprintf (stderr, "## unparsable name in autoload in %s\n",
868 filename);
869 continue;
871 read_lisp_symbol (infile, buffer);
872 if (strcmp (buffer, "quote"))
874 fprintf (stderr, "## unparsable name in autoload in %s\n",
875 filename);
876 continue;
878 read_lisp_symbol (infile, buffer);
879 c = getc (infile);
880 if (c != ')')
882 fprintf (stderr,
883 "## unparsable quoted name in autoload in %s\n",
884 filename);
885 continue;
888 skip_white (infile);
889 if ((c = getc (infile)) != '\"')
891 fprintf (stderr, "## autoload of %s unparsable (%s)\n",
892 buffer, filename);
893 continue;
895 read_c_string (infile, 0);
896 skip_white (infile);
898 if (saved_string == 0)
900 /* If the next three characters aren't `dquote bslash newline'
901 then we're not reading a docstring. */
902 if ((c = getc (infile)) != '"' ||
903 (c = getc (infile)) != '\\' ||
904 (c = getc (infile)) != '\n')
906 #ifdef DEBUG
907 fprintf (stderr, "## non-docstring in %s (%s)\n",
908 buffer, filename);
909 #endif
910 continue;
915 #ifdef DEBUG
916 else if (! strcmp (buffer, "if") ||
917 ! strcmp (buffer, "byte-code"))
919 #endif
921 else
923 #ifdef DEBUG
924 fprintf (stderr, "## unrecognised top-level form, %s (%s)\n",
925 buffer, filename);
926 #endif
927 continue;
930 /* At this point, we should either use the previous
931 dynamic doc string in saved_string
932 or gobble a doc string from the input file.
934 In the latter case, the opening quote (and leading
935 backslash-newline) have already been read. */
937 putc (037, outfile);
938 putc (type, outfile);
939 fprintf (outfile, "%s\n", buffer);
940 if (saved_string)
942 fputs (saved_string, outfile);
943 /* Don't use one dynamic doc string twice. */
944 free (saved_string);
945 saved_string = 0;
947 else
948 read_c_string (infile, 1);
950 fclose (infile);
951 return 0;