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