merge trunk
[emacs.git] / lib-src / make-docfile.c
blob411b7057861a8aa5aea983263962159ec9ca6b1b
1 /* Generate doc-string file for GNU Emacs from source files.
3 Copyright (C) 1985-1986, 1992-1994, 1997, 1999-2012
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 /* The arguments given to this program are all the C and Lisp source files
23 of GNU Emacs. .elc and .el and .c files are allowed.
24 A .o file can also be specified; the .c file it was made from is used.
25 This helps the makefile pass the correct list of files.
26 Option -d DIR means change to DIR before looking for files.
28 The results, which go to standard output or to a file
29 specified with -a or -o (-a to append, -o to start from nothing),
30 are entries containing function or variable names and their documentation.
31 Each entry starts with a ^_ character.
32 Then comes F for a function or V for a variable.
33 Then comes the function or variable name, terminated with a newline.
34 Then comes the documentation for that function or variable.
37 #include <config.h>
39 #include <stdio.h>
40 #include <stdlib.h> /* config.h unconditionally includes this anyway */
41 #ifdef MSDOS
42 #include <fcntl.h>
43 #endif /* MSDOS */
44 #ifdef WINDOWSNT
45 /* Defined to be sys_fopen in ms-w32.h, but only #ifdef emacs, so this
46 is really just insurance. */
47 #undef fopen
48 #include <fcntl.h>
49 #include <direct.h>
50 #endif /* WINDOWSNT */
52 #ifdef DOS_NT
53 /* Defined to be sys_chdir in ms-w32.h, but only #ifdef emacs, so this
54 is really just insurance.
56 Similarly, msdos defines this as sys_chdir, but we're not linking with the
57 file where that function is defined. */
58 #undef chdir
59 #define READ_TEXT "rt"
60 #define READ_BINARY "rb"
61 #else /* not DOS_NT */
62 #define READ_TEXT "r"
63 #define READ_BINARY "r"
64 #endif /* not DOS_NT */
66 static int scan_file (char *filename);
67 static int scan_lisp_file (const char *filename, const char *mode);
68 static int scan_c_file (char *filename, const char *mode);
69 static void start_globals (void);
70 static void write_globals (void);
72 #include <unistd.h>
74 /* Stdio stream for output to the DOC file. */
75 FILE *outfile;
77 /* Name this program was invoked with. */
78 char *progname;
80 /* Nonzero if this invocation is generating globals.h. */
81 int generate_globals;
83 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
85 /* VARARGS1 */
86 static void
87 error (const char *s1, const char *s2)
89 fprintf (stderr, "%s: ", progname);
90 fprintf (stderr, s1, s2);
91 fprintf (stderr, "\n");
94 /* Print error message and exit. */
96 /* VARARGS1 */
97 static _Noreturn void
98 fatal (const char *s1, const char *s2)
100 error (s1, s2);
101 exit (EXIT_FAILURE);
104 /* Like malloc but get fatal error if memory is exhausted. */
106 static void *
107 xmalloc (unsigned int size)
109 void *result = (void *) malloc (size);
110 if (result == NULL)
111 fatal ("virtual memory exhausted", 0);
112 return result;
115 /* Like realloc but get fatal error if memory is exhausted. */
117 static void *
118 xrealloc (void *arg, unsigned int size)
120 void *result = (void *) realloc (arg, size);
121 if (result == NULL)
122 fatal ("virtual memory exhausted", 0);
123 return result;
128 main (int argc, char **argv)
130 int i;
131 int err_count = 0;
132 int first_infile;
134 progname = argv[0];
136 outfile = stdout;
138 /* Don't put CRs in the DOC file. */
139 #ifdef MSDOS
140 _fmode = O_BINARY;
141 #if 0 /* Suspicion is that this causes hanging.
142 So instead we require people to use -o on MSDOS. */
143 (stdout)->_flag &= ~_IOTEXT;
144 _setmode (fileno (stdout), O_BINARY);
145 #endif
146 outfile = 0;
147 #endif /* MSDOS */
148 #ifdef WINDOWSNT
149 _fmode = O_BINARY;
150 _setmode (fileno (stdout), O_BINARY);
151 #endif /* WINDOWSNT */
153 /* If first two args are -o FILE, output to FILE. */
154 i = 1;
155 if (argc > i + 1 && !strcmp (argv[i], "-o"))
157 outfile = fopen (argv[i + 1], "w");
158 i += 2;
160 if (argc > i + 1 && !strcmp (argv[i], "-a"))
162 outfile = fopen (argv[i + 1], "a");
163 i += 2;
165 if (argc > i + 1 && !strcmp (argv[i], "-d"))
167 if (chdir (argv[i + 1]) != 0)
169 perror (argv[i + 1]);
170 return EXIT_FAILURE;
172 i += 2;
174 if (argc > i && !strcmp (argv[i], "-g"))
176 generate_globals = 1;
177 ++i;
180 if (outfile == 0)
181 fatal ("No output file specified", "");
183 if (generate_globals)
184 start_globals ();
186 first_infile = i;
187 for (; i < argc; i++)
189 int j;
190 /* Don't process one file twice. */
191 for (j = first_infile; j < i; j++)
192 if (! strcmp (argv[i], argv[j]))
193 break;
194 if (j == i)
195 err_count += scan_file (argv[i]);
198 if (err_count == 0 && generate_globals)
199 write_globals ();
201 return (err_count > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
204 /* Add a source file name boundary marker in the output file. */
205 static void
206 put_filename (char *filename)
208 char *tmp;
210 for (tmp = filename; *tmp; tmp++)
212 if (IS_DIRECTORY_SEP (*tmp))
213 filename = tmp + 1;
216 putc (037, outfile);
217 putc ('S', outfile);
218 fprintf (outfile, "%s\n", filename);
221 /* Read file FILENAME and output its doc strings to outfile. */
222 /* Return 1 if file is not found, 0 if it is found. */
224 static int
225 scan_file (char *filename)
228 size_t len = strlen (filename);
230 if (!generate_globals)
231 put_filename (filename);
232 if (len > 4 && !strcmp (filename + len - 4, ".elc"))
233 return scan_lisp_file (filename, READ_BINARY);
234 else if (len > 3 && !strcmp (filename + len - 3, ".el"))
235 return scan_lisp_file (filename, READ_TEXT);
236 else
237 return scan_c_file (filename, READ_TEXT);
240 static void
241 start_globals (void)
243 fprintf (outfile, "/* This file was auto-generated by make-docfile. */\n");
244 fprintf (outfile, "/* DO NOT EDIT. */\n");
245 fprintf (outfile, "struct emacs_globals {\n");
248 static char input_buffer[128];
250 /* Some state during the execution of `read_c_string_or_comment'. */
251 struct rcsoc_state
253 /* A count of spaces and newlines that have been read, but not output. */
254 unsigned pending_spaces, pending_newlines;
256 /* Where we're reading from. */
257 FILE *in_file;
259 /* If non-zero, a buffer into which to copy characters. */
260 char *buf_ptr;
261 /* If non-zero, a file into which to copy characters. */
262 FILE *out_file;
264 /* A keyword we look for at the beginning of lines. If found, it is
265 not copied, and SAW_KEYWORD is set to true. */
266 const char *keyword;
267 /* The current point we've reached in an occurrence of KEYWORD in
268 the input stream. */
269 const char *cur_keyword_ptr;
270 /* Set to true if we saw an occurrence of KEYWORD. */
271 int saw_keyword;
274 /* Output CH to the file or buffer in STATE. Any pending newlines or
275 spaces are output first. */
277 static inline void
278 put_char (int ch, struct rcsoc_state *state)
280 int out_ch;
283 if (state->pending_newlines > 0)
285 state->pending_newlines--;
286 out_ch = '\n';
288 else if (state->pending_spaces > 0)
290 state->pending_spaces--;
291 out_ch = ' ';
293 else
294 out_ch = ch;
296 if (state->out_file)
297 putc (out_ch, state->out_file);
298 if (state->buf_ptr)
299 *state->buf_ptr++ = out_ch;
301 while (out_ch != ch);
304 /* If in the middle of scanning a keyword, continue scanning with
305 character CH, otherwise output CH to the file or buffer in STATE.
306 Any pending newlines or spaces are output first, as well as any
307 previously scanned characters that were thought to be part of a
308 keyword, but were in fact not. */
310 static void
311 scan_keyword_or_put_char (int ch, struct rcsoc_state *state)
313 if (state->keyword
314 && *state->cur_keyword_ptr == ch
315 && (state->cur_keyword_ptr > state->keyword
316 || state->pending_newlines > 0))
317 /* We might be looking at STATE->keyword at some point.
318 Keep looking until we know for sure. */
320 if (*++state->cur_keyword_ptr == '\0')
321 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
323 state->saw_keyword = 1;
325 /* Reset the scanning pointer. */
326 state->cur_keyword_ptr = state->keyword;
328 /* Canonicalize whitespace preceding a usage string. */
329 state->pending_newlines = 2;
330 state->pending_spaces = 0;
332 /* Skip any whitespace between the keyword and the
333 usage string. */
335 ch = getc (state->in_file);
336 while (ch == ' ' || ch == '\n');
338 /* Output the open-paren we just read. */
339 put_char (ch, state);
341 /* Skip the function name and replace it with `fn'. */
343 ch = getc (state->in_file);
344 while (ch != ' ' && ch != ')');
345 put_char ('f', state);
346 put_char ('n', state);
348 /* Put back the last character. */
349 ungetc (ch, state->in_file);
352 else
354 if (state->keyword && state->cur_keyword_ptr > state->keyword)
355 /* We scanned the beginning of a potential usage
356 keyword, but it was a false alarm. Output the
357 part we scanned. */
359 const char *p;
361 for (p = state->keyword; p < state->cur_keyword_ptr; p++)
362 put_char (*p, state);
364 state->cur_keyword_ptr = state->keyword;
367 put_char (ch, state);
372 /* Skip a C string or C-style comment from INFILE, and return the
373 character that follows. COMMENT non-zero means skip a comment. If
374 PRINTFLAG is positive, output string contents to outfile. If it is
375 negative, store contents in buf. Convert escape sequences \n and
376 \t to newline and tab; discard \ followed by newline.
377 If SAW_USAGE is non-zero, then any occurrences of the string `usage:'
378 at the beginning of a line will be removed, and *SAW_USAGE set to
379 true if any were encountered. */
381 static int
382 read_c_string_or_comment (FILE *infile, int printflag, int comment, int *saw_usage)
384 register int c;
385 struct rcsoc_state state;
387 state.in_file = infile;
388 state.buf_ptr = (printflag < 0 ? input_buffer : 0);
389 state.out_file = (printflag > 0 ? outfile : 0);
390 state.pending_spaces = 0;
391 state.pending_newlines = 0;
392 state.keyword = (saw_usage ? "usage:" : 0);
393 state.cur_keyword_ptr = state.keyword;
394 state.saw_keyword = 0;
396 c = getc (infile);
397 if (comment)
398 while (c == '\n' || c == '\r' || c == '\t' || c == ' ')
399 c = getc (infile);
401 while (c != EOF)
403 while (c != EOF && (comment ? c != '*' : c != '"'))
405 if (c == '\\')
407 c = getc (infile);
408 if (c == '\n' || c == '\r')
410 c = getc (infile);
411 continue;
413 if (c == 'n')
414 c = '\n';
415 if (c == 't')
416 c = '\t';
419 if (c == ' ')
420 state.pending_spaces++;
421 else if (c == '\n')
423 state.pending_newlines++;
424 state.pending_spaces = 0;
426 else
427 scan_keyword_or_put_char (c, &state);
429 c = getc (infile);
432 if (c != EOF)
433 c = getc (infile);
435 if (comment)
437 if (c == '/')
439 c = getc (infile);
440 break;
443 scan_keyword_or_put_char ('*', &state);
445 else
447 if (c != '"')
448 break;
450 /* If we had a "", concatenate the two strings. */
451 c = getc (infile);
455 if (printflag < 0)
456 *state.buf_ptr = 0;
458 if (saw_usage)
459 *saw_usage = state.saw_keyword;
461 return c;
466 /* Write to file OUT the argument names of function FUNC, whose text is in BUF.
467 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
469 static void
470 write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs)
472 register char *p;
473 int in_ident = 0;
474 char *ident_start IF_LINT (= NULL);
475 size_t ident_length = 0;
477 fprintf (out, "(fn");
479 if (*buf == '(')
480 ++buf;
482 for (p = buf; *p; p++)
484 char c = *p;
486 /* Notice when a new identifier starts. */
487 if ((('A' <= c && c <= 'Z')
488 || ('a' <= c && c <= 'z')
489 || ('0' <= c && c <= '9')
490 || c == '_')
491 != in_ident)
493 if (!in_ident)
495 in_ident = 1;
496 ident_start = p;
498 else
500 in_ident = 0;
501 ident_length = p - ident_start;
505 /* Found the end of an argument, write out the last seen
506 identifier. */
507 if (c == ',' || c == ')')
509 if (ident_length == 0)
511 error ("empty arg list for `%s' should be (void), not ()", func);
512 continue;
515 if (strncmp (ident_start, "void", ident_length) == 0)
516 continue;
518 putc (' ', out);
520 if (minargs == 0 && maxargs > 0)
521 fprintf (out, "&optional ");
523 minargs--;
524 maxargs--;
526 /* In C code, `default' is a reserved word, so we spell it
527 `defalt'; demangle that here. */
528 if (ident_length == 6 && memcmp (ident_start, "defalt", 6) == 0)
529 fprintf (out, "DEFAULT");
530 else
531 while (ident_length-- > 0)
533 c = *ident_start++;
534 if (c >= 'a' && c <= 'z')
535 /* Upcase the letter. */
536 c += 'A' - 'a';
537 else if (c == '_')
538 /* Print underscore as hyphen. */
539 c = '-';
540 putc (c, out);
545 putc (')', out);
548 /* The types of globals. These are sorted roughly in decreasing alignment
549 order to avoid allocation gaps, except that functions are last. */
550 enum global_type
552 INVALID,
553 LISP_OBJECT,
554 EMACS_INTEGER,
555 BOOLEAN,
556 FUNCTION,
559 /* A single global. */
560 struct global
562 enum global_type type;
563 char *name;
564 int value;
567 /* All the variable names we saw while scanning C sources in `-g'
568 mode. */
569 int num_globals;
570 int num_globals_allocated;
571 struct global *globals;
573 static void
574 add_global (enum global_type type, char *name, int value)
576 /* Ignore the one non-symbol that can occur. */
577 if (strcmp (name, "..."))
579 ++num_globals;
581 if (num_globals_allocated == 0)
583 num_globals_allocated = 100;
584 globals = xmalloc (num_globals_allocated * sizeof (struct global));
586 else if (num_globals == num_globals_allocated)
588 num_globals_allocated *= 2;
589 globals = xrealloc (globals,
590 num_globals_allocated * sizeof (struct global));
593 globals[num_globals - 1].type = type;
594 globals[num_globals - 1].name = name;
595 globals[num_globals - 1].value = value;
599 static int
600 compare_globals (const void *a, const void *b)
602 const struct global *ga = a;
603 const struct global *gb = b;
605 if (ga->type != gb->type)
606 return ga->type - gb->type;
608 return strcmp (ga->name, gb->name);
611 static void
612 close_emacs_globals (void)
614 fprintf (outfile, "};\n");
615 fprintf (outfile, "extern struct emacs_globals globals;\n");
618 static void
619 write_globals (void)
621 int i, seen_defun = 0;
622 qsort (globals, num_globals, sizeof (struct global), compare_globals);
623 for (i = 0; i < num_globals; ++i)
625 char const *type;
627 switch (globals[i].type)
629 case EMACS_INTEGER:
630 type = "EMACS_INT";
631 break;
632 case BOOLEAN:
633 type = "bool";
634 break;
635 case LISP_OBJECT:
636 type = "Lisp_Object";
637 break;
638 case FUNCTION:
639 if (!seen_defun)
641 close_emacs_globals ();
642 fprintf (outfile, "\n");
643 seen_defun = 1;
645 break;
646 default:
647 fatal ("not a recognized DEFVAR_", 0);
650 if (globals[i].type != FUNCTION)
652 fprintf (outfile, " %s f_%s;\n", type, globals[i].name);
653 fprintf (outfile, "#define %s globals.f_%s\n",
654 globals[i].name, globals[i].name);
656 else
658 /* It would be nice to have a cleaner way to deal with these
659 special hacks. */
660 if (strcmp (globals[i].name, "Fthrow") == 0
661 || strcmp (globals[i].name, "Ftop_level") == 0
662 || strcmp (globals[i].name, "Fkill_emacs") == 0
663 || strcmp (globals[i].name, "Fexit_recursive_edit") == 0
664 || strcmp (globals[i].name, "Fabort_recursive_edit") == 0)
665 fprintf (outfile, "_Noreturn ");
666 fprintf (outfile, "EXFUN (%s, ", globals[i].name);
667 if (globals[i].value == -1)
668 fprintf (outfile, "MANY");
669 else if (globals[i].value == -2)
670 fprintf (outfile, "UNEVALLED");
671 else
672 fprintf (outfile, "%d", globals[i].value);
673 fprintf (outfile, ");\n");
676 while (i + 1 < num_globals
677 && !strcmp (globals[i].name, globals[i + 1].name))
679 if (globals[i].type == FUNCTION
680 && globals[i].value != globals[i + 1].value)
681 error ("function '%s' defined twice with differing signatures",
682 globals[i].name);
683 ++i;
687 if (!seen_defun)
688 close_emacs_globals ();
692 /* Read through a c file. If a .o file is named,
693 the corresponding .c or .m file is read instead.
694 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
695 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
697 static int
698 scan_c_file (char *filename, const char *mode)
700 FILE *infile;
701 register int c;
702 register int commas;
703 int minargs, maxargs;
704 int extension = filename[strlen (filename) - 1];
706 if (extension == 'o')
707 filename[strlen (filename) - 1] = 'c';
709 infile = fopen (filename, mode);
711 if (infile == NULL && extension == 'o')
713 /* Try .m. */
714 filename[strlen (filename) - 1] = 'm';
715 infile = fopen (filename, mode);
716 if (infile == NULL)
717 filename[strlen (filename) - 1] = 'c'; /* Don't confuse people. */
720 /* No error if non-ex input file. */
721 if (infile == NULL)
723 perror (filename);
724 return 0;
727 /* Reset extension to be able to detect duplicate files. */
728 filename[strlen (filename) - 1] = extension;
730 c = '\n';
731 while (!feof (infile))
733 int doc_keyword = 0;
734 int defunflag = 0;
735 int defvarperbufferflag = 0;
736 int defvarflag = 0;
737 enum global_type type = INVALID;
738 char *name IF_LINT (= 0);
740 if (c != '\n' && c != '\r')
742 c = getc (infile);
743 continue;
745 c = getc (infile);
746 if (c == ' ')
748 while (c == ' ')
749 c = getc (infile);
750 if (c != 'D')
751 continue;
752 c = getc (infile);
753 if (c != 'E')
754 continue;
755 c = getc (infile);
756 if (c != 'F')
757 continue;
758 c = getc (infile);
759 if (c != 'V')
760 continue;
761 c = getc (infile);
762 if (c != 'A')
763 continue;
764 c = getc (infile);
765 if (c != 'R')
766 continue;
767 c = getc (infile);
768 if (c != '_')
769 continue;
771 defvarflag = 1;
773 c = getc (infile);
774 defvarperbufferflag = (c == 'P');
775 if (generate_globals)
777 if (c == 'I')
778 type = EMACS_INTEGER;
779 else if (c == 'L')
780 type = LISP_OBJECT;
781 else if (c == 'B')
782 type = BOOLEAN;
785 c = getc (infile);
786 /* We need to distinguish between DEFVAR_BOOL and
787 DEFVAR_BUFFER_DEFAULTS. */
788 if (generate_globals && type == BOOLEAN && c != 'O')
789 type = INVALID;
791 else if (c == 'D')
793 c = getc (infile);
794 if (c != 'E')
795 continue;
796 c = getc (infile);
797 if (c != 'F')
798 continue;
799 c = getc (infile);
800 defunflag = c == 'U';
802 else continue;
804 if (generate_globals
805 && (!defvarflag || defvarperbufferflag || type == INVALID)
806 && !defunflag)
807 continue;
809 while (c != '(')
811 if (c < 0)
812 goto eof;
813 c = getc (infile);
816 /* Lisp variable or function name. */
817 c = getc (infile);
818 if (c != '"')
819 continue;
820 c = read_c_string_or_comment (infile, -1, 0, 0);
822 if (generate_globals)
824 int i = 0;
826 /* Skip "," and whitespace. */
829 c = getc (infile);
831 while (c == ',' || c == ' ' || c == '\t' || c == '\n' || c == '\r');
833 /* Read in the identifier. */
836 input_buffer[i++] = c;
837 c = getc (infile);
839 while (! (c == ',' || c == ' ' || c == '\t'
840 || c == '\n' || c == '\r'));
841 input_buffer[i] = '\0';
843 name = xmalloc (i + 1);
844 memcpy (name, input_buffer, i + 1);
846 if (!defunflag)
848 add_global (type, name, 0);
849 continue;
853 /* DEFVAR_LISP ("name", addr, "doc")
854 DEFVAR_LISP ("name", addr /\* doc *\/)
855 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
857 if (defunflag)
858 commas = generate_globals ? 4 : 5;
859 else if (defvarperbufferflag)
860 commas = 3;
861 else if (defvarflag)
862 commas = 1;
863 else /* For DEFSIMPLE and DEFPRED. */
864 commas = 2;
866 while (commas)
868 if (c == ',')
870 commas--;
872 if (defunflag && (commas == 1 || commas == 2))
874 int scanned = 0;
876 c = getc (infile);
877 while (c == ' ' || c == '\n' || c == '\r' || c == '\t');
878 if (c < 0)
879 goto eof;
880 ungetc (c, infile);
881 if (commas == 2) /* Pick up minargs. */
882 scanned = fscanf (infile, "%d", &minargs);
883 else /* Pick up maxargs. */
884 if (c == 'M' || c == 'U') /* MANY || UNEVALLED */
886 if (generate_globals)
887 maxargs = (c == 'M') ? -1 : -2;
888 else
889 maxargs = -1;
891 else
892 scanned = fscanf (infile, "%d", &maxargs);
893 if (scanned < 0)
894 goto eof;
898 if (c == EOF)
899 goto eof;
900 c = getc (infile);
903 if (generate_globals)
905 add_global (FUNCTION, name, maxargs);
906 continue;
909 while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
910 c = getc (infile);
912 if (c == '"')
913 c = read_c_string_or_comment (infile, 0, 0, 0);
915 while (c != EOF && c != ',' && c != '/')
916 c = getc (infile);
917 if (c == ',')
919 c = getc (infile);
920 while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
921 c = getc (infile);
922 while ((c >= 'a' && c <= 'z') || (c >= 'Z' && c <= 'Z'))
923 c = getc (infile);
924 if (c == ':')
926 doc_keyword = 1;
927 c = getc (infile);
928 while (c == ' ' || c == '\n' || c == '\r' || c == '\t')
929 c = getc (infile);
933 if (c == '"'
934 || (c == '/'
935 && (c = getc (infile),
936 ungetc (c, infile),
937 c == '*')))
939 int comment = c != '"';
940 int saw_usage;
942 putc (037, outfile);
943 putc (defvarflag ? 'V' : 'F', outfile);
944 fprintf (outfile, "%s\n", input_buffer);
946 if (comment)
947 getc (infile); /* Skip past `*'. */
948 c = read_c_string_or_comment (infile, 1, comment, &saw_usage);
950 /* If this is a defun, find the arguments and print them. If
951 this function takes MANY or UNEVALLED args, then the C source
952 won't give the names of the arguments, so we shouldn't bother
953 trying to find them.
955 Various doc-string styles:
956 0: DEFUN (..., "DOC") (args) [!comment]
957 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
958 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
960 if (defunflag && maxargs != -1 && !saw_usage)
962 char argbuf[1024], *p = argbuf;
964 if (!comment || doc_keyword)
965 while (c != ')')
967 if (c < 0)
968 goto eof;
969 c = getc (infile);
972 /* Skip into arguments. */
973 while (c != '(')
975 if (c < 0)
976 goto eof;
977 c = getc (infile);
979 /* Copy arguments into ARGBUF. */
980 *p++ = c;
982 *p++ = c = getc (infile);
983 while (c != ')');
984 *p = '\0';
985 /* Output them. */
986 fprintf (outfile, "\n\n");
987 write_c_args (outfile, input_buffer, argbuf, minargs, maxargs);
989 else if (defunflag && maxargs == -1 && !saw_usage)
990 /* The DOC should provide the usage form. */
991 fprintf (stderr, "Missing `usage' for function `%s'.\n",
992 input_buffer);
995 eof:
996 fclose (infile);
997 return 0;
1000 /* Read a file of Lisp code, compiled or interpreted.
1001 Looks for
1002 (defun NAME ARGS DOCSTRING ...)
1003 (defmacro NAME ARGS DOCSTRING ...)
1004 (defsubst NAME ARGS DOCSTRING ...)
1005 (autoload (quote NAME) FILE DOCSTRING ...)
1006 (defvar NAME VALUE DOCSTRING)
1007 (defconst NAME VALUE DOCSTRING)
1008 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
1009 (fset (quote NAME) #[... DOCSTRING ...])
1010 (defalias (quote NAME) #[... DOCSTRING ...])
1011 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
1012 starting in column zero.
1013 (quote NAME) may appear as 'NAME as well.
1015 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
1016 When we find that, we save it for the following defining-form,
1017 and we use that instead of reading a doc string within that defining-form.
1019 For defvar, defconst, and fset we skip to the docstring with a kludgy
1020 formatting convention: all docstrings must appear on the same line as the
1021 initial open-paren (the one in column zero) and must contain a backslash
1022 and a newline immediately after the initial double-quote. No newlines
1023 must appear between the beginning of the form and the first double-quote.
1024 For defun, defmacro, and autoload, we know how to skip over the
1025 arglist, but the doc string must still have a backslash and newline
1026 immediately after the double quote.
1027 The only source files that must follow this convention are preloaded
1028 uncompiled ones like loaddefs.el and bindings.el; aside
1029 from that, it is always the .elc file that we look at, and they are no
1030 problem because byte-compiler output follows this convention.
1031 The NAME and DOCSTRING are output.
1032 NAME is preceded by `F' for a function or `V' for a variable.
1033 An entry is output only if DOCSTRING has \ newline just after the opening ".
1036 static void
1037 skip_white (FILE *infile)
1039 char c = ' ';
1040 while (c == ' ' || c == '\t' || c == '\n' || c == '\r')
1041 c = getc (infile);
1042 ungetc (c, infile);
1045 static void
1046 read_lisp_symbol (FILE *infile, char *buffer)
1048 char c;
1049 char *fillp = buffer;
1051 skip_white (infile);
1052 while (1)
1054 c = getc (infile);
1055 if (c == '\\')
1056 *(++fillp) = getc (infile);
1057 else if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '(' || c == ')')
1059 ungetc (c, infile);
1060 *fillp = 0;
1061 break;
1063 else
1064 *fillp++ = c;
1067 if (! buffer[0])
1068 fprintf (stderr, "## expected a symbol, got '%c'\n", c);
1070 skip_white (infile);
1073 static int
1074 search_lisp_doc_at_eol (FILE *infile)
1076 char c = 0, c1 = 0, c2 = 0;
1078 /* Skip until the end of line; remember two previous chars. */
1079 while (c != '\n' && c != '\r' && c != EOF)
1081 c2 = c1;
1082 c1 = c;
1083 c = getc (infile);
1086 /* If two previous characters were " and \,
1087 this is a doc string. Otherwise, there is none. */
1088 if (c2 != '"' || c1 != '\\')
1090 #ifdef DEBUG
1091 fprintf (stderr, "## non-docstring in %s (%s)\n",
1092 buffer, filename);
1093 #endif
1094 if (c != EOF)
1095 ungetc (c, infile);
1096 return 0;
1098 return 1;
1101 static int
1102 scan_lisp_file (const char *filename, const char *mode)
1104 FILE *infile;
1105 register int c;
1106 char *saved_string = 0;
1108 if (generate_globals)
1109 fatal ("scanning lisp file when -g specified", 0);
1111 infile = fopen (filename, mode);
1112 if (infile == NULL)
1114 perror (filename);
1115 return 0; /* No error. */
1118 c = '\n';
1119 while (!feof (infile))
1121 char buffer[BUFSIZ];
1122 char type;
1124 /* If not at end of line, skip till we get to one. */
1125 if (c != '\n' && c != '\r')
1127 c = getc (infile);
1128 continue;
1130 /* Skip the line break. */
1131 while (c == '\n' || c == '\r')
1132 c = getc (infile);
1133 /* Detect a dynamic doc string and save it for the next expression. */
1134 if (c == '#')
1136 c = getc (infile);
1137 if (c == '@')
1139 size_t length = 0;
1140 size_t i;
1142 /* Read the length. */
1143 while ((c = getc (infile),
1144 c >= '0' && c <= '9'))
1146 length *= 10;
1147 length += c - '0';
1150 if (length <= 1)
1151 fatal ("invalid dynamic doc string length", "");
1153 if (c != ' ')
1154 fatal ("space not found after dynamic doc string length", "");
1156 /* The next character is a space that is counted in the length
1157 but not part of the doc string.
1158 We already read it, so just ignore it. */
1159 length--;
1161 /* Read in the contents. */
1162 free (saved_string);
1163 saved_string = (char *) xmalloc (length);
1164 for (i = 0; i < length; i++)
1165 saved_string[i] = getc (infile);
1166 /* The last character is a ^_.
1167 That is needed in the .elc file
1168 but it is redundant in DOC. So get rid of it here. */
1169 saved_string[length - 1] = 0;
1170 /* Skip the line break. */
1171 while (c == '\n' || c == '\r')
1172 c = getc (infile);
1173 /* Skip the following line. */
1174 while (c != '\n' && c != '\r')
1175 c = getc (infile);
1177 continue;
1180 if (c != '(')
1181 continue;
1183 read_lisp_symbol (infile, buffer);
1185 if (! strcmp (buffer, "defun")
1186 || ! strcmp (buffer, "defmacro")
1187 || ! strcmp (buffer, "defsubst"))
1189 type = 'F';
1190 read_lisp_symbol (infile, buffer);
1192 /* Skip the arguments: either "nil" or a list in parens. */
1194 c = getc (infile);
1195 if (c == 'n') /* nil */
1197 if ((c = getc (infile)) != 'i'
1198 || (c = getc (infile)) != 'l')
1200 fprintf (stderr, "## unparsable arglist in %s (%s)\n",
1201 buffer, filename);
1202 continue;
1205 else if (c != '(')
1207 fprintf (stderr, "## unparsable arglist in %s (%s)\n",
1208 buffer, filename);
1209 continue;
1211 else
1212 while (c != ')')
1213 c = getc (infile);
1214 skip_white (infile);
1216 /* If the next three characters aren't `dquote bslash newline'
1217 then we're not reading a docstring.
1219 if ((c = getc (infile)) != '"'
1220 || (c = getc (infile)) != '\\'
1221 || ((c = getc (infile)) != '\n' && c != '\r'))
1223 #ifdef DEBUG
1224 fprintf (stderr, "## non-docstring in %s (%s)\n",
1225 buffer, filename);
1226 #endif
1227 continue;
1231 /* defcustom can only occur in uncompiled Lisp files. */
1232 else if (! strcmp (buffer, "defvar")
1233 || ! strcmp (buffer, "defconst")
1234 || ! strcmp (buffer, "defcustom"))
1236 type = 'V';
1237 read_lisp_symbol (infile, buffer);
1239 if (saved_string == 0)
1240 if (!search_lisp_doc_at_eol (infile))
1241 continue;
1244 else if (! strcmp (buffer, "custom-declare-variable")
1245 || ! strcmp (buffer, "defvaralias")
1248 type = 'V';
1250 c = getc (infile);
1251 if (c == '\'')
1252 read_lisp_symbol (infile, buffer);
1253 else
1255 if (c != '(')
1257 fprintf (stderr,
1258 "## unparsable name in custom-declare-variable in %s\n",
1259 filename);
1260 continue;
1262 read_lisp_symbol (infile, buffer);
1263 if (strcmp (buffer, "quote"))
1265 fprintf (stderr,
1266 "## unparsable name in custom-declare-variable in %s\n",
1267 filename);
1268 continue;
1270 read_lisp_symbol (infile, buffer);
1271 c = getc (infile);
1272 if (c != ')')
1274 fprintf (stderr,
1275 "## unparsable quoted name in custom-declare-variable in %s\n",
1276 filename);
1277 continue;
1281 if (saved_string == 0)
1282 if (!search_lisp_doc_at_eol (infile))
1283 continue;
1286 else if (! strcmp (buffer, "fset") || ! strcmp (buffer, "defalias"))
1288 type = 'F';
1290 c = getc (infile);
1291 if (c == '\'')
1292 read_lisp_symbol (infile, buffer);
1293 else
1295 if (c != '(')
1297 fprintf (stderr, "## unparsable name in fset in %s\n",
1298 filename);
1299 continue;
1301 read_lisp_symbol (infile, buffer);
1302 if (strcmp (buffer, "quote"))
1304 fprintf (stderr, "## unparsable name in fset in %s\n",
1305 filename);
1306 continue;
1308 read_lisp_symbol (infile, buffer);
1309 c = getc (infile);
1310 if (c != ')')
1312 fprintf (stderr,
1313 "## unparsable quoted name in fset in %s\n",
1314 filename);
1315 continue;
1319 if (saved_string == 0)
1320 if (!search_lisp_doc_at_eol (infile))
1321 continue;
1324 else if (! strcmp (buffer, "autoload"))
1326 type = 'F';
1327 c = getc (infile);
1328 if (c == '\'')
1329 read_lisp_symbol (infile, buffer);
1330 else
1332 if (c != '(')
1334 fprintf (stderr, "## unparsable name in autoload in %s\n",
1335 filename);
1336 continue;
1338 read_lisp_symbol (infile, buffer);
1339 if (strcmp (buffer, "quote"))
1341 fprintf (stderr, "## unparsable name in autoload in %s\n",
1342 filename);
1343 continue;
1345 read_lisp_symbol (infile, buffer);
1346 c = getc (infile);
1347 if (c != ')')
1349 fprintf (stderr,
1350 "## unparsable quoted name in autoload in %s\n",
1351 filename);
1352 continue;
1355 skip_white (infile);
1356 if ((c = getc (infile)) != '\"')
1358 fprintf (stderr, "## autoload of %s unparsable (%s)\n",
1359 buffer, filename);
1360 continue;
1362 read_c_string_or_comment (infile, 0, 0, 0);
1364 if (saved_string == 0)
1365 if (!search_lisp_doc_at_eol (infile))
1366 continue;
1369 #ifdef DEBUG
1370 else if (! strcmp (buffer, "if")
1371 || ! strcmp (buffer, "byte-code"))
1372 continue;
1373 #endif
1375 else
1377 #ifdef DEBUG
1378 fprintf (stderr, "## unrecognized top-level form, %s (%s)\n",
1379 buffer, filename);
1380 #endif
1381 continue;
1384 /* At this point, we should either use the previous dynamic doc string in
1385 saved_string or gobble a doc string from the input file.
1386 In the latter case, the opening quote (and leading backslash-newline)
1387 have already been read. */
1389 putc (037, outfile);
1390 putc (type, outfile);
1391 fprintf (outfile, "%s\n", buffer);
1392 if (saved_string)
1394 fputs (saved_string, outfile);
1395 /* Don't use one dynamic doc string twice. */
1396 free (saved_string);
1397 saved_string = 0;
1399 else
1400 read_c_string_or_comment (infile, 1, 0, 0);
1402 fclose (infile);
1403 return 0;
1407 /* make-docfile.c ends here */