Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-15
[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
2126 = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2127 / BOOL_VECTOR_BITS_PER_CHAR);
2128
2129 UNREAD (c);
2130 tmp = read1 (readcharfun, pch, first_in_list);
2131 if (STRING_MULTIBYTE (tmp)
2132 || (size_in_chars != SCHARS (tmp)
2133 /* We used to print 1 char too many
2134 when the number of bits was a multiple of 8.
2135 Accept such input in case it came from an old
2136 version. */
2137 && ! (XFASTINT (length)
2138 == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
2139 Fsignal (Qinvalid_read_syntax,
2140 Fcons (make_string ("#&...", 5), Qnil));
2141
2142 val = Fmake_bool_vector (length, Qnil);
2143 bcopy (SDATA (tmp), XBOOL_VECTOR (val)->data,
2144 size_in_chars);
2145 /* Clear the extraneous bits in the last byte. */
2146 if (XINT (length) != size_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2147 XBOOL_VECTOR (val)->data[size_in_chars - 1]
2148 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2149 return val;
2150 }
2151 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
2152 Qnil));
2153 }
2154 if (c == '[')
2155 {
2156 /* Accept compiled functions at read-time so that we don't have to
2157 build them using function calls. */
2158 Lisp_Object tmp;
2159 tmp = read_vector (readcharfun, 1);
2160 return Fmake_byte_code (XVECTOR (tmp)->size,
2161 XVECTOR (tmp)->contents);
2162 }
2163 if (c == '(')
2164 {
2165 Lisp_Object tmp;
2166 struct gcpro gcpro1;
2167 int ch;
2168
2169 /* Read the string itself. */
2170 tmp = read1 (readcharfun, &ch, 0);
2171 if (ch != 0 || !STRINGP (tmp))
2172 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
2173 GCPRO1 (tmp);
2174 /* Read the intervals and their properties. */
2175 while (1)
2176 {
2177 Lisp_Object beg, end, plist;
2178
2179 beg = read1 (readcharfun, &ch, 0);
2180 end = plist = Qnil;
2181 if (ch == ')')
2182 break;
2183 if (ch == 0)
2184 end = read1 (readcharfun, &ch, 0);
2185 if (ch == 0)
2186 plist = read1 (readcharfun, &ch, 0);
2187 if (ch)
2188 Fsignal (Qinvalid_read_syntax,
2189 Fcons (build_string ("invalid string property list"),
2190 Qnil));
2191 Fset_text_properties (beg, end, plist, tmp);
2192 }
2193 UNGCPRO;
2194 return tmp;
2195 }
2196
2197 /* #@NUMBER is used to skip NUMBER following characters.
2198 That's used in .elc files to skip over doc strings
2199 and function definitions. */
2200 if (c == '@')
2201 {
2202 int i, nskip = 0;
2203
2204 load_each_byte = 1;
2205 /* Read a decimal integer. */
2206 while ((c = READCHAR) >= 0
2207 && c >= '0' && c <= '9')
2208 {
2209 nskip *= 10;
2210 nskip += c - '0';
2211 }
2212 if (c >= 0)
2213 UNREAD (c);
2214
2215 if (load_force_doc_strings
2216 && (EQ (readcharfun, Qget_file_char)
2217 || EQ (readcharfun, Qget_emacs_mule_file_char)))
2218 {
2219 /* If we are supposed to force doc strings into core right now,
2220 record the last string that we skipped,
2221 and record where in the file it comes from. */
2222
2223 /* But first exchange saved_doc_string
2224 with prev_saved_doc_string, so we save two strings. */
2225 {
2226 char *temp = saved_doc_string;
2227 int temp_size = saved_doc_string_size;
2228 file_offset temp_pos = saved_doc_string_position;
2229 int temp_len = saved_doc_string_length;
2230
2231 saved_doc_string = prev_saved_doc_string;
2232 saved_doc_string_size = prev_saved_doc_string_size;
2233 saved_doc_string_position = prev_saved_doc_string_position;
2234 saved_doc_string_length = prev_saved_doc_string_length;
2235
2236 prev_saved_doc_string = temp;
2237 prev_saved_doc_string_size = temp_size;
2238 prev_saved_doc_string_position = temp_pos;
2239 prev_saved_doc_string_length = temp_len;
2240 }
2241
2242 if (saved_doc_string_size == 0)
2243 {
2244 saved_doc_string_size = nskip + 100;
2245 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
2246 }
2247 if (nskip > saved_doc_string_size)
2248 {
2249 saved_doc_string_size = nskip + 100;
2250 saved_doc_string = (char *) xrealloc (saved_doc_string,
2251 saved_doc_string_size);
2252 }
2253
2254 saved_doc_string_position = file_tell (instream);
2255
2256 /* Copy that many characters into saved_doc_string. */
2257 for (i = 0; i < nskip && c >= 0; i++)
2258 saved_doc_string[i] = c = READCHAR;
2259
2260 saved_doc_string_length = i;
2261 }
2262 else
2263 {
2264 /* Skip that many characters. */
2265 for (i = 0; i < nskip && c >= 0; i++)
2266 c = READCHAR;
2267 }
2268
2269 load_each_byte = 0;
2270 goto retry;
2271 }
2272 if (c == '!')
2273 {
2274 /* #! appears at the beginning of an executable file.
2275 Skip the first line. */
2276 while (c != '\n' && c >= 0)
2277 c = READCHAR;
2278 goto retry;
2279 }
2280 if (c == '$')
2281 return Vload_file_name;
2282 if (c == '\'')
2283 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
2284 /* #:foo is the uninterned symbol named foo. */
2285 if (c == ':')
2286 {
2287 uninterned_symbol = 1;
2288 c = READCHAR;
2289 goto default_label;
2290 }
2291 /* Reader forms that can reuse previously read objects. */
2292 if (c >= '0' && c <= '9')
2293 {
2294 int n = 0;
2295 Lisp_Object tem;
2296
2297 /* Read a non-negative integer. */
2298 while (c >= '0' && c <= '9')
2299 {
2300 n *= 10;
2301 n += c - '0';
2302 c = READCHAR;
2303 }
2304 /* #n=object returns object, but associates it with n for #n#. */
2305 if (c == '=')
2306 {
2307 /* Make a placeholder for #n# to use temporarily */
2308 Lisp_Object placeholder;
2309 Lisp_Object cell;
2310
2311 placeholder = Fcons(Qnil, Qnil);
2312 cell = Fcons (make_number (n), placeholder);
2313 read_objects = Fcons (cell, read_objects);
2314
2315 /* Read the object itself. */
2316 tem = read0 (readcharfun);
2317
2318 /* Now put it everywhere the placeholder was... */
2319 substitute_object_in_subtree (tem, placeholder);
2320
2321 /* ...and #n# will use the real value from now on. */
2322 Fsetcdr (cell, tem);
2323
2324 return tem;
2325 }
2326 /* #n# returns a previously read object. */
2327 if (c == '#')
2328 {
2329 tem = Fassq (make_number (n), read_objects);
2330 if (CONSP (tem))
2331 return XCDR (tem);
2332 /* Fall through to error message. */
2333 }
2334 else if (c == 'r' || c == 'R')
2335 return read_integer (readcharfun, n);
2336
2337 /* Fall through to error message. */
2338 }
2339 else if (c == 'x' || c == 'X')
2340 return read_integer (readcharfun, 16);
2341 else if (c == 'o' || c == 'O')
2342 return read_integer (readcharfun, 8);
2343 else if (c == 'b' || c == 'B')
2344 return read_integer (readcharfun, 2);
2345
2346 UNREAD (c);
2347 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
2348
2349 case ';':
2350 while ((c = READCHAR) >= 0 && c != '\n');
2351 goto retry;
2352
2353 case '\'':
2354 {
2355 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2356 }
2357
2358 case '`':
2359 if (first_in_list)
2360 goto default_label;
2361 else
2362 {
2363 Lisp_Object value;
2364
2365 new_backquote_flag++;
2366 value = read0 (readcharfun);
2367 new_backquote_flag--;
2368
2369 return Fcons (Qbackquote, Fcons (value, Qnil));
2370 }
2371
2372 case ',':
2373 if (new_backquote_flag)
2374 {
2375 Lisp_Object comma_type = Qnil;
2376 Lisp_Object value;
2377 int ch = READCHAR;
2378
2379 if (ch == '@')
2380 comma_type = Qcomma_at;
2381 else if (ch == '.')
2382 comma_type = Qcomma_dot;
2383 else
2384 {
2385 if (ch >= 0) UNREAD (ch);
2386 comma_type = Qcomma;
2387 }
2388
2389 new_backquote_flag--;
2390 value = read0 (readcharfun);
2391 new_backquote_flag++;
2392 return Fcons (comma_type, Fcons (value, Qnil));
2393 }
2394 else
2395 goto default_label;
2396
2397 case '?':
2398 {
2399 int modifiers;
2400 int next_char;
2401 int ok;
2402
2403 c = READCHAR;
2404 if (c < 0)
2405 end_of_file_error ();
2406
2407 /* Accept `single space' syntax like (list ? x) where the
2408 whitespace character is SPC or TAB.
2409 Other literal whitespace like NL, CR, and FF are not accepted,
2410 as there are well-established escape sequences for these. */
2411 if (c == ' ' || c == '\t')
2412 return make_number (c);
2413
2414 if (c == '\\')
2415 c = read_escape (readcharfun, 0);
2416 modifiers = c & CHAR_MODIFIER_MASK;
2417 c &= ~CHAR_MODIFIER_MASK;
2418 if (CHAR_BYTE8_P (c))
2419 c = CHAR_TO_BYTE8 (c);
2420 c |= modifiers;
2421
2422 next_char = READCHAR;
2423 if (next_char == '.')
2424 {
2425 /* Only a dotted-pair dot is valid after a char constant. */
2426 int next_next_char = READCHAR;
2427 UNREAD (next_next_char);
2428
2429 ok = (next_next_char <= 040
2430 || (next_next_char < 0200
2431 && (index ("\"';([#?", next_next_char)
2432 || (!first_in_list && next_next_char == '`')
2433 || (new_backquote_flag && next_next_char == ','))));
2434 }
2435 else
2436 {
2437 ok = (next_char <= 040
2438 || (next_char < 0200
2439 && (index ("\"';()[]#?", next_char)
2440 || (!first_in_list && next_char == '`')
2441 || (new_backquote_flag && next_char == ','))));
2442 }
2443 UNREAD (next_char);
2444 if (!ok)
2445 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("?", 1), Qnil));
2446
2447 return make_number (c);
2448 }
2449
2450 case '"':
2451 {
2452 char *p = read_buffer;
2453 char *end = read_buffer + read_buffer_size;
2454 register int c;
2455 /* Nonzero if we saw an escape sequence specifying
2456 a multibyte character. */
2457 int force_multibyte = 0;
2458 /* Nonzero if we saw an escape sequence specifying
2459 a single-byte character. */
2460 int force_singlebyte = 0;
2461 int cancel = 0;
2462 int nchars = 0;
2463
2464 while ((c = READCHAR) >= 0
2465 && c != '\"')
2466 {
2467 if (end - p < MAX_MULTIBYTE_LENGTH)
2468 {
2469 int offset = p - read_buffer;
2470 read_buffer = (char *) xrealloc (read_buffer,
2471 read_buffer_size *= 2);
2472 p = read_buffer + offset;
2473 end = read_buffer + read_buffer_size;
2474 }
2475
2476 if (c == '\\')
2477 {
2478 int modifiers;
2479
2480 c = read_escape (readcharfun, 1);
2481
2482 /* C is -1 if \ newline has just been seen */
2483 if (c == -1)
2484 {
2485 if (p == read_buffer)
2486 cancel = 1;
2487 continue;
2488 }
2489
2490 modifiers = c & CHAR_MODIFIER_MASK;
2491 c = c & ~CHAR_MODIFIER_MASK;
2492
2493 if (CHAR_BYTE8_P (c))
2494 force_singlebyte = 1;
2495 else if (! ASCII_CHAR_P (c))
2496 force_multibyte = 1;
2497 else /* i.e. ASCII_CHAR_P (c) */
2498 {
2499 /* Allow `\C- ' and `\C-?'. */
2500 if (modifiers == CHAR_CTL)
2501 {
2502 if (c == ' ')
2503 c = 0, modifiers = 0;
2504 else if (c == '?')
2505 c = 127, modifiers = 0;
2506 }
2507 if (modifiers & CHAR_SHIFT)
2508 {
2509 /* Shift modifier is valid only with [A-Za-z]. */
2510 if (c >= 'A' && c <= 'Z')
2511 modifiers &= ~CHAR_SHIFT;
2512 else if (c >= 'a' && c <= 'z')
2513 c -= ('a' - 'A'), modifiers &= ~CHAR_SHIFT;
2514 }
2515
2516 if (modifiers & CHAR_META)
2517 {
2518 /* Move the meta bit to the right place for a
2519 string. */
2520 modifiers &= ~CHAR_META;
2521 c = BYTE8_TO_CHAR (c | 0x80);
2522 force_singlebyte = 1;
2523 }
2524 }
2525
2526 /* Any modifiers remaining are invalid. */
2527 if (modifiers)
2528 error ("Invalid modifier in string");
2529 p += CHAR_STRING (c, (unsigned char *) p);
2530 }
2531 else
2532 {
2533 p += CHAR_STRING (c, (unsigned char *) p);
2534 if (CHAR_BYTE8_P (c))
2535 force_singlebyte = 1;
2536 else if (! ASCII_CHAR_P (c))
2537 force_multibyte = 1;
2538 }
2539 nchars++;
2540 }
2541
2542 if (c < 0)
2543 end_of_file_error ();
2544
2545 /* If purifying, and string starts with \ newline,
2546 return zero instead. This is for doc strings
2547 that we are really going to find in etc/DOC.nn.nn */
2548 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2549 return make_number (0);
2550
2551 if (force_multibyte)
2552 /* READ_BUFFER already contains valid multibyte forms. */
2553 ;
2554 else if (force_singlebyte)
2555 {
2556 nchars = str_as_unibyte (read_buffer, p - read_buffer);
2557 p = read_buffer + nchars;
2558 }
2559 else
2560 /* Otherwise, READ_BUFFER contains only ASCII. */
2561 ;
2562
2563 /* We want readchar_count to be the number of characters, not
2564 bytes. Hence we adjust for multibyte characters in the
2565 string. ... But it doesn't seem to be necessary, because
2566 READCHAR *does* read multibyte characters from buffers. */
2567 /* readchar_count -= (p - read_buffer) - nchars; */
2568 if (read_pure)
2569 return make_pure_string (read_buffer, nchars, p - read_buffer,
2570 (force_multibyte
2571 || (p - read_buffer != nchars)));
2572 return make_specified_string (read_buffer, nchars, p - read_buffer,
2573 (force_multibyte
2574 || (p - read_buffer != nchars)));
2575 }
2576
2577 case '.':
2578 {
2579 int next_char = READCHAR;
2580 UNREAD (next_char);
2581
2582 if (next_char <= 040
2583 || (next_char < 0200
2584 && index ("\"';([#?", next_char)
2585 || (!first_in_list && next_char == '`')
2586 || (new_backquote_flag && next_char == ',')))
2587 {
2588 *pch = c;
2589 return Qnil;
2590 }
2591
2592 /* Otherwise, we fall through! Note that the atom-reading loop
2593 below will now loop at least once, assuring that we will not
2594 try to UNREAD two characters in a row. */
2595 }
2596 default:
2597 default_label:
2598 if (c <= 040) goto retry;
2599 {
2600 char *p = read_buffer;
2601 int quoted = 0;
2602
2603 {
2604 char *end = read_buffer + read_buffer_size;
2605
2606 while (c > 040
2607 && (c >= 0200
2608 || (!index ("\"';()[]#", c)
2609 && !(!first_in_list && c == '`')
2610 && !(new_backquote_flag && c == ','))))
2611 {
2612 if (end - p < MAX_MULTIBYTE_LENGTH)
2613 {
2614 int offset = p - read_buffer;
2615 read_buffer = (char *) xrealloc (read_buffer,
2616 read_buffer_size *= 2);
2617 p = read_buffer + offset;
2618 end = read_buffer + read_buffer_size;
2619 }
2620
2621 if (c == '\\')
2622 {
2623 c = READCHAR;
2624 if (c == -1)
2625 end_of_file_error ();
2626 quoted = 1;
2627 }
2628
2629 p += CHAR_STRING (c, p);
2630 c = READCHAR;
2631 }
2632
2633 if (p == end)
2634 {
2635 int offset = p - read_buffer;
2636 read_buffer = (char *) xrealloc (read_buffer,
2637 read_buffer_size *= 2);
2638 p = read_buffer + offset;
2639 end = read_buffer + read_buffer_size;
2640 }
2641 *p = 0;
2642 if (c >= 0)
2643 UNREAD (c);
2644 }
2645
2646 if (!quoted && !uninterned_symbol)
2647 {
2648 register char *p1;
2649 register Lisp_Object val;
2650 p1 = read_buffer;
2651 if (*p1 == '+' || *p1 == '-') p1++;
2652 /* Is it an integer? */
2653 if (p1 != p)
2654 {
2655 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2656 /* Integers can have trailing decimal points. */
2657 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2658 if (p1 == p)
2659 /* It is an integer. */
2660 {
2661 if (p1[-1] == '.')
2662 p1[-1] = '\0';
2663 /* Fixme: if we have strtol, use that, and check
2664 for overflow. */
2665 if (sizeof (int) == sizeof (EMACS_INT))
2666 XSETINT (val, atoi (read_buffer));
2667 else if (sizeof (long) == sizeof (EMACS_INT))
2668 XSETINT (val, atol (read_buffer));
2669 else
2670 abort ();
2671 return val;
2672 }
2673 }
2674 if (isfloat_string (read_buffer))
2675 {
2676 /* Compute NaN and infinities using 0.0 in a variable,
2677 to cope with compilers that think they are smarter
2678 than we are. */
2679 double zero = 0.0;
2680
2681 double value;
2682
2683 /* Negate the value ourselves. This treats 0, NaNs,
2684 and infinity properly on IEEE floating point hosts,
2685 and works around a common bug where atof ("-0.0")
2686 drops the sign. */
2687 int negative = read_buffer[0] == '-';
2688
2689 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2690 returns 1, is if the input ends in e+INF or e+NaN. */
2691 switch (p[-1])
2692 {
2693 case 'F':
2694 value = 1.0 / zero;
2695 break;
2696 case 'N':
2697 value = zero / zero;
2698 break;
2699 default:
2700 value = atof (read_buffer + negative);
2701 break;
2702 }
2703
2704 return make_float (negative ? - value : value);
2705 }
2706 }
2707 {
2708 Lisp_Object result = uninterned_symbol ? make_symbol (read_buffer)
2709 : intern (read_buffer);
2710 if (EQ (Vread_with_symbol_positions, Qt)
2711 || EQ (Vread_with_symbol_positions, readcharfun))
2712 Vread_symbol_positions_list =
2713 /* Kind of a hack; this will probably fail if characters
2714 in the symbol name were escaped. Not really a big
2715 deal, though. */
2716 Fcons (Fcons (result,
2717 make_number (readchar_count
2718 - XFASTINT (Flength (Fsymbol_name (result))))),
2719 Vread_symbol_positions_list);
2720 return result;
2721 }
2722 }
2723 }
2724 }
2725 \f
2726
2727 /* List of nodes we've seen during substitute_object_in_subtree. */
2728 static Lisp_Object seen_list;
2729
2730 static void
2731 substitute_object_in_subtree (object, placeholder)
2732 Lisp_Object object;
2733 Lisp_Object placeholder;
2734 {
2735 Lisp_Object check_object;
2736
2737 /* We haven't seen any objects when we start. */
2738 seen_list = Qnil;
2739
2740 /* Make all the substitutions. */
2741 check_object
2742 = substitute_object_recurse (object, placeholder, object);
2743
2744 /* Clear seen_list because we're done with it. */
2745 seen_list = Qnil;
2746
2747 /* The returned object here is expected to always eq the
2748 original. */
2749 if (!EQ (check_object, object))
2750 error ("Unexpected mutation error in reader");
2751 }
2752
2753 /* Feval doesn't get called from here, so no gc protection is needed. */
2754 #define SUBSTITUTE(get_val, set_val) \
2755 { \
2756 Lisp_Object old_value = get_val; \
2757 Lisp_Object true_value \
2758 = substitute_object_recurse (object, placeholder,\
2759 old_value); \
2760 \
2761 if (!EQ (old_value, true_value)) \
2762 { \
2763 set_val; \
2764 } \
2765 }
2766
2767 static Lisp_Object
2768 substitute_object_recurse (object, placeholder, subtree)
2769 Lisp_Object object;
2770 Lisp_Object placeholder;
2771 Lisp_Object subtree;
2772 {
2773 /* If we find the placeholder, return the target object. */
2774 if (EQ (placeholder, subtree))
2775 return object;
2776
2777 /* If we've been to this node before, don't explore it again. */
2778 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2779 return subtree;
2780
2781 /* If this node can be the entry point to a cycle, remember that
2782 we've seen it. It can only be such an entry point if it was made
2783 by #n=, which means that we can find it as a value in
2784 read_objects. */
2785 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2786 seen_list = Fcons (subtree, seen_list);
2787
2788 /* Recurse according to subtree's type.
2789 Every branch must return a Lisp_Object. */
2790 switch (XTYPE (subtree))
2791 {
2792 case Lisp_Vectorlike:
2793 {
2794 int i;
2795 int length = XINT (Flength(subtree));
2796 for (i = 0; i < length; i++)
2797 {
2798 Lisp_Object idx = make_number (i);
2799 SUBSTITUTE (Faref (subtree, idx),
2800 Faset (subtree, idx, true_value));
2801 }
2802 return subtree;
2803 }
2804
2805 case Lisp_Cons:
2806 {
2807 SUBSTITUTE (Fcar_safe (subtree),
2808 Fsetcar (subtree, true_value));
2809 SUBSTITUTE (Fcdr_safe (subtree),
2810 Fsetcdr (subtree, true_value));
2811 return subtree;
2812 }
2813
2814 case Lisp_String:
2815 {
2816 /* Check for text properties in each interval.
2817 substitute_in_interval contains part of the logic. */
2818
2819 INTERVAL root_interval = STRING_INTERVALS (subtree);
2820 Lisp_Object arg = Fcons (object, placeholder);
2821
2822 traverse_intervals_noorder (root_interval,
2823 &substitute_in_interval, arg);
2824
2825 return subtree;
2826 }
2827
2828 /* Other types don't recurse any further. */
2829 default:
2830 return subtree;
2831 }
2832 }
2833
2834 /* Helper function for substitute_object_recurse. */
2835 static void
2836 substitute_in_interval (interval, arg)
2837 INTERVAL interval;
2838 Lisp_Object arg;
2839 {
2840 Lisp_Object object = Fcar (arg);
2841 Lisp_Object placeholder = Fcdr (arg);
2842
2843 SUBSTITUTE(interval->plist, interval->plist = true_value);
2844 }
2845
2846 \f
2847 #define LEAD_INT 1
2848 #define DOT_CHAR 2
2849 #define TRAIL_INT 4
2850 #define E_CHAR 8
2851 #define EXP_INT 16
2852
2853 int
2854 isfloat_string (cp)
2855 register char *cp;
2856 {
2857 register int state;
2858
2859 char *start = cp;
2860
2861 state = 0;
2862 if (*cp == '+' || *cp == '-')
2863 cp++;
2864
2865 if (*cp >= '0' && *cp <= '9')
2866 {
2867 state |= LEAD_INT;
2868 while (*cp >= '0' && *cp <= '9')
2869 cp++;
2870 }
2871 if (*cp == '.')
2872 {
2873 state |= DOT_CHAR;
2874 cp++;
2875 }
2876 if (*cp >= '0' && *cp <= '9')
2877 {
2878 state |= TRAIL_INT;
2879 while (*cp >= '0' && *cp <= '9')
2880 cp++;
2881 }
2882 if (*cp == 'e' || *cp == 'E')
2883 {
2884 state |= E_CHAR;
2885 cp++;
2886 if (*cp == '+' || *cp == '-')
2887 cp++;
2888 }
2889
2890 if (*cp >= '0' && *cp <= '9')
2891 {
2892 state |= EXP_INT;
2893 while (*cp >= '0' && *cp <= '9')
2894 cp++;
2895 }
2896 else if (cp == start)
2897 ;
2898 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2899 {
2900 state |= EXP_INT;
2901 cp += 3;
2902 }
2903 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2904 {
2905 state |= EXP_INT;
2906 cp += 3;
2907 }
2908
2909 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2910 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2911 || state == (DOT_CHAR|TRAIL_INT)
2912 || state == (LEAD_INT|E_CHAR|EXP_INT)
2913 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2914 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2915 }
2916
2917 \f
2918 static Lisp_Object
2919 read_vector (readcharfun, bytecodeflag)
2920 Lisp_Object readcharfun;
2921 int bytecodeflag;
2922 {
2923 register int i;
2924 register int size;
2925 register Lisp_Object *ptr;
2926 register Lisp_Object tem, item, vector;
2927 register struct Lisp_Cons *otem;
2928 Lisp_Object len;
2929
2930 tem = read_list (1, readcharfun);
2931 len = Flength (tem);
2932 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2933
2934 size = XVECTOR (vector)->size;
2935 ptr = XVECTOR (vector)->contents;
2936 for (i = 0; i < size; i++)
2937 {
2938 item = Fcar (tem);
2939 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2940 bytecode object, the docstring containing the bytecode and
2941 constants values must be treated as unibyte and passed to
2942 Fread, to get the actual bytecode string and constants vector. */
2943 if (bytecodeflag && load_force_doc_strings)
2944 {
2945 if (i == COMPILED_BYTECODE)
2946 {
2947 if (!STRINGP (item))
2948 error ("invalid byte code");
2949
2950 /* Delay handling the bytecode slot until we know whether
2951 it is lazily-loaded (we can tell by whether the
2952 constants slot is nil). */
2953 ptr[COMPILED_CONSTANTS] = item;
2954 item = Qnil;
2955 }
2956 else if (i == COMPILED_CONSTANTS)
2957 {
2958 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2959
2960 if (NILP (item))
2961 {
2962 /* Coerce string to unibyte (like string-as-unibyte,
2963 but without generating extra garbage and
2964 guaranteeing no change in the contents). */
2965 STRING_SET_CHARS (bytestr, SBYTES (bytestr));
2966 STRING_SET_UNIBYTE (bytestr);
2967
2968 item = Fread (Fcons (bytestr, readcharfun));
2969 if (!CONSP (item))
2970 error ("invalid byte code");
2971
2972 otem = XCONS (item);
2973 bytestr = XCAR (item);
2974 item = XCDR (item);
2975 free_cons (otem);
2976 }
2977
2978 /* Now handle the bytecode slot. */
2979 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2980 }
2981 else if (i == COMPILED_DOC_STRING
2982 && STRINGP (item)
2983 && ! STRING_MULTIBYTE (item))
2984 {
2985 if (EQ (readcharfun, Qget_emacs_mule_file_char))
2986 item = Fdecode_coding_string (item, Qemacs_mule, Qnil, Qnil);
2987 else
2988 item = Fstring_as_multibyte (item);
2989 }
2990 }
2991 ptr[i] = read_pure ? Fpurecopy (item) : item;
2992 otem = XCONS (tem);
2993 tem = Fcdr (tem);
2994 free_cons (otem);
2995 }
2996 return vector;
2997 }
2998
2999 /* FLAG = 1 means check for ] to terminate rather than ) and .
3000 FLAG = -1 means check for starting with defun
3001 and make structure pure. */
3002
3003 static Lisp_Object
3004 read_list (flag, readcharfun)
3005 int flag;
3006 register Lisp_Object readcharfun;
3007 {
3008 /* -1 means check next element for defun,
3009 0 means don't check,
3010 1 means already checked and found defun. */
3011 int defunflag = flag < 0 ? -1 : 0;
3012 Lisp_Object val, tail;
3013 register Lisp_Object elt, tem;
3014 struct gcpro gcpro1, gcpro2;
3015 /* 0 is the normal case.
3016 1 means this list is a doc reference; replace it with the number 0.
3017 2 means this list is a doc reference; replace it with the doc string. */
3018 int doc_reference = 0;
3019
3020 /* Initialize this to 1 if we are reading a list. */
3021 int first_in_list = flag <= 0;
3022
3023 val = Qnil;
3024 tail = Qnil;
3025
3026 while (1)
3027 {
3028 int ch;
3029 GCPRO2 (val, tail);
3030 elt = read1 (readcharfun, &ch, first_in_list);
3031 UNGCPRO;
3032
3033 first_in_list = 0;
3034
3035 /* While building, if the list starts with #$, treat it specially. */
3036 if (EQ (elt, Vload_file_name)
3037 && ! NILP (elt)
3038 && !NILP (Vpurify_flag))
3039 {
3040 if (NILP (Vdoc_file_name))
3041 /* We have not yet called Snarf-documentation, so assume
3042 this file is described in the DOC-MM.NN file
3043 and Snarf-documentation will fill in the right value later.
3044 For now, replace the whole list with 0. */
3045 doc_reference = 1;
3046 else
3047 /* We have already called Snarf-documentation, so make a relative
3048 file name for this file, so it can be found properly
3049 in the installed Lisp directory.
3050 We don't use Fexpand_file_name because that would make
3051 the directory absolute now. */
3052 elt = concat2 (build_string ("../lisp/"),
3053 Ffile_name_nondirectory (elt));
3054 }
3055 else if (EQ (elt, Vload_file_name)
3056 && ! NILP (elt)
3057 && load_force_doc_strings)
3058 doc_reference = 2;
3059
3060 if (ch)
3061 {
3062 if (flag > 0)
3063 {
3064 if (ch == ']')
3065 return val;
3066 Fsignal (Qinvalid_read_syntax,
3067 Fcons (make_string (") or . in a vector", 18), Qnil));
3068 }
3069 if (ch == ')')
3070 return val;
3071 if (ch == '.')
3072 {
3073 GCPRO2 (val, tail);
3074 if (!NILP (tail))
3075 XSETCDR (tail, read0 (readcharfun));
3076 else
3077 val = read0 (readcharfun);
3078 read1 (readcharfun, &ch, 0);
3079 UNGCPRO;
3080 if (ch == ')')
3081 {
3082 if (doc_reference == 1)
3083 return make_number (0);
3084 if (doc_reference == 2)
3085 {
3086 /* Get a doc string from the file we are loading.
3087 If it's in saved_doc_string, get it from there.
3088
3089 Here, we don't know if the string is a
3090 bytecode string or a doc string. As a
3091 bytecode string must be unibyte, we always
3092 return a unibyte string. If it is actually a
3093 doc string, caller must make it
3094 multibyte. */
3095
3096 int pos = XINT (XCDR (val));
3097 /* Position is negative for user variables. */
3098 if (pos < 0) pos = -pos;
3099 if (pos >= saved_doc_string_position
3100 && pos < (saved_doc_string_position
3101 + saved_doc_string_length))
3102 {
3103 int start = pos - saved_doc_string_position;
3104 int from, to;
3105
3106 /* Process quoting with ^A,
3107 and find the end of the string,
3108 which is marked with ^_ (037). */
3109 for (from = start, to = start;
3110 saved_doc_string[from] != 037;)
3111 {
3112 int c = saved_doc_string[from++];
3113 if (c == 1)
3114 {
3115 c = saved_doc_string[from++];
3116 if (c == 1)
3117 saved_doc_string[to++] = c;
3118 else if (c == '0')
3119 saved_doc_string[to++] = 0;
3120 else if (c == '_')
3121 saved_doc_string[to++] = 037;
3122 }
3123 else
3124 saved_doc_string[to++] = c;
3125 }
3126
3127 return make_unibyte_string (saved_doc_string + start,
3128 to - start);
3129 }
3130 /* Look in prev_saved_doc_string the same way. */
3131 else if (pos >= prev_saved_doc_string_position
3132 && pos < (prev_saved_doc_string_position
3133 + prev_saved_doc_string_length))
3134 {
3135 int start = pos - prev_saved_doc_string_position;
3136 int from, to;
3137
3138 /* Process quoting with ^A,
3139 and find the end of the string,
3140 which is marked with ^_ (037). */
3141 for (from = start, to = start;
3142 prev_saved_doc_string[from] != 037;)
3143 {
3144 int c = prev_saved_doc_string[from++];
3145 if (c == 1)
3146 {
3147 c = prev_saved_doc_string[from++];
3148 if (c == 1)
3149 prev_saved_doc_string[to++] = c;
3150 else if (c == '0')
3151 prev_saved_doc_string[to++] = 0;
3152 else if (c == '_')
3153 prev_saved_doc_string[to++] = 037;
3154 }
3155 else
3156 prev_saved_doc_string[to++] = c;
3157 }
3158
3159 return make_unibyte_string (prev_saved_doc_string
3160 + start,
3161 to - start);
3162 }
3163 else
3164 return get_doc_string (val, 1, 0);
3165 }
3166
3167 return val;
3168 }
3169 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
3170 }
3171 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
3172 }
3173 tem = (read_pure && flag <= 0
3174 ? pure_cons (elt, Qnil)
3175 : Fcons (elt, Qnil));
3176 if (!NILP (tail))
3177 XSETCDR (tail, tem);
3178 else
3179 val = tem;
3180 tail = tem;
3181 if (defunflag < 0)
3182 defunflag = EQ (elt, Qdefun);
3183 else if (defunflag > 0)
3184 read_pure = 1;
3185 }
3186 }
3187 \f
3188 Lisp_Object Vobarray;
3189 Lisp_Object initial_obarray;
3190
3191 /* oblookup stores the bucket number here, for the sake of Funintern. */
3192
3193 int oblookup_last_bucket_number;
3194
3195 static int hash_string ();
3196
3197 /* Get an error if OBARRAY is not an obarray.
3198 If it is one, return it. */
3199
3200 Lisp_Object
3201 check_obarray (obarray)
3202 Lisp_Object obarray;
3203 {
3204 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
3205 {
3206 /* If Vobarray is now invalid, force it to be valid. */
3207 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
3208
3209 obarray = wrong_type_argument (Qvectorp, obarray);
3210 }
3211 return obarray;
3212 }
3213
3214 /* Intern the C string STR: return a symbol with that name,
3215 interned in the current obarray. */
3216
3217 Lisp_Object
3218 intern (str)
3219 const char *str;
3220 {
3221 Lisp_Object tem;
3222 int len = strlen (str);
3223 Lisp_Object obarray;
3224
3225 obarray = Vobarray;
3226 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
3227 obarray = check_obarray (obarray);
3228 tem = oblookup (obarray, str, len, len);
3229 if (SYMBOLP (tem))
3230 return tem;
3231 return Fintern (make_string (str, len), obarray);
3232 }
3233
3234 /* Create an uninterned symbol with name STR. */
3235
3236 Lisp_Object
3237 make_symbol (str)
3238 char *str;
3239 {
3240 int len = strlen (str);
3241
3242 return Fmake_symbol ((!NILP (Vpurify_flag)
3243 ? make_pure_string (str, len, len, 0)
3244 : make_string (str, len)));
3245 }
3246 \f
3247 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
3248 doc: /* Return the canonical symbol whose name is STRING.
3249 If there is none, one is created by this function and returned.
3250 A second optional argument specifies the obarray to use;
3251 it defaults to the value of `obarray'. */)
3252 (string, obarray)
3253 Lisp_Object string, obarray;
3254 {
3255 register Lisp_Object tem, sym, *ptr;
3256
3257 if (NILP (obarray)) obarray = Vobarray;
3258 obarray = check_obarray (obarray);
3259
3260 CHECK_STRING (string);
3261
3262 tem = oblookup (obarray, SDATA (string),
3263 SCHARS (string),
3264 SBYTES (string));
3265 if (!INTEGERP (tem))
3266 return tem;
3267
3268 if (!NILP (Vpurify_flag))
3269 string = Fpurecopy (string);
3270 sym = Fmake_symbol (string);
3271
3272 if (EQ (obarray, initial_obarray))
3273 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3274 else
3275 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
3276
3277 if ((SREF (string, 0) == ':')
3278 && EQ (obarray, initial_obarray))
3279 {
3280 XSYMBOL (sym)->constant = 1;
3281 XSYMBOL (sym)->value = sym;
3282 }
3283
3284 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
3285 if (SYMBOLP (*ptr))
3286 XSYMBOL (sym)->next = XSYMBOL (*ptr);
3287 else
3288 XSYMBOL (sym)->next = 0;
3289 *ptr = sym;
3290 return sym;
3291 }
3292
3293 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
3294 doc: /* Return the canonical symbol named NAME, or nil if none exists.
3295 NAME may be a string or a symbol. If it is a symbol, that exact
3296 symbol is searched for.
3297 A second optional argument specifies the obarray to use;
3298 it defaults to the value of `obarray'. */)
3299 (name, obarray)
3300 Lisp_Object name, obarray;
3301 {
3302 register Lisp_Object tem, string;
3303
3304 if (NILP (obarray)) obarray = Vobarray;
3305 obarray = check_obarray (obarray);
3306
3307 if (!SYMBOLP (name))
3308 {
3309 CHECK_STRING (name);
3310 string = name;
3311 }
3312 else
3313 string = SYMBOL_NAME (name);
3314
3315 tem = oblookup (obarray, SDATA (string), SCHARS (string), SBYTES (string));
3316 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
3317 return Qnil;
3318 else
3319 return tem;
3320 }
3321 \f
3322 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
3323 doc: /* Delete the symbol named NAME, if any, from OBARRAY.
3324 The value is t if a symbol was found and deleted, nil otherwise.
3325 NAME may be a string or a symbol. If it is a symbol, that symbol
3326 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
3327 OBARRAY defaults to the value of the variable `obarray'. */)
3328 (name, obarray)
3329 Lisp_Object name, obarray;
3330 {
3331 register Lisp_Object string, tem;
3332 int hash;
3333
3334 if (NILP (obarray)) obarray = Vobarray;
3335 obarray = check_obarray (obarray);
3336
3337 if (SYMBOLP (name))
3338 string = SYMBOL_NAME (name);
3339 else
3340 {
3341 CHECK_STRING (name);
3342 string = name;
3343 }
3344
3345 tem = oblookup (obarray, SDATA (string),
3346 SCHARS (string),
3347 SBYTES (string));
3348 if (INTEGERP (tem))
3349 return Qnil;
3350 /* If arg was a symbol, don't delete anything but that symbol itself. */
3351 if (SYMBOLP (name) && !EQ (name, tem))
3352 return Qnil;
3353
3354 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3355 XSYMBOL (tem)->constant = 0;
3356 XSYMBOL (tem)->indirect_variable = 0;
3357
3358 hash = oblookup_last_bucket_number;
3359
3360 if (EQ (XVECTOR (obarray)->contents[hash], tem))
3361 {
3362 if (XSYMBOL (tem)->next)
3363 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
3364 else
3365 XSETINT (XVECTOR (obarray)->contents[hash], 0);
3366 }
3367 else
3368 {
3369 Lisp_Object tail, following;
3370
3371 for (tail = XVECTOR (obarray)->contents[hash];
3372 XSYMBOL (tail)->next;
3373 tail = following)
3374 {
3375 XSETSYMBOL (following, XSYMBOL (tail)->next);
3376 if (EQ (following, tem))
3377 {
3378 XSYMBOL (tail)->next = XSYMBOL (following)->next;
3379 break;
3380 }
3381 }
3382 }
3383
3384 return Qt;
3385 }
3386 \f
3387 /* Return the symbol in OBARRAY whose names matches the string
3388 of SIZE characters (SIZE_BYTE bytes) at PTR.
3389 If there is no such symbol in OBARRAY, return nil.
3390
3391 Also store the bucket number in oblookup_last_bucket_number. */
3392
3393 Lisp_Object
3394 oblookup (obarray, ptr, size, size_byte)
3395 Lisp_Object obarray;
3396 register const char *ptr;
3397 int size, size_byte;
3398 {
3399 int hash;
3400 int obsize;
3401 register Lisp_Object tail;
3402 Lisp_Object bucket, tem;
3403
3404 if (!VECTORP (obarray)
3405 || (obsize = XVECTOR (obarray)->size) == 0)
3406 {
3407 obarray = check_obarray (obarray);
3408 obsize = XVECTOR (obarray)->size;
3409 }
3410 /* This is sometimes needed in the middle of GC. */
3411 obsize &= ~ARRAY_MARK_FLAG;
3412 /* Combining next two lines breaks VMS C 2.3. */
3413 hash = hash_string (ptr, size_byte);
3414 hash %= obsize;
3415 bucket = XVECTOR (obarray)->contents[hash];
3416 oblookup_last_bucket_number = hash;
3417 if (XFASTINT (bucket) == 0)
3418 ;
3419 else if (!SYMBOLP (bucket))
3420 error ("Bad data in guts of obarray"); /* Like CADR error message */
3421 else
3422 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3423 {
3424 if (SBYTES (SYMBOL_NAME (tail)) == size_byte
3425 && SCHARS (SYMBOL_NAME (tail)) == size
3426 && !bcmp (SDATA (SYMBOL_NAME (tail)), ptr, size_byte))
3427 return tail;
3428 else if (XSYMBOL (tail)->next == 0)
3429 break;
3430 }
3431 XSETINT (tem, hash);
3432 return tem;
3433 }
3434
3435 static int
3436 hash_string (ptr, len)
3437 const unsigned char *ptr;
3438 int len;
3439 {
3440 register const unsigned char *p = ptr;
3441 register const unsigned char *end = p + len;
3442 register unsigned char c;
3443 register int hash = 0;
3444
3445 while (p != end)
3446 {
3447 c = *p++;
3448 if (c >= 0140) c -= 40;
3449 hash = ((hash<<3) + (hash>>28) + c);
3450 }
3451 return hash & 07777777777;
3452 }
3453 \f
3454 void
3455 map_obarray (obarray, fn, arg)
3456 Lisp_Object obarray;
3457 void (*fn) P_ ((Lisp_Object, Lisp_Object));
3458 Lisp_Object arg;
3459 {
3460 register int i;
3461 register Lisp_Object tail;
3462 CHECK_VECTOR (obarray);
3463 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3464 {
3465 tail = XVECTOR (obarray)->contents[i];
3466 if (SYMBOLP (tail))
3467 while (1)
3468 {
3469 (*fn) (tail, arg);
3470 if (XSYMBOL (tail)->next == 0)
3471 break;
3472 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3473 }
3474 }
3475 }
3476
3477 void
3478 mapatoms_1 (sym, function)
3479 Lisp_Object sym, function;
3480 {
3481 call1 (function, sym);
3482 }
3483
3484 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3485 doc: /* Call FUNCTION on every symbol in OBARRAY.
3486 OBARRAY defaults to the value of `obarray'. */)
3487 (function, obarray)
3488 Lisp_Object function, obarray;
3489 {
3490 if (NILP (obarray)) obarray = Vobarray;
3491 obarray = check_obarray (obarray);
3492
3493 map_obarray (obarray, mapatoms_1, function);
3494 return Qnil;
3495 }
3496
3497 #define OBARRAY_SIZE 1511
3498
3499 void
3500 init_obarray ()
3501 {
3502 Lisp_Object oblength;
3503 int hash;
3504 Lisp_Object *tem;
3505
3506 XSETFASTINT (oblength, OBARRAY_SIZE);
3507
3508 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
3509 Vobarray = Fmake_vector (oblength, make_number (0));
3510 initial_obarray = Vobarray;
3511 staticpro (&initial_obarray);
3512 /* Intern nil in the obarray */
3513 XSYMBOL (Qnil)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3514 XSYMBOL (Qnil)->constant = 1;
3515
3516 /* These locals are to kludge around a pyramid compiler bug. */
3517 hash = hash_string ("nil", 3);
3518 /* Separate statement here to avoid VAXC bug. */
3519 hash %= OBARRAY_SIZE;
3520 tem = &XVECTOR (Vobarray)->contents[hash];
3521 *tem = Qnil;
3522
3523 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
3524 XSYMBOL (Qnil)->function = Qunbound;
3525 XSYMBOL (Qunbound)->value = Qunbound;
3526 XSYMBOL (Qunbound)->function = Qunbound;
3527
3528 Qt = intern ("t");
3529 XSYMBOL (Qnil)->value = Qnil;
3530 XSYMBOL (Qnil)->plist = Qnil;
3531 XSYMBOL (Qt)->value = Qt;
3532 XSYMBOL (Qt)->constant = 1;
3533
3534 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3535 Vpurify_flag = Qt;
3536
3537 Qvariable_documentation = intern ("variable-documentation");
3538 staticpro (&Qvariable_documentation);
3539
3540 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3541 read_buffer = (char *) xmalloc (read_buffer_size);
3542 }
3543 \f
3544 void
3545 defsubr (sname)
3546 struct Lisp_Subr *sname;
3547 {
3548 Lisp_Object sym;
3549 sym = intern (sname->symbol_name);
3550 XSETSUBR (XSYMBOL (sym)->function, sname);
3551 }
3552
3553 #ifdef NOTDEF /* use fset in subr.el now */
3554 void
3555 defalias (sname, string)
3556 struct Lisp_Subr *sname;
3557 char *string;
3558 {
3559 Lisp_Object sym;
3560 sym = intern (string);
3561 XSETSUBR (XSYMBOL (sym)->function, sname);
3562 }
3563 #endif /* NOTDEF */
3564
3565 /* Define an "integer variable"; a symbol whose value is forwarded
3566 to a C variable of type int. Sample call: */
3567 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3568 void
3569 defvar_int (namestring, address)
3570 char *namestring;
3571 EMACS_INT *address;
3572 {
3573 Lisp_Object sym, val;
3574 sym = intern (namestring);
3575 val = allocate_misc ();
3576 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3577 XINTFWD (val)->intvar = address;
3578 SET_SYMBOL_VALUE (sym, val);
3579 }
3580
3581 /* Similar but define a variable whose value is t if address contains 1,
3582 nil if address contains 0 */
3583 void
3584 defvar_bool (namestring, address)
3585 char *namestring;
3586 int *address;
3587 {
3588 Lisp_Object sym, val;
3589 sym = intern (namestring);
3590 val = allocate_misc ();
3591 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3592 XBOOLFWD (val)->boolvar = address;
3593 SET_SYMBOL_VALUE (sym, val);
3594 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3595 }
3596
3597 /* Similar but define a variable whose value is the Lisp Object stored
3598 at address. Two versions: with and without gc-marking of the C
3599 variable. The nopro version is used when that variable will be
3600 gc-marked for some other reason, since marking the same slot twice
3601 can cause trouble with strings. */
3602 void
3603 defvar_lisp_nopro (namestring, address)
3604 char *namestring;
3605 Lisp_Object *address;
3606 {
3607 Lisp_Object sym, val;
3608 sym = intern (namestring);
3609 val = allocate_misc ();
3610 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3611 XOBJFWD (val)->objvar = address;
3612 SET_SYMBOL_VALUE (sym, val);
3613 }
3614
3615 void
3616 defvar_lisp (namestring, address)
3617 char *namestring;
3618 Lisp_Object *address;
3619 {
3620 defvar_lisp_nopro (namestring, address);
3621 staticpro (address);
3622 }
3623
3624 /* Similar but define a variable whose value is the Lisp Object stored in
3625 the current buffer. address is the address of the slot in the buffer
3626 that is current now. */
3627
3628 void
3629 defvar_per_buffer (namestring, address, type, doc)
3630 char *namestring;
3631 Lisp_Object *address;
3632 Lisp_Object type;
3633 char *doc;
3634 {
3635 Lisp_Object sym, val;
3636 int offset;
3637
3638 sym = intern (namestring);
3639 val = allocate_misc ();
3640 offset = (char *)address - (char *)current_buffer;
3641
3642 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3643 XBUFFER_OBJFWD (val)->offset = offset;
3644 SET_SYMBOL_VALUE (sym, val);
3645 PER_BUFFER_SYMBOL (offset) = sym;
3646 PER_BUFFER_TYPE (offset) = type;
3647
3648 if (PER_BUFFER_IDX (offset) == 0)
3649 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3650 slot of buffer_local_flags */
3651 abort ();
3652 }
3653
3654
3655 /* Similar but define a variable whose value is the Lisp Object stored
3656 at a particular offset in the current kboard object. */
3657
3658 void
3659 defvar_kboard (namestring, offset)
3660 char *namestring;
3661 int offset;
3662 {
3663 Lisp_Object sym, val;
3664 sym = intern (namestring);
3665 val = allocate_misc ();
3666 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3667 XKBOARD_OBJFWD (val)->offset = offset;
3668 SET_SYMBOL_VALUE (sym, val);
3669 }
3670 \f
3671 /* Record the value of load-path used at the start of dumping
3672 so we can see if the site changed it later during dumping. */
3673 static Lisp_Object dump_path;
3674
3675 void
3676 init_lread ()
3677 {
3678 char *normal;
3679 int turn_off_warning = 0;
3680
3681 /* Compute the default load-path. */
3682 #ifdef CANNOT_DUMP
3683 normal = PATH_LOADSEARCH;
3684 Vload_path = decode_env_path (0, normal);
3685 #else
3686 if (NILP (Vpurify_flag))
3687 normal = PATH_LOADSEARCH;
3688 else
3689 normal = PATH_DUMPLOADSEARCH;
3690
3691 /* In a dumped Emacs, we normally have to reset the value of
3692 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3693 uses ../lisp, instead of the path of the installed elisp
3694 libraries. However, if it appears that Vload_path was changed
3695 from the default before dumping, don't override that value. */
3696 if (initialized)
3697 {
3698 if (! NILP (Fequal (dump_path, Vload_path)))
3699 {
3700 Vload_path = decode_env_path (0, normal);
3701 if (!NILP (Vinstallation_directory))
3702 {
3703 Lisp_Object tem, tem1, sitelisp;
3704
3705 /* Remove site-lisp dirs from path temporarily and store
3706 them in sitelisp, then conc them on at the end so
3707 they're always first in path. */
3708 sitelisp = Qnil;
3709 while (1)
3710 {
3711 tem = Fcar (Vload_path);
3712 tem1 = Fstring_match (build_string ("site-lisp"),
3713 tem, Qnil);
3714 if (!NILP (tem1))
3715 {
3716 Vload_path = Fcdr (Vload_path);
3717 sitelisp = Fcons (tem, sitelisp);
3718 }
3719 else
3720 break;
3721 }
3722
3723 /* Add to the path the lisp subdir of the
3724 installation dir, if it exists. */
3725 tem = Fexpand_file_name (build_string ("lisp"),
3726 Vinstallation_directory);
3727 tem1 = Ffile_exists_p (tem);
3728 if (!NILP (tem1))
3729 {
3730 if (NILP (Fmember (tem, Vload_path)))
3731 {
3732 turn_off_warning = 1;
3733 Vload_path = Fcons (tem, Vload_path);
3734 }
3735 }
3736 else
3737 /* That dir doesn't exist, so add the build-time
3738 Lisp dirs instead. */
3739 Vload_path = nconc2 (Vload_path, dump_path);
3740
3741 /* Add leim under the installation dir, if it exists. */
3742 tem = Fexpand_file_name (build_string ("leim"),
3743 Vinstallation_directory);
3744 tem1 = Ffile_exists_p (tem);
3745 if (!NILP (tem1))
3746 {
3747 if (NILP (Fmember (tem, Vload_path)))
3748 Vload_path = Fcons (tem, Vload_path);
3749 }
3750
3751 /* Add site-list under the installation dir, if it exists. */
3752 tem = Fexpand_file_name (build_string ("site-lisp"),
3753 Vinstallation_directory);
3754 tem1 = Ffile_exists_p (tem);
3755 if (!NILP (tem1))
3756 {
3757 if (NILP (Fmember (tem, Vload_path)))
3758 Vload_path = Fcons (tem, Vload_path);
3759 }
3760
3761 /* If Emacs was not built in the source directory,
3762 and it is run from where it was built, add to load-path
3763 the lisp, leim and site-lisp dirs under that directory. */
3764
3765 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3766 {
3767 Lisp_Object tem2;
3768
3769 tem = Fexpand_file_name (build_string ("src/Makefile"),
3770 Vinstallation_directory);
3771 tem1 = Ffile_exists_p (tem);
3772
3773 /* Don't be fooled if they moved the entire source tree
3774 AFTER dumping Emacs. If the build directory is indeed
3775 different from the source dir, src/Makefile.in and
3776 src/Makefile will not be found together. */
3777 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3778 Vinstallation_directory);
3779 tem2 = Ffile_exists_p (tem);
3780 if (!NILP (tem1) && NILP (tem2))
3781 {
3782 tem = Fexpand_file_name (build_string ("lisp"),
3783 Vsource_directory);
3784
3785 if (NILP (Fmember (tem, Vload_path)))
3786 Vload_path = Fcons (tem, Vload_path);
3787
3788 tem = Fexpand_file_name (build_string ("leim"),
3789 Vsource_directory);
3790
3791 if (NILP (Fmember (tem, Vload_path)))
3792 Vload_path = Fcons (tem, Vload_path);
3793
3794 tem = Fexpand_file_name (build_string ("site-lisp"),
3795 Vsource_directory);
3796
3797 if (NILP (Fmember (tem, Vload_path)))
3798 Vload_path = Fcons (tem, Vload_path);
3799 }
3800 }
3801 if (!NILP (sitelisp))
3802 Vload_path = nconc2 (Fnreverse (sitelisp), Vload_path);
3803 }
3804 }
3805 }
3806 else
3807 {
3808 /* NORMAL refers to the lisp dir in the source directory. */
3809 /* We used to add ../lisp at the front here, but
3810 that caused trouble because it was copied from dump_path
3811 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3812 It should be unnecessary. */
3813 Vload_path = decode_env_path (0, normal);
3814 dump_path = Vload_path;
3815 }
3816 #endif
3817
3818 #if (!(defined(WINDOWSNT) || (defined(HAVE_CARBON))))
3819 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3820 almost never correct, thereby causing a warning to be printed out that
3821 confuses users. Since PATH_LOADSEARCH is always overridden by the
3822 EMACSLOADPATH environment variable below, disable the warning on NT.
3823 Also, when using the "self-contained" option for Carbon Emacs for MacOSX,
3824 the "standard" paths may not exist and would be overridden by
3825 EMACSLOADPATH as on NT. Since this depends on how the executable
3826 was build and packaged, turn off the warnings in general */
3827
3828 /* Warn if dirs in the *standard* path don't exist. */
3829 if (!turn_off_warning)
3830 {
3831 Lisp_Object path_tail;
3832
3833 for (path_tail = Vload_path;
3834 !NILP (path_tail);
3835 path_tail = XCDR (path_tail))
3836 {
3837 Lisp_Object dirfile;
3838 dirfile = Fcar (path_tail);
3839 if (STRINGP (dirfile))
3840 {
3841 dirfile = Fdirectory_file_name (dirfile);
3842 if (access (SDATA (dirfile), 0) < 0)
3843 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3844 XCAR (path_tail));
3845 }
3846 }
3847 }
3848 #endif /* !(WINDOWSNT || HAVE_CARBON) */
3849
3850 /* If the EMACSLOADPATH environment variable is set, use its value.
3851 This doesn't apply if we're dumping. */
3852 #ifndef CANNOT_DUMP
3853 if (NILP (Vpurify_flag)
3854 && egetenv ("EMACSLOADPATH"))
3855 #endif
3856 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3857
3858 Vvalues = Qnil;
3859
3860 load_in_progress = 0;
3861 Vload_file_name = Qnil;
3862
3863 load_descriptor_list = Qnil;
3864
3865 Vstandard_input = Qt;
3866 Vloads_in_progress = Qnil;
3867 }
3868
3869 /* Print a warning, using format string FORMAT, that directory DIRNAME
3870 does not exist. Print it on stderr and put it in *Message*. */
3871
3872 void
3873 dir_warning (format, dirname)
3874 char *format;
3875 Lisp_Object dirname;
3876 {
3877 char *buffer
3878 = (char *) alloca (SCHARS (dirname) + strlen (format) + 5);
3879
3880 fprintf (stderr, format, SDATA (dirname));
3881 sprintf (buffer, format, SDATA (dirname));
3882 /* Don't log the warning before we've initialized!! */
3883 if (initialized)
3884 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3885 }
3886
3887 void
3888 syms_of_lread ()
3889 {
3890 defsubr (&Sread);
3891 defsubr (&Sread_from_string);
3892 defsubr (&Sintern);
3893 defsubr (&Sintern_soft);
3894 defsubr (&Sunintern);
3895 defsubr (&Sload);
3896 defsubr (&Seval_buffer);
3897 defsubr (&Seval_region);
3898 defsubr (&Sread_char);
3899 defsubr (&Sread_char_exclusive);
3900 defsubr (&Sread_event);
3901 defsubr (&Sget_file_char);
3902 defsubr (&Smapatoms);
3903 defsubr (&Slocate_file_internal);
3904
3905 DEFVAR_LISP ("obarray", &Vobarray,
3906 doc: /* Symbol table for use by `intern' and `read'.
3907 It is a vector whose length ought to be prime for best results.
3908 The vector's contents don't make sense if examined from Lisp programs;
3909 to find all the symbols in an obarray, use `mapatoms'. */);
3910
3911 DEFVAR_LISP ("values", &Vvalues,
3912 doc: /* List of values of all expressions which were read, evaluated and printed.
3913 Order is reverse chronological. */);
3914
3915 DEFVAR_LISP ("standard-input", &Vstandard_input,
3916 doc: /* Stream for read to get input from.
3917 See documentation of `read' for possible values. */);
3918 Vstandard_input = Qt;
3919
3920 DEFVAR_LISP ("read-with-symbol-positions", &Vread_with_symbol_positions,
3921 doc: /* If non-nil, add position of read symbols to `read-symbol-positions-list'.
3922
3923 If this variable is a buffer, then only forms read from that buffer
3924 will be added to `read-symbol-positions-list'.
3925 If this variable is t, then all read forms will be added.
3926 The effect of all other values other than nil are not currently
3927 defined, although they may be in the future.
3928
3929 The positions are relative to the last call to `read' or
3930 `read-from-string'. It is probably a bad idea to set this variable at
3931 the toplevel; bind it instead. */);
3932 Vread_with_symbol_positions = Qnil;
3933
3934 DEFVAR_LISP ("read-symbol-positions-list", &Vread_symbol_positions_list,
3935 doc: /* A list mapping read symbols to their positions.
3936 This variable is modified during calls to `read' or
3937 `read-from-string', but only when `read-with-symbol-positions' is
3938 non-nil.
3939
3940 Each element of the list looks like (SYMBOL . CHAR-POSITION), where
3941 CHAR-POSITION is an integer giving the offset of that occurrence of the
3942 symbol from the position where `read' or `read-from-string' started.
3943
3944 Note that a symbol will appear multiple times in this list, if it was
3945 read multiple times. The list is in the same order as the symbols
3946 were read in. */);
3947 Vread_symbol_positions_list = Qnil;
3948
3949 DEFVAR_LISP ("load-path", &Vload_path,
3950 doc: /* *List of directories to search for files to load.
3951 Each element is a string (directory name) or nil (try default directory).
3952 Initialized based on EMACSLOADPATH environment variable, if any,
3953 otherwise to default specified by file `epaths.h' when Emacs was built. */);
3954
3955 DEFVAR_LISP ("load-suffixes", &Vload_suffixes,
3956 doc: /* *List of suffixes to try for files to load.
3957 This list should not include the empty string. */);
3958 Vload_suffixes = Fcons (build_string (".elc"),
3959 Fcons (build_string (".el"), Qnil));
3960 /* We don't use empty_string because it's not initialized yet. */
3961 default_suffixes = Fcons (build_string (""), Qnil);
3962 staticpro (&default_suffixes);
3963
3964 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3965 doc: /* Non-nil iff inside of `load'. */);
3966
3967 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3968 doc: /* An alist of expressions to be evalled when particular files are loaded.
3969 Each element looks like (FILENAME FORMS...).
3970 When `load' is run and the file-name argument is FILENAME,
3971 the FORMS in the corresponding element are executed at the end of loading.
3972
3973 FILENAME must match exactly! Normally FILENAME is the name of a library,
3974 with no directory specified, since that is how `load' is normally called.
3975 An error in FORMS does not undo the load,
3976 but does prevent execution of the rest of the FORMS.
3977 FILENAME can also be a symbol (a feature) and FORMS are then executed
3978 when the corresponding call to `provide' is made. */);
3979 Vafter_load_alist = Qnil;
3980
3981 DEFVAR_LISP ("load-history", &Vload_history,
3982 doc: /* Alist mapping source file names to symbols and features.
3983 Each alist element is a list that starts with a file name,
3984 except for one element (optional) that starts with nil and describes
3985 definitions evaluated from buffers not visiting files.
3986 The remaining elements of each list are symbols defined as functions,
3987 and cons cells of the form `(provide . FEATURE)', `(require . FEATURE)',
3988 `(defvar . VARIABLE), `(autoload . SYMBOL)', and `(t . SYMBOL)'.
3989 An element `(t . SYMBOL)' precedes an entry that is just SYMBOL,
3990 and means that SYMBOL was an autoload before this file redefined it
3991 as a function. */);
3992 Vload_history = Qnil;
3993
3994 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3995 doc: /* Full name of file being loaded by `load'. */);
3996 Vload_file_name = Qnil;
3997
3998 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3999 doc: /* File name, including directory, of user's initialization file.
4000 If the file loaded had extension `.elc' and there was a corresponding `.el'
4001 file, this variable contains the name of the .el file, suitable for use
4002 by functions like `custom-save-all' which edit the init file. */);
4003 Vuser_init_file = Qnil;
4004
4005 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
4006 doc: /* Used for internal purposes by `load'. */);
4007 Vcurrent_load_list = Qnil;
4008
4009 DEFVAR_LISP ("load-read-function", &Vload_read_function,
4010 doc: /* Function used by `load' and `eval-region' for reading expressions.
4011 The default is nil, which means use the function `read'. */);
4012 Vload_read_function = Qnil;
4013
4014 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
4015 doc: /* Function called in `load' for loading an Emacs lisp source file.
4016 This function is for doing code conversion before reading the source file.
4017 If nil, loading is done without any code conversion.
4018 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where
4019 FULLNAME is the full name of FILE.
4020 See `load' for the meaning of the remaining arguments. */);
4021 Vload_source_file_function = Qnil;
4022
4023 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
4024 doc: /* Non-nil means `load' should force-load all dynamic doc strings.
4025 This is useful when the file being loaded is a temporary copy. */);
4026 load_force_doc_strings = 0;
4027
4028 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
4029 doc: /* Non-nil means `read' converts strings to unibyte whenever possible.
4030 This is normally bound by `load' and `eval-buffer' to control `read',
4031 and is not meant for users to change. */);
4032 load_convert_to_unibyte = 0;
4033
4034 DEFVAR_LISP ("source-directory", &Vsource_directory,
4035 doc: /* Directory in which Emacs sources were found when Emacs was built.
4036 You cannot count on them to still be there! */);
4037 Vsource_directory
4038 = Fexpand_file_name (build_string ("../"),
4039 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
4040
4041 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
4042 doc: /* List of files that were preloaded (when dumping Emacs). */);
4043 Vpreloaded_file_list = Qnil;
4044
4045 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
4046 doc: /* List of all DEFVAR_BOOL variables, used by the byte code optimizer. */);
4047 Vbyte_boolean_vars = Qnil;
4048
4049 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
4050 doc: /* Non-nil means load dangerous compiled Lisp files.
4051 Some versions of XEmacs use different byte codes than Emacs. These
4052 incompatible byte codes can make Emacs crash when it tries to execute
4053 them. */);
4054 load_dangerous_libraries = 0;
4055
4056 DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
4057 doc: /* Regular expression matching safe to load compiled Lisp files.
4058 When Emacs loads a compiled Lisp file, it reads the first 512 bytes
4059 from the file, and matches them against this regular expression.
4060 When the regular expression matches, the file is considered to be safe
4061 to load. See also `load-dangerous-libraries'. */);
4062 Vbytecomp_version_regexp
4063 = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
4064
4065 /* Vsource_directory was initialized in init_lread. */
4066
4067 load_descriptor_list = Qnil;
4068 staticpro (&load_descriptor_list);
4069
4070 Qcurrent_load_list = intern ("current-load-list");
4071 staticpro (&Qcurrent_load_list);
4072
4073 Qstandard_input = intern ("standard-input");
4074 staticpro (&Qstandard_input);
4075
4076 Qread_char = intern ("read-char");
4077 staticpro (&Qread_char);
4078
4079 Qget_file_char = intern ("get-file-char");
4080 staticpro (&Qget_file_char);
4081
4082 Qget_emacs_mule_file_char = intern ("get-emacs-mule-file-char");
4083 staticpro (&Qget_emacs_mule_file_char);
4084
4085 Qload_force_doc_strings = intern ("load-force-doc-strings");
4086 staticpro (&Qload_force_doc_strings);
4087
4088 Qbackquote = intern ("`");
4089 staticpro (&Qbackquote);
4090 Qcomma = intern (",");
4091 staticpro (&Qcomma);
4092 Qcomma_at = intern (",@");
4093 staticpro (&Qcomma_at);
4094 Qcomma_dot = intern (",.");
4095 staticpro (&Qcomma_dot);
4096
4097 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
4098 staticpro (&Qinhibit_file_name_operation);
4099
4100 Qascii_character = intern ("ascii-character");
4101 staticpro (&Qascii_character);
4102
4103 Qfunction = intern ("function");
4104 staticpro (&Qfunction);
4105
4106 Qload = intern ("load");
4107 staticpro (&Qload);
4108
4109 Qload_file_name = intern ("load-file-name");
4110 staticpro (&Qload_file_name);
4111
4112 staticpro (&dump_path);
4113
4114 staticpro (&read_objects);
4115 read_objects = Qnil;
4116 staticpro (&seen_list);
4117
4118 Vloads_in_progress = Qnil;
4119 staticpro (&Vloads_in_progress);
4120 }
4121
4122 /* arch-tag: a0d02733-0f96-4844-a659-9fd53c4f414d
4123 (do not change this comment) */