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