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