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