1 /* Generate doc-string file for GNU Emacs from source files.
3 Copyright (C) 1985-1986, 1992-1994, 1997, 1999-2018 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 <https://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>
49 #include <unlocked-io.h>
52 /* Defined to be sys_fopen in ms-w32.h, but only #ifdef emacs, so this
53 is really just insurance. */
56 #endif /* WINDOWSNT */
59 /* Defined to be sys_chdir in ms-w32.h, but only #ifdef emacs, so this
60 is really just insurance.
62 Similarly, msdos defines this as sys_chdir, but we're not linking with the
63 file where that function is defined. */
65 #define IS_SLASH(c) ((c) == '/' || (c) == '\\' || (c) == ':')
66 #else /* not DOS_NT */
67 #define IS_SLASH(c) ((c) == '/')
68 #endif /* not DOS_NT */
70 static void scan_file (char *filename
);
71 static void scan_lisp_file (const char *filename
, const char *mode
);
72 static void scan_c_file (char *filename
, const char *mode
);
73 static void scan_c_stream (FILE *infile
);
74 static void start_globals (void);
75 static void write_globals (void);
79 /* Name this program was invoked with. */
80 static char *progname
;
82 /* True if this invocation is generating globals.h. */
83 static bool generate_globals
;
85 /* Print error message. Args are like vprintf. */
87 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
88 verror (char const *m
, va_list ap
)
90 fprintf (stderr
, "%s: ", progname
);
91 vfprintf (stderr
, m
, ap
);
92 fprintf (stderr
, "\n");
95 /* Print error message. Args are like printf. */
97 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
98 error (char const *m
, ...)
106 /* Print error message and exit. Args are like printf. */
108 static _Noreturn
void ATTRIBUTE_FORMAT_PRINTF (1, 2)
109 fatal (char const *m
, ...)
118 static _Noreturn
void
119 memory_exhausted (void)
121 fatal ("virtual memory exhausted");
124 /* Like malloc but get fatal error if memory is exhausted. */
126 static void * ATTRIBUTE_MALLOC
127 xmalloc (ptrdiff_t size
)
129 void *result
= malloc (size
);
135 /* Like realloc but get fatal error if memory is exhausted. */
138 xrealloc (void *arg
, ptrdiff_t size
)
140 void *result
= realloc (arg
, size
);
148 main (int argc
, char **argv
)
154 /* If first two args are -o FILE, output to FILE. */
156 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-o"))
158 if (! freopen (argv
[i
+ 1], "w", stdout
))
160 perror (argv
[i
+ 1]);
165 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-a"))
167 if (! freopen (argv
[i
+ 1], "a", stdout
))
169 perror (argv
[i
+ 1]);
174 if (argc
> i
+ 1 && !strcmp (argv
[i
], "-d"))
176 if (chdir (argv
[i
+ 1]) != 0)
178 perror (argv
[i
+ 1]);
183 if (argc
> i
&& !strcmp (argv
[i
], "-g"))
185 generate_globals
= true;
189 set_binary_mode (fileno (stdout
), O_BINARY
);
191 if (generate_globals
)
195 scan_c_stream (stdin
);
198 int first_infile
= i
;
199 for (; i
< argc
; i
++)
202 /* Don't process one file twice. */
203 for (j
= first_infile
; j
< i
; j
++)
204 if (strcmp (argv
[i
], argv
[j
]) == 0)
211 if (generate_globals
)
214 if (ferror (stdout
) || fclose (stdout
) != 0)
215 fatal ("write error");
220 /* Add a source file name boundary marker in the output file. */
222 put_filename (char *filename
)
226 for (tmp
= filename
; *tmp
; tmp
++)
228 if (IS_DIRECTORY_SEP (*tmp
))
232 printf ("\037S%s\n", filename
);
235 /* Read file FILENAME and output its doc strings to stdout.
236 Return true if file is found, false otherwise. */
239 scan_file (char *filename
)
241 ptrdiff_t len
= strlen (filename
);
243 if (!generate_globals
)
244 put_filename (filename
);
245 if (len
> 4 && !strcmp (filename
+ len
- 4, ".elc"))
246 scan_lisp_file (filename
, "rb");
247 else if (len
> 3 && !strcmp (filename
+ len
- 3, ".el"))
248 scan_lisp_file (filename
, "r");
250 scan_c_file (filename
, "r");
256 puts ("/* This file was auto-generated by make-docfile. */");
257 puts ("/* DO NOT EDIT. */");
258 puts ("struct emacs_globals {");
261 static char input_buffer
[128];
263 /* Some state during the execution of `read_c_string_or_comment'. */
266 /* A count of spaces and newlines that have been read, but not output. */
267 intmax_t pending_spaces
, pending_newlines
;
269 /* Where we're reading from. */
272 /* If non-zero, a buffer into which to copy characters. */
274 /* If non-zero, a file into which to copy characters. */
277 /* A keyword we look for at the beginning of lines. If found, it is
278 not copied, and SAW_KEYWORD is set to true. */
280 /* The current point we've reached in an occurrence of KEYWORD in
282 const char *cur_keyword_ptr
;
283 /* Set to true if we saw an occurrence of KEYWORD. */
287 /* Output CH to the file or buffer in STATE. Any pending newlines or
288 spaces are output first. */
291 put_char (char ch
, struct rcsoc_state
*state
)
296 if (state
->pending_newlines
> 0)
298 state
->pending_newlines
--;
301 else if (state
->pending_spaces
> 0)
303 state
->pending_spaces
--;
310 putc (out_ch
, state
->out_file
);
312 *state
->buf_ptr
++ = out_ch
;
314 while (out_ch
!= ch
);
317 /* If in the middle of scanning a keyword, continue scanning with
318 character CH, otherwise output CH to the file or buffer in STATE.
319 Any pending newlines or spaces are output first, as well as any
320 previously scanned characters that were thought to be part of a
321 keyword, but were in fact not. */
324 scan_keyword_or_put_char (char ch
, struct rcsoc_state
*state
)
327 && *state
->cur_keyword_ptr
== ch
328 && (state
->cur_keyword_ptr
> state
->keyword
329 || state
->pending_newlines
> 0))
330 /* We might be looking at STATE->keyword at some point.
331 Keep looking until we know for sure. */
333 if (*++state
->cur_keyword_ptr
== '\0')
334 /* Saw the whole keyword. Set SAW_KEYWORD flag to true. */
336 state
->saw_keyword
= true;
338 /* Reset the scanning pointer. */
339 state
->cur_keyword_ptr
= state
->keyword
;
341 /* Canonicalize whitespace preceding a usage string. */
342 state
->pending_newlines
= 2;
343 state
->pending_spaces
= 0;
345 /* Skip any spaces and newlines between the keyword and the
349 c
= getc (state
->in_file
);
350 while (c
== ' ' || c
== '\n');
352 /* Output the open-paren we just read. */
354 fatal ("Missing '(' after keyword");
357 /* Skip the function name and replace it with `fn'. */
360 c
= getc (state
->in_file
);
362 fatal ("Unexpected EOF after keyword");
364 while (c
!= ' ' && c
!= ')');
366 put_char ('f', state
);
367 put_char ('n', state
);
369 /* Put back the last character. */
370 ungetc (c
, state
->in_file
);
375 if (state
->keyword
&& state
->cur_keyword_ptr
> state
->keyword
)
376 /* We scanned the beginning of a potential usage
377 keyword, but it was a false alarm. Output the
382 for (p
= state
->keyword
; p
< state
->cur_keyword_ptr
; p
++)
383 put_char (*p
, state
);
385 state
->cur_keyword_ptr
= state
->keyword
;
388 put_char (ch
, state
);
393 /* Skip a C string or C-style comment from INFILE, and return the
394 byte that follows, or EOF. COMMENT means skip a comment. If
395 PRINTFLAG is positive, output string contents to stdout. If it is
396 negative, store contents in buf. Convert escape sequences \n and
397 \t to newline and tab; discard \ followed by newline.
398 If SAW_USAGE is non-null, then any occurrences of the string "usage:"
399 at the beginning of a line will be removed, and *SAW_USAGE set to
400 true if any were encountered. */
403 read_c_string_or_comment (FILE *infile
, int printflag
, bool comment
,
407 struct rcsoc_state state
;
409 state
.in_file
= infile
;
410 state
.buf_ptr
= (printflag
< 0 ? input_buffer
: 0);
411 state
.out_file
= (printflag
> 0 ? stdout
: 0);
412 state
.pending_spaces
= 0;
413 state
.pending_newlines
= 0;
414 state
.keyword
= (saw_usage
? "usage:" : 0);
415 state
.cur_keyword_ptr
= state
.keyword
;
416 state
.saw_keyword
= false;
420 while (c_isspace (c
))
425 while (c
!= EOF
&& (comment
? c
!= '*' : c
!= '"'))
432 case '\n': case '\r':
435 case 'n': c
= '\n'; break;
436 case 't': c
= '\t'; break;
441 state
.pending_spaces
++;
444 state
.pending_newlines
++;
445 state
.pending_spaces
= 0;
448 scan_keyword_or_put_char (c
, &state
);
464 scan_keyword_or_put_char ('*', &state
);
471 /* If we had a "", concatenate the two strings. */
480 *saw_usage
= state
.saw_keyword
;
487 /* Write to stdout the argument names of function FUNC, whose text is in BUF.
488 MINARGS and MAXARGS are the minimum and maximum number of arguments. */
491 write_c_args (char *func
, char *buf
, int minargs
, int maxargs
)
494 bool in_ident
= false;
495 char *ident_start UNINIT
;
496 ptrdiff_t ident_length
= 0;
498 fputs ("(fn", stdout
);
503 for (p
= buf
; *p
; p
++)
507 /* Notice when a new identifier starts. */
508 if ((c_isalnum (c
) || c
== '_')
519 ident_length
= p
- ident_start
;
523 /* Found the end of an argument, write out the last seen
525 if (c
== ',' || c
== ')')
527 if (ident_length
== 0)
529 error ("empty arg list for '%s' should be (void), not ()", func
);
533 if (strncmp (ident_start
, "void", ident_length
) == 0)
538 if (minargs
== 0 && maxargs
> 0)
539 fputs ("&optional ", stdout
);
544 /* In C code, `default' is a reserved word, so we spell it
545 `defalt'; demangle that here. */
546 if (ident_length
== 6 && memcmp (ident_start
, "defalt", 6) == 0)
547 fputs ("DEFAULT", stdout
);
549 while (ident_length
-- > 0)
551 c
= c_toupper (*ident_start
++);
553 /* Print underscore as hyphen. */
563 /* The types of globals. These are sorted roughly in decreasing alignment
564 order to avoid allocation gaps, except that symbols and functions
576 /* A single global. */
579 enum global_type type
;
589 /* Bit values for FLAGS field from the above. Applied for DEFUNs only. */
590 enum { DEFUN_noreturn
= 1, DEFUN_const
= 2, DEFUN_noinline
= 4 };
592 /* All the variable names we saw while scanning C sources in `-g'
594 static ptrdiff_t num_globals
;
595 static ptrdiff_t num_globals_allocated
;
596 static struct global
*globals
;
598 static struct global
*
599 add_global (enum global_type type
, char const *name
, int value
,
602 /* Ignore the one non-symbol that can occur. */
603 if (strcmp (name
, "..."))
605 if (num_globals
== num_globals_allocated
)
607 ptrdiff_t num_globals_max
= (min (PTRDIFF_MAX
, SIZE_MAX
)
609 if (num_globals_allocated
== num_globals_max
)
611 if (num_globals_allocated
< num_globals_max
/ 2)
612 num_globals_allocated
= 2 * num_globals_allocated
+ 1;
614 num_globals_allocated
= num_globals_max
;
615 globals
= xrealloc (globals
, num_globals_allocated
* sizeof *globals
);
620 ptrdiff_t namesize
= strlen (name
) + 1;
621 char *buf
= xmalloc (namesize
+ (svalue
? strlen (svalue
) + 1 : 0));
622 globals
[num_globals
- 1].type
= type
;
623 globals
[num_globals
- 1].name
= strcpy (buf
, name
);
625 globals
[num_globals
- 1].v
.svalue
= strcpy (buf
+ namesize
, svalue
);
627 globals
[num_globals
- 1].v
.value
= value
;
628 globals
[num_globals
- 1].flags
= 0;
629 return globals
+ num_globals
- 1;
635 compare_globals (const void *a
, const void *b
)
637 const struct global
*ga
= a
;
638 const struct global
*gb
= b
;
640 if (ga
->type
!= gb
->type
)
641 return ga
->type
- gb
->type
;
643 /* Consider "nil" to be the least, so that iQnil is zero. That
644 way, Qnil's internal representation is zero, which is a bit faster. */
645 if (ga
->type
== SYMBOL
)
647 bool a_nil
= strcmp (ga
->name
, "Qnil") == 0;
648 bool b_nil
= strcmp (gb
->name
, "Qnil") == 0;
650 return b_nil
- a_nil
;
653 return strcmp (ga
->name
, gb
->name
);
657 close_emacs_globals (ptrdiff_t num_symbols
)
660 "extern struct emacs_globals globals;\n"
662 "#ifndef DEFINE_SYMBOLS\n"
665 "struct Lisp_Symbol lispsym[%td];\n"),
673 bool seen_defun
= false;
674 ptrdiff_t symnum
= 0;
675 ptrdiff_t num_symbols
= 0;
676 qsort (globals
, num_globals
, sizeof (struct global
), compare_globals
);
679 for (i
= 0; i
< num_globals
; i
++)
681 while (i
+ 1 < num_globals
682 && strcmp (globals
[i
].name
, globals
[i
+ 1].name
) == 0)
684 if (globals
[i
].type
== FUNCTION
685 && globals
[i
].v
.value
!= globals
[i
+ 1].v
.value
)
686 error ("function '%s' defined twice with differing signatures",
688 free (globals
[i
].name
);
691 num_symbols
+= globals
[i
].type
== SYMBOL
;
692 globals
[j
++] = globals
[i
];
696 for (i
= 0; i
< num_globals
; ++i
)
698 char const *type
= 0;
700 switch (globals
[i
].type
)
709 type
= "Lisp_Object";
715 close_emacs_globals (num_symbols
);
721 fatal ("not a recognized DEFVAR_");
726 printf (" %s f_%s;\n", type
, globals
[i
].name
);
727 printf ("#define %s globals.f_%s\n",
728 globals
[i
].name
, globals
[i
].name
);
730 else if (globals
[i
].type
== SYMBOL
)
731 printf (("#define i%s %td\n"
732 "DEFINE_LISP_SYMBOL (%s)\n"),
733 globals
[i
].name
, symnum
++, globals
[i
].name
);
736 if (globals
[i
].flags
& DEFUN_noreturn
)
737 fputs ("_Noreturn ", stdout
);
738 if (globals
[i
].flags
& DEFUN_noinline
)
739 fputs ("NO_INLINE ", stdout
);
741 printf ("EXFUN (%s, ", globals
[i
].name
);
742 if (globals
[i
].v
.value
== -1)
743 fputs ("MANY", stdout
);
744 else if (globals
[i
].v
.value
== -2)
745 fputs ("UNEVALLED", stdout
);
747 printf ("%d", globals
[i
].v
.value
);
750 if (globals
[i
].flags
& DEFUN_const
)
751 fputs (" ATTRIBUTE_CONST", stdout
);
758 close_emacs_globals (num_symbols
);
760 puts ("#ifdef DEFINE_SYMBOLS");
761 puts ("static char const *const defsym_name[] = {");
762 for (ptrdiff_t i
= 0; i
< num_globals
; i
++)
763 if (globals
[i
].type
== SYMBOL
)
764 printf ("\t\"%s\",\n", globals
[i
].v
.svalue
);
768 puts ("#define Qnil builtin_lisp_symbol (0)");
769 puts ("#if DEFINE_NON_NIL_Q_SYMBOL_MACROS");
771 for (ptrdiff_t i
= 0; i
< num_globals
; i
++)
772 if (globals
[i
].type
== SYMBOL
&& num_symbols
++ != 0)
773 printf ("# define %s builtin_lisp_symbol (%td)\n",
774 globals
[i
].name
, num_symbols
- 1);
779 /* Read through a c file. If a .o file is named,
780 the corresponding .c or .m file is read instead.
781 Looks for DEFUN constructs such as are defined in ../src/lisp.h.
782 Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */
785 scan_c_file (char *filename
, const char *mode
)
788 char extension
= filename
[strlen (filename
) - 1];
790 if (extension
== 'o')
791 filename
[strlen (filename
) - 1] = 'c';
793 infile
= fopen (filename
, mode
);
795 if (infile
== NULL
&& extension
== 'o')
798 filename
[strlen (filename
) - 1] = 'm';
799 infile
= fopen (filename
, mode
);
801 filename
[strlen (filename
) - 1] = 'c'; /* Don't confuse people. */
810 /* Reset extension to be able to detect duplicate files. */
811 filename
[strlen (filename
) - 1] = extension
;
812 scan_c_stream (infile
);
815 /* Return 1 if next input from INFILE is equal to P, -1 if EOF,
816 0 if input doesn't match. */
819 stream_match (FILE *infile
, const char *p
)
823 int c
= getc (infile
);
833 scan_c_stream (FILE *infile
)
835 int commas
, minargs
, maxargs
;
838 while (!feof (infile
))
840 bool doc_keyword
= false;
841 bool defunflag
= false;
842 bool defvarperbufferflag
= false;
843 bool defvarflag
= false;
844 enum global_type type
= INVALID
;
845 static char name
[sizeof input_buffer
];
847 if (c
!= '\n' && c
!= '\r')
875 if (c
!= ' ' && c
!= '\t' && c
!= '(')
894 defvarperbufferflag
= (c
== 'P');
895 if (generate_globals
)
898 type
= EMACS_INTEGER
;
906 /* We need to distinguish between DEFVAR_BOOL and
907 DEFVAR_BUFFER_DEFAULTS. */
908 if (generate_globals
&& type
== BOOLEAN
&& c
!= 'O')
923 defunflag
= c
== 'U';
928 && (!defvarflag
|| defvarperbufferflag
|| type
== INVALID
)
929 && !defunflag
&& type
!= SYMBOL
)
941 /* Lisp variable or function name. */
945 c
= read_c_string_or_comment (infile
, -1, false, 0);
948 if (generate_globals
)
951 char const *svalue
= 0;
953 /* Skip "," and whitespace. */
958 while (c
== ',' || c_isspace (c
));
960 /* Read in the identifier. */
965 input_buffer
[i
++] = c
;
966 if (sizeof input_buffer
<= i
)
967 fatal ("identifier too long");
970 while (! (c
== ',' || c_isspace (c
)));
972 input_buffer
[i
] = '\0';
973 memcpy (name
, input_buffer
, i
+ 1);
979 while (c_isspace (c
));
983 c
= read_c_string_or_comment (infile
, -1, false, 0);
984 svalue
= input_buffer
;
989 add_global (type
, name
, 0, svalue
);
997 /* DEFVAR_LISP ("name", addr, "doc")
998 DEFVAR_LISP ("name", addr /\* doc *\/)
999 DEFVAR_LISP ("name", addr, doc: /\* doc *\/) */
1002 commas
= generate_globals
? 4 : 5;
1003 else if (defvarperbufferflag
)
1005 else if (defvarflag
)
1007 else /* For DEFSIMPLE and DEFPRED. */
1016 if (defunflag
&& (commas
== 1 || commas
== 2))
1021 while (c_isspace (c
));
1026 if (commas
== 2) /* Pick up minargs. */
1027 scanned
= fscanf (infile
, "%d", &minargs
);
1028 else /* Pick up maxargs. */
1029 if (c
== 'M' || c
== 'U') /* MANY || UNEVALLED */
1031 if (generate_globals
)
1032 maxargs
= (c
== 'M') ? -1 : -2;
1037 scanned
= fscanf (infile
, "%d", &maxargs
);
1048 if (generate_globals
)
1050 struct global
*g
= add_global (FUNCTION
, name
, maxargs
, 0);
1054 /* The following code tries to recognize function attributes
1055 specified after the docstring, e.g.:
1057 DEFUN ("foo", Ffoo, Sfoo, X, Y, Z,
1059 attributes: attribute1 attribute2 ...)
1060 (Lisp_Object arg...)
1062 Now only ’const’, ’noinline’ and 'noreturn' attributes
1065 /* Advance to the end of docstring. */
1069 int d
= getc (infile
);
1074 if (c
== '*' && d
== '/')
1076 c
= d
, d
= getc (infile
);
1080 /* Skip spaces, if any. */
1087 while (c_isspace (c
));
1089 /* Check for 'attributes:' token. */
1090 if (c
== 'a' && stream_match (infile
, "ttributes:"))
1092 char *p
= input_buffer
;
1093 /* Collect attributes up to ')'. */
1101 if (p
- input_buffer
> sizeof (input_buffer
))
1106 if (strstr (input_buffer
, "noreturn"))
1107 g
->flags
|= DEFUN_noreturn
;
1108 if (strstr (input_buffer
, "const"))
1109 g
->flags
|= DEFUN_const
;
1110 if (strstr (input_buffer
, "noinline"))
1111 g
->flags
|= DEFUN_noinline
;
1116 while (c_isspace (c
))
1120 c
= read_c_string_or_comment (infile
, 0, false, 0);
1122 while (c
!= EOF
&& c
!= ',' && c
!= '/')
1128 while (c_isspace (c
));
1130 while (c_isalpha (c
))
1137 while (c_isspace (c
));
1143 && (c
= getc (infile
),
1147 bool comment
= c
!= '"';
1150 printf ("\037%c%s\n", defvarflag
? 'V' : 'F', input_buffer
);
1153 getc (infile
); /* Skip past `*'. */
1154 c
= read_c_string_or_comment (infile
, 1, comment
, &saw_usage
);
1156 /* If this is a defun, find the arguments and print them. If
1157 this function takes MANY or UNEVALLED args, then the C source
1158 won't give the names of the arguments, so we shouldn't bother
1159 trying to find them.
1161 Various doc-string styles:
1162 0: DEFUN (..., "DOC") (args) [!comment]
1163 1: DEFUN (..., /\* DOC *\/ (args)) [comment && !doc_keyword]
1164 2: DEFUN (..., doc: /\* DOC *\/) (args) [comment && doc_keyword]
1166 if (defunflag
&& maxargs
!= -1 && !saw_usage
)
1168 char argbuf
[1024], *p
= argbuf
;
1170 if (!comment
|| doc_keyword
)
1178 /* Skip into arguments. */
1185 /* Copy arguments into ARGBUF. */
1198 fputs ("\n\n", stdout
);
1199 write_c_args (input_buffer
, argbuf
, minargs
, maxargs
);
1201 else if (defunflag
&& maxargs
== -1 && !saw_usage
)
1202 /* The DOC should provide the usage form. */
1203 fprintf (stderr
, "Missing 'usage' for function '%s'.\n",
1208 if (ferror (infile
) || fclose (infile
) != 0)
1209 fatal ("read error");
1212 /* Read a file of Lisp code, compiled or interpreted.
1214 (defun NAME ARGS DOCSTRING ...)
1215 (defmacro NAME ARGS DOCSTRING ...)
1216 (defsubst NAME ARGS DOCSTRING ...)
1217 (autoload (quote NAME) FILE DOCSTRING ...)
1218 (defvar NAME VALUE DOCSTRING)
1219 (defconst NAME VALUE DOCSTRING)
1220 (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
1221 (fset (quote NAME) #[... DOCSTRING ...])
1222 (defalias (quote NAME) #[... DOCSTRING ...])
1223 (custom-declare-variable (quote NAME) VALUE DOCSTRING ...)
1224 starting in column zero.
1225 (quote NAME) may appear as 'NAME as well.
1227 We also look for #@LENGTH CONTENTS^_ at the beginning of the line.
1228 When we find that, we save it for the following defining-form,
1229 and we use that instead of reading a doc string within that defining-form.
1231 For defvar, defconst, and fset we skip to the docstring with a kludgy
1232 formatting convention: all docstrings must appear on the same line as the
1233 initial open-paren (the one in column zero) and must contain a backslash
1234 and a newline immediately after the initial double-quote. No newlines
1235 must appear between the beginning of the form and the first double-quote.
1236 For defun, defmacro, and autoload, we know how to skip over the
1237 arglist, but the doc string must still have a backslash and newline
1238 immediately after the double quote.
1239 The only source files that must follow this convention are preloaded
1240 uncompiled ones like loaddefs.el; aside from that, it is always the .elc
1241 file that we should look at, and they are no problem because byte-compiler
1242 output follows this convention.
1243 The NAME and DOCSTRING are output.
1244 NAME is preceded by `F' for a function or `V' for a variable.
1245 An entry is output only if DOCSTRING has \ newline just after the opening ".
1249 skip_white (FILE *infile
)
1254 while (c_isspace (c
));
1260 read_lisp_symbol (FILE *infile
, char *buffer
)
1263 char *fillp
= buffer
;
1265 skip_white (infile
);
1276 else if (c_isspace (c
) || c
== '(' || c
== ')' || c
< 0)
1287 fprintf (stderr
, "## expected a symbol, got '%c'\n", c
);
1289 skip_white (infile
);
1293 search_lisp_doc_at_eol (FILE *infile
)
1295 int c
= 0, c1
= 0, c2
= 0;
1297 /* Skip until the end of line; remember two previous chars. */
1298 while (c
!= '\n' && c
!= '\r' && c
!= EOF
)
1305 /* If two previous characters were " and \,
1306 this is a doc string. Otherwise, there is none. */
1307 if (c2
!= '"' || c1
!= '\\')
1310 fprintf (stderr
, "## non-docstring found\n");
1318 #define DEF_ELISP_FILE(fn) { #fn, sizeof(#fn) - 1 }
1321 scan_lisp_file (const char *filename
, const char *mode
)
1325 char *saved_string
= 0;
1326 /* These are the only files that are loaded uncompiled, and must
1327 follow the conventions of the doc strings expected by this
1328 function. These conventions are automatically followed by the
1329 byte compiler when it produces the .elc files. */
1333 } const uncompiled
[] = {
1334 DEF_ELISP_FILE (loaddefs
.el
),
1335 DEF_ELISP_FILE (loadup
.el
),
1336 DEF_ELISP_FILE (charprop
.el
),
1337 DEF_ELISP_FILE (cp51932
.el
),
1338 DEF_ELISP_FILE (eucjp
-ms
.el
)
1341 int flen
= strlen (filename
);
1343 if (generate_globals
)
1344 fatal ("scanning lisp file when -g specified");
1345 if (flen
> 3 && !strcmp (filename
+ flen
- 3, ".el"))
1348 for (i
= 0; i
< sizeof (uncompiled
) / sizeof (uncompiled
[0]); i
++)
1350 if (uncompiled
[i
].fl
<= flen
1351 && !strcmp (filename
+ flen
- uncompiled
[i
].fl
, uncompiled
[i
].fn
)
1352 && (flen
== uncompiled
[i
].fl
1353 || IS_SLASH (filename
[flen
- uncompiled
[i
].fl
- 1])))
1360 fatal ("uncompiled lisp file %s is not supported", filename
);
1363 infile
= fopen (filename
, mode
);
1367 exit (EXIT_FAILURE
);
1371 while (!feof (infile
))
1373 char buffer
[BUFSIZ
];
1376 /* If not at end of line, skip till we get to one. */
1377 if (c
!= '\n' && c
!= '\r')
1382 /* Skip the line break. */
1383 while (c
== '\n' || c
== '\r')
1385 /* Detect a dynamic doc string and save it for the next expression. */
1391 ptrdiff_t length
= 0;
1394 /* Read the length. */
1395 while ((c
= getc (infile
),
1398 if (INT_MULTIPLY_WRAPV (length
, 10, &length
)
1399 || INT_ADD_WRAPV (length
, c
- '0', &length
)
1400 || SIZE_MAX
< length
)
1401 memory_exhausted ();
1405 fatal ("invalid dynamic doc string length");
1408 fatal ("space not found after dynamic doc string length");
1410 /* The next character is a space that is counted in the length
1411 but not part of the doc string.
1412 We already read it, so just ignore it. */
1415 /* Read in the contents. */
1416 free (saved_string
);
1417 saved_string
= xmalloc (length
);
1418 for (i
= 0; i
< length
; i
++)
1419 saved_string
[i
] = getc (infile
);
1420 /* The last character is a ^_.
1421 That is needed in the .elc file
1422 but it is redundant in DOC. So get rid of it here. */
1423 saved_string
[length
- 1] = 0;
1424 /* Skip the line break. */
1425 while (c
== '\n' || c
== '\r')
1427 /* Skip the following line. */
1428 while (! (c
== '\n' || c
== '\r' || c
< 0))
1437 read_lisp_symbol (infile
, buffer
);
1439 if (! strcmp (buffer
, "defun")
1440 || ! strcmp (buffer
, "defmacro")
1441 || ! strcmp (buffer
, "defsubst"))
1444 read_lisp_symbol (infile
, buffer
);
1446 /* Skip the arguments: either "nil" or a list in parens. */
1449 if (c
== 'n') /* nil */
1451 if ((c
= getc (infile
)) != 'i'
1452 || (c
= getc (infile
)) != 'l')
1454 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
1461 fprintf (stderr
, "## unparsable arglist in %s (%s)\n",
1466 while (! (c
== ')' || c
< 0))
1468 skip_white (infile
);
1470 /* If the next three characters aren't `dquote bslash newline'
1471 then we're not reading a docstring.
1473 if ((c
= getc (infile
)) != '"'
1474 || (c
= getc (infile
)) != '\\'
1475 || ((c
= getc (infile
)) != '\n' && c
!= '\r'))
1478 fprintf (stderr
, "## non-docstring in %s (%s)\n",
1485 /* defcustom can only occur in uncompiled Lisp files. */
1486 else if (! strcmp (buffer
, "defvar")
1487 || ! strcmp (buffer
, "defconst")
1488 || ! strcmp (buffer
, "defcustom"))
1491 read_lisp_symbol (infile
, buffer
);
1493 if (saved_string
== 0)
1494 if (!search_lisp_doc_at_eol (infile
))
1498 else if (! strcmp (buffer
, "custom-declare-variable")
1499 || ! strcmp (buffer
, "defvaralias")
1506 read_lisp_symbol (infile
, buffer
);
1512 "## unparsable name in custom-declare-variable in %s\n",
1516 read_lisp_symbol (infile
, buffer
);
1517 if (strcmp (buffer
, "quote"))
1520 "## unparsable name in custom-declare-variable in %s\n",
1524 read_lisp_symbol (infile
, buffer
);
1529 "## unparsable quoted name in custom-declare-variable in %s\n",
1535 if (saved_string
== 0)
1536 if (!search_lisp_doc_at_eol (infile
))
1540 else if (! strcmp (buffer
, "fset") || ! strcmp (buffer
, "defalias"))
1546 read_lisp_symbol (infile
, buffer
);
1551 fprintf (stderr
, "## unparsable name in fset in %s\n",
1555 read_lisp_symbol (infile
, buffer
);
1556 if (strcmp (buffer
, "quote"))
1558 fprintf (stderr
, "## unparsable name in fset in %s\n",
1562 read_lisp_symbol (infile
, buffer
);
1567 "## unparsable quoted name in fset in %s\n",
1573 if (saved_string
== 0)
1574 if (!search_lisp_doc_at_eol (infile
))
1578 else if (! strcmp (buffer
, "autoload"))
1583 read_lisp_symbol (infile
, buffer
);
1588 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1592 read_lisp_symbol (infile
, buffer
);
1593 if (strcmp (buffer
, "quote"))
1595 fprintf (stderr
, "## unparsable name in autoload in %s\n",
1599 read_lisp_symbol (infile
, buffer
);
1604 "## unparsable quoted name in autoload in %s\n",
1609 skip_white (infile
);
1613 fprintf (stderr
, "## autoload of %s unparsable (%s)\n",
1617 read_c_string_or_comment (infile
, 0, false, 0);
1619 if (saved_string
== 0)
1620 if (!search_lisp_doc_at_eol (infile
))
1625 else if (! strcmp (buffer
, "if")
1626 || ! strcmp (buffer
, "byte-code"))
1633 fprintf (stderr
, "## unrecognized top-level form, %s (%s)\n",
1639 /* At this point, we should either use the previous dynamic doc string in
1640 saved_string or gobble a doc string from the input file.
1641 In the latter case, the opening quote (and leading backslash-newline)
1642 have already been read. */
1644 printf ("\037%c%s\n", type
, buffer
);
1647 fputs (saved_string
, stdout
);
1648 /* Don't use one dynamic doc string twice. */
1649 free (saved_string
);
1653 read_c_string_or_comment (infile
, 1, false, 0);
1655 free (saved_string
);
1656 if (ferror (infile
) || fclose (infile
) != 0)
1657 fatal ("%s: read error", filename
);
1661 /* make-docfile.c ends here */