Define _GNU_SOURCE in files delaying config.h
[emacs.git] / src / lread.c
blobdc7c00bbfae68f27b3f728fdcd8168f9f8550f5c
1 /* Lisp parsing and input streams.
3 Copyright (C) 1985-1989, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* Tell globals.h to define tables needed by init_obarray. */
22 #define DEFINE_SYMBOLS
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 <math.h>
31 #include <stat-time.h>
32 #include "lisp.h"
33 #include "dispextern.h"
34 #include "intervals.h"
35 #include "character.h"
36 #include "buffer.h"
37 #include "charset.h"
38 #include <epaths.h>
39 #include "commands.h"
40 #include "keyboard.h"
41 #include "systime.h"
42 #include "termhooks.h"
43 #include "blockinput.h"
44 #include <c-ctype.h>
46 #ifdef MSDOS
47 #include "msdos.h"
48 #if __DJGPP__ == 2 && __DJGPP_MINOR__ < 5
49 # define INFINITY __builtin_inf()
50 # define NAN __builtin_nan("")
51 #endif
52 #endif
54 #ifdef HAVE_NS
55 #include "nsterm.h"
56 #endif
58 #include <unistd.h>
60 #ifdef HAVE_SETLOCALE
61 #include <locale.h>
62 #endif /* HAVE_SETLOCALE */
64 #include <fcntl.h>
66 #ifdef HAVE_FSEEKO
67 #define file_offset off_t
68 #define file_tell ftello
69 #else
70 #define file_offset long
71 #define file_tell ftell
72 #endif
74 /* The association list of objects read with the #n=object form.
75 Each member of the list has the form (n . object), and is used to
76 look up the object for the corresponding #n# construct.
77 It must be set to nil before all top-level calls to read0. */
78 static Lisp_Object read_objects;
80 /* File for get_file_char to read from. Use by load. */
81 static FILE *instream;
83 /* For use within read-from-string (this reader is non-reentrant!!) */
84 static ptrdiff_t read_from_string_index;
85 static ptrdiff_t read_from_string_index_byte;
86 static ptrdiff_t read_from_string_limit;
88 /* Number of characters read in the current call to Fread or
89 Fread_from_string. */
90 static EMACS_INT readchar_count;
92 /* This contains the last string skipped with #@. */
93 static char *saved_doc_string;
94 /* Length of buffer allocated in saved_doc_string. */
95 static ptrdiff_t saved_doc_string_size;
96 /* Length of actual data in saved_doc_string. */
97 static ptrdiff_t saved_doc_string_length;
98 /* This is the file position that string came from. */
99 static file_offset saved_doc_string_position;
101 /* This contains the previous string skipped with #@.
102 We copy it from saved_doc_string when a new string
103 is put in saved_doc_string. */
104 static char *prev_saved_doc_string;
105 /* Length of buffer allocated in prev_saved_doc_string. */
106 static ptrdiff_t prev_saved_doc_string_size;
107 /* Length of actual data in prev_saved_doc_string. */
108 static ptrdiff_t prev_saved_doc_string_length;
109 /* This is the file position that string came from. */
110 static file_offset prev_saved_doc_string_position;
112 /* True means inside a new-style backquote
113 with no surrounding parentheses.
114 Fread initializes this to false, so we need not specbind it
115 or worry about what happens to it when there is an error. */
116 static bool new_backquote_flag;
118 /* A list of file names for files being loaded in Fload. Used to
119 check for recursive loads. */
121 static Lisp_Object Vloads_in_progress;
123 static int read_emacs_mule_char (int, int (*) (int, Lisp_Object),
124 Lisp_Object);
126 static void readevalloop (Lisp_Object, FILE *, Lisp_Object, bool,
127 Lisp_Object, Lisp_Object,
128 Lisp_Object, Lisp_Object);
130 /* Functions that read one byte from the current source READCHARFUN
131 or unreads one byte. If the integer argument C is -1, it returns
132 one read byte, or -1 when there's no more byte in the source. If C
133 is 0 or positive, it unreads C, and the return value is not
134 interesting. */
136 static int readbyte_for_lambda (int, Lisp_Object);
137 static int readbyte_from_file (int, Lisp_Object);
138 static int readbyte_from_string (int, Lisp_Object);
140 /* Handle unreading and rereading of characters.
141 Write READCHAR to read a character,
142 UNREAD(c) to unread c to be read again.
144 These macros correctly read/unread multibyte characters. */
146 #define READCHAR readchar (readcharfun, NULL)
147 #define UNREAD(c) unreadchar (readcharfun, c)
149 /* Same as READCHAR but set *MULTIBYTE to the multibyteness of the source. */
150 #define READCHAR_REPORT_MULTIBYTE(multibyte) readchar (readcharfun, multibyte)
152 /* When READCHARFUN is Qget_file_char, Qget_emacs_mule_file_char,
153 Qlambda, or a cons, we use this to keep an unread character because
154 a file stream can't handle multibyte-char unreading. The value -1
155 means that there's no unread character. */
156 static int unread_char;
158 static int
159 readchar (Lisp_Object readcharfun, bool *multibyte)
161 Lisp_Object tem;
162 register int c;
163 int (*readbyte) (int, Lisp_Object);
164 unsigned char buf[MAX_MULTIBYTE_LENGTH];
165 int i, len;
166 bool emacs_mule_encoding = 0;
168 if (multibyte)
169 *multibyte = 0;
171 readchar_count++;
173 if (BUFFERP (readcharfun))
175 register struct buffer *inbuffer = XBUFFER (readcharfun);
177 ptrdiff_t pt_byte = BUF_PT_BYTE (inbuffer);
179 if (! BUFFER_LIVE_P (inbuffer))
180 return -1;
182 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
183 return -1;
185 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
187 /* Fetch the character code from the buffer. */
188 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
189 BUF_INC_POS (inbuffer, pt_byte);
190 c = STRING_CHAR (p);
191 if (multibyte)
192 *multibyte = 1;
194 else
196 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
197 if (! ASCII_CHAR_P (c))
198 c = BYTE8_TO_CHAR (c);
199 pt_byte++;
201 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
203 return c;
205 if (MARKERP (readcharfun))
207 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
209 ptrdiff_t bytepos = marker_byte_position (readcharfun);
211 if (bytepos >= BUF_ZV_BYTE (inbuffer))
212 return -1;
214 if (! NILP (BVAR (inbuffer, enable_multibyte_characters)))
216 /* Fetch the character code from the buffer. */
217 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
218 BUF_INC_POS (inbuffer, bytepos);
219 c = STRING_CHAR (p);
220 if (multibyte)
221 *multibyte = 1;
223 else
225 c = BUF_FETCH_BYTE (inbuffer, bytepos);
226 if (! ASCII_CHAR_P (c))
227 c = BYTE8_TO_CHAR (c);
228 bytepos++;
231 XMARKER (readcharfun)->bytepos = bytepos;
232 XMARKER (readcharfun)->charpos++;
234 return c;
237 if (EQ (readcharfun, Qlambda))
239 readbyte = readbyte_for_lambda;
240 goto read_multibyte;
243 if (EQ (readcharfun, Qget_file_char))
245 readbyte = readbyte_from_file;
246 goto read_multibyte;
249 if (STRINGP (readcharfun))
251 if (read_from_string_index >= read_from_string_limit)
252 c = -1;
253 else if (STRING_MULTIBYTE (readcharfun))
255 if (multibyte)
256 *multibyte = 1;
257 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, readcharfun,
258 read_from_string_index,
259 read_from_string_index_byte);
261 else
263 c = SREF (readcharfun, read_from_string_index_byte);
264 read_from_string_index++;
265 read_from_string_index_byte++;
267 return c;
270 if (CONSP (readcharfun) && STRINGP (XCAR (readcharfun)))
272 /* This is the case that read_vector is reading from a unibyte
273 string that contains a byte sequence previously skipped
274 because of #@NUMBER. The car part of readcharfun is that
275 string, and the cdr part is a value of readcharfun given to
276 read_vector. */
277 readbyte = readbyte_from_string;
278 if (EQ (XCDR (readcharfun), Qget_emacs_mule_file_char))
279 emacs_mule_encoding = 1;
280 goto read_multibyte;
283 if (EQ (readcharfun, Qget_emacs_mule_file_char))
285 readbyte = readbyte_from_file;
286 emacs_mule_encoding = 1;
287 goto read_multibyte;
290 tem = call0 (readcharfun);
292 if (NILP (tem))
293 return -1;
294 return XINT (tem);
296 read_multibyte:
297 if (unread_char >= 0)
299 c = unread_char;
300 unread_char = -1;
301 return c;
303 c = (*readbyte) (-1, readcharfun);
304 if (c < 0)
305 return c;
306 if (multibyte)
307 *multibyte = 1;
308 if (ASCII_CHAR_P (c))
309 return c;
310 if (emacs_mule_encoding)
311 return read_emacs_mule_char (c, readbyte, readcharfun);
312 i = 0;
313 buf[i++] = c;
314 len = BYTES_BY_CHAR_HEAD (c);
315 while (i < len)
317 c = (*readbyte) (-1, readcharfun);
318 if (c < 0 || ! TRAILING_CODE_P (c))
320 while (--i > 1)
321 (*readbyte) (buf[i], readcharfun);
322 return BYTE8_TO_CHAR (buf[0]);
324 buf[i++] = c;
326 return STRING_CHAR (buf);
329 #define FROM_FILE_P(readcharfun) \
330 (EQ (readcharfun, Qget_file_char) \
331 || EQ (readcharfun, Qget_emacs_mule_file_char))
333 static void
334 skip_dyn_bytes (Lisp_Object readcharfun, ptrdiff_t n)
336 if (FROM_FILE_P (readcharfun))
338 block_input (); /* FIXME: Not sure if it's needed. */
339 fseek (instream, n, SEEK_CUR);
340 unblock_input ();
342 else
343 { /* We're not reading directly from a file. In that case, it's difficult
344 to reliably count bytes, since these are usually meant for the file's
345 encoding, whereas we're now typically in the internal encoding.
346 But luckily, skip_dyn_bytes is used to skip over a single
347 dynamic-docstring (or dynamic byte-code) which is always quoted such
348 that \037 is the final char. */
349 int c;
350 do {
351 c = READCHAR;
352 } while (c >= 0 && c != '\037');
356 static void
357 skip_dyn_eof (Lisp_Object readcharfun)
359 if (FROM_FILE_P (readcharfun))
361 block_input (); /* FIXME: Not sure if it's needed. */
362 fseek (instream, 0, SEEK_END);
363 unblock_input ();
365 else
366 while (READCHAR >= 0);
369 /* Unread the character C in the way appropriate for the stream READCHARFUN.
370 If the stream is a user function, call it with the char as argument. */
372 static void
373 unreadchar (Lisp_Object readcharfun, int c)
375 readchar_count--;
376 if (c == -1)
377 /* Don't back up the pointer if we're unreading the end-of-input mark,
378 since readchar didn't advance it when we read it. */
380 else if (BUFFERP (readcharfun))
382 struct buffer *b = XBUFFER (readcharfun);
383 ptrdiff_t charpos = BUF_PT (b);
384 ptrdiff_t bytepos = BUF_PT_BYTE (b);
386 if (! NILP (BVAR (b, enable_multibyte_characters)))
387 BUF_DEC_POS (b, bytepos);
388 else
389 bytepos--;
391 SET_BUF_PT_BOTH (b, charpos - 1, bytepos);
393 else if (MARKERP (readcharfun))
395 struct buffer *b = XMARKER (readcharfun)->buffer;
396 ptrdiff_t bytepos = XMARKER (readcharfun)->bytepos;
398 XMARKER (readcharfun)->charpos--;
399 if (! NILP (BVAR (b, enable_multibyte_characters)))
400 BUF_DEC_POS (b, bytepos);
401 else
402 bytepos--;
404 XMARKER (readcharfun)->bytepos = bytepos;
406 else if (STRINGP (readcharfun))
408 read_from_string_index--;
409 read_from_string_index_byte
410 = string_char_to_byte (readcharfun, read_from_string_index);
412 else if (CONSP (readcharfun) && STRINGP (XCAR (readcharfun)))
414 unread_char = c;
416 else if (EQ (readcharfun, Qlambda))
418 unread_char = c;
420 else if (FROM_FILE_P (readcharfun))
422 unread_char = c;
424 else
425 call1 (readcharfun, make_number (c));
428 static int
429 readbyte_for_lambda (int c, Lisp_Object readcharfun)
431 return read_bytecode_char (c >= 0);
435 static int
436 readbyte_from_file (int c, Lisp_Object readcharfun)
438 if (c >= 0)
440 block_input ();
441 ungetc (c, instream);
442 unblock_input ();
443 return 0;
446 block_input ();
447 c = getc (instream);
449 /* Interrupted reads have been observed while reading over the network. */
450 while (c == EOF && ferror (instream) && errno == EINTR)
452 unblock_input ();
453 QUIT;
454 block_input ();
455 clearerr (instream);
456 c = getc (instream);
459 unblock_input ();
461 return (c == EOF ? -1 : c);
464 static int
465 readbyte_from_string (int c, Lisp_Object readcharfun)
467 Lisp_Object string = XCAR (readcharfun);
469 if (c >= 0)
471 read_from_string_index--;
472 read_from_string_index_byte
473 = string_char_to_byte (string, read_from_string_index);
476 if (read_from_string_index >= read_from_string_limit)
477 c = -1;
478 else
479 FETCH_STRING_CHAR_ADVANCE (c, string,
480 read_from_string_index,
481 read_from_string_index_byte);
482 return c;
486 /* Read one non-ASCII character from INSTREAM. The character is
487 encoded in `emacs-mule' and the first byte is already read in
488 C. */
490 static int
491 read_emacs_mule_char (int c, int (*readbyte) (int, Lisp_Object), Lisp_Object readcharfun)
493 /* Emacs-mule coding uses at most 4-byte for one character. */
494 unsigned char buf[4];
495 int len = emacs_mule_bytes[c];
496 struct charset *charset;
497 int i;
498 unsigned code;
500 if (len == 1)
501 /* C is not a valid leading-code of `emacs-mule'. */
502 return BYTE8_TO_CHAR (c);
504 i = 0;
505 buf[i++] = c;
506 while (i < len)
508 c = (*readbyte) (-1, readcharfun);
509 if (c < 0xA0)
511 while (--i > 1)
512 (*readbyte) (buf[i], readcharfun);
513 return BYTE8_TO_CHAR (buf[0]);
515 buf[i++] = c;
518 if (len == 2)
520 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
521 code = buf[1] & 0x7F;
523 else if (len == 3)
525 if (buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_11
526 || buf[0] == EMACS_MULE_LEADING_CODE_PRIVATE_12)
528 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
529 code = buf[2] & 0x7F;
531 else
533 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[0]]);
534 code = ((buf[1] << 8) | buf[2]) & 0x7F7F;
537 else
539 charset = CHARSET_FROM_ID (emacs_mule_charset[buf[1]]);
540 code = ((buf[2] << 8) | buf[3]) & 0x7F7F;
542 c = DECODE_CHAR (charset, code);
543 if (c < 0)
544 Fsignal (Qinvalid_read_syntax,
545 list1 (build_string ("invalid multibyte form")));
546 return c;
550 static Lisp_Object read_internal_start (Lisp_Object, Lisp_Object,
551 Lisp_Object);
552 static Lisp_Object read0 (Lisp_Object);
553 static Lisp_Object read1 (Lisp_Object, int *, bool);
555 static Lisp_Object read_list (bool, Lisp_Object);
556 static Lisp_Object read_vector (Lisp_Object, bool);
558 static Lisp_Object substitute_object_recurse (Lisp_Object, Lisp_Object,
559 Lisp_Object);
560 static void substitute_object_in_subtree (Lisp_Object,
561 Lisp_Object);
562 static void substitute_in_interval (INTERVAL, Lisp_Object);
565 /* Get a character from the tty. */
567 /* Read input events until we get one that's acceptable for our purposes.
569 If NO_SWITCH_FRAME, switch-frame events are stashed
570 until we get a character we like, and then stuffed into
571 unread_switch_frame.
573 If ASCII_REQUIRED, check function key events to see
574 if the unmodified version of the symbol has a Qascii_character
575 property, and use that character, if present.
577 If ERROR_NONASCII, signal an error if the input we
578 get isn't an ASCII character with modifiers. If it's false but
579 ASCII_REQUIRED is true, just re-read until we get an ASCII
580 character.
582 If INPUT_METHOD, invoke the current input method
583 if the character warrants that.
585 If SECONDS is a number, wait that many seconds for input, and
586 return Qnil if no input arrives within that time. */
588 static Lisp_Object
589 read_filtered_event (bool no_switch_frame, bool ascii_required,
590 bool error_nonascii, bool input_method, Lisp_Object seconds)
592 Lisp_Object val, delayed_switch_frame;
593 struct timespec end_time;
595 #ifdef HAVE_WINDOW_SYSTEM
596 if (display_hourglass_p)
597 cancel_hourglass ();
598 #endif
600 delayed_switch_frame = Qnil;
602 /* Compute timeout. */
603 if (NUMBERP (seconds))
605 double duration = extract_float (seconds);
606 struct timespec wait_time = dtotimespec (duration);
607 end_time = timespec_add (current_timespec (), wait_time);
610 /* Read until we get an acceptable event. */
611 retry:
613 val = read_char (0, Qnil, (input_method ? Qnil : Qt), 0,
614 NUMBERP (seconds) ? &end_time : NULL);
615 while (INTEGERP (val) && XINT (val) == -2); /* wrong_kboard_jmpbuf */
617 if (BUFFERP (val))
618 goto retry;
620 /* `switch-frame' events are put off until after the next ASCII
621 character. This is better than signaling an error just because
622 the last characters were typed to a separate minibuffer frame,
623 for example. Eventually, some code which can deal with
624 switch-frame events will read it and process it. */
625 if (no_switch_frame
626 && EVENT_HAS_PARAMETERS (val)
627 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (val)), Qswitch_frame))
629 delayed_switch_frame = val;
630 goto retry;
633 if (ascii_required && !(NUMBERP (seconds) && NILP (val)))
635 /* Convert certain symbols to their ASCII equivalents. */
636 if (SYMBOLP (val))
638 Lisp_Object tem, tem1;
639 tem = Fget (val, Qevent_symbol_element_mask);
640 if (!NILP (tem))
642 tem1 = Fget (Fcar (tem), Qascii_character);
643 /* Merge this symbol's modifier bits
644 with the ASCII equivalent of its basic code. */
645 if (!NILP (tem1))
646 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
650 /* If we don't have a character now, deal with it appropriately. */
651 if (!INTEGERP (val))
653 if (error_nonascii)
655 Vunread_command_events = list1 (val);
656 error ("Non-character input-event");
658 else
659 goto retry;
663 if (! NILP (delayed_switch_frame))
664 unread_switch_frame = delayed_switch_frame;
666 #if 0
668 #ifdef HAVE_WINDOW_SYSTEM
669 if (display_hourglass_p)
670 start_hourglass ();
671 #endif
673 #endif
675 return val;
678 DEFUN ("read-char", Fread_char, Sread_char, 0, 3, 0,
679 doc: /* Read a character from the command input (keyboard or macro).
680 It is returned as a number.
681 If the character has modifiers, they are resolved and reflected to the
682 character code if possible (e.g. C-SPC -> 0).
684 If the user generates an event which is not a character (i.e. a mouse
685 click or function key event), `read-char' signals an error. As an
686 exception, switch-frame events are put off until non-character events
687 can be read.
688 If you want to read non-character events, or ignore them, call
689 `read-event' or `read-char-exclusive' instead.
691 If the optional argument PROMPT is non-nil, display that as a prompt.
692 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
693 input method is turned on in the current buffer, that input method
694 is used for reading a character.
695 If the optional argument SECONDS is non-nil, it should be a number
696 specifying the maximum number of seconds to wait for input. If no
697 input arrives in that time, return nil. SECONDS may be a
698 floating-point value. */)
699 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
701 Lisp_Object val;
703 if (! NILP (prompt))
704 message_with_string ("%s", prompt, 0);
705 val = read_filtered_event (1, 1, 1, ! NILP (inherit_input_method), seconds);
707 return (NILP (val) ? Qnil
708 : make_number (char_resolve_modifier_mask (XINT (val))));
711 DEFUN ("read-event", Fread_event, Sread_event, 0, 3, 0,
712 doc: /* Read an event object from the input stream.
713 If the optional argument PROMPT is non-nil, display that as a prompt.
714 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
715 input method is turned on in the current buffer, that input method
716 is used for reading a character.
717 If the optional argument SECONDS is non-nil, it should be a number
718 specifying the maximum number of seconds to wait for input. If no
719 input arrives in that time, return nil. SECONDS may be a
720 floating-point value. */)
721 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
723 if (! NILP (prompt))
724 message_with_string ("%s", prompt, 0);
725 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method), seconds);
728 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 3, 0,
729 doc: /* Read a character from the command input (keyboard or macro).
730 It is returned as a number. Non-character events are ignored.
731 If the character has modifiers, they are resolved and reflected to the
732 character code if possible (e.g. C-SPC -> 0).
734 If the optional argument PROMPT is non-nil, display that as a prompt.
735 If the optional argument INHERIT-INPUT-METHOD is non-nil and some
736 input method is turned on in the current buffer, that input method
737 is used for reading a character.
738 If the optional argument SECONDS is non-nil, it should be a number
739 specifying the maximum number of seconds to wait for input. If no
740 input arrives in that time, return nil. SECONDS may be a
741 floating-point value. */)
742 (Lisp_Object prompt, Lisp_Object inherit_input_method, Lisp_Object seconds)
744 Lisp_Object val;
746 if (! NILP (prompt))
747 message_with_string ("%s", prompt, 0);
749 val = read_filtered_event (1, 1, 0, ! NILP (inherit_input_method), seconds);
751 return (NILP (val) ? Qnil
752 : make_number (char_resolve_modifier_mask (XINT (val))));
755 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
756 doc: /* Don't use this yourself. */)
757 (void)
759 register Lisp_Object val;
760 block_input ();
761 XSETINT (val, getc (instream));
762 unblock_input ();
763 return val;
769 /* Return true if the lisp code read using READCHARFUN defines a non-nil
770 `lexical-binding' file variable. After returning, the stream is
771 positioned following the first line, if it is a comment or #! line,
772 otherwise nothing is read. */
774 static bool
775 lisp_file_lexically_bound_p (Lisp_Object readcharfun)
777 int ch = READCHAR;
779 if (ch == '#')
781 ch = READCHAR;
782 if (ch != '!')
784 UNREAD (ch);
785 UNREAD ('#');
786 return 0;
788 while (ch != '\n' && ch != EOF)
789 ch = READCHAR;
790 if (ch == '\n') ch = READCHAR;
791 /* It is OK to leave the position after a #! line, since
792 that is what read1 does. */
795 if (ch != ';')
796 /* The first line isn't a comment, just give up. */
798 UNREAD (ch);
799 return 0;
801 else
802 /* Look for an appropriate file-variable in the first line. */
804 bool rv = 0;
805 enum {
806 NOMINAL, AFTER_FIRST_DASH, AFTER_ASTERIX
807 } beg_end_state = NOMINAL;
808 bool in_file_vars = 0;
810 #define UPDATE_BEG_END_STATE(ch) \
811 if (beg_end_state == NOMINAL) \
812 beg_end_state = (ch == '-' ? AFTER_FIRST_DASH : NOMINAL); \
813 else if (beg_end_state == AFTER_FIRST_DASH) \
814 beg_end_state = (ch == '*' ? AFTER_ASTERIX : NOMINAL); \
815 else if (beg_end_state == AFTER_ASTERIX) \
817 if (ch == '-') \
818 in_file_vars = !in_file_vars; \
819 beg_end_state = NOMINAL; \
822 /* Skip until we get to the file vars, if any. */
825 ch = READCHAR;
826 UPDATE_BEG_END_STATE (ch);
828 while (!in_file_vars && ch != '\n' && ch != EOF);
830 while (in_file_vars)
832 char var[100], val[100];
833 unsigned i;
835 ch = READCHAR;
837 /* Read a variable name. */
838 while (ch == ' ' || ch == '\t')
839 ch = READCHAR;
841 i = 0;
842 while (ch != ':' && ch != '\n' && ch != EOF && in_file_vars)
844 if (i < sizeof var - 1)
845 var[i++] = ch;
846 UPDATE_BEG_END_STATE (ch);
847 ch = READCHAR;
850 /* Stop scanning if no colon was found before end marker. */
851 if (!in_file_vars || ch == '\n' || ch == EOF)
852 break;
854 while (i > 0 && (var[i - 1] == ' ' || var[i - 1] == '\t'))
855 i--;
856 var[i] = '\0';
858 if (ch == ':')
860 /* Read a variable value. */
861 ch = READCHAR;
863 while (ch == ' ' || ch == '\t')
864 ch = READCHAR;
866 i = 0;
867 while (ch != ';' && ch != '\n' && ch != EOF && in_file_vars)
869 if (i < sizeof val - 1)
870 val[i++] = ch;
871 UPDATE_BEG_END_STATE (ch);
872 ch = READCHAR;
874 if (! in_file_vars)
875 /* The value was terminated by an end-marker, which remove. */
876 i -= 3;
877 while (i > 0 && (val[i - 1] == ' ' || val[i - 1] == '\t'))
878 i--;
879 val[i] = '\0';
881 if (strcmp (var, "lexical-binding") == 0)
882 /* This is it... */
884 rv = (strcmp (val, "nil") != 0);
885 break;
890 while (ch != '\n' && ch != EOF)
891 ch = READCHAR;
893 return rv;
897 /* Value is a version number of byte compiled code if the file
898 associated with file descriptor FD is a compiled Lisp file that's
899 safe to load. Only files compiled with Emacs are safe to load.
900 Files compiled with XEmacs can lead to a crash in Fbyte_code
901 because of an incompatible change in the byte compiler. */
903 static int
904 safe_to_load_version (int fd)
906 char buf[512];
907 int nbytes, i;
908 int version = 1;
910 /* Read the first few bytes from the file, and look for a line
911 specifying the byte compiler version used. */
912 nbytes = emacs_read (fd, buf, sizeof buf);
913 if (nbytes > 0)
915 /* Skip to the next newline, skipping over the initial `ELC'
916 with NUL bytes following it, but note the version. */
917 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
918 if (i == 4)
919 version = buf[i];
921 if (i >= nbytes
922 || fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
923 buf + i, nbytes - i) < 0)
924 version = 0;
927 lseek (fd, 0, SEEK_SET);
928 return version;
932 /* Callback for record_unwind_protect. Restore the old load list OLD,
933 after loading a file successfully. */
935 static void
936 record_load_unwind (Lisp_Object old)
938 Vloads_in_progress = old;
941 /* This handler function is used via internal_condition_case_1. */
943 static Lisp_Object
944 load_error_handler (Lisp_Object data)
946 return Qnil;
949 static void
950 load_warn_old_style_backquotes (Lisp_Object file)
952 if (!NILP (Vold_style_backquotes))
954 AUTO_STRING (format, "Loading `%s': old-style backquotes detected!");
955 CALLN (Fmessage, format, file);
959 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
960 doc: /* Return the suffixes that `load' should try if a suffix is \
961 required.
962 This uses the variables `load-suffixes' and `load-file-rep-suffixes'. */)
963 (void)
965 Lisp_Object lst = Qnil, suffixes = Vload_suffixes, suffix, ext;
966 while (CONSP (suffixes))
968 Lisp_Object exts = Vload_file_rep_suffixes;
969 suffix = XCAR (suffixes);
970 suffixes = XCDR (suffixes);
971 while (CONSP (exts))
973 ext = XCAR (exts);
974 exts = XCDR (exts);
975 lst = Fcons (concat2 (suffix, ext), lst);
978 return Fnreverse (lst);
981 /* Returns true if STRING ends with SUFFIX */
982 static bool
983 suffix_p (Lisp_Object string, const char *suffix)
985 ptrdiff_t suffix_len = strlen (suffix);
986 ptrdiff_t string_len = SBYTES (string);
988 return string_len >= suffix_len && !strcmp (SSDATA (string) + string_len - suffix_len, suffix);
991 DEFUN ("load", Fload, Sload, 1, 5, 0,
992 doc: /* Execute a file of Lisp code named FILE.
993 First try FILE with `.elc' appended, then try with `.el', then try
994 with a system-dependent suffix of dynamic modules (see `load-suffixes'),
995 then try FILE unmodified (the exact suffixes in the exact order are
996 determined by `load-suffixes'). Environment variable references in
997 FILE are replaced with their values by calling `substitute-in-file-name'.
998 This function searches the directories in `load-path'.
1000 If optional second arg NOERROR is non-nil,
1001 report no error if FILE doesn't exist.
1002 Print messages at start and end of loading unless
1003 optional third arg NOMESSAGE is non-nil (but `force-load-messages'
1004 overrides that).
1005 If optional fourth arg NOSUFFIX is non-nil, don't try adding
1006 suffixes to the specified name FILE.
1007 If optional fifth arg MUST-SUFFIX is non-nil, insist on
1008 the suffix `.elc' or `.el' or the module suffix; don't accept just
1009 FILE unless it ends in one of those suffixes or includes a directory name.
1011 If NOSUFFIX is nil, then if a file could not be found, try looking for
1012 a different representation of the file by adding non-empty suffixes to
1013 its name, before trying another file. Emacs uses this feature to find
1014 compressed versions of files when Auto Compression mode is enabled.
1015 If NOSUFFIX is non-nil, disable this feature.
1017 The suffixes that this function tries out, when NOSUFFIX is nil, are
1018 given by the return value of `get-load-suffixes' and the values listed
1019 in `load-file-rep-suffixes'. If MUST-SUFFIX is non-nil, only the
1020 return value of `get-load-suffixes' is used, i.e. the file name is
1021 required to have a non-empty suffix.
1023 When searching suffixes, this function normally stops at the first
1024 one that exists. If the option `load-prefer-newer' is non-nil,
1025 however, it tries all suffixes, and uses whichever file is the newest.
1027 Loading a file records its definitions, and its `provide' and
1028 `require' calls, in an element of `load-history' whose
1029 car is the file name loaded. See `load-history'.
1031 While the file is in the process of being loaded, the variable
1032 `load-in-progress' is non-nil and the variable `load-file-name'
1033 is bound to the file's name.
1035 Return t if the file exists and loads successfully. */)
1036 (Lisp_Object file, Lisp_Object noerror, Lisp_Object nomessage,
1037 Lisp_Object nosuffix, Lisp_Object must_suffix)
1039 FILE *stream;
1040 int fd;
1041 int fd_index UNINIT;
1042 ptrdiff_t count = SPECPDL_INDEX ();
1043 Lisp_Object found, efound, hist_file_name;
1044 /* True means we printed the ".el is newer" message. */
1045 bool newer = 0;
1046 /* True means we are loading a compiled file. */
1047 bool compiled = 0;
1048 Lisp_Object handler;
1049 bool safe_p = 1;
1050 const char *fmode = "r" FOPEN_TEXT;
1051 int version;
1053 CHECK_STRING (file);
1055 /* If file name is magic, call the handler. */
1056 /* This shouldn't be necessary any more now that `openp' handles it right.
1057 handler = Ffind_file_name_handler (file, Qload);
1058 if (!NILP (handler))
1059 return call5 (handler, Qload, file, noerror, nomessage, nosuffix); */
1061 /* The presence of this call is the result of a historical accident:
1062 it used to be in every file-operation and when it got removed
1063 everywhere, it accidentally stayed here. Since then, enough people
1064 supposedly have things like (load "$PROJECT/foo.el") in their .emacs
1065 that it seemed risky to remove. */
1066 if (! NILP (noerror))
1068 file = internal_condition_case_1 (Fsubstitute_in_file_name, file,
1069 Qt, load_error_handler);
1070 if (NILP (file))
1071 return Qnil;
1073 else
1074 file = Fsubstitute_in_file_name (file);
1076 /* Avoid weird lossage with null string as arg,
1077 since it would try to load a directory as a Lisp file. */
1078 if (SCHARS (file) == 0)
1080 fd = -1;
1081 errno = ENOENT;
1083 else
1085 Lisp_Object suffixes;
1086 found = Qnil;
1088 if (! NILP (must_suffix))
1090 /* Don't insist on adding a suffix if FILE already ends with one. */
1091 if (suffix_p (file, ".el")
1092 || suffix_p (file, ".elc")
1093 #ifdef HAVE_MODULES
1094 || suffix_p (file, MODULES_SUFFIX)
1095 #endif
1097 must_suffix = Qnil;
1098 /* Don't insist on adding a suffix
1099 if the argument includes a directory name. */
1100 else if (! NILP (Ffile_name_directory (file)))
1101 must_suffix = Qnil;
1104 if (!NILP (nosuffix))
1105 suffixes = Qnil;
1106 else
1108 suffixes = Fget_load_suffixes ();
1109 if (NILP (must_suffix))
1110 suffixes = CALLN (Fappend, suffixes, Vload_file_rep_suffixes);
1113 fd = openp (Vload_path, file, suffixes, &found, Qnil, load_prefer_newer);
1116 if (fd == -1)
1118 if (NILP (noerror))
1119 report_file_error ("Cannot open load file", file);
1120 return Qnil;
1123 /* Tell startup.el whether or not we found the user's init file. */
1124 if (EQ (Qt, Vuser_init_file))
1125 Vuser_init_file = found;
1127 /* If FD is -2, that means openp found a magic file. */
1128 if (fd == -2)
1130 if (NILP (Fequal (found, file)))
1131 /* If FOUND is a different file name from FILE,
1132 find its handler even if we have already inhibited
1133 the `load' operation on FILE. */
1134 handler = Ffind_file_name_handler (found, Qt);
1135 else
1136 handler = Ffind_file_name_handler (found, Qload);
1137 if (! NILP (handler))
1138 return call5 (handler, Qload, found, noerror, nomessage, Qt);
1139 #ifdef DOS_NT
1140 /* Tramp has to deal with semi-broken packages that prepend
1141 drive letters to remote files. For that reason, Tramp
1142 catches file operations that test for file existence, which
1143 makes openp think X:/foo.elc files are remote. However,
1144 Tramp does not catch `load' operations for such files, so we
1145 end up with a nil as the `load' handler above. If we would
1146 continue with fd = -2, we will behave wrongly, and in
1147 particular try reading a .elc file in the "rt" mode instead
1148 of "rb". See bug #9311 for the results. To work around
1149 this, we try to open the file locally, and go with that if it
1150 succeeds. */
1151 fd = emacs_open (SSDATA (ENCODE_FILE (found)), O_RDONLY, 0);
1152 if (fd == -1)
1153 fd = -2;
1154 #endif
1157 if (0 <= fd)
1159 fd_index = SPECPDL_INDEX ();
1160 record_unwind_protect_int (close_file_unwind, fd);
1163 #ifdef HAVE_MODULES
1164 if (suffix_p (found, MODULES_SUFFIX))
1165 return unbind_to (count, Fmodule_load (found));
1166 #endif
1168 /* Check if we're stuck in a recursive load cycle.
1170 2000-09-21: It's not possible to just check for the file loaded
1171 being a member of Vloads_in_progress. This fails because of the
1172 way the byte compiler currently works; `provide's are not
1173 evaluated, see font-lock.el/jit-lock.el as an example. This
1174 leads to a certain amount of ``normal'' recursion.
1176 Also, just loading a file recursively is not always an error in
1177 the general case; the second load may do something different. */
1179 int load_count = 0;
1180 Lisp_Object tem;
1181 for (tem = Vloads_in_progress; CONSP (tem); tem = XCDR (tem))
1182 if (!NILP (Fequal (found, XCAR (tem))) && (++load_count > 3))
1183 signal_error ("Recursive load", Fcons (found, Vloads_in_progress));
1184 record_unwind_protect (record_load_unwind, Vloads_in_progress);
1185 Vloads_in_progress = Fcons (found, Vloads_in_progress);
1188 /* All loads are by default dynamic, unless the file itself specifies
1189 otherwise using a file-variable in the first line. This is bound here
1190 so that it takes effect whether or not we use
1191 Vload_source_file_function. */
1192 specbind (Qlexical_binding, Qnil);
1194 /* Get the name for load-history. */
1195 hist_file_name = (! NILP (Vpurify_flag)
1196 ? concat2 (Ffile_name_directory (file),
1197 Ffile_name_nondirectory (found))
1198 : found) ;
1200 version = -1;
1202 /* Check for the presence of old-style quotes and warn about them. */
1203 specbind (Qold_style_backquotes, Qnil);
1204 record_unwind_protect (load_warn_old_style_backquotes, file);
1206 int is_elc;
1207 if ((is_elc = suffix_p (found, ".elc")) != 0
1208 /* version = 1 means the file is empty, in which case we can
1209 treat it as not byte-compiled. */
1210 || (fd >= 0 && (version = safe_to_load_version (fd)) > 1))
1211 /* Load .elc files directly, but not when they are
1212 remote and have no handler! */
1214 if (fd != -2)
1216 struct stat s1, s2;
1217 int result;
1219 if (version < 0
1220 && ! (version = safe_to_load_version (fd)))
1222 safe_p = 0;
1223 if (!load_dangerous_libraries)
1224 error ("File `%s' was not compiled in Emacs", SDATA (found));
1225 else if (!NILP (nomessage) && !force_load_messages)
1226 message_with_string ("File `%s' not compiled in Emacs", found, 1);
1229 compiled = 1;
1231 efound = ENCODE_FILE (found);
1232 fmode = "r" FOPEN_BINARY;
1234 /* openp already checked for newness, no point doing it again.
1235 FIXME would be nice to get a message when openp
1236 ignores suffix order due to load_prefer_newer. */
1237 if (!load_prefer_newer && is_elc)
1239 result = stat (SSDATA (efound), &s1);
1240 if (result == 0)
1242 SSET (efound, SBYTES (efound) - 1, 0);
1243 result = stat (SSDATA (efound), &s2);
1244 SSET (efound, SBYTES (efound) - 1, 'c');
1247 if (result == 0
1248 && timespec_cmp (get_stat_mtime (&s1), get_stat_mtime (&s2)) < 0)
1250 /* Make the progress messages mention that source is newer. */
1251 newer = 1;
1253 /* If we won't print another message, mention this anyway. */
1254 if (!NILP (nomessage) && !force_load_messages)
1256 Lisp_Object msg_file;
1257 msg_file = Fsubstring (found, make_number (0), make_number (-1));
1258 message_with_string ("Source file `%s' newer than byte-compiled file",
1259 msg_file, 1);
1262 } /* !load_prefer_newer */
1265 else
1267 /* We are loading a source file (*.el). */
1268 if (!NILP (Vload_source_file_function))
1270 Lisp_Object val;
1272 if (fd >= 0)
1274 emacs_close (fd);
1275 clear_unwind_protect (fd_index);
1277 val = call4 (Vload_source_file_function, found, hist_file_name,
1278 NILP (noerror) ? Qnil : Qt,
1279 (NILP (nomessage) || force_load_messages) ? Qnil : Qt);
1280 return unbind_to (count, val);
1284 if (fd < 0)
1286 /* We somehow got here with fd == -2, meaning the file is deemed
1287 to be remote. Don't even try to reopen the file locally;
1288 just force a failure. */
1289 stream = NULL;
1290 errno = EINVAL;
1292 else
1294 #ifdef WINDOWSNT
1295 emacs_close (fd);
1296 clear_unwind_protect (fd_index);
1297 efound = ENCODE_FILE (found);
1298 stream = emacs_fopen (SSDATA (efound), fmode);
1299 #else
1300 stream = fdopen (fd, fmode);
1301 #endif
1303 if (! stream)
1304 report_file_error ("Opening stdio stream", file);
1305 set_unwind_protect_ptr (fd_index, fclose_unwind, stream);
1307 if (! NILP (Vpurify_flag))
1308 Vpreloaded_file_list = Fcons (Fpurecopy (file), Vpreloaded_file_list);
1310 if (NILP (nomessage) || force_load_messages)
1312 if (!safe_p)
1313 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
1314 file, 1);
1315 else if (!compiled)
1316 message_with_string ("Loading %s (source)...", file, 1);
1317 else if (newer)
1318 message_with_string ("Loading %s (compiled; note, source file is newer)...",
1319 file, 1);
1320 else /* The typical case; compiled file newer than source file. */
1321 message_with_string ("Loading %s...", file, 1);
1324 specbind (Qload_file_name, found);
1325 specbind (Qinhibit_file_name_operation, Qnil);
1326 specbind (Qload_in_progress, Qt);
1328 instream = stream;
1329 if (lisp_file_lexically_bound_p (Qget_file_char))
1330 Fset (Qlexical_binding, Qt);
1332 if (! version || version >= 22)
1333 readevalloop (Qget_file_char, stream, hist_file_name,
1334 0, Qnil, Qnil, Qnil, Qnil);
1335 else
1337 /* We can't handle a file which was compiled with
1338 byte-compile-dynamic by older version of Emacs. */
1339 specbind (Qload_force_doc_strings, Qt);
1340 readevalloop (Qget_emacs_mule_file_char, stream, hist_file_name,
1341 0, Qnil, Qnil, Qnil, Qnil);
1343 unbind_to (count, Qnil);
1345 /* Run any eval-after-load forms for this file. */
1346 if (!NILP (Ffboundp (Qdo_after_load_evaluation)))
1347 call1 (Qdo_after_load_evaluation, hist_file_name) ;
1349 xfree (saved_doc_string);
1350 saved_doc_string = 0;
1351 saved_doc_string_size = 0;
1353 xfree (prev_saved_doc_string);
1354 prev_saved_doc_string = 0;
1355 prev_saved_doc_string_size = 0;
1357 if (!noninteractive && (NILP (nomessage) || force_load_messages))
1359 if (!safe_p)
1360 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
1361 file, 1);
1362 else if (!compiled)
1363 message_with_string ("Loading %s (source)...done", file, 1);
1364 else if (newer)
1365 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
1366 file, 1);
1367 else /* The typical case; compiled file newer than source file. */
1368 message_with_string ("Loading %s...done", file, 1);
1371 return Qt;
1374 static bool
1375 complete_filename_p (Lisp_Object pathname)
1377 const unsigned char *s = SDATA (pathname);
1378 return (IS_DIRECTORY_SEP (s[0])
1379 || (SCHARS (pathname) > 2
1380 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2])));
1383 DEFUN ("locate-file-internal", Flocate_file_internal, Slocate_file_internal, 2, 4, 0,
1384 doc: /* Search for FILENAME through PATH.
1385 Returns the file's name in absolute form, or nil if not found.
1386 If SUFFIXES is non-nil, it should be a list of suffixes to append to
1387 file name when searching.
1388 If non-nil, PREDICATE is used instead of `file-readable-p'.
1389 PREDICATE can also be an integer to pass to the faccessat(2) function,
1390 in which case file-name-handlers are ignored.
1391 This function will normally skip directories, so if you want it to find
1392 directories, make sure the PREDICATE function returns `dir-ok' for them. */)
1393 (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
1395 Lisp_Object file;
1396 int fd = openp (path, filename, suffixes, &file, predicate, false);
1397 if (NILP (predicate) && fd >= 0)
1398 emacs_close (fd);
1399 return file;
1402 /* Search for a file whose name is STR, looking in directories
1403 in the Lisp list PATH, and trying suffixes from SUFFIX.
1404 On success, return a file descriptor (or 1 or -2 as described below).
1405 On failure, return -1 and set errno.
1407 SUFFIXES is a list of strings containing possible suffixes.
1408 The empty suffix is automatically added if the list is empty.
1410 PREDICATE t means the files are binary.
1411 PREDICATE non-nil and non-t means don't open the files,
1412 just look for one that satisfies the predicate. In this case,
1413 return 1 on success. The predicate can be a lisp function or
1414 an integer to pass to `access' (in which case file-name-handlers
1415 are ignored).
1417 If STOREPTR is nonzero, it points to a slot where the name of
1418 the file actually found should be stored as a Lisp string.
1419 nil is stored there on failure.
1421 If the file we find is remote, return -2
1422 but store the found remote file name in *STOREPTR.
1424 If NEWER is true, try all SUFFIXes and return the result for the
1425 newest file that exists. Does not apply to remote files,
1426 or if a non-nil and non-t PREDICATE is specified. */
1429 openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
1430 Lisp_Object *storeptr, Lisp_Object predicate, bool newer)
1432 ptrdiff_t fn_size = 100;
1433 char buf[100];
1434 char *fn = buf;
1435 bool absolute;
1436 ptrdiff_t want_length;
1437 Lisp_Object filename;
1438 Lisp_Object string, tail, encoded_fn, save_string;
1439 ptrdiff_t max_suffix_len = 0;
1440 int last_errno = ENOENT;
1441 int save_fd = -1;
1442 USE_SAFE_ALLOCA;
1444 /* The last-modified time of the newest matching file found.
1445 Initialize it to something less than all valid timestamps. */
1446 struct timespec save_mtime = make_timespec (TYPE_MINIMUM (time_t), -1);
1448 CHECK_STRING (str);
1450 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
1452 CHECK_STRING_CAR (tail);
1453 max_suffix_len = max (max_suffix_len,
1454 SBYTES (XCAR (tail)));
1457 string = filename = encoded_fn = save_string = Qnil;
1459 if (storeptr)
1460 *storeptr = Qnil;
1462 absolute = complete_filename_p (str);
1464 for (; CONSP (path); path = XCDR (path))
1466 ptrdiff_t baselen, prefixlen;
1468 filename = Fexpand_file_name (str, XCAR (path));
1469 if (!complete_filename_p (filename))
1470 /* If there are non-absolute elts in PATH (eg "."). */
1471 /* Of course, this could conceivably lose if luser sets
1472 default-directory to be something non-absolute... */
1474 filename = Fexpand_file_name (filename, BVAR (current_buffer, directory));
1475 if (!complete_filename_p (filename))
1476 /* Give up on this path element! */
1477 continue;
1480 /* Calculate maximum length of any filename made from
1481 this path element/specified file name and any possible suffix. */
1482 want_length = max_suffix_len + SBYTES (filename);
1483 if (fn_size <= want_length)
1485 fn_size = 100 + want_length;
1486 fn = SAFE_ALLOCA (fn_size);
1489 /* Copy FILENAME's data to FN but remove starting /: if any. */
1490 prefixlen = ((SCHARS (filename) > 2
1491 && SREF (filename, 0) == '/'
1492 && SREF (filename, 1) == ':')
1493 ? 2 : 0);
1494 baselen = SBYTES (filename) - prefixlen;
1495 memcpy (fn, SDATA (filename) + prefixlen, baselen);
1497 /* Loop over suffixes. */
1498 for (tail = NILP (suffixes) ? list1 (empty_unibyte_string) : suffixes;
1499 CONSP (tail); tail = XCDR (tail))
1501 Lisp_Object suffix = XCAR (tail);
1502 ptrdiff_t fnlen, lsuffix = SBYTES (suffix);
1503 Lisp_Object handler;
1505 /* Make complete filename by appending SUFFIX. */
1506 memcpy (fn + baselen, SDATA (suffix), lsuffix + 1);
1507 fnlen = baselen + lsuffix;
1509 /* Check that the file exists and is not a directory. */
1510 /* We used to only check for handlers on non-absolute file names:
1511 if (absolute)
1512 handler = Qnil;
1513 else
1514 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1515 It's not clear why that was the case and it breaks things like
1516 (load "/bar.el") where the file is actually "/bar.el.gz". */
1517 /* make_string has its own ideas on when to return a unibyte
1518 string and when a multibyte string, but we know better.
1519 We must have a unibyte string when dumping, since
1520 file-name encoding is shaky at best at that time, and in
1521 particular default-file-name-coding-system is reset
1522 several times during loadup. We therefore don't want to
1523 encode the file before passing it to file I/O library
1524 functions. */
1525 if (!STRING_MULTIBYTE (filename) && !STRING_MULTIBYTE (suffix))
1526 string = make_unibyte_string (fn, fnlen);
1527 else
1528 string = make_string (fn, fnlen);
1529 handler = Ffind_file_name_handler (string, Qfile_exists_p);
1530 if ((!NILP (handler) || (!NILP (predicate) && !EQ (predicate, Qt)))
1531 && !NATNUMP (predicate))
1533 bool exists;
1534 if (NILP (predicate) || EQ (predicate, Qt))
1535 exists = !NILP (Ffile_readable_p (string));
1536 else
1538 Lisp_Object tmp = call1 (predicate, string);
1539 if (NILP (tmp))
1540 exists = false;
1541 else if (EQ (tmp, Qdir_ok)
1542 || NILP (Ffile_directory_p (string)))
1543 exists = true;
1544 else
1546 exists = false;
1547 last_errno = EISDIR;
1551 if (exists)
1553 /* We succeeded; return this descriptor and filename. */
1554 if (storeptr)
1555 *storeptr = string;
1556 SAFE_FREE ();
1557 return -2;
1560 else
1562 int fd;
1563 const char *pfn;
1564 struct stat st;
1566 encoded_fn = ENCODE_FILE (string);
1567 pfn = SSDATA (encoded_fn);
1569 /* Check that we can access or open it. */
1570 if (NATNUMP (predicate))
1572 fd = -1;
1573 if (INT_MAX < XFASTINT (predicate))
1574 last_errno = EINVAL;
1575 else if (faccessat (AT_FDCWD, pfn, XFASTINT (predicate),
1576 AT_EACCESS)
1577 == 0)
1579 if (file_directory_p (pfn))
1580 last_errno = EISDIR;
1581 else
1582 fd = 1;
1585 else
1587 fd = emacs_open (pfn, O_RDONLY, 0);
1588 if (fd < 0)
1590 if (errno != ENOENT)
1591 last_errno = errno;
1593 else
1595 int err = (fstat (fd, &st) != 0 ? errno
1596 : S_ISDIR (st.st_mode) ? EISDIR : 0);
1597 if (err)
1599 last_errno = err;
1600 emacs_close (fd);
1601 fd = -1;
1606 if (fd >= 0)
1608 if (newer && !NATNUMP (predicate))
1610 struct timespec mtime = get_stat_mtime (&st);
1612 if (timespec_cmp (mtime, save_mtime) <= 0)
1613 emacs_close (fd);
1614 else
1616 if (0 <= save_fd)
1617 emacs_close (save_fd);
1618 save_fd = fd;
1619 save_mtime = mtime;
1620 save_string = string;
1623 else
1625 /* We succeeded; return this descriptor and filename. */
1626 if (storeptr)
1627 *storeptr = string;
1628 SAFE_FREE ();
1629 return fd;
1633 /* No more suffixes. Return the newest. */
1634 if (0 <= save_fd && ! CONSP (XCDR (tail)))
1636 if (storeptr)
1637 *storeptr = save_string;
1638 SAFE_FREE ();
1639 return save_fd;
1643 if (absolute)
1644 break;
1647 SAFE_FREE ();
1648 errno = last_errno;
1649 return -1;
1653 /* Merge the list we've accumulated of globals from the current input source
1654 into the load_history variable. The details depend on whether
1655 the source has an associated file name or not.
1657 FILENAME is the file name that we are loading from.
1659 ENTIRE is true if loading that entire file, false if evaluating
1660 part of it. */
1662 static void
1663 build_load_history (Lisp_Object filename, bool entire)
1665 Lisp_Object tail, prev, newelt;
1666 Lisp_Object tem, tem2;
1667 bool foundit = 0;
1669 tail = Vload_history;
1670 prev = Qnil;
1672 while (CONSP (tail))
1674 tem = XCAR (tail);
1676 /* Find the feature's previous assoc list... */
1677 if (!NILP (Fequal (filename, Fcar (tem))))
1679 foundit = 1;
1681 /* If we're loading the entire file, remove old data. */
1682 if (entire)
1684 if (NILP (prev))
1685 Vload_history = XCDR (tail);
1686 else
1687 Fsetcdr (prev, XCDR (tail));
1690 /* Otherwise, cons on new symbols that are not already members. */
1691 else
1693 tem2 = Vcurrent_load_list;
1695 while (CONSP (tem2))
1697 newelt = XCAR (tem2);
1699 if (NILP (Fmember (newelt, tem)))
1700 Fsetcar (tail, Fcons (XCAR (tem),
1701 Fcons (newelt, XCDR (tem))));
1703 tem2 = XCDR (tem2);
1704 QUIT;
1708 else
1709 prev = tail;
1710 tail = XCDR (tail);
1711 QUIT;
1714 /* If we're loading an entire file, cons the new assoc onto the
1715 front of load-history, the most-recently-loaded position. Also
1716 do this if we didn't find an existing member for the file. */
1717 if (entire || !foundit)
1718 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1719 Vload_history);
1722 static void
1723 readevalloop_1 (int old)
1725 load_convert_to_unibyte = old;
1728 /* Signal an `end-of-file' error, if possible with file name
1729 information. */
1731 static _Noreturn void
1732 end_of_file_error (void)
1734 if (STRINGP (Vload_file_name))
1735 xsignal1 (Qend_of_file, Vload_file_name);
1737 xsignal0 (Qend_of_file);
1740 static Lisp_Object
1741 readevalloop_eager_expand_eval (Lisp_Object val, Lisp_Object macroexpand)
1743 /* If we macroexpand the toplevel form non-recursively and it ends
1744 up being a `progn' (or if it was a progn to start), treat each
1745 form in the progn as a top-level form. This way, if one form in
1746 the progn defines a macro, that macro is in effect when we expand
1747 the remaining forms. See similar code in bytecomp.el. */
1748 val = call2 (macroexpand, val, Qnil);
1749 if (EQ (CAR_SAFE (val), Qprogn))
1751 Lisp_Object subforms = XCDR (val);
1753 for (val = Qnil; CONSP (subforms); subforms = XCDR (subforms))
1754 val = readevalloop_eager_expand_eval (XCAR (subforms),
1755 macroexpand);
1757 else
1758 val = eval_sub (call2 (macroexpand, val, Qt));
1759 return val;
1762 /* UNIBYTE specifies how to set load_convert_to_unibyte
1763 for this invocation.
1764 READFUN, if non-nil, is used instead of `read'.
1766 START, END specify region to read in current buffer (from eval-region).
1767 If the input is not from a buffer, they must be nil. */
1769 static void
1770 readevalloop (Lisp_Object readcharfun,
1771 FILE *stream,
1772 Lisp_Object sourcename,
1773 bool printflag,
1774 Lisp_Object unibyte, Lisp_Object readfun,
1775 Lisp_Object start, Lisp_Object end)
1777 int c;
1778 Lisp_Object val;
1779 ptrdiff_t count = SPECPDL_INDEX ();
1780 struct buffer *b = 0;
1781 bool continue_reading_p;
1782 Lisp_Object lex_bound;
1783 /* True if reading an entire buffer. */
1784 bool whole_buffer = 0;
1785 /* True on the first time around. */
1786 bool first_sexp = 1;
1787 Lisp_Object macroexpand = intern ("internal-macroexpand-for-load");
1789 if (NILP (Ffboundp (macroexpand))
1790 /* Don't macroexpand in .elc files, since it should have been done
1791 already. We actually don't know whether we're in a .elc file or not,
1792 so we use circumstantial evidence: .el files normally go through
1793 Vload_source_file_function -> load-with-code-conversion
1794 -> eval-buffer. */
1795 || EQ (readcharfun, Qget_file_char)
1796 || EQ (readcharfun, Qget_emacs_mule_file_char))
1797 macroexpand = Qnil;
1799 if (MARKERP (readcharfun))
1801 if (NILP (start))
1802 start = readcharfun;
1805 if (BUFFERP (readcharfun))
1806 b = XBUFFER (readcharfun);
1807 else if (MARKERP (readcharfun))
1808 b = XMARKER (readcharfun)->buffer;
1810 /* We assume START is nil when input is not from a buffer. */
1811 if (! NILP (start) && !b)
1812 emacs_abort ();
1814 specbind (Qstandard_input, readcharfun);
1815 specbind (Qcurrent_load_list, Qnil);
1816 record_unwind_protect_int (readevalloop_1, load_convert_to_unibyte);
1817 load_convert_to_unibyte = !NILP (unibyte);
1819 /* If lexical binding is active (either because it was specified in
1820 the file's header, or via a buffer-local variable), create an empty
1821 lexical environment, otherwise, turn off lexical binding. */
1822 lex_bound = find_symbol_value (Qlexical_binding);
1823 specbind (Qinternal_interpreter_environment,
1824 (NILP (lex_bound) || EQ (lex_bound, Qunbound)
1825 ? Qnil : list1 (Qt)));
1827 /* Try to ensure sourcename is a truename, except whilst preloading. */
1828 if (NILP (Vpurify_flag)
1829 && !NILP (sourcename) && !NILP (Ffile_name_absolute_p (sourcename))
1830 && !NILP (Ffboundp (Qfile_truename)))
1831 sourcename = call1 (Qfile_truename, sourcename) ;
1833 LOADHIST_ATTACH (sourcename);
1835 continue_reading_p = 1;
1836 while (continue_reading_p)
1838 ptrdiff_t count1 = SPECPDL_INDEX ();
1840 if (b != 0 && !BUFFER_LIVE_P (b))
1841 error ("Reading from killed buffer");
1843 if (!NILP (start))
1845 /* Switch to the buffer we are reading from. */
1846 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1847 set_buffer_internal (b);
1849 /* Save point in it. */
1850 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1851 /* Save ZV in it. */
1852 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1853 /* Those get unbound after we read one expression. */
1855 /* Set point and ZV around stuff to be read. */
1856 Fgoto_char (start);
1857 if (!NILP (end))
1858 Fnarrow_to_region (make_number (BEGV), end);
1860 /* Just for cleanliness, convert END to a marker
1861 if it is an integer. */
1862 if (INTEGERP (end))
1863 end = Fpoint_max_marker ();
1866 /* On the first cycle, we can easily test here
1867 whether we are reading the whole buffer. */
1868 if (b && first_sexp)
1869 whole_buffer = (PT == BEG && ZV == Z);
1871 instream = stream;
1872 read_next:
1873 c = READCHAR;
1874 if (c == ';')
1876 while ((c = READCHAR) != '\n' && c != -1);
1877 goto read_next;
1879 if (c < 0)
1881 unbind_to (count1, Qnil);
1882 break;
1885 /* Ignore whitespace here, so we can detect eof. */
1886 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
1887 || c == NO_BREAK_SPACE)
1888 goto read_next;
1890 if (!NILP (Vpurify_flag) && c == '(')
1892 val = read_list (0, readcharfun);
1894 else
1896 UNREAD (c);
1897 read_objects = Qnil;
1898 if (!NILP (readfun))
1900 val = call1 (readfun, readcharfun);
1902 /* If READCHARFUN has set point to ZV, we should
1903 stop reading, even if the form read sets point
1904 to a different value when evaluated. */
1905 if (BUFFERP (readcharfun))
1907 struct buffer *buf = XBUFFER (readcharfun);
1908 if (BUF_PT (buf) == BUF_ZV (buf))
1909 continue_reading_p = 0;
1912 else if (! NILP (Vload_read_function))
1913 val = call1 (Vload_read_function, readcharfun);
1914 else
1915 val = read_internal_start (readcharfun, Qnil, Qnil);
1918 if (!NILP (start) && continue_reading_p)
1919 start = Fpoint_marker ();
1921 /* Restore saved point and BEGV. */
1922 unbind_to (count1, Qnil);
1924 /* Now eval what we just read. */
1925 if (!NILP (macroexpand))
1926 val = readevalloop_eager_expand_eval (val, macroexpand);
1927 else
1928 val = eval_sub (val);
1930 if (printflag)
1932 Vvalues = Fcons (val, Vvalues);
1933 if (EQ (Vstandard_output, Qt))
1934 Fprin1 (val, Qnil);
1935 else
1936 Fprint (val, Qnil);
1939 first_sexp = 0;
1942 build_load_history (sourcename,
1943 stream || whole_buffer);
1945 unbind_to (count, Qnil);
1948 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1949 doc: /* Execute the accessible portion of current buffer as Lisp code.
1950 You can use \\[narrow-to-region] to limit the part of buffer to be evaluated.
1951 When called from a Lisp program (i.e., not interactively), this
1952 function accepts up to five optional arguments:
1953 BUFFER is the buffer to evaluate (nil means use current buffer),
1954 or a name of a buffer (a string).
1955 PRINTFLAG controls printing of output by any output functions in the
1956 evaluated code, such as `print', `princ', and `prin1':
1957 a value of nil means discard it; anything else is the stream to print to.
1958 See Info node `(elisp)Output Streams' for details on streams.
1959 FILENAME specifies the file name to use for `load-history'.
1960 UNIBYTE, if non-nil, specifies `load-convert-to-unibyte' for this
1961 invocation.
1962 DO-ALLOW-PRINT, if non-nil, specifies that output functions in the
1963 evaluated code should work normally even if PRINTFLAG is nil, in
1964 which case the output is displayed in the echo area.
1966 This function preserves the position of point. */)
1967 (Lisp_Object buffer, Lisp_Object printflag, Lisp_Object filename, Lisp_Object unibyte, Lisp_Object do_allow_print)
1969 ptrdiff_t count = SPECPDL_INDEX ();
1970 Lisp_Object tem, buf;
1972 if (NILP (buffer))
1973 buf = Fcurrent_buffer ();
1974 else
1975 buf = Fget_buffer (buffer);
1976 if (NILP (buf))
1977 error ("No such buffer");
1979 if (NILP (printflag) && NILP (do_allow_print))
1980 tem = Qsymbolp;
1981 else
1982 tem = printflag;
1984 if (NILP (filename))
1985 filename = BVAR (XBUFFER (buf), filename);
1987 specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
1988 specbind (Qstandard_output, tem);
1989 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1990 BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1991 specbind (Qlexical_binding, lisp_file_lexically_bound_p (buf) ? Qt : Qnil);
1992 readevalloop (buf, 0, filename,
1993 !NILP (printflag), unibyte, Qnil, Qnil, Qnil);
1994 unbind_to (count, Qnil);
1996 return Qnil;
1999 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
2000 doc: /* Execute the region as Lisp code.
2001 When called from programs, expects two arguments,
2002 giving starting and ending indices in the current buffer
2003 of the text to be executed.
2004 Programs can pass third argument PRINTFLAG which controls output:
2005 a value of nil means discard it; anything else is stream for printing it.
2006 See Info node `(elisp)Output Streams' for details on streams.
2007 Also the fourth argument READ-FUNCTION, if non-nil, is used
2008 instead of `read' to read each expression. It gets one argument
2009 which is the input stream for reading characters.
2011 This function does not move point. */)
2012 (Lisp_Object start, Lisp_Object end, Lisp_Object printflag, Lisp_Object read_function)
2014 /* FIXME: Do the eval-sexp-add-defvars dance! */
2015 ptrdiff_t count = SPECPDL_INDEX ();
2016 Lisp_Object tem, cbuf;
2018 cbuf = Fcurrent_buffer ();
2020 if (NILP (printflag))
2021 tem = Qsymbolp;
2022 else
2023 tem = printflag;
2024 specbind (Qstandard_output, tem);
2025 specbind (Qeval_buffer_list, Fcons (cbuf, Veval_buffer_list));
2027 /* `readevalloop' calls functions which check the type of start and end. */
2028 readevalloop (cbuf, 0, BVAR (XBUFFER (cbuf), filename),
2029 !NILP (printflag), Qnil, read_function,
2030 start, end);
2032 return unbind_to (count, Qnil);
2036 DEFUN ("read", Fread, Sread, 0, 1, 0,
2037 doc: /* Read one Lisp expression as text from STREAM, return as Lisp object.
2038 If STREAM is nil, use the value of `standard-input' (which see).
2039 STREAM or the value of `standard-input' may be:
2040 a buffer (read from point and advance it)
2041 a marker (read from where it points and advance it)
2042 a function (call it with no arguments for each character,
2043 call it with a char as argument to push a char back)
2044 a string (takes text from string, starting at the beginning)
2045 t (read text line using minibuffer and use it, or read from
2046 standard input in batch mode). */)
2047 (Lisp_Object stream)
2049 if (NILP (stream))
2050 stream = Vstandard_input;
2051 if (EQ (stream, Qt))
2052 stream = Qread_char;
2053 if (EQ (stream, Qread_char))
2054 /* FIXME: ?! When is this used !? */
2055 return call1 (intern ("read-minibuffer"),
2056 build_string ("Lisp expression: "));
2058 return read_internal_start (stream, Qnil, Qnil);
2061 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
2062 doc: /* Read one Lisp expression which is represented as text by STRING.
2063 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
2064 FINAL-STRING-INDEX is an integer giving the position of the next
2065 remaining character in STRING. START and END optionally delimit
2066 a substring of STRING from which to read; they default to 0 and
2067 \(length STRING) respectively. Negative values are counted from
2068 the end of STRING. */)
2069 (Lisp_Object string, Lisp_Object start, Lisp_Object end)
2071 Lisp_Object ret;
2072 CHECK_STRING (string);
2073 /* `read_internal_start' sets `read_from_string_index'. */
2074 ret = read_internal_start (string, start, end);
2075 return Fcons (ret, make_number (read_from_string_index));
2078 /* Function to set up the global context we need in toplevel read
2079 calls. START and END only used when STREAM is a string. */
2080 static Lisp_Object
2081 read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
2083 Lisp_Object retval;
2085 readchar_count = 0;
2086 new_backquote_flag = 0;
2087 read_objects = Qnil;
2088 if (EQ (Vread_with_symbol_positions, Qt)
2089 || EQ (Vread_with_symbol_positions, stream))
2090 Vread_symbol_positions_list = Qnil;
2092 if (STRINGP (stream)
2093 || ((CONSP (stream) && STRINGP (XCAR (stream)))))
2095 ptrdiff_t startval, endval;
2096 Lisp_Object string;
2098 if (STRINGP (stream))
2099 string = stream;
2100 else
2101 string = XCAR (stream);
2103 validate_subarray (string, start, end, SCHARS (string),
2104 &startval, &endval);
2106 read_from_string_index = startval;
2107 read_from_string_index_byte = string_char_to_byte (string, startval);
2108 read_from_string_limit = endval;
2111 retval = read0 (stream);
2112 if (EQ (Vread_with_symbol_positions, Qt)
2113 || EQ (Vread_with_symbol_positions, stream))
2114 Vread_symbol_positions_list = Fnreverse (Vread_symbol_positions_list);
2115 return retval;
2119 /* Signal Qinvalid_read_syntax error.
2120 S is error string of length N (if > 0) */
2122 static _Noreturn void
2123 invalid_syntax (const char *s)
2125 xsignal1 (Qinvalid_read_syntax, build_string (s));
2129 /* Use this for recursive reads, in contexts where internal tokens
2130 are not allowed. */
2132 static Lisp_Object
2133 read0 (Lisp_Object readcharfun)
2135 register Lisp_Object val;
2136 int c;
2138 val = read1 (readcharfun, &c, 0);
2139 if (!c)
2140 return val;
2142 xsignal1 (Qinvalid_read_syntax,
2143 Fmake_string (make_number (1), make_number (c)));
2146 static ptrdiff_t read_buffer_size;
2147 static char *read_buffer;
2149 /* Grow the read buffer by at least MAX_MULTIBYTE_LENGTH bytes. */
2151 static void
2152 grow_read_buffer (void)
2154 read_buffer = xpalloc (read_buffer, &read_buffer_size,
2155 MAX_MULTIBYTE_LENGTH, -1, 1);
2158 /* Return the scalar value that has the Unicode character name NAME.
2159 Raise 'invalid-read-syntax' if there is no such character. */
2160 static int
2161 character_name_to_code (char const *name, ptrdiff_t name_len)
2163 /* For "U+XXXX", pass the leading '+' to string_to_number to reject
2164 monstrosities like "U+-0000". */
2165 Lisp_Object code
2166 = (name[0] == 'U' && name[1] == '+'
2167 ? string_to_number (name + 1, 16, false)
2168 : call2 (Qchar_from_name, make_unibyte_string (name, name_len), Qt));
2170 if (! RANGED_INTEGERP (0, code, MAX_UNICODE_CHAR)
2171 || char_surrogate_p (XINT (code)))
2173 AUTO_STRING (format, "\\N{%s}");
2174 AUTO_STRING_WITH_LEN (namestr, name, name_len);
2175 xsignal1 (Qinvalid_read_syntax, CALLN (Fformat, format, namestr));
2178 return XINT (code);
2181 /* Bound on the length of a Unicode character name. As of
2182 Unicode 9.0.0 the maximum is 83, so this should be safe. */
2183 enum { UNICODE_CHARACTER_NAME_LENGTH_BOUND = 200 };
2185 /* Read a \-escape sequence, assuming we already read the `\'.
2186 If the escape sequence forces unibyte, return eight-bit char. */
2188 static int
2189 read_escape (Lisp_Object readcharfun, bool stringp)
2191 int c = READCHAR;
2192 /* \u allows up to four hex digits, \U up to eight. Default to the
2193 behavior for \u, and change this value in the case that \U is seen. */
2194 int unicode_hex_count = 4;
2196 switch (c)
2198 case -1:
2199 end_of_file_error ();
2201 case 'a':
2202 return '\007';
2203 case 'b':
2204 return '\b';
2205 case 'd':
2206 return 0177;
2207 case 'e':
2208 return 033;
2209 case 'f':
2210 return '\f';
2211 case 'n':
2212 return '\n';
2213 case 'r':
2214 return '\r';
2215 case 't':
2216 return '\t';
2217 case 'v':
2218 return '\v';
2219 case '\n':
2220 return -1;
2221 case ' ':
2222 if (stringp)
2223 return -1;
2224 return ' ';
2226 case 'M':
2227 c = READCHAR;
2228 if (c != '-')
2229 error ("Invalid escape character syntax");
2230 c = READCHAR;
2231 if (c == '\\')
2232 c = read_escape (readcharfun, 0);
2233 return c | meta_modifier;
2235 case 'S':
2236 c = READCHAR;
2237 if (c != '-')
2238 error ("Invalid escape character syntax");
2239 c = READCHAR;
2240 if (c == '\\')
2241 c = read_escape (readcharfun, 0);
2242 return c | shift_modifier;
2244 case 'H':
2245 c = READCHAR;
2246 if (c != '-')
2247 error ("Invalid escape character syntax");
2248 c = READCHAR;
2249 if (c == '\\')
2250 c = read_escape (readcharfun, 0);
2251 return c | hyper_modifier;
2253 case 'A':
2254 c = READCHAR;
2255 if (c != '-')
2256 error ("Invalid escape character syntax");
2257 c = READCHAR;
2258 if (c == '\\')
2259 c = read_escape (readcharfun, 0);
2260 return c | alt_modifier;
2262 case 's':
2263 c = READCHAR;
2264 if (stringp || c != '-')
2266 UNREAD (c);
2267 return ' ';
2269 c = READCHAR;
2270 if (c == '\\')
2271 c = read_escape (readcharfun, 0);
2272 return c | super_modifier;
2274 case 'C':
2275 c = READCHAR;
2276 if (c != '-')
2277 error ("Invalid escape character syntax");
2278 case '^':
2279 c = READCHAR;
2280 if (c == '\\')
2281 c = read_escape (readcharfun, 0);
2282 if ((c & ~CHAR_MODIFIER_MASK) == '?')
2283 return 0177 | (c & CHAR_MODIFIER_MASK);
2284 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2285 return c | ctrl_modifier;
2286 /* ASCII control chars are made from letters (both cases),
2287 as well as the non-letters within 0100...0137. */
2288 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
2289 return (c & (037 | ~0177));
2290 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
2291 return (c & (037 | ~0177));
2292 else
2293 return c | ctrl_modifier;
2295 case '0':
2296 case '1':
2297 case '2':
2298 case '3':
2299 case '4':
2300 case '5':
2301 case '6':
2302 case '7':
2303 /* An octal escape, as in ANSI C. */
2305 register int i = c - '0';
2306 register int count = 0;
2307 while (++count < 3)
2309 if ((c = READCHAR) >= '0' && c <= '7')
2311 i *= 8;
2312 i += c - '0';
2314 else
2316 UNREAD (c);
2317 break;
2321 if (i >= 0x80 && i < 0x100)
2322 i = BYTE8_TO_CHAR (i);
2323 return i;
2326 case 'x':
2327 /* A hex escape, as in ANSI C. */
2329 unsigned int i = 0;
2330 int count = 0;
2331 while (1)
2333 c = READCHAR;
2334 if (c >= '0' && c <= '9')
2336 i *= 16;
2337 i += c - '0';
2339 else if ((c >= 'a' && c <= 'f')
2340 || (c >= 'A' && c <= 'F'))
2342 i *= 16;
2343 if (c >= 'a' && c <= 'f')
2344 i += c - 'a' + 10;
2345 else
2346 i += c - 'A' + 10;
2348 else
2350 UNREAD (c);
2351 break;
2353 /* Allow hex escapes as large as ?\xfffffff, because some
2354 packages use them to denote characters with modifiers. */
2355 if ((CHAR_META | (CHAR_META - 1)) < i)
2356 error ("Hex character out of range: \\x%x...", i);
2357 count += count < 3;
2360 if (count < 3 && i >= 0x80)
2361 return BYTE8_TO_CHAR (i);
2362 return i;
2365 case 'U':
2366 /* Post-Unicode-2.0: Up to eight hex chars. */
2367 unicode_hex_count = 8;
2368 case 'u':
2370 /* A Unicode escape. We only permit them in strings and characters,
2371 not arbitrarily in the source code, as in some other languages. */
2373 unsigned int i = 0;
2374 int count = 0;
2376 while (++count <= unicode_hex_count)
2378 c = READCHAR;
2379 /* `isdigit' and `isalpha' may be locale-specific, which we don't
2380 want. */
2381 if (c >= '0' && c <= '9') i = (i << 4) + (c - '0');
2382 else if (c >= 'a' && c <= 'f') i = (i << 4) + (c - 'a') + 10;
2383 else if (c >= 'A' && c <= 'F') i = (i << 4) + (c - 'A') + 10;
2384 else
2385 error ("Non-hex digit used for Unicode escape");
2387 if (i > 0x10FFFF)
2388 error ("Non-Unicode character: 0x%x", i);
2389 return i;
2392 case 'N':
2393 /* Named character. */
2395 c = READCHAR;
2396 if (c != '{')
2397 invalid_syntax ("Expected opening brace after \\N");
2398 char name[UNICODE_CHARACTER_NAME_LENGTH_BOUND + 1];
2399 bool whitespace = false;
2400 ptrdiff_t length = 0;
2401 while (true)
2403 c = READCHAR;
2404 if (c < 0)
2405 end_of_file_error ();
2406 if (c == '}')
2407 break;
2408 if (! (0 < c && c < 0x80))
2410 AUTO_STRING (format,
2411 "Invalid character U+%04X in character name");
2412 xsignal1 (Qinvalid_read_syntax,
2413 CALLN (Fformat, format, make_natnum (c)));
2415 /* Treat multiple adjacent whitespace characters as a
2416 single space character. This makes it easier to use
2417 character names in e.g. multi-line strings. */
2418 if (c_isspace (c))
2420 if (whitespace)
2421 continue;
2422 c = ' ';
2423 whitespace = true;
2425 else
2426 whitespace = false;
2427 name[length++] = c;
2428 if (length >= sizeof name)
2429 invalid_syntax ("Character name too long");
2431 if (length == 0)
2432 invalid_syntax ("Empty character name");
2433 name[length] = '\0';
2434 return character_name_to_code (name, length);
2437 default:
2438 return c;
2442 /* Return the digit that CHARACTER stands for in the given BASE.
2443 Return -1 if CHARACTER is out of range for BASE,
2444 and -2 if CHARACTER is not valid for any supported BASE. */
2445 static int
2446 digit_to_number (int character, int base)
2448 int digit;
2450 if ('0' <= character && character <= '9')
2451 digit = character - '0';
2452 else if ('a' <= character && character <= 'z')
2453 digit = character - 'a' + 10;
2454 else if ('A' <= character && character <= 'Z')
2455 digit = character - 'A' + 10;
2456 else
2457 return -2;
2459 return digit < base ? digit : -1;
2462 /* Read an integer in radix RADIX using READCHARFUN to read
2463 characters. RADIX must be in the interval [2..36]; if it isn't, a
2464 read error is signaled . Value is the integer read. Signals an
2465 error if encountering invalid read syntax or if RADIX is out of
2466 range. */
2468 static Lisp_Object
2469 read_integer (Lisp_Object readcharfun, EMACS_INT radix)
2471 /* Room for sign, leading 0, other digits, trailing null byte.
2472 Also, room for invalid syntax diagnostic. */
2473 char buf[max (1 + 1 + UINTMAX_WIDTH + 1,
2474 sizeof "integer, radix " + INT_STRLEN_BOUND (EMACS_INT))];
2476 int valid = -1; /* 1 if valid, 0 if not, -1 if incomplete. */
2478 if (radix < 2 || radix > 36)
2479 valid = 0;
2480 else
2482 char *p = buf;
2483 int c, digit;
2485 c = READCHAR;
2486 if (c == '-' || c == '+')
2488 *p++ = c;
2489 c = READCHAR;
2492 if (c == '0')
2494 *p++ = c;
2495 valid = 1;
2497 /* Ignore redundant leading zeros, so the buffer doesn't
2498 fill up with them. */
2500 c = READCHAR;
2501 while (c == '0');
2504 while ((digit = digit_to_number (c, radix)) >= -1)
2506 if (digit == -1)
2507 valid = 0;
2508 if (valid < 0)
2509 valid = 1;
2511 if (p < buf + sizeof buf - 1)
2512 *p++ = c;
2513 else
2514 valid = 0;
2516 c = READCHAR;
2519 UNREAD (c);
2520 *p = '\0';
2523 if (! valid)
2525 sprintf (buf, "integer, radix %"pI"d", radix);
2526 invalid_syntax (buf);
2529 return string_to_number (buf, radix, 0);
2533 /* If the next token is ')' or ']' or '.', we store that character
2534 in *PCH and the return value is not interesting. Else, we store
2535 zero in *PCH and we read and return one lisp object.
2537 FIRST_IN_LIST is true if this is the first element of a list. */
2539 static Lisp_Object
2540 read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
2542 int c;
2543 bool uninterned_symbol = 0;
2544 bool multibyte;
2546 *pch = 0;
2548 retry:
2550 c = READCHAR_REPORT_MULTIBYTE (&multibyte);
2551 if (c < 0)
2552 end_of_file_error ();
2554 switch (c)
2556 case '(':
2557 return read_list (0, readcharfun);
2559 case '[':
2560 return read_vector (readcharfun, 0);
2562 case ')':
2563 case ']':
2565 *pch = c;
2566 return Qnil;
2569 case '#':
2570 c = READCHAR;
2571 if (c == 's')
2573 c = READCHAR;
2574 if (c == '(')
2576 /* Accept extended format for hashtables (extensible to
2577 other types), e.g.
2578 #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */
2579 Lisp_Object tmp = read_list (0, readcharfun);
2580 Lisp_Object head = CAR_SAFE (tmp);
2581 Lisp_Object data = Qnil;
2582 Lisp_Object val = Qnil;
2583 /* The size is 2 * number of allowed keywords to
2584 make-hash-table. */
2585 Lisp_Object params[10];
2586 Lisp_Object ht;
2587 Lisp_Object key = Qnil;
2588 int param_count = 0;
2590 if (!EQ (head, Qhash_table))
2591 error ("Invalid extended read marker at head of #s list "
2592 "(only hash-table allowed)");
2594 tmp = CDR_SAFE (tmp);
2596 /* This is repetitive but fast and simple. */
2597 params[param_count] = QCsize;
2598 params[param_count + 1] = Fplist_get (tmp, Qsize);
2599 if (!NILP (params[param_count + 1]))
2600 param_count += 2;
2602 params[param_count] = QCtest;
2603 params[param_count + 1] = Fplist_get (tmp, Qtest);
2604 if (!NILP (params[param_count + 1]))
2605 param_count += 2;
2607 params[param_count] = QCweakness;
2608 params[param_count + 1] = Fplist_get (tmp, Qweakness);
2609 if (!NILP (params[param_count + 1]))
2610 param_count += 2;
2612 params[param_count] = QCrehash_size;
2613 params[param_count + 1] = Fplist_get (tmp, Qrehash_size);
2614 if (!NILP (params[param_count + 1]))
2615 param_count += 2;
2617 params[param_count] = QCrehash_threshold;
2618 params[param_count + 1] = Fplist_get (tmp, Qrehash_threshold);
2619 if (!NILP (params[param_count + 1]))
2620 param_count += 2;
2622 /* This is the hashtable data. */
2623 data = Fplist_get (tmp, Qdata);
2625 /* Now use params to make a new hashtable and fill it. */
2626 ht = Fmake_hash_table (param_count, params);
2628 while (CONSP (data))
2630 key = XCAR (data);
2631 data = XCDR (data);
2632 if (!CONSP (data))
2633 error ("Odd number of elements in hashtable data");
2634 val = XCAR (data);
2635 data = XCDR (data);
2636 Fputhash (key, val, ht);
2639 return ht;
2641 UNREAD (c);
2642 invalid_syntax ("#");
2644 if (c == '^')
2646 c = READCHAR;
2647 if (c == '[')
2649 Lisp_Object tmp;
2650 tmp = read_vector (readcharfun, 0);
2651 if (ASIZE (tmp) < CHAR_TABLE_STANDARD_SLOTS)
2652 error ("Invalid size char-table");
2653 XSETPVECTYPE (XVECTOR (tmp), PVEC_CHAR_TABLE);
2654 return tmp;
2656 else if (c == '^')
2658 c = READCHAR;
2659 if (c == '[')
2661 /* Sub char-table can't be read as a regular
2662 vector because of a two C integer fields. */
2663 Lisp_Object tbl, tmp = read_list (1, readcharfun);
2664 ptrdiff_t size = XINT (Flength (tmp));
2665 int i, depth, min_char;
2666 struct Lisp_Cons *cell;
2668 if (size == 0)
2669 error ("Zero-sized sub char-table");
2671 if (! RANGED_INTEGERP (1, XCAR (tmp), 3))
2672 error ("Invalid depth in sub char-table");
2673 depth = XINT (XCAR (tmp));
2674 if (chartab_size[depth] != size - 2)
2675 error ("Invalid size in sub char-table");
2676 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2677 free_cons (cell);
2679 if (! RANGED_INTEGERP (0, XCAR (tmp), MAX_CHAR))
2680 error ("Invalid minimum character in sub-char-table");
2681 min_char = XINT (XCAR (tmp));
2682 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
2683 free_cons (cell);
2685 tbl = make_uninit_sub_char_table (depth, min_char);
2686 for (i = 0; i < size; i++)
2688 XSUB_CHAR_TABLE (tbl)->contents[i] = XCAR (tmp);
2689 cell = XCONS (tmp), tmp = XCDR (tmp);
2690 free_cons (cell);
2692 return tbl;
2694 invalid_syntax ("#^^");
2696 invalid_syntax ("#^");
2698 if (c == '&')
2700 Lisp_Object length;
2701 length = read1 (readcharfun, pch, first_in_list);
2702 c = READCHAR;
2703 if (c == '"')
2705 Lisp_Object tmp, val;
2706 EMACS_INT size_in_chars = bool_vector_bytes (XFASTINT (length));
2707 unsigned char *data;
2709 UNREAD (c);
2710 tmp = read1 (readcharfun, pch, first_in_list);
2711 if (STRING_MULTIBYTE (tmp)
2712 || (size_in_chars != SCHARS (tmp)
2713 /* We used to print 1 char too many
2714 when the number of bits was a multiple of 8.
2715 Accept such input in case it came from an old
2716 version. */
2717 && ! (XFASTINT (length)
2718 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2719 invalid_syntax ("#&...");
2721 val = make_uninit_bool_vector (XFASTINT (length));
2722 data = bool_vector_uchar_data (val);
2723 memcpy (data, SDATA (tmp), size_in_chars);
2724 /* Clear the extraneous bits in the last byte. */
2725 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2726 data[size_in_chars - 1]
2727 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2728 return val;
2730 invalid_syntax ("#&...");
2732 if (c == '[')
2734 /* Accept compiled functions at read-time so that we don't have to
2735 build them using function calls. */
2736 Lisp_Object tmp;
2737 struct Lisp_Vector *vec;
2738 tmp = read_vector (readcharfun, 1);
2739 vec = XVECTOR (tmp);
2740 if (vec->header.size == 0)
2741 invalid_syntax ("Empty byte-code object");
2742 make_byte_code (vec);
2743 return tmp;
2745 if (c == '(')
2747 Lisp_Object tmp;
2748 int ch;
2750 /* Read the string itself. */
2751 tmp = read1 (readcharfun, &ch, 0);
2752 if (ch != 0 || !STRINGP (tmp))
2753 invalid_syntax ("#");
2754 /* Read the intervals and their properties. */
2755 while (1)
2757 Lisp_Object beg, end, plist;
2759 beg = read1 (readcharfun, &ch, 0);
2760 end = plist = Qnil;
2761 if (ch == ')')
2762 break;
2763 if (ch == 0)
2764 end = read1 (readcharfun, &ch, 0);
2765 if (ch == 0)
2766 plist = read1 (readcharfun, &ch, 0);
2767 if (ch)
2768 invalid_syntax ("Invalid string property list");
2769 Fset_text_properties (beg, end, plist, tmp);
2772 return tmp;
2775 /* #@NUMBER is used to skip NUMBER following bytes.
2776 That's used in .elc files to skip over doc strings
2777 and function definitions. */
2778 if (c == '@')
2780 enum { extra = 100 };
2781 ptrdiff_t i, nskip = 0, digits = 0;
2783 /* Read a decimal integer. */
2784 while ((c = READCHAR) >= 0
2785 && c >= '0' && c <= '9')
2787 if ((STRING_BYTES_BOUND - extra) / 10 <= nskip)
2788 string_overflow ();
2789 digits++;
2790 nskip *= 10;
2791 nskip += c - '0';
2792 if (digits == 2 && nskip == 0)
2793 { /* We've just seen #@00, which means "skip to end". */
2794 skip_dyn_eof (readcharfun);
2795 return Qnil;
2798 if (nskip > 0)
2799 /* We can't use UNREAD here, because in the code below we side-step
2800 READCHAR. Instead, assume the first char after #@NNN occupies
2801 a single byte, which is the case normally since it's just
2802 a space. */
2803 nskip--;
2804 else
2805 UNREAD (c);
2807 if (load_force_doc_strings
2808 && (FROM_FILE_P (readcharfun)))
2810 /* If we are supposed to force doc strings into core right now,
2811 record the last string that we skipped,
2812 and record where in the file it comes from. */
2814 /* But first exchange saved_doc_string
2815 with prev_saved_doc_string, so we save two strings. */
2817 char *temp = saved_doc_string;
2818 ptrdiff_t temp_size = saved_doc_string_size;
2819 file_offset temp_pos = saved_doc_string_position;
2820 ptrdiff_t temp_len = saved_doc_string_length;
2822 saved_doc_string = prev_saved_doc_string;
2823 saved_doc_string_size = prev_saved_doc_string_size;
2824 saved_doc_string_position = prev_saved_doc_string_position;
2825 saved_doc_string_length = prev_saved_doc_string_length;
2827 prev_saved_doc_string = temp;
2828 prev_saved_doc_string_size = temp_size;
2829 prev_saved_doc_string_position = temp_pos;
2830 prev_saved_doc_string_length = temp_len;
2833 if (saved_doc_string_size == 0)
2835 saved_doc_string = xmalloc (nskip + extra);
2836 saved_doc_string_size = nskip + extra;
2838 if (nskip > saved_doc_string_size)
2840 saved_doc_string = xrealloc (saved_doc_string, nskip + extra);
2841 saved_doc_string_size = nskip + extra;
2844 saved_doc_string_position = file_tell (instream);
2846 /* Copy that many characters into saved_doc_string. */
2847 block_input ();
2848 for (i = 0; i < nskip && c >= 0; i++)
2849 saved_doc_string[i] = c = getc (instream);
2850 unblock_input ();
2852 saved_doc_string_length = i;
2854 else
2855 /* Skip that many bytes. */
2856 skip_dyn_bytes (readcharfun, nskip);
2858 goto retry;
2860 if (c == '!')
2862 /* #! appears at the beginning of an executable file.
2863 Skip the first line. */
2864 while (c != '\n' && c >= 0)
2865 c = READCHAR;
2866 goto retry;
2868 if (c == '$')
2869 return Vload_file_name;
2870 if (c == '\'')
2871 return list2 (Qfunction, read0 (readcharfun));
2872 /* #:foo is the uninterned symbol named foo. */
2873 if (c == ':')
2875 uninterned_symbol = 1;
2876 c = READCHAR;
2877 if (!(c > 040
2878 && c != NO_BREAK_SPACE
2879 && (c >= 0200
2880 || strchr ("\"';()[]#`,", c) == NULL)))
2882 /* No symbol character follows, this is the empty
2883 symbol. */
2884 UNREAD (c);
2885 return Fmake_symbol (empty_unibyte_string);
2887 goto read_symbol;
2889 /* ## is the empty symbol. */
2890 if (c == '#')
2891 return Fintern (empty_unibyte_string, Qnil);
2892 /* Reader forms that can reuse previously read objects. */
2893 if (c >= '0' && c <= '9')
2895 EMACS_INT n = 0;
2896 Lisp_Object tem;
2898 /* Read a non-negative integer. */
2899 while (c >= '0' && c <= '9')
2901 if (MOST_POSITIVE_FIXNUM / 10 < n
2902 || MOST_POSITIVE_FIXNUM < n * 10 + c - '0')
2903 n = MOST_POSITIVE_FIXNUM + 1;
2904 else
2905 n = n * 10 + c - '0';
2906 c = READCHAR;
2909 if (n <= MOST_POSITIVE_FIXNUM)
2911 if (c == 'r' || c == 'R')
2912 return read_integer (readcharfun, n);
2914 if (! NILP (Vread_circle))
2916 /* #n=object returns object, but associates it with
2917 n for #n#. */
2918 if (c == '=')
2920 /* Make a placeholder for #n# to use temporarily. */
2921 AUTO_CONS (placeholder, Qnil, Qnil);
2922 Lisp_Object cell = Fcons (make_number (n), placeholder);
2923 read_objects = Fcons (cell, read_objects);
2925 /* Read the object itself. */
2926 tem = read0 (readcharfun);
2928 /* Now put it everywhere the placeholder was... */
2929 substitute_object_in_subtree (tem, placeholder);
2931 /* ...and #n# will use the real value from now on. */
2932 Fsetcdr (cell, tem);
2934 return tem;
2937 /* #n# returns a previously read object. */
2938 if (c == '#')
2940 tem = Fassq (make_number (n), read_objects);
2941 if (CONSP (tem))
2942 return XCDR (tem);
2946 /* Fall through to error message. */
2948 else if (c == 'x' || c == 'X')
2949 return read_integer (readcharfun, 16);
2950 else if (c == 'o' || c == 'O')
2951 return read_integer (readcharfun, 8);
2952 else if (c == 'b' || c == 'B')
2953 return read_integer (readcharfun, 2);
2955 UNREAD (c);
2956 invalid_syntax ("#");
2958 case ';':
2959 while ((c = READCHAR) >= 0 && c != '\n');
2960 goto retry;
2962 case '\'':
2963 return list2 (Qquote, read0 (readcharfun));
2965 case '`':
2967 int next_char = READCHAR;
2968 UNREAD (next_char);
2969 /* Transition from old-style to new-style:
2970 If we see "(`" it used to mean old-style, which usually works
2971 fine because ` should almost never appear in such a position
2972 for new-style. But occasionally we need "(`" to mean new
2973 style, so we try to distinguish the two by the fact that we
2974 can either write "( `foo" or "(` foo", where the first
2975 intends to use new-style whereas the second intends to use
2976 old-style. For Emacs-25, we should completely remove this
2977 first_in_list exception (old-style can still be obtained via
2978 "(\`" anyway). */
2979 if (!new_backquote_flag && first_in_list && next_char == ' ')
2981 Vold_style_backquotes = Qt;
2982 goto default_label;
2984 else
2986 Lisp_Object value;
2987 bool saved_new_backquote_flag = new_backquote_flag;
2989 new_backquote_flag = 1;
2990 value = read0 (readcharfun);
2991 new_backquote_flag = saved_new_backquote_flag;
2993 return list2 (Qbackquote, value);
2996 case ',':
2998 int next_char = READCHAR;
2999 UNREAD (next_char);
3000 /* Transition from old-style to new-style:
3001 It used to be impossible to have a new-style , other than within
3002 a new-style `. This is sufficient when ` and , are used in the
3003 normal way, but ` and , can also appear in args to macros that
3004 will not interpret them in the usual way, in which case , may be
3005 used without any ` anywhere near.
3006 So we now use the same heuristic as for backquote: old-style
3007 unquotes are only recognized when first on a list, and when
3008 followed by a space.
3009 Because it's more difficult to peek 2 chars ahead, a new-style
3010 ,@ can still not be used outside of a `, unless it's in the middle
3011 of a list. */
3012 if (new_backquote_flag
3013 || !first_in_list
3014 || (next_char != ' ' && next_char != '@'))
3016 Lisp_Object comma_type = Qnil;
3017 Lisp_Object value;
3018 int ch = READCHAR;
3020 if (ch == '@')
3021 comma_type = Qcomma_at;
3022 else if (ch == '.')
3023 comma_type = Qcomma_dot;
3024 else
3026 if (ch >= 0) UNREAD (ch);
3027 comma_type = Qcomma;
3030 value = read0 (readcharfun);
3031 return list2 (comma_type, value);
3033 else
3035 Vold_style_backquotes = Qt;
3036 goto default_label;
3039 case '?':
3041 int modifiers;
3042 int next_char;
3043 bool ok;
3045 c = READCHAR;
3046 if (c < 0)
3047 end_of_file_error ();
3049 /* Accept `single space' syntax like (list ? x) where the
3050 whitespace character is SPC or TAB.
3051 Other literal whitespace like NL, CR, and FF are not accepted,
3052 as there are well-established escape sequences for these. */
3053 if (c == ' ' || c == '\t')
3054 return make_number (c);
3056 if (c == '\\')
3057 c = read_escape (readcharfun, 0);
3058 modifiers = c & CHAR_MODIFIER_MASK;
3059 c &= ~CHAR_MODIFIER_MASK;
3060 if (CHAR_BYTE8_P (c))
3061 c = CHAR_TO_BYTE8 (c);
3062 c |= modifiers;
3064 next_char = READCHAR;
3065 ok = (next_char <= 040
3066 || (next_char < 0200
3067 && strchr ("\"';()[]#?`,.", next_char) != NULL));
3068 UNREAD (next_char);
3069 if (ok)
3070 return make_number (c);
3072 invalid_syntax ("?");
3075 case '"':
3077 char *p = read_buffer;
3078 char *end = read_buffer + read_buffer_size;
3079 int ch;
3080 /* True if we saw an escape sequence specifying
3081 a multibyte character. */
3082 bool force_multibyte = 0;
3083 /* True if we saw an escape sequence specifying
3084 a single-byte character. */
3085 bool force_singlebyte = 0;
3086 bool cancel = 0;
3087 ptrdiff_t nchars = 0;
3089 while ((ch = READCHAR) >= 0
3090 && ch != '\"')
3092 if (end - p < MAX_MULTIBYTE_LENGTH)
3094 ptrdiff_t offset = p - read_buffer;
3095 grow_read_buffer ();
3096 p = read_buffer + offset;
3097 end = read_buffer + read_buffer_size;
3100 if (ch == '\\')
3102 int modifiers;
3104 ch = read_escape (readcharfun, 1);
3106 /* CH is -1 if \ newline or \ space has just been seen. */
3107 if (ch == -1)
3109 if (p == read_buffer)
3110 cancel = 1;
3111 continue;
3114 modifiers = ch & CHAR_MODIFIER_MASK;
3115 ch = ch & ~CHAR_MODIFIER_MASK;
3117 if (CHAR_BYTE8_P (ch))
3118 force_singlebyte = 1;
3119 else if (! ASCII_CHAR_P (ch))
3120 force_multibyte = 1;
3121 else /* I.e. ASCII_CHAR_P (ch). */
3123 /* Allow `\C- ' and `\C-?'. */
3124 if (modifiers == CHAR_CTL)
3126 if (ch == ' ')
3127 ch = 0, modifiers = 0;
3128 else if (ch == '?')
3129 ch = 127, modifiers = 0;
3131 if (modifiers & CHAR_SHIFT)
3133 /* Shift modifier is valid only with [A-Za-z]. */
3134 if (ch >= 'A' && ch <= 'Z')
3135 modifiers &= ~CHAR_SHIFT;
3136 else if (ch >= 'a' && ch <= 'z')
3137 ch -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
3140 if (modifiers & CHAR_META)
3142 /* Move the meta bit to the right place for a
3143 string. */
3144 modifiers &= ~CHAR_META;
3145 ch = BYTE8_TO_CHAR (ch | 0x80);
3146 force_singlebyte = 1;
3150 /* Any modifiers remaining are invalid. */
3151 if (modifiers)
3152 error ("Invalid modifier in string");
3153 p += CHAR_STRING (ch, (unsigned char *) p);
3155 else
3157 p += CHAR_STRING (ch, (unsigned char *) p);
3158 if (CHAR_BYTE8_P (ch))
3159 force_singlebyte = 1;
3160 else if (! ASCII_CHAR_P (ch))
3161 force_multibyte = 1;
3163 nchars++;
3166 if (ch < 0)
3167 end_of_file_error ();
3169 /* If purifying, and string starts with \ newline,
3170 return zero instead. This is for doc strings
3171 that we are really going to find in etc/DOC.nn.nn. */
3172 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
3173 return make_number (0);
3175 if (! force_multibyte && force_singlebyte)
3177 /* READ_BUFFER contains raw 8-bit bytes and no multibyte
3178 forms. Convert it to unibyte. */
3179 nchars = str_as_unibyte ((unsigned char *) read_buffer,
3180 p - read_buffer);
3181 p = read_buffer + nchars;
3184 return make_specified_string (read_buffer, nchars, p - read_buffer,
3185 (force_multibyte
3186 || (p - read_buffer != nchars)));
3189 case '.':
3191 int next_char = READCHAR;
3192 UNREAD (next_char);
3194 if (next_char <= 040
3195 || (next_char < 0200
3196 && strchr ("\"';([#?`,", next_char) != NULL))
3198 *pch = c;
3199 return Qnil;
3202 /* Otherwise, we fall through! Note that the atom-reading loop
3203 below will now loop at least once, assuring that we will not
3204 try to UNREAD two characters in a row. */
3206 default:
3207 default_label:
3208 if (c <= 040) goto retry;
3209 if (c == NO_BREAK_SPACE)
3210 goto retry;
3212 read_symbol:
3214 char *p = read_buffer;
3215 bool quoted = 0;
3216 EMACS_INT start_position = readchar_count - 1;
3219 char *end = read_buffer + read_buffer_size;
3223 if (end - p < MAX_MULTIBYTE_LENGTH)
3225 ptrdiff_t offset = p - read_buffer;
3226 grow_read_buffer ();
3227 p = read_buffer + offset;
3228 end = read_buffer + read_buffer_size;
3231 if (c == '\\')
3233 c = READCHAR;
3234 if (c == -1)
3235 end_of_file_error ();
3236 quoted = 1;
3239 if (multibyte)
3240 p += CHAR_STRING (c, (unsigned char *) p);
3241 else
3242 *p++ = c;
3243 c = READCHAR;
3245 while (c > 040
3246 && c != NO_BREAK_SPACE
3247 && (c >= 0200
3248 || strchr ("\"';()[]#`,", c) == NULL));
3250 if (p == end)
3252 ptrdiff_t offset = p - read_buffer;
3253 grow_read_buffer ();
3254 p = read_buffer + offset;
3255 end = read_buffer + read_buffer_size;
3257 *p = 0;
3258 UNREAD (c);
3261 if (!quoted && !uninterned_symbol)
3263 Lisp_Object result = string_to_number (read_buffer, 10, 0);
3264 if (! NILP (result))
3265 return result;
3268 Lisp_Object name, result;
3269 ptrdiff_t nbytes = p - read_buffer;
3270 ptrdiff_t nchars
3271 = (multibyte
3272 ? multibyte_chars_in_text ((unsigned char *) read_buffer,
3273 nbytes)
3274 : nbytes);
3276 name = ((uninterned_symbol && ! NILP (Vpurify_flag)
3277 ? make_pure_string : make_specified_string)
3278 (read_buffer, nchars, nbytes, multibyte));
3279 result = (uninterned_symbol ? Fmake_symbol (name)
3280 : Fintern (name, Qnil));
3282 if (EQ (Vread_with_symbol_positions, Qt)
3283 || EQ (Vread_with_symbol_positions, readcharfun))
3284 Vread_symbol_positions_list
3285 = Fcons (Fcons (result, make_number (start_position)),
3286 Vread_symbol_positions_list);
3287 return result;
3294 /* List of nodes we've seen during substitute_object_in_subtree. */
3295 static Lisp_Object seen_list;
3297 static void
3298 substitute_object_in_subtree (Lisp_Object object, Lisp_Object placeholder)
3300 Lisp_Object check_object;
3302 /* We haven't seen any objects when we start. */
3303 seen_list = Qnil;
3305 /* Make all the substitutions. */
3306 check_object
3307 = substitute_object_recurse (object, placeholder, object);
3309 /* Clear seen_list because we're done with it. */
3310 seen_list = Qnil;
3312 /* The returned object here is expected to always eq the
3313 original. */
3314 if (!EQ (check_object, object))
3315 error ("Unexpected mutation error in reader");
3318 /* Feval doesn't get called from here, so no gc protection is needed. */
3319 #define SUBSTITUTE(get_val, set_val) \
3320 do { \
3321 Lisp_Object old_value = get_val; \
3322 Lisp_Object true_value \
3323 = substitute_object_recurse (object, placeholder, \
3324 old_value); \
3326 if (!EQ (old_value, true_value)) \
3328 set_val; \
3330 } while (0)
3332 static Lisp_Object
3333 substitute_object_recurse (Lisp_Object object, Lisp_Object placeholder, Lisp_Object subtree)
3335 /* If we find the placeholder, return the target object. */
3336 if (EQ (placeholder, subtree))
3337 return object;
3339 /* If we've been to this node before, don't explore it again. */
3340 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
3341 return subtree;
3343 /* If this node can be the entry point to a cycle, remember that
3344 we've seen it. It can only be such an entry point if it was made
3345 by #n=, which means that we can find it as a value in
3346 read_objects. */
3347 if (!EQ (Qnil, Frassq (subtree, read_objects)))
3348 seen_list = Fcons (subtree, seen_list);
3350 /* Recurse according to subtree's type.
3351 Every branch must return a Lisp_Object. */
3352 switch (XTYPE (subtree))
3354 case Lisp_Vectorlike:
3356 ptrdiff_t i = 0, length = 0;
3357 if (BOOL_VECTOR_P (subtree))
3358 return subtree; /* No sub-objects anyway. */
3359 else if (CHAR_TABLE_P (subtree) || SUB_CHAR_TABLE_P (subtree)
3360 || COMPILEDP (subtree) || HASH_TABLE_P (subtree))
3361 length = ASIZE (subtree) & PSEUDOVECTOR_SIZE_MASK;
3362 else if (VECTORP (subtree))
3363 length = ASIZE (subtree);
3364 else
3365 /* An unknown pseudovector may contain non-Lisp fields, so we
3366 can't just blindly traverse all its fields. We used to call
3367 `Flength' which signaled `sequencep', so I just preserved this
3368 behavior. */
3369 wrong_type_argument (Qsequencep, subtree);
3371 if (SUB_CHAR_TABLE_P (subtree))
3372 i = 2;
3373 for ( ; i < length; i++)
3374 SUBSTITUTE (AREF (subtree, i),
3375 ASET (subtree, i, true_value));
3376 return subtree;
3379 case Lisp_Cons:
3381 SUBSTITUTE (XCAR (subtree),
3382 XSETCAR (subtree, true_value));
3383 SUBSTITUTE (XCDR (subtree),
3384 XSETCDR (subtree, true_value));
3385 return subtree;
3388 case Lisp_String:
3390 /* Check for text properties in each interval.
3391 substitute_in_interval contains part of the logic. */
3393 INTERVAL root_interval = string_intervals (subtree);
3394 AUTO_CONS (arg, object, placeholder);
3396 traverse_intervals_noorder (root_interval,
3397 &substitute_in_interval, arg);
3399 return subtree;
3402 /* Other types don't recurse any further. */
3403 default:
3404 return subtree;
3408 /* Helper function for substitute_object_recurse. */
3409 static void
3410 substitute_in_interval (INTERVAL interval, Lisp_Object arg)
3412 Lisp_Object object = Fcar (arg);
3413 Lisp_Object placeholder = Fcdr (arg);
3415 SUBSTITUTE (interval->plist, set_interval_plist (interval, true_value));
3419 #define LEAD_INT 1
3420 #define DOT_CHAR 2
3421 #define TRAIL_INT 4
3422 #define E_EXP 16
3425 /* Convert STRING to a number, assuming base BASE. Return a fixnum if CP has
3426 integer syntax and fits in a fixnum, else return the nearest float if CP has
3427 either floating point or integer syntax and BASE is 10, else return nil. If
3428 IGNORE_TRAILING, consider just the longest prefix of CP that has
3429 valid floating point syntax. Signal an overflow if BASE is not 10 and the
3430 number has integer syntax but does not fit. */
3432 Lisp_Object
3433 string_to_number (char const *string, int base, bool ignore_trailing)
3435 int state;
3436 char const *cp = string;
3437 int leading_digit;
3438 bool float_syntax = 0;
3439 double value = 0;
3441 /* Negate the value ourselves. This treats 0, NaNs, and infinity properly on
3442 IEEE floating point hosts, and works around a formerly-common bug where
3443 atof ("-0.0") drops the sign. */
3444 bool negative = *cp == '-';
3446 bool signedp = negative || *cp == '+';
3447 cp += signedp;
3449 state = 0;
3451 leading_digit = digit_to_number (*cp, base);
3452 if (leading_digit >= 0)
3454 state |= LEAD_INT;
3456 ++cp;
3457 while (digit_to_number (*cp, base) >= 0);
3459 if (*cp == '.')
3461 state |= DOT_CHAR;
3462 cp++;
3465 if (base == 10)
3467 if ('0' <= *cp && *cp <= '9')
3469 state |= TRAIL_INT;
3471 cp++;
3472 while ('0' <= *cp && *cp <= '9');
3474 if (*cp == 'e' || *cp == 'E')
3476 char const *ecp = cp;
3477 cp++;
3478 if (*cp == '+' || *cp == '-')
3479 cp++;
3480 if ('0' <= *cp && *cp <= '9')
3482 state |= E_EXP;
3484 cp++;
3485 while ('0' <= *cp && *cp <= '9');
3487 else if (cp[-1] == '+'
3488 && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
3490 state |= E_EXP;
3491 cp += 3;
3492 value = INFINITY;
3494 else if (cp[-1] == '+'
3495 && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
3497 state |= E_EXP;
3498 cp += 3;
3499 /* NAN is a "positive" NaN on all known Emacs hosts. */
3500 value = NAN;
3502 else
3503 cp = ecp;
3506 float_syntax = ((state & (DOT_CHAR|TRAIL_INT)) == (DOT_CHAR|TRAIL_INT)
3507 || state == (LEAD_INT|E_EXP));
3510 /* Return nil if the number uses invalid syntax. If IGNORE_TRAILING, accept
3511 any prefix that matches. Otherwise, the entire string must match. */
3512 if (! (ignore_trailing
3513 ? ((state & LEAD_INT) != 0 || float_syntax)
3514 : (!*cp && ((state & ~DOT_CHAR) == LEAD_INT || float_syntax))))
3515 return Qnil;
3517 /* If the number uses integer and not float syntax, and is in C-language
3518 range, use its value, preferably as a fixnum. */
3519 if (leading_digit >= 0 && ! float_syntax)
3521 uintmax_t n;
3523 /* Fast special case for single-digit integers. This also avoids a
3524 glitch when BASE is 16 and IGNORE_TRAILING, because in that
3525 case some versions of strtoumax accept numbers like "0x1" that Emacs
3526 does not allow. */
3527 if (digit_to_number (string[signedp + 1], base) < 0)
3528 return make_number (negative ? -leading_digit : leading_digit);
3530 errno = 0;
3531 n = strtoumax (string + signedp, NULL, base);
3532 if (errno == ERANGE)
3534 /* Unfortunately there's no simple and accurate way to convert
3535 non-base-10 numbers that are out of C-language range. */
3536 if (base != 10)
3537 xsignal1 (Qoverflow_error, build_string (string));
3539 else if (n <= (negative ? -MOST_NEGATIVE_FIXNUM : MOST_POSITIVE_FIXNUM))
3541 EMACS_INT signed_n = n;
3542 return make_number (negative ? -signed_n : signed_n);
3544 else
3545 value = n;
3548 /* Either the number uses float syntax, or it does not fit into a fixnum.
3549 Convert it from string to floating point, unless the value is already
3550 known because it is an infinity, a NAN, or its absolute value fits in
3551 uintmax_t. */
3552 if (! value)
3553 value = atof (string + signedp);
3555 return make_float (negative ? -value : value);
3559 static Lisp_Object
3560 read_vector (Lisp_Object readcharfun, bool bytecodeflag)
3562 ptrdiff_t i, size;
3563 Lisp_Object *ptr;
3564 Lisp_Object tem, item, vector;
3565 struct Lisp_Cons *otem;
3566 Lisp_Object len;
3568 tem = read_list (1, readcharfun);
3569 len = Flength (tem);
3570 vector = Fmake_vector (len, Qnil);
3572 size = ASIZE (vector);
3573 ptr = XVECTOR (vector)->contents;
3574 for (i = 0; i < size; i++)
3576 item = Fcar (tem);
3577 /* If `load-force-doc-strings' is t when reading a lazily-loaded
3578 bytecode object, the docstring containing the bytecode and
3579 constants values must be treated as unibyte and passed to
3580 Fread, to get the actual bytecode string and constants vector. */
3581 if (bytecodeflag && load_force_doc_strings)
3583 if (i == COMPILED_BYTECODE)
3585 if (!STRINGP (item))
3586 error ("Invalid byte code");
3588 /* Delay handling the bytecode slot until we know whether
3589 it is lazily-loaded (we can tell by whether the
3590 constants slot is nil). */
3591 ASET (vector, COMPILED_CONSTANTS, item);
3592 item = Qnil;
3594 else if (i == COMPILED_CONSTANTS)
3596 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
3598 if (NILP (item))
3600 /* Coerce string to unibyte (like string-as-unibyte,
3601 but without generating extra garbage and
3602 guaranteeing no change in the contents). */
3603 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
3604 STRING_SET_UNIBYTE (bytestr);
3606 item = Fread (Fcons (bytestr, readcharfun));
3607 if (!CONSP (item))
3608 error ("Invalid byte code");
3610 otem = XCONS (item);
3611 bytestr = XCAR (item);
3612 item = XCDR (item);
3613 free_cons (otem);
3616 /* Now handle the bytecode slot. */
3617 ASET (vector, COMPILED_BYTECODE, bytestr);
3619 else if (i == COMPILED_DOC_STRING
3620 && STRINGP (item)
3621 && ! STRING_MULTIBYTE (item))
3623 if (EQ (readcharfun, Qget_emacs_mule_file_char))
3624 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
3625 else
3626 item = Fstring_as_multibyte (item);
3629 ASET (vector, i, item);
3630 otem = XCONS (tem);
3631 tem = Fcdr (tem);
3632 free_cons (otem);
3634 return vector;
3637 /* FLAG means check for ']' to terminate rather than ')' and '.'. */
3639 static Lisp_Object
3640 read_list (bool flag, Lisp_Object readcharfun)
3642 Lisp_Object val, tail;
3643 Lisp_Object elt, tem;
3644 /* 0 is the normal case.
3645 1 means this list is a doc reference; replace it with the number 0.
3646 2 means this list is a doc reference; replace it with the doc string. */
3647 int doc_reference = 0;
3649 /* Initialize this to 1 if we are reading a list. */
3650 bool first_in_list = flag <= 0;
3652 val = Qnil;
3653 tail = Qnil;
3655 while (1)
3657 int ch;
3658 elt = read1 (readcharfun, &ch, first_in_list);
3660 first_in_list = 0;
3662 /* While building, if the list starts with #$, treat it specially. */
3663 if (EQ (elt, Vload_file_name)
3664 && ! NILP (elt)
3665 && !NILP (Vpurify_flag))
3667 if (NILP (Vdoc_file_name))
3668 /* We have not yet called Snarf-documentation, so assume
3669 this file is described in the DOC file
3670 and Snarf-documentation will fill in the right value later.
3671 For now, replace the whole list with 0. */
3672 doc_reference = 1;
3673 else
3674 /* We have already called Snarf-documentation, so make a relative
3675 file name for this file, so it can be found properly
3676 in the installed Lisp directory.
3677 We don't use Fexpand_file_name because that would make
3678 the directory absolute now. */
3680 AUTO_STRING (dot_dot_lisp, "../lisp/");
3681 elt = concat2 (dot_dot_lisp, Ffile_name_nondirectory (elt));
3684 else if (EQ (elt, Vload_file_name)
3685 && ! NILP (elt)
3686 && load_force_doc_strings)
3687 doc_reference = 2;
3689 if (ch)
3691 if (flag > 0)
3693 if (ch == ']')
3694 return val;
3695 invalid_syntax (") or . in a vector");
3697 if (ch == ')')
3698 return val;
3699 if (ch == '.')
3701 if (!NILP (tail))
3702 XSETCDR (tail, read0 (readcharfun));
3703 else
3704 val = read0 (readcharfun);
3705 read1 (readcharfun, &ch, 0);
3707 if (ch == ')')
3709 if (doc_reference == 1)
3710 return make_number (0);
3711 if (doc_reference == 2 && INTEGERP (XCDR (val)))
3713 char *saved = NULL;
3714 file_offset saved_position;
3715 /* Get a doc string from the file we are loading.
3716 If it's in saved_doc_string, get it from there.
3718 Here, we don't know if the string is a
3719 bytecode string or a doc string. As a
3720 bytecode string must be unibyte, we always
3721 return a unibyte string. If it is actually a
3722 doc string, caller must make it
3723 multibyte. */
3725 /* Position is negative for user variables. */
3726 EMACS_INT pos = eabs (XINT (XCDR (val)));
3727 if (pos >= saved_doc_string_position
3728 && pos < (saved_doc_string_position
3729 + saved_doc_string_length))
3731 saved = saved_doc_string;
3732 saved_position = saved_doc_string_position;
3734 /* Look in prev_saved_doc_string the same way. */
3735 else if (pos >= prev_saved_doc_string_position
3736 && pos < (prev_saved_doc_string_position
3737 + prev_saved_doc_string_length))
3739 saved = prev_saved_doc_string;
3740 saved_position = prev_saved_doc_string_position;
3742 if (saved)
3744 ptrdiff_t start = pos - saved_position;
3745 ptrdiff_t from, to;
3747 /* Process quoting with ^A,
3748 and find the end of the string,
3749 which is marked with ^_ (037). */
3750 for (from = start, to = start;
3751 saved[from] != 037;)
3753 int c = saved[from++];
3754 if (c == 1)
3756 c = saved[from++];
3757 saved[to++] = (c == 1 ? c
3758 : c == '0' ? 0
3759 : c == '_' ? 037
3760 : c);
3762 else
3763 saved[to++] = c;
3766 return make_unibyte_string (saved + start,
3767 to - start);
3769 else
3770 return get_doc_string (val, 1, 0);
3773 return val;
3775 invalid_syntax (". in wrong context");
3777 invalid_syntax ("] in a list");
3779 tem = list1 (elt);
3780 if (!NILP (tail))
3781 XSETCDR (tail, tem);
3782 else
3783 val = tem;
3784 tail = tem;
3788 static Lisp_Object initial_obarray;
3790 /* `oblookup' stores the bucket number here, for the sake of Funintern. */
3792 static size_t oblookup_last_bucket_number;
3794 /* Get an error if OBARRAY is not an obarray.
3795 If it is one, return it. */
3797 Lisp_Object
3798 check_obarray (Lisp_Object obarray)
3800 /* We don't want to signal a wrong-type-argument error when we are
3801 shutting down due to a fatal error, and we don't want to hit
3802 assertions in VECTORP and ASIZE if the fatal error was during GC. */
3803 if (!fatal_error_in_progress
3804 && (!VECTORP (obarray) || ASIZE (obarray) == 0))
3806 /* If Vobarray is now invalid, force it to be valid. */
3807 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3808 wrong_type_argument (Qvectorp, obarray);
3810 return obarray;
3813 /* Intern symbol SYM in OBARRAY using bucket INDEX. */
3815 static Lisp_Object
3816 intern_sym (Lisp_Object sym, Lisp_Object obarray, Lisp_Object index)
3818 Lisp_Object *ptr;
3820 XSYMBOL (sym)->interned = (EQ (obarray, initial_obarray)
3821 ? SYMBOL_INTERNED_IN_INITIAL_OBARRAY
3822 : SYMBOL_INTERNED);
3824 if (SREF (SYMBOL_NAME (sym), 0) == ':' && EQ (obarray, initial_obarray))
3826 XSYMBOL (sym)->constant = 1;
3827 XSYMBOL (sym)->redirect = SYMBOL_PLAINVAL;
3828 SET_SYMBOL_VAL (XSYMBOL (sym), sym);
3831 ptr = aref_addr (obarray, XINT (index));
3832 set_symbol_next (sym, SYMBOLP (*ptr) ? XSYMBOL (*ptr) : NULL);
3833 *ptr = sym;
3834 return sym;
3837 /* Intern a symbol with name STRING in OBARRAY using bucket INDEX. */
3839 Lisp_Object
3840 intern_driver (Lisp_Object string, Lisp_Object obarray, Lisp_Object index)
3842 return intern_sym (Fmake_symbol (string), obarray, index);
3845 /* Intern the C string STR: return a symbol with that name,
3846 interned in the current obarray. */
3848 Lisp_Object
3849 intern_1 (const char *str, ptrdiff_t len)
3851 Lisp_Object obarray = check_obarray (Vobarray);
3852 Lisp_Object tem = oblookup (obarray, str, len, len);
3854 return (SYMBOLP (tem) ? tem
3855 /* The above `oblookup' was done on the basis of nchars==nbytes, so
3856 the string has to be unibyte. */
3857 : intern_driver (make_unibyte_string (str, len),
3858 obarray, tem));
3861 Lisp_Object
3862 intern_c_string_1 (const char *str, ptrdiff_t len)
3864 Lisp_Object obarray = check_obarray (Vobarray);
3865 Lisp_Object tem = oblookup (obarray, str, len, len);
3867 if (!SYMBOLP (tem))
3869 /* Creating a non-pure string from a string literal not implemented yet.
3870 We could just use make_string here and live with the extra copy. */
3871 eassert (!NILP (Vpurify_flag));
3872 tem = intern_driver (make_pure_c_string (str, len), obarray, tem);
3874 return tem;
3877 static void
3878 define_symbol (Lisp_Object sym, char const *str)
3880 ptrdiff_t len = strlen (str);
3881 Lisp_Object string = make_pure_c_string (str, len);
3882 init_symbol (sym, string);
3884 /* Qunbound is uninterned, so that it's not confused with any symbol
3885 'unbound' created by a Lisp program. */
3886 if (! EQ (sym, Qunbound))
3888 Lisp_Object bucket = oblookup (initial_obarray, str, len, len);
3889 eassert (INTEGERP (bucket));
3890 intern_sym (sym, initial_obarray, bucket);
3894 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
3895 doc: /* Return the canonical symbol whose name is STRING.
3896 If there is none, one is created by this function and returned.
3897 A second optional argument specifies the obarray to use;
3898 it defaults to the value of `obarray'. */)
3899 (Lisp_Object string, Lisp_Object obarray)
3901 Lisp_Object tem;
3903 obarray = check_obarray (NILP (obarray) ? Vobarray : obarray);
3904 CHECK_STRING (string);
3906 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
3907 if (!SYMBOLP (tem))
3908 tem = intern_driver (NILP (Vpurify_flag) ? string : Fpurecopy (string),
3909 obarray, tem);
3910 return tem;
3913 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
3914 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3915 NAME may be a string or a symbol. If it is a symbol, that exact
3916 symbol is searched for.
3917 A second optional argument specifies the obarray to use;
3918 it defaults to the value of `obarray'. */)
3919 (Lisp_Object name, Lisp_Object obarray)
3921 register Lisp_Object tem, string;
3923 if (NILP (obarray)) obarray = Vobarray;
3924 obarray = check_obarray (obarray);
3926 if (!SYMBOLP (name))
3928 CHECK_STRING (name);
3929 string = name;
3931 else
3932 string = SYMBOL_NAME (name);
3934 tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
3935 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3936 return Qnil;
3937 else
3938 return tem;
3941 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3942 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3943 The value is t if a symbol was found and deleted, nil otherwise.
3944 NAME may be a string or a symbol. If it is a symbol, that symbol
3945 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3946 OBARRAY, if nil, defaults to the value of the variable `obarray'.
3947 usage: (unintern NAME OBARRAY) */)
3948 (Lisp_Object name, Lisp_Object obarray)
3950 register Lisp_Object string, tem;
3951 size_t hash;
3953 if (NILP (obarray)) obarray = Vobarray;
3954 obarray = check_obarray (obarray);
3956 if (SYMBOLP (name))
3957 string = SYMBOL_NAME (name);
3958 else
3960 CHECK_STRING (name);
3961 string = name;
3964 tem = oblookup (obarray, SSDATA (string),
3965 SCHARS (string),
3966 SBYTES (string));
3967 if (INTEGERP (tem))
3968 return Qnil;
3969 /* If arg was a symbol, don't delete anything but that symbol itself. */
3970 if (SYMBOLP (name) && !EQ (name, tem))
3971 return Qnil;
3973 /* There are plenty of other symbols which will screw up the Emacs
3974 session if we unintern them, as well as even more ways to use
3975 `setq' or `fset' or whatnot to make the Emacs session
3976 unusable. Let's not go down this silly road. --Stef */
3977 /* if (EQ (tem, Qnil) || EQ (tem, Qt))
3978 error ("Attempt to unintern t or nil"); */
3980 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3982 hash = oblookup_last_bucket_number;
3984 if (EQ (AREF (obarray, hash), tem))
3986 if (XSYMBOL (tem)->next)
3988 Lisp_Object sym;
3989 XSETSYMBOL (sym, XSYMBOL (tem)->next);
3990 ASET (obarray, hash, sym);
3992 else
3993 ASET (obarray, hash, make_number (0));
3995 else
3997 Lisp_Object tail, following;
3999 for (tail = AREF (obarray, hash);
4000 XSYMBOL (tail)->next;
4001 tail = following)
4003 XSETSYMBOL (following, XSYMBOL (tail)->next);
4004 if (EQ (following, tem))
4006 set_symbol_next (tail, XSYMBOL (following)->next);
4007 break;
4012 return Qt;
4015 /* Return the symbol in OBARRAY whose names matches the string
4016 of SIZE characters (SIZE_BYTE bytes) at PTR.
4017 If there is no such symbol, return the integer bucket number of
4018 where the symbol would be if it were present.
4020 Also store the bucket number in oblookup_last_bucket_number. */
4022 Lisp_Object
4023 oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff_t size_byte)
4025 size_t hash;
4026 size_t obsize;
4027 register Lisp_Object tail;
4028 Lisp_Object bucket, tem;
4030 obarray = check_obarray (obarray);
4031 /* This is sometimes needed in the middle of GC. */
4032 obsize = gc_asize (obarray);
4033 hash = hash_string (ptr, size_byte) % obsize;
4034 bucket = AREF (obarray, hash);
4035 oblookup_last_bucket_number = hash;
4036 if (EQ (bucket, make_number (0)))
4038 else if (!SYMBOLP (bucket))
4039 error ("Bad data in guts of obarray"); /* Like CADR error message. */
4040 else
4041 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
4043 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
4044 && SCHARS (SYMBOL_NAME (tail)) == size
4045 && !memcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
4046 return tail;
4047 else if (XSYMBOL (tail)->next == 0)
4048 break;
4050 XSETINT (tem, hash);
4051 return tem;
4054 void
4055 map_obarray (Lisp_Object obarray, void (*fn) (Lisp_Object, Lisp_Object), Lisp_Object arg)
4057 ptrdiff_t i;
4058 register Lisp_Object tail;
4059 CHECK_VECTOR (obarray);
4060 for (i = ASIZE (obarray) - 1; i >= 0; i--)
4062 tail = AREF (obarray, i);
4063 if (SYMBOLP (tail))
4064 while (1)
4066 (*fn) (tail, arg);
4067 if (XSYMBOL (tail)->next == 0)
4068 break;
4069 XSETSYMBOL (tail, XSYMBOL (tail)->next);
4074 static void
4075 mapatoms_1 (Lisp_Object sym, Lisp_Object function)
4077 call1 (function, sym);
4080 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
4081 doc: /* Call FUNCTION on every symbol in OBARRAY.
4082 OBARRAY defaults to the value of `obarray'. */)
4083 (Lisp_Object function, Lisp_Object obarray)
4085 if (NILP (obarray)) obarray = Vobarray;
4086 obarray = check_obarray (obarray);
4088 map_obarray (obarray, mapatoms_1, function);
4089 return Qnil;
4092 #define OBARRAY_SIZE 1511
4094 void
4095 init_obarray (void)
4097 Lisp_Object oblength;
4098 ptrdiff_t size = 100 + MAX_MULTIBYTE_LENGTH;
4100 XSETFASTINT (oblength, OBARRAY_SIZE);
4102 Vobarray = Fmake_vector (oblength, make_number (0));
4103 initial_obarray = Vobarray;
4104 staticpro (&initial_obarray);
4106 for (int i = 0; i < ARRAYELTS (lispsym); i++)
4107 define_symbol (builtin_lisp_symbol (i), defsym_name[i]);
4109 DEFSYM (Qunbound, "unbound");
4111 DEFSYM (Qnil, "nil");
4112 SET_SYMBOL_VAL (XSYMBOL (Qnil), Qnil);
4113 XSYMBOL (Qnil)->constant = 1;
4114 XSYMBOL (Qnil)->declared_special = true;
4116 DEFSYM (Qt, "t");
4117 SET_SYMBOL_VAL (XSYMBOL (Qt), Qt);
4118 XSYMBOL (Qt)->constant = 1;
4119 XSYMBOL (Qt)->declared_special = true;
4121 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
4122 Vpurify_flag = Qt;
4124 DEFSYM (Qvariable_documentation, "variable-documentation");
4126 read_buffer = xmalloc (size);
4127 read_buffer_size = size;
4130 void
4131 defsubr (struct Lisp_Subr *sname)
4133 Lisp_Object sym, tem;
4134 sym = intern_c_string (sname->symbol_name);
4135 XSETPVECTYPE (sname, PVEC_SUBR);
4136 XSETSUBR (tem, sname);
4137 set_symbol_function (sym, tem);
4140 #ifdef NOTDEF /* Use fset in subr.el now! */
4141 void
4142 defalias (struct Lisp_Subr *sname, char *string)
4144 Lisp_Object sym;
4145 sym = intern (string);
4146 XSETSUBR (XSYMBOL (sym)->function, sname);
4148 #endif /* NOTDEF */
4150 /* Define an "integer variable"; a symbol whose value is forwarded to a
4151 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile):
4152 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
4153 void
4154 defvar_int (struct Lisp_Intfwd *i_fwd,
4155 const char *namestring, EMACS_INT *address)
4157 Lisp_Object sym;
4158 sym = intern_c_string (namestring);
4159 i_fwd->type = Lisp_Fwd_Int;
4160 i_fwd->intvar = address;
4161 XSYMBOL (sym)->declared_special = 1;
4162 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4163 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)i_fwd);
4166 /* Similar but define a variable whose value is t if address contains 1,
4167 nil if address contains 0. */
4168 void
4169 defvar_bool (struct Lisp_Boolfwd *b_fwd,
4170 const char *namestring, bool *address)
4172 Lisp_Object sym;
4173 sym = intern_c_string (namestring);
4174 b_fwd->type = Lisp_Fwd_Bool;
4175 b_fwd->boolvar = address;
4176 XSYMBOL (sym)->declared_special = 1;
4177 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4178 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)b_fwd);
4179 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
4182 /* Similar but define a variable whose value is the Lisp Object stored
4183 at address. Two versions: with and without gc-marking of the C
4184 variable. The nopro version is used when that variable will be
4185 gc-marked for some other reason, since marking the same slot twice
4186 can cause trouble with strings. */
4187 void
4188 defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd,
4189 const char *namestring, Lisp_Object *address)
4191 Lisp_Object sym;
4192 sym = intern_c_string (namestring);
4193 o_fwd->type = Lisp_Fwd_Obj;
4194 o_fwd->objvar = address;
4195 XSYMBOL (sym)->declared_special = 1;
4196 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4197 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)o_fwd);
4200 void
4201 defvar_lisp (struct Lisp_Objfwd *o_fwd,
4202 const char *namestring, Lisp_Object *address)
4204 defvar_lisp_nopro (o_fwd, namestring, address);
4205 staticpro (address);
4208 /* Similar but define a variable whose value is the Lisp Object stored
4209 at a particular offset in the current kboard object. */
4211 void
4212 defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
4213 const char *namestring, int offset)
4215 Lisp_Object sym;
4216 sym = intern_c_string (namestring);
4217 ko_fwd->type = Lisp_Fwd_Kboard_Obj;
4218 ko_fwd->offset = offset;
4219 XSYMBOL (sym)->declared_special = 1;
4220 XSYMBOL (sym)->redirect = SYMBOL_FORWARDED;
4221 SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
4224 /* Check that the elements of lpath exist. */
4226 static void
4227 load_path_check (Lisp_Object lpath)
4229 Lisp_Object path_tail;
4231 /* The only elements that might not exist are those from
4232 PATH_LOADSEARCH, EMACSLOADPATH. Anything else is only added if
4233 it exists. */
4234 for (path_tail = lpath; !NILP (path_tail); path_tail = XCDR (path_tail))
4236 Lisp_Object dirfile;
4237 dirfile = Fcar (path_tail);
4238 if (STRINGP (dirfile))
4240 dirfile = Fdirectory_file_name (dirfile);
4241 if (! file_accessible_directory_p (dirfile))
4242 dir_warning ("Lisp directory", XCAR (path_tail));
4247 /* Return the default load-path, to be used if EMACSLOADPATH is unset.
4248 This does not include the standard site-lisp directories
4249 under the installation prefix (i.e., PATH_SITELOADSEARCH),
4250 but it does (unless no_site_lisp is set) include site-lisp
4251 directories in the source/build directories if those exist and we
4252 are running uninstalled.
4254 Uses the following logic:
4255 If CANNOT_DUMP: Use PATH_LOADSEARCH.
4256 The remainder is what happens when dumping works:
4257 If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
4258 Otherwise use PATH_LOADSEARCH.
4260 If !initialized, then just return PATH_DUMPLOADSEARCH.
4261 If initialized:
4262 If Vinstallation_directory is not nil (ie, running uninstalled):
4263 If installation-dir/lisp exists and not already a member,
4264 we must be running uninstalled. Reset the load-path
4265 to just installation-dir/lisp. (The default PATH_LOADSEARCH
4266 refers to the eventual installation directories. Since we
4267 are not yet installed, we should not use them, even if they exist.)
4268 If installation-dir/lisp does not exist, just add
4269 PATH_DUMPLOADSEARCH at the end instead.
4270 Add installation-dir/site-lisp (if !no_site_lisp, and exists
4271 and not already a member) at the front.
4272 If installation-dir != source-dir (ie running an uninstalled,
4273 out-of-tree build) AND install-dir/src/Makefile exists BUT
4274 install-dir/src/Makefile.in does NOT exist (this is a sanity
4275 check), then repeat the above steps for source-dir/lisp, site-lisp. */
4277 static Lisp_Object
4278 load_path_default (void)
4280 Lisp_Object lpath = Qnil;
4281 const char *normal;
4283 #ifdef CANNOT_DUMP
4284 #ifdef HAVE_NS
4285 const char *loadpath = ns_load_path ();
4286 #endif
4288 normal = PATH_LOADSEARCH;
4289 #ifdef HAVE_NS
4290 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4291 #else
4292 lpath = decode_env_path (0, normal, 0);
4293 #endif
4295 #else /* !CANNOT_DUMP */
4297 normal = NILP (Vpurify_flag) ? PATH_LOADSEARCH : PATH_DUMPLOADSEARCH;
4299 if (initialized)
4301 #ifdef HAVE_NS
4302 const char *loadpath = ns_load_path ();
4303 lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
4304 #else
4305 lpath = decode_env_path (0, normal, 0);
4306 #endif
4307 if (!NILP (Vinstallation_directory))
4309 Lisp_Object tem, tem1;
4311 /* Add to the path the lisp subdir of the installation
4312 dir, if it is accessible. Note: in out-of-tree builds,
4313 this directory is empty save for Makefile. */
4314 tem = Fexpand_file_name (build_string ("lisp"),
4315 Vinstallation_directory);
4316 tem1 = Ffile_accessible_directory_p (tem);
4317 if (!NILP (tem1))
4319 if (NILP (Fmember (tem, lpath)))
4321 /* We are running uninstalled. The default load-path
4322 points to the eventual installed lisp directories.
4323 We should not use those now, even if they exist,
4324 so start over from a clean slate. */
4325 lpath = list1 (tem);
4328 else
4329 /* That dir doesn't exist, so add the build-time
4330 Lisp dirs instead. */
4332 Lisp_Object dump_path =
4333 decode_env_path (0, PATH_DUMPLOADSEARCH, 0);
4334 lpath = nconc2 (lpath, dump_path);
4337 /* Add site-lisp under the installation dir, if it exists. */
4338 if (!no_site_lisp)
4340 tem = Fexpand_file_name (build_string ("site-lisp"),
4341 Vinstallation_directory);
4342 tem1 = Ffile_accessible_directory_p (tem);
4343 if (!NILP (tem1))
4345 if (NILP (Fmember (tem, lpath)))
4346 lpath = Fcons (tem, lpath);
4350 /* If Emacs was not built in the source directory,
4351 and it is run from where it was built, add to load-path
4352 the lisp and site-lisp dirs under that directory. */
4354 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
4356 Lisp_Object tem2;
4358 tem = Fexpand_file_name (build_string ("src/Makefile"),
4359 Vinstallation_directory);
4360 tem1 = Ffile_exists_p (tem);
4362 /* Don't be fooled if they moved the entire source tree
4363 AFTER dumping Emacs. If the build directory is indeed
4364 different from the source dir, src/Makefile.in and
4365 src/Makefile will not be found together. */
4366 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
4367 Vinstallation_directory);
4368 tem2 = Ffile_exists_p (tem);
4369 if (!NILP (tem1) && NILP (tem2))
4371 tem = Fexpand_file_name (build_string ("lisp"),
4372 Vsource_directory);
4374 if (NILP (Fmember (tem, lpath)))
4375 lpath = Fcons (tem, lpath);
4377 if (!no_site_lisp)
4379 tem = Fexpand_file_name (build_string ("site-lisp"),
4380 Vsource_directory);
4381 tem1 = Ffile_accessible_directory_p (tem);
4382 if (!NILP (tem1))
4384 if (NILP (Fmember (tem, lpath)))
4385 lpath = Fcons (tem, lpath);
4389 } /* Vinstallation_directory != Vsource_directory */
4391 } /* if Vinstallation_directory */
4393 else /* !initialized */
4395 /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
4396 source directory. We used to add ../lisp (ie the lisp dir in
4397 the build directory) at the front here, but that should not
4398 be necessary, since in out of tree builds lisp/ is empty, save
4399 for Makefile. */
4400 lpath = decode_env_path (0, normal, 0);
4402 #endif /* !CANNOT_DUMP */
4404 return lpath;
4407 void
4408 init_lread (void)
4410 /* First, set Vload_path. */
4412 /* Ignore EMACSLOADPATH when dumping. */
4413 #ifdef CANNOT_DUMP
4414 bool use_loadpath = true;
4415 #else
4416 bool use_loadpath = NILP (Vpurify_flag);
4417 #endif
4419 if (use_loadpath && egetenv ("EMACSLOADPATH"))
4421 Vload_path = decode_env_path ("EMACSLOADPATH", 0, 1);
4423 /* Check (non-nil) user-supplied elements. */
4424 load_path_check (Vload_path);
4426 /* If no nils in the environment variable, use as-is.
4427 Otherwise, replace any nils with the default. */
4428 if (! NILP (Fmemq (Qnil, Vload_path)))
4430 Lisp_Object elem, elpath = Vload_path;
4431 Lisp_Object default_lpath = load_path_default ();
4433 /* Check defaults, before adding site-lisp. */
4434 load_path_check (default_lpath);
4436 /* Add the site-lisp directories to the front of the default. */
4437 if (!no_site_lisp && PATH_SITELOADSEARCH[0] != '\0')
4439 Lisp_Object sitelisp;
4440 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4441 if (! NILP (sitelisp))
4442 default_lpath = nconc2 (sitelisp, default_lpath);
4445 Vload_path = Qnil;
4447 /* Replace nils from EMACSLOADPATH by default. */
4448 while (CONSP (elpath))
4450 elem = XCAR (elpath);
4451 elpath = XCDR (elpath);
4452 Vload_path = CALLN (Fappend, Vload_path,
4453 NILP (elem) ? default_lpath : list1 (elem));
4455 } /* Fmemq (Qnil, Vload_path) */
4457 else
4459 Vload_path = load_path_default ();
4461 /* Check before adding site-lisp directories.
4462 The install should have created them, but they are not
4463 required, so no need to warn if they are absent.
4464 Or we might be running before installation. */
4465 load_path_check (Vload_path);
4467 /* Add the site-lisp directories at the front. */
4468 if (initialized && !no_site_lisp && PATH_SITELOADSEARCH[0] != '\0')
4470 Lisp_Object sitelisp;
4471 sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
4472 if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
4476 Vvalues = Qnil;
4478 load_in_progress = 0;
4479 Vload_file_name = Qnil;
4480 Vstandard_input = Qt;
4481 Vloads_in_progress = Qnil;
4484 /* Print a warning that directory intended for use USE and with name
4485 DIRNAME cannot be accessed. On entry, errno should correspond to
4486 the access failure. Print the warning on stderr and put it in
4487 *Messages*. */
4489 void
4490 dir_warning (char const *use, Lisp_Object dirname)
4492 static char const format[] = "Warning: %s '%s': %s\n";
4493 char *diagnostic = emacs_strerror (errno);
4494 fprintf (stderr, format, use, SSDATA (ENCODE_SYSTEM (dirname)), diagnostic);
4496 /* Don't log the warning before we've initialized!! */
4497 if (initialized)
4499 ptrdiff_t diaglen = strlen (diagnostic);
4500 AUTO_STRING_WITH_LEN (diag, diagnostic, diaglen);
4501 if (! NILP (Vlocale_coding_system))
4503 Lisp_Object s
4504 = code_convert_string_norecord (diag, Vlocale_coding_system, false);
4505 diagnostic = SSDATA (s);
4506 diaglen = SBYTES (s);
4508 USE_SAFE_ALLOCA;
4509 char *buffer = SAFE_ALLOCA (sizeof format - 3 * (sizeof "%s" - 1)
4510 + strlen (use) + SBYTES (dirname) + diaglen);
4511 ptrdiff_t message_len = esprintf (buffer, format, use, SSDATA (dirname),
4512 diagnostic);
4513 message_dolog (buffer, message_len, 0, STRING_MULTIBYTE (dirname));
4514 SAFE_FREE ();
4518 void
4519 syms_of_lread (void)
4521 defsubr (&Sread);
4522 defsubr (&Sread_from_string);
4523 defsubr (&Sintern);
4524 defsubr (&Sintern_soft);
4525 defsubr (&Sunintern);
4526 defsubr (&Sget_load_suffixes);
4527 defsubr (&Sload);
4528 defsubr (&Seval_buffer);
4529 defsubr (&Seval_region);
4530 defsubr (&Sread_char);
4531 defsubr (&Sread_char_exclusive);
4532 defsubr (&Sread_event);
4533 defsubr (&Sget_file_char);
4534 defsubr (&Smapatoms);
4535 defsubr (&Slocate_file_internal);
4537 DEFVAR_LISP ("obarray", Vobarray,
4538 doc: /* Symbol table for use by `intern' and `read'.
4539 It is a vector whose length ought to be prime for best results.
4540 The vector's contents don't make sense if examined from Lisp programs;
4541 to find all the symbols in an obarray, use `mapatoms'. */);
4543 DEFVAR_LISP ("values", Vvalues,
4544 doc: /* List of values of all expressions which were read, evaluated and printed.
4545 Order is reverse chronological. */);
4546 XSYMBOL (intern ("values"))->declared_special = 0;
4548 DEFVAR_LISP ("standard-input", Vstandard_input,
4549 doc: /* Stream for read to get input from.
4550 See documentation of `read' for possible values. */);
4551 Vstandard_input = Qt;
4553 DEFVAR_LISP ("read-with-symbol-positions", Vread_with_symbol_positions,
4554 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
4556 If this variable is a buffer, then only forms read from that buffer
4557 will be added to `read-symbol-positions-list'.
4558 If this variable is t, then all read forms will be added.
4559 The effect of all other values other than nil are not currently
4560 defined, although they may be in the future.
4562 The positions are relative to the last call to `read' or
4563 `read-from-string'. It is probably a bad idea to set this variable at
4564 the toplevel; bind it instead. */);
4565 Vread_with_symbol_positions = Qnil;
4567 DEFVAR_LISP ("read-symbol-positions-list", Vread_symbol_positions_list,
4568 doc: /* A list mapping read symbols to their positions.
4569 This variable is modified during calls to `read' or
4570 `read-from-string', but only when `read-with-symbol-positions' is
4571 non-nil.
4573 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
4574 CHAR-POSITION is an integer giving the offset of that occurrence of the
4575 symbol from the position where `read' or `read-from-string' started.
4577 Note that a symbol will appear multiple times in this list, if it was
4578 read multiple times. The list is in the same order as the symbols
4579 were read in. */);
4580 Vread_symbol_positions_list = Qnil;
4582 DEFVAR_LISP ("read-circle", Vread_circle,
4583 doc: /* Non-nil means read recursive structures using #N= and #N# syntax. */);
4584 Vread_circle = Qt;
4586 DEFVAR_LISP ("load-path", Vload_path,
4587 doc: /* List of directories to search for files to load.
4588 Each element is a string (directory file name) or nil (meaning
4589 `default-directory').
4590 This list is consulted by the `require' function.
4591 Initialized during startup as described in Info node `(elisp)Library Search'.
4592 Use `directory-file-name' when adding items to this path. However, Lisp
4593 programs that process this list should tolerate directories both with
4594 and without trailing slashes. */);
4596 DEFVAR_LISP ("load-suffixes", Vload_suffixes,
4597 doc: /* List of suffixes for Emacs Lisp files and dynamic modules.
4598 This list includes suffixes for both compiled and source Emacs Lisp files.
4599 This list should not include the empty string.
4600 `load' and related functions try to append these suffixes, in order,
4601 to the specified file name if a suffix is allowed or required. */);
4602 #ifdef HAVE_MODULES
4603 Vload_suffixes = list3 (build_pure_c_string (".elc"),
4604 build_pure_c_string (".el"),
4605 build_pure_c_string (MODULES_SUFFIX));
4606 #else
4607 Vload_suffixes = list2 (build_pure_c_string (".elc"),
4608 build_pure_c_string (".el"));
4609 #endif
4610 DEFVAR_LISP ("module-file-suffix", Vmodule_file_suffix,
4611 doc: /* Suffix of loadable module file, or nil of modules are not supported. */);
4612 #ifdef HAVE_MODULES
4613 Vmodule_file_suffix = build_pure_c_string (MODULES_SUFFIX);
4614 #else
4615 Vmodule_file_suffix = Qnil;
4616 #endif
4617 DEFVAR_LISP ("load-file-rep-suffixes", Vload_file_rep_suffixes,
4618 doc: /* List of suffixes that indicate representations of \
4619 the same file.
4620 This list should normally start with the empty string.
4622 Enabling Auto Compression mode appends the suffixes in
4623 `jka-compr-load-suffixes' to this list and disabling Auto Compression
4624 mode removes them again. `load' and related functions use this list to
4625 determine whether they should look for compressed versions of a file
4626 and, if so, which suffixes they should try to append to the file name
4627 in order to do so. However, if you want to customize which suffixes
4628 the loading functions recognize as compression suffixes, you should
4629 customize `jka-compr-load-suffixes' rather than the present variable. */);
4630 Vload_file_rep_suffixes = list1 (empty_unibyte_string);
4632 DEFVAR_BOOL ("load-in-progress", load_in_progress,
4633 doc: /* Non-nil if inside of `load'. */);
4634 DEFSYM (Qload_in_progress, "load-in-progress");
4636 DEFVAR_LISP ("after-load-alist", Vafter_load_alist,
4637 doc: /* An alist of functions to be evalled when particular files are loaded.
4638 Each element looks like (REGEXP-OR-FEATURE FUNCS...).
4640 REGEXP-OR-FEATURE is either a regular expression to match file names, or
4641 a symbol (a feature name).
4643 When `load' is run and the file-name argument matches an element's
4644 REGEXP-OR-FEATURE, or when `provide' is run and provides the symbol
4645 REGEXP-OR-FEATURE, the FUNCS in the element are called.
4647 An error in FORMS does not undo the load, but does prevent execution of
4648 the rest of the FORMS. */);
4649 Vafter_load_alist = Qnil;
4651 DEFVAR_LISP ("load-history", Vload_history,
4652 doc: /* Alist mapping loaded file names to symbols and features.
4653 Each alist element should be a list (FILE-NAME ENTRIES...), where
4654 FILE-NAME is the name of a file that has been loaded into Emacs.
4655 The file name is absolute and true (i.e. it doesn't contain symlinks).
4656 As an exception, one of the alist elements may have FILE-NAME nil,
4657 for symbols and features not associated with any file.
4659 The remaining ENTRIES in the alist element describe the functions and
4660 variables defined in that file, the features provided, and the
4661 features required. Each entry has the form `(provide . FEATURE)',
4662 `(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
4663 `(defface . SYMBOL)', or `(t . SYMBOL)'. Entries like `(t . SYMBOL)'
4664 may precede a `(defun . FUNCTION)' entry, and means that SYMBOL was an
4665 autoload before this file redefined it as a function. In addition,
4666 entries may also be single symbols, which means that SYMBOL was
4667 defined by `defvar' or `defconst'.
4669 During preloading, the file name recorded is relative to the main Lisp
4670 directory. These file names are converted to absolute at startup. */);
4671 Vload_history = Qnil;
4673 DEFVAR_LISP ("load-file-name", Vload_file_name,
4674 doc: /* Full name of file being loaded by `load'. */);
4675 Vload_file_name = Qnil;
4677 DEFVAR_LISP ("user-init-file", Vuser_init_file,
4678 doc: /* File name, including directory, of user's initialization file.
4679 If the file loaded had extension `.elc', and the corresponding source file
4680 exists, this variable contains the name of source file, suitable for use
4681 by functions like `custom-save-all' which edit the init file.
4682 While Emacs loads and evaluates the init file, value is the real name
4683 of the file, regardless of whether or not it has the `.elc' extension. */);
4684 Vuser_init_file = Qnil;
4686 DEFVAR_LISP ("current-load-list", Vcurrent_load_list,
4687 doc: /* Used for internal purposes by `load'. */);
4688 Vcurrent_load_list = Qnil;
4690 DEFVAR_LISP ("load-read-function", Vload_read_function,
4691 doc: /* Function used by `load' and `eval-region' for reading expressions.
4692 Called with a single argument (the stream from which to read).
4693 The default is to use the function `read'. */);
4694 DEFSYM (Qread, "read");
4695 Vload_read_function = Qread;
4697 DEFVAR_LISP ("load-source-file-function", Vload_source_file_function,
4698 doc: /* Function called in `load' to load an Emacs Lisp source file.
4699 The value should be a function for doing code conversion before
4700 reading a source file. It can also be nil, in which case loading is
4701 done without any code conversion.
4703 If the value is a function, it is called with four arguments,
4704 FULLNAME, FILE, NOERROR, NOMESSAGE. FULLNAME is the absolute name of
4705 the file to load, FILE is the non-absolute name (for messages etc.),
4706 and NOERROR and NOMESSAGE are the corresponding arguments passed to
4707 `load'. The function should return t if the file was loaded. */);
4708 Vload_source_file_function = Qnil;
4710 DEFVAR_BOOL ("load-force-doc-strings", load_force_doc_strings,
4711 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4712 This is useful when the file being loaded is a temporary copy. */);
4713 load_force_doc_strings = 0;
4715 DEFVAR_BOOL ("load-convert-to-unibyte", load_convert_to_unibyte,
4716 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4717 This is normally bound by `load' and `eval-buffer' to control `read',
4718 and is not meant for users to change. */);
4719 load_convert_to_unibyte = 0;
4721 DEFVAR_LISP ("source-directory", Vsource_directory,
4722 doc: /* Directory in which Emacs sources were found when Emacs was built.
4723 You cannot count on them to still be there! */);
4724 Vsource_directory
4725 = Fexpand_file_name (build_string ("../"),
4726 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH, 0)));
4728 DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
4729 doc: /* List of files that were preloaded (when dumping Emacs). */);
4730 Vpreloaded_file_list = Qnil;
4732 DEFVAR_LISP ("byte-boolean-vars", Vbyte_boolean_vars,
4733 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4734 Vbyte_boolean_vars = Qnil;
4736 DEFVAR_BOOL ("load-dangerous-libraries", load_dangerous_libraries,
4737 doc: /* Non-nil means load dangerous compiled Lisp files.
4738 Some versions of XEmacs use different byte codes than Emacs. These
4739 incompatible byte codes can make Emacs crash when it tries to execute
4740 them. */);
4741 load_dangerous_libraries = 0;
4743 DEFVAR_BOOL ("force-load-messages", force_load_messages,
4744 doc: /* Non-nil means force printing messages when loading Lisp files.
4745 This overrides the value of the NOMESSAGE argument to `load'. */);
4746 force_load_messages = 0;
4748 DEFVAR_LISP ("bytecomp-version-regexp", Vbytecomp_version_regexp,
4749 doc: /* Regular expression matching safe to load compiled Lisp files.
4750 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4751 from the file, and matches them against this regular expression.
4752 When the regular expression matches, the file is considered to be safe
4753 to load. See also `load-dangerous-libraries'. */);
4754 Vbytecomp_version_regexp
4755 = build_pure_c_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4757 DEFSYM (Qlexical_binding, "lexical-binding");
4758 DEFVAR_LISP ("lexical-binding", Vlexical_binding,
4759 doc: /* Whether to use lexical binding when evaluating code.
4760 Non-nil means that the code in the current buffer should be evaluated
4761 with lexical binding.
4762 This variable is automatically set from the file variables of an
4763 interpreted Lisp file read using `load'. Unlike other file local
4764 variables, this must be set in the first line of a file. */);
4765 Vlexical_binding = Qnil;
4766 Fmake_variable_buffer_local (Qlexical_binding);
4768 DEFVAR_LISP ("eval-buffer-list", Veval_buffer_list,
4769 doc: /* List of buffers being read from by calls to `eval-buffer' and `eval-region'. */);
4770 Veval_buffer_list = Qnil;
4772 DEFVAR_LISP ("old-style-backquotes", Vold_style_backquotes,
4773 doc: /* Set to non-nil when `read' encounters an old-style backquote. */);
4774 Vold_style_backquotes = Qnil;
4775 DEFSYM (Qold_style_backquotes, "old-style-backquotes");
4777 DEFVAR_BOOL ("load-prefer-newer", load_prefer_newer,
4778 doc: /* Non-nil means `load' prefers the newest version of a file.
4779 This applies when a filename suffix is not explicitly specified and
4780 `load' is trying various possible suffixes (see `load-suffixes' and
4781 `load-file-rep-suffixes'). Normally, it stops at the first file
4782 that exists unless you explicitly specify one or the other. If this
4783 option is non-nil, it checks all suffixes and uses whichever file is
4784 newest.
4785 Note that if you customize this, obviously it will not affect files
4786 that are loaded before your customizations are read! */);
4787 load_prefer_newer = 0;
4789 /* Vsource_directory was initialized in init_lread. */
4791 DEFSYM (Qcurrent_load_list, "current-load-list");
4792 DEFSYM (Qstandard_input, "standard-input");
4793 DEFSYM (Qread_char, "read-char");
4794 DEFSYM (Qget_file_char, "get-file-char");
4796 /* Used instead of Qget_file_char while loading *.elc files compiled
4797 by Emacs 21 or older. */
4798 DEFSYM (Qget_emacs_mule_file_char, "get-emacs-mule-file-char");
4800 DEFSYM (Qload_force_doc_strings, "load-force-doc-strings");
4802 DEFSYM (Qbackquote, "`");
4803 DEFSYM (Qcomma, ",");
4804 DEFSYM (Qcomma_at, ",@");
4805 DEFSYM (Qcomma_dot, ",.");
4807 DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
4808 DEFSYM (Qascii_character, "ascii-character");
4809 DEFSYM (Qfunction, "function");
4810 DEFSYM (Qload, "load");
4811 DEFSYM (Qload_file_name, "load-file-name");
4812 DEFSYM (Qeval_buffer_list, "eval-buffer-list");
4813 DEFSYM (Qfile_truename, "file-truename");
4814 DEFSYM (Qdir_ok, "dir-ok");
4815 DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
4817 staticpro (&read_objects);
4818 read_objects = Qnil;
4819 staticpro (&seen_list);
4820 seen_list = Qnil;
4822 Vloads_in_progress = Qnil;
4823 staticpro (&Vloads_in_progress);
4825 DEFSYM (Qhash_table, "hash-table");
4826 DEFSYM (Qdata, "data");
4827 DEFSYM (Qtest, "test");
4828 DEFSYM (Qsize, "size");
4829 DEFSYM (Qweakness, "weakness");
4830 DEFSYM (Qrehash_size, "rehash-size");
4831 DEFSYM (Qrehash_threshold, "rehash-threshold");
4833 DEFSYM (Qchar_from_name, "char-from-name");