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