1 /* Generate doc-string file for GNU Emacs from source files.
3 Copyright (C) 1985-1986, 1992-1994, 1997, 1999-2017 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 (at
11 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.
45 #include <binary-io.h>
48 #include <unlocked-io.h>
51 /* Defined to be sys_fopen in ms-w32.h, but only #ifdef emacs, so this
52 is really just insurance. */
55 #endif /* WINDOWSNT */
58 /* Defined to be sys_chdir in ms-w32.h, but only #ifdef emacs, so this
59 is really just insurance.
61 Similarly, msdos defines this as sys_chdir, but we're not linking with the
62 file where that function is defined. */
64 #define IS_SLASH(c) ((c) == '/' || (c) == '\\' || (c) == ':')
65 #else /* not DOS_NT */
66 #define IS_SLASH(c) ((c) == '/')
67 #endif /* not DOS_NT */
69 static void scan_file (char *filename
);
70 static void scan_lisp_file (const char *filename
, const char *mode
);
71 static void scan_c_file (char *filename
, const char *mode
);
72 static void scan_c_stream (FILE *infile
);
73 static void start_globals (void);
74 static void write_globals (void);
78 /* Name this program was invoked with. */
79 static char *progname
;
81 /* True if this invocation is generating globals.h. */
82 static bool generate_globals
;
84 /* Print error message. Args are like vprintf. */
86 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
87 verror (char const *m
, va_list ap
)
89 fprintf (stderr
, "%s: ", progname
);
90 vfprintf (stderr
, m
, ap
);
91 fprintf (stderr
, "\n");
94 /* Print error message. Args are like printf. */
96 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
97 error (char const *m
, ...)
105 /* Print error message and exit. Args are like printf. */
107 static _Noreturn
void ATTRIBUTE_FORMAT_PRINTF (1, 2)
108 fatal (char const *m
, ...)
117 static _Noreturn
void
118 memory_exhausted (void)
120 fatal ("virtual memory exhausted");
123 /* Like malloc but get fatal error if memory is exhausted. */
126 xmalloc (ptrdiff_t size
)
128 void *result
= malloc (size
);
134 /* Like realloc but get fatal error if memory is exhausted. */
137 xrealloc (void *arg
, ptrdiff_t size
)
139 void *result
= realloc (arg
, size
);
147 main (int argc
, char **argv
)
153 /* If first two args are -o FILE, output to FILE. */
155 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
157 if (! freopen (argv
[i
+ 1], "w", stdout
))
159 perror (argv
[i
+ 1]);
164 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
166 if (! freopen (argv
[i
+ 1], "a", stdout
))
168 perror (argv
[i
+ 1]);
173 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
175 if (chdir (argv
[i
+ 1]) != 0)
177 perror (argv
[i
+ 1]);
182 if (argc
> i
&& !strcmp (argv
[i
], "-g"))
184 generate_globals
= true;
188 set_binary_mode (fileno (stdout
), O_BINARY
);
190 if (generate_globals
)
194 scan_c_stream (stdin
);
197 int first_infile
= i
;
198 for (; i
< argc
; i
++)
201 /* Don't process one file twice. */
202 for (j
= first_infile
; j
< i
; j
++)
203 if (strcmp (argv
[i
], argv
[j
]) == 0)
210 if (generate_globals
)
213 if (ferror (stdout
) || fclose (stdout
) != 0)
214 fatal ("write error");
219 /* Add a source file name boundary marker in the output file. */
221 put_filename (char *filename
)
225 for (tmp
= filename
; *tmp
; tmp
++)
227 if (IS_DIRECTORY_SEP (*tmp
))
231 printf ("\037S%s\n", filename
);
234 /* Read file FILENAME and output its doc strings to stdout.
235 Return true if file is found, false otherwise. */
238 scan_file (char *filename
)
240 ptrdiff_t len
= strlen (filename
);
242 if (!generate_globals
)
243 put_filename (filename
);
244 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
245 scan_lisp_file (filename
, "rb");
246 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
247 scan_lisp_file (filename
, "r");
249 scan_c_file (filename
, "r");
255 puts ("/* This file was auto-generated by make-docfile. */");
256 puts ("/* DO NOT EDIT. */");
257 puts ("struct emacs_globals {");
260 static char input_buffer
[128];
262 /* Some state during the execution of `read_c_string_or_comment'. */
265 /* A count of spaces and newlines that have been read, but not output. */
266 intmax_t pending_spaces
, pending_newlines
;
268 /* Where we're reading from. */
271 /* If non-zero, a buffer into which to copy characters. */
273 /* If non-zero, a file into which to copy characters. */
276 /* A keyword we look for at the beginning of lines. If found, it is
277 not copied, and SAW_KEYWORD is set to true. */
279 /* The current point we've reached in an occurrence of KEYWORD in
281 const char *cur_keyword_ptr
;
282 /* Set to true if we saw an occurrence of KEYWORD. */
286 /* Output CH to the file or buffer in STATE. Any pending newlines or
287 spaces are output first. */
290 put_char (char ch
, struct rcsoc_state
*state
)
295 if (state
->pending_newlines
> 0)
297 state
->pending_newlines
--;
300 else if (state
->pending_spaces
> 0)
302 state
->pending_spaces
--;
309 putc (out_ch
, state
->out_file
);
311 *state
->buf_ptr
++ = out_ch
;
313 while (out_ch
!= ch
);
316 /* If in the middle of scanning a keyword, continue scanning with
317 character CH, otherwise output CH to the file or buffer in STATE.
318 Any pending newlines or spaces are output first, as well as any
319 previously scanned characters that were thought to be part of a
320 keyword, but were in fact not. */
323 scan_keyword_or_put_char (char ch
, struct rcsoc_state
*state
)
326 && *state
->cur_keyword_ptr
== ch
327 && (state
->cur_keyword_ptr
> state
->keyword
328 || state
->pending_newlines
> 0))
329 /* We might be looking at STATE->keyword at some point.
330 Keep looking until we know for sure. */
332 if (*++state
->cur_keyword_ptr
== '\0')
333 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
335 state
->saw_keyword
= true;
337 /* Reset the scanning pointer. */
338 state
->cur_keyword_ptr
= state
->keyword
;
340 /* Canonicalize whitespace preceding a usage string. */
341 state
->pending_newlines
= 2;
342 state
->pending_spaces
= 0;
344 /* Skip any whitespace between the keyword and the
348 c
= getc (state
->in_file
);
349 while (c
== ' ' || c
== '\n');
351 /* Output the open-paren we just read. */
353 fatal ("Missing '(' after keyword");
356 /* Skip the function name and replace it with `fn'. */
359 c
= getc (state
->in_file
);
361 fatal ("Unexpected EOF after keyword");
363 while (c
!= ' ' && c
!= ')');
364 put_char ('f', state
);
365 put_char ('n', state
);
367 /* Put back the last character. */
368 ungetc (c
, state
->in_file
);
373 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
374 /* We scanned the beginning of a potential usage
375 keyword, but it was a false alarm. Output the
380 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
381 put_char (*p
, state
);
383 state
->cur_keyword_ptr
= state
->keyword
;
386 put_char (ch
, state
);
391 /* Skip a C string or C-style comment from INFILE, and return the
392 byte that follows, or EOF. COMMENT means skip a comment. If
393 PRINTFLAG is positive, output string contents to stdout. If it is
394 negative, store contents in buf. Convert escape sequences \n and
395 \t to newline and tab; discard \ followed by newline.
396 If SAW_USAGE is non-null, then any occurrences of the string "usage:"
397 at the beginning of a line will be removed, and *SAW_USAGE set to
398 true if any were encountered. */
401 read_c_string_or_comment (FILE *infile
, int printflag
, bool comment
,
405 struct rcsoc_state state
;
407 state
.in_file
= infile
;
408 state
.buf_ptr
= (printflag
< 0 ? input_buffer
: 0);
409 state
.out_file
= (printflag
> 0 ? stdout
: 0);
410 state
.pending_spaces
= 0;
411 state
.pending_newlines
= 0;
412 state
.keyword
= (saw_usage
? "usage:" : 0);
413 state
.cur_keyword_ptr
= state
.keyword
;
414 state
.saw_keyword
= false;
418 while (c
== '\n' || c
== '\r' || c
== '\t' || c
== ' ')
423 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
428 if (c
== '\n' || c
== '\r')
440 state
.pending_spaces
++;
443 state
.pending_newlines
++;
444 state
.pending_spaces
= 0;
447 scan_keyword_or_put_char (c
, &state
);
463 scan_keyword_or_put_char ('*', &state
);
470 /* If we had a "", concatenate the two strings. */
479 *saw_usage
= state
.saw_keyword
;
486 /* Write to stdout the argument names of function FUNC, whose text is in BUF.
487 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
490 write_c_args (char *func
, char *buf
, int minargs
, int maxargs
)
493 bool in_ident
= false;
494 char *ident_start UNINIT
;
495 ptrdiff_t ident_length
= 0;
497 fputs ("(fn", stdout
);
502 for (p
= buf
; *p
; p
++)
506 /* Notice when a new identifier starts. */
507 if ((('A' <= c
&& c
<= 'Z')
508 || ('a' <= c
&& c
<= 'z')
509 || ('0' <= c
&& c
<= '9')
521 ident_length
= p
- ident_start
;
525 /* Found the end of an argument, write out the last seen
527 if (c
== ',' || c
== ')')
529 if (ident_length
== 0)
531 error ("empty arg list for '%s' should be (void), not ()", func
);
535 if (strncmp (ident_start
, "void", ident_length
) == 0)
540 if (minargs
== 0 && maxargs
> 0)
541 fputs ("&optional ", stdout
);
546 /* In C code, `default' is a reserved word, so we spell it
547 `defalt'; demangle that here. */
548 if (ident_length
== 6 && memcmp (ident_start
, "defalt", 6) == 0)
549 fputs ("DEFAULT", stdout
);
551 while (ident_length
-- > 0)
554 if (c
>= 'a' && c
<= 'z')
555 /* Upcase the letter. */
558 /* Print underscore as hyphen. */
568 /* The types of globals. These are sorted roughly in decreasing alignment
569 order to avoid allocation gaps, except that symbols and functions
581 /* A single global. */
584 enum global_type type
;
594 /* Bit values for FLAGS field from the above. Applied for DEFUNs only. */
595 enum { DEFUN_noreturn
= 1, DEFUN_const
= 2 };
597 /* All the variable names we saw while scanning C sources in `-g'
599 static ptrdiff_t num_globals
;
600 static ptrdiff_t num_globals_allocated
;
601 static struct global
*globals
;
603 static struct global
*
604 add_global (enum global_type type
, char const *name
, int value
,
607 /* Ignore the one non-symbol that can occur. */
608 if (strcmp (name
, "..."))
610 if (num_globals
== num_globals_allocated
)
612 ptrdiff_t num_globals_max
= (min (PTRDIFF_MAX
, SIZE_MAX
)
614 if (num_globals_allocated
== num_globals_max
)
616 if (num_globals_allocated
< num_globals_max
/ 2)
617 num_globals_allocated
= 2 * num_globals_allocated
+ 1;
619 num_globals_allocated
= num_globals_max
;
620 globals
= xrealloc (globals
, num_globals_allocated
* sizeof *globals
);
625 ptrdiff_t namesize
= strlen (name
) + 1;
626 char *buf
= xmalloc (namesize
+ (svalue
? strlen (svalue
) + 1 : 0));
627 globals
[num_globals
- 1].type
= type
;
628 globals
[num_globals
- 1].name
= strcpy (buf
, name
);
630 globals
[num_globals
- 1].v
.svalue
= strcpy (buf
+ namesize
, svalue
);
632 globals
[num_globals
- 1].v
.value
= value
;
633 globals
[num_globals
- 1].flags
= 0;
634 return globals
+ num_globals
- 1;
640 compare_globals (const void *a
, const void *b
)
642 const struct global
*ga
= a
;
643 const struct global
*gb
= b
;
645 if (ga
->type
!= gb
->type
)
646 return ga
->type
- gb
->type
;
648 /* Consider "nil" to be the least, so that iQnil is zero. That
649 way, Qnil's internal representation is zero, which is a bit faster. */
650 if (ga
->type
== SYMBOL
)
652 bool a_nil
= strcmp (ga
->name
, "Qnil") == 0;
653 bool b_nil
= strcmp (gb
->name
, "Qnil") == 0;
655 return b_nil
- a_nil
;
658 return strcmp (ga
->name
, gb
->name
);
662 close_emacs_globals (ptrdiff_t num_symbols
)
665 "extern struct emacs_globals globals;\n"
667 "#ifndef DEFINE_SYMBOLS\n"
671 " struct Lisp_Symbol alignas (GCALIGNMENT) s;\n"
672 "} lispsym[%td];\n"),
680 bool seen_defun
= false;
681 ptrdiff_t symnum
= 0;
682 ptrdiff_t num_symbols
= 0;
683 qsort (globals
, num_globals
, sizeof (struct global
), compare_globals
);
686 for (i
= 0; i
< num_globals
; i
++)
688 while (i
+ 1 < num_globals
689 && strcmp (globals
[i
].name
, globals
[i
+ 1].name
) == 0)
691 if (globals
[i
].type
== FUNCTION
692 && globals
[i
].v
.value
!= globals
[i
+ 1].v
.value
)
693 error ("function '%s' defined twice with differing signatures",
695 free (globals
[i
].name
);
698 num_symbols
+= globals
[i
].type
== SYMBOL
;
699 globals
[j
++] = globals
[i
];
703 for (i
= 0; i
< num_globals
; ++i
)
705 char const *type
= 0;
707 switch (globals
[i
].type
)
716 type
= "Lisp_Object";
722 close_emacs_globals (num_symbols
);
728 fatal ("not a recognized DEFVAR_");
733 printf (" %s f_%s;\n", type
, globals
[i
].name
);
734 printf ("#define %s globals.f_%s\n",
735 globals
[i
].name
, globals
[i
].name
);
737 else if (globals
[i
].type
== SYMBOL
)
738 printf (("#define i%s %td\n"
739 "DEFINE_LISP_SYMBOL (%s)\n"),
740 globals
[i
].name
, symnum
++, globals
[i
].name
);
743 if (globals
[i
].flags
& DEFUN_noreturn
)
744 fputs ("_Noreturn ", stdout
);
746 printf ("EXFUN (%s, ", globals
[i
].name
);
747 if (globals
[i
].v
.value
== -1)
748 fputs ("MANY", stdout
);
749 else if (globals
[i
].v
.value
== -2)
750 fputs ("UNEVALLED", stdout
);
752 printf ("%d", globals
[i
].v
.value
);
755 if (globals
[i
].flags
& DEFUN_const
)
756 fputs (" ATTRIBUTE_CONST", stdout
);
763 close_emacs_globals (num_symbols
);
765 puts ("#ifdef DEFINE_SYMBOLS");
766 puts ("static char const *const defsym_name[] = {");
767 for (ptrdiff_t i
= 0; i
< num_globals
; i
++)
768 if (globals
[i
].type
== SYMBOL
)
769 printf ("\t\"%s\",\n", globals
[i
].v
.svalue
);
773 puts ("#define Qnil builtin_lisp_symbol (0)");
774 puts ("#if DEFINE_NON_NIL_Q_SYMBOL_MACROS");
776 for (ptrdiff_t i
= 0; i
< num_globals
; i
++)
777 if (globals
[i
].type
== SYMBOL
&& num_symbols
++ != 0)
778 printf ("# define %s builtin_lisp_symbol (%td)\n",
779 globals
[i
].name
, num_symbols
- 1);
784 /* Read through a c file. If a .o file is named,
785 the corresponding .c or .m file is read instead.
786 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
787 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
790 scan_c_file (char *filename
, const char *mode
)
793 char extension
= filename
[strlen (filename
) - 1];
795 if (extension
== 'o')
796 filename
[strlen (filename
) - 1] = 'c';
798 infile
= fopen (filename
, mode
);
800 if (infile
== NULL
&& extension
== 'o')
803 filename
[strlen (filename
) - 1] = 'm';
804 infile
= fopen (filename
, mode
);
806 filename
[strlen (filename
) - 1] = 'c'; /* Don't confuse people. */
815 /* Reset extension to be able to detect duplicate files. */
816 filename
[strlen (filename
) - 1] = extension
;
817 scan_c_stream (infile
);
820 /* Return 1 if next input from INFILE is equal to P, -1 if EOF,
821 0 if input doesn't match. */
824 stream_match (FILE *infile
, const char *p
)
828 int c
= getc (infile
);
838 scan_c_stream (FILE *infile
)
840 int commas
, minargs
, maxargs
;
843 while (!feof (infile
))
845 bool doc_keyword
= false;
846 bool defunflag
= false;
847 bool defvarperbufferflag
= false;
848 bool defvarflag
= false;
849 enum global_type type
= INVALID
;
850 static char name
[sizeof input_buffer
];
852 if (c
!= '\n' && c
!= '\r')
880 if (c
!= ' ' && c
!= '\t' && c
!= '(')
899 defvarperbufferflag
= (c
== 'P');
900 if (generate_globals
)
903 type
= EMACS_INTEGER
;
911 /* We need to distinguish between DEFVAR_BOOL and
912 DEFVAR_BUFFER_DEFAULTS. */
913 if (generate_globals
&& type
== BOOLEAN
&& c
!= 'O')
928 defunflag
= c
== 'U';
933 && (!defvarflag
|| defvarperbufferflag
|| type
== INVALID
)
934 && !defunflag
&& type
!= SYMBOL
)
946 /* Lisp variable or function name. */
950 c
= read_c_string_or_comment (infile
, -1, false, 0);
953 if (generate_globals
)
956 char const *svalue
= 0;
958 /* Skip "," and whitespace. */
963 while (c
== ',' || c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r');
965 /* Read in the identifier. */
970 input_buffer
[i
++] = c
;
971 if (sizeof input_buffer
<= i
)
972 fatal ("identifier too long");
975 while (! (c
== ',' || c
== ' ' || c
== '\t'
976 || c
== '\n' || c
== '\r'));
977 input_buffer
[i
] = '\0';
978 memcpy (name
, input_buffer
, i
+ 1);
984 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r');
987 c
= read_c_string_or_comment (infile
, -1, false, 0);
988 svalue
= input_buffer
;
993 add_global (type
, name
, 0, svalue
);
1001 /* DEFVAR_LISP ("name", addr, "doc")
1002 DEFVAR_LISP ("name", addr /\* doc *\/)
1003 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
1006 commas
= generate_globals
? 4 : 5;
1007 else if (defvarperbufferflag
)
1009 else if (defvarflag
)
1011 else /* For DEFSIMPLE and DEFPRED. */
1020 if (defunflag
&& (commas
== 1 || commas
== 2))
1025 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
1029 if (commas
== 2) /* Pick up minargs. */
1030 scanned
= fscanf (infile
, "%d", &minargs
);
1031 else /* Pick up maxargs. */
1032 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
1034 if (generate_globals
)
1035 maxargs
= (c
== 'M') ? -1 : -2;
1040 scanned
= fscanf (infile
, "%d", &maxargs
);
1051 if (generate_globals
)
1053 struct global
*g
= add_global (FUNCTION
, name
, maxargs
, 0);
1057 /* The following code tries to recognize function attributes
1058 specified after the docstring, e.g.:
1060 DEFUN ("foo", Ffoo, Sfoo, X, Y, Z,
1062 attributes: attribute1 attribute2 ...)
1063 (Lisp_Object arg...)
1065 Now only 'noreturn' and 'const' attributes are used. */
1067 /* Advance to the end of docstring. */
1071 int d
= getc (infile
);
1076 if (c
== '*' && d
== '/')
1078 c
= d
, d
= getc (infile
);
1082 /* Skip spaces, if any. */
1089 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t');
1090 /* Check for 'attributes:' token. */
1091 if (c
== 'a' && stream_match (infile
, "ttributes:"))
1093 char *p
= input_buffer
;
1094 /* Collect attributes up to ')'. */
1102 if (p
- input_buffer
> sizeof (input_buffer
))
1107 if (strstr (input_buffer
, "noreturn"))
1108 g
->flags
|= DEFUN_noreturn
;
1109 if (strstr (input_buffer
, "const"))
1110 g
->flags
|= DEFUN_const
;
1115 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
1119 c
= read_c_string_or_comment (infile
, 0, false, 0);
1121 while (c
!= EOF
&& c
!= ',' && c
!= '/')
1126 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
1128 while ((c
>= 'a' && c
<= 'z') || (c
>= 'Z' && c
<= 'Z'))
1134 while (c
== ' ' || c
== '\n' || c
== '\r' || c
== '\t')
1141 && (c
= getc (infile
),
1145 bool comment
= c
!= '"';
1148 printf ("\037%c%s\n", defvarflag
? 'V' : 'F', input_buffer
);
1151 getc (infile
); /* Skip past `*'. */
1152 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
1154 /* If this is a defun, find the arguments and print them. If
1155 this function takes MANY or UNEVALLED args, then the C source
1156 won't give the names of the arguments, so we shouldn't bother
1157 trying to find them.
1159 Various doc-string styles:
1160 0: DEFUN (..., "DOC") (args) [!comment]
1161 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
1162 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
1164 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
1166 char argbuf
[1024], *p
= argbuf
;
1168 if (!comment
|| doc_keyword
)
1176 /* Skip into arguments. */
1183 /* Copy arguments into ARGBUF. */
1186 *p
++ = c
= getc (infile
);
1190 fputs ("\n\n", stdout
);
1191 write_c_args (input_buffer
, argbuf
, minargs
, maxargs
);
1193 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
1194 /* The DOC should provide the usage form. */
1195 fprintf (stderr
, "Missing 'usage' for function '%s'.\n",
1200 if (ferror (infile
) || fclose (infile
) != 0)
1201 fatal ("read error");
1204 /* Read a file of Lisp code, compiled or interpreted.
1206 (defun NAME ARGS DOCSTRING ...)
1207 (defmacro NAME ARGS DOCSTRING ...)
1208 (defsubst NAME ARGS DOCSTRING ...)
1209 (autoload (quote NAME) FILE DOCSTRING ...)
1210 (defvar NAME VALUE DOCSTRING)
1211 (defconst NAME VALUE DOCSTRING)
1212 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
1213 (fset (quote NAME) #[... DOCSTRING ...])
1214 (defalias (quote NAME) #[... DOCSTRING ...])
1215 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
1216 starting in column zero.
1217 (quote NAME) may appear as 'NAME as well.
1219 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
1220 When we find that, we save it for the following defining-form,
1221 and we use that instead of reading a doc string within that defining-form.
1223 For defvar, defconst, and fset we skip to the docstring with a kludgy
1224 formatting convention: all docstrings must appear on the same line as the
1225 initial open-paren (the one in column zero) and must contain a backslash
1226 and a newline immediately after the initial double-quote. No newlines
1227 must appear between the beginning of the form and the first double-quote.
1228 For defun, defmacro, and autoload, we know how to skip over the
1229 arglist, but the doc string must still have a backslash and newline
1230 immediately after the double quote.
1231 The only source files that must follow this convention are preloaded
1232 uncompiled ones like loaddefs.el; aside from that, it is always the .elc
1233 file that we should look at, and they are no problem because byte-compiler
1234 output follows this convention.
1235 The NAME and DOCSTRING are output.
1236 NAME is preceded by `F' for a function or `V' for a variable.
1237 An entry is output only if DOCSTRING has \ newline just after the opening ".
1241 skip_white (FILE *infile
)
1244 while (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
1250 read_lisp_symbol (FILE *infile
, char *buffer
)
1253 char *fillp
= buffer
;
1255 skip_white (infile
);
1260 *(++fillp
) = getc (infile
);
1261 else if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '(' || c
== ')')
1272 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
1274 skip_white (infile
);
1278 search_lisp_doc_at_eol (FILE *infile
)
1280 int c
= 0, c1
= 0, c2
= 0;
1282 /* Skip until the end of line; remember two previous chars. */
1283 while (c
!= '\n' && c
!= '\r' && c
!= EOF
)
1290 /* If two previous characters were " and \,
1291 this is a doc string. Otherwise, there is none. */
1292 if (c2
!= '"' || c1
!= '\\')
1295 fprintf (stderr
, "## non-docstring found\n");
1303 #define DEF_ELISP_FILE(fn) { #fn, sizeof(#fn) - 1 }
1306 scan_lisp_file (const char *filename
, const char *mode
)
1310 char *saved_string
= 0;
1311 /* These are the only files that are loaded uncompiled, and must
1312 follow the conventions of the doc strings expected by this
1313 function. These conventions are automatically followed by the
1314 byte compiler when it produces the .elc files. */
1318 } const uncompiled
[] = {
1319 DEF_ELISP_FILE (loaddefs
.el
),
1320 DEF_ELISP_FILE (loadup
.el
),
1321 DEF_ELISP_FILE (charprop
.el
),
1322 DEF_ELISP_FILE (cp51932
.el
),
1323 DEF_ELISP_FILE (eucjp
-ms
.el
)
1326 int flen
= strlen (filename
);
1328 if (generate_globals
)
1329 fatal ("scanning lisp file when -g specified");
1330 if (flen
> 3 && !strcmp (filename
+ flen
- 3, ".el"))
1333 for (i
= 0; i
< sizeof (uncompiled
) / sizeof (uncompiled
[0]); i
++)
1335 if (uncompiled
[i
].fl
<= flen
1336 && !strcmp (filename
+ flen
- uncompiled
[i
].fl
, uncompiled
[i
].fn
)
1337 && (flen
== uncompiled
[i
].fl
1338 || IS_SLASH (filename
[flen
- uncompiled
[i
].fl
- 1])))
1345 fatal ("uncompiled lisp file %s is not supported", filename
);
1348 infile
= fopen (filename
, mode
);
1352 exit (EXIT_FAILURE
);
1356 while (!feof (infile
))
1358 char buffer
[BUFSIZ
];
1361 /* If not at end of line, skip till we get to one. */
1362 if (c
!= '\n' && c
!= '\r')
1367 /* Skip the line break. */
1368 while (c
== '\n' || c
== '\r')
1370 /* Detect a dynamic doc string and save it for the next expression. */
1376 ptrdiff_t length
= 0;
1379 /* Read the length. */
1380 while ((c
= getc (infile
),
1381 c
>= '0' && c
<= '9'))
1383 if (INT_MULTIPLY_WRAPV (length
, 10, &length
)
1384 || INT_ADD_WRAPV (length
, c
- '0', &length
)
1385 || SIZE_MAX
< length
)
1386 memory_exhausted ();
1390 fatal ("invalid dynamic doc string length");
1393 fatal ("space not found after dynamic doc string length");
1395 /* The next character is a space that is counted in the length
1396 but not part of the doc string.
1397 We already read it, so just ignore it. */
1400 /* Read in the contents. */
1401 free (saved_string
);
1402 saved_string
= xmalloc (length
);
1403 for (i
= 0; i
< length
; i
++)
1404 saved_string
[i
] = getc (infile
);
1405 /* The last character is a ^_.
1406 That is needed in the .elc file
1407 but it is redundant in DOC. So get rid of it here. */
1408 saved_string
[length
- 1] = 0;
1409 /* Skip the line break. */
1410 while (c
== '\n' || c
== '\r')
1412 /* Skip the following line. */
1413 while (c
!= '\n' && c
!= '\r')
1422 read_lisp_symbol (infile
, buffer
);
1424 if (! strcmp (buffer
, "defun")
1425 || ! strcmp (buffer
, "defmacro")
1426 || ! strcmp (buffer
, "defsubst"))
1429 read_lisp_symbol (infile
, buffer
);
1431 /* Skip the arguments: either "nil" or a list in parens. */
1434 if (c
== 'n') /* nil */
1436 if ((c
= getc (infile
)) != 'i'
1437 || (c
= getc (infile
)) != 'l')
1439 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
1446 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
1453 skip_white (infile
);
1455 /* If the next three characters aren't `dquote bslash newline'
1456 then we're not reading a docstring.
1458 if ((c
= getc (infile
)) != '"'
1459 || (c
= getc (infile
)) != '\\'
1460 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1463 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1470 /* defcustom can only occur in uncompiled Lisp files. */
1471 else if (! strcmp (buffer
, "defvar")
1472 || ! strcmp (buffer
, "defconst")
1473 || ! strcmp (buffer
, "defcustom"))
1476 read_lisp_symbol (infile
, buffer
);
1478 if (saved_string
== 0)
1479 if (!search_lisp_doc_at_eol (infile
))
1483 else if (! strcmp (buffer
, "custom-declare-variable")
1484 || ! strcmp (buffer
, "defvaralias")
1491 read_lisp_symbol (infile
, buffer
);
1497 "## unparsable name in custom-declare-variable in %s\n",
1501 read_lisp_symbol (infile
, buffer
);
1502 if (strcmp (buffer
, "quote"))
1505 "## unparsable name in custom-declare-variable in %s\n",
1509 read_lisp_symbol (infile
, buffer
);
1514 "## unparsable quoted name in custom-declare-variable in %s\n",
1520 if (saved_string
== 0)
1521 if (!search_lisp_doc_at_eol (infile
))
1525 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1531 read_lisp_symbol (infile
, buffer
);
1536 fprintf (stderr
, "## unparsable name in fset in %s\n",
1540 read_lisp_symbol (infile
, buffer
);
1541 if (strcmp (buffer
, "quote"))
1543 fprintf (stderr
, "## unparsable name in fset in %s\n",
1547 read_lisp_symbol (infile
, buffer
);
1552 "## unparsable quoted name in fset in %s\n",
1558 if (saved_string
== 0)
1559 if (!search_lisp_doc_at_eol (infile
))
1563 else if (! strcmp (buffer
, "autoload"))
1568 read_lisp_symbol (infile
, buffer
);
1573 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1577 read_lisp_symbol (infile
, buffer
);
1578 if (strcmp (buffer
, "quote"))
1580 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1584 read_lisp_symbol (infile
, buffer
);
1589 "## unparsable quoted name in autoload in %s\n",
1594 skip_white (infile
);
1595 if ((c
= getc (infile
)) != '\"')
1597 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1601 read_c_string_or_comment (infile
, 0, false, 0);
1603 if (saved_string
== 0)
1604 if (!search_lisp_doc_at_eol (infile
))
1609 else if (! strcmp (buffer
, "if")
1610 || ! strcmp (buffer
, "byte-code"))
1617 fprintf (stderr
, "## unrecognized top-level form, %s (%s)\n",
1623 /* At this point, we should either use the previous dynamic doc string in
1624 saved_string or gobble a doc string from the input file.
1625 In the latter case, the opening quote (and leading backslash-newline)
1626 have already been read. */
1628 printf ("\037%c%s\n", type
, buffer
);
1631 fputs (saved_string
, stdout
);
1632 /* Don't use one dynamic doc string twice. */
1633 free (saved_string
);
1637 read_c_string_or_comment (infile
, 1, false, 0);
1639 free (saved_string
);
1640 if (ferror (infile
) || fclose (infile
) != 0)
1641 fatal ("%s: read error", filename
);
1645 /* make-docfile.c ends here */