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