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