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