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