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