1 /* Generate doc-string file for GNU Emacs from source files.
3 Copyright (C) 1985-1986, 1992-1994, 1997, 1999-2015 Free Software
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.
41 #include <stdlib.h> /* config.h unconditionally includes this anyway */
44 /* Defined to be sys_fopen in ms-w32.h, but only #ifdef emacs, so this
45 is really just insurance. */
48 #endif /* WINDOWSNT */
50 #include <binary-io.h>
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. */
59 #define IS_SLASH(c) ((c) == '/' || (c) == '\\' || (c) == ':')
60 #else /* not DOS_NT */
61 #define IS_SLASH(c) ((c) == '/')
62 #endif /* not DOS_NT */
64 static int scan_file (char *filename
);
65 static int scan_lisp_file (const char *filename
, const char *mode
);
66 static int scan_c_file (char *filename
, const char *mode
);
67 static int scan_c_stream (FILE *infile
);
68 static void start_globals (void);
69 static void write_globals (void);
73 /* Name this program was invoked with. */
76 /* Nonzero if this invocation is generating globals.h. */
79 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
83 error (const char *s1
, const char *s2
)
85 fprintf (stderr
, "%s: ", progname
);
86 fprintf (stderr
, s1
, s2
);
87 fprintf (stderr
, "\n");
90 /* Print error message and exit. */
94 fatal (const char *s1
, const char *s2
)
100 /* Like malloc but get fatal error if memory is exhausted. */
103 xmalloc (unsigned int size
)
105 void *result
= (void *) malloc (size
);
107 fatal ("virtual memory exhausted", 0);
111 /* Like strdup, but get fatal error if memory is exhausted. */
116 char *result
= strdup (s
);
118 fatal ("virtual memory exhausted", 0);
122 /* Like realloc but get fatal error if memory is exhausted. */
125 xrealloc (void *arg
, unsigned int size
)
127 void *result
= (void *) realloc (arg
, size
);
129 fatal ("virtual memory exhausted", 0);
135 main (int argc
, char **argv
)
142 /* If first two args are -o FILE, output to FILE. */
144 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
146 if (! freopen (argv
[i
+ 1], "w", stdout
))
148 perror (argv
[i
+ 1]);
153 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
155 if (! freopen (argv
[i
+ 1], "a", stdout
))
157 perror (argv
[i
+ 1]);
162 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
164 if (chdir (argv
[i
+ 1]) != 0)
166 perror (argv
[i
+ 1]);
171 if (argc
> i
&& !strcmp (argv
[i
], "-g"))
173 generate_globals
= 1;
177 set_binary_mode (fileno (stdout
), O_BINARY
);
179 if (generate_globals
)
183 scan_c_stream (stdin
);
186 int first_infile
= i
;
187 for (; i
< argc
; i
++)
190 /* Don't process one file twice. */
191 for (j
= first_infile
; j
< i
; j
++)
192 if (strcmp (argv
[i
], argv
[j
]) == 0)
195 err_count
+= scan_file (argv
[i
]);
199 if (err_count
== 0 && generate_globals
)
202 return (err_count
> 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
205 /* Add a source file name boundary marker in the output file. */
207 put_filename (char *filename
)
211 for (tmp
= filename
; *tmp
; tmp
++)
213 if (IS_DIRECTORY_SEP (*tmp
))
217 printf ("\037S%s\n", filename
);
220 /* Read file FILENAME and output its doc strings to stdout.
221 Return 1 if file is not found, 0 if it is found. */
224 scan_file (char *filename
)
227 size_t len
= strlen (filename
);
229 if (!generate_globals
)
230 put_filename (filename
);
231 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
232 return scan_lisp_file (filename
, "rb");
233 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
234 return scan_lisp_file (filename
, "r");
236 return scan_c_file (filename
, "r");
242 puts ("/* This file was auto-generated by make-docfile. */");
243 puts ("/* DO NOT EDIT. */");
244 puts ("struct emacs_globals {");
247 static char input_buffer
[128];
249 /* Some state during the execution of `read_c_string_or_comment'. */
252 /* A count of spaces and newlines that have been read, but not output. */
253 unsigned pending_spaces
, pending_newlines
;
255 /* Where we're reading from. */
258 /* If non-zero, a buffer into which to copy characters. */
260 /* If non-zero, a file into which to copy characters. */
263 /* A keyword we look for at the beginning of lines. If found, it is
264 not copied, and SAW_KEYWORD is set to true. */
266 /* The current point we've reached in an occurrence of KEYWORD in
268 const char *cur_keyword_ptr
;
269 /* Set to true if we saw an occurrence of KEYWORD. */
273 /* Output CH to the file or buffer in STATE. Any pending newlines or
274 spaces are output first. */
277 put_char (int ch
, struct rcsoc_state
*state
)
282 if (state
->pending_newlines
> 0)
284 state
->pending_newlines
--;
287 else if (state
->pending_spaces
> 0)
289 state
->pending_spaces
--;
296 putc (out_ch
, state
->out_file
);
298 *state
->buf_ptr
++ = out_ch
;
300 while (out_ch
!= ch
);
303 /* If in the middle of scanning a keyword, continue scanning with
304 character CH, otherwise output CH to the file or buffer in STATE.
305 Any pending newlines or spaces are output first, as well as any
306 previously scanned characters that were thought to be part of a
307 keyword, but were in fact not. */
310 scan_keyword_or_put_char (int ch
, struct rcsoc_state
*state
)
313 && *state
->cur_keyword_ptr
== ch
314 && (state
->cur_keyword_ptr
> state
->keyword
315 || state
->pending_newlines
> 0))
316 /* We might be looking at STATE->keyword at some point.
317 Keep looking until we know for sure. */
319 if (*++state
->cur_keyword_ptr
== '\0')
320 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
322 state
->saw_keyword
= 1;
324 /* Reset the scanning pointer. */
325 state
->cur_keyword_ptr
= state
->keyword
;
327 /* Canonicalize whitespace preceding a usage string. */
328 state
->pending_newlines
= 2;
329 state
->pending_spaces
= 0;
331 /* Skip any whitespace between the keyword and the
334 ch
= getc (state
->in_file
);
335 while (ch
== ' ' || ch
== '\n');
337 /* Output the open-paren we just read. */
338 put_char (ch
, state
);
340 /* Skip the function name and replace it with `fn'. */
342 ch
= getc (state
->in_file
);
343 while (ch
!= ' ' && ch
!= ')');
344 put_char ('f', state
);
345 put_char ('n', state
);
347 /* Put back the last character. */
348 ungetc (ch
, state
->in_file
);
353 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
354 /* We scanned the beginning of a potential usage
355 keyword, but it was a false alarm. Output the
360 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
361 put_char (*p
, state
);
363 state
->cur_keyword_ptr
= state
->keyword
;
366 put_char (ch
, state
);
371 /* Skip a C string or C-style comment from INFILE, and return the
372 character that follows. COMMENT non-zero means skip a comment. If
373 PRINTFLAG is positive, output string contents to stdout. If it is
374 negative, store contents in buf. Convert escape sequences \n and
375 \t to newline and tab; discard \ followed by newline.
376 If SAW_USAGE is non-zero, then any occurrences of the string `usage:'
377 at the beginning of a line will be removed, and *SAW_USAGE set to
378 true if any were encountered. */
381 read_c_string_or_comment (FILE *infile
, int printflag
, int comment
, int *saw_usage
)
384 struct rcsoc_state state
;
386 state
.in_file
= infile
;
387 state
.buf_ptr
= (printflag
< 0 ? input_buffer
: 0);
388 state
.out_file
= (printflag
> 0 ? stdout
: 0);
389 state
.pending_spaces
= 0;
390 state
.pending_newlines
= 0;
391 state
.keyword
= (saw_usage
? "usage:" : 0);
392 state
.cur_keyword_ptr
= state
.keyword
;
393 state
.saw_keyword
= 0;
397 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
402 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
407 if (c
== '\n' || c
== '\r')
419 state
.pending_spaces
++;
422 state
.pending_newlines
++;
423 state
.pending_spaces
= 0;
426 scan_keyword_or_put_char (c
, &state
);
442 scan_keyword_or_put_char ('*', &state
);
449 /* If we had a "", concatenate the two strings. */
458 *saw_usage
= state
.saw_keyword
;
465 /* Write to stdout the argument names of function FUNC, whose text is in BUF.
466 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
469 write_c_args (char *func
, char *buf
, int minargs
, int maxargs
)
473 char *ident_start
IF_LINT (= NULL
);
474 size_t ident_length
= 0;
476 fputs ("(fn", stdout
);
481 for (p
= buf
; *p
; p
++)
485 /* Notice when a new identifier starts. */
486 if ((('A' <= c
&& c
<= 'Z')
487 || ('a' <= c
&& c
<= 'z')
488 || ('0' <= c
&& c
<= '9')
500 ident_length
= p
- ident_start
;
504 /* Found the end of an argument, write out the last seen
506 if (c
== ',' || c
== ')')
508 if (ident_length
== 0)
510 error ("empty arg list for `%s' should be (void), not ()", func
);
514 if (strncmp (ident_start
, "void", ident_length
) == 0)
519 if (minargs
== 0 && maxargs
> 0)
520 fputs ("&optional ", stdout
);
525 /* In C code, `default' is a reserved word, so we spell it
526 `defalt'; demangle that here. */
527 if (ident_length
== 6 && memcmp (ident_start
, "defalt", 6) == 0)
528 fputs ("DEFAULT", stdout
);
530 while (ident_length
-- > 0)
533 if (c
>= 'a' && c
<= 'z')
534 /* Upcase the letter. */
537 /* Print underscore as hyphen. */
547 /* The types of globals. These are sorted roughly in decreasing alignment
548 order to avoid allocation gaps, except that symbols and functions
560 /* A single global. */
563 enum global_type type
;
573 /* Bit values for FLAGS field from the above. Applied for DEFUNs only. */
574 enum { DEFUN_noreturn
= 1, DEFUN_const
= 2 };
576 /* All the variable names we saw while scanning C sources in `-g'
579 int num_globals_allocated
;
580 struct global
*globals
;
582 static struct global
*
583 add_global (enum global_type type
, char *name
, int value
, char const *svalue
)
585 /* Ignore the one non-symbol that can occur. */
586 if (strcmp (name
, "..."))
590 if (num_globals_allocated
== 0)
592 num_globals_allocated
= 100;
593 globals
= xmalloc (num_globals_allocated
* sizeof (struct global
));
595 else if (num_globals
== num_globals_allocated
)
597 num_globals_allocated
*= 2;
598 globals
= xrealloc (globals
,
599 num_globals_allocated
* sizeof (struct global
));
602 globals
[num_globals
- 1].type
= type
;
603 globals
[num_globals
- 1].name
= name
;
605 globals
[num_globals
- 1].v
.svalue
= svalue
;
607 globals
[num_globals
- 1].v
.value
= value
;
608 globals
[num_globals
- 1].flags
= 0;
609 return globals
+ num_globals
- 1;
615 compare_globals (const void *a
, const void *b
)
617 const struct global
*ga
= a
;
618 const struct global
*gb
= b
;
620 if (ga
->type
!= gb
->type
)
621 return ga
->type
- gb
->type
;
623 /* Consider "nil" to be the least, so that iQnil is zero. That
624 way, Qnil's internal representation is zero, which is a bit faster. */
625 if (ga
->type
== SYMBOL
)
627 bool a_nil
= strcmp (ga
->name
, "Qnil") == 0;
628 bool b_nil
= strcmp (gb
->name
, "Qnil") == 0;
630 return b_nil
- a_nil
;
633 return strcmp (ga
->name
, gb
->name
);
637 close_emacs_globals (int num_symbols
)
640 "extern struct emacs_globals globals;\n"
642 "#ifndef DEFINE_SYMBOLS\n"
645 "struct Lisp_Symbol alignas (GCALIGNMENT) lispsym[%d];\n"),
653 bool seen_defun
= false;
656 qsort (globals
, num_globals
, sizeof (struct global
), compare_globals
);
659 for (i
= 0; i
< num_globals
; i
++)
661 while (i
+ 1 < num_globals
662 && strcmp (globals
[i
].name
, globals
[i
+ 1].name
) == 0)
664 if (globals
[i
].type
== FUNCTION
665 && globals
[i
].v
.value
!= globals
[i
+ 1].v
.value
)
666 error ("function '%s' defined twice with differing signatures",
670 num_symbols
+= globals
[i
].type
== SYMBOL
;
671 globals
[j
++] = globals
[i
];
675 for (i
= 0; i
< num_globals
; ++i
)
677 char const *type
= 0;
679 switch (globals
[i
].type
)
688 type
= "Lisp_Object";
694 close_emacs_globals (num_symbols
);
700 fatal ("not a recognized DEFVAR_", 0);
705 printf (" %s f_%s;\n", type
, globals
[i
].name
);
706 printf ("#define %s globals.f_%s\n",
707 globals
[i
].name
, globals
[i
].name
);
709 else if (globals
[i
].type
== SYMBOL
)
710 printf (("DEFINE_LISP_SYMBOL_BEGIN (%s)\n"
712 "#define %s builtin_lisp_symbol (i%s)\n"
713 "DEFINE_LISP_SYMBOL_END (%s)\n\n"),
714 globals
[i
].name
, globals
[i
].name
, symnum
++,
715 globals
[i
].name
, globals
[i
].name
, globals
[i
].name
);
718 if (globals
[i
].flags
& DEFUN_noreturn
)
719 fputs ("_Noreturn ", stdout
);
721 printf ("EXFUN (%s, ", globals
[i
].name
);
722 if (globals
[i
].v
.value
== -1)
723 fputs ("MANY", stdout
);
724 else if (globals
[i
].v
.value
== -2)
725 fputs ("UNEVALLED", stdout
);
727 printf ("%d", globals
[i
].v
.value
);
730 if (globals
[i
].flags
& DEFUN_const
)
731 fputs (" ATTRIBUTE_CONST", stdout
);
738 close_emacs_globals (num_symbols
);
740 puts ("#ifdef DEFINE_SYMBOLS");
741 puts ("static char const *const defsym_name[] = {");
742 for (int i
= 0; i
< num_globals
; i
++)
744 if (globals
[i
].type
== SYMBOL
)
745 printf ("\t\"%s\",\n", globals
[i
].v
.svalue
);
746 while (i
+ 1 < num_globals
747 && strcmp (globals
[i
].name
, globals
[i
+ 1].name
) == 0)
755 /* Read through a c file. If a .o file is named,
756 the corresponding .c or .m file is read instead.
757 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
758 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
761 scan_c_file (char *filename
, const char *mode
)
764 int extension
= filename
[strlen (filename
) - 1];
766 if (extension
== 'o')
767 filename
[strlen (filename
) - 1] = 'c';
769 infile
= fopen (filename
, mode
);
771 if (infile
== NULL
&& extension
== 'o')
774 filename
[strlen (filename
) - 1] = 'm';
775 infile
= fopen (filename
, mode
);
777 filename
[strlen (filename
) - 1] = 'c'; /* Don't confuse people. */
780 /* No error if non-ex input file. */
787 /* Reset extension to be able to detect duplicate files. */
788 filename
[strlen (filename
) - 1] = extension
;
789 return scan_c_stream (infile
);
792 /* Return 1 if next input from INFILE is equal to P, -1 if EOF,
793 0 if input doesn't match. */
796 stream_match (FILE *infile
, const char *p
)
800 int c
= getc (infile
);
810 scan_c_stream (FILE *infile
)
812 int commas
, minargs
, maxargs
;
815 while (!feof (infile
))
819 int defvarperbufferflag
= 0;
821 enum global_type type
= INVALID
;
822 char *name
IF_LINT (= 0);
824 if (c
!= '\n' && c
!= '\r')
852 if (c
!= ' ' && c
!= '\t' && c
!= '(')
871 defvarperbufferflag
= (c
== 'P');
872 if (generate_globals
)
875 type
= EMACS_INTEGER
;
883 /* We need to distinguish between DEFVAR_BOOL and
884 DEFVAR_BUFFER_DEFAULTS. */
885 if (generate_globals
&& type
== BOOLEAN
&& c
!= 'O')
900 defunflag
= c
== 'U';
905 && (!defvarflag
|| defvarperbufferflag
|| type
== INVALID
)
906 && !defunflag
&& type
!= SYMBOL
)
918 /* Lisp variable or function name. */
922 c
= read_c_string_or_comment (infile
, -1, 0, 0);
925 if (generate_globals
)
928 char const *svalue
= 0;
930 /* Skip "," and whitespace. */
935 while (c
== ',' || c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r');
937 /* Read in the identifier. */
942 input_buffer
[i
++] = c
;
945 while (! (c
== ',' || c
== ' ' || c
== '\t'
946 || c
== '\n' || c
== '\r'));
947 input_buffer
[i
] = '\0';
949 name
= xmalloc (i
+ 1);
950 memcpy (name
, input_buffer
, i
+ 1);
956 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r');
959 c
= read_c_string_or_comment (infile
, -1, 0, 0);
960 svalue
= xstrdup (input_buffer
);
965 add_global (type
, name
, 0, svalue
);
973 /* DEFVAR_LISP ("name", addr, "doc")
974 DEFVAR_LISP ("name", addr /\* doc *\/)
975 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
978 commas
= generate_globals
? 4 : 5;
979 else if (defvarperbufferflag
)
983 else /* For DEFSIMPLE and DEFPRED. */
992 if (defunflag
&& (commas
== 1 || commas
== 2))
997 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
1001 if (commas
== 2) /* Pick up minargs. */
1002 scanned
= fscanf (infile
, "%d", &minargs
);
1003 else /* Pick up maxargs. */
1004 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
1006 if (generate_globals
)
1007 maxargs
= (c
== 'M') ? -1 : -2;
1012 scanned
= fscanf (infile
, "%d", &maxargs
);
1023 if (generate_globals
)
1025 struct global
*g
= add_global (FUNCTION
, name
, maxargs
, 0);
1027 /* The following code tries to recognize function attributes
1028 specified after the docstring, e.g.:
1030 DEFUN ("foo", Ffoo, Sfoo, X, Y, Z,
1032 attributes: attribute1 attribute2 ...)
1033 (Lisp_Object arg...)
1035 Now only 'noreturn' and 'const' attributes are used. */
1037 /* Advance to the end of docstring. */
1041 int d
= getc (infile
);
1046 if (c
== '*' && d
== '/')
1048 c
= d
, d
= getc (infile
);
1052 /* Skip spaces, if any. */
1059 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
1060 /* Check for 'attributes:' token. */
1061 if (c
== 'a' && stream_match (infile
, "ttributes:"))
1063 char *p
= input_buffer
;
1064 /* Collect attributes up to ')'. */
1072 if (p
- input_buffer
> sizeof (input_buffer
))
1077 if (strstr (input_buffer
, "noreturn"))
1078 g
->flags
|= DEFUN_noreturn
;
1079 if (strstr (input_buffer
, "const"))
1080 g
->flags
|= DEFUN_const
;
1085 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
1089 c
= read_c_string_or_comment (infile
, 0, 0, 0);
1091 while (c
!= EOF
&& c
!= ',' && c
!= '/')
1096 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
1098 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
1104 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
1111 && (c
= getc (infile
),
1115 int comment
= c
!= '"';
1118 printf ("\037%c%s\n", defvarflag
? 'V' : 'F', input_buffer
);
1121 getc (infile
); /* Skip past `*'. */
1122 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
1124 /* If this is a defun, find the arguments and print them. If
1125 this function takes MANY or UNEVALLED args, then the C source
1126 won't give the names of the arguments, so we shouldn't bother
1127 trying to find them.
1129 Various doc-string styles:
1130 0: DEFUN (..., "DOC") (args) [!comment]
1131 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
1132 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
1134 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
1136 char argbuf
[1024], *p
= argbuf
;
1138 if (!comment
|| doc_keyword
)
1146 /* Skip into arguments. */
1153 /* Copy arguments into ARGBUF. */
1156 *p
++ = c
= getc (infile
);
1160 fputs ("\n\n", stdout
);
1161 write_c_args (input_buffer
, argbuf
, minargs
, maxargs
);
1163 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
1164 /* The DOC should provide the usage form. */
1165 fprintf (stderr
, "Missing `usage' for function `%s'.\n",
1174 /* Read a file of Lisp code, compiled or interpreted.
1176 (defun NAME ARGS DOCSTRING ...)
1177 (defmacro NAME ARGS DOCSTRING ...)
1178 (defsubst NAME ARGS DOCSTRING ...)
1179 (autoload (quote NAME) FILE DOCSTRING ...)
1180 (defvar NAME VALUE DOCSTRING)
1181 (defconst NAME VALUE DOCSTRING)
1182 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
1183 (fset (quote NAME) #[... DOCSTRING ...])
1184 (defalias (quote NAME) #[... DOCSTRING ...])
1185 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
1186 starting in column zero.
1187 (quote NAME) may appear as 'NAME as well.
1189 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
1190 When we find that, we save it for the following defining-form,
1191 and we use that instead of reading a doc string within that defining-form.
1193 For defvar, defconst, and fset we skip to the docstring with a kludgy
1194 formatting convention: all docstrings must appear on the same line as the
1195 initial open-paren (the one in column zero) and must contain a backslash
1196 and a newline immediately after the initial double-quote. No newlines
1197 must appear between the beginning of the form and the first double-quote.
1198 For defun, defmacro, and autoload, we know how to skip over the
1199 arglist, but the doc string must still have a backslash and newline
1200 immediately after the double quote.
1201 The only source files that must follow this convention are preloaded
1202 uncompiled ones like loaddefs.el; aside from that, it is always the .elc
1203 file that we should look at, and they are no problem because byte-compiler
1204 output follows this convention.
1205 The NAME and DOCSTRING are output.
1206 NAME is preceded by `F' for a function or `V' for a variable.
1207 An entry is output only if DOCSTRING has \ newline just after the opening ".
1211 skip_white (FILE *infile
)
1214 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
1220 read_lisp_symbol (FILE *infile
, char *buffer
)
1223 char *fillp
= buffer
;
1225 skip_white (infile
);
1230 *(++fillp
) = getc (infile
);
1231 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
1242 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
1244 skip_white (infile
);
1248 search_lisp_doc_at_eol (FILE *infile
)
1250 int c
= 0, c1
= 0, c2
= 0;
1252 /* Skip until the end of line; remember two previous chars. */
1253 while (c
!= '\n' && c
!= '\r' && c
!= EOF
)
1260 /* If two previous characters were " and \,
1261 this is a doc string. Otherwise, there is none. */
1262 if (c2
!= '"' || c1
!= '\\')
1265 fprintf (stderr
, "## non-docstring found\n");
1274 #define DEF_ELISP_FILE(fn) { #fn, sizeof(#fn) - 1 }
1277 scan_lisp_file (const char *filename
, const char *mode
)
1281 char *saved_string
= 0;
1282 /* These are the only files that are loaded uncompiled, and must
1283 follow the conventions of the doc strings expected by this
1284 function. These conventions are automatically followed by the
1285 byte compiler when it produces the .elc files. */
1289 } const uncompiled
[] = {
1290 DEF_ELISP_FILE (loaddefs
.el
),
1291 DEF_ELISP_FILE (loadup
.el
),
1292 DEF_ELISP_FILE (charprop
.el
),
1293 DEF_ELISP_FILE (cp51932
.el
),
1294 DEF_ELISP_FILE (eucjp
-ms
.el
)
1297 size_t flen
= strlen (filename
);
1299 if (generate_globals
)
1300 fatal ("scanning lisp file when -g specified", 0);
1301 if (flen
> 3 && !strcmp (filename
+ flen
- 3, ".el"))
1303 for (i
= 0, match
= 0; i
< sizeof (uncompiled
) / sizeof (uncompiled
[0]);
1306 if (uncompiled
[i
].fl
<= flen
1307 && !strcmp (filename
+ flen
- uncompiled
[i
].fl
, uncompiled
[i
].fn
)
1308 && (flen
== uncompiled
[i
].fl
1309 || IS_SLASH (filename
[flen
- uncompiled
[i
].fl
- 1])))
1316 fatal ("uncompiled lisp file %s is not supported", filename
);
1319 infile
= fopen (filename
, mode
);
1323 return 0; /* No error. */
1327 while (!feof (infile
))
1329 char buffer
[BUFSIZ
];
1332 /* If not at end of line, skip till we get to one. */
1333 if (c
!= '\n' && c
!= '\r')
1338 /* Skip the line break. */
1339 while (c
== '\n' || c
== '\r')
1341 /* Detect a dynamic doc string and save it for the next expression. */
1350 /* Read the length. */
1351 while ((c
= getc (infile
),
1352 c
>= '0' && c
<= '9'))
1359 fatal ("invalid dynamic doc string length", "");
1362 fatal ("space not found after dynamic doc string length", "");
1364 /* The next character is a space that is counted in the length
1365 but not part of the doc string.
1366 We already read it, so just ignore it. */
1369 /* Read in the contents. */
1370 free (saved_string
);
1371 saved_string
= (char *) xmalloc (length
);
1372 for (i
= 0; i
< length
; i
++)
1373 saved_string
[i
] = getc (infile
);
1374 /* The last character is a ^_.
1375 That is needed in the .elc file
1376 but it is redundant in DOC. So get rid of it here. */
1377 saved_string
[length
- 1] = 0;
1378 /* Skip the line break. */
1379 while (c
== '\n' || c
== '\r')
1381 /* Skip the following line. */
1382 while (c
!= '\n' && c
!= '\r')
1391 read_lisp_symbol (infile
, buffer
);
1393 if (! strcmp (buffer
, "defun")
1394 || ! strcmp (buffer
, "defmacro")
1395 || ! strcmp (buffer
, "defsubst"))
1398 read_lisp_symbol (infile
, buffer
);
1400 /* Skip the arguments: either "nil" or a list in parens. */
1403 if (c
== 'n') /* nil */
1405 if ((c
= getc (infile
)) != 'i'
1406 || (c
= getc (infile
)) != 'l')
1408 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
1415 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
1422 skip_white (infile
);
1424 /* If the next three characters aren't `dquote bslash newline'
1425 then we're not reading a docstring.
1427 if ((c
= getc (infile
)) != '"'
1428 || (c
= getc (infile
)) != '\\'
1429 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1432 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1439 /* defcustom can only occur in uncompiled Lisp files. */
1440 else if (! strcmp (buffer
, "defvar")
1441 || ! strcmp (buffer
, "defconst")
1442 || ! strcmp (buffer
, "defcustom"))
1445 read_lisp_symbol (infile
, buffer
);
1447 if (saved_string
== 0)
1448 if (!search_lisp_doc_at_eol (infile
))
1452 else if (! strcmp (buffer
, "custom-declare-variable")
1453 || ! strcmp (buffer
, "defvaralias")
1460 read_lisp_symbol (infile
, buffer
);
1466 "## unparsable name in custom-declare-variable in %s\n",
1470 read_lisp_symbol (infile
, buffer
);
1471 if (strcmp (buffer
, "quote"))
1474 "## unparsable name in custom-declare-variable in %s\n",
1478 read_lisp_symbol (infile
, buffer
);
1483 "## unparsable quoted name in custom-declare-variable in %s\n",
1489 if (saved_string
== 0)
1490 if (!search_lisp_doc_at_eol (infile
))
1494 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1500 read_lisp_symbol (infile
, buffer
);
1505 fprintf (stderr
, "## unparsable name in fset in %s\n",
1509 read_lisp_symbol (infile
, buffer
);
1510 if (strcmp (buffer
, "quote"))
1512 fprintf (stderr
, "## unparsable name in fset in %s\n",
1516 read_lisp_symbol (infile
, buffer
);
1521 "## unparsable quoted name in fset in %s\n",
1527 if (saved_string
== 0)
1528 if (!search_lisp_doc_at_eol (infile
))
1532 else if (! strcmp (buffer
, "autoload"))
1537 read_lisp_symbol (infile
, buffer
);
1542 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1546 read_lisp_symbol (infile
, buffer
);
1547 if (strcmp (buffer
, "quote"))
1549 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1553 read_lisp_symbol (infile
, buffer
);
1558 "## unparsable quoted name in autoload in %s\n",
1563 skip_white (infile
);
1564 if ((c
= getc (infile
)) != '\"')
1566 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1570 read_c_string_or_comment (infile
, 0, 0, 0);
1572 if (saved_string
== 0)
1573 if (!search_lisp_doc_at_eol (infile
))
1578 else if (! strcmp (buffer
, "if")
1579 || ! strcmp (buffer
, "byte-code"))
1586 fprintf (stderr
, "## unrecognized top-level form, %s (%s)\n",
1592 /* At this point, we should either use the previous dynamic doc string in
1593 saved_string or gobble a doc string from the input file.
1594 In the latter case, the opening quote (and leading backslash-newline)
1595 have already been read. */
1597 printf ("\037%c%s\n", type
, buffer
);
1600 fputs (saved_string
, stdout
);
1601 /* Don't use one dynamic doc string twice. */
1602 free (saved_string
);
1606 read_c_string_or_comment (infile
, 1, 0, 0);
1613 /* make-docfile.c ends here */