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