(Vload_suffixes, default_suffixes): New vars.
[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 XSETFASTINT (XCAR (lispstream), (EMACS_UINT)stream >> 16);
836 XSETFASTINT (XCDR (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 for (tail = suffixes; CONSP (tail); tail = XCDR (tail))
967 {
968 CHECK_STRING (XCAR (tail), 0);
969 max_suffix_len = max (max_suffix_len,
970 STRING_BYTES (XSTRING (XCAR (tail))));
971 }
972
973 string = filename = Qnil;
974 GCPRO5 (str, string, filename, path, suffixes);
975
976 if (storeptr)
977 *storeptr = Qnil;
978
979 if (complete_filename_p (str))
980 absolute = 1;
981
982 for (; CONSP (path); path = XCDR (path))
983 {
984 filename = Fexpand_file_name (str, XCAR (path));
985 if (!complete_filename_p (filename))
986 /* If there are non-absolute elts in PATH (eg ".") */
987 /* Of course, this could conceivably lose if luser sets
988 default-directory to be something non-absolute... */
989 {
990 filename = Fexpand_file_name (filename, current_buffer->directory);
991 if (!complete_filename_p (filename))
992 /* Give up on this path element! */
993 continue;
994 }
995
996 /* Calculate maximum size of any filename made from
997 this path element/specified file name and any possible suffix. */
998 want_size = max_suffix_len + STRING_BYTES (XSTRING (filename)) + 1;
999 if (fn_size < want_size)
1000 fn = (char *) alloca (fn_size = 100 + want_size);
1001
1002 /* Loop over suffixes. */
1003 for (tail = NILP (suffixes) ? default_suffixes : suffixes;
1004 CONSP (tail); tail = XCDR (tail))
1005 {
1006 int lsuffix = STRING_BYTES (XSTRING (XCAR (tail)));
1007 Lisp_Object handler;
1008
1009 /* Concatenate path element/specified name with the suffix.
1010 If the directory starts with /:, remove that. */
1011 if (XSTRING (filename)->size > 2
1012 && XSTRING (filename)->data[0] == '/'
1013 && XSTRING (filename)->data[1] == ':')
1014 {
1015 strncpy (fn, XSTRING (filename)->data + 2,
1016 STRING_BYTES (XSTRING (filename)) - 2);
1017 fn[STRING_BYTES (XSTRING (filename)) - 2] = 0;
1018 }
1019 else
1020 {
1021 strncpy (fn, XSTRING (filename)->data,
1022 STRING_BYTES (XSTRING (filename)));
1023 fn[STRING_BYTES (XSTRING (filename))] = 0;
1024 }
1025
1026 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
1027 strncat (fn, XSTRING (XCAR (tail))->data, lsuffix);
1028
1029 /* Check that the file exists and is not a directory. */
1030 /* We used to only check for handlers on non-absolute file names:
1031 if (absolute)
1032 handler = Qnil;
1033 else
1034 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1035 It's not clear why that was the case and it breaks things like
1036 (load "/bar.el") where the file is actually "/bar.el.gz". */
1037 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1038 if (!NILP (handler) && !exec_only)
1039 {
1040 int exists;
1041
1042 string = build_string (fn);
1043 exists = !NILP (Ffile_readable_p (string));
1044 if (exists && !NILP (Ffile_directory_p (build_string (fn))))
1045 exists = 0;
1046
1047 if (exists)
1048 {
1049 /* We succeeded; return this descriptor and filename. */
1050 if (storeptr)
1051 *storeptr = build_string (fn);
1052 UNGCPRO;
1053 return -2;
1054 }
1055 }
1056 else
1057 {
1058 int exists = (stat (fn, &st) >= 0
1059 && (st.st_mode & S_IFMT) != S_IFDIR);
1060 if (exists)
1061 {
1062 /* Check that we can access or open it. */
1063 if (exec_only)
1064 fd = (access (fn, X_OK) == 0) ? 1 : -1;
1065 else
1066 fd = emacs_open (fn, O_RDONLY, 0);
1067
1068 if (fd >= 0)
1069 {
1070 /* We succeeded; return this descriptor and filename. */
1071 if (storeptr)
1072 *storeptr = build_string (fn);
1073 UNGCPRO;
1074 return fd;
1075 }
1076 }
1077 }
1078 }
1079 if (absolute)
1080 break;
1081 }
1082
1083 UNGCPRO;
1084 return -1;
1085 }
1086
1087 \f
1088 /* Merge the list we've accumulated of globals from the current input source
1089 into the load_history variable. The details depend on whether
1090 the source has an associated file name or not. */
1091
1092 static void
1093 build_load_history (stream, source)
1094 FILE *stream;
1095 Lisp_Object source;
1096 {
1097 register Lisp_Object tail, prev, newelt;
1098 register Lisp_Object tem, tem2;
1099 register int foundit, loading;
1100
1101 loading = stream || !NARROWED;
1102
1103 tail = Vload_history;
1104 prev = Qnil;
1105 foundit = 0;
1106 while (!NILP (tail))
1107 {
1108 tem = Fcar (tail);
1109
1110 /* Find the feature's previous assoc list... */
1111 if (!NILP (Fequal (source, Fcar (tem))))
1112 {
1113 foundit = 1;
1114
1115 /* If we're loading, remove it. */
1116 if (loading)
1117 {
1118 if (NILP (prev))
1119 Vload_history = Fcdr (tail);
1120 else
1121 Fsetcdr (prev, Fcdr (tail));
1122 }
1123
1124 /* Otherwise, cons on new symbols that are not already members. */
1125 else
1126 {
1127 tem2 = Vcurrent_load_list;
1128
1129 while (CONSP (tem2))
1130 {
1131 newelt = Fcar (tem2);
1132
1133 if (NILP (Fmemq (newelt, tem)))
1134 Fsetcar (tail, Fcons (Fcar (tem),
1135 Fcons (newelt, Fcdr (tem))));
1136
1137 tem2 = Fcdr (tem2);
1138 QUIT;
1139 }
1140 }
1141 }
1142 else
1143 prev = tail;
1144 tail = Fcdr (tail);
1145 QUIT;
1146 }
1147
1148 /* If we're loading, cons the new assoc onto the front of load-history,
1149 the most-recently-loaded position. Also do this if we didn't find
1150 an existing member for the current source. */
1151 if (loading || !foundit)
1152 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1153 Vload_history);
1154 }
1155
1156 Lisp_Object
1157 unreadpure (junk) /* Used as unwind-protect function in readevalloop */
1158 Lisp_Object junk;
1159 {
1160 read_pure = 0;
1161 return Qnil;
1162 }
1163
1164 static Lisp_Object
1165 readevalloop_1 (old)
1166 Lisp_Object old;
1167 {
1168 load_convert_to_unibyte = ! NILP (old);
1169 return Qnil;
1170 }
1171
1172 /* Signal an `end-of-file' error, if possible with file name
1173 information. */
1174
1175 static void
1176 end_of_file_error ()
1177 {
1178 Lisp_Object data;
1179
1180 if (STRINGP (Vload_file_name))
1181 data = Fcons (Vload_file_name, Qnil);
1182 else
1183 data = Qnil;
1184
1185 Fsignal (Qend_of_file, data);
1186 }
1187
1188 /* UNIBYTE specifies how to set load_convert_to_unibyte
1189 for this invocation.
1190 READFUN, if non-nil, is used instead of `read'. */
1191
1192 static void
1193 readevalloop (readcharfun, stream, sourcename, evalfun, printflag, unibyte, readfun)
1194 Lisp_Object readcharfun;
1195 FILE *stream;
1196 Lisp_Object sourcename;
1197 Lisp_Object (*evalfun) ();
1198 int printflag;
1199 Lisp_Object unibyte, readfun;
1200 {
1201 register int c;
1202 register Lisp_Object val;
1203 int count = specpdl_ptr - specpdl;
1204 struct gcpro gcpro1;
1205 struct buffer *b = 0;
1206 int continue_reading_p;
1207
1208 if (BUFFERP (readcharfun))
1209 b = XBUFFER (readcharfun);
1210 else if (MARKERP (readcharfun))
1211 b = XMARKER (readcharfun)->buffer;
1212
1213 specbind (Qstandard_input, readcharfun);
1214 specbind (Qcurrent_load_list, Qnil);
1215 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1216 load_convert_to_unibyte = !NILP (unibyte);
1217
1218 readchar_backlog = -1;
1219
1220 GCPRO1 (sourcename);
1221
1222 LOADHIST_ATTACH (sourcename);
1223
1224 continue_reading_p = 1;
1225 while (continue_reading_p)
1226 {
1227 if (b != 0 && NILP (b->name))
1228 error ("Reading from killed buffer");
1229
1230 instream = stream;
1231 c = READCHAR;
1232 if (c == ';')
1233 {
1234 while ((c = READCHAR) != '\n' && c != -1);
1235 continue;
1236 }
1237 if (c < 0) break;
1238
1239 /* Ignore whitespace here, so we can detect eof. */
1240 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
1241 continue;
1242
1243 if (!NILP (Vpurify_flag) && c == '(')
1244 {
1245 int count1 = specpdl_ptr - specpdl;
1246 record_unwind_protect (unreadpure, Qnil);
1247 val = read_list (-1, readcharfun);
1248 unbind_to (count1, Qnil);
1249 }
1250 else
1251 {
1252 UNREAD (c);
1253 read_objects = Qnil;
1254 if (!NILP (readfun))
1255 {
1256 val = call1 (readfun, readcharfun);
1257
1258 /* If READCHARFUN has set point to ZV, we should
1259 stop reading, even if the form read sets point
1260 to a different value when evaluated. */
1261 if (BUFFERP (readcharfun))
1262 {
1263 struct buffer *b = XBUFFER (readcharfun);
1264 if (BUF_PT (b) == BUF_ZV (b))
1265 continue_reading_p = 0;
1266 }
1267 }
1268 else if (! NILP (Vload_read_function))
1269 val = call1 (Vload_read_function, readcharfun);
1270 else
1271 val = read0 (readcharfun);
1272 }
1273
1274 val = (*evalfun) (val);
1275
1276 if (printflag)
1277 {
1278 Vvalues = Fcons (val, Vvalues);
1279 if (EQ (Vstandard_output, Qt))
1280 Fprin1 (val, Qnil);
1281 else
1282 Fprint (val, Qnil);
1283 }
1284 }
1285
1286 build_load_history (stream, sourcename);
1287 UNGCPRO;
1288
1289 unbind_to (count, Qnil);
1290 }
1291
1292 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1293 "Execute the current buffer as Lisp code.\n\
1294 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
1295 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
1296 PRINTFLAG controls printing of output:\n\
1297 nil means discard it; anything else is stream for print.\n\
1298 \n\
1299 If the optional third argument FILENAME is non-nil,\n\
1300 it specifies the file name to use for `load-history'.\n\
1301 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'\n\
1302 for this invocation.\n\
1303 \n\
1304 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that\n\
1305 `print' and related functions should work normally even if PRINTFLAG is nil.\n\
1306 \n\
1307 This function preserves the position of point.")
1308 (buffer, printflag, filename, unibyte, do_allow_print)
1309 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1310 {
1311 int count = specpdl_ptr - specpdl;
1312 Lisp_Object tem, buf;
1313
1314 if (NILP (buffer))
1315 buf = Fcurrent_buffer ();
1316 else
1317 buf = Fget_buffer (buffer);
1318 if (NILP (buf))
1319 error ("No such buffer");
1320
1321 if (NILP (printflag) && NILP (do_allow_print))
1322 tem = Qsymbolp;
1323 else
1324 tem = printflag;
1325
1326 if (NILP (filename))
1327 filename = XBUFFER (buf)->filename;
1328
1329 specbind (Qstandard_output, tem);
1330 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1331 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1332 readevalloop (buf, 0, filename, Feval, !NILP (printflag), unibyte, Qnil);
1333 unbind_to (count, Qnil);
1334
1335 return Qnil;
1336 }
1337
1338 #if 0
1339 XDEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
1340 "Execute the current buffer as Lisp code.\n\
1341 Programs can pass argument PRINTFLAG which controls printing of output:\n\
1342 nil means discard it; anything else is stream for print.\n\
1343 \n\
1344 If there is no error, point does not move. If there is an error,\n\
1345 point remains at the end of the last character read from the buffer.")
1346 (printflag)
1347 Lisp_Object printflag;
1348 {
1349 int count = specpdl_ptr - specpdl;
1350 Lisp_Object tem, cbuf;
1351
1352 cbuf = Fcurrent_buffer ()
1353
1354 if (NILP (printflag))
1355 tem = Qsymbolp;
1356 else
1357 tem = printflag;
1358 specbind (Qstandard_output, tem);
1359 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1360 SET_PT (BEGV);
1361 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1362 !NILP (printflag), Qnil, Qnil);
1363 return unbind_to (count, Qnil);
1364 }
1365 #endif
1366
1367 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1368 "Execute the region as Lisp code.\n\
1369 When called from programs, expects two arguments,\n\
1370 giving starting and ending indices in the current buffer\n\
1371 of the text to be executed.\n\
1372 Programs can pass third argument PRINTFLAG which controls output:\n\
1373 nil means discard it; anything else is stream for printing it.\n\
1374 Also the fourth argument READ-FUNCTION, if non-nil, is used\n\
1375 instead of `read' to read each expression. It gets one argument\n\
1376 which is the input stream for reading characters.\n\
1377 \n\
1378 This function does not move point.")
1379 (start, end, printflag, read_function)
1380 Lisp_Object start, end, printflag, read_function;
1381 {
1382 int count = specpdl_ptr - specpdl;
1383 Lisp_Object tem, cbuf;
1384
1385 cbuf = Fcurrent_buffer ();
1386
1387 if (NILP (printflag))
1388 tem = Qsymbolp;
1389 else
1390 tem = printflag;
1391 specbind (Qstandard_output, tem);
1392
1393 if (NILP (printflag))
1394 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1395 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1396
1397 /* This both uses start and checks its type. */
1398 Fgoto_char (start);
1399 Fnarrow_to_region (make_number (BEGV), end);
1400 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1401 !NILP (printflag), Qnil, read_function);
1402
1403 return unbind_to (count, Qnil);
1404 }
1405
1406 \f
1407 DEFUN ("read", Fread, Sread, 0, 1, 0,
1408 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1409 If STREAM is nil, use the value of `standard-input' (which see).\n\
1410 STREAM or the value of `standard-input' may be:\n\
1411 a buffer (read from point and advance it)\n\
1412 a marker (read from where it points and advance it)\n\
1413 a function (call it with no arguments for each character,\n\
1414 call it with a char as argument to push a char back)\n\
1415 a string (takes text from string, starting at the beginning)\n\
1416 t (read text line using minibuffer and use it, or read from\n\
1417 standard input in batch mode).")
1418 (stream)
1419 Lisp_Object stream;
1420 {
1421 extern Lisp_Object Fread_minibuffer ();
1422
1423 if (NILP (stream))
1424 stream = Vstandard_input;
1425 if (EQ (stream, Qt))
1426 stream = Qread_char;
1427
1428 readchar_backlog = -1;
1429 new_backquote_flag = 0;
1430 read_objects = Qnil;
1431
1432 if (EQ (stream, Qread_char))
1433 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1434
1435 if (STRINGP (stream))
1436 return Fcar (Fread_from_string (stream, Qnil, Qnil));
1437
1438 return read0 (stream);
1439 }
1440
1441 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1442 "Read one Lisp expression which is represented as text by STRING.\n\
1443 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1444 START and END optionally delimit a substring of STRING from which to read;\n\
1445 they default to 0 and (length STRING) respectively.")
1446 (string, start, end)
1447 Lisp_Object string, start, end;
1448 {
1449 int startval, endval;
1450 Lisp_Object tem;
1451
1452 CHECK_STRING (string,0);
1453
1454 if (NILP (end))
1455 endval = XSTRING (string)->size;
1456 else
1457 {
1458 CHECK_NUMBER (end, 2);
1459 endval = XINT (end);
1460 if (endval < 0 || endval > XSTRING (string)->size)
1461 args_out_of_range (string, end);
1462 }
1463
1464 if (NILP (start))
1465 startval = 0;
1466 else
1467 {
1468 CHECK_NUMBER (start, 1);
1469 startval = XINT (start);
1470 if (startval < 0 || startval > endval)
1471 args_out_of_range (string, start);
1472 }
1473
1474 read_from_string_index = startval;
1475 read_from_string_index_byte = string_char_to_byte (string, startval);
1476 read_from_string_limit = endval;
1477
1478 new_backquote_flag = 0;
1479 read_objects = Qnil;
1480
1481 tem = read0 (string);
1482 return Fcons (tem, make_number (read_from_string_index));
1483 }
1484 \f
1485 /* Use this for recursive reads, in contexts where internal tokens
1486 are not allowed. */
1487
1488 static Lisp_Object
1489 read0 (readcharfun)
1490 Lisp_Object readcharfun;
1491 {
1492 register Lisp_Object val;
1493 int c;
1494
1495 val = read1 (readcharfun, &c, 0);
1496 if (c)
1497 Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1),
1498 make_number (c)),
1499 Qnil));
1500
1501 return val;
1502 }
1503 \f
1504 static int read_buffer_size;
1505 static char *read_buffer;
1506
1507 /* Read multibyte form and return it as a character. C is a first
1508 byte of multibyte form, and rest of them are read from
1509 READCHARFUN. */
1510
1511 static int
1512 read_multibyte (c, readcharfun)
1513 register int c;
1514 Lisp_Object readcharfun;
1515 {
1516 /* We need the actual character code of this multibyte
1517 characters. */
1518 unsigned char str[MAX_MULTIBYTE_LENGTH];
1519 int len = 0;
1520 int bytes;
1521
1522 str[len++] = c;
1523 while ((c = READCHAR) >= 0xA0
1524 && len < MAX_MULTIBYTE_LENGTH)
1525 str[len++] = c;
1526 UNREAD (c);
1527 if (UNIBYTE_STR_AS_MULTIBYTE_P (str, len, bytes))
1528 return STRING_CHAR (str, len);
1529 /* The byte sequence is not valid as multibyte. Unread all bytes
1530 but the first one, and return the first byte. */
1531 while (--len > 0)
1532 UNREAD (str[len]);
1533 return str[0];
1534 }
1535
1536 /* Read a \-escape sequence, assuming we already read the `\'. */
1537
1538 static int
1539 read_escape (readcharfun, stringp)
1540 Lisp_Object readcharfun;
1541 int stringp;
1542 {
1543 register int c = READCHAR;
1544 switch (c)
1545 {
1546 case -1:
1547 error ("End of file");
1548
1549 case 'a':
1550 return '\007';
1551 case 'b':
1552 return '\b';
1553 case 'd':
1554 return 0177;
1555 case 'e':
1556 return 033;
1557 case 'f':
1558 return '\f';
1559 case 'n':
1560 return '\n';
1561 case 'r':
1562 return '\r';
1563 case 't':
1564 return '\t';
1565 case 'v':
1566 return '\v';
1567 case '\n':
1568 return -1;
1569 case ' ':
1570 if (stringp)
1571 return -1;
1572 return ' ';
1573
1574 case 'M':
1575 c = READCHAR;
1576 if (c != '-')
1577 error ("Invalid escape character syntax");
1578 c = READCHAR;
1579 if (c == '\\')
1580 c = read_escape (readcharfun, 0);
1581 return c | meta_modifier;
1582
1583 case 'S':
1584 c = READCHAR;
1585 if (c != '-')
1586 error ("Invalid escape character syntax");
1587 c = READCHAR;
1588 if (c == '\\')
1589 c = read_escape (readcharfun, 0);
1590 return c | shift_modifier;
1591
1592 case 'H':
1593 c = READCHAR;
1594 if (c != '-')
1595 error ("Invalid escape character syntax");
1596 c = READCHAR;
1597 if (c == '\\')
1598 c = read_escape (readcharfun, 0);
1599 return c | hyper_modifier;
1600
1601 case 'A':
1602 c = READCHAR;
1603 if (c != '-')
1604 error ("Invalid escape character syntax");
1605 c = READCHAR;
1606 if (c == '\\')
1607 c = read_escape (readcharfun, 0);
1608 return c | alt_modifier;
1609
1610 case 's':
1611 c = READCHAR;
1612 if (c != '-')
1613 error ("Invalid escape character syntax");
1614 c = READCHAR;
1615 if (c == '\\')
1616 c = read_escape (readcharfun, 0);
1617 return c | super_modifier;
1618
1619 case 'C':
1620 c = READCHAR;
1621 if (c != '-')
1622 error ("Invalid escape character syntax");
1623 case '^':
1624 c = READCHAR;
1625 if (c == '\\')
1626 c = read_escape (readcharfun, 0);
1627 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1628 return 0177 | (c & CHAR_MODIFIER_MASK);
1629 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1630 return c | ctrl_modifier;
1631 /* ASCII control chars are made from letters (both cases),
1632 as well as the non-letters within 0100...0137. */
1633 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1634 return (c & (037 | ~0177));
1635 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1636 return (c & (037 | ~0177));
1637 else
1638 return c | ctrl_modifier;
1639
1640 case '0':
1641 case '1':
1642 case '2':
1643 case '3':
1644 case '4':
1645 case '5':
1646 case '6':
1647 case '7':
1648 /* An octal escape, as in ANSI C. */
1649 {
1650 register int i = c - '0';
1651 register int count = 0;
1652 while (++count < 3)
1653 {
1654 if ((c = READCHAR) >= '0' && c <= '7')
1655 {
1656 i *= 8;
1657 i += c - '0';
1658 }
1659 else
1660 {
1661 UNREAD (c);
1662 break;
1663 }
1664 }
1665 return i;
1666 }
1667
1668 case 'x':
1669 /* A hex escape, as in ANSI C. */
1670 {
1671 int i = 0;
1672 while (1)
1673 {
1674 c = READCHAR;
1675 if (c >= '0' && c <= '9')
1676 {
1677 i *= 16;
1678 i += c - '0';
1679 }
1680 else if ((c >= 'a' && c <= 'f')
1681 || (c >= 'A' && c <= 'F'))
1682 {
1683 i *= 16;
1684 if (c >= 'a' && c <= 'f')
1685 i += c - 'a' + 10;
1686 else
1687 i += c - 'A' + 10;
1688 }
1689 else
1690 {
1691 UNREAD (c);
1692 break;
1693 }
1694 }
1695 return i;
1696 }
1697
1698 default:
1699 if (BASE_LEADING_CODE_P (c))
1700 c = read_multibyte (c, readcharfun);
1701 return c;
1702 }
1703 }
1704
1705
1706 /* Read an integer in radix RADIX using READCHARFUN to read
1707 characters. RADIX must be in the interval [2..36]; if it isn't, a
1708 read error is signaled . Value is the integer read. Signals an
1709 error if encountering invalid read syntax or if RADIX is out of
1710 range. */
1711
1712 static Lisp_Object
1713 read_integer (readcharfun, radix)
1714 Lisp_Object readcharfun;
1715 int radix;
1716 {
1717 int ndigits = 0, invalid_p, c, sign = 0;
1718 EMACS_INT number = 0;
1719
1720 if (radix < 2 || radix > 36)
1721 invalid_p = 1;
1722 else
1723 {
1724 number = ndigits = invalid_p = 0;
1725 sign = 1;
1726
1727 c = READCHAR;
1728 if (c == '-')
1729 {
1730 c = READCHAR;
1731 sign = -1;
1732 }
1733 else if (c == '+')
1734 c = READCHAR;
1735
1736 while (c >= 0)
1737 {
1738 int digit;
1739
1740 if (c >= '0' && c <= '9')
1741 digit = c - '0';
1742 else if (c >= 'a' && c <= 'z')
1743 digit = c - 'a' + 10;
1744 else if (c >= 'A' && c <= 'Z')
1745 digit = c - 'A' + 10;
1746 else
1747 {
1748 UNREAD (c);
1749 break;
1750 }
1751
1752 if (digit < 0 || digit >= radix)
1753 invalid_p = 1;
1754
1755 number = radix * number + digit;
1756 ++ndigits;
1757 c = READCHAR;
1758 }
1759 }
1760
1761 if (ndigits == 0 || invalid_p)
1762 {
1763 char buf[50];
1764 sprintf (buf, "integer, radix %d", radix);
1765 Fsignal (Qinvalid_read_syntax, Fcons (build_string (buf), Qnil));
1766 }
1767
1768 return make_number (sign * number);
1769 }
1770
1771
1772 /* If the next token is ')' or ']' or '.', we store that character
1773 in *PCH and the return value is not interesting. Else, we store
1774 zero in *PCH and we read and return one lisp object.
1775
1776 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1777
1778 static Lisp_Object
1779 read1 (readcharfun, pch, first_in_list)
1780 register Lisp_Object readcharfun;
1781 int *pch;
1782 int first_in_list;
1783 {
1784 register int c;
1785 int uninterned_symbol = 0;
1786
1787 *pch = 0;
1788
1789 retry:
1790
1791 c = READCHAR;
1792 if (c < 0)
1793 end_of_file_error ();
1794
1795 switch (c)
1796 {
1797 case '(':
1798 return read_list (0, readcharfun);
1799
1800 case '[':
1801 return read_vector (readcharfun, 0);
1802
1803 case ')':
1804 case ']':
1805 {
1806 *pch = c;
1807 return Qnil;
1808 }
1809
1810 case '#':
1811 c = READCHAR;
1812 if (c == '^')
1813 {
1814 c = READCHAR;
1815 if (c == '[')
1816 {
1817 Lisp_Object tmp;
1818 tmp = read_vector (readcharfun, 0);
1819 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1820 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1821 error ("Invalid size char-table");
1822 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1823 XCHAR_TABLE (tmp)->top = Qt;
1824 return tmp;
1825 }
1826 else if (c == '^')
1827 {
1828 c = READCHAR;
1829 if (c == '[')
1830 {
1831 Lisp_Object tmp;
1832 tmp = read_vector (readcharfun, 0);
1833 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
1834 error ("Invalid size char-table");
1835 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1836 XCHAR_TABLE (tmp)->top = Qnil;
1837 return tmp;
1838 }
1839 Fsignal (Qinvalid_read_syntax,
1840 Fcons (make_string ("#^^", 3), Qnil));
1841 }
1842 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1843 }
1844 if (c == '&')
1845 {
1846 Lisp_Object length;
1847 length = read1 (readcharfun, pch, first_in_list);
1848 c = READCHAR;
1849 if (c == '"')
1850 {
1851 Lisp_Object tmp, val;
1852 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1853 / BITS_PER_CHAR);
1854
1855 UNREAD (c);
1856 tmp = read1 (readcharfun, pch, first_in_list);
1857 if (size_in_chars != XSTRING (tmp)->size
1858 /* We used to print 1 char too many
1859 when the number of bits was a multiple of 8.
1860 Accept such input in case it came from an old version. */
1861 && ! (XFASTINT (length)
1862 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1863 Fsignal (Qinvalid_read_syntax,
1864 Fcons (make_string ("#&...", 5), Qnil));
1865
1866 val = Fmake_bool_vector (length, Qnil);
1867 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1868 size_in_chars);
1869 /* Clear the extraneous bits in the last byte. */
1870 if (XINT (length) != size_in_chars * BITS_PER_CHAR)
1871 XBOOL_VECTOR (val)->data[size_in_chars - 1]
1872 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1873 return val;
1874 }
1875 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1876 Qnil));
1877 }
1878 if (c == '[')
1879 {
1880 /* Accept compiled functions at read-time so that we don't have to
1881 build them using function calls. */
1882 Lisp_Object tmp;
1883 tmp = read_vector (readcharfun, 1);
1884 return Fmake_byte_code (XVECTOR (tmp)->size,
1885 XVECTOR (tmp)->contents);
1886 }
1887 if (c == '(')
1888 {
1889 Lisp_Object tmp;
1890 struct gcpro gcpro1;
1891 int ch;
1892
1893 /* Read the string itself. */
1894 tmp = read1 (readcharfun, &ch, 0);
1895 if (ch != 0 || !STRINGP (tmp))
1896 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1897 GCPRO1 (tmp);
1898 /* Read the intervals and their properties. */
1899 while (1)
1900 {
1901 Lisp_Object beg, end, plist;
1902
1903 beg = read1 (readcharfun, &ch, 0);
1904 end = plist = Qnil;
1905 if (ch == ')')
1906 break;
1907 if (ch == 0)
1908 end = read1 (readcharfun, &ch, 0);
1909 if (ch == 0)
1910 plist = read1 (readcharfun, &ch, 0);
1911 if (ch)
1912 Fsignal (Qinvalid_read_syntax,
1913 Fcons (build_string ("invalid string property list"),
1914 Qnil));
1915 Fset_text_properties (beg, end, plist, tmp);
1916 }
1917 UNGCPRO;
1918 return tmp;
1919 }
1920
1921 /* #@NUMBER is used to skip NUMBER following characters.
1922 That's used in .elc files to skip over doc strings
1923 and function definitions. */
1924 if (c == '@')
1925 {
1926 int i, nskip = 0;
1927
1928 /* Read a decimal integer. */
1929 while ((c = READCHAR) >= 0
1930 && c >= '0' && c <= '9')
1931 {
1932 nskip *= 10;
1933 nskip += c - '0';
1934 }
1935 if (c >= 0)
1936 UNREAD (c);
1937
1938 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1939 {
1940 /* If we are supposed to force doc strings into core right now,
1941 record the last string that we skipped,
1942 and record where in the file it comes from. */
1943
1944 /* But first exchange saved_doc_string
1945 with prev_saved_doc_string, so we save two strings. */
1946 {
1947 char *temp = saved_doc_string;
1948 int temp_size = saved_doc_string_size;
1949 file_offset temp_pos = saved_doc_string_position;
1950 int temp_len = saved_doc_string_length;
1951
1952 saved_doc_string = prev_saved_doc_string;
1953 saved_doc_string_size = prev_saved_doc_string_size;
1954 saved_doc_string_position = prev_saved_doc_string_position;
1955 saved_doc_string_length = prev_saved_doc_string_length;
1956
1957 prev_saved_doc_string = temp;
1958 prev_saved_doc_string_size = temp_size;
1959 prev_saved_doc_string_position = temp_pos;
1960 prev_saved_doc_string_length = temp_len;
1961 }
1962
1963 if (saved_doc_string_size == 0)
1964 {
1965 saved_doc_string_size = nskip + 100;
1966 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1967 }
1968 if (nskip > saved_doc_string_size)
1969 {
1970 saved_doc_string_size = nskip + 100;
1971 saved_doc_string = (char *) xrealloc (saved_doc_string,
1972 saved_doc_string_size);
1973 }
1974
1975 saved_doc_string_position = file_tell (instream);
1976
1977 /* Copy that many characters into saved_doc_string. */
1978 for (i = 0; i < nskip && c >= 0; i++)
1979 saved_doc_string[i] = c = READCHAR;
1980
1981 saved_doc_string_length = i;
1982 }
1983 else
1984 {
1985 /* Skip that many characters. */
1986 for (i = 0; i < nskip && c >= 0; i++)
1987 c = READCHAR;
1988 }
1989
1990 goto retry;
1991 }
1992 if (c == '$')
1993 return Vload_file_name;
1994 if (c == '\'')
1995 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1996 /* #:foo is the uninterned symbol named foo. */
1997 if (c == ':')
1998 {
1999 uninterned_symbol = 1;
2000 c = READCHAR;
2001 goto default_label;
2002 }
2003 /* Reader forms that can reuse previously read objects. */
2004 if (c >= '0' && c <= '9')
2005 {
2006 int n = 0;
2007 Lisp_Object tem;
2008
2009 /* Read a non-negative integer. */
2010 while (c >= '0' && c <= '9')
2011 {
2012 n *= 10;
2013 n += c - '0';
2014 c = READCHAR;
2015 }
2016 /* #n=object returns object, but associates it with n for #n#. */
2017 if (c == '=')
2018 {
2019 /* Make a placeholder for #n# to use temporarily */
2020 Lisp_Object placeholder;
2021 Lisp_Object cell;
2022
2023 placeholder = Fcons(Qnil, Qnil);
2024 cell = Fcons (make_number (n), placeholder);
2025 read_objects = Fcons (cell, read_objects);
2026
2027 /* Read the object itself. */
2028 tem = read0 (readcharfun);
2029
2030 /* Now put it everywhere the placeholder was... */
2031 substitute_object_in_subtree (tem, placeholder);
2032
2033 /* ...and #n# will use the real value from now on. */
2034 Fsetcdr (cell, tem);
2035
2036 return tem;
2037 }
2038 /* #n# returns a previously read object. */
2039 if (c == '#')
2040 {
2041 tem = Fassq (make_number (n), read_objects);
2042 if (CONSP (tem))
2043 return XCDR (tem);
2044 /* Fall through to error message. */
2045 }
2046 else if (c == 'r' || c == 'R')
2047 return read_integer (readcharfun, n);
2048
2049 /* Fall through to error message. */
2050 }
2051 else if (c == 'x' || c == 'X')
2052 return read_integer (readcharfun, 16);
2053 else if (c == 'o' || c == 'O')
2054 return read_integer (readcharfun, 8);
2055 else if (c == 'b' || c == 'B')
2056 return read_integer (readcharfun, 2);
2057
2058 UNREAD (c);
2059 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
2060
2061 case ';':
2062 while ((c = READCHAR) >= 0 && c != '\n');
2063 goto retry;
2064
2065 case '\'':
2066 {
2067 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2068 }
2069
2070 case '`':
2071 if (first_in_list)
2072 goto default_label;
2073 else
2074 {
2075 Lisp_Object value;
2076
2077 new_backquote_flag = 1;
2078 value = read0 (readcharfun);
2079 new_backquote_flag = 0;
2080
2081 return Fcons (Qbackquote, Fcons (value, Qnil));
2082 }
2083
2084 case ',':
2085 if (new_backquote_flag)
2086 {
2087 Lisp_Object comma_type = Qnil;
2088 Lisp_Object value;
2089 int ch = READCHAR;
2090
2091 if (ch == '@')
2092 comma_type = Qcomma_at;
2093 else if (ch == '.')
2094 comma_type = Qcomma_dot;
2095 else
2096 {
2097 if (ch >= 0) UNREAD (ch);
2098 comma_type = Qcomma;
2099 }
2100
2101 new_backquote_flag = 0;
2102 value = read0 (readcharfun);
2103 new_backquote_flag = 1;
2104 return Fcons (comma_type, Fcons (value, Qnil));
2105 }
2106 else
2107 goto default_label;
2108
2109 case '?':
2110 {
2111 c = READCHAR;
2112 if (c < 0)
2113 end_of_file_error ();
2114
2115 if (c == '\\')
2116 c = read_escape (readcharfun, 0);
2117 else if (BASE_LEADING_CODE_P (c))
2118 c = read_multibyte (c, readcharfun);
2119
2120 return make_number (c);
2121 }
2122
2123 case '"':
2124 {
2125 register char *p = read_buffer;
2126 register char *end = read_buffer + read_buffer_size;
2127 register int c;
2128 /* Nonzero if we saw an escape sequence specifying
2129 a multibyte character. */
2130 int force_multibyte = 0;
2131 /* Nonzero if we saw an escape sequence specifying
2132 a single-byte character. */
2133 int force_singlebyte = 0;
2134 int cancel = 0;
2135 int nchars;
2136
2137 while ((c = READCHAR) >= 0
2138 && c != '\"')
2139 {
2140 if (end - p < MAX_MULTIBYTE_LENGTH)
2141 {
2142 int offset = p - read_buffer;
2143 read_buffer = (char *) xrealloc (read_buffer,
2144 read_buffer_size *= 2);
2145 p = read_buffer + offset;
2146 end = read_buffer + read_buffer_size;
2147 }
2148
2149 if (c == '\\')
2150 {
2151 c = read_escape (readcharfun, 1);
2152
2153 /* C is -1 if \ newline has just been seen */
2154 if (c == -1)
2155 {
2156 if (p == read_buffer)
2157 cancel = 1;
2158 continue;
2159 }
2160
2161 /* If an escape specifies a non-ASCII single-byte character,
2162 this must be a unibyte string. */
2163 if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))
2164 && ! ASCII_BYTE_P ((c & ~CHAR_MODIFIER_MASK)))
2165 force_singlebyte = 1;
2166 }
2167
2168 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2169 {
2170 /* Any modifiers for a multibyte character are invalid. */
2171 if (c & CHAR_MODIFIER_MASK)
2172 error ("Invalid modifier in string");
2173 p += CHAR_STRING (c, p);
2174 force_multibyte = 1;
2175 }
2176 else
2177 {
2178 /* Allow `\C- ' and `\C-?'. */
2179 if (c == (CHAR_CTL | ' '))
2180 c = 0;
2181 else if (c == (CHAR_CTL | '?'))
2182 c = 127;
2183
2184 if (c & CHAR_SHIFT)
2185 {
2186 /* Shift modifier is valid only with [A-Za-z]. */
2187 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
2188 c &= ~CHAR_SHIFT;
2189 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
2190 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
2191 }
2192
2193 if (c & CHAR_META)
2194 /* Move the meta bit to the right place for a string. */
2195 c = (c & ~CHAR_META) | 0x80;
2196 if (c & ~0xff)
2197 error ("Invalid modifier in string");
2198 *p++ = c;
2199 }
2200 }
2201 if (c < 0)
2202 end_of_file_error ();
2203
2204 /* If purifying, and string starts with \ newline,
2205 return zero instead. This is for doc strings
2206 that we are really going to find in etc/DOC.nn.nn */
2207 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2208 return make_number (0);
2209
2210 if (force_multibyte)
2211 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2212 p - read_buffer, &nchars);
2213 else if (force_singlebyte)
2214 nchars = p - read_buffer;
2215 else if (load_convert_to_unibyte)
2216 {
2217 Lisp_Object string;
2218 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2219 p - read_buffer, &nchars);
2220 if (p - read_buffer != nchars)
2221 {
2222 string = make_multibyte_string (read_buffer, nchars,
2223 p - read_buffer);
2224 return Fstring_make_unibyte (string);
2225 }
2226 }
2227 else if (EQ (readcharfun, Qget_file_char)
2228 || EQ (readcharfun, Qlambda))
2229 /* Nowadays, reading directly from a file is used only for
2230 compiled Emacs Lisp files, and those always use the
2231 Emacs internal encoding. Meanwhile, Qlambda is used
2232 for reading dynamic byte code (compiled with
2233 byte-compile-dynamic = t). */
2234 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2235 p - read_buffer, &nchars);
2236 else
2237 /* In all other cases, if we read these bytes as
2238 separate characters, treat them as separate characters now. */
2239 nchars = p - read_buffer;
2240
2241 if (read_pure)
2242 return make_pure_string (read_buffer, nchars, p - read_buffer,
2243 (force_multibyte
2244 || (p - read_buffer != nchars)));
2245 return make_specified_string (read_buffer, nchars, p - read_buffer,
2246 (force_multibyte
2247 || (p - read_buffer != nchars)));
2248 }
2249
2250 case '.':
2251 {
2252 int next_char = READCHAR;
2253 UNREAD (next_char);
2254
2255 if (next_char <= 040
2256 || index ("\"'`,(", next_char))
2257 {
2258 *pch = c;
2259 return Qnil;
2260 }
2261
2262 /* Otherwise, we fall through! Note that the atom-reading loop
2263 below will now loop at least once, assuring that we will not
2264 try to UNREAD two characters in a row. */
2265 }
2266 default:
2267 default_label:
2268 if (c <= 040) goto retry;
2269 {
2270 char *p = read_buffer;
2271 int quoted = 0;
2272
2273 {
2274 char *end = read_buffer + read_buffer_size;
2275
2276 while (c > 040
2277 && !(c == '\"' || c == '\'' || c == ';'
2278 || c == '(' || c == ')'
2279 || c == '[' || c == ']' || c == '#'))
2280 {
2281 if (end - p < MAX_MULTIBYTE_LENGTH)
2282 {
2283 int offset = p - read_buffer;
2284 read_buffer = (char *) xrealloc (read_buffer,
2285 read_buffer_size *= 2);
2286 p = read_buffer + offset;
2287 end = read_buffer + read_buffer_size;
2288 }
2289
2290 if (c == '\\')
2291 {
2292 c = READCHAR;
2293 if (c == -1)
2294 end_of_file_error ();
2295 quoted = 1;
2296 }
2297
2298 if (! SINGLE_BYTE_CHAR_P (c))
2299 p += CHAR_STRING (c, p);
2300 else
2301 *p++ = c;
2302
2303 c = READCHAR;
2304 }
2305
2306 if (p == end)
2307 {
2308 int offset = p - read_buffer;
2309 read_buffer = (char *) xrealloc (read_buffer,
2310 read_buffer_size *= 2);
2311 p = read_buffer + offset;
2312 end = read_buffer + read_buffer_size;
2313 }
2314 *p = 0;
2315 if (c >= 0)
2316 UNREAD (c);
2317 }
2318
2319 if (!quoted && !uninterned_symbol)
2320 {
2321 register char *p1;
2322 register Lisp_Object val;
2323 p1 = read_buffer;
2324 if (*p1 == '+' || *p1 == '-') p1++;
2325 /* Is it an integer? */
2326 if (p1 != p)
2327 {
2328 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2329 /* Integers can have trailing decimal points. */
2330 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2331 if (p1 == p)
2332 /* It is an integer. */
2333 {
2334 if (p1[-1] == '.')
2335 p1[-1] = '\0';
2336 if (sizeof (int) == sizeof (EMACS_INT))
2337 XSETINT (val, atoi (read_buffer));
2338 else if (sizeof (long) == sizeof (EMACS_INT))
2339 XSETINT (val, atol (read_buffer));
2340 else
2341 abort ();
2342 return val;
2343 }
2344 }
2345 if (isfloat_string (read_buffer))
2346 {
2347 /* Compute NaN and infinities using 0.0 in a variable,
2348 to cope with compilers that think they are smarter
2349 than we are. */
2350 double zero = 0.0;
2351
2352 double value;
2353
2354 /* Negate the value ourselves. This treats 0, NaNs,
2355 and infinity properly on IEEE floating point hosts,
2356 and works around a common bug where atof ("-0.0")
2357 drops the sign. */
2358 int negative = read_buffer[0] == '-';
2359
2360 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2361 returns 1, is if the input ends in e+INF or e+NaN. */
2362 switch (p[-1])
2363 {
2364 case 'F':
2365 value = 1.0 / zero;
2366 break;
2367 case 'N':
2368 value = zero / zero;
2369 break;
2370 default:
2371 value = atof (read_buffer + negative);
2372 break;
2373 }
2374
2375 return make_float (negative ? - value : value);
2376 }
2377 }
2378
2379 if (uninterned_symbol)
2380 return make_symbol (read_buffer);
2381 else
2382 return intern (read_buffer);
2383 }
2384 }
2385 }
2386 \f
2387
2388 /* List of nodes we've seen during substitute_object_in_subtree. */
2389 static Lisp_Object seen_list;
2390
2391 static void
2392 substitute_object_in_subtree (object, placeholder)
2393 Lisp_Object object;
2394 Lisp_Object placeholder;
2395 {
2396 Lisp_Object check_object;
2397
2398 /* We haven't seen any objects when we start. */
2399 seen_list = Qnil;
2400
2401 /* Make all the substitutions. */
2402 check_object
2403 = substitute_object_recurse (object, placeholder, object);
2404
2405 /* Clear seen_list because we're done with it. */
2406 seen_list = Qnil;
2407
2408 /* The returned object here is expected to always eq the
2409 original. */
2410 if (!EQ (check_object, object))
2411 error ("Unexpected mutation error in reader");
2412 }
2413
2414 /* Feval doesn't get called from here, so no gc protection is needed. */
2415 #define SUBSTITUTE(get_val, set_val) \
2416 { \
2417 Lisp_Object old_value = get_val; \
2418 Lisp_Object true_value \
2419 = substitute_object_recurse (object, placeholder,\
2420 old_value); \
2421 \
2422 if (!EQ (old_value, true_value)) \
2423 { \
2424 set_val; \
2425 } \
2426 }
2427
2428 static Lisp_Object
2429 substitute_object_recurse (object, placeholder, subtree)
2430 Lisp_Object object;
2431 Lisp_Object placeholder;
2432 Lisp_Object subtree;
2433 {
2434 /* If we find the placeholder, return the target object. */
2435 if (EQ (placeholder, subtree))
2436 return object;
2437
2438 /* If we've been to this node before, don't explore it again. */
2439 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2440 return subtree;
2441
2442 /* If this node can be the entry point to a cycle, remember that
2443 we've seen it. It can only be such an entry point if it was made
2444 by #n=, which means that we can find it as a value in
2445 read_objects. */
2446 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2447 seen_list = Fcons (subtree, seen_list);
2448
2449 /* Recurse according to subtree's type.
2450 Every branch must return a Lisp_Object. */
2451 switch (XTYPE (subtree))
2452 {
2453 case Lisp_Vectorlike:
2454 {
2455 int i;
2456 int length = XINT (Flength(subtree));
2457 for (i = 0; i < length; i++)
2458 {
2459 Lisp_Object idx = make_number (i);
2460 SUBSTITUTE (Faref (subtree, idx),
2461 Faset (subtree, idx, true_value));
2462 }
2463 return subtree;
2464 }
2465
2466 case Lisp_Cons:
2467 {
2468 SUBSTITUTE (Fcar_safe (subtree),
2469 Fsetcar (subtree, true_value));
2470 SUBSTITUTE (Fcdr_safe (subtree),
2471 Fsetcdr (subtree, true_value));
2472 return subtree;
2473 }
2474
2475 case Lisp_String:
2476 {
2477 /* Check for text properties in each interval.
2478 substitute_in_interval contains part of the logic. */
2479
2480 INTERVAL root_interval = XSTRING (subtree)->intervals;
2481 Lisp_Object arg = Fcons (object, placeholder);
2482
2483 traverse_intervals (root_interval, 1, 0,
2484 &substitute_in_interval, arg);
2485
2486 return subtree;
2487 }
2488
2489 /* Other types don't recurse any further. */
2490 default:
2491 return subtree;
2492 }
2493 }
2494
2495 /* Helper function for substitute_object_recurse. */
2496 static void
2497 substitute_in_interval (interval, arg)
2498 INTERVAL interval;
2499 Lisp_Object arg;
2500 {
2501 Lisp_Object object = Fcar (arg);
2502 Lisp_Object placeholder = Fcdr (arg);
2503
2504 SUBSTITUTE(interval->plist, interval->plist = true_value);
2505 }
2506
2507 \f
2508 #define LEAD_INT 1
2509 #define DOT_CHAR 2
2510 #define TRAIL_INT 4
2511 #define E_CHAR 8
2512 #define EXP_INT 16
2513
2514 int
2515 isfloat_string (cp)
2516 register char *cp;
2517 {
2518 register int state;
2519
2520 char *start = cp;
2521
2522 state = 0;
2523 if (*cp == '+' || *cp == '-')
2524 cp++;
2525
2526 if (*cp >= '0' && *cp <= '9')
2527 {
2528 state |= LEAD_INT;
2529 while (*cp >= '0' && *cp <= '9')
2530 cp++;
2531 }
2532 if (*cp == '.')
2533 {
2534 state |= DOT_CHAR;
2535 cp++;
2536 }
2537 if (*cp >= '0' && *cp <= '9')
2538 {
2539 state |= TRAIL_INT;
2540 while (*cp >= '0' && *cp <= '9')
2541 cp++;
2542 }
2543 if (*cp == 'e' || *cp == 'E')
2544 {
2545 state |= E_CHAR;
2546 cp++;
2547 if (*cp == '+' || *cp == '-')
2548 cp++;
2549 }
2550
2551 if (*cp >= '0' && *cp <= '9')
2552 {
2553 state |= EXP_INT;
2554 while (*cp >= '0' && *cp <= '9')
2555 cp++;
2556 }
2557 else if (cp == start)
2558 ;
2559 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2560 {
2561 state |= EXP_INT;
2562 cp += 3;
2563 }
2564 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2565 {
2566 state |= EXP_INT;
2567 cp += 3;
2568 }
2569
2570 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2571 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2572 || state == (DOT_CHAR|TRAIL_INT)
2573 || state == (LEAD_INT|E_CHAR|EXP_INT)
2574 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2575 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2576 }
2577
2578 \f
2579 static Lisp_Object
2580 read_vector (readcharfun, bytecodeflag)
2581 Lisp_Object readcharfun;
2582 int bytecodeflag;
2583 {
2584 register int i;
2585 register int size;
2586 register Lisp_Object *ptr;
2587 register Lisp_Object tem, item, vector;
2588 register struct Lisp_Cons *otem;
2589 Lisp_Object len;
2590
2591 tem = read_list (1, readcharfun);
2592 len = Flength (tem);
2593 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2594
2595 size = XVECTOR (vector)->size;
2596 ptr = XVECTOR (vector)->contents;
2597 for (i = 0; i < size; i++)
2598 {
2599 item = Fcar (tem);
2600 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2601 bytecode object, the docstring containing the bytecode and
2602 constants values must be treated as unibyte and passed to
2603 Fread, to get the actual bytecode string and constants vector. */
2604 if (bytecodeflag && load_force_doc_strings)
2605 {
2606 if (i == COMPILED_BYTECODE)
2607 {
2608 if (!STRINGP (item))
2609 error ("invalid byte code");
2610
2611 /* Delay handling the bytecode slot until we know whether
2612 it is lazily-loaded (we can tell by whether the
2613 constants slot is nil). */
2614 ptr[COMPILED_CONSTANTS] = item;
2615 item = Qnil;
2616 }
2617 else if (i == COMPILED_CONSTANTS)
2618 {
2619 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2620
2621 if (NILP (item))
2622 {
2623 /* Coerce string to unibyte (like string-as-unibyte,
2624 but without generating extra garbage and
2625 guaranteeing no change in the contents). */
2626 XSTRING (bytestr)->size = STRING_BYTES (XSTRING (bytestr));
2627 SET_STRING_BYTES (XSTRING (bytestr), -1);
2628
2629 item = Fread (bytestr);
2630 if (!CONSP (item))
2631 error ("invalid byte code");
2632
2633 otem = XCONS (item);
2634 bytestr = XCAR (item);
2635 item = XCDR (item);
2636 free_cons (otem);
2637 }
2638
2639 /* Now handle the bytecode slot. */
2640 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2641 }
2642 }
2643 ptr[i] = read_pure ? Fpurecopy (item) : item;
2644 otem = XCONS (tem);
2645 tem = Fcdr (tem);
2646 free_cons (otem);
2647 }
2648 return vector;
2649 }
2650
2651 /* FLAG = 1 means check for ] to terminate rather than ) and .
2652 FLAG = -1 means check for starting with defun
2653 and make structure pure. */
2654
2655 static Lisp_Object
2656 read_list (flag, readcharfun)
2657 int flag;
2658 register Lisp_Object readcharfun;
2659 {
2660 /* -1 means check next element for defun,
2661 0 means don't check,
2662 1 means already checked and found defun. */
2663 int defunflag = flag < 0 ? -1 : 0;
2664 Lisp_Object val, tail;
2665 register Lisp_Object elt, tem;
2666 struct gcpro gcpro1, gcpro2;
2667 /* 0 is the normal case.
2668 1 means this list is a doc reference; replace it with the number 0.
2669 2 means this list is a doc reference; replace it with the doc string. */
2670 int doc_reference = 0;
2671
2672 /* Initialize this to 1 if we are reading a list. */
2673 int first_in_list = flag <= 0;
2674
2675 val = Qnil;
2676 tail = Qnil;
2677
2678 while (1)
2679 {
2680 int ch;
2681 GCPRO2 (val, tail);
2682 elt = read1 (readcharfun, &ch, first_in_list);
2683 UNGCPRO;
2684
2685 first_in_list = 0;
2686
2687 /* While building, if the list starts with #$, treat it specially. */
2688 if (EQ (elt, Vload_file_name)
2689 && ! NILP (elt)
2690 && !NILP (Vpurify_flag))
2691 {
2692 if (NILP (Vdoc_file_name))
2693 /* We have not yet called Snarf-documentation, so assume
2694 this file is described in the DOC-MM.NN file
2695 and Snarf-documentation will fill in the right value later.
2696 For now, replace the whole list with 0. */
2697 doc_reference = 1;
2698 else
2699 /* We have already called Snarf-documentation, so make a relative
2700 file name for this file, so it can be found properly
2701 in the installed Lisp directory.
2702 We don't use Fexpand_file_name because that would make
2703 the directory absolute now. */
2704 elt = concat2 (build_string ("../lisp/"),
2705 Ffile_name_nondirectory (elt));
2706 }
2707 else if (EQ (elt, Vload_file_name)
2708 && ! NILP (elt)
2709 && load_force_doc_strings)
2710 doc_reference = 2;
2711
2712 if (ch)
2713 {
2714 if (flag > 0)
2715 {
2716 if (ch == ']')
2717 return val;
2718 Fsignal (Qinvalid_read_syntax,
2719 Fcons (make_string (") or . in a vector", 18), Qnil));
2720 }
2721 if (ch == ')')
2722 return val;
2723 if (ch == '.')
2724 {
2725 GCPRO2 (val, tail);
2726 if (!NILP (tail))
2727 XCDR (tail) = read0 (readcharfun);
2728 else
2729 val = read0 (readcharfun);
2730 read1 (readcharfun, &ch, 0);
2731 UNGCPRO;
2732 if (ch == ')')
2733 {
2734 if (doc_reference == 1)
2735 return make_number (0);
2736 if (doc_reference == 2)
2737 {
2738 /* Get a doc string from the file we are loading.
2739 If it's in saved_doc_string, get it from there. */
2740 int pos = XINT (XCDR (val));
2741 /* Position is negative for user variables. */
2742 if (pos < 0) pos = -pos;
2743 if (pos >= saved_doc_string_position
2744 && pos < (saved_doc_string_position
2745 + saved_doc_string_length))
2746 {
2747 int start = pos - saved_doc_string_position;
2748 int from, to;
2749
2750 /* Process quoting with ^A,
2751 and find the end of the string,
2752 which is marked with ^_ (037). */
2753 for (from = start, to = start;
2754 saved_doc_string[from] != 037;)
2755 {
2756 int c = saved_doc_string[from++];
2757 if (c == 1)
2758 {
2759 c = saved_doc_string[from++];
2760 if (c == 1)
2761 saved_doc_string[to++] = c;
2762 else if (c == '0')
2763 saved_doc_string[to++] = 0;
2764 else if (c == '_')
2765 saved_doc_string[to++] = 037;
2766 }
2767 else
2768 saved_doc_string[to++] = c;
2769 }
2770
2771 return make_string (saved_doc_string + start,
2772 to - start);
2773 }
2774 /* Look in prev_saved_doc_string the same way. */
2775 else if (pos >= prev_saved_doc_string_position
2776 && pos < (prev_saved_doc_string_position
2777 + prev_saved_doc_string_length))
2778 {
2779 int start = pos - prev_saved_doc_string_position;
2780 int from, to;
2781
2782 /* Process quoting with ^A,
2783 and find the end of the string,
2784 which is marked with ^_ (037). */
2785 for (from = start, to = start;
2786 prev_saved_doc_string[from] != 037;)
2787 {
2788 int c = prev_saved_doc_string[from++];
2789 if (c == 1)
2790 {
2791 c = prev_saved_doc_string[from++];
2792 if (c == 1)
2793 prev_saved_doc_string[to++] = c;
2794 else if (c == '0')
2795 prev_saved_doc_string[to++] = 0;
2796 else if (c == '_')
2797 prev_saved_doc_string[to++] = 037;
2798 }
2799 else
2800 prev_saved_doc_string[to++] = c;
2801 }
2802
2803 return make_string (prev_saved_doc_string + start,
2804 to - start);
2805 }
2806 else
2807 return get_doc_string (val, 0, 0);
2808 }
2809
2810 return val;
2811 }
2812 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2813 }
2814 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2815 }
2816 tem = (read_pure && flag <= 0
2817 ? pure_cons (elt, Qnil)
2818 : Fcons (elt, Qnil));
2819 if (!NILP (tail))
2820 XCDR (tail) = tem;
2821 else
2822 val = tem;
2823 tail = tem;
2824 if (defunflag < 0)
2825 defunflag = EQ (elt, Qdefun);
2826 else if (defunflag > 0)
2827 read_pure = 1;
2828 }
2829 }
2830 \f
2831 Lisp_Object Vobarray;
2832 Lisp_Object initial_obarray;
2833
2834 /* oblookup stores the bucket number here, for the sake of Funintern. */
2835
2836 int oblookup_last_bucket_number;
2837
2838 static int hash_string ();
2839 Lisp_Object oblookup ();
2840
2841 /* Get an error if OBARRAY is not an obarray.
2842 If it is one, return it. */
2843
2844 Lisp_Object
2845 check_obarray (obarray)
2846 Lisp_Object obarray;
2847 {
2848 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2849 {
2850 /* If Vobarray is now invalid, force it to be valid. */
2851 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2852
2853 obarray = wrong_type_argument (Qvectorp, obarray);
2854 }
2855 return obarray;
2856 }
2857
2858 /* Intern the C string STR: return a symbol with that name,
2859 interned in the current obarray. */
2860
2861 Lisp_Object
2862 intern (str)
2863 char *str;
2864 {
2865 Lisp_Object tem;
2866 int len = strlen (str);
2867 Lisp_Object obarray;
2868
2869 obarray = Vobarray;
2870 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2871 obarray = check_obarray (obarray);
2872 tem = oblookup (obarray, str, len, len);
2873 if (SYMBOLP (tem))
2874 return tem;
2875 return Fintern (make_string (str, len), obarray);
2876 }
2877
2878 /* Create an uninterned symbol with name STR. */
2879
2880 Lisp_Object
2881 make_symbol (str)
2882 char *str;
2883 {
2884 int len = strlen (str);
2885
2886 return Fmake_symbol ((!NILP (Vpurify_flag)
2887 ? make_pure_string (str, len, len, 0)
2888 : make_string (str, len)));
2889 }
2890 \f
2891 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2892 "Return the canonical symbol whose name is STRING.\n\
2893 If there is none, one is created by this function and returned.\n\
2894 A second optional argument specifies the obarray to use;\n\
2895 it defaults to the value of `obarray'.")
2896 (string, obarray)
2897 Lisp_Object string, obarray;
2898 {
2899 register Lisp_Object tem, sym, *ptr;
2900
2901 if (NILP (obarray)) obarray = Vobarray;
2902 obarray = check_obarray (obarray);
2903
2904 CHECK_STRING (string, 0);
2905
2906 tem = oblookup (obarray, XSTRING (string)->data,
2907 XSTRING (string)->size,
2908 STRING_BYTES (XSTRING (string)));
2909 if (!INTEGERP (tem))
2910 return tem;
2911
2912 if (!NILP (Vpurify_flag))
2913 string = Fpurecopy (string);
2914 sym = Fmake_symbol (string);
2915
2916 if (EQ (obarray, initial_obarray))
2917 XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
2918 else
2919 XSYMBOL (sym)->interned = SYMBOL_INTERNED;
2920
2921 if ((XSTRING (string)->data[0] == ':')
2922 && EQ (obarray, initial_obarray))
2923 {
2924 XSYMBOL (sym)->constant = 1;
2925 XSYMBOL (sym)->value = sym;
2926 }
2927
2928 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2929 if (SYMBOLP (*ptr))
2930 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2931 else
2932 XSYMBOL (sym)->next = 0;
2933 *ptr = sym;
2934 return sym;
2935 }
2936
2937 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2938 "Return the canonical symbol named NAME, or nil if none exists.\n\
2939 NAME may be a string or a symbol. If it is a symbol, that exact\n\
2940 symbol is searched for.\n\
2941 A second optional argument specifies the obarray to use;\n\
2942 it defaults to the value of `obarray'.")
2943 (name, obarray)
2944 Lisp_Object name, obarray;
2945 {
2946 register Lisp_Object tem;
2947 struct Lisp_String *string;
2948
2949 if (NILP (obarray)) obarray = Vobarray;
2950 obarray = check_obarray (obarray);
2951
2952 if (!SYMBOLP (name))
2953 {
2954 CHECK_STRING (name, 0);
2955 string = XSTRING (name);
2956 }
2957 else
2958 string = XSYMBOL (name)->name;
2959
2960 tem = oblookup (obarray, string->data, string->size, STRING_BYTES (string));
2961 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
2962 return Qnil;
2963 else
2964 return tem;
2965 }
2966 \f
2967 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2968 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2969 The value is t if a symbol was found and deleted, nil otherwise.\n\
2970 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2971 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2972 OBARRAY defaults to the value of the variable `obarray'.")
2973 (name, obarray)
2974 Lisp_Object name, obarray;
2975 {
2976 register Lisp_Object string, tem;
2977 int hash;
2978
2979 if (NILP (obarray)) obarray = Vobarray;
2980 obarray = check_obarray (obarray);
2981
2982 if (SYMBOLP (name))
2983 XSETSTRING (string, XSYMBOL (name)->name);
2984 else
2985 {
2986 CHECK_STRING (name, 0);
2987 string = name;
2988 }
2989
2990 tem = oblookup (obarray, XSTRING (string)->data,
2991 XSTRING (string)->size,
2992 STRING_BYTES (XSTRING (string)));
2993 if (INTEGERP (tem))
2994 return Qnil;
2995 /* If arg was a symbol, don't delete anything but that symbol itself. */
2996 if (SYMBOLP (name) && !EQ (name, tem))
2997 return Qnil;
2998
2999 XSYMBOL (tem)->interned = SYMBOL_UNINTERNED;
3000 XSYMBOL (tem)->constant = 0;
3001 XSYMBOL (tem)->indirect_variable = 0;
3002
3003 hash = oblookup_last_bucket_number;
3004
3005 if (EQ (XVECTOR (obarray)->contents[hash], tem))
3006 {
3007 if (XSYMBOL (tem)->next)
3008 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
3009 else
3010 XSETINT (XVECTOR (obarray)->contents[hash], 0);
3011 }
3012 else
3013 {
3014 Lisp_Object tail, following;
3015
3016 for (tail = XVECTOR (obarray)->contents[hash];
3017 XSYMBOL (tail)->next;
3018 tail = following)
3019 {
3020 XSETSYMBOL (following, XSYMBOL (tail)->next);
3021 if (EQ (following, tem))
3022 {
3023 XSYMBOL (tail)->next = XSYMBOL (following)->next;
3024 break;
3025 }
3026 }
3027 }
3028
3029 return Qt;
3030 }
3031 \f
3032 /* Return the symbol in OBARRAY whose names matches the string
3033 of SIZE characters (SIZE_BYTE bytes) at PTR.
3034 If there is no such symbol in OBARRAY, return nil.
3035
3036 Also store the bucket number in oblookup_last_bucket_number. */
3037
3038 Lisp_Object
3039 oblookup (obarray, ptr, size, size_byte)
3040 Lisp_Object obarray;
3041 register char *ptr;
3042 int size, size_byte;
3043 {
3044 int hash;
3045 int obsize;
3046 register Lisp_Object tail;
3047 Lisp_Object bucket, tem;
3048
3049 if (!VECTORP (obarray)
3050 || (obsize = XVECTOR (obarray)->size) == 0)
3051 {
3052 obarray = check_obarray (obarray);
3053 obsize = XVECTOR (obarray)->size;
3054 }
3055 /* This is sometimes needed in the middle of GC. */
3056 obsize &= ~ARRAY_MARK_FLAG;
3057 /* Combining next two lines breaks VMS C 2.3. */
3058 hash = hash_string (ptr, size_byte);
3059 hash %= obsize;
3060 bucket = XVECTOR (obarray)->contents[hash];
3061 oblookup_last_bucket_number = hash;
3062 if (XFASTINT (bucket) == 0)
3063 ;
3064 else if (!SYMBOLP (bucket))
3065 error ("Bad data in guts of obarray"); /* Like CADR error message */
3066 else
3067 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3068 {
3069 if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte
3070 && XSYMBOL (tail)->name->size == size
3071 && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte))
3072 return tail;
3073 else if (XSYMBOL (tail)->next == 0)
3074 break;
3075 }
3076 XSETINT (tem, hash);
3077 return tem;
3078 }
3079
3080 static int
3081 hash_string (ptr, len)
3082 unsigned char *ptr;
3083 int len;
3084 {
3085 register unsigned char *p = ptr;
3086 register unsigned char *end = p + len;
3087 register unsigned char c;
3088 register int hash = 0;
3089
3090 while (p != end)
3091 {
3092 c = *p++;
3093 if (c >= 0140) c -= 40;
3094 hash = ((hash<<3) + (hash>>28) + c);
3095 }
3096 return hash & 07777777777;
3097 }
3098 \f
3099 void
3100 map_obarray (obarray, fn, arg)
3101 Lisp_Object obarray;
3102 void (*fn) P_ ((Lisp_Object, Lisp_Object));
3103 Lisp_Object arg;
3104 {
3105 register int i;
3106 register Lisp_Object tail;
3107 CHECK_VECTOR (obarray, 1);
3108 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3109 {
3110 tail = XVECTOR (obarray)->contents[i];
3111 if (SYMBOLP (tail))
3112 while (1)
3113 {
3114 (*fn) (tail, arg);
3115 if (XSYMBOL (tail)->next == 0)
3116 break;
3117 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3118 }
3119 }
3120 }
3121
3122 void
3123 mapatoms_1 (sym, function)
3124 Lisp_Object sym, function;
3125 {
3126 call1 (function, sym);
3127 }
3128
3129 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3130 "Call FUNCTION on every symbol in OBARRAY.\n\
3131 OBARRAY defaults to the value of `obarray'.")
3132 (function, obarray)
3133 Lisp_Object function, obarray;
3134 {
3135 if (NILP (obarray)) obarray = Vobarray;
3136 obarray = check_obarray (obarray);
3137
3138 map_obarray (obarray, mapatoms_1, function);
3139 return Qnil;
3140 }
3141
3142 #define OBARRAY_SIZE 1511
3143
3144 void
3145 init_obarray ()
3146 {
3147 Lisp_Object oblength;
3148 int hash;
3149 Lisp_Object *tem;
3150
3151 XSETFASTINT (oblength, OBARRAY_SIZE);
3152
3153 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
3154 Vobarray = Fmake_vector (oblength, make_number (0));
3155 initial_obarray = Vobarray;
3156 staticpro (&initial_obarray);
3157 /* Intern nil in the obarray */
3158 XSYMBOL (Qnil)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
3159 XSYMBOL (Qnil)->constant = 1;
3160
3161 /* These locals are to kludge around a pyramid compiler bug. */
3162 hash = hash_string ("nil", 3);
3163 /* Separate statement here to avoid VAXC bug. */
3164 hash %= OBARRAY_SIZE;
3165 tem = &XVECTOR (Vobarray)->contents[hash];
3166 *tem = Qnil;
3167
3168 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
3169 XSYMBOL (Qnil)->function = Qunbound;
3170 XSYMBOL (Qunbound)->value = Qunbound;
3171 XSYMBOL (Qunbound)->function = Qunbound;
3172
3173 Qt = intern ("t");
3174 XSYMBOL (Qnil)->value = Qnil;
3175 XSYMBOL (Qnil)->plist = Qnil;
3176 XSYMBOL (Qt)->value = Qt;
3177 XSYMBOL (Qt)->constant = 1;
3178
3179 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3180 Vpurify_flag = Qt;
3181
3182 Qvariable_documentation = intern ("variable-documentation");
3183 staticpro (&Qvariable_documentation);
3184
3185 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3186 read_buffer = (char *) xmalloc (read_buffer_size);
3187 }
3188 \f
3189 void
3190 defsubr (sname)
3191 struct Lisp_Subr *sname;
3192 {
3193 Lisp_Object sym;
3194 sym = intern (sname->symbol_name);
3195 XSETSUBR (XSYMBOL (sym)->function, sname);
3196 }
3197
3198 #ifdef NOTDEF /* use fset in subr.el now */
3199 void
3200 defalias (sname, string)
3201 struct Lisp_Subr *sname;
3202 char *string;
3203 {
3204 Lisp_Object sym;
3205 sym = intern (string);
3206 XSETSUBR (XSYMBOL (sym)->function, sname);
3207 }
3208 #endif /* NOTDEF */
3209
3210 /* Define an "integer variable"; a symbol whose value is forwarded
3211 to a C variable of type int. Sample call: */
3212 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3213 void
3214 defvar_int (namestring, address)
3215 char *namestring;
3216 int *address;
3217 {
3218 Lisp_Object sym, val;
3219 sym = intern (namestring);
3220 val = allocate_misc ();
3221 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3222 XINTFWD (val)->intvar = address;
3223 SET_SYMBOL_VALUE (sym, val);
3224 }
3225
3226 /* Similar but define a variable whose value is T if address contains 1,
3227 NIL if address contains 0 */
3228 void
3229 defvar_bool (namestring, address)
3230 char *namestring;
3231 int *address;
3232 {
3233 Lisp_Object sym, val;
3234 sym = intern (namestring);
3235 val = allocate_misc ();
3236 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3237 XBOOLFWD (val)->boolvar = address;
3238 SET_SYMBOL_VALUE (sym, val);
3239 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3240 }
3241
3242 /* Similar but define a variable whose value is the Lisp Object stored
3243 at address. Two versions: with and without gc-marking of the C
3244 variable. The nopro version is used when that variable will be
3245 gc-marked for some other reason, since marking the same slot twice
3246 can cause trouble with strings. */
3247 void
3248 defvar_lisp_nopro (namestring, address)
3249 char *namestring;
3250 Lisp_Object *address;
3251 {
3252 Lisp_Object sym, val;
3253 sym = intern (namestring);
3254 val = allocate_misc ();
3255 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3256 XOBJFWD (val)->objvar = address;
3257 SET_SYMBOL_VALUE (sym, val);
3258 }
3259
3260 void
3261 defvar_lisp (namestring, address)
3262 char *namestring;
3263 Lisp_Object *address;
3264 {
3265 defvar_lisp_nopro (namestring, address);
3266 staticpro (address);
3267 }
3268
3269 /* Similar but define a variable whose value is the Lisp Object stored in
3270 the current buffer. address is the address of the slot in the buffer
3271 that is current now. */
3272
3273 void
3274 defvar_per_buffer (namestring, address, type, doc)
3275 char *namestring;
3276 Lisp_Object *address;
3277 Lisp_Object type;
3278 char *doc;
3279 {
3280 Lisp_Object sym, val;
3281 int offset;
3282 extern struct buffer buffer_local_symbols;
3283
3284 sym = intern (namestring);
3285 val = allocate_misc ();
3286 offset = (char *)address - (char *)current_buffer;
3287
3288 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3289 XBUFFER_OBJFWD (val)->offset = offset;
3290 SET_SYMBOL_VALUE (sym, val);
3291 PER_BUFFER_SYMBOL (offset) = sym;
3292 PER_BUFFER_TYPE (offset) = type;
3293
3294 if (PER_BUFFER_IDX (offset) == 0)
3295 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3296 slot of buffer_local_flags */
3297 abort ();
3298 }
3299
3300
3301 /* Similar but define a variable whose value is the Lisp Object stored
3302 at a particular offset in the current kboard object. */
3303
3304 void
3305 defvar_kboard (namestring, offset)
3306 char *namestring;
3307 int offset;
3308 {
3309 Lisp_Object sym, val;
3310 sym = intern (namestring);
3311 val = allocate_misc ();
3312 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3313 XKBOARD_OBJFWD (val)->offset = offset;
3314 SET_SYMBOL_VALUE (sym, val);
3315 }
3316 \f
3317 /* Record the value of load-path used at the start of dumping
3318 so we can see if the site changed it later during dumping. */
3319 static Lisp_Object dump_path;
3320
3321 void
3322 init_lread ()
3323 {
3324 char *normal;
3325 int turn_off_warning = 0;
3326
3327 /* Compute the default load-path. */
3328 #ifdef CANNOT_DUMP
3329 normal = PATH_LOADSEARCH;
3330 Vload_path = decode_env_path (0, normal);
3331 #else
3332 if (NILP (Vpurify_flag))
3333 normal = PATH_LOADSEARCH;
3334 else
3335 normal = PATH_DUMPLOADSEARCH;
3336
3337 /* In a dumped Emacs, we normally have to reset the value of
3338 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3339 uses ../lisp, instead of the path of the installed elisp
3340 libraries. However, if it appears that Vload_path was changed
3341 from the default before dumping, don't override that value. */
3342 if (initialized)
3343 {
3344 if (! NILP (Fequal (dump_path, Vload_path)))
3345 {
3346 Vload_path = decode_env_path (0, normal);
3347 if (!NILP (Vinstallation_directory))
3348 {
3349 /* Add to the path the lisp subdir of the
3350 installation dir, if it exists. */
3351 Lisp_Object tem, tem1;
3352 tem = Fexpand_file_name (build_string ("lisp"),
3353 Vinstallation_directory);
3354 tem1 = Ffile_exists_p (tem);
3355 if (!NILP (tem1))
3356 {
3357 if (NILP (Fmember (tem, Vload_path)))
3358 {
3359 turn_off_warning = 1;
3360 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3361 }
3362 }
3363 else
3364 /* That dir doesn't exist, so add the build-time
3365 Lisp dirs instead. */
3366 Vload_path = nconc2 (Vload_path, dump_path);
3367
3368 /* Add leim under the installation dir, if it exists. */
3369 tem = Fexpand_file_name (build_string ("leim"),
3370 Vinstallation_directory);
3371 tem1 = Ffile_exists_p (tem);
3372 if (!NILP (tem1))
3373 {
3374 if (NILP (Fmember (tem, Vload_path)))
3375 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3376 }
3377
3378 /* Add site-list under the installation dir, if it exists. */
3379 tem = Fexpand_file_name (build_string ("site-lisp"),
3380 Vinstallation_directory);
3381 tem1 = Ffile_exists_p (tem);
3382 if (!NILP (tem1))
3383 {
3384 if (NILP (Fmember (tem, Vload_path)))
3385 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3386 }
3387
3388 /* If Emacs was not built in the source directory,
3389 and it is run from where it was built, add to load-path
3390 the lisp, leim and site-lisp dirs under that directory. */
3391
3392 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3393 {
3394 Lisp_Object tem2;
3395
3396 tem = Fexpand_file_name (build_string ("src/Makefile"),
3397 Vinstallation_directory);
3398 tem1 = Ffile_exists_p (tem);
3399
3400 /* Don't be fooled if they moved the entire source tree
3401 AFTER dumping Emacs. If the build directory is indeed
3402 different from the source dir, src/Makefile.in and
3403 src/Makefile will not be found together. */
3404 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3405 Vinstallation_directory);
3406 tem2 = Ffile_exists_p (tem);
3407 if (!NILP (tem1) && NILP (tem2))
3408 {
3409 tem = Fexpand_file_name (build_string ("lisp"),
3410 Vsource_directory);
3411
3412 if (NILP (Fmember (tem, Vload_path)))
3413 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3414
3415 tem = Fexpand_file_name (build_string ("leim"),
3416 Vsource_directory);
3417
3418 if (NILP (Fmember (tem, Vload_path)))
3419 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3420
3421 tem = Fexpand_file_name (build_string ("site-lisp"),
3422 Vsource_directory);
3423
3424 if (NILP (Fmember (tem, Vload_path)))
3425 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3426 }
3427 }
3428 }
3429 }
3430 }
3431 else
3432 {
3433 /* NORMAL refers to the lisp dir in the source directory. */
3434 /* We used to add ../lisp at the front here, but
3435 that caused trouble because it was copied from dump_path
3436 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3437 It should be unnecessary. */
3438 Vload_path = decode_env_path (0, normal);
3439 dump_path = Vload_path;
3440 }
3441 #endif
3442
3443 #ifndef WINDOWSNT
3444 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3445 almost never correct, thereby causing a warning to be printed out that
3446 confuses users. Since PATH_LOADSEARCH is always overridden by the
3447 EMACSLOADPATH environment variable below, disable the warning on NT. */
3448
3449 /* Warn if dirs in the *standard* path don't exist. */
3450 if (!turn_off_warning)
3451 {
3452 Lisp_Object path_tail;
3453
3454 for (path_tail = Vload_path;
3455 !NILP (path_tail);
3456 path_tail = XCDR (path_tail))
3457 {
3458 Lisp_Object dirfile;
3459 dirfile = Fcar (path_tail);
3460 if (STRINGP (dirfile))
3461 {
3462 dirfile = Fdirectory_file_name (dirfile);
3463 if (access (XSTRING (dirfile)->data, 0) < 0)
3464 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3465 XCAR (path_tail));
3466 }
3467 }
3468 }
3469 #endif /* WINDOWSNT */
3470
3471 /* If the EMACSLOADPATH environment variable is set, use its value.
3472 This doesn't apply if we're dumping. */
3473 #ifndef CANNOT_DUMP
3474 if (NILP (Vpurify_flag)
3475 && egetenv ("EMACSLOADPATH"))
3476 #endif
3477 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3478
3479 Vvalues = Qnil;
3480
3481 load_in_progress = 0;
3482 Vload_file_name = Qnil;
3483
3484 load_descriptor_list = Qnil;
3485
3486 Vstandard_input = Qt;
3487 Vloads_in_progress = Qnil;
3488 }
3489
3490 /* Print a warning, using format string FORMAT, that directory DIRNAME
3491 does not exist. Print it on stderr and put it in *Message*. */
3492
3493 void
3494 dir_warning (format, dirname)
3495 char *format;
3496 Lisp_Object dirname;
3497 {
3498 char *buffer
3499 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
3500
3501 fprintf (stderr, format, XSTRING (dirname)->data);
3502 sprintf (buffer, format, XSTRING (dirname)->data);
3503 /* Don't log the warning before we've initialized!! */
3504 if (initialized)
3505 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3506 }
3507
3508 void
3509 syms_of_lread ()
3510 {
3511 defsubr (&Sread);
3512 defsubr (&Sread_from_string);
3513 defsubr (&Sintern);
3514 defsubr (&Sintern_soft);
3515 defsubr (&Sunintern);
3516 defsubr (&Sload);
3517 defsubr (&Seval_buffer);
3518 defsubr (&Seval_region);
3519 defsubr (&Sread_char);
3520 defsubr (&Sread_char_exclusive);
3521 defsubr (&Sread_event);
3522 defsubr (&Sget_file_char);
3523 defsubr (&Smapatoms);
3524
3525 DEFVAR_LISP ("obarray", &Vobarray,
3526 "Symbol table for use by `intern' and `read'.\n\
3527 It is a vector whose length ought to be prime for best results.\n\
3528 The vector's contents don't make sense if examined from Lisp programs;\n\
3529 to find all the symbols in an obarray, use `mapatoms'.");
3530
3531 DEFVAR_LISP ("values", &Vvalues,
3532 "List of values of all expressions which were read, evaluated and printed.\n\
3533 Order is reverse chronological.");
3534
3535 DEFVAR_LISP ("standard-input", &Vstandard_input,
3536 "Stream for read to get input from.\n\
3537 See documentation of `read' for possible values.");
3538 Vstandard_input = Qt;
3539
3540 DEFVAR_LISP ("load-path", &Vload_path,
3541 "*List of directories to search for files to load.\n\
3542 Each element is a string (directory name) or nil (try default directory).\n\
3543 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3544 otherwise to default specified by file `epaths.h' when Emacs was built.");
3545
3546 DEFVAR_LISP ("load-suffixes", &Vload_suffixes,
3547 "*List of suffixes to try for files to load.
3548 This list should not include the empty string.");
3549 Vload_suffixes = Fcons (build_string (".elc"),
3550 Fcons (build_string (".el"), Qnil));
3551 default_suffixes = Fcons (empty_string, Qnil);
3552 staticpro (&default_suffixes);
3553
3554 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3555 "Non-nil iff inside of `load'.");
3556
3557 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3558 "An alist of expressions to be evalled when particular files are loaded.\n\
3559 Each element looks like (FILENAME FORMS...).\n\
3560 When `load' is run and the file-name argument is FILENAME,\n\
3561 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3562 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3563 with no directory specified, since that is how `load' is normally called.\n\
3564 An error in FORMS does not undo the load,\n\
3565 but does prevent execution of the rest of the FORMS.");
3566 Vafter_load_alist = Qnil;
3567
3568 DEFVAR_LISP ("load-history", &Vload_history,
3569 "Alist mapping source file names to symbols and features.\n\
3570 Each alist element is a list that starts with a file name,\n\
3571 except for one element (optional) that starts with nil and describes\n\
3572 definitions evaluated from buffers not visiting files.\n\
3573 The remaining elements of each list are symbols defined as functions\n\
3574 or variables, and cons cells `(provide . FEATURE)', `(require . FEATURE)',\n\
3575 and `(autoload . SYMBOL)'.");
3576 Vload_history = Qnil;
3577
3578 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3579 "Full name of file being loaded by `load'.");
3580 Vload_file_name = Qnil;
3581
3582 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3583 "File name, including directory, of user's initialization file.\n\
3584 If the file loaded had extension `.elc' and there was a corresponding `.el'\n\
3585 file, this variable contains the name of the .el file, suitable for use\n\
3586 by functions like `custom-save-all' which edit the init file.");
3587 Vuser_init_file = Qnil;
3588
3589 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
3590 "Used for internal purposes by `load'.");
3591 Vcurrent_load_list = Qnil;
3592
3593 DEFVAR_LISP ("load-read-function", &Vload_read_function,
3594 "Function used by `load' and `eval-region' for reading expressions.\n\
3595 The default is nil, which means use the function `read'.");
3596 Vload_read_function = Qnil;
3597
3598 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
3599 "Function called in `load' for loading an Emacs lisp source file.\n\
3600 This function is for doing code conversion before reading the source file.\n\
3601 If nil, loading is done without any code conversion.\n\
3602 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3603 FULLNAME is the full name of FILE.\n\
3604 See `load' for the meaning of the remaining arguments.");
3605 Vload_source_file_function = Qnil;
3606
3607 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
3608 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3609 This is useful when the file being loaded is a temporary copy.");
3610 load_force_doc_strings = 0;
3611
3612 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
3613 "Non-nil means `read' converts strings to unibyte whenever possible.\n\
3614 This is normally bound by `load' and `eval-buffer' to control `read',\n\
3615 and is not meant for users to change.");
3616 load_convert_to_unibyte = 0;
3617
3618 DEFVAR_LISP ("source-directory", &Vsource_directory,
3619 "Directory in which Emacs sources were found when Emacs was built.\n\
3620 You cannot count on them to still be there!");
3621 Vsource_directory
3622 = Fexpand_file_name (build_string ("../"),
3623 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3624
3625 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
3626 "List of files that were preloaded (when dumping Emacs).");
3627 Vpreloaded_file_list = Qnil;
3628
3629 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
3630 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3631 Vbyte_boolean_vars = Qnil;
3632
3633 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
3634 "Non-nil means load dangerous compiled Lisp files.\n\
3635 Some versions of XEmacs use different byte codes than Emacs. These\n\
3636 incompatible byte codes can make Emacs crash when it tries to execute\n\
3637 them.");
3638 load_dangerous_libraries = 0;
3639
3640 DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
3641 "Regular expression matching safe to load compiled Lisp files.\n\
3642 When Emacs loads a compiled Lisp file, it reads the first 512 bytes\n\
3643 from the file, and matches them against this regular expression.\n\
3644 When the regular expression matches, the file is considered to be safe\n\
3645 to load. See also `load-dangerous-libraries'.");
3646 Vbytecomp_version_regexp
3647 = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
3648
3649 DEFVAR_LISP ("recursive-load-depth-limit", &Vrecursive_load_depth_limit,
3650 "Limit for depth of recursive loads.\n\
3651 Value should be either an integer > 0 specifying the limit, or nil for\n\
3652 no limit.");
3653 Vrecursive_load_depth_limit = make_number (10);
3654
3655 /* Vsource_directory was initialized in init_lread. */
3656
3657 load_descriptor_list = Qnil;
3658 staticpro (&load_descriptor_list);
3659
3660 Qcurrent_load_list = intern ("current-load-list");
3661 staticpro (&Qcurrent_load_list);
3662
3663 Qstandard_input = intern ("standard-input");
3664 staticpro (&Qstandard_input);
3665
3666 Qread_char = intern ("read-char");
3667 staticpro (&Qread_char);
3668
3669 Qget_file_char = intern ("get-file-char");
3670 staticpro (&Qget_file_char);
3671
3672 Qbackquote = intern ("`");
3673 staticpro (&Qbackquote);
3674 Qcomma = intern (",");
3675 staticpro (&Qcomma);
3676 Qcomma_at = intern (",@");
3677 staticpro (&Qcomma_at);
3678 Qcomma_dot = intern (",.");
3679 staticpro (&Qcomma_dot);
3680
3681 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
3682 staticpro (&Qinhibit_file_name_operation);
3683
3684 Qascii_character = intern ("ascii-character");
3685 staticpro (&Qascii_character);
3686
3687 Qfunction = intern ("function");
3688 staticpro (&Qfunction);
3689
3690 Qload = intern ("load");
3691 staticpro (&Qload);
3692
3693 Qload_file_name = intern ("load-file-name");
3694 staticpro (&Qload_file_name);
3695
3696 staticpro (&dump_path);
3697
3698 staticpro (&read_objects);
3699 read_objects = Qnil;
3700 staticpro (&seen_list);
3701
3702 Vloads_in_progress = Qnil;
3703 staticpro (&Vloads_in_progress);
3704 }