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