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