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