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