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