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