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