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