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