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