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