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