]> code.delx.au - gnu-emacs/blob - src/lread.c
Compute C decls for DEFSYMs automatically
[gnu-emacs] / src / lread.c
1 /* Lisp parsing and input streams.
2
3 Copyright (C) 1985-1989, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
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.
12
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.
17
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/>. */
20
21 /* Tell globals.h to define tables needed by init_obarray. */
22 #define DEFINE_SYMBOLS
23
24 #include <config.h>
25 #include "sysstdio.h"
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/file.h>
29 #include <errno.h>
30 #include <limits.h> /* For CHAR_BIT. */
31 #include <stat-time.h>
32 #include "lisp.h"
33 #include "intervals.h"
34 #include "character.h"
35 #include "buffer.h"
36 #include "charset.h"
37 #include "coding.h"
38 #include <epaths.h>
39 #include "commands.h"
40 #include "keyboard.h"
41 #include "frame.h"
42 #include "termhooks.h"
43 #include "blockinput.h"
44
45 #ifdef MSDOS
46 #include "msdos.h"
47 #endif
48
49 #ifdef HAVE_NS
50 #include "nsterm.h"
51 #endif
52
53 #include <unistd.h>
54
55 #ifdef HAVE_SETLOCALE
56 #include <locale.h>
57 #endif /* HAVE_SETLOCALE */
58
59 #include <fcntl.h>
60
61 #ifdef HAVE_FSEEKO
62 #define file_offset off_t
63 #define file_tell ftello
64 #else
65 #define file_offset long
66 #define file_tell ftell
67 #endif
68
69 /* The association list of objects read with the #n=object form.
70 Each member of the list has the form (n . object), and is used to
71 look up the object for the corresponding #n# construct.
72 It must be set to nil before all top-level calls to read0. */
73 static Lisp_Object read_objects;
74
75 /* File for get_file_char to read from. Use by load. */
76 static FILE *instream;
77
78 /* For use within read-from-string (this reader is non-reentrant!!) */
79 static ptrdiff_t read_from_string_index;
80 static ptrdiff_t read_from_string_index_byte;
81 static ptrdiff_t read_from_string_limit;
82
83 /* Number of characters read in the current call to Fread or
84 Fread_from_string. */
85 static EMACS_INT readchar_count;
86
87 /* This contains the last string skipped with #@. */
88 static char *saved_doc_string;
89 /* Length of buffer allocated in saved_doc_string. */
90 static ptrdiff_t saved_doc_string_size;
91 /* Length of actual data in saved_doc_string. */
92 static ptrdiff_t saved_doc_string_length;
93 /* This is the file position that string came from. */
94 static file_offset saved_doc_string_position;
95
96 /* This contains the previous string skipped with #@.
97 We copy it from saved_doc_string when a new string
98 is put in saved_doc_string. */
99 static char *prev_saved_doc_string;
100 /* Length of buffer allocated in prev_saved_doc_string. */
101 static ptrdiff_t prev_saved_doc_string_size;
102 /* Length of actual data in prev_saved_doc_string. */
103 static ptrdiff_t prev_saved_doc_string_length;
104 /* This is the file position that string came from. */
105 static file_offset prev_saved_doc_string_position;
106
107 /* True means inside a new-style backquote
108 with no surrounding parentheses.
109 Fread initializes this to false, so we need not specbind it
110 or worry about what happens to it when there is an error. */
111 static bool new_backquote_flag;
112
113 /* A list of file names for files being loaded in Fload. Used to
114 check for recursive loads. */
115
116 static Lisp_Object Vloads_in_progress;
117
118 static int read_emacs_mule_char (int, int (*) (int, Lisp_Object),
119 Lisp_Object);
120
121 static void readevalloop (Lisp_Object, FILE *, Lisp_Object, bool,
122 Lisp_Object, Lisp_Object,
123 Lisp_Object, Lisp_Object);
124 \f
125 /* Functions that read one byte from the current source READCHARFUN
126 or unreads one byte. If the integer argument C is -1, it returns
127 one read byte, or -1 when there's no more byte in the source. If C
128 is 0 or positive, it unreads C, and the return value is not
129 interesting. */
130
131 static int readbyte_for_lambda (int, Lisp_Object);
132 static int readbyte_from_file (int, Lisp_Object);
133 static int readbyte_from_string (int, Lisp_Object);
134
135 /* Handle unreading and rereading of characters.
136 Write READCHAR to read a character,
137 UNREAD(c) to unread c to be read again.
138
139 These macros correctly read/unread multibyte characters. */
140
141 #define READCHAR readchar (readcharfun, NULL)
142 #define UNREAD(c) unreadchar (readcharfun, c)
143
144 /* Same as READCHAR but set *MULTIBYTE to the multibyteness of the source. */
145 #define READCHAR_REPORT_MULTIBYTE(multibyte) readchar (readcharfun, multibyte)
146
147 /* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
148 Qlambda, or a cons, we use this to keep an unread character because
149 a file stream can't handle multibyte-char unreading. The value -1
150 means that there's no unread character. */
151 static int unread_char;
152
153 static int
154 readchar (Lisp_Object readcharfun, bool *multibyte)
155 {
156 Lisp_Object tem;
157 register int c;
158 int (*readbyte) (int, Lisp_Object);
159 unsigned char buf[MAX_MULTIBYTE_LENGTH];
160 int i, len;
161 bool emacs_mule_encoding = 0;
162
163 if (multibyte)
164 *multibyte = 0;
165
166 readchar_count++;
167
168 if (BUFFERP (readcharfun))
169 {
170 register struct buffer *inbuffer = XBUFFER (readcharfun);
171
172 ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
173
174 if (! BUFFER_LIVE_P (inbuffer))
175 return -1;
176
177 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
178 return -1;
179
180 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
181 {
182 /* Fetch the character code from the buffer. */
183 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
184 BUF_INC_POS (inbuffer, pt_byte);
185 c = STRING_CHAR (p);
186 if (multibyte)
187 *multibyte = 1;
188 }
189 else
190 {
191 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
192 if (! ASCII_CHAR_P (c))
193 c = BYTE8_TO_CHAR (c);
194 pt_byte++;
195 }
196 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
197
198 return c;
199 }
200 if (MARKERP (readcharfun))
201 {
202 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
203
204 ptrdiff_t bytepos = marker_byte_position (readcharfun);
205
206 if (bytepos >= BUF_ZV_BYTE (inbuffer))
207 return -1;
208
209 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
210 {
211 /* Fetch the character code from the buffer. */
212 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
213 BUF_INC_POS (inbuffer, bytepos);
214 c = STRING_CHAR (p);
215 if (multibyte)
216 *multibyte = 1;
217 }
218 else
219 {
220 c = BUF_FETCH_BYTE (inbuffer, bytepos);
221 if (! ASCII_CHAR_P (c))
222 c = BYTE8_TO_CHAR (c);
223 bytepos++;
224 }
225
226 XMARKER (readcharfun)->bytepos = bytepos;
227 XMARKER (readcharfun)->charpos++;
228
229 return c;
230 }
231
232 if (EQ (readcharfun, Qlambda))
233 {
234 readbyte = readbyte_for_lambda;
235 goto read_multibyte;
236 }
237
238 if (EQ (readcharfun, Qget_file_char))
239 {
240 readbyte = readbyte_from_file;
241 goto read_multibyte;
242 }
243
244 if (STRINGP (readcharfun))
245 {
246 if (read_from_string_index >= read_from_string_limit)
247 c = -1;
248 else if (STRING_MULTIBYTE (readcharfun))
249 {
250 if (multibyte)
251 *multibyte = 1;
252 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, readcharfun,
253 read_from_string_index,
254 read_from_string_index_byte);
255 }
256 else
257 {
258 c = SREF (readcharfun, read_from_string_index_byte);
259 read_from_string_index++;
260 read_from_string_index_byte++;
261 }
262 return c;
263 }
264
265 if (CONSP (readcharfun))
266 {
267 /* This is the case that read_vector is reading from a unibyte
268 string that contains a byte sequence previously skipped
269 because of #@NUMBER. The car part of readcharfun is that
270 string, and the cdr part is a value of readcharfun given to
271 read_vector. */
272 readbyte = readbyte_from_string;
273 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
274 emacs_mule_encoding = 1;
275 goto read_multibyte;
276 }
277
278 if (EQ (readcharfun, Qget_emacs_mule_file_char))
279 {
280 readbyte = readbyte_from_file;
281 emacs_mule_encoding = 1;
282 goto read_multibyte;
283 }
284
285 tem = call0 (readcharfun);
286
287 if (NILP (tem))
288 return -1;
289 return XINT (tem);
290
291 read_multibyte:
292 if (unread_char >= 0)
293 {
294 c = unread_char;
295 unread_char = -1;
296 return c;
297 }
298 c = (*readbyte) (-1, readcharfun);
299 if (c < 0)
300 return c;
301 if (multibyte)
302 *multibyte = 1;
303 if (ASCII_CHAR_P (c))
304 return c;
305 if (emacs_mule_encoding)
306 return read_emacs_mule_char (c, readbyte, readcharfun);
307 i = 0;
308 buf[i++] = c;
309 len = BYTES_BY_CHAR_HEAD (c);
310 while (i < len)
311 {
312 c = (*readbyte) (-1, readcharfun);
313 if (c < 0 || ! TRAILING_CODE_P (c))
314 {
315 while (--i > 1)
316 (*readbyte) (buf[i], readcharfun);
317 return BYTE8_TO_CHAR (buf[0]);
318 }
319 buf[i++] = c;
320 }
321 return STRING_CHAR (buf);
322 }
323
324 #define FROM_FILE_P(readcharfun) \
325 (EQ (readcharfun, Qget_file_char) \
326 || EQ (readcharfun, Qget_emacs_mule_file_char))
327
328 static void
329 skip_dyn_bytes (Lisp_Object readcharfun, ptrdiff_t n)
330 {
331 if (FROM_FILE_P (readcharfun))
332 {
333 block_input (); /* FIXME: Not sure if it's needed. */
334 fseek (instream, n, SEEK_CUR);
335 unblock_input ();
336 }
337 else
338 { /* We're not reading directly from a file. In that case, it's difficult
339 to reliably count bytes, since these are usually meant for the file's
340 encoding, whereas we're now typically in the internal encoding.
341 But luckily, skip_dyn_bytes is used to skip over a single
342 dynamic-docstring (or dynamic byte-code) which is always quoted such
343 that \037 is the final char. */
344 int c;
345 do {
346 c = READCHAR;
347 } while (c >= 0 && c != '\037');
348 }
349 }
350
351 static void
352 skip_dyn_eof (Lisp_Object readcharfun)
353 {
354 if (FROM_FILE_P (readcharfun))
355 {
356 block_input (); /* FIXME: Not sure if it's needed. */
357 fseek (instream, 0, SEEK_END);
358 unblock_input ();
359 }
360 else
361 while (READCHAR >= 0);
362 }
363
364 /* Unread the character C in the way appropriate for the stream READCHARFUN.
365 If the stream is a user function, call it with the char as argument. */
366
367 static void
368 unreadchar (Lisp_Object readcharfun, int c)
369 {
370 readchar_count--;
371 if (c == -1)
372 /* Don't back up the pointer if we're unreading the end-of-input mark,
373 since readchar didn't advance it when we read it. */
374 ;
375 else if (BUFFERP (readcharfun))
376 {
377 struct buffer *b = XBUFFER (readcharfun);
378 ptrdiff_t charpos = BUF_PT (b);
379 ptrdiff_t bytepos = BUF_PT_BYTE (b);
380
381 if (! NILP (BVAR (b, enable_multibyte_characters)))
382 BUF_DEC_POS (b, bytepos);
383 else
384 bytepos--;
385
386 SET_BUF_PT_BOTH (b, charpos - 1, bytepos);
387 }
388 else if (MARKERP (readcharfun))
389 {
390 struct buffer *b = XMARKER (readcharfun)->buffer;
391 ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
392
393 XMARKER (readcharfun)->charpos--;
394 if (! NILP (BVAR (b, enable_multibyte_characters)))
395 BUF_DEC_POS (b, bytepos);
396 else
397 bytepos--;
398
399 XMARKER (readcharfun)->bytepos = bytepos;
400 }
401 else if (STRINGP (readcharfun))
402 {
403 read_from_string_index--;
404 read_from_string_index_byte
405 = string_char_to_byte (readcharfun, read_from_string_index);
406 }
407 else if (CONSP (readcharfun))
408 {
409 unread_char = c;
410 }
411 else if (EQ (readcharfun, Qlambda))
412 {
413 unread_char = c;
414 }
415 else if (FROM_FILE_P (readcharfun))
416 {
417 unread_char = c;
418 }
419 else
420 call1 (readcharfun, make_number (c));
421 }
422
423 static int
424 readbyte_for_lambda (int c, Lisp_Object readcharfun)
425 {
426 return read_bytecode_char (c >= 0);
427 }
428
429
430 static int
431 readbyte_from_file (int c, Lisp_Object readcharfun)
432 {
433 if (c >= 0)
434 {
435 block_input ();
436 ungetc (c, instream);
437 unblock_input ();
438 return 0;
439 }
440
441 block_input ();
442 c = getc (instream);
443
444 /* Interrupted reads have been observed while reading over the network. */
445 while (c == EOF && ferror (instream) && errno == EINTR)
446 {
447 unblock_input ();
448 QUIT;
449 block_input ();
450 clearerr (instream);
451 c = getc (instream);
452 }
453
454 unblock_input ();
455
456 return (c == EOF ? -1 : c);
457 }
458
459 static int
460 readbyte_from_string (int c, Lisp_Object readcharfun)
461 {
462 Lisp_Object string = XCAR (readcharfun);
463
464 if (c >= 0)
465 {
466 read_from_string_index--;
467 read_from_string_index_byte
468 = string_char_to_byte (string, read_from_string_index);
469 }
470
471 if (read_from_string_index >= read_from_string_limit)
472 c = -1;
473 else
474 FETCH_STRING_CHAR_ADVANCE (c, string,
475 read_from_string_index,
476 read_from_string_index_byte);
477 return c;
478 }
479
480
481 /* Read one non-ASCII character from INSTREAM. The character is
482 encoded in `emacs-mule' and the first byte is already read in
483 C. */
484
485 static int
486 read_emacs_mule_char (int c, int (*readbyte) (int, Lisp_Object), Lisp_Object readcharfun)
487 {
488 /* Emacs-mule coding uses at most 4-byte for one character. */
489 unsigned char buf[4];
490 int len = emacs_mule_bytes[c];
491 struct charset *charset;
492 int i;
493 unsigned code;
494
495 if (len == 1)
496 /* C is not a valid leading-code of `emacs-mule'. */
497 return BYTE8_TO_CHAR (c);
498
499 i = 0;
500 buf[i++] = c;
501 while (i < len)
502 {
503 c = (*readbyte) (-1, readcharfun);
504 if (c < 0xA0)
505 {
506 while (--i > 1)
507 (*readbyte) (buf[i], readcharfun);
508 return BYTE8_TO_CHAR (buf[0]);
509 }
510 buf[i++] = c;
511 }
512
513 if (len == 2)
514 {
515 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
516 code = buf[1] & 0x7F;
517 }
518 else if (len == 3)
519 {
520 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
521 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
522 {
523 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
524 code = buf[2] & 0x7F;
525 }
526 else
527 {
528 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
529 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
530 }
531 }
532 else
533 {
534 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
535 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
536 }
537 c = DECODE_CHAR (charset, code);
538 if (c < 0)
539 Fsignal (Qinvalid_read_syntax,
540 list1 (build_string ("invalid multibyte form")));
541 return c;
542 }
543
544
545 static Lisp_Object read_internal_start (Lisp_Object, Lisp_Object,
546 Lisp_Object);
547 static Lisp_Object read0 (Lisp_Object);
548 static Lisp_Object read1 (Lisp_Object, int *, bool);
549
550 static Lisp_Object read_list (bool, Lisp_Object);
551 static Lisp_Object read_vector (Lisp_Object, bool);
552
553 static Lisp_Object substitute_object_recurse (Lisp_Object, Lisp_Object,
554 Lisp_Object);
555 static void substitute_object_in_subtree (Lisp_Object,
556 Lisp_Object);
557 static void substitute_in_interval (INTERVAL, Lisp_Object);
558
559 \f
560 /* Get a character from the tty. */
561
562 /* Read input events until we get one that's acceptable for our purposes.
563
564 If NO_SWITCH_FRAME, switch-frame events are stashed
565 until we get a character we like, and then stuffed into
566 unread_switch_frame.
567
568 If ASCII_REQUIRED, check function key events to see
569 if the unmodified version of the symbol has a Qascii_character
570 property, and use that character, if present.
571
572 If ERROR_NONASCII, signal an error if the input we
573 get isn't an ASCII character with modifiers. If it's false but
574 ASCII_REQUIRED is true, just re-read until we get an ASCII
575 character.
576
577 If INPUT_METHOD, invoke the current input method
578 if the character warrants that.
579
580 If SECONDS is a number, wait that many seconds for input, and
581 return Qnil if no input arrives within that time. */
582
583 static Lisp_Object
584 read_filtered_event (bool no_switch_frame, bool ascii_required,
585 bool error_nonascii, bool input_method, Lisp_Object seconds)
586 {
587 Lisp_Object val, delayed_switch_frame;
588 struct timespec end_time;
589
590 #ifdef HAVE_WINDOW_SYSTEM
591 if (display_hourglass_p)
592 cancel_hourglass ();
593 #endif
594
595 delayed_switch_frame = Qnil;
596
597 /* Compute timeout. */
598 if (NUMBERP (seconds))
599 {
600 double duration = extract_float (seconds);
601 struct timespec wait_time = dtotimespec (duration);
602 end_time = timespec_add (current_timespec (), wait_time);
603 }
604
605 /* Read until we get an acceptable event. */
606 retry:
607 do
608 val = read_char (0, Qnil, (input_method ? Qnil : Qt), 0,
609 NUMBERP (seconds) ? &end_time : NULL);
610 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
611
612 if (BUFFERP (val))
613 goto retry;
614
615 /* `switch-frame' events are put off until after the next ASCII
616 character. This is better than signaling an error just because
617 the last characters were typed to a separate minibuffer frame,
618 for example. Eventually, some code which can deal with
619 switch-frame events will read it and process it. */
620 if (no_switch_frame
621 && EVENT_HAS_PARAMETERS (val)
622 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
623 {
624 delayed_switch_frame = val;
625 goto retry;
626 }
627
628 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
629 {
630 /* Convert certain symbols to their ASCII equivalents. */
631 if (SYMBOLP (val))
632 {
633 Lisp_Object tem, tem1;
634 tem = Fget (val, Qevent_symbol_element_mask);
635 if (!NILP (tem))
636 {
637 tem1 = Fget (Fcar (tem), Qascii_character);
638 /* Merge this symbol's modifier bits
639 with the ASCII equivalent of its basic code. */
640 if (!NILP (tem1))
641 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
642 }
643 }
644
645 /* If we don't have a character now, deal with it appropriately. */
646 if (!INTEGERP (val))
647 {
648 if (error_nonascii)
649 {
650 Vunread_command_events = list1 (val);
651 error ("Non-character input-event");
652 }
653 else
654 goto retry;
655 }
656 }
657
658 if (! NILP (delayed_switch_frame))
659 unread_switch_frame = delayed_switch_frame;
660
661 #if 0
662
663 #ifdef HAVE_WINDOW_SYSTEM
664 if (display_hourglass_p)
665 start_hourglass ();
666 #endif
667
668 #endif
669
670 return val;
671 }
672
673 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
674 doc: /* Read a character from the command input (keyboard or macro).
675 It is returned as a number.
676 If the character has modifiers, they are resolved and reflected to the
677 character code if possible (e.g. C-SPC -> 0).
678
679 If the user generates an event which is not a character (i.e. a mouse
680 click or function key event), `read-char' signals an error. As an
681 exception, switch-frame events are put off until non-character events
682 can be read.
683 If you want to read non-character events, or ignore them, call
684 `read-event' or `read-char-exclusive' instead.
685
686 If the optional argument PROMPT is non-nil, display that as a prompt.
687 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
688 input method is turned on in the current buffer, that input method
689 is used for reading a character.
690 If the optional argument SECONDS is non-nil, it should be a number
691 specifying the maximum number of seconds to wait for input. If no
692 input arrives in that time, return nil. SECONDS may be a
693 floating-point value. */)
694 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
695 {
696 Lisp_Object val;
697
698 if (! NILP (prompt))
699 message_with_string ("%s", prompt, 0);
700 val = read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
701
702 return (NILP (val) ? Qnil
703 : make_number (char_resolve_modifier_mask (XINT (val))));
704 }
705
706 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
707 doc: /* Read an event object from the input stream.
708 If the optional argument PROMPT is non-nil, display that as a prompt.
709 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
710 input method is turned on in the current buffer, that input method
711 is used for reading a character.
712 If the optional argument SECONDS is non-nil, it should be a number
713 specifying the maximum number of seconds to wait for input. If no
714 input arrives in that time, return nil. SECONDS may be a
715 floating-point value. */)
716 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
717 {
718 if (! NILP (prompt))
719 message_with_string ("%s", prompt, 0);
720 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
721 }
722
723 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
724 doc: /* Read a character from the command input (keyboard or macro).
725 It is returned as a number. Non-character events are ignored.
726 If the character has modifiers, they are resolved and reflected to the
727 character code if possible (e.g. C-SPC -> 0).
728
729 If the optional argument PROMPT is non-nil, display that as a prompt.
730 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
731 input method is turned on in the current buffer, that input method
732 is used for reading a character.
733 If the optional argument SECONDS is non-nil, it should be a number
734 specifying the maximum number of seconds to wait for input. If no
735 input arrives in that time, return nil. SECONDS may be a
736 floating-point value. */)
737 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
738 {
739 Lisp_Object val;
740
741 if (! NILP (prompt))
742 message_with_string ("%s", prompt, 0);
743
744 val = read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
745
746 return (NILP (val) ? Qnil
747 : make_number (char_resolve_modifier_mask (XINT (val))));
748 }
749
750 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
751 doc: /* Don't use this yourself. */)
752 (void)
753 {
754 register Lisp_Object val;
755 block_input ();
756 XSETINT (val, getc (instream));
757 unblock_input ();
758 return val;
759 }
760
761
762 \f
763
764 /* Return true if the lisp code read using READCHARFUN defines a non-nil
765 `lexical-binding' file variable. After returning, the stream is
766 positioned following the first line, if it is a comment or #! line,
767 otherwise nothing is read. */
768
769 static bool
770 lisp_file_lexically_bound_p (Lisp_Object readcharfun)
771 {
772 int ch = READCHAR;
773
774 if (ch == '#')
775 {
776 ch = READCHAR;
777 if (ch != '!')
778 {
779 UNREAD (ch);
780 UNREAD ('#');
781 return 0;
782 }
783 while (ch != '\n' && ch != EOF)
784 ch = READCHAR;
785 if (ch == '\n') ch = READCHAR;
786 /* It is OK to leave the position after a #! line, since
787 that is what read1 does. */
788 }
789
790 if (ch != ';')
791 /* The first line isn't a comment, just give up. */
792 {
793 UNREAD (ch);
794 return 0;
795 }
796 else
797 /* Look for an appropriate file-variable in the first line. */
798 {
799 bool rv = 0;
800 enum {
801 NOMINAL, AFTER_FIRST_DASH, AFTER_ASTERIX
802 } beg_end_state = NOMINAL;
803 bool in_file_vars = 0;
804
805 #define UPDATE_BEG_END_STATE(ch) \
806 if (beg_end_state == NOMINAL) \
807 beg_end_state = (ch == '-' ? AFTER_FIRST_DASH : NOMINAL); \
808 else if (beg_end_state == AFTER_FIRST_DASH) \
809 beg_end_state = (ch == '*' ? AFTER_ASTERIX : NOMINAL); \
810 else if (beg_end_state == AFTER_ASTERIX) \
811 { \
812 if (ch == '-') \
813 in_file_vars = !in_file_vars; \
814 beg_end_state = NOMINAL; \
815 }
816
817 /* Skip until we get to the file vars, if any. */
818 do
819 {
820 ch = READCHAR;
821 UPDATE_BEG_END_STATE (ch);
822 }
823 while (!in_file_vars && ch != '\n' && ch != EOF);
824
825 while (in_file_vars)
826 {
827 char var[100], val[100];
828 unsigned i;
829
830 ch = READCHAR;
831
832 /* Read a variable name. */
833 while (ch == ' ' || ch == '\t')
834 ch = READCHAR;
835
836 i = 0;
837 while (ch != ':' && ch != '\n' && ch != EOF && in_file_vars)
838 {
839 if (i < sizeof var - 1)
840 var[i++] = ch;
841 UPDATE_BEG_END_STATE (ch);
842 ch = READCHAR;
843 }
844
845 /* Stop scanning if no colon was found before end marker. */
846 if (!in_file_vars || ch == '\n' || ch == EOF)
847 break;
848
849 while (i > 0 && (var[i - 1] == ' ' || var[i - 1] == '\t'))
850 i--;
851 var[i] = '\0';
852
853 if (ch == ':')
854 {
855 /* Read a variable value. */
856 ch = READCHAR;
857
858 while (ch == ' ' || ch == '\t')
859 ch = READCHAR;
860
861 i = 0;
862 while (ch != ';' && ch != '\n' && ch != EOF && in_file_vars)
863 {
864 if (i < sizeof val - 1)
865 val[i++] = ch;
866 UPDATE_BEG_END_STATE (ch);
867 ch = READCHAR;
868 }
869 if (! in_file_vars)
870 /* The value was terminated by an end-marker, which remove. */
871 i -= 3;
872 while (i > 0 && (val[i - 1] == ' ' || val[i - 1] == '\t'))
873 i--;
874 val[i] = '\0';
875
876 if (strcmp (var, "lexical-binding") == 0)
877 /* This is it... */
878 {
879 rv = (strcmp (val, "nil") != 0);
880 break;
881 }
882 }
883 }
884
885 while (ch != '\n' && ch != EOF)
886 ch = READCHAR;
887
888 return rv;
889 }
890 }
891 \f
892 /* Value is a version number of byte compiled code if the file
893 associated with file descriptor FD is a compiled Lisp file that's
894 safe to load. Only files compiled with Emacs are safe to load.
895 Files compiled with XEmacs can lead to a crash in Fbyte_code
896 because of an incompatible change in the byte compiler. */
897
898 static int
899 safe_to_load_version (int fd)
900 {
901 char buf[512];
902 int nbytes, i;
903 int version = 1;
904
905 /* Read the first few bytes from the file, and look for a line
906 specifying the byte compiler version used. */
907 nbytes = emacs_read (fd, buf, sizeof buf);
908 if (nbytes > 0)
909 {
910 /* Skip to the next newline, skipping over the initial `ELC'
911 with NUL bytes following it, but note the version. */
912 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
913 if (i == 4)
914 version = buf[i];
915
916 if (i >= nbytes
917 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
918 buf + i, nbytes - i) < 0)
919 version = 0;
920 }
921
922 lseek (fd, 0, SEEK_SET);
923 return version;
924 }
925
926
927 /* Callback for record_unwind_protect. Restore the old load list OLD,
928 after loading a file successfully. */
929
930 static void
931 record_load_unwind (Lisp_Object old)
932 {
933 Vloads_in_progress = old;
934 }
935
936 /* This handler function is used via internal_condition_case_1. */
937
938 static Lisp_Object
939 load_error_handler (Lisp_Object data)
940 {
941 return Qnil;
942 }
943
944 static void
945 load_warn_old_style_backquotes (Lisp_Object file)
946 {
947 if (!NILP (Vold_style_backquotes))
948 {
949 AUTO_STRING (format, "Loading `%s': old-style backquotes detected!");
950 Fmessage (2, (Lisp_Object []) {format, file});
951 }
952 }
953
954 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
955 doc: /* Return the suffixes that `load' should try if a suffix is \
956 required.
957 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
958 (void)
959 {
960 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
961 while (CONSP (suffixes))
962 {
963 Lisp_Object exts = Vload_file_rep_suffixes;
964 suffix = XCAR (suffixes);
965 suffixes = XCDR (suffixes);
966 while (CONSP (exts))
967 {
968 ext = XCAR (exts);
969 exts = XCDR (exts);
970 lst = Fcons (concat2 (suffix, ext), lst);
971 }
972 }
973 return Fnreverse (lst);
974 }
975
976 DEFUN ("load", Fload, Sload, 1, 5, 0,
977 doc: /* Execute a file of Lisp code named FILE.
978 First try FILE with `.elc' appended, then try with `.el',
979 then try FILE unmodified (the exact suffixes in the exact order are
980 determined by `load-suffixes'). Environment variable references in
981 FILE are replaced with their values by calling `substitute-in-file-name'.
982 This function searches the directories in `load-path'.
983
984 If optional second arg NOERROR is non-nil,
985 report no error if FILE doesn't exist.
986 Print messages at start and end of loading unless
987 optional third arg NOMESSAGE is non-nil (but `force-load-messages'
988 overrides that).
989 If optional fourth arg NOSUFFIX is non-nil, don't try adding
990 suffixes `.elc' or `.el' to the specified name FILE.
991 If optional fifth arg MUST-SUFFIX is non-nil, insist on
992 the suffix `.elc' or `.el'; don't accept just FILE unless
993 it ends in one of those suffixes or includes a directory name.
994
995 If NOSUFFIX is nil, then if a file could not be found, try looking for
996 a different representation of the file by adding non-empty suffixes to
997 its name, before trying another file. Emacs uses this feature to find
998 compressed versions of files when Auto Compression mode is enabled.
999 If NOSUFFIX is non-nil, disable this feature.
1000
1001 The suffixes that this function tries out, when NOSUFFIX is nil, are
1002 given by the return value of `get-load-suffixes' and the values listed
1003 in `load-file-rep-suffixes'. If MUST-SUFFIX is non-nil, only the
1004 return value of `get-load-suffixes' is used, i.e. the file name is
1005 required to have a non-empty suffix.
1006
1007 When searching suffixes, this function normally stops at the first
1008 one that exists. If the option `load-prefer-newer' is non-nil,
1009 however, it tries all suffixes, and uses whichever file is the newest.
1010
1011 Loading a file records its definitions, and its `provide' and
1012 `require' calls, in an element of `load-history' whose
1013 car is the file name loaded. See `load-history'.
1014
1015 While the file is in the process of being loaded, the variable
1016 `load-in-progress' is non-nil and the variable `load-file-name'
1017 is bound to the file's name.
1018
1019 Return t if the file exists and loads successfully. */)
1020 (Lisp_Object file, Lisp_Object noerror, Lisp_Object nomessage,
1021 Lisp_Object nosuffix, Lisp_Object must_suffix)
1022 {
1023 FILE *stream;
1024 int fd;
1025 int fd_index;
1026 ptrdiff_t count = SPECPDL_INDEX ();
1027 struct gcpro gcpro1, gcpro2, gcpro3;
1028 Lisp_Object found, efound, hist_file_name;
1029 /* True means we printed the ".el is newer" message. */
1030 bool newer = 0;
1031 /* True means we are loading a compiled file. */
1032 bool compiled = 0;
1033 Lisp_Object handler;
1034 bool safe_p = 1;
1035 const char *fmode = "r";
1036 int version;
1037
1038 #ifdef DOS_NT
1039 fmode = "rt";
1040 #endif /* DOS_NT */
1041
1042 CHECK_STRING (file);
1043
1044 /* If file name is magic, call the handler. */
1045 /* This shouldn't be necessary any more now that `openp' handles it right.
1046 handler = Ffind_file_name_handler (file, Qload);
1047 if (!NILP (handler))
1048 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
1049
1050 /* Do this after the handler to avoid
1051 the need to gcpro noerror, nomessage and nosuffix.
1052 (Below here, we care only whether they are nil or not.)
1053 The presence of this call is the result of a historical accident:
1054 it used to be in every file-operation and when it got removed
1055 everywhere, it accidentally stayed here. Since then, enough people
1056 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
1057 that it seemed risky to remove. */
1058 if (! NILP (noerror))
1059 {
1060 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
1061 Qt, load_error_handler);
1062 if (NILP (file))
1063 return Qnil;
1064 }
1065 else
1066 file = Fsubstitute_in_file_name (file);
1067
1068 /* Avoid weird lossage with null string as arg,
1069 since it would try to load a directory as a Lisp file. */
1070 if (SCHARS (file) == 0)
1071 {
1072 fd = -1;
1073 errno = ENOENT;
1074 }
1075 else
1076 {
1077 Lisp_Object suffixes;
1078 found = Qnil;
1079 GCPRO2 (file, found);
1080
1081 if (! NILP (must_suffix))
1082 {
1083 /* Don't insist on adding a suffix if FILE already ends with one. */
1084 ptrdiff_t size = SBYTES (file);
1085 if (size > 3
1086 && !strcmp (SSDATA (file) + size - 3, ".el"))
1087 must_suffix = Qnil;
1088 else if (size > 4
1089 && !strcmp (SSDATA (file) + size - 4, ".elc"))
1090 must_suffix = Qnil;
1091 /* Don't insist on adding a suffix
1092 if the argument includes a directory name. */
1093 else if (! NILP (Ffile_name_directory (file)))
1094 must_suffix = Qnil;
1095 }
1096
1097 if (!NILP (nosuffix))
1098 suffixes = Qnil;
1099 else
1100 {
1101 suffixes = Fget_load_suffixes ();
1102 if (NILP (must_suffix))
1103 {
1104 Lisp_Object arg[2];
1105 arg[0] = suffixes;
1106 arg[1] = Vload_file_rep_suffixes;
1107 suffixes = Fappend (2, arg);
1108 }
1109 }
1110
1111 fd = openp (Vload_path, file, suffixes, &found, Qnil, load_prefer_newer);
1112 UNGCPRO;
1113 }
1114
1115 if (fd == -1)
1116 {
1117 if (NILP (noerror))
1118 report_file_error ("Cannot open load file", file);
1119 return Qnil;
1120 }
1121
1122 /* Tell startup.el whether or not we found the user's init file. */
1123 if (EQ (Qt, Vuser_init_file))
1124 Vuser_init_file = found;
1125
1126 /* If FD is -2, that means openp found a magic file. */
1127 if (fd == -2)
1128 {
1129 if (NILP (Fequal (found, file)))
1130 /* If FOUND is a different file name from FILE,
1131 find its handler even if we have already inhibited
1132 the `load' operation on FILE. */
1133 handler = Ffind_file_name_handler (found, Qt);
1134 else
1135 handler = Ffind_file_name_handler (found, Qload);
1136 if (! NILP (handler))
1137 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1138 #ifdef DOS_NT
1139 /* Tramp has to deal with semi-broken packages that prepend
1140 drive letters to remote files. For that reason, Tramp
1141 catches file operations that test for file existence, which
1142 makes openp think X:/foo.elc files are remote. However,
1143 Tramp does not catch `load' operations for such files, so we
1144 end up with a nil as the `load' handler above. If we would
1145 continue with fd = -2, we will behave wrongly, and in
1146 particular try reading a .elc file in the "rt" mode instead
1147 of "rb". See bug #9311 for the results. To work around
1148 this, we try to open the file locally, and go with that if it
1149 succeeds. */
1150 fd = emacs_open (SSDATA (ENCODE_FILE (found)), O_RDONLY, 0);
1151 if (fd == -1)
1152 fd = -2;
1153 #endif
1154 }
1155
1156 if (fd < 0)
1157 {
1158 /* Pacify older GCC with --enable-gcc-warnings. */
1159 IF_LINT (fd_index = 0);
1160 }
1161 else
1162 {
1163 fd_index = SPECPDL_INDEX ();
1164 record_unwind_protect_int (close_file_unwind, fd);
1165 }
1166
1167 /* Check if we're stuck in a recursive load cycle.
1168
1169 2000-09-21: It's not possible to just check for the file loaded
1170 being a member of Vloads_in_progress. This fails because of the
1171 way the byte compiler currently works; `provide's are not
1172 evaluated, see font-lock.el/jit-lock.el as an example. This
1173 leads to a certain amount of ``normal'' recursion.
1174
1175 Also, just loading a file recursively is not always an error in
1176 the general case; the second load may do something different. */
1177 {
1178 int load_count = 0;
1179 Lisp_Object tem;
1180 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
1181 if (!NILP (Fequal (found, XCAR (tem))) && (++load_count > 3))
1182 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
1183 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1184 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1185 }
1186
1187 /* All loads are by default dynamic, unless the file itself specifies
1188 otherwise using a file-variable in the first line. This is bound here
1189 so that it takes effect whether or not we use
1190 Vload_source_file_function. */
1191 specbind (Qlexical_binding, Qnil);
1192
1193 /* Get the name for load-history. */
1194 hist_file_name = (! NILP (Vpurify_flag)
1195 ? concat2 (Ffile_name_directory (file),
1196 Ffile_name_nondirectory (found))
1197 : found) ;
1198
1199 version = -1;
1200
1201 /* Check for the presence of old-style quotes and warn about them. */
1202 specbind (Qold_style_backquotes, Qnil);
1203 record_unwind_protect (load_warn_old_style_backquotes, file);
1204
1205 if (!memcmp (SDATA (found) + SBYTES (found) - 4, ".elc", 4)
1206 || (fd >= 0 && (version = safe_to_load_version (fd)) > 0))
1207 /* Load .elc files directly, but not when they are
1208 remote and have no handler! */
1209 {
1210 if (fd != -2)
1211 {
1212 struct stat s1, s2;
1213 int result;
1214
1215 GCPRO3 (file, found, hist_file_name);
1216
1217 if (version < 0
1218 && ! (version = safe_to_load_version (fd)))
1219 {
1220 safe_p = 0;
1221 if (!load_dangerous_libraries)
1222 error ("File `%s' was not compiled in Emacs", SDATA (found));
1223 else if (!NILP (nomessage) && !force_load_messages)
1224 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1225 }
1226
1227 compiled = 1;
1228
1229 efound = ENCODE_FILE (found);
1230
1231 #ifdef DOS_NT
1232 fmode = "rb";
1233 #endif /* DOS_NT */
1234
1235 /* openp already checked for newness, no point doing it again.
1236 FIXME would be nice to get a message when openp
1237 ignores suffix order due to load_prefer_newer. */
1238 if (!load_prefer_newer)
1239 {
1240 result = stat (SSDATA (efound), &s1);
1241 if (result == 0)
1242 {
1243 SSET (efound, SBYTES (efound) - 1, 0);
1244 result = stat (SSDATA (efound), &s2);
1245 SSET (efound, SBYTES (efound) - 1, 'c');
1246 }
1247
1248 if (result == 0
1249 && timespec_cmp (get_stat_mtime (&s1), get_stat_mtime (&s2)) < 0)
1250 {
1251 /* Make the progress messages mention that source is newer. */
1252 newer = 1;
1253
1254 /* If we won't print another message, mention this anyway. */
1255 if (!NILP (nomessage) && !force_load_messages)
1256 {
1257 Lisp_Object msg_file;
1258 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1259 message_with_string ("Source file `%s' newer than byte-compiled file",
1260 msg_file, 1);
1261 }
1262 }
1263 } /* !load_prefer_newer */
1264 UNGCPRO;
1265 }
1266 }
1267 else
1268 {
1269 /* We are loading a source file (*.el). */
1270 if (!NILP (Vload_source_file_function))
1271 {
1272 Lisp_Object val;
1273
1274 if (fd >= 0)
1275 {
1276 emacs_close (fd);
1277 clear_unwind_protect (fd_index);
1278 }
1279 val = call4 (Vload_source_file_function, found, hist_file_name,
1280 NILP (noerror) ? Qnil : Qt,
1281 (NILP (nomessage) || force_load_messages) ? Qnil : Qt);
1282 return unbind_to (count, val);
1283 }
1284 }
1285
1286 GCPRO3 (file, found, hist_file_name);
1287
1288 if (fd < 0)
1289 {
1290 /* We somehow got here with fd == -2, meaning the file is deemed
1291 to be remote. Don't even try to reopen the file locally;
1292 just force a failure. */
1293 stream = NULL;
1294 errno = EINVAL;
1295 }
1296 else
1297 {
1298 #ifdef WINDOWSNT
1299 emacs_close (fd);
1300 clear_unwind_protect (fd_index);
1301 efound = ENCODE_FILE (found);
1302 stream = emacs_fopen (SSDATA (efound), fmode);
1303 #else
1304 stream = fdopen (fd, fmode);
1305 #endif
1306 }
1307 if (! stream)
1308 report_file_error ("Opening stdio stream", file);
1309 set_unwind_protect_ptr (fd_index, fclose_unwind, stream);
1310
1311 if (! NILP (Vpurify_flag))
1312 Vpreloaded_file_list = Fcons (Fpurecopy (file), Vpreloaded_file_list);
1313
1314 if (NILP (nomessage) || force_load_messages)
1315 {
1316 if (!safe_p)
1317 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1318 file, 1);
1319 else if (!compiled)
1320 message_with_string ("Loading %s (source)...", file, 1);
1321 else if (newer)
1322 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1323 file, 1);
1324 else /* The typical case; compiled file newer than source file. */
1325 message_with_string ("Loading %s...", file, 1);
1326 }
1327
1328 specbind (Qload_file_name, found);
1329 specbind (Qinhibit_file_name_operation, Qnil);
1330 specbind (Qload_in_progress, Qt);
1331
1332 instream = stream;
1333 if (lisp_file_lexically_bound_p (Qget_file_char))
1334 Fset (Qlexical_binding, Qt);
1335
1336 if (! version || version >= 22)
1337 readevalloop (Qget_file_char, stream, hist_file_name,
1338 0, Qnil, Qnil, Qnil, Qnil);
1339 else
1340 {
1341 /* We can't handle a file which was compiled with
1342 byte-compile-dynamic by older version of Emacs. */
1343 specbind (Qload_force_doc_strings, Qt);
1344 readevalloop (Qget_emacs_mule_file_char, stream, hist_file_name,
1345 0, Qnil, Qnil, Qnil, Qnil);
1346 }
1347 unbind_to (count, Qnil);
1348
1349 /* Run any eval-after-load forms for this file. */
1350 if (!NILP (Ffboundp (Qdo_after_load_evaluation)))
1351 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1352
1353 UNGCPRO;
1354
1355 xfree (saved_doc_string);
1356 saved_doc_string = 0;
1357 saved_doc_string_size = 0;
1358
1359 xfree (prev_saved_doc_string);
1360 prev_saved_doc_string = 0;
1361 prev_saved_doc_string_size = 0;
1362
1363 if (!noninteractive && (NILP (nomessage) || force_load_messages))
1364 {
1365 if (!safe_p)
1366 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1367 file, 1);
1368 else if (!compiled)
1369 message_with_string ("Loading %s (source)...done", file, 1);
1370 else if (newer)
1371 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1372 file, 1);
1373 else /* The typical case; compiled file newer than source file. */
1374 message_with_string ("Loading %s...done", file, 1);
1375 }
1376
1377 return Qt;
1378 }
1379 \f
1380 static bool
1381 complete_filename_p (Lisp_Object pathname)
1382 {
1383 const unsigned char *s = SDATA (pathname);
1384 return (IS_DIRECTORY_SEP (s[0])
1385 || (SCHARS (pathname) > 2
1386 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])));
1387 }
1388
1389 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1390 doc: /* Search for FILENAME through PATH.
1391 Returns the file's name in absolute form, or nil if not found.
1392 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1393 file name when searching.
1394 If non-nil, PREDICATE is used instead of `file-readable-p'.
1395 PREDICATE can also be an integer to pass to the faccessat(2) function,
1396 in which case file-name-handlers are ignored.
1397 This function will normally skip directories, so if you want it to find
1398 directories, make sure the PREDICATE function returns `dir-ok' for them. */)
1399 (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
1400 {
1401 Lisp_Object file;
1402 int fd = openp (path, filename, suffixes, &file, predicate, false);
1403 if (NILP (predicate) && fd >= 0)
1404 emacs_close (fd);
1405 return file;
1406 }
1407
1408 /* Search for a file whose name is STR, looking in directories
1409 in the Lisp list PATH, and trying suffixes from SUFFIX.
1410 On success, return a file descriptor (or 1 or -2 as described below).
1411 On failure, return -1 and set errno.
1412
1413 SUFFIXES is a list of strings containing possible suffixes.
1414 The empty suffix is automatically added if the list is empty.
1415
1416 PREDICATE non-nil means don't open the files,
1417 just look for one that satisfies the predicate. In this case,
1418 return 1 on success. The predicate can be a lisp function or
1419 an integer to pass to `access' (in which case file-name-handlers
1420 are ignored).
1421
1422 If STOREPTR is nonzero, it points to a slot where the name of
1423 the file actually found should be stored as a Lisp string.
1424 nil is stored there on failure.
1425
1426 If the file we find is remote, return -2
1427 but store the found remote file name in *STOREPTR.
1428
1429 If NEWER is true, try all SUFFIXes and return the result for the
1430 newest file that exists. Does not apply to remote files,
1431 or if PREDICATE is specified. */
1432
1433 int
1434 openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
1435 Lisp_Object *storeptr, Lisp_Object predicate, bool newer)
1436 {
1437 ptrdiff_t fn_size = 100;
1438 char buf[100];
1439 char *fn = buf;
1440 bool absolute;
1441 ptrdiff_t want_length;
1442 Lisp_Object filename;
1443 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6, gcpro7;
1444 Lisp_Object string, tail, encoded_fn, save_string;
1445 ptrdiff_t max_suffix_len = 0;
1446 int last_errno = ENOENT;
1447 int save_fd = -1;
1448 USE_SAFE_ALLOCA;
1449
1450 /* The last-modified time of the newest matching file found.
1451 Initialize it to something less than all valid timestamps. */
1452 struct timespec save_mtime = make_timespec (TYPE_MINIMUM (time_t), -1);
1453
1454 CHECK_STRING (str);
1455
1456 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1457 {
1458 CHECK_STRING_CAR (tail);
1459 max_suffix_len = max (max_suffix_len,
1460 SBYTES (XCAR (tail)));
1461 }
1462
1463 string = filename = encoded_fn = save_string = Qnil;
1464 GCPRO7 (str, string, save_string, filename, path, suffixes, encoded_fn);
1465
1466 if (storeptr)
1467 *storeptr = Qnil;
1468
1469 absolute = complete_filename_p (str);
1470
1471 for (; CONSP (path); path = XCDR (path))
1472 {
1473 filename = Fexpand_file_name (str, XCAR (path));
1474 if (!complete_filename_p (filename))
1475 /* If there are non-absolute elts in PATH (eg "."). */
1476 /* Of course, this could conceivably lose if luser sets
1477 default-directory to be something non-absolute... */
1478 {
1479 filename = Fexpand_file_name (filename, BVAR (current_buffer, directory));
1480 if (!complete_filename_p (filename))
1481 /* Give up on this path element! */
1482 continue;
1483 }
1484
1485 /* Calculate maximum length of any filename made from
1486 this path element/specified file name and any possible suffix. */
1487 want_length = max_suffix_len + SBYTES (filename);
1488 if (fn_size <= want_length)
1489 {
1490 fn_size = 100 + want_length;
1491 fn = SAFE_ALLOCA (fn_size);
1492 }
1493
1494 /* Loop over suffixes. */
1495 for (tail = NILP (suffixes) ? list1 (empty_unibyte_string) : suffixes;
1496 CONSP (tail); tail = XCDR (tail))
1497 {
1498 Lisp_Object suffix = XCAR (tail);
1499 ptrdiff_t fnlen, lsuffix = SBYTES (suffix);
1500 Lisp_Object handler;
1501
1502 /* Concatenate path element/specified name with the suffix.
1503 If the directory starts with /:, remove that. */
1504 int prefixlen = ((SCHARS (filename) > 2
1505 && SREF (filename, 0) == '/'
1506 && SREF (filename, 1) == ':')
1507 ? 2 : 0);
1508 fnlen = SBYTES (filename) - prefixlen;
1509 memcpy (fn, SDATA (filename) + prefixlen, fnlen);
1510 memcpy (fn + fnlen, SDATA (suffix), lsuffix + 1);
1511 fnlen += lsuffix;
1512 /* Check that the file exists and is not a directory. */
1513 /* We used to only check for handlers on non-absolute file names:
1514 if (absolute)
1515 handler = Qnil;
1516 else
1517 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1518 It's not clear why that was the case and it breaks things like
1519 (load "/bar.el") where the file is actually "/bar.el.gz". */
1520 /* make_string has its own ideas on when to return a unibyte
1521 string and when a multibyte string, but we know better.
1522 We must have a unibyte string when dumping, since
1523 file-name encoding is shaky at best at that time, and in
1524 particular default-file-name-coding-system is reset
1525 several times during loadup. We therefore don't want to
1526 encode the file before passing it to file I/O library
1527 functions. */
1528 if (!STRING_MULTIBYTE (filename) && !STRING_MULTIBYTE (suffix))
1529 string = make_unibyte_string (fn, fnlen);
1530 else
1531 string = make_string (fn, fnlen);
1532 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1533 if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
1534 {
1535 bool exists;
1536 if (NILP (predicate))
1537 exists = !NILP (Ffile_readable_p (string));
1538 else
1539 {
1540 Lisp_Object tmp = call1 (predicate, string);
1541 if (NILP (tmp))
1542 exists = false;
1543 else if (EQ (tmp, Qdir_ok)
1544 || NILP (Ffile_directory_p (string)))
1545 exists = true;
1546 else
1547 {
1548 exists = false;
1549 last_errno = EISDIR;
1550 }
1551 }
1552
1553 if (exists)
1554 {
1555 /* We succeeded; return this descriptor and filename. */
1556 if (storeptr)
1557 *storeptr = string;
1558 SAFE_FREE ();
1559 UNGCPRO;
1560 return -2;
1561 }
1562 }
1563 else
1564 {
1565 int fd;
1566 const char *pfn;
1567 struct stat st;
1568
1569 encoded_fn = ENCODE_FILE (string);
1570 pfn = SSDATA (encoded_fn);
1571
1572 /* Check that we can access or open it. */
1573 if (NATNUMP (predicate))
1574 {
1575 fd = -1;
1576 if (INT_MAX < XFASTINT (predicate))
1577 last_errno = EINVAL;
1578 else if (faccessat (AT_FDCWD, pfn, XFASTINT (predicate),
1579 AT_EACCESS)
1580 == 0)
1581 {
1582 if (file_directory_p (pfn))
1583 last_errno = EISDIR;
1584 else
1585 fd = 1;
1586 }
1587 }
1588 else
1589 {
1590 fd = emacs_open (pfn, O_RDONLY, 0);
1591 if (fd < 0)
1592 {
1593 if (errno != ENOENT)
1594 last_errno = errno;
1595 }
1596 else
1597 {
1598 int err = (fstat (fd, &st) != 0 ? errno
1599 : S_ISDIR (st.st_mode) ? EISDIR : 0);
1600 if (err)
1601 {
1602 last_errno = err;
1603 emacs_close (fd);
1604 fd = -1;
1605 }
1606 }
1607 }
1608
1609 if (fd >= 0)
1610 {
1611 if (newer && !NATNUMP (predicate))
1612 {
1613 struct timespec mtime = get_stat_mtime (&st);
1614
1615 if (timespec_cmp (mtime, save_mtime) <= 0)
1616 emacs_close (fd);
1617 else
1618 {
1619 if (0 <= save_fd)
1620 emacs_close (save_fd);
1621 save_fd = fd;
1622 save_mtime = mtime;
1623 save_string = string;
1624 }
1625 }
1626 else
1627 {
1628 /* We succeeded; return this descriptor and filename. */
1629 if (storeptr)
1630 *storeptr = string;
1631 SAFE_FREE ();
1632 UNGCPRO;
1633 return fd;
1634 }
1635 }
1636
1637 /* No more suffixes. Return the newest. */
1638 if (0 <= save_fd && ! CONSP (XCDR (tail)))
1639 {
1640 if (storeptr)
1641 *storeptr = save_string;
1642 SAFE_FREE ();
1643 UNGCPRO;
1644 return save_fd;
1645 }
1646 }
1647 }
1648 if (absolute)
1649 break;
1650 }
1651
1652 SAFE_FREE ();
1653 UNGCPRO;
1654 errno = last_errno;
1655 return -1;
1656 }
1657
1658 \f
1659 /* Merge the list we've accumulated of globals from the current input source
1660 into the load_history variable. The details depend on whether
1661 the source has an associated file name or not.
1662
1663 FILENAME is the file name that we are loading from.
1664
1665 ENTIRE is true if loading that entire file, false if evaluating
1666 part of it. */
1667
1668 static void
1669 build_load_history (Lisp_Object filename, bool entire)
1670 {
1671 Lisp_Object tail, prev, newelt;
1672 Lisp_Object tem, tem2;
1673 bool foundit = 0;
1674
1675 tail = Vload_history;
1676 prev = Qnil;
1677
1678 while (CONSP (tail))
1679 {
1680 tem = XCAR (tail);
1681
1682 /* Find the feature's previous assoc list... */
1683 if (!NILP (Fequal (filename, Fcar (tem))))
1684 {
1685 foundit = 1;
1686
1687 /* If we're loading the entire file, remove old data. */
1688 if (entire)
1689 {
1690 if (NILP (prev))
1691 Vload_history = XCDR (tail);
1692 else
1693 Fsetcdr (prev, XCDR (tail));
1694 }
1695
1696 /* Otherwise, cons on new symbols that are not already members. */
1697 else
1698 {
1699 tem2 = Vcurrent_load_list;
1700
1701 while (CONSP (tem2))
1702 {
1703 newelt = XCAR (tem2);
1704
1705 if (NILP (Fmember (newelt, tem)))
1706 Fsetcar (tail, Fcons (XCAR (tem),
1707 Fcons (newelt, XCDR (tem))));
1708
1709 tem2 = XCDR (tem2);
1710 QUIT;
1711 }
1712 }
1713 }
1714 else
1715 prev = tail;
1716 tail = XCDR (tail);
1717 QUIT;
1718 }
1719
1720 /* If we're loading an entire file, cons the new assoc onto the
1721 front of load-history, the most-recently-loaded position. Also
1722 do this if we didn't find an existing member for the file. */
1723 if (entire || !foundit)
1724 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1725 Vload_history);
1726 }
1727
1728 static void
1729 readevalloop_1 (int old)
1730 {
1731 load_convert_to_unibyte = old;
1732 }
1733
1734 /* Signal an `end-of-file' error, if possible with file name
1735 information. */
1736
1737 static _Noreturn void
1738 end_of_file_error (void)
1739 {
1740 if (STRINGP (Vload_file_name))
1741 xsignal1 (Qend_of_file, Vload_file_name);
1742
1743 xsignal0 (Qend_of_file);
1744 }
1745
1746 static Lisp_Object
1747 readevalloop_eager_expand_eval (Lisp_Object val, Lisp_Object macroexpand)
1748 {
1749 /* If we macroexpand the toplevel form non-recursively and it ends
1750 up being a `progn' (or if it was a progn to start), treat each
1751 form in the progn as a top-level form. This way, if one form in
1752 the progn defines a macro, that macro is in effect when we expand
1753 the remaining forms. See similar code in bytecomp.el. */
1754 val = call2 (macroexpand, val, Qnil);
1755 if (EQ (CAR_SAFE (val), Qprogn))
1756 {
1757 struct gcpro gcpro1;
1758 Lisp_Object subforms = XCDR (val);
1759
1760 GCPRO1 (subforms);
1761 for (val = Qnil; CONSP (subforms); subforms = XCDR (subforms))
1762 val = readevalloop_eager_expand_eval (XCAR (subforms),
1763 macroexpand);
1764 UNGCPRO;
1765 }
1766 else
1767 val = eval_sub (call2 (macroexpand, val, Qt));
1768 return val;
1769 }
1770
1771 /* UNIBYTE specifies how to set load_convert_to_unibyte
1772 for this invocation.
1773 READFUN, if non-nil, is used instead of `read'.
1774
1775 START, END specify region to read in current buffer (from eval-region).
1776 If the input is not from a buffer, they must be nil. */
1777
1778 static void
1779 readevalloop (Lisp_Object readcharfun,
1780 FILE *stream,
1781 Lisp_Object sourcename,
1782 bool printflag,
1783 Lisp_Object unibyte, Lisp_Object readfun,
1784 Lisp_Object start, Lisp_Object end)
1785 {
1786 register int c;
1787 register Lisp_Object val;
1788 ptrdiff_t count = SPECPDL_INDEX ();
1789 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1790 struct buffer *b = 0;
1791 bool continue_reading_p;
1792 Lisp_Object lex_bound;
1793 /* True if reading an entire buffer. */
1794 bool whole_buffer = 0;
1795 /* True on the first time around. */
1796 bool first_sexp = 1;
1797 Lisp_Object macroexpand = intern ("internal-macroexpand-for-load");
1798
1799 if (NILP (Ffboundp (macroexpand))
1800 /* Don't macroexpand in .elc files, since it should have been done
1801 already. We actually don't know whether we're in a .elc file or not,
1802 so we use circumstantial evidence: .el files normally go through
1803 Vload_source_file_function -> load-with-code-conversion
1804 -> eval-buffer. */
1805 || EQ (readcharfun, Qget_file_char)
1806 || EQ (readcharfun, Qget_emacs_mule_file_char))
1807 macroexpand = Qnil;
1808
1809 if (MARKERP (readcharfun))
1810 {
1811 if (NILP (start))
1812 start = readcharfun;
1813 }
1814
1815 if (BUFFERP (readcharfun))
1816 b = XBUFFER (readcharfun);
1817 else if (MARKERP (readcharfun))
1818 b = XMARKER (readcharfun)->buffer;
1819
1820 /* We assume START is nil when input is not from a buffer. */
1821 if (! NILP (start) && !b)
1822 emacs_abort ();
1823
1824 specbind (Qstandard_input, readcharfun); /* GCPROs readcharfun. */
1825 specbind (Qcurrent_load_list, Qnil);
1826 record_unwind_protect_int (readevalloop_1, load_convert_to_unibyte);
1827 load_convert_to_unibyte = !NILP (unibyte);
1828
1829 /* If lexical binding is active (either because it was specified in
1830 the file's header, or via a buffer-local variable), create an empty
1831 lexical environment, otherwise, turn off lexical binding. */
1832 lex_bound = find_symbol_value (Qlexical_binding);
1833 specbind (Qinternal_interpreter_environment,
1834 (NILP (lex_bound) || EQ (lex_bound, Qunbound)
1835 ? Qnil : list1 (Qt)));
1836
1837 GCPRO4 (sourcename, readfun, start, end);
1838
1839 /* Try to ensure sourcename is a truename, except whilst preloading. */
1840 if (NILP (Vpurify_flag)
1841 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1842 && !NILP (Ffboundp (Qfile_truename)))
1843 sourcename = call1 (Qfile_truename, sourcename) ;
1844
1845 LOADHIST_ATTACH (sourcename);
1846
1847 continue_reading_p = 1;
1848 while (continue_reading_p)
1849 {
1850 ptrdiff_t count1 = SPECPDL_INDEX ();
1851
1852 if (b != 0 && !BUFFER_LIVE_P (b))
1853 error ("Reading from killed buffer");
1854
1855 if (!NILP (start))
1856 {
1857 /* Switch to the buffer we are reading from. */
1858 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1859 set_buffer_internal (b);
1860
1861 /* Save point in it. */
1862 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1863 /* Save ZV in it. */
1864 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1865 /* Those get unbound after we read one expression. */
1866
1867 /* Set point and ZV around stuff to be read. */
1868 Fgoto_char (start);
1869 if (!NILP (end))
1870 Fnarrow_to_region (make_number (BEGV), end);
1871
1872 /* Just for cleanliness, convert END to a marker
1873 if it is an integer. */
1874 if (INTEGERP (end))
1875 end = Fpoint_max_marker ();
1876 }
1877
1878 /* On the first cycle, we can easily test here
1879 whether we are reading the whole buffer. */
1880 if (b && first_sexp)
1881 whole_buffer = (PT == BEG && ZV == Z);
1882
1883 instream = stream;
1884 read_next:
1885 c = READCHAR;
1886 if (c == ';')
1887 {
1888 while ((c = READCHAR) != '\n' && c != -1);
1889 goto read_next;
1890 }
1891 if (c < 0)
1892 {
1893 unbind_to (count1, Qnil);
1894 break;
1895 }
1896
1897 /* Ignore whitespace here, so we can detect eof. */
1898 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
1899 || c == 0xa0) /* NBSP */
1900 goto read_next;
1901
1902 if (!NILP (Vpurify_flag) && c == '(')
1903 {
1904 val = read_list (0, readcharfun);
1905 }
1906 else
1907 {
1908 UNREAD (c);
1909 read_objects = Qnil;
1910 if (!NILP (readfun))
1911 {
1912 val = call1 (readfun, readcharfun);
1913
1914 /* If READCHARFUN has set point to ZV, we should
1915 stop reading, even if the form read sets point
1916 to a different value when evaluated. */
1917 if (BUFFERP (readcharfun))
1918 {
1919 struct buffer *buf = XBUFFER (readcharfun);
1920 if (BUF_PT (buf) == BUF_ZV (buf))
1921 continue_reading_p = 0;
1922 }
1923 }
1924 else if (! NILP (Vload_read_function))
1925 val = call1 (Vload_read_function, readcharfun);
1926 else
1927 val = read_internal_start (readcharfun, Qnil, Qnil);
1928 }
1929
1930 if (!NILP (start) && continue_reading_p)
1931 start = Fpoint_marker ();
1932
1933 /* Restore saved point and BEGV. */
1934 unbind_to (count1, Qnil);
1935
1936 /* Now eval what we just read. */
1937 if (!NILP (macroexpand))
1938 val = readevalloop_eager_expand_eval (val, macroexpand);
1939 else
1940 val = eval_sub (val);
1941
1942 if (printflag)
1943 {
1944 Vvalues = Fcons (val, Vvalues);
1945 if (EQ (Vstandard_output, Qt))
1946 Fprin1 (val, Qnil);
1947 else
1948 Fprint (val, Qnil);
1949 }
1950
1951 first_sexp = 0;
1952 }
1953
1954 build_load_history (sourcename,
1955 stream || whole_buffer);
1956
1957 UNGCPRO;
1958
1959 unbind_to (count, Qnil);
1960 }
1961
1962 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1963 doc: /* Execute the current buffer as Lisp code.
1964 When called from a Lisp program (i.e., not interactively), this
1965 function accepts up to five optional arguments:
1966 BUFFER is the buffer to evaluate (nil means use current buffer).
1967 PRINTFLAG controls printing of output:
1968 A value of nil means discard it; anything else is stream for print.
1969 FILENAME specifies the file name to use for `load-history'.
1970 UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this
1971 invocation.
1972 DO-ALLOW-PRINT, if non-nil, specifies that `print' and related
1973 functions should work normally even if PRINTFLAG is nil.
1974
1975 This function preserves the position of point. */)
1976 (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
1977 {
1978 ptrdiff_t count = SPECPDL_INDEX ();
1979 Lisp_Object tem, buf;
1980
1981 if (NILP (buffer))
1982 buf = Fcurrent_buffer ();
1983 else
1984 buf = Fget_buffer (buffer);
1985 if (NILP (buf))
1986 error ("No such buffer");
1987
1988 if (NILP (printflag) && NILP (do_allow_print))
1989 tem = Qsymbolp;
1990 else
1991 tem = printflag;
1992
1993 if (NILP (filename))
1994 filename = BVAR (XBUFFER (buf), filename);
1995
1996 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
1997 specbind (Qstandard_output, tem);
1998 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1999 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
2000 specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
2001 readevalloop (buf, 0, filename,
2002 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
2003 unbind_to (count, Qnil);
2004
2005 return Qnil;
2006 }
2007
2008 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
2009 doc: /* Execute the region as Lisp code.
2010 When called from programs, expects two arguments,
2011 giving starting and ending indices in the current buffer
2012 of the text to be executed.
2013 Programs can pass third argument PRINTFLAG which controls output:
2014 A value of nil means discard it; anything else is stream for printing it.
2015 Also the fourth argument READ-FUNCTION, if non-nil, is used
2016 instead of `read' to read each expression. It gets one argument
2017 which is the input stream for reading characters.
2018
2019 This function does not move point. */)
2020 (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
2021 {
2022 /* FIXME: Do the eval-sexp-add-defvars dance! */
2023 ptrdiff_t count = SPECPDL_INDEX ();
2024 Lisp_Object tem, cbuf;
2025
2026 cbuf = Fcurrent_buffer ();
2027
2028 if (NILP (printflag))
2029 tem = Qsymbolp;
2030 else
2031 tem = printflag;
2032 specbind (Qstandard_output, tem);
2033 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
2034
2035 /* `readevalloop' calls functions which check the type of start and end. */
2036 readevalloop (cbuf, 0, BVAR (XBUFFER (cbuf), filename),
2037 !NILP (printflag), Qnil, read_function,
2038 start, end);
2039
2040 return unbind_to (count, Qnil);
2041 }
2042
2043 \f
2044 DEFUN ("read", Fread, Sread, 0, 1, 0,
2045 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
2046 If STREAM is nil, use the value of `standard-input' (which see).
2047 STREAM or the value of `standard-input' may be:
2048 a buffer (read from point and advance it)
2049 a marker (read from where it points and advance it)
2050 a function (call it with no arguments for each character,
2051 call it with a char as argument to push a char back)
2052 a string (takes text from string, starting at the beginning)
2053 t (read text line using minibuffer and use it, or read from
2054 standard input in batch mode). */)
2055 (Lisp_Object stream)
2056 {
2057 if (NILP (stream))
2058 stream = Vstandard_input;
2059 if (EQ (stream, Qt))
2060 stream = Qread_char;
2061 if (EQ (stream, Qread_char))
2062 /* FIXME: ?! When is this used !? */
2063 return call1 (intern ("read-minibuffer"),
2064 build_string ("Lisp expression: "));
2065
2066 return read_internal_start (stream, Qnil, Qnil);
2067 }
2068
2069 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
2070 doc: /* Read one Lisp expression which is represented as text by STRING.
2071 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
2072 FINAL-STRING-INDEX is an integer giving the position of the next
2073 remaining character in STRING. START and END optionally delimit
2074 a substring of STRING from which to read; they default to 0 and
2075 (length STRING) respectively. Negative values are counted from
2076 the end of STRING. */)
2077 (Lisp_Object string, Lisp_Object start, Lisp_Object end)
2078 {
2079 Lisp_Object ret;
2080 CHECK_STRING (string);
2081 /* `read_internal_start' sets `read_from_string_index'. */
2082 ret = read_internal_start (string, start, end);
2083 return Fcons (ret, make_number (read_from_string_index));
2084 }
2085
2086 /* Function to set up the global context we need in toplevel read
2087 calls. START and END only used when STREAM is a string. */
2088 static Lisp_Object
2089 read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
2090 {
2091 Lisp_Object retval;
2092
2093 readchar_count = 0;
2094 new_backquote_flag = 0;
2095 read_objects = Qnil;
2096 if (EQ (Vread_with_symbol_positions, Qt)
2097 || EQ (Vread_with_symbol_positions, stream))
2098 Vread_symbol_positions_list = Qnil;
2099
2100 if (STRINGP (stream)
2101 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
2102 {
2103 ptrdiff_t startval, endval;
2104 Lisp_Object string;
2105
2106 if (STRINGP (stream))
2107 string = stream;
2108 else
2109 string = XCAR (stream);
2110
2111 validate_subarray (string, start, end, SCHARS (string),
2112 &startval, &endval);
2113
2114 read_from_string_index = startval;
2115 read_from_string_index_byte = string_char_to_byte (string, startval);
2116 read_from_string_limit = endval;
2117 }
2118
2119 retval = read0 (stream);
2120 if (EQ (Vread_with_symbol_positions, Qt)
2121 || EQ (Vread_with_symbol_positions, stream))
2122 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
2123 return retval;
2124 }
2125 \f
2126
2127 /* Signal Qinvalid_read_syntax error.
2128 S is error string of length N (if > 0) */
2129
2130 static _Noreturn void
2131 invalid_syntax (const char *s)
2132 {
2133 xsignal1 (Qinvalid_read_syntax, build_string (s));
2134 }
2135
2136
2137 /* Use this for recursive reads, in contexts where internal tokens
2138 are not allowed. */
2139
2140 static Lisp_Object
2141 read0 (Lisp_Object readcharfun)
2142 {
2143 register Lisp_Object val;
2144 int c;
2145
2146 val = read1 (readcharfun, &c, 0);
2147 if (!c)
2148 return val;
2149
2150 xsignal1 (Qinvalid_read_syntax,
2151 Fmake_string (make_number (1), make_number (c)));
2152 }
2153 \f
2154 static ptrdiff_t read_buffer_size;
2155 static char *read_buffer;
2156
2157 /* Read a \-escape sequence, assuming we already read the `\'.
2158 If the escape sequence forces unibyte, return eight-bit char. */
2159
2160 static int
2161 read_escape (Lisp_Object readcharfun, bool stringp)
2162 {
2163 int c = READCHAR;
2164 /* \u allows up to four hex digits, \U up to eight. Default to the
2165 behavior for \u, and change this value in the case that \U is seen. */
2166 int unicode_hex_count = 4;
2167
2168 switch (c)
2169 {
2170 case -1:
2171 end_of_file_error ();
2172
2173 case 'a':
2174 return '\007';
2175 case 'b':
2176 return '\b';
2177 case 'd':
2178 return 0177;
2179 case 'e':
2180 return 033;
2181 case 'f':
2182 return '\f';
2183 case 'n':
2184 return '\n';
2185 case 'r':
2186 return '\r';
2187 case 't':
2188 return '\t';
2189 case 'v':
2190 return '\v';
2191 case '\n':
2192 return -1;
2193 case ' ':
2194 if (stringp)
2195 return -1;
2196 return ' ';
2197
2198 case 'M':
2199 c = READCHAR;
2200 if (c != '-')
2201 error ("Invalid escape character syntax");
2202 c = READCHAR;
2203 if (c == '\\')
2204 c = read_escape (readcharfun, 0);
2205 return c | meta_modifier;
2206
2207 case 'S':
2208 c = READCHAR;
2209 if (c != '-')
2210 error ("Invalid escape character syntax");
2211 c = READCHAR;
2212 if (c == '\\')
2213 c = read_escape (readcharfun, 0);
2214 return c | shift_modifier;
2215
2216 case 'H':
2217 c = READCHAR;
2218 if (c != '-')
2219 error ("Invalid escape character syntax");
2220 c = READCHAR;
2221 if (c == '\\')
2222 c = read_escape (readcharfun, 0);
2223 return c | hyper_modifier;
2224
2225 case 'A':
2226 c = READCHAR;
2227 if (c != '-')
2228 error ("Invalid escape character syntax");
2229 c = READCHAR;
2230 if (c == '\\')
2231 c = read_escape (readcharfun, 0);
2232 return c | alt_modifier;
2233
2234 case 's':
2235 c = READCHAR;
2236 if (stringp || c != '-')
2237 {
2238 UNREAD (c);
2239 return ' ';
2240 }
2241 c = READCHAR;
2242 if (c == '\\')
2243 c = read_escape (readcharfun, 0);
2244 return c | super_modifier;
2245
2246 case 'C':
2247 c = READCHAR;
2248 if (c != '-')
2249 error ("Invalid escape character syntax");
2250 case '^':
2251 c = READCHAR;
2252 if (c == '\\')
2253 c = read_escape (readcharfun, 0);
2254 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2255 return 0177 | (c & CHAR_MODIFIER_MASK);
2256 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2257 return c | ctrl_modifier;
2258 /* ASCII control chars are made from letters (both cases),
2259 as well as the non-letters within 0100...0137. */
2260 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2261 return (c & (037 | ~0177));
2262 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2263 return (c & (037 | ~0177));
2264 else
2265 return c | ctrl_modifier;
2266
2267 case '0':
2268 case '1':
2269 case '2':
2270 case '3':
2271 case '4':
2272 case '5':
2273 case '6':
2274 case '7':
2275 /* An octal escape, as in ANSI C. */
2276 {
2277 register int i = c - '0';
2278 register int count = 0;
2279 while (++count < 3)
2280 {
2281 if ((c = READCHAR) >= '0' && c <= '7')
2282 {
2283 i *= 8;
2284 i += c - '0';
2285 }
2286 else
2287 {
2288 UNREAD (c);
2289 break;
2290 }
2291 }
2292
2293 if (i >= 0x80 && i < 0x100)
2294 i = BYTE8_TO_CHAR (i);
2295 return i;
2296 }
2297
2298 case 'x':
2299 /* A hex escape, as in ANSI C. */
2300 {
2301 unsigned int i = 0;
2302 int count = 0;
2303 while (1)
2304 {
2305 c = READCHAR;
2306 if (c >= '0' && c <= '9')
2307 {
2308 i *= 16;
2309 i += c - '0';
2310 }
2311 else if ((c >= 'a' && c <= 'f')
2312 || (c >= 'A' && c <= 'F'))
2313 {
2314 i *= 16;
2315 if (c >= 'a' && c <= 'f')
2316 i += c - 'a' + 10;
2317 else
2318 i += c - 'A' + 10;
2319 }
2320 else
2321 {
2322 UNREAD (c);
2323 break;
2324 }
2325 /* Allow hex escapes as large as ?\xfffffff, because some
2326 packages use them to denote characters with modifiers. */
2327 if ((CHAR_META | (CHAR_META - 1)) < i)
2328 error ("Hex character out of range: \\x%x...", i);
2329 count += count < 3;
2330 }
2331
2332 if (count < 3 && i >= 0x80)
2333 return BYTE8_TO_CHAR (i);
2334 return i;
2335 }
2336
2337 case 'U':
2338 /* Post-Unicode-2.0: Up to eight hex chars. */
2339 unicode_hex_count = 8;
2340 case 'u':
2341
2342 /* A Unicode escape. We only permit them in strings and characters,
2343 not arbitrarily in the source code, as in some other languages. */
2344 {
2345 unsigned int i = 0;
2346 int count = 0;
2347
2348 while (++count <= unicode_hex_count)
2349 {
2350 c = READCHAR;
2351 /* `isdigit' and `isalpha' may be locale-specific, which we don't
2352 want. */
2353 if (c >= '0' && c <= '9') i = (i << 4) + (c - '0');
2354 else if (c >= 'a' && c <= 'f') i = (i << 4) + (c - 'a') + 10;
2355 else if (c >= 'A' && c <= 'F') i = (i << 4) + (c - 'A') + 10;
2356 else
2357 error ("Non-hex digit used for Unicode escape");
2358 }
2359 if (i > 0x10FFFF)
2360 error ("Non-Unicode character: 0x%x", i);
2361 return i;
2362 }
2363
2364 default:
2365 return c;
2366 }
2367 }
2368
2369 /* Return the digit that CHARACTER stands for in the given BASE.
2370 Return -1 if CHARACTER is out of range for BASE,
2371 and -2 if CHARACTER is not valid for any supported BASE. */
2372 static int
2373 digit_to_number (int character, int base)
2374 {
2375 int digit;
2376
2377 if ('0' <= character && character <= '9')
2378 digit = character - '0';
2379 else if ('a' <= character && character <= 'z')
2380 digit = character - 'a' + 10;
2381 else if ('A' <= character && character <= 'Z')
2382 digit = character - 'A' + 10;
2383 else
2384 return -2;
2385
2386 return digit < base ? digit : -1;
2387 }
2388
2389 /* Read an integer in radix RADIX using READCHARFUN to read
2390 characters. RADIX must be in the interval [2..36]; if it isn't, a
2391 read error is signaled . Value is the integer read. Signals an
2392 error if encountering invalid read syntax or if RADIX is out of
2393 range. */
2394
2395 static Lisp_Object
2396 read_integer (Lisp_Object readcharfun, EMACS_INT radix)
2397 {
2398 /* Room for sign, leading 0, other digits, trailing null byte.
2399 Also, room for invalid syntax diagnostic. */
2400 char buf[max (1 + 1 + sizeof (uintmax_t) * CHAR_BIT + 1,
2401 sizeof "integer, radix " + INT_STRLEN_BOUND (EMACS_INT))];
2402
2403 int valid = -1; /* 1 if valid, 0 if not, -1 if incomplete. */
2404
2405 if (radix < 2 || radix > 36)
2406 valid = 0;
2407 else
2408 {
2409 char *p = buf;
2410 int c, digit;
2411
2412 c = READCHAR;
2413 if (c == '-' || c == '+')
2414 {
2415 *p++ = c;
2416 c = READCHAR;
2417 }
2418
2419 if (c == '0')
2420 {
2421 *p++ = c;
2422 valid = 1;
2423
2424 /* Ignore redundant leading zeros, so the buffer doesn't
2425 fill up with them. */
2426 do
2427 c = READCHAR;
2428 while (c == '0');
2429 }
2430
2431 while ((digit = digit_to_number (c, radix)) >= -1)
2432 {
2433 if (digit == -1)
2434 valid = 0;
2435 if (valid < 0)
2436 valid = 1;
2437
2438 if (p < buf + sizeof buf - 1)
2439 *p++ = c;
2440 else
2441 valid = 0;
2442
2443 c = READCHAR;
2444 }
2445
2446 UNREAD (c);
2447 *p = '\0';
2448 }
2449
2450 if (! valid)
2451 {
2452 sprintf (buf, "integer, radix %"pI"d", radix);
2453 invalid_syntax (buf);
2454 }
2455
2456 return string_to_number (buf, radix, 0);
2457 }
2458
2459
2460 /* If the next token is ')' or ']' or '.', we store that character
2461 in *PCH and the return value is not interesting. Else, we store
2462 zero in *PCH and we read and return one lisp object.
2463
2464 FIRST_IN_LIST is true if this is the first element of a list. */
2465
2466 static Lisp_Object
2467 read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
2468 {
2469 int c;
2470 bool uninterned_symbol = 0;
2471 bool multibyte;
2472
2473 *pch = 0;
2474
2475 retry:
2476
2477 c = READCHAR_REPORT_MULTIBYTE (&multibyte);
2478 if (c < 0)
2479 end_of_file_error ();
2480
2481 switch (c)
2482 {
2483 case '(':
2484 return read_list (0, readcharfun);
2485
2486 case '[':
2487 return read_vector (readcharfun, 0);
2488
2489 case ')':
2490 case ']':
2491 {
2492 *pch = c;
2493 return Qnil;
2494 }
2495
2496 case '#':
2497 c = READCHAR;
2498 if (c == 's')
2499 {
2500 c = READCHAR;
2501 if (c == '(')
2502 {
2503 /* Accept extended format for hashtables (extensible to
2504 other types), e.g.
2505 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2506 Lisp_Object tmp = read_list (0, readcharfun);
2507 Lisp_Object head = CAR_SAFE (tmp);
2508 Lisp_Object data = Qnil;
2509 Lisp_Object val = Qnil;
2510 /* The size is 2 * number of allowed keywords to
2511 make-hash-table. */
2512 Lisp_Object params[10];
2513 Lisp_Object ht;
2514 Lisp_Object key = Qnil;
2515 int param_count = 0;
2516
2517 if (!EQ (head, Qhash_table))
2518 error ("Invalid extended read marker at head of #s list "
2519 "(only hash-table allowed)");
2520
2521 tmp = CDR_SAFE (tmp);
2522
2523 /* This is repetitive but fast and simple. */
2524 params[param_count] = QCsize;
2525 params[param_count + 1] = Fplist_get (tmp, Qsize);
2526 if (!NILP (params[param_count + 1]))
2527 param_count += 2;
2528
2529 params[param_count] = QCtest;
2530 params[param_count + 1] = Fplist_get (tmp, Qtest);
2531 if (!NILP (params[param_count + 1]))
2532 param_count += 2;
2533
2534 params[param_count] = QCweakness;
2535 params[param_count + 1] = Fplist_get (tmp, Qweakness);
2536 if (!NILP (params[param_count + 1]))
2537 param_count += 2;
2538
2539 params[param_count] = QCrehash_size;
2540 params[param_count + 1] = Fplist_get (tmp, Qrehash_size);
2541 if (!NILP (params[param_count + 1]))
2542 param_count += 2;
2543
2544 params[param_count] = QCrehash_threshold;
2545 params[param_count + 1] = Fplist_get (tmp, Qrehash_threshold);
2546 if (!NILP (params[param_count + 1]))
2547 param_count += 2;
2548
2549 /* This is the hashtable data. */
2550 data = Fplist_get (tmp, Qdata);
2551
2552 /* Now use params to make a new hashtable and fill it. */
2553 ht = Fmake_hash_table (param_count, params);
2554
2555 while (CONSP (data))
2556 {
2557 key = XCAR (data);
2558 data = XCDR (data);
2559 if (!CONSP (data))
2560 error ("Odd number of elements in hashtable data");
2561 val = XCAR (data);
2562 data = XCDR (data);
2563 Fputhash (key, val, ht);
2564 }
2565
2566 return ht;
2567 }
2568 UNREAD (c);
2569 invalid_syntax ("#");
2570 }
2571 if (c == '^')
2572 {
2573 c = READCHAR;
2574 if (c == '[')
2575 {
2576 Lisp_Object tmp;
2577 tmp = read_vector (readcharfun, 0);
2578 if (ASIZE (tmp) < CHAR_TABLE_STANDARD_SLOTS)
2579 error ("Invalid size char-table");
2580 XSETPVECTYPE (XVECTOR (tmp), PVEC_CHAR_TABLE);
2581 return tmp;
2582 }
2583 else if (c == '^')
2584 {
2585 c = READCHAR;
2586 if (c == '[')
2587 {
2588 /* Sub char-table can't be read as a regular
2589 vector because of a two C integer fields. */
2590 Lisp_Object tbl, tmp = read_list (1, readcharfun);
2591 ptrdiff_t size = XINT (Flength (tmp));
2592 int i, depth, min_char;
2593 struct Lisp_Cons *cell;
2594
2595 if (size == 0)
2596 error ("Zero-sized sub char-table");
2597
2598 if (! RANGED_INTEGERP (1, XCAR (tmp), 3))
2599 error ("Invalid depth in sub char-table");
2600 depth = XINT (XCAR (tmp));
2601 if (chartab_size[depth] != size - 2)
2602 error ("Invalid size in sub char-table");
2603 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2604 free_cons (cell);
2605
2606 if (! RANGED_INTEGERP (0, XCAR (tmp), MAX_CHAR))
2607 error ("Invalid minimum character in sub-char-table");
2608 min_char = XINT (XCAR (tmp));
2609 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2610 free_cons (cell);
2611
2612 tbl = make_uninit_sub_char_table (depth, min_char);
2613 for (i = 0; i < size; i++)
2614 {
2615 XSUB_CHAR_TABLE (tbl)->contents[i] = XCAR (tmp);
2616 cell = XCONS (tmp), tmp = XCDR (tmp);
2617 free_cons (cell);
2618 }
2619 return tbl;
2620 }
2621 invalid_syntax ("#^^");
2622 }
2623 invalid_syntax ("#^");
2624 }
2625 if (c == '&')
2626 {
2627 Lisp_Object length;
2628 length = read1 (readcharfun, pch, first_in_list);
2629 c = READCHAR;
2630 if (c == '"')
2631 {
2632 Lisp_Object tmp, val;
2633 EMACS_INT size_in_chars = bool_vector_bytes (XFASTINT (length));
2634 unsigned char *data;
2635
2636 UNREAD (c);
2637 tmp = read1 (readcharfun, pch, first_in_list);
2638 if (STRING_MULTIBYTE (tmp)
2639 || (size_in_chars != SCHARS (tmp)
2640 /* We used to print 1 char too many
2641 when the number of bits was a multiple of 8.
2642 Accept such input in case it came from an old
2643 version. */
2644 && ! (XFASTINT (length)
2645 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2646 invalid_syntax ("#&...");
2647
2648 val = make_uninit_bool_vector (XFASTINT (length));
2649 data = bool_vector_uchar_data (val);
2650 memcpy (data, SDATA (tmp), size_in_chars);
2651 /* Clear the extraneous bits in the last byte. */
2652 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2653 data[size_in_chars - 1]
2654 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2655 return val;
2656 }
2657 invalid_syntax ("#&...");
2658 }
2659 if (c == '[')
2660 {
2661 /* Accept compiled functions at read-time so that we don't have to
2662 build them using function calls. */
2663 Lisp_Object tmp;
2664 struct Lisp_Vector *vec;
2665 tmp = read_vector (readcharfun, 1);
2666 vec = XVECTOR (tmp);
2667 if (vec->header.size == 0)
2668 invalid_syntax ("Empty byte-code object");
2669 make_byte_code (vec);
2670 return tmp;
2671 }
2672 if (c == '(')
2673 {
2674 Lisp_Object tmp;
2675 struct gcpro gcpro1;
2676 int ch;
2677
2678 /* Read the string itself. */
2679 tmp = read1 (readcharfun, &ch, 0);
2680 if (ch != 0 || !STRINGP (tmp))
2681 invalid_syntax ("#");
2682 GCPRO1 (tmp);
2683 /* Read the intervals and their properties. */
2684 while (1)
2685 {
2686 Lisp_Object beg, end, plist;
2687
2688 beg = read1 (readcharfun, &ch, 0);
2689 end = plist = Qnil;
2690 if (ch == ')')
2691 break;
2692 if (ch == 0)
2693 end = read1 (readcharfun, &ch, 0);
2694 if (ch == 0)
2695 plist = read1 (readcharfun, &ch, 0);
2696 if (ch)
2697 invalid_syntax ("Invalid string property list");
2698 Fset_text_properties (beg, end, plist, tmp);
2699 }
2700 UNGCPRO;
2701 return tmp;
2702 }
2703
2704 /* #@NUMBER is used to skip NUMBER following bytes.
2705 That's used in .elc files to skip over doc strings
2706 and function definitions. */
2707 if (c == '@')
2708 {
2709 enum { extra = 100 };
2710 ptrdiff_t i, nskip = 0, digits = 0;
2711
2712 /* Read a decimal integer. */
2713 while ((c = READCHAR) >= 0
2714 && c >= '0' && c <= '9')
2715 {
2716 if ((STRING_BYTES_BOUND - extra) / 10 <= nskip)
2717 string_overflow ();
2718 digits++;
2719 nskip *= 10;
2720 nskip += c - '0';
2721 if (digits == 2 && nskip == 0)
2722 { /* We've just seen #@00, which means "skip to end". */
2723 skip_dyn_eof (readcharfun);
2724 return Qnil;
2725 }
2726 }
2727 if (nskip > 0)
2728 /* We can't use UNREAD here, because in the code below we side-step
2729 READCHAR. Instead, assume the first char after #@NNN occupies
2730 a single byte, which is the case normally since it's just
2731 a space. */
2732 nskip--;
2733 else
2734 UNREAD (c);
2735
2736 if (load_force_doc_strings
2737 && (FROM_FILE_P (readcharfun)))
2738 {
2739 /* If we are supposed to force doc strings into core right now,
2740 record the last string that we skipped,
2741 and record where in the file it comes from. */
2742
2743 /* But first exchange saved_doc_string
2744 with prev_saved_doc_string, so we save two strings. */
2745 {
2746 char *temp = saved_doc_string;
2747 ptrdiff_t temp_size = saved_doc_string_size;
2748 file_offset temp_pos = saved_doc_string_position;
2749 ptrdiff_t temp_len = saved_doc_string_length;
2750
2751 saved_doc_string = prev_saved_doc_string;
2752 saved_doc_string_size = prev_saved_doc_string_size;
2753 saved_doc_string_position = prev_saved_doc_string_position;
2754 saved_doc_string_length = prev_saved_doc_string_length;
2755
2756 prev_saved_doc_string = temp;
2757 prev_saved_doc_string_size = temp_size;
2758 prev_saved_doc_string_position = temp_pos;
2759 prev_saved_doc_string_length = temp_len;
2760 }
2761
2762 if (saved_doc_string_size == 0)
2763 {
2764 saved_doc_string = xmalloc (nskip + extra);
2765 saved_doc_string_size = nskip + extra;
2766 }
2767 if (nskip > saved_doc_string_size)
2768 {
2769 saved_doc_string = xrealloc (saved_doc_string, nskip + extra);
2770 saved_doc_string_size = nskip + extra;
2771 }
2772
2773 saved_doc_string_position = file_tell (instream);
2774
2775 /* Copy that many characters into saved_doc_string. */
2776 block_input ();
2777 for (i = 0; i < nskip && c >= 0; i++)
2778 saved_doc_string[i] = c = getc (instream);
2779 unblock_input ();
2780
2781 saved_doc_string_length = i;
2782 }
2783 else
2784 /* Skip that many bytes. */
2785 skip_dyn_bytes (readcharfun, nskip);
2786
2787 goto retry;
2788 }
2789 if (c == '!')
2790 {
2791 /* #! appears at the beginning of an executable file.
2792 Skip the first line. */
2793 while (c != '\n' && c >= 0)
2794 c = READCHAR;
2795 goto retry;
2796 }
2797 if (c == '$')
2798 return Vload_file_name;
2799 if (c == '\'')
2800 return list2 (Qfunction, read0 (readcharfun));
2801 /* #:foo is the uninterned symbol named foo. */
2802 if (c == ':')
2803 {
2804 uninterned_symbol = 1;
2805 c = READCHAR;
2806 if (!(c > 040
2807 && c != 0xa0 /* NBSP */
2808 && (c >= 0200
2809 || strchr ("\"';()[]#`,", c) == NULL)))
2810 {
2811 /* No symbol character follows, this is the empty
2812 symbol. */
2813 UNREAD (c);
2814 return Fmake_symbol (empty_unibyte_string);
2815 }
2816 goto read_symbol;
2817 }
2818 /* ## is the empty symbol. */
2819 if (c == '#')
2820 return Fintern (empty_unibyte_string, Qnil);
2821 /* Reader forms that can reuse previously read objects. */
2822 if (c >= '0' && c <= '9')
2823 {
2824 EMACS_INT n = 0;
2825 Lisp_Object tem;
2826
2827 /* Read a non-negative integer. */
2828 while (c >= '0' && c <= '9')
2829 {
2830 if (MOST_POSITIVE_FIXNUM / 10 < n
2831 || MOST_POSITIVE_FIXNUM < n * 10 + c - '0')
2832 n = MOST_POSITIVE_FIXNUM + 1;
2833 else
2834 n = n * 10 + c - '0';
2835 c = READCHAR;
2836 }
2837
2838 if (n <= MOST_POSITIVE_FIXNUM)
2839 {
2840 if (c == 'r' || c == 'R')
2841 return read_integer (readcharfun, n);
2842
2843 if (! NILP (Vread_circle))
2844 {
2845 /* #n=object returns object, but associates it with
2846 n for #n#. */
2847 if (c == '=')
2848 {
2849 /* Make a placeholder for #n# to use temporarily. */
2850 AUTO_CONS (placeholder, Qnil, Qnil);
2851 Lisp_Object cell = Fcons (make_number (n), placeholder);
2852 read_objects = Fcons (cell, read_objects);
2853
2854 /* Read the object itself. */
2855 tem = read0 (readcharfun);
2856
2857 /* Now put it everywhere the placeholder was... */
2858 substitute_object_in_subtree (tem, placeholder);
2859
2860 /* ...and #n# will use the real value from now on. */
2861 Fsetcdr (cell, tem);
2862
2863 return tem;
2864 }
2865
2866 /* #n# returns a previously read object. */
2867 if (c == '#')
2868 {
2869 tem = Fassq (make_number (n), read_objects);
2870 if (CONSP (tem))
2871 return XCDR (tem);
2872 }
2873 }
2874 }
2875 /* Fall through to error message. */
2876 }
2877 else if (c == 'x' || c == 'X')
2878 return read_integer (readcharfun, 16);
2879 else if (c == 'o' || c == 'O')
2880 return read_integer (readcharfun, 8);
2881 else if (c == 'b' || c == 'B')
2882 return read_integer (readcharfun, 2);
2883
2884 UNREAD (c);
2885 invalid_syntax ("#");
2886
2887 case ';':
2888 while ((c = READCHAR) >= 0 && c != '\n');
2889 goto retry;
2890
2891 case '\'':
2892 return list2 (Qquote, read0 (readcharfun));
2893
2894 case '`':
2895 {
2896 int next_char = READCHAR;
2897 UNREAD (next_char);
2898 /* Transition from old-style to new-style:
2899 If we see "(`" it used to mean old-style, which usually works
2900 fine because ` should almost never appear in such a position
2901 for new-style. But occasionally we need "(`" to mean new
2902 style, so we try to distinguish the two by the fact that we
2903 can either write "( `foo" or "(` foo", where the first
2904 intends to use new-style whereas the second intends to use
2905 old-style. For Emacs-25, we should completely remove this
2906 first_in_list exception (old-style can still be obtained via
2907 "(\`" anyway). */
2908 if (!new_backquote_flag && first_in_list && next_char == ' ')
2909 {
2910 Vold_style_backquotes = Qt;
2911 goto default_label;
2912 }
2913 else
2914 {
2915 Lisp_Object value;
2916 bool saved_new_backquote_flag = new_backquote_flag;
2917
2918 new_backquote_flag = 1;
2919 value = read0 (readcharfun);
2920 new_backquote_flag = saved_new_backquote_flag;
2921
2922 return list2 (Qbackquote, value);
2923 }
2924 }
2925 case ',':
2926 {
2927 int next_char = READCHAR;
2928 UNREAD (next_char);
2929 /* Transition from old-style to new-style:
2930 It used to be impossible to have a new-style , other than within
2931 a new-style `. This is sufficient when ` and , are used in the
2932 normal way, but ` and , can also appear in args to macros that
2933 will not interpret them in the usual way, in which case , may be
2934 used without any ` anywhere near.
2935 So we now use the same heuristic as for backquote: old-style
2936 unquotes are only recognized when first on a list, and when
2937 followed by a space.
2938 Because it's more difficult to peek 2 chars ahead, a new-style
2939 ,@ can still not be used outside of a `, unless it's in the middle
2940 of a list. */
2941 if (new_backquote_flag
2942 || !first_in_list
2943 || (next_char != ' ' && next_char != '@'))
2944 {
2945 Lisp_Object comma_type = Qnil;
2946 Lisp_Object value;
2947 int ch = READCHAR;
2948
2949 if (ch == '@')
2950 comma_type = Qcomma_at;
2951 else if (ch == '.')
2952 comma_type = Qcomma_dot;
2953 else
2954 {
2955 if (ch >= 0) UNREAD (ch);
2956 comma_type = Qcomma;
2957 }
2958
2959 value = read0 (readcharfun);
2960 return list2 (comma_type, value);
2961 }
2962 else
2963 {
2964 Vold_style_backquotes = Qt;
2965 goto default_label;
2966 }
2967 }
2968 case '?':
2969 {
2970 int modifiers;
2971 int next_char;
2972 bool ok;
2973
2974 c = READCHAR;
2975 if (c < 0)
2976 end_of_file_error ();
2977
2978 /* Accept `single space' syntax like (list ? x) where the
2979 whitespace character is SPC or TAB.
2980 Other literal whitespace like NL, CR, and FF are not accepted,
2981 as there are well-established escape sequences for these. */
2982 if (c == ' ' || c == '\t')
2983 return make_number (c);
2984
2985 if (c == '\\')
2986 c = read_escape (readcharfun, 0);
2987 modifiers = c & CHAR_MODIFIER_MASK;
2988 c &= ~CHAR_MODIFIER_MASK;
2989 if (CHAR_BYTE8_P (c))
2990 c = CHAR_TO_BYTE8 (c);
2991 c |= modifiers;
2992
2993 next_char = READCHAR;
2994 ok = (next_char <= 040
2995 || (next_char < 0200
2996 && strchr ("\"';()[]#?`,.", next_char) != NULL));
2997 UNREAD (next_char);
2998 if (ok)
2999 return make_number (c);
3000
3001 invalid_syntax ("?");
3002 }
3003
3004 case '"':
3005 {
3006 char *p = read_buffer;
3007 char *end = read_buffer + read_buffer_size;
3008 int ch;
3009 /* True if we saw an escape sequence specifying
3010 a multibyte character. */
3011 bool force_multibyte = 0;
3012 /* True if we saw an escape sequence specifying
3013 a single-byte character. */
3014 bool force_singlebyte = 0;
3015 bool cancel = 0;
3016 ptrdiff_t nchars = 0;
3017
3018 while ((ch = READCHAR) >= 0
3019 && ch != '\"')
3020 {
3021 if (end - p < MAX_MULTIBYTE_LENGTH)
3022 {
3023 ptrdiff_t offset = p - read_buffer;
3024 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
3025 memory_full (SIZE_MAX);
3026 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
3027 read_buffer_size *= 2;
3028 p = read_buffer + offset;
3029 end = read_buffer + read_buffer_size;
3030 }
3031
3032 if (ch == '\\')
3033 {
3034 int modifiers;
3035
3036 ch = read_escape (readcharfun, 1);
3037
3038 /* CH is -1 if \ newline has just been seen. */
3039 if (ch == -1)
3040 {
3041 if (p == read_buffer)
3042 cancel = 1;
3043 continue;
3044 }
3045
3046 modifiers = ch & CHAR_MODIFIER_MASK;
3047 ch = ch & ~CHAR_MODIFIER_MASK;
3048
3049 if (CHAR_BYTE8_P (ch))
3050 force_singlebyte = 1;
3051 else if (! ASCII_CHAR_P (ch))
3052 force_multibyte = 1;
3053 else /* I.e. ASCII_CHAR_P (ch). */
3054 {
3055 /* Allow `\C- ' and `\C-?'. */
3056 if (modifiers == CHAR_CTL)
3057 {
3058 if (ch == ' ')
3059 ch = 0, modifiers = 0;
3060 else if (ch == '?')
3061 ch = 127, modifiers = 0;
3062 }
3063 if (modifiers & CHAR_SHIFT)
3064 {
3065 /* Shift modifier is valid only with [A-Za-z]. */
3066 if (ch >= 'A' && ch <= 'Z')
3067 modifiers &= ~CHAR_SHIFT;
3068 else if (ch >= 'a' && ch <= 'z')
3069 ch -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
3070 }
3071
3072 if (modifiers & CHAR_META)
3073 {
3074 /* Move the meta bit to the right place for a
3075 string. */
3076 modifiers &= ~CHAR_META;
3077 ch = BYTE8_TO_CHAR (ch | 0x80);
3078 force_singlebyte = 1;
3079 }
3080 }
3081
3082 /* Any modifiers remaining are invalid. */
3083 if (modifiers)
3084 error ("Invalid modifier in string");
3085 p += CHAR_STRING (ch, (unsigned char *) p);
3086 }
3087 else
3088 {
3089 p += CHAR_STRING (ch, (unsigned char *) p);
3090 if (CHAR_BYTE8_P (ch))
3091 force_singlebyte = 1;
3092 else if (! ASCII_CHAR_P (ch))
3093 force_multibyte = 1;
3094 }
3095 nchars++;
3096 }
3097
3098 if (ch < 0)
3099 end_of_file_error ();
3100
3101 /* If purifying, and string starts with \ newline,
3102 return zero instead. This is for doc strings
3103 that we are really going to find in etc/DOC.nn.nn. */
3104 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
3105 return make_number (0);
3106
3107 if (! force_multibyte && force_singlebyte)
3108 {
3109 /* READ_BUFFER contains raw 8-bit bytes and no multibyte
3110 forms. Convert it to unibyte. */
3111 nchars = str_as_unibyte ((unsigned char *) read_buffer,
3112 p - read_buffer);
3113 p = read_buffer + nchars;
3114 }
3115
3116 return make_specified_string (read_buffer, nchars, p - read_buffer,
3117 (force_multibyte
3118 || (p - read_buffer != nchars)));
3119 }
3120
3121 case '.':
3122 {
3123 int next_char = READCHAR;
3124 UNREAD (next_char);
3125
3126 if (next_char <= 040
3127 || (next_char < 0200
3128 && strchr ("\"';([#?`,", next_char) != NULL))
3129 {
3130 *pch = c;
3131 return Qnil;
3132 }
3133
3134 /* Otherwise, we fall through! Note that the atom-reading loop
3135 below will now loop at least once, assuring that we will not
3136 try to UNREAD two characters in a row. */
3137 }
3138 default:
3139 default_label:
3140 if (c <= 040) goto retry;
3141 if (c == 0xa0) /* NBSP */
3142 goto retry;
3143
3144 read_symbol:
3145 {
3146 char *p = read_buffer;
3147 bool quoted = 0;
3148 EMACS_INT start_position = readchar_count - 1;
3149
3150 {
3151 char *end = read_buffer + read_buffer_size;
3152
3153 do
3154 {
3155 if (end - p < MAX_MULTIBYTE_LENGTH)
3156 {
3157 ptrdiff_t offset = p - read_buffer;
3158 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
3159 memory_full (SIZE_MAX);
3160 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
3161 read_buffer_size *= 2;
3162 p = read_buffer + offset;
3163 end = read_buffer + read_buffer_size;
3164 }
3165
3166 if (c == '\\')
3167 {
3168 c = READCHAR;
3169 if (c == -1)
3170 end_of_file_error ();
3171 quoted = 1;
3172 }
3173
3174 if (multibyte)
3175 p += CHAR_STRING (c, (unsigned char *) p);
3176 else
3177 *p++ = c;
3178 c = READCHAR;
3179 }
3180 while (c > 040
3181 && c != 0xa0 /* NBSP */
3182 && (c >= 0200
3183 || strchr ("\"';()[]#`,", c) == NULL));
3184
3185 if (p == end)
3186 {
3187 ptrdiff_t offset = p - read_buffer;
3188 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size)
3189 memory_full (SIZE_MAX);
3190 read_buffer = xrealloc (read_buffer, read_buffer_size * 2);
3191 read_buffer_size *= 2;
3192 p = read_buffer + offset;
3193 end = read_buffer + read_buffer_size;
3194 }
3195 *p = 0;
3196 UNREAD (c);
3197 }
3198
3199 if (!quoted && !uninterned_symbol)
3200 {
3201 Lisp_Object result = string_to_number (read_buffer, 10, 0);
3202 if (! NILP (result))
3203 return result;
3204 }
3205 {
3206 Lisp_Object name, result;
3207 ptrdiff_t nbytes = p - read_buffer;
3208 ptrdiff_t nchars
3209 = (multibyte
3210 ? multibyte_chars_in_text ((unsigned char *) read_buffer,
3211 nbytes)
3212 : nbytes);
3213
3214 name = ((uninterned_symbol && ! NILP (Vpurify_flag)
3215 ? make_pure_string : make_specified_string)
3216 (read_buffer, nchars, nbytes, multibyte));
3217 result = (uninterned_symbol ? Fmake_symbol (name)
3218 : Fintern (name, Qnil));
3219
3220 if (EQ (Vread_with_symbol_positions, Qt)
3221 || EQ (Vread_with_symbol_positions, readcharfun))
3222 Vread_symbol_positions_list
3223 = Fcons (Fcons (result, make_number (start_position)),
3224 Vread_symbol_positions_list);
3225 return result;
3226 }
3227 }
3228 }
3229 }
3230 \f
3231
3232 /* List of nodes we've seen during substitute_object_in_subtree. */
3233 static Lisp_Object seen_list;
3234
3235 static void
3236 substitute_object_in_subtree (Lisp_Object object, Lisp_Object placeholder)
3237 {
3238 Lisp_Object check_object;
3239
3240 /* We haven't seen any objects when we start. */
3241 seen_list = Qnil;
3242
3243 /* Make all the substitutions. */
3244 check_object
3245 = substitute_object_recurse (object, placeholder, object);
3246
3247 /* Clear seen_list because we're done with it. */
3248 seen_list = Qnil;
3249
3250 /* The returned object here is expected to always eq the
3251 original. */
3252 if (!EQ (check_object, object))
3253 error ("Unexpected mutation error in reader");
3254 }
3255
3256 /* Feval doesn't get called from here, so no gc protection is needed. */
3257 #define SUBSTITUTE(get_val, set_val) \
3258 do { \
3259 Lisp_Object old_value = get_val; \
3260 Lisp_Object true_value \
3261 = substitute_object_recurse (object, placeholder, \
3262 old_value); \
3263 \
3264 if (!EQ (old_value, true_value)) \
3265 { \
3266 set_val; \
3267 } \
3268 } while (0)
3269
3270 static Lisp_Object
3271 substitute_object_recurse (Lisp_Object object, Lisp_Object placeholder, Lisp_Object subtree)
3272 {
3273 /* If we find the placeholder, return the target object. */
3274 if (EQ (placeholder, subtree))
3275 return object;
3276
3277 /* If we've been to this node before, don't explore it again. */
3278 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
3279 return subtree;
3280
3281 /* If this node can be the entry point to a cycle, remember that
3282 we've seen it. It can only be such an entry point if it was made
3283 by #n=, which means that we can find it as a value in
3284 read_objects. */
3285 if (!EQ (Qnil, Frassq (subtree, read_objects)))
3286 seen_list = Fcons (subtree, seen_list);
3287
3288 /* Recurse according to subtree's type.
3289 Every branch must return a Lisp_Object. */
3290 switch (XTYPE (subtree))
3291 {
3292 case Lisp_Vectorlike:
3293 {
3294 ptrdiff_t i, length = 0;
3295 if (BOOL_VECTOR_P (subtree))
3296 return subtree; /* No sub-objects anyway. */
3297 else if (CHAR_TABLE_P (subtree) || SUB_CHAR_TABLE_P (subtree)
3298 || COMPILEDP (subtree) || HASH_TABLE_P (subtree))
3299 length = ASIZE (subtree) & PSEUDOVECTOR_SIZE_MASK;
3300 else if (VECTORP (subtree))
3301 length = ASIZE (subtree);
3302 else
3303 /* An unknown pseudovector may contain non-Lisp fields, so we
3304 can't just blindly traverse all its fields. We used to call
3305 `Flength' which signaled `sequencep', so I just preserved this
3306 behavior. */
3307 wrong_type_argument (Qsequencep, subtree);
3308
3309 for (i = 0; i < length; i++)
3310 SUBSTITUTE (AREF (subtree, i),
3311 ASET (subtree, i, true_value));
3312 return subtree;
3313 }
3314
3315 case Lisp_Cons:
3316 {
3317 SUBSTITUTE (XCAR (subtree),
3318 XSETCAR (subtree, true_value));
3319 SUBSTITUTE (XCDR (subtree),
3320 XSETCDR (subtree, true_value));
3321 return subtree;
3322 }
3323
3324 case Lisp_String:
3325 {
3326 /* Check for text properties in each interval.
3327 substitute_in_interval contains part of the logic. */
3328
3329 INTERVAL root_interval = string_intervals (subtree);
3330 AUTO_CONS (arg, object, placeholder);
3331
3332 traverse_intervals_noorder (root_interval,
3333 &substitute_in_interval, arg);
3334
3335 return subtree;
3336 }
3337
3338 /* Other types don't recurse any further. */
3339 default:
3340 return subtree;
3341 }
3342 }
3343
3344 /* Helper function for substitute_object_recurse. */
3345 static void
3346 substitute_in_interval (INTERVAL interval, Lisp_Object arg)
3347 {
3348 Lisp_Object object = Fcar (arg);
3349 Lisp_Object placeholder = Fcdr (arg);
3350
3351 SUBSTITUTE (interval->plist, set_interval_plist (interval, true_value));
3352 }
3353
3354 \f
3355 #define LEAD_INT 1
3356 #define DOT_CHAR 2
3357 #define TRAIL_INT 4
3358 #define E_EXP 16
3359
3360
3361 /* Convert STRING to a number, assuming base BASE. Return a fixnum if CP has
3362 integer syntax and fits in a fixnum, else return the nearest float if CP has
3363 either floating point or integer syntax and BASE is 10, else return nil. If
3364 IGNORE_TRAILING, consider just the longest prefix of CP that has
3365 valid floating point syntax. Signal an overflow if BASE is not 10 and the
3366 number has integer syntax but does not fit. */
3367
3368 Lisp_Object
3369 string_to_number (char const *string, int base, bool ignore_trailing)
3370 {
3371 int state;
3372 char const *cp = string;
3373 int leading_digit;
3374 bool float_syntax = 0;
3375 double value = 0;
3376
3377 /* Compute NaN and infinities using a variable, to cope with compilers that
3378 think they are smarter than we are. */
3379 double zero = 0;
3380
3381 /* Negate the value ourselves. This treats 0, NaNs, and infinity properly on
3382 IEEE floating point hosts, and works around a formerly-common bug where
3383 atof ("-0.0") drops the sign. */
3384 bool negative = *cp == '-';
3385
3386 bool signedp = negative || *cp == '+';
3387 cp += signedp;
3388
3389 state = 0;
3390
3391 leading_digit = digit_to_number (*cp, base);
3392 if (leading_digit >= 0)
3393 {
3394 state |= LEAD_INT;
3395 do
3396 ++cp;
3397 while (digit_to_number (*cp, base) >= 0);
3398 }
3399 if (*cp == '.')
3400 {
3401 state |= DOT_CHAR;
3402 cp++;
3403 }
3404
3405 if (base == 10)
3406 {
3407 if ('0' <= *cp && *cp <= '9')
3408 {
3409 state |= TRAIL_INT;
3410 do
3411 cp++;
3412 while ('0' <= *cp && *cp <= '9');
3413 }
3414 if (*cp == 'e' || *cp == 'E')
3415 {
3416 char const *ecp = cp;
3417 cp++;
3418 if (*cp == '+' || *cp == '-')
3419 cp++;
3420 if ('0' <= *cp && *cp <= '9')
3421 {
3422 state |= E_EXP;
3423 do
3424 cp++;
3425 while ('0' <= *cp && *cp <= '9');
3426 }
3427 else if (cp[-1] == '+'
3428 && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3429 {
3430 state |= E_EXP;
3431 cp += 3;
3432 value = 1.0 / zero;
3433 }
3434 else if (cp[-1] == '+'
3435 && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3436 {
3437 state |= E_EXP;
3438 cp += 3;
3439 value = zero / zero;
3440
3441 /* If that made a "negative" NaN, negate it. */
3442 {
3443 int i;
3444 union { double d; char c[sizeof (double)]; }
3445 u_data, u_minus_zero;
3446 u_data.d = value;
3447 u_minus_zero.d = -0.0;
3448 for (i = 0; i < sizeof (double); i++)
3449 if (u_data.c[i] & u_minus_zero.c[i])
3450 {
3451 value = -value;
3452 break;
3453 }
3454 }
3455 /* Now VALUE is a positive NaN. */
3456 }
3457 else
3458 cp = ecp;
3459 }
3460
3461 float_syntax = ((state & (DOT_CHAR|TRAIL_INT)) == (DOT_CHAR|TRAIL_INT)
3462 || state == (LEAD_INT|E_EXP));
3463 }
3464
3465 /* Return nil if the number uses invalid syntax. If IGNORE_TRAILING, accept
3466 any prefix that matches. Otherwise, the entire string must match. */
3467 if (! (ignore_trailing
3468 ? ((state & LEAD_INT) != 0 || float_syntax)
3469 : (!*cp && ((state & ~DOT_CHAR) == LEAD_INT || float_syntax))))
3470 return Qnil;
3471
3472 /* If the number uses integer and not float syntax, and is in C-language
3473 range, use its value, preferably as a fixnum. */
3474 if (leading_digit >= 0 && ! float_syntax)
3475 {
3476 uintmax_t n;
3477
3478 /* Fast special case for single-digit integers. This also avoids a
3479 glitch when BASE is 16 and IGNORE_TRAILING, because in that
3480 case some versions of strtoumax accept numbers like "0x1" that Emacs
3481 does not allow. */
3482 if (digit_to_number (string[signedp + 1], base) < 0)
3483 return make_number (negative ? -leading_digit : leading_digit);
3484
3485 errno = 0;
3486 n = strtoumax (string + signedp, NULL, base);
3487 if (errno == ERANGE)
3488 {
3489 /* Unfortunately there's no simple and accurate way to convert
3490 non-base-10 numbers that are out of C-language range. */
3491 if (base != 10)
3492 xsignal1 (Qoverflow_error, build_string (string));
3493 }
3494 else if (n <= (negative ? -MOST_NEGATIVE_FIXNUM : MOST_POSITIVE_FIXNUM))
3495 {
3496 EMACS_INT signed_n = n;
3497 return make_number (negative ? -signed_n : signed_n);
3498 }
3499 else
3500 value = n;
3501 }
3502
3503 /* Either the number uses float syntax, or it does not fit into a fixnum.
3504 Convert it from string to floating point, unless the value is already
3505 known because it is an infinity, a NAN, or its absolute value fits in
3506 uintmax_t. */
3507 if (! value)
3508 value = atof (string + signedp);
3509
3510 return make_float (negative ? -value : value);
3511 }
3512
3513 \f
3514 static Lisp_Object
3515 read_vector (Lisp_Object readcharfun, bool bytecodeflag)
3516 {
3517 ptrdiff_t i, size;
3518 Lisp_Object *ptr;
3519 Lisp_Object tem, item, vector;
3520 struct Lisp_Cons *otem;
3521 Lisp_Object len;
3522
3523 tem = read_list (1, readcharfun);
3524 len = Flength (tem);
3525 vector = Fmake_vector (len, Qnil);
3526
3527 size = ASIZE (vector);
3528 ptr = XVECTOR (vector)->contents;
3529 for (i = 0; i < size; i++)
3530 {
3531 item = Fcar (tem);
3532 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3533 bytecode object, the docstring containing the bytecode and
3534 constants values must be treated as unibyte and passed to
3535 Fread, to get the actual bytecode string and constants vector. */
3536 if (bytecodeflag && load_force_doc_strings)
3537 {
3538 if (i == COMPILED_BYTECODE)
3539 {
3540 if (!STRINGP (item))
3541 error ("Invalid byte code");
3542
3543 /* Delay handling the bytecode slot until we know whether
3544 it is lazily-loaded (we can tell by whether the
3545 constants slot is nil). */
3546 ASET (vector, COMPILED_CONSTANTS, item);
3547 item = Qnil;
3548 }
3549 else if (i == COMPILED_CONSTANTS)
3550 {
3551 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3552
3553 if (NILP (item))
3554 {
3555 /* Coerce string to unibyte (like string-as-unibyte,
3556 but without generating extra garbage and
3557 guaranteeing no change in the contents). */
3558 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3559 STRING_SET_UNIBYTE (bytestr);
3560
3561 item = Fread (Fcons (bytestr, readcharfun));
3562 if (!CONSP (item))
3563 error ("Invalid byte code");
3564
3565 otem = XCONS (item);
3566 bytestr = XCAR (item);
3567 item = XCDR (item);
3568 free_cons (otem);
3569 }
3570
3571 /* Now handle the bytecode slot. */
3572 ASET (vector, COMPILED_BYTECODE, bytestr);
3573 }
3574 else if (i == COMPILED_DOC_STRING
3575 && STRINGP (item)
3576 && ! STRING_MULTIBYTE (item))
3577 {
3578 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3579 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3580 else
3581 item = Fstring_as_multibyte (item);
3582 }
3583 }
3584 ASET (vector, i, item);
3585 otem = XCONS (tem);
3586 tem = Fcdr (tem);
3587 free_cons (otem);
3588 }
3589 return vector;
3590 }
3591
3592 /* FLAG means check for ']' to terminate rather than ')' and '.'. */
3593
3594 static Lisp_Object
3595 read_list (bool flag, Lisp_Object readcharfun)
3596 {
3597 Lisp_Object val, tail;
3598 Lisp_Object elt, tem;
3599 struct gcpro gcpro1, gcpro2;
3600 /* 0 is the normal case.
3601 1 means this list is a doc reference; replace it with the number 0.
3602 2 means this list is a doc reference; replace it with the doc string. */
3603 int doc_reference = 0;
3604
3605 /* Initialize this to 1 if we are reading a list. */
3606 bool first_in_list = flag <= 0;
3607
3608 val = Qnil;
3609 tail = Qnil;
3610
3611 while (1)
3612 {
3613 int ch;
3614 GCPRO2 (val, tail);
3615 elt = read1 (readcharfun, &ch, first_in_list);
3616 UNGCPRO;
3617
3618 first_in_list = 0;
3619
3620 /* While building, if the list starts with #$, treat it specially. */
3621 if (EQ (elt, Vload_file_name)
3622 && ! NILP (elt)
3623 && !NILP (Vpurify_flag))
3624 {
3625 if (NILP (Vdoc_file_name))
3626 /* We have not yet called Snarf-documentation, so assume
3627 this file is described in the DOC file
3628 and Snarf-documentation will fill in the right value later.
3629 For now, replace the whole list with 0. */
3630 doc_reference = 1;
3631 else
3632 /* We have already called Snarf-documentation, so make a relative
3633 file name for this file, so it can be found properly
3634 in the installed Lisp directory.
3635 We don't use Fexpand_file_name because that would make
3636 the directory absolute now. */
3637 {
3638 AUTO_STRING (dot_dot_lisp, "../lisp/");
3639 elt = concat2 (dot_dot_lisp, Ffile_name_nondirectory (elt));
3640 }
3641 }
3642 else if (EQ (elt, Vload_file_name)
3643 && ! NILP (elt)
3644 && load_force_doc_strings)
3645 doc_reference = 2;
3646
3647 if (ch)
3648 {
3649 if (flag > 0)
3650 {
3651 if (ch == ']')
3652 return val;
3653 invalid_syntax (") or . in a vector");
3654 }
3655 if (ch == ')')
3656 return val;
3657 if (ch == '.')
3658 {
3659 GCPRO2 (val, tail);
3660 if (!NILP (tail))
3661 XSETCDR (tail, read0 (readcharfun));
3662 else
3663 val = read0 (readcharfun);
3664 read1 (readcharfun, &ch, 0);
3665 UNGCPRO;
3666 if (ch == ')')
3667 {
3668 if (doc_reference == 1)
3669 return make_number (0);
3670 if (doc_reference == 2 && INTEGERP (XCDR (val)))
3671 {
3672 char *saved = NULL;
3673 file_offset saved_position;
3674 /* Get a doc string from the file we are loading.
3675 If it's in saved_doc_string, get it from there.
3676
3677 Here, we don't know if the string is a
3678 bytecode string or a doc string. As a
3679 bytecode string must be unibyte, we always
3680 return a unibyte string. If it is actually a
3681 doc string, caller must make it
3682 multibyte. */
3683
3684 /* Position is negative for user variables. */
3685 EMACS_INT pos = eabs (XINT (XCDR (val)));
3686 if (pos >= saved_doc_string_position
3687 && pos < (saved_doc_string_position
3688 + saved_doc_string_length))
3689 {
3690 saved = saved_doc_string;
3691 saved_position = saved_doc_string_position;
3692 }
3693 /* Look in prev_saved_doc_string the same way. */
3694 else if (pos >= prev_saved_doc_string_position
3695 && pos < (prev_saved_doc_string_position
3696 + prev_saved_doc_string_length))
3697 {
3698 saved = prev_saved_doc_string;
3699 saved_position = prev_saved_doc_string_position;
3700 }
3701 if (saved)
3702 {
3703 ptrdiff_t start = pos - saved_position;
3704 ptrdiff_t from, to;
3705
3706 /* Process quoting with ^A,
3707 and find the end of the string,
3708 which is marked with ^_ (037). */
3709 for (from = start, to = start;
3710 saved[from] != 037;)
3711 {
3712 int c = saved[from++];
3713 if (c == 1)
3714 {
3715 c = saved[from++];
3716 saved[to++] = (c == 1 ? c
3717 : c == '0' ? 0
3718 : c == '_' ? 037
3719 : c);
3720 }
3721 else
3722 saved[to++] = c;
3723 }
3724
3725 return make_unibyte_string (saved + start,
3726 to - start);
3727 }
3728 else
3729 return get_doc_string (val, 1, 0);
3730 }
3731
3732 return val;
3733 }
3734 invalid_syntax (". in wrong context");
3735 }
3736 invalid_syntax ("] in a list");
3737 }
3738 tem = list1 (elt);
3739 if (!NILP (tail))
3740 XSETCDR (tail, tem);
3741 else
3742 val = tem;
3743 tail = tem;
3744 }
3745 }
3746 \f
3747 static Lisp_Object initial_obarray;
3748
3749 /* `oblookup' stores the bucket number here, for the sake of Funintern. */
3750
3751 static size_t oblookup_last_bucket_number;
3752
3753 /* Get an error if OBARRAY is not an obarray.
3754 If it is one, return it. */
3755
3756 Lisp_Object
3757 check_obarray (Lisp_Object obarray)
3758 {
3759 if (!VECTORP (obarray) || ASIZE (obarray) == 0)
3760 {
3761 /* If Vobarray is now invalid, force it to be valid. */
3762 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3763 wrong_type_argument (Qvectorp, obarray);
3764 }
3765 return obarray;
3766 }
3767
3768 /* Intern symbol SYM in OBARRAY using bucket INDEX. */
3769
3770 static Lisp_Object
3771 intern_sym (Lisp_Object sym, Lisp_Object obarray, Lisp_Object index)
3772 {
3773 Lisp_Object *ptr;
3774
3775 XSYMBOL (sym)->interned = (EQ (obarray, initial_obarray)
3776 ? SYMBOL_INTERNED_IN_INITIAL_OBARRAY
3777 : SYMBOL_INTERNED);
3778
3779 if (SREF (SYMBOL_NAME (sym), 0) == ':' && EQ (obarray, initial_obarray))
3780 {
3781 XSYMBOL (sym)->constant = 1;
3782 XSYMBOL (sym)->redirect = SYMBOL_PLAINVAL;
3783 SET_SYMBOL_VAL (XSYMBOL (sym), sym);
3784 }
3785
3786 ptr = aref_addr (obarray, XINT (index));
3787 set_symbol_next (sym, SYMBOLP (*ptr) ? XSYMBOL (*ptr) : NULL);
3788 *ptr = sym;
3789 return sym;
3790 }
3791
3792 /* Intern a symbol with name STRING in OBARRAY using bucket INDEX. */
3793
3794 Lisp_Object
3795 intern_driver (Lisp_Object string, Lisp_Object obarray, Lisp_Object index)
3796 {
3797 return intern_sym (Fmake_symbol (string), obarray, index);
3798 }
3799
3800 /* Intern the C string STR: return a symbol with that name,
3801 interned in the current obarray. */
3802
3803 Lisp_Object
3804 intern_1 (const char *str, ptrdiff_t len)
3805 {
3806 Lisp_Object obarray = check_obarray (Vobarray);
3807 Lisp_Object tem = oblookup (obarray, str, len, len);
3808
3809 return SYMBOLP (tem) ? tem : intern_driver (make_string (str, len),
3810 obarray, tem);
3811 }
3812
3813 Lisp_Object
3814 intern_c_string_1 (const char *str, ptrdiff_t len)
3815 {
3816 Lisp_Object obarray = check_obarray (Vobarray);
3817 Lisp_Object tem = oblookup (obarray, str, len, len);
3818
3819 if (!SYMBOLP (tem))
3820 {
3821 /* Creating a non-pure string from a string literal not implemented yet.
3822 We could just use make_string here and live with the extra copy. */
3823 eassert (!NILP (Vpurify_flag));
3824 tem = intern_driver (make_pure_c_string (str, len), obarray, tem);
3825 }
3826 return tem;
3827 }
3828
3829 static void
3830 define_symbol (Lisp_Object sym, char const *str)
3831 {
3832 ptrdiff_t len = strlen (str);
3833 Lisp_Object string = make_pure_c_string (str, len);
3834 init_symbol (sym, string);
3835
3836 /* Qunbound is uninterned, so that it's not confused with any symbol
3837 'unbound' created by a Lisp program. */
3838 if (! EQ (sym, Qunbound))
3839 {
3840 Lisp_Object bucket = oblookup (initial_obarray, str, len, len);
3841 eassert (INTEGERP (bucket));
3842 intern_sym (sym, initial_obarray, bucket);
3843 }
3844 }
3845 \f
3846 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
3847 doc: /* Return the canonical symbol whose name is STRING.
3848 If there is none, one is created by this function and returned.
3849 A second optional argument specifies the obarray to use;
3850 it defaults to the value of `obarray'. */)
3851 (Lisp_Object string, Lisp_Object obarray)
3852 {
3853 Lisp_Object tem;
3854
3855 obarray = check_obarray (NILP (obarray) ? Vobarray : obarray);
3856 CHECK_STRING (string);
3857
3858 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
3859 if (!SYMBOLP (tem))
3860 tem = intern_driver (NILP (Vpurify_flag) ? string : Fpurecopy (string),
3861 obarray, tem);
3862 return tem;
3863 }
3864
3865 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
3866 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3867 NAME may be a string or a symbol. If it is a symbol, that exact
3868 symbol is searched for.
3869 A second optional argument specifies the obarray to use;
3870 it defaults to the value of `obarray'. */)
3871 (Lisp_Object name, Lisp_Object obarray)
3872 {
3873 register Lisp_Object tem, string;
3874
3875 if (NILP (obarray)) obarray = Vobarray;
3876 obarray = check_obarray (obarray);
3877
3878 if (!SYMBOLP (name))
3879 {
3880 CHECK_STRING (name);
3881 string = name;
3882 }
3883 else
3884 string = SYMBOL_NAME (name);
3885
3886 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
3887 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3888 return Qnil;
3889 else
3890 return tem;
3891 }
3892 \f
3893 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3894 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3895 The value is t if a symbol was found and deleted, nil otherwise.
3896 NAME may be a string or a symbol. If it is a symbol, that symbol
3897 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3898 OBARRAY, if nil, defaults to the value of the variable `obarray'.
3899 usage: (unintern NAME OBARRAY) */)
3900 (Lisp_Object name, Lisp_Object obarray)
3901 {
3902 register Lisp_Object string, tem;
3903 size_t hash;
3904
3905 if (NILP (obarray)) obarray = Vobarray;
3906 obarray = check_obarray (obarray);
3907
3908 if (SYMBOLP (name))
3909 string = SYMBOL_NAME (name);
3910 else
3911 {
3912 CHECK_STRING (name);
3913 string = name;
3914 }
3915
3916 tem = oblookup (obarray, SSDATA (string),
3917 SCHARS (string),
3918 SBYTES (string));
3919 if (INTEGERP (tem))
3920 return Qnil;
3921 /* If arg was a symbol, don't delete anything but that symbol itself. */
3922 if (SYMBOLP (name) && !EQ (name, tem))
3923 return Qnil;
3924
3925 /* There are plenty of other symbols which will screw up the Emacs
3926 session if we unintern them, as well as even more ways to use
3927 `setq' or `fset' or whatnot to make the Emacs session
3928 unusable. Let's not go down this silly road. --Stef */
3929 /* if (EQ (tem, Qnil) || EQ (tem, Qt))
3930 error ("Attempt to unintern t or nil"); */
3931
3932 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3933
3934 hash = oblookup_last_bucket_number;
3935
3936 if (EQ (AREF (obarray, hash), tem))
3937 {
3938 if (XSYMBOL (tem)->next)
3939 {
3940 Lisp_Object sym;
3941 XSETSYMBOL (sym, XSYMBOL (tem)->next);
3942 ASET (obarray, hash, sym);
3943 }
3944 else
3945 ASET (obarray, hash, make_number (0));
3946 }
3947 else
3948 {
3949 Lisp_Object tail, following;
3950
3951 for (tail = AREF (obarray, hash);
3952 XSYMBOL (tail)->next;
3953 tail = following)
3954 {
3955 XSETSYMBOL (following, XSYMBOL (tail)->next);
3956 if (EQ (following, tem))
3957 {
3958 set_symbol_next (tail, XSYMBOL (following)->next);
3959 break;
3960 }
3961 }
3962 }
3963
3964 return Qt;
3965 }
3966 \f
3967 /* Return the symbol in OBARRAY whose names matches the string
3968 of SIZE characters (SIZE_BYTE bytes) at PTR.
3969 If there is no such symbol, return the integer bucket number of
3970 where the symbol would be if it were present.
3971
3972 Also store the bucket number in oblookup_last_bucket_number. */
3973
3974 Lisp_Object
3975 oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff_t size_byte)
3976 {
3977 size_t hash;
3978 size_t obsize;
3979 register Lisp_Object tail;
3980 Lisp_Object bucket, tem;
3981
3982 obarray = check_obarray (obarray);
3983 obsize = ASIZE (obarray);
3984
3985 /* This is sometimes needed in the middle of GC. */
3986 obsize &= ~ARRAY_MARK_FLAG;
3987 hash = hash_string (ptr, size_byte) % obsize;
3988 bucket = AREF (obarray, hash);
3989 oblookup_last_bucket_number = hash;
3990 if (EQ (bucket, make_number (0)))
3991 ;
3992 else if (!SYMBOLP (bucket))
3993 error ("Bad data in guts of obarray"); /* Like CADR error message. */
3994 else
3995 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3996 {
3997 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
3998 && SCHARS (SYMBOL_NAME (tail)) == size
3999 && !memcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
4000 return tail;
4001 else if (XSYMBOL (tail)->next == 0)
4002 break;
4003 }
4004 XSETINT (tem, hash);
4005 return tem;
4006 }
4007 \f
4008 void
4009 map_obarray (Lisp_Object obarray, void (*fn) (Lisp_Object, Lisp_Object), Lisp_Object arg)
4010 {
4011 ptrdiff_t i;
4012 register Lisp_Object tail;
4013 CHECK_VECTOR (obarray);
4014 for (i = ASIZE (obarray) - 1; i >= 0; i--)
4015 {
4016 tail = AREF (obarray, i);
4017 if (SYMBOLP (tail))
4018 while (1)
4019 {
4020 (*fn) (tail, arg);
4021 if (XSYMBOL (tail)->next == 0)
4022 break;
4023 XSETSYMBOL (tail, XSYMBOL (tail)->next);
4024 }
4025 }
4026 }
4027
4028 static void
4029 mapatoms_1 (Lisp_Object sym, Lisp_Object function)
4030 {
4031 call1 (function, sym);
4032 }
4033
4034 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
4035 doc: /* Call FUNCTION on every symbol in OBARRAY.
4036 OBARRAY defaults to the value of `obarray'. */)
4037 (Lisp_Object function, Lisp_Object obarray)
4038 {
4039 if (NILP (obarray)) obarray = Vobarray;
4040 obarray = check_obarray (obarray);
4041
4042 map_obarray (obarray, mapatoms_1, function);
4043 return Qnil;
4044 }
4045
4046 #define OBARRAY_SIZE 1511
4047
4048 void
4049 init_obarray (void)
4050 {
4051 Lisp_Object oblength;
4052 ptrdiff_t size = 100 + MAX_MULTIBYTE_LENGTH;
4053
4054 XSETFASTINT (oblength, OBARRAY_SIZE);
4055
4056 Vobarray = Fmake_vector (oblength, make_number (0));
4057 initial_obarray = Vobarray;
4058 staticpro (&initial_obarray);
4059
4060 for (int i = 0; i < ARRAYELTS (lispsym); i++)
4061 define_symbol (make_lisp_symbol (&lispsym[i]), defsym_name[i]);
4062
4063 DEFSYM (Qunbound, "unbound");
4064
4065 DEFSYM (Qnil, "nil");
4066 SET_SYMBOL_VAL (XSYMBOL (Qnil), Qnil);
4067 XSYMBOL (Qnil)->constant = 1;
4068 XSYMBOL (Qnil)->declared_special = true;
4069
4070 DEFSYM (Qt, "t");
4071 SET_SYMBOL_VAL (XSYMBOL (Qt), Qt);
4072 XSYMBOL (Qt)->constant = 1;
4073 XSYMBOL (Qt)->declared_special = true;
4074
4075 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
4076 Vpurify_flag = Qt;
4077
4078 DEFSYM (Qvariable_documentation, "variable-documentation");
4079
4080 read_buffer = xmalloc (size);
4081 read_buffer_size = size;
4082 }
4083 \f
4084 void
4085 defsubr (struct Lisp_Subr *sname)
4086 {
4087 Lisp_Object sym, tem;
4088 sym = intern_c_string (sname->symbol_name);
4089 XSETPVECTYPE (sname, PVEC_SUBR);
4090 XSETSUBR (tem, sname);
4091 set_symbol_function (sym, tem);
4092 }
4093
4094 #ifdef NOTDEF /* Use fset in subr.el now! */
4095 void
4096 defalias (struct Lisp_Subr *sname, char *string)
4097 {
4098 Lisp_Object sym;
4099 sym = intern (string);
4100 XSETSUBR (XSYMBOL (sym)->function, sname);
4101 }
4102 #endif /* NOTDEF */
4103
4104 /* Define an "integer variable"; a symbol whose value is forwarded to a
4105 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile):
4106 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
4107 void
4108 defvar_int (struct Lisp_Intfwd *i_fwd,
4109 const char *namestring, EMACS_INT *address)
4110 {
4111 Lisp_Object sym;
4112 sym = intern_c_string (namestring);
4113 i_fwd->type = Lisp_Fwd_Int;
4114 i_fwd->intvar = address;
4115 XSYMBOL (sym)->declared_special = 1;
4116 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4117 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)i_fwd);
4118 }
4119
4120 /* Similar but define a variable whose value is t if address contains 1,
4121 nil if address contains 0. */
4122 void
4123 defvar_bool (struct Lisp_Boolfwd *b_fwd,
4124 const char *namestring, bool *address)
4125 {
4126 Lisp_Object sym;
4127 sym = intern_c_string (namestring);
4128 b_fwd->type = Lisp_Fwd_Bool;
4129 b_fwd->boolvar = address;
4130 XSYMBOL (sym)->declared_special = 1;
4131 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4132 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)b_fwd);
4133 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
4134 }
4135
4136 /* Similar but define a variable whose value is the Lisp Object stored
4137 at address. Two versions: with and without gc-marking of the C
4138 variable. The nopro version is used when that variable will be
4139 gc-marked for some other reason, since marking the same slot twice
4140 can cause trouble with strings. */
4141 void
4142 defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd,
4143 const char *namestring, Lisp_Object *address)
4144 {
4145 Lisp_Object sym;
4146 sym = intern_c_string (namestring);
4147 o_fwd->type = Lisp_Fwd_Obj;
4148 o_fwd->objvar = address;
4149 XSYMBOL (sym)->declared_special = 1;
4150 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4151 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)o_fwd);
4152 }
4153
4154 void
4155 defvar_lisp (struct Lisp_Objfwd *o_fwd,
4156 const char *namestring, Lisp_Object *address)
4157 {
4158 defvar_lisp_nopro (o_fwd, namestring, address);
4159 staticpro (address);
4160 }
4161
4162 /* Similar but define a variable whose value is the Lisp Object stored
4163 at a particular offset in the current kboard object. */
4164
4165 void
4166 defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
4167 const char *namestring, int offset)
4168 {
4169 Lisp_Object sym;
4170 sym = intern_c_string (namestring);
4171 ko_fwd->type = Lisp_Fwd_Kboard_Obj;
4172 ko_fwd->offset = offset;
4173 XSYMBOL (sym)->declared_special = 1;
4174 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4175 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
4176 }
4177 \f
4178 /* Check that the elements of lpath exist. */
4179
4180 static void
4181 load_path_check (Lisp_Object lpath)
4182 {
4183 Lisp_Object path_tail;
4184
4185 /* The only elements that might not exist are those from
4186 PATH_LOADSEARCH, EMACSLOADPATH. Anything else is only added if
4187 it exists. */
4188 for (path_tail = lpath; !NILP (path_tail); path_tail = XCDR (path_tail))
4189 {
4190 Lisp_Object dirfile;
4191 dirfile = Fcar (path_tail);
4192 if (STRINGP (dirfile))
4193 {
4194 dirfile = Fdirectory_file_name (dirfile);
4195 if (! file_accessible_directory_p (dirfile))
4196 dir_warning ("Lisp directory", XCAR (path_tail));
4197 }
4198 }
4199 }
4200
4201 /* Return the default load-path, to be used if EMACSLOADPATH is unset.
4202 This does not include the standard site-lisp directories
4203 under the installation prefix (i.e., PATH_SITELOADSEARCH),
4204 but it does (unless no_site_lisp is set) include site-lisp
4205 directories in the source/build directories if those exist and we
4206 are running uninstalled.
4207
4208 Uses the following logic:
4209 If CANNOT_DUMP: Use PATH_LOADSEARCH.
4210 The remainder is what happens when dumping works:
4211 If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
4212 Otherwise use PATH_LOADSEARCH.
4213
4214 If !initialized, then just return PATH_DUMPLOADSEARCH.
4215 If initialized:
4216 If Vinstallation_directory is not nil (ie, running uninstalled):
4217 If installation-dir/lisp exists and not already a member,
4218 we must be running uninstalled. Reset the load-path
4219 to just installation-dir/lisp. (The default PATH_LOADSEARCH
4220 refers to the eventual installation directories. Since we
4221 are not yet installed, we should not use them, even if they exist.)
4222 If installation-dir/lisp does not exist, just add
4223 PATH_DUMPLOADSEARCH at the end instead.
4224 Add installation-dir/site-lisp (if !no_site_lisp, and exists
4225 and not already a member) at the front.
4226 If installation-dir != source-dir (ie running an uninstalled,
4227 out-of-tree build) AND install-dir/src/Makefile exists BUT
4228 install-dir/src/Makefile.in does NOT exist (this is a sanity
4229 check), then repeat the above steps for source-dir/lisp, site-lisp. */
4230
4231 static Lisp_Object
4232 load_path_default (void)
4233 {
4234 Lisp_Object lpath = Qnil;
4235 const char *normal;
4236
4237 #ifdef CANNOT_DUMP
4238 #ifdef HAVE_NS
4239 const char *loadpath = ns_load_path ();
4240 #endif
4241
4242 normal = PATH_LOADSEARCH;
4243 #ifdef HAVE_NS
4244 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4245 #else
4246 lpath = decode_env_path (0, normal, 0);
4247 #endif
4248
4249 #else /* !CANNOT_DUMP */
4250
4251 normal = NILP (Vpurify_flag) ? PATH_LOADSEARCH : PATH_DUMPLOADSEARCH;
4252
4253 if (initialized)
4254 {
4255 #ifdef HAVE_NS
4256 const char *loadpath = ns_load_path ();
4257 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4258 #else
4259 lpath = decode_env_path (0, normal, 0);
4260 #endif
4261 if (!NILP (Vinstallation_directory))
4262 {
4263 Lisp_Object tem, tem1;
4264
4265 /* Add to the path the lisp subdir of the installation
4266 dir, if it is accessible. Note: in out-of-tree builds,
4267 this directory is empty save for Makefile. */
4268 tem = Fexpand_file_name (build_string ("lisp"),
4269 Vinstallation_directory);
4270 tem1 = Ffile_accessible_directory_p (tem);
4271 if (!NILP (tem1))
4272 {
4273 if (NILP (Fmember (tem, lpath)))
4274 {
4275 /* We are running uninstalled. The default load-path
4276 points to the eventual installed lisp directories.
4277 We should not use those now, even if they exist,
4278 so start over from a clean slate. */
4279 lpath = list1 (tem);
4280 }
4281 }
4282 else
4283 /* That dir doesn't exist, so add the build-time
4284 Lisp dirs instead. */
4285 {
4286 Lisp_Object dump_path =
4287 decode_env_path (0, PATH_DUMPLOADSEARCH, 0);
4288 lpath = nconc2 (lpath, dump_path);
4289 }
4290
4291 /* Add site-lisp under the installation dir, if it exists. */
4292 if (!no_site_lisp)
4293 {
4294 tem = Fexpand_file_name (build_string ("site-lisp"),
4295 Vinstallation_directory);
4296 tem1 = Ffile_accessible_directory_p (tem);
4297 if (!NILP (tem1))
4298 {
4299 if (NILP (Fmember (tem, lpath)))
4300 lpath = Fcons (tem, lpath);
4301 }
4302 }
4303
4304 /* If Emacs was not built in the source directory,
4305 and it is run from where it was built, add to load-path
4306 the lisp and site-lisp dirs under that directory. */
4307
4308 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
4309 {
4310 Lisp_Object tem2;
4311
4312 tem = Fexpand_file_name (build_string ("src/Makefile"),
4313 Vinstallation_directory);
4314 tem1 = Ffile_exists_p (tem);
4315
4316 /* Don't be fooled if they moved the entire source tree
4317 AFTER dumping Emacs. If the build directory is indeed
4318 different from the source dir, src/Makefile.in and
4319 src/Makefile will not be found together. */
4320 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4321 Vinstallation_directory);
4322 tem2 = Ffile_exists_p (tem);
4323 if (!NILP (tem1) && NILP (tem2))
4324 {
4325 tem = Fexpand_file_name (build_string ("lisp"),
4326 Vsource_directory);
4327
4328 if (NILP (Fmember (tem, lpath)))
4329 lpath = Fcons (tem, lpath);
4330
4331 if (!no_site_lisp)
4332 {
4333 tem = Fexpand_file_name (build_string ("site-lisp"),
4334 Vsource_directory);
4335 tem1 = Ffile_accessible_directory_p (tem);
4336 if (!NILP (tem1))
4337 {
4338 if (NILP (Fmember (tem, lpath)))
4339 lpath = Fcons (tem, lpath);
4340 }
4341 }
4342 }
4343 } /* Vinstallation_directory != Vsource_directory */
4344
4345 } /* if Vinstallation_directory */
4346 }
4347 else /* !initialized */
4348 {
4349 /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
4350 source directory. We used to add ../lisp (ie the lisp dir in
4351 the build directory) at the front here, but that should not
4352 be necessary, since in out of tree builds lisp/ is empty, save
4353 for Makefile. */
4354 lpath = decode_env_path (0, normal, 0);
4355 }
4356 #endif /* !CANNOT_DUMP */
4357
4358 return lpath;
4359 }
4360
4361 void
4362 init_lread (void)
4363 {
4364 /* First, set Vload_path. */
4365
4366 /* Ignore EMACSLOADPATH when dumping. */
4367 #ifdef CANNOT_DUMP
4368 bool use_loadpath = true;
4369 #else
4370 bool use_loadpath = NILP (Vpurify_flag);
4371 #endif
4372
4373 if (use_loadpath && egetenv ("EMACSLOADPATH"))
4374 {
4375 Vload_path = decode_env_path ("EMACSLOADPATH", 0, 1);
4376
4377 /* Check (non-nil) user-supplied elements. */
4378 load_path_check (Vload_path);
4379
4380 /* If no nils in the environment variable, use as-is.
4381 Otherwise, replace any nils with the default. */
4382 if (! NILP (Fmemq (Qnil, Vload_path)))
4383 {
4384 Lisp_Object elem, elpath = Vload_path;
4385 Lisp_Object default_lpath = load_path_default ();
4386
4387 /* Check defaults, before adding site-lisp. */
4388 load_path_check (default_lpath);
4389
4390 /* Add the site-lisp directories to the front of the default. */
4391 if (!no_site_lisp)
4392 {
4393 Lisp_Object sitelisp;
4394 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4395 if (! NILP (sitelisp))
4396 default_lpath = nconc2 (sitelisp, default_lpath);
4397 }
4398
4399 Vload_path = Qnil;
4400
4401 /* Replace nils from EMACSLOADPATH by default. */
4402 while (CONSP (elpath))
4403 {
4404 Lisp_Object arg[2];
4405 elem = XCAR (elpath);
4406 elpath = XCDR (elpath);
4407 arg[0] = Vload_path;
4408 arg[1] = NILP (elem) ? default_lpath : Fcons (elem, Qnil);
4409 Vload_path = Fappend (2, arg);
4410 }
4411 } /* Fmemq (Qnil, Vload_path) */
4412 }
4413 else
4414 {
4415 Vload_path = load_path_default ();
4416
4417 /* Check before adding site-lisp directories.
4418 The install should have created them, but they are not
4419 required, so no need to warn if they are absent.
4420 Or we might be running before installation. */
4421 load_path_check (Vload_path);
4422
4423 /* Add the site-lisp directories at the front. */
4424 if (initialized && !no_site_lisp)
4425 {
4426 Lisp_Object sitelisp;
4427 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4428 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4429 }
4430 }
4431
4432 Vvalues = Qnil;
4433
4434 load_in_progress = 0;
4435 Vload_file_name = Qnil;
4436 Vstandard_input = Qt;
4437 Vloads_in_progress = Qnil;
4438 }
4439
4440 /* Print a warning that directory intended for use USE and with name
4441 DIRNAME cannot be accessed. On entry, errno should correspond to
4442 the access failure. Print the warning on stderr and put it in
4443 *Messages*. */
4444
4445 void
4446 dir_warning (char const *use, Lisp_Object dirname)
4447 {
4448 static char const format[] = "Warning: %s `%s': %s\n";
4449 int access_errno = errno;
4450 fprintf (stderr, format, use, SSDATA (dirname), strerror (access_errno));
4451
4452 /* Don't log the warning before we've initialized!! */
4453 if (initialized)
4454 {
4455 char const *diagnostic = emacs_strerror (access_errno);
4456 USE_SAFE_ALLOCA;
4457 char *buffer = SAFE_ALLOCA (sizeof format - 3 * (sizeof "%s" - 1)
4458 + strlen (use) + SBYTES (dirname)
4459 + strlen (diagnostic));
4460 ptrdiff_t message_len = esprintf (buffer, format, use, SSDATA (dirname),
4461 diagnostic);
4462 message_dolog (buffer, message_len, 0, STRING_MULTIBYTE (dirname));
4463 SAFE_FREE ();
4464 }
4465 }
4466
4467 void
4468 syms_of_lread (void)
4469 {
4470 defsubr (&Sread);
4471 defsubr (&Sread_from_string);
4472 defsubr (&Sintern);
4473 defsubr (&Sintern_soft);
4474 defsubr (&Sunintern);
4475 defsubr (&Sget_load_suffixes);
4476 defsubr (&Sload);
4477 defsubr (&Seval_buffer);
4478 defsubr (&Seval_region);
4479 defsubr (&Sread_char);
4480 defsubr (&Sread_char_exclusive);
4481 defsubr (&Sread_event);
4482 defsubr (&Sget_file_char);
4483 defsubr (&Smapatoms);
4484 defsubr (&Slocate_file_internal);
4485
4486 DEFVAR_LISP ("obarray", Vobarray,
4487 doc: /* Symbol table for use by `intern' and `read'.
4488 It is a vector whose length ought to be prime for best results.
4489 The vector's contents don't make sense if examined from Lisp programs;
4490 to find all the symbols in an obarray, use `mapatoms'. */);
4491
4492 DEFVAR_LISP ("values", Vvalues,
4493 doc: /* List of values of all expressions which were read, evaluated and printed.
4494 Order is reverse chronological. */);
4495 XSYMBOL (intern ("values"))->declared_special = 0;
4496
4497 DEFVAR_LISP ("standard-input", Vstandard_input,
4498 doc: /* Stream for read to get input from.
4499 See documentation of `read' for possible values. */);
4500 Vstandard_input = Qt;
4501
4502 DEFVAR_LISP ("read-with-symbol-positions", Vread_with_symbol_positions,
4503 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4504
4505 If this variable is a buffer, then only forms read from that buffer
4506 will be added to `read-symbol-positions-list'.
4507 If this variable is t, then all read forms will be added.
4508 The effect of all other values other than nil are not currently
4509 defined, although they may be in the future.
4510
4511 The positions are relative to the last call to `read' or
4512 `read-from-string'. It is probably a bad idea to set this variable at
4513 the toplevel; bind it instead. */);
4514 Vread_with_symbol_positions = Qnil;
4515
4516 DEFVAR_LISP ("read-symbol-positions-list", Vread_symbol_positions_list,
4517 doc: /* A list mapping read symbols to their positions.
4518 This variable is modified during calls to `read' or
4519 `read-from-string', but only when `read-with-symbol-positions' is
4520 non-nil.
4521
4522 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4523 CHAR-POSITION is an integer giving the offset of that occurrence of the
4524 symbol from the position where `read' or `read-from-string' started.
4525
4526 Note that a symbol will appear multiple times in this list, if it was
4527 read multiple times. The list is in the same order as the symbols
4528 were read in. */);
4529 Vread_symbol_positions_list = Qnil;
4530
4531 DEFVAR_LISP ("read-circle", Vread_circle,
4532 doc: /* Non-nil means read recursive structures using #N= and #N# syntax. */);
4533 Vread_circle = Qt;
4534
4535 DEFVAR_LISP ("load-path", Vload_path,
4536 doc: /* List of directories to search for files to load.
4537 Each element is a string (directory name) or nil (meaning `default-directory').
4538 Initialized during startup as described in Info node `(elisp)Library Search'. */);
4539
4540 DEFVAR_LISP ("load-suffixes", Vload_suffixes,
4541 doc: /* List of suffixes for (compiled or source) Emacs Lisp files.
4542 This list should not include the empty string.
4543 `load' and related functions try to append these suffixes, in order,
4544 to the specified file name if a Lisp suffix is allowed or required. */);
4545 Vload_suffixes = list2 (build_pure_c_string (".elc"),
4546 build_pure_c_string (".el"));
4547 DEFVAR_LISP ("load-file-rep-suffixes", Vload_file_rep_suffixes,
4548 doc: /* List of suffixes that indicate representations of \
4549 the same file.
4550 This list should normally start with the empty string.
4551
4552 Enabling Auto Compression mode appends the suffixes in
4553 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4554 mode removes them again. `load' and related functions use this list to
4555 determine whether they should look for compressed versions of a file
4556 and, if so, which suffixes they should try to append to the file name
4557 in order to do so. However, if you want to customize which suffixes
4558 the loading functions recognize as compression suffixes, you should
4559 customize `jka-compr-load-suffixes' rather than the present variable. */);
4560 Vload_file_rep_suffixes = list1 (empty_unibyte_string);
4561
4562 DEFVAR_BOOL ("load-in-progress", load_in_progress,
4563 doc: /* Non-nil if inside of `load'. */);
4564 DEFSYM (Qload_in_progress, "load-in-progress");
4565
4566 DEFVAR_LISP ("after-load-alist", Vafter_load_alist,
4567 doc: /* An alist of functions to be evalled when particular files are loaded.
4568 Each element looks like (REGEXP-OR-FEATURE FUNCS...).
4569
4570 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4571 a symbol \(a feature name).
4572
4573 When `load' is run and the file-name argument matches an element's
4574 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4575 REGEXP-OR-FEATURE, the FUNCS in the element are called.
4576
4577 An error in FORMS does not undo the load, but does prevent execution of
4578 the rest of the FORMS. */);
4579 Vafter_load_alist = Qnil;
4580
4581 DEFVAR_LISP ("load-history", Vload_history,
4582 doc: /* Alist mapping loaded file names to symbols and features.
4583 Each alist element should be a list (FILE-NAME ENTRIES...), where
4584 FILE-NAME is the name of a file that has been loaded into Emacs.
4585 The file name is absolute and true (i.e. it doesn't contain symlinks).
4586 As an exception, one of the alist elements may have FILE-NAME nil,
4587 for symbols and features not associated with any file.
4588
4589 The remaining ENTRIES in the alist element describe the functions and
4590 variables defined in that file, the features provided, and the
4591 features required. Each entry has the form `(provide . FEATURE)',
4592 `(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
4593 `(defface . SYMBOL)', or `(t . SYMBOL)'. Entries like `(t . SYMBOL)'
4594 may precede a `(defun . FUNCTION)' entry, and means that SYMBOL was an
4595 autoload before this file redefined it as a function. In addition,
4596 entries may also be single symbols, which means that SYMBOL was
4597 defined by `defvar' or `defconst'.
4598
4599 During preloading, the file name recorded is relative to the main Lisp
4600 directory. These file names are converted to absolute at startup. */);
4601 Vload_history = Qnil;
4602
4603 DEFVAR_LISP ("load-file-name", Vload_file_name,
4604 doc: /* Full name of file being loaded by `load'. */);
4605 Vload_file_name = Qnil;
4606
4607 DEFVAR_LISP ("user-init-file", Vuser_init_file,
4608 doc: /* File name, including directory, of user's initialization file.
4609 If the file loaded had extension `.elc', and the corresponding source file
4610 exists, this variable contains the name of source file, suitable for use
4611 by functions like `custom-save-all' which edit the init file.
4612 While Emacs loads and evaluates the init file, value is the real name
4613 of the file, regardless of whether or not it has the `.elc' extension. */);
4614 Vuser_init_file = Qnil;
4615
4616 DEFVAR_LISP ("current-load-list", Vcurrent_load_list,
4617 doc: /* Used for internal purposes by `load'. */);
4618 Vcurrent_load_list = Qnil;
4619
4620 DEFVAR_LISP ("load-read-function", Vload_read_function,
4621 doc: /* Function used by `load' and `eval-region' for reading expressions.
4622 The default is nil, which means use the function `read'. */);
4623 Vload_read_function = Qnil;
4624
4625 DEFVAR_LISP ("load-source-file-function", Vload_source_file_function,
4626 doc: /* Function called in `load' to load an Emacs Lisp source file.
4627 The value should be a function for doing code conversion before
4628 reading a source file. It can also be nil, in which case loading is
4629 done without any code conversion.
4630
4631 If the value is a function, it is called with four arguments,
4632 FULLNAME, FILE, NOERROR, NOMESSAGE. FULLNAME is the absolute name of
4633 the file to load, FILE is the non-absolute name (for messages etc.),
4634 and NOERROR and NOMESSAGE are the corresponding arguments passed to
4635 `load'. The function should return t if the file was loaded. */);
4636 Vload_source_file_function = Qnil;
4637
4638 DEFVAR_BOOL ("load-force-doc-strings", load_force_doc_strings,
4639 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4640 This is useful when the file being loaded is a temporary copy. */);
4641 load_force_doc_strings = 0;
4642
4643 DEFVAR_BOOL ("load-convert-to-unibyte", load_convert_to_unibyte,
4644 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4645 This is normally bound by `load' and `eval-buffer' to control `read',
4646 and is not meant for users to change. */);
4647 load_convert_to_unibyte = 0;
4648
4649 DEFVAR_LISP ("source-directory", Vsource_directory,
4650 doc: /* Directory in which Emacs sources were found when Emacs was built.
4651 You cannot count on them to still be there! */);
4652 Vsource_directory
4653 = Fexpand_file_name (build_string ("../"),
4654 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH, 0)));
4655
4656 DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
4657 doc: /* List of files that were preloaded (when dumping Emacs). */);
4658 Vpreloaded_file_list = Qnil;
4659
4660 DEFVAR_LISP ("byte-boolean-vars", Vbyte_boolean_vars,
4661 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4662 Vbyte_boolean_vars = Qnil;
4663
4664 DEFVAR_BOOL ("load-dangerous-libraries", load_dangerous_libraries,
4665 doc: /* Non-nil means load dangerous compiled Lisp files.
4666 Some versions of XEmacs use different byte codes than Emacs. These
4667 incompatible byte codes can make Emacs crash when it tries to execute
4668 them. */);
4669 load_dangerous_libraries = 0;
4670
4671 DEFVAR_BOOL ("force-load-messages", force_load_messages,
4672 doc: /* Non-nil means force printing messages when loading Lisp files.
4673 This overrides the value of the NOMESSAGE argument to `load'. */);
4674 force_load_messages = 0;
4675
4676 DEFVAR_LISP ("bytecomp-version-regexp", Vbytecomp_version_regexp,
4677 doc: /* Regular expression matching safe to load compiled Lisp files.
4678 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4679 from the file, and matches them against this regular expression.
4680 When the regular expression matches, the file is considered to be safe
4681 to load. See also `load-dangerous-libraries'. */);
4682 Vbytecomp_version_regexp
4683 = build_pure_c_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4684
4685 DEFSYM (Qlexical_binding, "lexical-binding");
4686 DEFVAR_LISP ("lexical-binding", Vlexical_binding,
4687 doc: /* Whether to use lexical binding when evaluating code.
4688 Non-nil means that the code in the current buffer should be evaluated
4689 with lexical binding.
4690 This variable is automatically set from the file variables of an
4691 interpreted Lisp file read using `load'. Unlike other file local
4692 variables, this must be set in the first line of a file. */);
4693 Vlexical_binding = Qnil;
4694 Fmake_variable_buffer_local (Qlexical_binding);
4695
4696 DEFVAR_LISP ("eval-buffer-list", Veval_buffer_list,
4697 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4698 Veval_buffer_list = Qnil;
4699
4700 DEFVAR_LISP ("old-style-backquotes", Vold_style_backquotes,
4701 doc: /* Set to non-nil when `read' encounters an old-style backquote. */);
4702 Vold_style_backquotes = Qnil;
4703 DEFSYM (Qold_style_backquotes, "old-style-backquotes");
4704
4705 DEFVAR_BOOL ("load-prefer-newer", load_prefer_newer,
4706 doc: /* Non-nil means `load' prefers the newest version of a file.
4707 This applies when a filename suffix is not explicitly specified and
4708 `load' is trying various possible suffixes (see `load-suffixes' and
4709 `load-file-rep-suffixes'). Normally, it stops at the first file
4710 that exists unless you explicitly specify one or the other. If this
4711 option is non-nil, it checks all suffixes and uses whichever file is
4712 newest.
4713 Note that if you customize this, obviously it will not affect files
4714 that are loaded before your customizations are read! */);
4715 load_prefer_newer = 0;
4716
4717 /* Vsource_directory was initialized in init_lread. */
4718
4719 DEFSYM (Qcurrent_load_list, "current-load-list");
4720 DEFSYM (Qstandard_input, "standard-input");
4721 DEFSYM (Qread_char, "read-char");
4722 DEFSYM (Qget_file_char, "get-file-char");
4723
4724 /* Used instead of Qget_file_char while loading *.elc files compiled
4725 by Emacs 21 or older. */
4726 DEFSYM (Qget_emacs_mule_file_char, "get-emacs-mule-file-char");
4727
4728 DEFSYM (Qload_force_doc_strings, "load-force-doc-strings");
4729
4730 DEFSYM (Qbackquote, "`");
4731 DEFSYM (Qcomma, ",");
4732 DEFSYM (Qcomma_at, ",@");
4733 DEFSYM (Qcomma_dot, ",.");
4734
4735 DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
4736 DEFSYM (Qascii_character, "ascii-character");
4737 DEFSYM (Qfunction, "function");
4738 DEFSYM (Qload, "load");
4739 DEFSYM (Qload_file_name, "load-file-name");
4740 DEFSYM (Qeval_buffer_list, "eval-buffer-list");
4741 DEFSYM (Qfile_truename, "file-truename");
4742 DEFSYM (Qdir_ok, "dir-ok");
4743 DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
4744
4745 staticpro (&read_objects);
4746 read_objects = Qnil;
4747 staticpro (&seen_list);
4748 seen_list = Qnil;
4749
4750 Vloads_in_progress = Qnil;
4751 staticpro (&Vloads_in_progress);
4752
4753 DEFSYM (Qhash_table, "hash-table");
4754 DEFSYM (Qdata, "data");
4755 DEFSYM (Qtest, "test");
4756 DEFSYM (Qsize, "size");
4757 DEFSYM (Qweakness, "weakness");
4758 DEFSYM (Qrehash_size, "rehash-size");
4759 DEFSYM (Qrehash_threshold, "rehash-threshold");
4760 }