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