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