(x_update_menu_appearance): Don't call
[emacs.git] / src / lread.c
blob735d9000fae7c74e321b3bd4d9b514c248c1a496
1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
23 #include <config.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <errno.h>
29 #include "lisp.h"
30 #include "intervals.h"
31 #include "buffer.h"
32 #include "charset.h"
33 #include <epaths.h>
34 #include "commands.h"
35 #include "keyboard.h"
36 #include "termhooks.h"
38 #ifdef lint
39 #include <sys/inode.h>
40 #endif /* lint */
42 #ifdef MSDOS
43 #if __DJGPP__ < 2
44 #include <unistd.h> /* to get X_OK */
45 #endif
46 #include "msdos.h"
47 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
53 #ifndef X_OK
54 #define X_OK 01
55 #endif
57 #include <math.h>
59 #ifdef HAVE_SETLOCALE
60 #include <locale.h>
61 #endif /* HAVE_SETLOCALE */
63 #ifndef O_RDONLY
64 #define O_RDONLY 0
65 #endif
67 #ifdef HAVE_FSEEKO
68 #define file_offset off_t
69 #define file_tell ftello
70 #else
71 #define file_offset long
72 #define file_tell ftell
73 #endif
75 #ifndef USE_CRT_DLL
76 extern int errno;
77 #endif
79 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
80 Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
81 Lisp_Object Qascii_character, Qload, Qload_file_name;
82 Lisp_Object Qbackquote, Qcomma, Qcomma_at, Qcomma_dot, Qfunction;
83 Lisp_Object Qinhibit_file_name_operation;
85 extern Lisp_Object Qevent_symbol_element_mask;
86 extern Lisp_Object Qfile_exists_p;
88 /* non-zero if inside `load' */
89 int load_in_progress;
91 /* Directory in which the sources were found. */
92 Lisp_Object Vsource_directory;
94 /* Search path for files to be loaded. */
95 Lisp_Object Vload_path;
97 /* File name of user's init file. */
98 Lisp_Object Vuser_init_file;
100 /* This is the user-visible association list that maps features to
101 lists of defs in their load files. */
102 Lisp_Object Vload_history;
104 /* This is used to build the load history. */
105 Lisp_Object Vcurrent_load_list;
107 /* List of files that were preloaded. */
108 Lisp_Object Vpreloaded_file_list;
110 /* Name of file actually being read by `load'. */
111 Lisp_Object Vload_file_name;
113 /* Function to use for reading, in `load' and friends. */
114 Lisp_Object Vload_read_function;
116 /* The association list of objects read with the #n=object form.
117 Each member of the list has the form (n . object), and is used to
118 look up the object for the corresponding #n# construct.
119 It must be set to nil before all top-level calls to read0. */
120 Lisp_Object read_objects;
122 /* Nonzero means load should forcibly load all dynamic doc strings. */
123 static int load_force_doc_strings;
125 /* Nonzero means read should convert strings to unibyte. */
126 static int load_convert_to_unibyte;
128 /* Function to use for loading an Emacs lisp source file (not
129 compiled) instead of readevalloop. */
130 Lisp_Object Vload_source_file_function;
132 /* List of all DEFVAR_BOOL variables. Used by the byte optimizer. */
133 Lisp_Object Vbyte_boolean_vars;
135 /* List of descriptors now open for Fload. */
136 static Lisp_Object load_descriptor_list;
138 /* File for get_file_char to read from. Use by load. */
139 static FILE *instream;
141 /* When nonzero, read conses in pure space */
142 static int read_pure;
144 /* For use within read-from-string (this reader is non-reentrant!!) */
145 static int read_from_string_index;
146 static int read_from_string_index_byte;
147 static int read_from_string_limit;
149 /* Number of bytes left to read in the buffer character
150 that `readchar' has already advanced over. */
151 static int readchar_backlog;
153 /* This contains the last string skipped with #@. */
154 static char *saved_doc_string;
155 /* Length of buffer allocated in saved_doc_string. */
156 static int saved_doc_string_size;
157 /* Length of actual data in saved_doc_string. */
158 static int saved_doc_string_length;
159 /* This is the file position that string came from. */
160 static file_offset saved_doc_string_position;
162 /* This contains the previous string skipped with #@.
163 We copy it from saved_doc_string when a new string
164 is put in saved_doc_string. */
165 static char *prev_saved_doc_string;
166 /* Length of buffer allocated in prev_saved_doc_string. */
167 static int prev_saved_doc_string_size;
168 /* Length of actual data in prev_saved_doc_string. */
169 static int prev_saved_doc_string_length;
170 /* This is the file position that string came from. */
171 static file_offset prev_saved_doc_string_position;
173 /* Nonzero means inside a new-style backquote
174 with no surrounding parentheses.
175 Fread initializes this to zero, so we need not specbind it
176 or worry about what happens to it when there is an error. */
177 static int new_backquote_flag;
179 /* A list of file names for files being loaded in Fload. Used to
180 check for recursive loads. */
182 static Lisp_Object Vloads_in_progress;
184 /* Limit of the depth of recursive loads. */
186 Lisp_Object Vrecursive_load_depth_limit;
188 /* Non-zero means load dangerous compiled Lisp files. */
190 int load_dangerous_libraries;
192 /* A regular expression used to detect files compiled with Emacs. */
194 static Lisp_Object Vbytecomp_version_regexp;
196 static void readevalloop P_ ((Lisp_Object, FILE*, Lisp_Object,
197 Lisp_Object (*) (), int,
198 Lisp_Object, Lisp_Object));
199 static Lisp_Object load_unwind P_ ((Lisp_Object));
200 static Lisp_Object load_descriptor_unwind P_ ((Lisp_Object));
203 /* Handle unreading and rereading of characters.
204 Write READCHAR to read a character,
205 UNREAD(c) to unread c to be read again.
207 These macros actually read/unread a byte code, multibyte characters
208 are not handled here. The caller should manage them if necessary.
211 #define READCHAR readchar (readcharfun)
212 #define UNREAD(c) unreadchar (readcharfun, c)
214 static int
215 readchar (readcharfun)
216 Lisp_Object readcharfun;
218 Lisp_Object tem;
219 register int c;
221 if (BUFFERP (readcharfun))
223 register struct buffer *inbuffer = XBUFFER (readcharfun);
225 int pt_byte = BUF_PT_BYTE (inbuffer);
226 int orig_pt_byte = pt_byte;
228 if (readchar_backlog > 0)
229 /* We get the address of the byte just passed,
230 which is the last byte of the character.
231 The other bytes in this character are consecutive with it,
232 because the gap can't be in the middle of a character. */
233 return *(BUF_BYTE_ADDRESS (inbuffer, BUF_PT_BYTE (inbuffer) - 1)
234 - --readchar_backlog);
236 if (pt_byte >= BUF_ZV_BYTE (inbuffer))
237 return -1;
239 readchar_backlog = -1;
241 if (! NILP (inbuffer->enable_multibyte_characters))
243 /* Fetch the character code from the buffer. */
244 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, pt_byte);
245 BUF_INC_POS (inbuffer, pt_byte);
246 c = STRING_CHAR (p, pt_byte - orig_pt_byte);
248 else
250 c = BUF_FETCH_BYTE (inbuffer, pt_byte);
251 pt_byte++;
253 SET_BUF_PT_BOTH (inbuffer, BUF_PT (inbuffer) + 1, pt_byte);
255 return c;
257 if (MARKERP (readcharfun))
259 register struct buffer *inbuffer = XMARKER (readcharfun)->buffer;
261 int bytepos = marker_byte_position (readcharfun);
262 int orig_bytepos = bytepos;
264 if (readchar_backlog > 0)
265 /* We get the address of the byte just passed,
266 which is the last byte of the character.
267 The other bytes in this character are consecutive with it,
268 because the gap can't be in the middle of a character. */
269 return *(BUF_BYTE_ADDRESS (inbuffer, XMARKER (readcharfun)->bytepos - 1)
270 - --readchar_backlog);
272 if (bytepos >= BUF_ZV_BYTE (inbuffer))
273 return -1;
275 readchar_backlog = -1;
277 if (! NILP (inbuffer->enable_multibyte_characters))
279 /* Fetch the character code from the buffer. */
280 unsigned char *p = BUF_BYTE_ADDRESS (inbuffer, bytepos);
281 BUF_INC_POS (inbuffer, bytepos);
282 c = STRING_CHAR (p, bytepos - orig_bytepos);
284 else
286 c = BUF_FETCH_BYTE (inbuffer, bytepos);
287 bytepos++;
290 XMARKER (readcharfun)->bytepos = bytepos;
291 XMARKER (readcharfun)->charpos++;
293 return c;
296 if (EQ (readcharfun, Qlambda))
297 return read_bytecode_char (0);
299 if (EQ (readcharfun, Qget_file_char))
301 c = getc (instream);
302 #ifdef EINTR
303 /* Interrupted reads have been observed while reading over the network */
304 while (c == EOF && ferror (instream) && errno == EINTR)
306 clearerr (instream);
307 c = getc (instream);
309 #endif
310 return c;
313 if (STRINGP (readcharfun))
315 if (read_from_string_index >= read_from_string_limit)
316 c = -1;
317 else
318 FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
319 read_from_string_index,
320 read_from_string_index_byte);
322 return c;
325 tem = call0 (readcharfun);
327 if (NILP (tem))
328 return -1;
329 return XINT (tem);
332 /* Unread the character C in the way appropriate for the stream READCHARFUN.
333 If the stream is a user function, call it with the char as argument. */
335 static void
336 unreadchar (readcharfun, c)
337 Lisp_Object readcharfun;
338 int c;
340 if (c == -1)
341 /* Don't back up the pointer if we're unreading the end-of-input mark,
342 since readchar didn't advance it when we read it. */
344 else if (BUFFERP (readcharfun))
346 struct buffer *b = XBUFFER (readcharfun);
347 int bytepos = BUF_PT_BYTE (b);
349 if (readchar_backlog >= 0)
350 readchar_backlog++;
351 else
353 BUF_PT (b)--;
354 if (! NILP (b->enable_multibyte_characters))
355 BUF_DEC_POS (b, bytepos);
356 else
357 bytepos--;
359 BUF_PT_BYTE (b) = bytepos;
362 else if (MARKERP (readcharfun))
364 struct buffer *b = XMARKER (readcharfun)->buffer;
365 int bytepos = XMARKER (readcharfun)->bytepos;
367 if (readchar_backlog >= 0)
368 readchar_backlog++;
369 else
371 XMARKER (readcharfun)->charpos--;
372 if (! NILP (b->enable_multibyte_characters))
373 BUF_DEC_POS (b, bytepos);
374 else
375 bytepos--;
377 XMARKER (readcharfun)->bytepos = bytepos;
380 else if (STRINGP (readcharfun))
382 read_from_string_index--;
383 read_from_string_index_byte
384 = string_char_to_byte (readcharfun, read_from_string_index);
386 else if (EQ (readcharfun, Qlambda))
387 read_bytecode_char (1);
388 else if (EQ (readcharfun, Qget_file_char))
389 ungetc (c, instream);
390 else
391 call1 (readcharfun, make_number (c));
394 static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
395 static int read_multibyte ();
396 static Lisp_Object substitute_object_recurse ();
397 static void substitute_object_in_subtree (), substitute_in_interval ();
400 /* Get a character from the tty. */
402 extern Lisp_Object read_char ();
404 /* Read input events until we get one that's acceptable for our purposes.
406 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
407 until we get a character we like, and then stuffed into
408 unread_switch_frame.
410 If ASCII_REQUIRED is non-zero, we check function key events to see
411 if the unmodified version of the symbol has a Qascii_character
412 property, and use that character, if present.
414 If ERROR_NONASCII is non-zero, we signal an error if the input we
415 get isn't an ASCII character with modifiers. If it's zero but
416 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
417 character.
419 If INPUT_METHOD is nonzero, we invoke the current input method
420 if the character warrants that. */
422 Lisp_Object
423 read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
424 input_method)
425 int no_switch_frame, ascii_required, error_nonascii, input_method;
427 register Lisp_Object val, delayed_switch_frame;
429 #ifdef HAVE_WINDOW_SYSTEM
430 if (display_hourglass_p)
431 cancel_hourglass ();
432 #endif
434 delayed_switch_frame = Qnil;
436 /* Read until we get an acceptable event. */
437 retry:
438 val = read_char (0, 0, 0,
439 (input_method ? Qnil : Qt),
442 if (BUFFERP (val))
443 goto retry;
445 /* switch-frame events are put off until after the next ASCII
446 character. This is better than signaling an error just because
447 the last characters were typed to a separate minibuffer frame,
448 for example. Eventually, some code which can deal with
449 switch-frame events will read it and process it. */
450 if (no_switch_frame
451 && EVENT_HAS_PARAMETERS (val)
452 && EQ (EVENT_HEAD (val), Qswitch_frame))
454 delayed_switch_frame = val;
455 goto retry;
458 if (ascii_required)
460 /* Convert certain symbols to their ASCII equivalents. */
461 if (SYMBOLP (val))
463 Lisp_Object tem, tem1;
464 tem = Fget (val, Qevent_symbol_element_mask);
465 if (!NILP (tem))
467 tem1 = Fget (Fcar (tem), Qascii_character);
468 /* Merge this symbol's modifier bits
469 with the ASCII equivalent of its basic code. */
470 if (!NILP (tem1))
471 XSETFASTINT (val, XINT (tem1) | XINT (Fcar (Fcdr (tem))));
475 /* If we don't have a character now, deal with it appropriately. */
476 if (!INTEGERP (val))
478 if (error_nonascii)
480 Vunread_command_events = Fcons (val, Qnil);
481 error ("Non-character input-event");
483 else
484 goto retry;
488 if (! NILP (delayed_switch_frame))
489 unread_switch_frame = delayed_switch_frame;
491 #ifdef HAVE_WINDOW_SYSTEM
492 if (display_hourglass_p)
493 start_hourglass ();
494 #endif
495 return val;
498 DEFUN ("read-char", Fread_char, Sread_char, 0, 2, 0,
499 "Read a character from the command input (keyboard or macro).\n\
500 It is returned as a number.\n\
501 If the user generates an event which is not a character (i.e. a mouse\n\
502 click or function key event), `read-char' signals an error. As an\n\
503 exception, switch-frame events are put off until non-ASCII events can\n\
504 be read.\n\
505 If you want to read non-character events, or ignore them, call\n\
506 `read-event' or `read-char-exclusive' instead.\n\
508 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
509 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
510 input method is turned on in the current buffer, that input method\n\
511 is used for reading a character.")
512 (prompt, inherit_input_method)
513 Lisp_Object prompt, inherit_input_method;
515 if (! NILP (prompt))
516 message_with_string ("%s", prompt, 0);
517 return read_filtered_event (1, 1, 1, ! NILP (inherit_input_method));
520 DEFUN ("read-event", Fread_event, Sread_event, 0, 2, 0,
521 "Read an event object from the input stream.\n\
522 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
523 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
524 input method is turned on in the current buffer, that input method\n\
525 is used for reading a character.")
526 (prompt, inherit_input_method)
527 Lisp_Object prompt, inherit_input_method;
529 if (! NILP (prompt))
530 message_with_string ("%s", prompt, 0);
531 return read_filtered_event (0, 0, 0, ! NILP (inherit_input_method));
534 DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 2, 0,
535 "Read a character from the command input (keyboard or macro).\n\
536 It is returned as a number. Non-character events are ignored.\n\
538 If the optional argument PROMPT is non-nil, display that as a prompt.\n\
539 If the optional argument INHERIT-INPUT-METHOD is non-nil and some\n\
540 input method is turned on in the current buffer, that input method\n\
541 is used for reading a character.")
542 (prompt, inherit_input_method)
543 Lisp_Object prompt, inherit_input_method;
545 if (! NILP (prompt))
546 message_with_string ("%s", prompt, 0);
547 return read_filtered_event (1, 1, 0, ! NILP (inherit_input_method));
550 DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
551 "Don't use this yourself.")
554 register Lisp_Object val;
555 XSETINT (val, getc (instream));
556 return val;
561 /* Value is non-zero if the file asswociated with file descriptor FD
562 is a compiled Lisp file that's safe to load. Only files compiled
563 with Emacs are safe to load. Files compiled with XEmacs can lead
564 to a crash in Fbyte_code because of an incompatible change in the
565 byte compiler. */
567 static int
568 safe_to_load_p (fd)
569 int fd;
571 char buf[512];
572 int nbytes, i;
573 int safe_p = 1;
575 /* Read the first few bytes from the file, and look for a line
576 specifying the byte compiler version used. */
577 nbytes = emacs_read (fd, buf, sizeof buf - 1);
578 if (nbytes > 0)
580 buf[nbytes] = '\0';
582 /* Skip to the next newline, skipping over the initial `ELC'
583 with NUL bytes following it. */
584 for (i = 0; i < nbytes && buf[i] != '\n'; ++i)
587 if (i < nbytes
588 && fast_c_string_match_ignore_case (Vbytecomp_version_regexp,
589 buf + i) < 0)
590 safe_p = 0;
593 lseek (fd, 0, SEEK_SET);
594 return safe_p;
598 /* Callback for record_unwind_protect. Restore the old load list OLD,
599 after loading a file successfully. */
601 static Lisp_Object
602 record_load_unwind (old)
603 Lisp_Object old;
605 return Vloads_in_progress = old;
609 DEFUN ("load", Fload, Sload, 1, 5, 0,
610 "Execute a file of Lisp code named FILE.\n\
611 First try FILE with `.elc' appended, then try with `.el',\n\
612 then try FILE unmodified. Environment variable references in FILE\n\
613 are replaced with their values by calling `substitute-in-file-name'.\n\
614 This function searches the directories in `load-path'.\n\
615 If optional second arg NOERROR is non-nil,\n\
616 report no error if FILE doesn't exist.\n\
617 Print messages at start and end of loading unless\n\
618 optional third arg NOMESSAGE is non-nil.\n\
619 If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
620 suffixes `.elc' or `.el' to the specified name FILE.\n\
621 If optional fifth arg MUST-SUFFIX is non-nil, insist on\n\
622 the suffix `.elc' or `.el'; don't accept just FILE unless\n\
623 it ends in one of those suffixes or includes a directory name.\n\
624 Return t if file exists.")
625 (file, noerror, nomessage, nosuffix, must_suffix)
626 Lisp_Object file, noerror, nomessage, nosuffix, must_suffix;
628 register FILE *stream;
629 register int fd = -1;
630 register Lisp_Object lispstream;
631 int count = specpdl_ptr - specpdl;
632 Lisp_Object temp;
633 struct gcpro gcpro1;
634 Lisp_Object found;
635 /* 1 means we printed the ".el is newer" message. */
636 int newer = 0;
637 /* 1 means we are loading a compiled file. */
638 int compiled = 0;
639 Lisp_Object handler;
640 int safe_p = 1;
641 char *fmode = "r";
642 #ifdef DOS_NT
643 fmode = "rt";
644 #endif /* DOS_NT */
646 CHECK_STRING (file, 0);
648 /* If file name is magic, call the handler. */
649 handler = Ffind_file_name_handler (file, Qload);
650 if (!NILP (handler))
651 return call5 (handler, Qload, file, noerror, nomessage, nosuffix);
653 /* Do this after the handler to avoid
654 the need to gcpro noerror, nomessage and nosuffix.
655 (Below here, we care only whether they are nil or not.) */
656 file = Fsubstitute_in_file_name (file);
658 /* Avoid weird lossage with null string as arg,
659 since it would try to load a directory as a Lisp file */
660 if (XSTRING (file)->size > 0)
662 int size = STRING_BYTES (XSTRING (file));
664 GCPRO1 (file);
666 if (! NILP (must_suffix))
668 /* Don't insist on adding a suffix if FILE already ends with one. */
669 if (size > 3
670 && !strcmp (XSTRING (file)->data + size - 3, ".el"))
671 must_suffix = Qnil;
672 else if (size > 4
673 && !strcmp (XSTRING (file)->data + size - 4, ".elc"))
674 must_suffix = Qnil;
675 /* Don't insist on adding a suffix
676 if the argument includes a directory name. */
677 else if (! NILP (Ffile_name_directory (file)))
678 must_suffix = Qnil;
681 fd = openp (Vload_path, file,
682 (!NILP (nosuffix) ? ""
683 : ! NILP (must_suffix) ? ".elc.gz:.elc:.el.gz:.el"
684 : ".elc:.elc.gz:.el.gz:.el:"),
685 &found, 0);
686 UNGCPRO;
689 if (fd == -1)
691 if (NILP (noerror))
692 while (1)
693 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
694 Fcons (file, Qnil)));
695 else
696 return Qnil;
699 /* Tell startup.el whether or not we found the user's init file. */
700 if (EQ (Qt, Vuser_init_file))
701 Vuser_init_file = found;
703 /* If FD is -2, that means openp found a magic file. */
704 if (fd == -2)
706 if (NILP (Fequal (found, file)))
707 /* If FOUND is a different file name from FILE,
708 find its handler even if we have already inhibited
709 the `load' operation on FILE. */
710 handler = Ffind_file_name_handler (found, Qt);
711 else
712 handler = Ffind_file_name_handler (found, Qload);
713 if (! NILP (handler))
714 return call5 (handler, Qload, found, noerror, nomessage, Qt);
717 /* Check if we're stuck in a recursive load cycle.
719 2000-09-21: It's not possible to just check for the file loaded
720 being a member of Vloads_in_progress. This fails because of the
721 way the byte compiler currently works; `provide's are not
722 evaluted, see font-lock.el/jit-lock.el as an example. This
723 leads to a certain amount of ``normal'' recursion.
725 Also, just loading a file recursively is not always an error in
726 the general case; the second load may do something different. */
727 if (INTEGERP (Vrecursive_load_depth_limit)
728 && XINT (Vrecursive_load_depth_limit) > 0)
730 Lisp_Object len = Flength (Vloads_in_progress);
731 if (XFASTINT (len) > XFASTINT (Vrecursive_load_depth_limit))
732 Fsignal (Qerror, Fcons (build_string ("Recursive load suspected"),
733 Fcons (found, Vloads_in_progress)));
734 record_unwind_protect (record_load_unwind, Vloads_in_progress);
735 Vloads_in_progress = Fcons (found, Vloads_in_progress);
738 if (!bcmp (&(XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 4]),
739 ".elc", 4))
740 /* Load .elc files directly, but not when they are
741 remote and have no handler! */
743 if (fd != -2)
745 struct stat s1, s2;
746 int result;
748 if (!safe_to_load_p (fd))
750 safe_p = 0;
751 if (!load_dangerous_libraries)
752 error ("File `%s' was not compiled in Emacs",
753 XSTRING (found)->data);
754 else if (!NILP (nomessage))
755 message_with_string ("File `%s' not compiled in Emacs", found, 1);
758 compiled = 1;
760 #ifdef DOS_NT
761 fmode = "rb";
762 #endif /* DOS_NT */
763 stat ((char *)XSTRING (found)->data, &s1);
764 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 0;
765 result = stat ((char *)XSTRING (found)->data, &s2);
766 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
768 /* Make the progress messages mention that source is newer. */
769 newer = 1;
771 /* If we won't print another message, mention this anyway. */
772 if (! NILP (nomessage))
773 message_with_string ("Source file `%s' newer than byte-compiled file",
774 found, 1);
776 XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 'c';
779 else
781 /* We are loading a source file (*.el). */
782 if (!NILP (Vload_source_file_function))
784 Lisp_Object val;
786 if (fd >= 0)
787 emacs_close (fd);
788 val = call4 (Vload_source_file_function, found, file,
789 NILP (noerror) ? Qnil : Qt,
790 NILP (nomessage) ? Qnil : Qt);
791 return unbind_to (count, val);
795 #ifdef WINDOWSNT
796 emacs_close (fd);
797 stream = fopen ((char *) XSTRING (found)->data, fmode);
798 #else /* not WINDOWSNT */
799 stream = fdopen (fd, fmode);
800 #endif /* not WINDOWSNT */
801 if (stream == 0)
803 emacs_close (fd);
804 error ("Failure to create stdio stream for %s", XSTRING (file)->data);
807 if (! NILP (Vpurify_flag))
808 Vpreloaded_file_list = Fcons (file, Vpreloaded_file_list);
810 if (NILP (nomessage))
812 if (!safe_p)
813 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...",
814 file, 1);
815 else if (!compiled)
816 message_with_string ("Loading %s (source)...", file, 1);
817 else if (newer)
818 message_with_string ("Loading %s (compiled; note, source file is newer)...",
819 file, 1);
820 else /* The typical case; compiled file newer than source file. */
821 message_with_string ("Loading %s...", file, 1);
824 GCPRO1 (file);
825 lispstream = Fcons (Qnil, Qnil);
826 XSETFASTINT (XCAR (lispstream), (EMACS_UINT)stream >> 16);
827 XSETFASTINT (XCDR (lispstream), (EMACS_UINT)stream & 0xffff);
828 record_unwind_protect (load_unwind, lispstream);
829 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
830 specbind (Qload_file_name, found);
831 specbind (Qinhibit_file_name_operation, Qnil);
832 load_descriptor_list
833 = Fcons (make_number (fileno (stream)), load_descriptor_list);
834 load_in_progress++;
835 readevalloop (Qget_file_char, stream, file, Feval, 0, Qnil, Qnil);
836 unbind_to (count, Qnil);
838 /* Run any load-hooks for this file. */
839 temp = Fassoc (file, Vafter_load_alist);
840 if (!NILP (temp))
841 Fprogn (Fcdr (temp));
842 UNGCPRO;
844 if (saved_doc_string)
845 free (saved_doc_string);
846 saved_doc_string = 0;
847 saved_doc_string_size = 0;
849 if (prev_saved_doc_string)
850 xfree (prev_saved_doc_string);
851 prev_saved_doc_string = 0;
852 prev_saved_doc_string_size = 0;
854 if (!noninteractive && NILP (nomessage))
856 if (!safe_p)
857 message_with_string ("Loading %s (compiled; note unsafe, not compiled in Emacs)...done",
858 file, 1);
859 else if (!compiled)
860 message_with_string ("Loading %s (source)...done", file, 1);
861 else if (newer)
862 message_with_string ("Loading %s (compiled; note, source file is newer)...done",
863 file, 1);
864 else /* The typical case; compiled file newer than source file. */
865 message_with_string ("Loading %s...done", file, 1);
868 return Qt;
871 static Lisp_Object
872 load_unwind (stream) /* used as unwind-protect function in load */
873 Lisp_Object stream;
875 fclose ((FILE *) (XFASTINT (XCAR (stream)) << 16
876 | XFASTINT (XCDR (stream))));
877 if (--load_in_progress < 0) load_in_progress = 0;
878 return Qnil;
881 static Lisp_Object
882 load_descriptor_unwind (oldlist)
883 Lisp_Object oldlist;
885 load_descriptor_list = oldlist;
886 return Qnil;
889 /* Close all descriptors in use for Floads.
890 This is used when starting a subprocess. */
892 void
893 close_load_descs ()
895 #ifndef WINDOWSNT
896 Lisp_Object tail;
897 for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
898 emacs_close (XFASTINT (XCAR (tail)));
899 #endif
902 static int
903 complete_filename_p (pathname)
904 Lisp_Object pathname;
906 register unsigned char *s = XSTRING (pathname)->data;
907 return (IS_DIRECTORY_SEP (s[0])
908 || (XSTRING (pathname)->size > 2
909 && IS_DEVICE_SEP (s[1]) && IS_DIRECTORY_SEP (s[2]))
910 #ifdef ALTOS
911 || *s == '@'
912 #endif
913 #ifdef VMS
914 || index (s, ':')
915 #endif /* VMS */
919 /* Search for a file whose name is STR, looking in directories
920 in the Lisp list PATH, and trying suffixes from SUFFIX.
921 SUFFIX is a string containing possible suffixes separated by colons.
922 On success, returns a file descriptor. On failure, returns -1.
924 EXEC_ONLY nonzero means don't open the files,
925 just look for one that is executable. In this case,
926 returns 1 on success.
928 If STOREPTR is nonzero, it points to a slot where the name of
929 the file actually found should be stored as a Lisp string.
930 nil is stored there on failure.
932 If the file we find is remote, return -2
933 but store the found remote file name in *STOREPTR.
934 We do not check for remote files if EXEC_ONLY is nonzero. */
937 openp (path, str, suffix, storeptr, exec_only)
938 Lisp_Object path, str;
939 char *suffix;
940 Lisp_Object *storeptr;
941 int exec_only;
943 register int fd;
944 int fn_size = 100;
945 char buf[100];
946 register char *fn = buf;
947 int absolute = 0;
948 int want_size;
949 Lisp_Object filename;
950 struct stat st;
951 struct gcpro gcpro1, gcpro2, gcpro3;
952 Lisp_Object string;
954 string = filename = Qnil;
955 GCPRO3 (str, string, filename);
957 if (storeptr)
958 *storeptr = Qnil;
960 if (complete_filename_p (str))
961 absolute = 1;
963 for (; !NILP (path); path = Fcdr (path))
965 char *nsuffix;
967 filename = Fexpand_file_name (str, Fcar (path));
968 if (!complete_filename_p (filename))
969 /* If there are non-absolute elts in PATH (eg ".") */
970 /* Of course, this could conceivably lose if luser sets
971 default-directory to be something non-absolute... */
973 filename = Fexpand_file_name (filename, current_buffer->directory);
974 if (!complete_filename_p (filename))
975 /* Give up on this path element! */
976 continue;
979 /* Calculate maximum size of any filename made from
980 this path element/specified file name and any possible suffix. */
981 want_size = strlen (suffix) + STRING_BYTES (XSTRING (filename)) + 1;
982 if (fn_size < want_size)
983 fn = (char *) alloca (fn_size = 100 + want_size);
985 nsuffix = suffix;
987 /* Loop over suffixes. */
988 while (1)
990 char *esuffix = (char *) index (nsuffix, ':');
991 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
992 Lisp_Object handler;
994 /* Concatenate path element/specified name with the suffix.
995 If the directory starts with /:, remove that. */
996 if (XSTRING (filename)->size > 2
997 && XSTRING (filename)->data[0] == '/'
998 && XSTRING (filename)->data[1] == ':')
1000 strncpy (fn, XSTRING (filename)->data + 2,
1001 STRING_BYTES (XSTRING (filename)) - 2);
1002 fn[STRING_BYTES (XSTRING (filename)) - 2] = 0;
1004 else
1006 strncpy (fn, XSTRING (filename)->data,
1007 STRING_BYTES (XSTRING (filename)));
1008 fn[STRING_BYTES (XSTRING (filename))] = 0;
1011 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
1012 strncat (fn, nsuffix, lsuffix);
1014 /* Check that the file exists and is not a directory. */
1015 if (absolute)
1016 handler = Qnil;
1017 else
1018 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
1019 if (! NILP (handler) && ! exec_only)
1021 int exists;
1023 string = build_string (fn);
1024 exists = ! NILP (exec_only ? Ffile_executable_p (string)
1025 : Ffile_readable_p (string));
1026 if (exists
1027 && ! NILP (Ffile_directory_p (build_string (fn))))
1028 exists = 0;
1030 if (exists)
1032 /* We succeeded; return this descriptor and filename. */
1033 if (storeptr)
1034 *storeptr = build_string (fn);
1035 UNGCPRO;
1036 return -2;
1039 else
1041 int exists = (stat (fn, &st) >= 0
1042 && (st.st_mode & S_IFMT) != S_IFDIR);
1043 if (exists)
1045 /* Check that we can access or open it. */
1046 if (exec_only)
1047 fd = (access (fn, X_OK) == 0) ? 1 : -1;
1048 else
1049 fd = emacs_open (fn, O_RDONLY, 0);
1051 if (fd >= 0)
1053 /* We succeeded; return this descriptor and filename. */
1054 if (storeptr)
1055 *storeptr = build_string (fn);
1056 UNGCPRO;
1057 return fd;
1062 /* Advance to next suffix. */
1063 if (esuffix == 0)
1064 break;
1065 nsuffix += lsuffix + 1;
1067 if (absolute)
1068 break;
1071 UNGCPRO;
1072 return -1;
1076 /* Merge the list we've accumulated of globals from the current input source
1077 into the load_history variable. The details depend on whether
1078 the source has an associated file name or not. */
1080 static void
1081 build_load_history (stream, source)
1082 FILE *stream;
1083 Lisp_Object source;
1085 register Lisp_Object tail, prev, newelt;
1086 register Lisp_Object tem, tem2;
1087 register int foundit, loading;
1089 loading = stream || !NARROWED;
1091 tail = Vload_history;
1092 prev = Qnil;
1093 foundit = 0;
1094 while (!NILP (tail))
1096 tem = Fcar (tail);
1098 /* Find the feature's previous assoc list... */
1099 if (!NILP (Fequal (source, Fcar (tem))))
1101 foundit = 1;
1103 /* If we're loading, remove it. */
1104 if (loading)
1106 if (NILP (prev))
1107 Vload_history = Fcdr (tail);
1108 else
1109 Fsetcdr (prev, Fcdr (tail));
1112 /* Otherwise, cons on new symbols that are not already members. */
1113 else
1115 tem2 = Vcurrent_load_list;
1117 while (CONSP (tem2))
1119 newelt = Fcar (tem2);
1121 if (NILP (Fmemq (newelt, tem)))
1122 Fsetcar (tail, Fcons (Fcar (tem),
1123 Fcons (newelt, Fcdr (tem))));
1125 tem2 = Fcdr (tem2);
1126 QUIT;
1130 else
1131 prev = tail;
1132 tail = Fcdr (tail);
1133 QUIT;
1136 /* If we're loading, cons the new assoc onto the front of load-history,
1137 the most-recently-loaded position. Also do this if we didn't find
1138 an existing member for the current source. */
1139 if (loading || !foundit)
1140 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1141 Vload_history);
1144 Lisp_Object
1145 unreadpure (junk) /* Used as unwind-protect function in readevalloop */
1146 Lisp_Object junk;
1148 read_pure = 0;
1149 return Qnil;
1152 static Lisp_Object
1153 readevalloop_1 (old)
1154 Lisp_Object old;
1156 load_convert_to_unibyte = ! NILP (old);
1157 return Qnil;
1160 /* Signal an `end-of-file' error, if possible with file name
1161 information. */
1163 static void
1164 end_of_file_error ()
1166 Lisp_Object data;
1168 if (STRINGP (Vload_file_name))
1169 data = Fcons (Vload_file_name, Qnil);
1170 else
1171 data = Qnil;
1173 Fsignal (Qend_of_file, data);
1176 /* UNIBYTE specifies how to set load_convert_to_unibyte
1177 for this invocation.
1178 READFUN, if non-nil, is used instead of `read'. */
1180 static void
1181 readevalloop (readcharfun, stream, sourcename, evalfun, printflag, unibyte, readfun)
1182 Lisp_Object readcharfun;
1183 FILE *stream;
1184 Lisp_Object sourcename;
1185 Lisp_Object (*evalfun) ();
1186 int printflag;
1187 Lisp_Object unibyte, readfun;
1189 register int c;
1190 register Lisp_Object val;
1191 int count = specpdl_ptr - specpdl;
1192 struct gcpro gcpro1;
1193 struct buffer *b = 0;
1194 int continue_reading_p;
1196 if (BUFFERP (readcharfun))
1197 b = XBUFFER (readcharfun);
1198 else if (MARKERP (readcharfun))
1199 b = XMARKER (readcharfun)->buffer;
1201 specbind (Qstandard_input, readcharfun);
1202 specbind (Qcurrent_load_list, Qnil);
1203 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1204 load_convert_to_unibyte = !NILP (unibyte);
1206 readchar_backlog = -1;
1208 GCPRO1 (sourcename);
1210 LOADHIST_ATTACH (sourcename);
1212 continue_reading_p = 1;
1213 while (continue_reading_p)
1215 if (b != 0 && NILP (b->name))
1216 error ("Reading from killed buffer");
1218 instream = stream;
1219 c = READCHAR;
1220 if (c == ';')
1222 while ((c = READCHAR) != '\n' && c != -1);
1223 continue;
1225 if (c < 0) break;
1227 /* Ignore whitespace here, so we can detect eof. */
1228 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
1229 continue;
1231 if (!NILP (Vpurify_flag) && c == '(')
1233 int count1 = specpdl_ptr - specpdl;
1234 record_unwind_protect (unreadpure, Qnil);
1235 val = read_list (-1, readcharfun);
1236 unbind_to (count1, Qnil);
1238 else
1240 UNREAD (c);
1241 read_objects = Qnil;
1242 if (!NILP (readfun))
1244 val = call1 (readfun, readcharfun);
1246 /* If READCHARFUN has set point to ZV, we should
1247 stop reading, even if the form read sets point
1248 to a different value when evaluated. */
1249 if (BUFFERP (readcharfun))
1251 struct buffer *b = XBUFFER (readcharfun);
1252 if (BUF_PT (b) == BUF_ZV (b))
1253 continue_reading_p = 0;
1256 else if (! NILP (Vload_read_function))
1257 val = call1 (Vload_read_function, readcharfun);
1258 else
1259 val = read0 (readcharfun);
1262 val = (*evalfun) (val);
1264 if (printflag)
1266 Vvalues = Fcons (val, Vvalues);
1267 if (EQ (Vstandard_output, Qt))
1268 Fprin1 (val, Qnil);
1269 else
1270 Fprint (val, Qnil);
1274 build_load_history (stream, sourcename);
1275 UNGCPRO;
1277 unbind_to (count, Qnil);
1280 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1281 "Execute the current buffer as Lisp code.\n\
1282 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
1283 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
1284 PRINTFLAG controls printing of output:\n\
1285 nil means discard it; anything else is stream for print.\n\
1287 If the optional third argument FILENAME is non-nil,\n\
1288 it specifies the file name to use for `load-history'.\n\
1289 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'\n\
1290 for this invocation.\n\
1292 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that\n\
1293 `print' and related functions should work normally even if PRINTFLAG is nil.\n\
1295 This function preserves the position of point.")
1296 (buffer, printflag, filename, unibyte, do_allow_print)
1297 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1299 int count = specpdl_ptr - specpdl;
1300 Lisp_Object tem, buf;
1302 if (NILP (buffer))
1303 buf = Fcurrent_buffer ();
1304 else
1305 buf = Fget_buffer (buffer);
1306 if (NILP (buf))
1307 error ("No such buffer");
1309 if (NILP (printflag) && NILP (do_allow_print))
1310 tem = Qsymbolp;
1311 else
1312 tem = printflag;
1314 if (NILP (filename))
1315 filename = XBUFFER (buf)->filename;
1317 specbind (Qstandard_output, tem);
1318 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1319 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1320 readevalloop (buf, 0, filename, Feval, !NILP (printflag), unibyte, Qnil);
1321 unbind_to (count, Qnil);
1323 return Qnil;
1326 #if 0
1327 XDEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
1328 "Execute the current buffer as Lisp code.\n\
1329 Programs can pass argument PRINTFLAG which controls printing of output:\n\
1330 nil means discard it; anything else is stream for print.\n\
1332 If there is no error, point does not move. If there is an error,\n\
1333 point remains at the end of the last character read from the buffer.")
1334 (printflag)
1335 Lisp_Object printflag;
1337 int count = specpdl_ptr - specpdl;
1338 Lisp_Object tem, cbuf;
1340 cbuf = Fcurrent_buffer ()
1342 if (NILP (printflag))
1343 tem = Qsymbolp;
1344 else
1345 tem = printflag;
1346 specbind (Qstandard_output, tem);
1347 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1348 SET_PT (BEGV);
1349 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1350 !NILP (printflag), Qnil, Qnil);
1351 return unbind_to (count, Qnil);
1353 #endif
1355 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1356 "Execute the region as Lisp code.\n\
1357 When called from programs, expects two arguments,\n\
1358 giving starting and ending indices in the current buffer\n\
1359 of the text to be executed.\n\
1360 Programs can pass third argument PRINTFLAG which controls output:\n\
1361 nil means discard it; anything else is stream for printing it.\n\
1362 Also the fourth argument READ-FUNCTION, if non-nil, is used\n\
1363 instead of `read' to read each expression. It gets one argument\n\
1364 which is the input stream for reading characters.\n\
1366 This function does not move point.")
1367 (start, end, printflag, read_function)
1368 Lisp_Object start, end, printflag, read_function;
1370 int count = specpdl_ptr - specpdl;
1371 Lisp_Object tem, cbuf;
1373 cbuf = Fcurrent_buffer ();
1375 if (NILP (printflag))
1376 tem = Qsymbolp;
1377 else
1378 tem = printflag;
1379 specbind (Qstandard_output, tem);
1381 if (NILP (printflag))
1382 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1383 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1385 /* This both uses start and checks its type. */
1386 Fgoto_char (start);
1387 Fnarrow_to_region (make_number (BEGV), end);
1388 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1389 !NILP (printflag), Qnil, read_function);
1391 return unbind_to (count, Qnil);
1395 DEFUN ("read", Fread, Sread, 0, 1, 0,
1396 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1397 If STREAM is nil, use the value of `standard-input' (which see).\n\
1398 STREAM or the value of `standard-input' may be:\n\
1399 a buffer (read from point and advance it)\n\
1400 a marker (read from where it points and advance it)\n\
1401 a function (call it with no arguments for each character,\n\
1402 call it with a char as argument to push a char back)\n\
1403 a string (takes text from string, starting at the beginning)\n\
1404 t (read text line using minibuffer and use it, or read from\n\
1405 standard input in batch mode).")
1406 (stream)
1407 Lisp_Object stream;
1409 extern Lisp_Object Fread_minibuffer ();
1411 if (NILP (stream))
1412 stream = Vstandard_input;
1413 if (EQ (stream, Qt))
1414 stream = Qread_char;
1416 readchar_backlog = -1;
1417 new_backquote_flag = 0;
1418 read_objects = Qnil;
1420 if (EQ (stream, Qread_char))
1421 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1423 if (STRINGP (stream))
1424 return Fcar (Fread_from_string (stream, Qnil, Qnil));
1426 return read0 (stream);
1429 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1430 "Read one Lisp expression which is represented as text by STRING.\n\
1431 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1432 START and END optionally delimit a substring of STRING from which to read;\n\
1433 they default to 0 and (length STRING) respectively.")
1434 (string, start, end)
1435 Lisp_Object string, start, end;
1437 int startval, endval;
1438 Lisp_Object tem;
1440 CHECK_STRING (string,0);
1442 if (NILP (end))
1443 endval = XSTRING (string)->size;
1444 else
1446 CHECK_NUMBER (end, 2);
1447 endval = XINT (end);
1448 if (endval < 0 || endval > XSTRING (string)->size)
1449 args_out_of_range (string, end);
1452 if (NILP (start))
1453 startval = 0;
1454 else
1456 CHECK_NUMBER (start, 1);
1457 startval = XINT (start);
1458 if (startval < 0 || startval > endval)
1459 args_out_of_range (string, start);
1462 read_from_string_index = startval;
1463 read_from_string_index_byte = string_char_to_byte (string, startval);
1464 read_from_string_limit = endval;
1466 new_backquote_flag = 0;
1467 read_objects = Qnil;
1469 tem = read0 (string);
1470 return Fcons (tem, make_number (read_from_string_index));
1473 /* Use this for recursive reads, in contexts where internal tokens
1474 are not allowed. */
1476 static Lisp_Object
1477 read0 (readcharfun)
1478 Lisp_Object readcharfun;
1480 register Lisp_Object val;
1481 int c;
1483 val = read1 (readcharfun, &c, 0);
1484 if (c)
1485 Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1),
1486 make_number (c)),
1487 Qnil));
1489 return val;
1492 static int read_buffer_size;
1493 static char *read_buffer;
1495 /* Read multibyte form and return it as a character. C is a first
1496 byte of multibyte form, and rest of them are read from
1497 READCHARFUN. */
1499 static int
1500 read_multibyte (c, readcharfun)
1501 register int c;
1502 Lisp_Object readcharfun;
1504 /* We need the actual character code of this multibyte
1505 characters. */
1506 unsigned char str[MAX_MULTIBYTE_LENGTH];
1507 int len = 0;
1508 int bytes;
1510 str[len++] = c;
1511 while ((c = READCHAR) >= 0xA0
1512 && len < MAX_MULTIBYTE_LENGTH)
1513 str[len++] = c;
1514 UNREAD (c);
1515 if (UNIBYTE_STR_AS_MULTIBYTE_P (str, len, bytes))
1516 return STRING_CHAR (str, len);
1517 /* The byte sequence is not valid as multibyte. Unread all bytes
1518 but the first one, and return the first byte. */
1519 while (--len > 0)
1520 UNREAD (str[len]);
1521 return str[0];
1524 /* Read a \-escape sequence, assuming we already read the `\'. */
1526 static int
1527 read_escape (readcharfun, stringp)
1528 Lisp_Object readcharfun;
1529 int stringp;
1531 register int c = READCHAR;
1532 switch (c)
1534 case -1:
1535 error ("End of file");
1537 case 'a':
1538 return '\007';
1539 case 'b':
1540 return '\b';
1541 case 'd':
1542 return 0177;
1543 case 'e':
1544 return 033;
1545 case 'f':
1546 return '\f';
1547 case 'n':
1548 return '\n';
1549 case 'r':
1550 return '\r';
1551 case 't':
1552 return '\t';
1553 case 'v':
1554 return '\v';
1555 case '\n':
1556 return -1;
1557 case ' ':
1558 if (stringp)
1559 return -1;
1560 return ' ';
1562 case 'M':
1563 c = READCHAR;
1564 if (c != '-')
1565 error ("Invalid escape character syntax");
1566 c = READCHAR;
1567 if (c == '\\')
1568 c = read_escape (readcharfun, 0);
1569 return c | meta_modifier;
1571 case 'S':
1572 c = READCHAR;
1573 if (c != '-')
1574 error ("Invalid escape character syntax");
1575 c = READCHAR;
1576 if (c == '\\')
1577 c = read_escape (readcharfun, 0);
1578 return c | shift_modifier;
1580 case 'H':
1581 c = READCHAR;
1582 if (c != '-')
1583 error ("Invalid escape character syntax");
1584 c = READCHAR;
1585 if (c == '\\')
1586 c = read_escape (readcharfun, 0);
1587 return c | hyper_modifier;
1589 case 'A':
1590 c = READCHAR;
1591 if (c != '-')
1592 error ("Invalid escape character syntax");
1593 c = READCHAR;
1594 if (c == '\\')
1595 c = read_escape (readcharfun, 0);
1596 return c | alt_modifier;
1598 case 's':
1599 c = READCHAR;
1600 if (c != '-')
1601 error ("Invalid escape character syntax");
1602 c = READCHAR;
1603 if (c == '\\')
1604 c = read_escape (readcharfun, 0);
1605 return c | super_modifier;
1607 case 'C':
1608 c = READCHAR;
1609 if (c != '-')
1610 error ("Invalid escape character syntax");
1611 case '^':
1612 c = READCHAR;
1613 if (c == '\\')
1614 c = read_escape (readcharfun, 0);
1615 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1616 return 0177 | (c & CHAR_MODIFIER_MASK);
1617 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1618 return c | ctrl_modifier;
1619 /* ASCII control chars are made from letters (both cases),
1620 as well as the non-letters within 0100...0137. */
1621 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1622 return (c & (037 | ~0177));
1623 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1624 return (c & (037 | ~0177));
1625 else
1626 return c | ctrl_modifier;
1628 case '0':
1629 case '1':
1630 case '2':
1631 case '3':
1632 case '4':
1633 case '5':
1634 case '6':
1635 case '7':
1636 /* An octal escape, as in ANSI C. */
1638 register int i = c - '0';
1639 register int count = 0;
1640 while (++count < 3)
1642 if ((c = READCHAR) >= '0' && c <= '7')
1644 i *= 8;
1645 i += c - '0';
1647 else
1649 UNREAD (c);
1650 break;
1653 return i;
1656 case 'x':
1657 /* A hex escape, as in ANSI C. */
1659 int i = 0;
1660 while (1)
1662 c = READCHAR;
1663 if (c >= '0' && c <= '9')
1665 i *= 16;
1666 i += c - '0';
1668 else if ((c >= 'a' && c <= 'f')
1669 || (c >= 'A' && c <= 'F'))
1671 i *= 16;
1672 if (c >= 'a' && c <= 'f')
1673 i += c - 'a' + 10;
1674 else
1675 i += c - 'A' + 10;
1677 else
1679 UNREAD (c);
1680 break;
1683 return i;
1686 default:
1687 if (BASE_LEADING_CODE_P (c))
1688 c = read_multibyte (c, readcharfun);
1689 return c;
1694 /* Read an integer in radix RADIX using READCHARFUN to read
1695 characters. RADIX must be in the interval [2..36]; if it isn't, a
1696 read error is signaled . Value is the integer read. Signals an
1697 error if encountering invalid read syntax or if RADIX is out of
1698 range. */
1700 static Lisp_Object
1701 read_integer (readcharfun, radix)
1702 Lisp_Object readcharfun;
1703 int radix;
1705 int ndigits = 0, invalid_p, c, sign = 0;
1706 EMACS_INT number = 0;
1708 if (radix < 2 || radix > 36)
1709 invalid_p = 1;
1710 else
1712 number = ndigits = invalid_p = 0;
1713 sign = 1;
1715 c = READCHAR;
1716 if (c == '-')
1718 c = READCHAR;
1719 sign = -1;
1721 else if (c == '+')
1722 c = READCHAR;
1724 while (c >= 0)
1726 int digit;
1728 if (c >= '0' && c <= '9')
1729 digit = c - '0';
1730 else if (c >= 'a' && c <= 'z')
1731 digit = c - 'a' + 10;
1732 else if (c >= 'A' && c <= 'Z')
1733 digit = c - 'A' + 10;
1734 else
1736 UNREAD (c);
1737 break;
1740 if (digit < 0 || digit >= radix)
1741 invalid_p = 1;
1743 number = radix * number + digit;
1744 ++ndigits;
1745 c = READCHAR;
1749 if (ndigits == 0 || invalid_p)
1751 char buf[50];
1752 sprintf (buf, "integer, radix %d", radix);
1753 Fsignal (Qinvalid_read_syntax, Fcons (build_string (buf), Qnil));
1756 return make_number (sign * number);
1760 /* If the next token is ')' or ']' or '.', we store that character
1761 in *PCH and the return value is not interesting. Else, we store
1762 zero in *PCH and we read and return one lisp object.
1764 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1766 static Lisp_Object
1767 read1 (readcharfun, pch, first_in_list)
1768 register Lisp_Object readcharfun;
1769 int *pch;
1770 int first_in_list;
1772 register int c;
1773 int uninterned_symbol = 0;
1775 *pch = 0;
1777 retry:
1779 c = READCHAR;
1780 if (c < 0)
1781 end_of_file_error ();
1783 switch (c)
1785 case '(':
1786 return read_list (0, readcharfun);
1788 case '[':
1789 return read_vector (readcharfun, 0);
1791 case ')':
1792 case ']':
1794 *pch = c;
1795 return Qnil;
1798 case '#':
1799 c = READCHAR;
1800 if (c == '^')
1802 c = READCHAR;
1803 if (c == '[')
1805 Lisp_Object tmp;
1806 tmp = read_vector (readcharfun, 0);
1807 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1808 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1809 error ("Invalid size char-table");
1810 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1811 XCHAR_TABLE (tmp)->top = Qt;
1812 return tmp;
1814 else if (c == '^')
1816 c = READCHAR;
1817 if (c == '[')
1819 Lisp_Object tmp;
1820 tmp = read_vector (readcharfun, 0);
1821 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
1822 error ("Invalid size char-table");
1823 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1824 XCHAR_TABLE (tmp)->top = Qnil;
1825 return tmp;
1827 Fsignal (Qinvalid_read_syntax,
1828 Fcons (make_string ("#^^", 3), Qnil));
1830 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1832 if (c == '&')
1834 Lisp_Object length;
1835 length = read1 (readcharfun, pch, first_in_list);
1836 c = READCHAR;
1837 if (c == '"')
1839 Lisp_Object tmp, val;
1840 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1841 / BITS_PER_CHAR);
1843 UNREAD (c);
1844 tmp = read1 (readcharfun, pch, first_in_list);
1845 if (size_in_chars != XSTRING (tmp)->size
1846 /* We used to print 1 char too many
1847 when the number of bits was a multiple of 8.
1848 Accept such input in case it came from an old version. */
1849 && ! (XFASTINT (length)
1850 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1851 Fsignal (Qinvalid_read_syntax,
1852 Fcons (make_string ("#&...", 5), Qnil));
1854 val = Fmake_bool_vector (length, Qnil);
1855 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1856 size_in_chars);
1857 /* Clear the extraneous bits in the last byte. */
1858 if (XINT (length) != size_in_chars * BITS_PER_CHAR)
1859 XBOOL_VECTOR (val)->data[size_in_chars - 1]
1860 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1861 return val;
1863 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1864 Qnil));
1866 if (c == '[')
1868 /* Accept compiled functions at read-time so that we don't have to
1869 build them using function calls. */
1870 Lisp_Object tmp;
1871 tmp = read_vector (readcharfun, 1);
1872 return Fmake_byte_code (XVECTOR (tmp)->size,
1873 XVECTOR (tmp)->contents);
1875 if (c == '(')
1877 Lisp_Object tmp;
1878 struct gcpro gcpro1;
1879 int ch;
1881 /* Read the string itself. */
1882 tmp = read1 (readcharfun, &ch, 0);
1883 if (ch != 0 || !STRINGP (tmp))
1884 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1885 GCPRO1 (tmp);
1886 /* Read the intervals and their properties. */
1887 while (1)
1889 Lisp_Object beg, end, plist;
1891 beg = read1 (readcharfun, &ch, 0);
1892 end = plist = Qnil;
1893 if (ch == ')')
1894 break;
1895 if (ch == 0)
1896 end = read1 (readcharfun, &ch, 0);
1897 if (ch == 0)
1898 plist = read1 (readcharfun, &ch, 0);
1899 if (ch)
1900 Fsignal (Qinvalid_read_syntax,
1901 Fcons (build_string ("invalid string property list"),
1902 Qnil));
1903 Fset_text_properties (beg, end, plist, tmp);
1905 UNGCPRO;
1906 return tmp;
1909 /* #@NUMBER is used to skip NUMBER following characters.
1910 That's used in .elc files to skip over doc strings
1911 and function definitions. */
1912 if (c == '@')
1914 int i, nskip = 0;
1916 /* Read a decimal integer. */
1917 while ((c = READCHAR) >= 0
1918 && c >= '0' && c <= '9')
1920 nskip *= 10;
1921 nskip += c - '0';
1923 if (c >= 0)
1924 UNREAD (c);
1926 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1928 /* If we are supposed to force doc strings into core right now,
1929 record the last string that we skipped,
1930 and record where in the file it comes from. */
1932 /* But first exchange saved_doc_string
1933 with prev_saved_doc_string, so we save two strings. */
1935 char *temp = saved_doc_string;
1936 int temp_size = saved_doc_string_size;
1937 file_offset temp_pos = saved_doc_string_position;
1938 int temp_len = saved_doc_string_length;
1940 saved_doc_string = prev_saved_doc_string;
1941 saved_doc_string_size = prev_saved_doc_string_size;
1942 saved_doc_string_position = prev_saved_doc_string_position;
1943 saved_doc_string_length = prev_saved_doc_string_length;
1945 prev_saved_doc_string = temp;
1946 prev_saved_doc_string_size = temp_size;
1947 prev_saved_doc_string_position = temp_pos;
1948 prev_saved_doc_string_length = temp_len;
1951 if (saved_doc_string_size == 0)
1953 saved_doc_string_size = nskip + 100;
1954 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1956 if (nskip > saved_doc_string_size)
1958 saved_doc_string_size = nskip + 100;
1959 saved_doc_string = (char *) xrealloc (saved_doc_string,
1960 saved_doc_string_size);
1963 saved_doc_string_position = file_tell (instream);
1965 /* Copy that many characters into saved_doc_string. */
1966 for (i = 0; i < nskip && c >= 0; i++)
1967 saved_doc_string[i] = c = READCHAR;
1969 saved_doc_string_length = i;
1971 else
1973 /* Skip that many characters. */
1974 for (i = 0; i < nskip && c >= 0; i++)
1975 c = READCHAR;
1978 goto retry;
1980 if (c == '$')
1981 return Vload_file_name;
1982 if (c == '\'')
1983 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1984 /* #:foo is the uninterned symbol named foo. */
1985 if (c == ':')
1987 uninterned_symbol = 1;
1988 c = READCHAR;
1989 goto default_label;
1991 /* Reader forms that can reuse previously read objects. */
1992 if (c >= '0' && c <= '9')
1994 int n = 0;
1995 Lisp_Object tem;
1997 /* Read a non-negative integer. */
1998 while (c >= '0' && c <= '9')
2000 n *= 10;
2001 n += c - '0';
2002 c = READCHAR;
2004 /* #n=object returns object, but associates it with n for #n#. */
2005 if (c == '=')
2007 /* Make a placeholder for #n# to use temporarily */
2008 Lisp_Object placeholder;
2009 Lisp_Object cell;
2011 placeholder = Fcons(Qnil, Qnil);
2012 cell = Fcons (make_number (n), placeholder);
2013 read_objects = Fcons (cell, read_objects);
2015 /* Read the object itself. */
2016 tem = read0 (readcharfun);
2018 /* Now put it everywhere the placeholder was... */
2019 substitute_object_in_subtree (tem, placeholder);
2021 /* ...and #n# will use the real value from now on. */
2022 Fsetcdr (cell, tem);
2024 return tem;
2026 /* #n# returns a previously read object. */
2027 if (c == '#')
2029 tem = Fassq (make_number (n), read_objects);
2030 if (CONSP (tem))
2031 return XCDR (tem);
2032 /* Fall through to error message. */
2034 else if (c == 'r' || c == 'R')
2035 return read_integer (readcharfun, n);
2037 /* Fall through to error message. */
2039 else if (c == 'x' || c == 'X')
2040 return read_integer (readcharfun, 16);
2041 else if (c == 'o' || c == 'O')
2042 return read_integer (readcharfun, 8);
2043 else if (c == 'b' || c == 'B')
2044 return read_integer (readcharfun, 2);
2046 UNREAD (c);
2047 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
2049 case ';':
2050 while ((c = READCHAR) >= 0 && c != '\n');
2051 goto retry;
2053 case '\'':
2055 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
2058 case '`':
2059 if (first_in_list)
2060 goto default_label;
2061 else
2063 Lisp_Object value;
2065 new_backquote_flag = 1;
2066 value = read0 (readcharfun);
2067 new_backquote_flag = 0;
2069 return Fcons (Qbackquote, Fcons (value, Qnil));
2072 case ',':
2073 if (new_backquote_flag)
2075 Lisp_Object comma_type = Qnil;
2076 Lisp_Object value;
2077 int ch = READCHAR;
2079 if (ch == '@')
2080 comma_type = Qcomma_at;
2081 else if (ch == '.')
2082 comma_type = Qcomma_dot;
2083 else
2085 if (ch >= 0) UNREAD (ch);
2086 comma_type = Qcomma;
2089 new_backquote_flag = 0;
2090 value = read0 (readcharfun);
2091 new_backquote_flag = 1;
2092 return Fcons (comma_type, Fcons (value, Qnil));
2094 else
2095 goto default_label;
2097 case '?':
2099 c = READCHAR;
2100 if (c < 0)
2101 end_of_file_error ();
2103 if (c == '\\')
2104 c = read_escape (readcharfun, 0);
2105 else if (BASE_LEADING_CODE_P (c))
2106 c = read_multibyte (c, readcharfun);
2108 return make_number (c);
2111 case '"':
2113 register char *p = read_buffer;
2114 register char *end = read_buffer + read_buffer_size;
2115 register int c;
2116 /* Nonzero if we saw an escape sequence specifying
2117 a multibyte character. */
2118 int force_multibyte = 0;
2119 /* Nonzero if we saw an escape sequence specifying
2120 a single-byte character. */
2121 int force_singlebyte = 0;
2122 int cancel = 0;
2123 int nchars;
2125 while ((c = READCHAR) >= 0
2126 && c != '\"')
2128 if (end - p < MAX_MULTIBYTE_LENGTH)
2130 int offset = p - read_buffer;
2131 read_buffer = (char *) xrealloc (read_buffer,
2132 read_buffer_size *= 2);
2133 p = read_buffer + offset;
2134 end = read_buffer + read_buffer_size;
2137 if (c == '\\')
2139 c = read_escape (readcharfun, 1);
2141 /* C is -1 if \ newline has just been seen */
2142 if (c == -1)
2144 if (p == read_buffer)
2145 cancel = 1;
2146 continue;
2149 /* If an escape specifies a non-ASCII single-byte character,
2150 this must be a unibyte string. */
2151 if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))
2152 && ! ASCII_BYTE_P ((c & ~CHAR_MODIFIER_MASK)))
2153 force_singlebyte = 1;
2156 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
2158 /* Any modifiers for a multibyte character are invalid. */
2159 if (c & CHAR_MODIFIER_MASK)
2160 error ("Invalid modifier in string");
2161 p += CHAR_STRING (c, p);
2162 force_multibyte = 1;
2164 else
2166 /* Allow `\C- ' and `\C-?'. */
2167 if (c == (CHAR_CTL | ' '))
2168 c = 0;
2169 else if (c == (CHAR_CTL | '?'))
2170 c = 127;
2172 if (c & CHAR_SHIFT)
2174 /* Shift modifier is valid only with [A-Za-z]. */
2175 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
2176 c &= ~CHAR_SHIFT;
2177 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
2178 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
2181 if (c & CHAR_META)
2182 /* Move the meta bit to the right place for a string. */
2183 c = (c & ~CHAR_META) | 0x80;
2184 if (c & ~0xff)
2185 error ("Invalid modifier in string");
2186 *p++ = c;
2189 if (c < 0)
2190 end_of_file_error ();
2192 /* If purifying, and string starts with \ newline,
2193 return zero instead. This is for doc strings
2194 that we are really going to find in etc/DOC.nn.nn */
2195 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2196 return make_number (0);
2198 if (force_multibyte)
2199 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2200 p - read_buffer, &nchars);
2201 else if (force_singlebyte)
2202 nchars = p - read_buffer;
2203 else if (load_convert_to_unibyte)
2205 Lisp_Object string;
2206 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2207 p - read_buffer, &nchars);
2208 if (p - read_buffer != nchars)
2210 string = make_multibyte_string (read_buffer, nchars,
2211 p - read_buffer);
2212 return Fstring_make_unibyte (string);
2215 else if (EQ (readcharfun, Qget_file_char)
2216 || EQ (readcharfun, Qlambda))
2217 /* Nowadays, reading directly from a file is used only for
2218 compiled Emacs Lisp files, and those always use the
2219 Emacs internal encoding. Meanwhile, Qlambda is used
2220 for reading dynamic byte code (compiled with
2221 byte-compile-dynamic = t). */
2222 p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
2223 p - read_buffer, &nchars);
2224 else
2225 /* In all other cases, if we read these bytes as
2226 separate characters, treat them as separate characters now. */
2227 nchars = p - read_buffer;
2229 if (read_pure)
2230 return make_pure_string (read_buffer, nchars, p - read_buffer,
2231 (force_multibyte
2232 || (p - read_buffer != nchars)));
2233 return make_specified_string (read_buffer, nchars, p - read_buffer,
2234 (force_multibyte
2235 || (p - read_buffer != nchars)));
2238 case '.':
2240 int next_char = READCHAR;
2241 UNREAD (next_char);
2243 if (next_char <= 040
2244 || index ("\"'`,(", next_char))
2246 *pch = c;
2247 return Qnil;
2250 /* Otherwise, we fall through! Note that the atom-reading loop
2251 below will now loop at least once, assuring that we will not
2252 try to UNREAD two characters in a row. */
2254 default:
2255 default_label:
2256 if (c <= 040) goto retry;
2258 char *p = read_buffer;
2259 int quoted = 0;
2262 char *end = read_buffer + read_buffer_size;
2264 while (c > 040
2265 && !(c == '\"' || c == '\'' || c == ';'
2266 || c == '(' || c == ')'
2267 || c == '[' || c == ']' || c == '#'))
2269 if (end - p < MAX_MULTIBYTE_LENGTH)
2271 int offset = p - read_buffer;
2272 read_buffer = (char *) xrealloc (read_buffer,
2273 read_buffer_size *= 2);
2274 p = read_buffer + offset;
2275 end = read_buffer + read_buffer_size;
2278 if (c == '\\')
2280 c = READCHAR;
2281 if (c == -1)
2282 end_of_file_error ();
2283 quoted = 1;
2286 if (! SINGLE_BYTE_CHAR_P (c))
2287 p += CHAR_STRING (c, p);
2288 else
2289 *p++ = c;
2291 c = READCHAR;
2294 if (p == end)
2296 int offset = p - read_buffer;
2297 read_buffer = (char *) xrealloc (read_buffer,
2298 read_buffer_size *= 2);
2299 p = read_buffer + offset;
2300 end = read_buffer + read_buffer_size;
2302 *p = 0;
2303 if (c >= 0)
2304 UNREAD (c);
2307 if (!quoted && !uninterned_symbol)
2309 register char *p1;
2310 register Lisp_Object val;
2311 p1 = read_buffer;
2312 if (*p1 == '+' || *p1 == '-') p1++;
2313 /* Is it an integer? */
2314 if (p1 != p)
2316 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2317 /* Integers can have trailing decimal points. */
2318 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2319 if (p1 == p)
2320 /* It is an integer. */
2322 if (p1[-1] == '.')
2323 p1[-1] = '\0';
2324 if (sizeof (int) == sizeof (EMACS_INT))
2325 XSETINT (val, atoi (read_buffer));
2326 else if (sizeof (long) == sizeof (EMACS_INT))
2327 XSETINT (val, atol (read_buffer));
2328 else
2329 abort ();
2330 return val;
2333 if (isfloat_string (read_buffer))
2335 /* Compute NaN and infinities using 0.0 in a variable,
2336 to cope with compilers that think they are smarter
2337 than we are. */
2338 double zero = 0.0;
2340 double value;
2342 /* Negate the value ourselves. This treats 0, NaNs,
2343 and infinity properly on IEEE floating point hosts,
2344 and works around a common bug where atof ("-0.0")
2345 drops the sign. */
2346 int negative = read_buffer[0] == '-';
2348 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2349 returns 1, is if the input ends in e+INF or e+NaN. */
2350 switch (p[-1])
2352 case 'F':
2353 value = 1.0 / zero;
2354 break;
2355 case 'N':
2356 value = zero / zero;
2357 break;
2358 default:
2359 value = atof (read_buffer + negative);
2360 break;
2363 return make_float (negative ? - value : value);
2367 if (uninterned_symbol)
2368 return make_symbol (read_buffer);
2369 else
2370 return intern (read_buffer);
2376 /* List of nodes we've seen during substitute_object_in_subtree. */
2377 static Lisp_Object seen_list;
2379 static void
2380 substitute_object_in_subtree (object, placeholder)
2381 Lisp_Object object;
2382 Lisp_Object placeholder;
2384 Lisp_Object check_object;
2386 /* We haven't seen any objects when we start. */
2387 seen_list = Qnil;
2389 /* Make all the substitutions. */
2390 check_object
2391 = substitute_object_recurse (object, placeholder, object);
2393 /* Clear seen_list because we're done with it. */
2394 seen_list = Qnil;
2396 /* The returned object here is expected to always eq the
2397 original. */
2398 if (!EQ (check_object, object))
2399 error ("Unexpected mutation error in reader");
2402 /* Feval doesn't get called from here, so no gc protection is needed. */
2403 #define SUBSTITUTE(get_val, set_val) \
2405 Lisp_Object old_value = get_val; \
2406 Lisp_Object true_value \
2407 = substitute_object_recurse (object, placeholder,\
2408 old_value); \
2410 if (!EQ (old_value, true_value)) \
2412 set_val; \
2416 static Lisp_Object
2417 substitute_object_recurse (object, placeholder, subtree)
2418 Lisp_Object object;
2419 Lisp_Object placeholder;
2420 Lisp_Object subtree;
2422 /* If we find the placeholder, return the target object. */
2423 if (EQ (placeholder, subtree))
2424 return object;
2426 /* If we've been to this node before, don't explore it again. */
2427 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2428 return subtree;
2430 /* If this node can be the entry point to a cycle, remember that
2431 we've seen it. It can only be such an entry point if it was made
2432 by #n=, which means that we can find it as a value in
2433 read_objects. */
2434 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2435 seen_list = Fcons (subtree, seen_list);
2437 /* Recurse according to subtree's type.
2438 Every branch must return a Lisp_Object. */
2439 switch (XTYPE (subtree))
2441 case Lisp_Vectorlike:
2443 int i;
2444 int length = XINT (Flength(subtree));
2445 for (i = 0; i < length; i++)
2447 Lisp_Object idx = make_number (i);
2448 SUBSTITUTE (Faref (subtree, idx),
2449 Faset (subtree, idx, true_value));
2451 return subtree;
2454 case Lisp_Cons:
2456 SUBSTITUTE (Fcar_safe (subtree),
2457 Fsetcar (subtree, true_value));
2458 SUBSTITUTE (Fcdr_safe (subtree),
2459 Fsetcdr (subtree, true_value));
2460 return subtree;
2463 case Lisp_String:
2465 /* Check for text properties in each interval.
2466 substitute_in_interval contains part of the logic. */
2468 INTERVAL root_interval = XSTRING (subtree)->intervals;
2469 Lisp_Object arg = Fcons (object, placeholder);
2471 traverse_intervals (root_interval, 1, 0,
2472 &substitute_in_interval, arg);
2474 return subtree;
2477 /* Other types don't recurse any further. */
2478 default:
2479 return subtree;
2483 /* Helper function for substitute_object_recurse. */
2484 static void
2485 substitute_in_interval (interval, arg)
2486 INTERVAL interval;
2487 Lisp_Object arg;
2489 Lisp_Object object = Fcar (arg);
2490 Lisp_Object placeholder = Fcdr (arg);
2492 SUBSTITUTE(interval->plist, interval->plist = true_value);
2496 #define LEAD_INT 1
2497 #define DOT_CHAR 2
2498 #define TRAIL_INT 4
2499 #define E_CHAR 8
2500 #define EXP_INT 16
2503 isfloat_string (cp)
2504 register char *cp;
2506 register int state;
2508 char *start = cp;
2510 state = 0;
2511 if (*cp == '+' || *cp == '-')
2512 cp++;
2514 if (*cp >= '0' && *cp <= '9')
2516 state |= LEAD_INT;
2517 while (*cp >= '0' && *cp <= '9')
2518 cp++;
2520 if (*cp == '.')
2522 state |= DOT_CHAR;
2523 cp++;
2525 if (*cp >= '0' && *cp <= '9')
2527 state |= TRAIL_INT;
2528 while (*cp >= '0' && *cp <= '9')
2529 cp++;
2531 if (*cp == 'e' || *cp == 'E')
2533 state |= E_CHAR;
2534 cp++;
2535 if (*cp == '+' || *cp == '-')
2536 cp++;
2539 if (*cp >= '0' && *cp <= '9')
2541 state |= EXP_INT;
2542 while (*cp >= '0' && *cp <= '9')
2543 cp++;
2545 else if (cp == start)
2547 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2549 state |= EXP_INT;
2550 cp += 3;
2552 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2554 state |= EXP_INT;
2555 cp += 3;
2558 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2559 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2560 || state == (DOT_CHAR|TRAIL_INT)
2561 || state == (LEAD_INT|E_CHAR|EXP_INT)
2562 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2563 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2567 static Lisp_Object
2568 read_vector (readcharfun, bytecodeflag)
2569 Lisp_Object readcharfun;
2570 int bytecodeflag;
2572 register int i;
2573 register int size;
2574 register Lisp_Object *ptr;
2575 register Lisp_Object tem, item, vector;
2576 register struct Lisp_Cons *otem;
2577 Lisp_Object len;
2579 tem = read_list (1, readcharfun);
2580 len = Flength (tem);
2581 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2583 size = XVECTOR (vector)->size;
2584 ptr = XVECTOR (vector)->contents;
2585 for (i = 0; i < size; i++)
2587 item = Fcar (tem);
2588 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2589 bytecode object, the docstring containing the bytecode and
2590 constants values must be treated as unibyte and passed to
2591 Fread, to get the actual bytecode string and constants vector. */
2592 if (bytecodeflag && load_force_doc_strings)
2594 if (i == COMPILED_BYTECODE)
2596 if (!STRINGP (item))
2597 error ("invalid byte code");
2599 /* Delay handling the bytecode slot until we know whether
2600 it is lazily-loaded (we can tell by whether the
2601 constants slot is nil). */
2602 ptr[COMPILED_CONSTANTS] = item;
2603 item = Qnil;
2605 else if (i == COMPILED_CONSTANTS)
2607 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2609 if (NILP (item))
2611 /* Coerce string to unibyte (like string-as-unibyte,
2612 but without generating extra garbage and
2613 guaranteeing no change in the contents). */
2614 XSTRING (bytestr)->size = STRING_BYTES (XSTRING (bytestr));
2615 SET_STRING_BYTES (XSTRING (bytestr), -1);
2617 item = Fread (bytestr);
2618 if (!CONSP (item))
2619 error ("invalid byte code");
2621 otem = XCONS (item);
2622 bytestr = XCAR (item);
2623 item = XCDR (item);
2624 free_cons (otem);
2627 /* Now handle the bytecode slot. */
2628 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2631 ptr[i] = read_pure ? Fpurecopy (item) : item;
2632 otem = XCONS (tem);
2633 tem = Fcdr (tem);
2634 free_cons (otem);
2636 return vector;
2639 /* FLAG = 1 means check for ] to terminate rather than ) and .
2640 FLAG = -1 means check for starting with defun
2641 and make structure pure. */
2643 static Lisp_Object
2644 read_list (flag, readcharfun)
2645 int flag;
2646 register Lisp_Object readcharfun;
2648 /* -1 means check next element for defun,
2649 0 means don't check,
2650 1 means already checked and found defun. */
2651 int defunflag = flag < 0 ? -1 : 0;
2652 Lisp_Object val, tail;
2653 register Lisp_Object elt, tem;
2654 struct gcpro gcpro1, gcpro2;
2655 /* 0 is the normal case.
2656 1 means this list is a doc reference; replace it with the number 0.
2657 2 means this list is a doc reference; replace it with the doc string. */
2658 int doc_reference = 0;
2660 /* Initialize this to 1 if we are reading a list. */
2661 int first_in_list = flag <= 0;
2663 val = Qnil;
2664 tail = Qnil;
2666 while (1)
2668 int ch;
2669 GCPRO2 (val, tail);
2670 elt = read1 (readcharfun, &ch, first_in_list);
2671 UNGCPRO;
2673 first_in_list = 0;
2675 /* While building, if the list starts with #$, treat it specially. */
2676 if (EQ (elt, Vload_file_name)
2677 && ! NILP (elt)
2678 && !NILP (Vpurify_flag))
2680 if (NILP (Vdoc_file_name))
2681 /* We have not yet called Snarf-documentation, so assume
2682 this file is described in the DOC-MM.NN file
2683 and Snarf-documentation will fill in the right value later.
2684 For now, replace the whole list with 0. */
2685 doc_reference = 1;
2686 else
2687 /* We have already called Snarf-documentation, so make a relative
2688 file name for this file, so it can be found properly
2689 in the installed Lisp directory.
2690 We don't use Fexpand_file_name because that would make
2691 the directory absolute now. */
2692 elt = concat2 (build_string ("../lisp/"),
2693 Ffile_name_nondirectory (elt));
2695 else if (EQ (elt, Vload_file_name)
2696 && ! NILP (elt)
2697 && load_force_doc_strings)
2698 doc_reference = 2;
2700 if (ch)
2702 if (flag > 0)
2704 if (ch == ']')
2705 return val;
2706 Fsignal (Qinvalid_read_syntax,
2707 Fcons (make_string (") or . in a vector", 18), Qnil));
2709 if (ch == ')')
2710 return val;
2711 if (ch == '.')
2713 GCPRO2 (val, tail);
2714 if (!NILP (tail))
2715 XCDR (tail) = read0 (readcharfun);
2716 else
2717 val = read0 (readcharfun);
2718 read1 (readcharfun, &ch, 0);
2719 UNGCPRO;
2720 if (ch == ')')
2722 if (doc_reference == 1)
2723 return make_number (0);
2724 if (doc_reference == 2)
2726 /* Get a doc string from the file we are loading.
2727 If it's in saved_doc_string, get it from there. */
2728 int pos = XINT (XCDR (val));
2729 /* Position is negative for user variables. */
2730 if (pos < 0) pos = -pos;
2731 if (pos >= saved_doc_string_position
2732 && pos < (saved_doc_string_position
2733 + saved_doc_string_length))
2735 int start = pos - saved_doc_string_position;
2736 int from, to;
2738 /* Process quoting with ^A,
2739 and find the end of the string,
2740 which is marked with ^_ (037). */
2741 for (from = start, to = start;
2742 saved_doc_string[from] != 037;)
2744 int c = saved_doc_string[from++];
2745 if (c == 1)
2747 c = saved_doc_string[from++];
2748 if (c == 1)
2749 saved_doc_string[to++] = c;
2750 else if (c == '0')
2751 saved_doc_string[to++] = 0;
2752 else if (c == '_')
2753 saved_doc_string[to++] = 037;
2755 else
2756 saved_doc_string[to++] = c;
2759 return make_string (saved_doc_string + start,
2760 to - start);
2762 /* Look in prev_saved_doc_string the same way. */
2763 else if (pos >= prev_saved_doc_string_position
2764 && pos < (prev_saved_doc_string_position
2765 + prev_saved_doc_string_length))
2767 int start = pos - prev_saved_doc_string_position;
2768 int from, to;
2770 /* Process quoting with ^A,
2771 and find the end of the string,
2772 which is marked with ^_ (037). */
2773 for (from = start, to = start;
2774 prev_saved_doc_string[from] != 037;)
2776 int c = prev_saved_doc_string[from++];
2777 if (c == 1)
2779 c = prev_saved_doc_string[from++];
2780 if (c == 1)
2781 prev_saved_doc_string[to++] = c;
2782 else if (c == '0')
2783 prev_saved_doc_string[to++] = 0;
2784 else if (c == '_')
2785 prev_saved_doc_string[to++] = 037;
2787 else
2788 prev_saved_doc_string[to++] = c;
2791 return make_string (prev_saved_doc_string + start,
2792 to - start);
2794 else
2795 return get_doc_string (val, 0, 0);
2798 return val;
2800 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2802 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2804 tem = (read_pure && flag <= 0
2805 ? pure_cons (elt, Qnil)
2806 : Fcons (elt, Qnil));
2807 if (!NILP (tail))
2808 XCDR (tail) = tem;
2809 else
2810 val = tem;
2811 tail = tem;
2812 if (defunflag < 0)
2813 defunflag = EQ (elt, Qdefun);
2814 else if (defunflag > 0)
2815 read_pure = 1;
2819 Lisp_Object Vobarray;
2820 Lisp_Object initial_obarray;
2822 /* oblookup stores the bucket number here, for the sake of Funintern. */
2824 int oblookup_last_bucket_number;
2826 static int hash_string ();
2827 Lisp_Object oblookup ();
2829 /* Get an error if OBARRAY is not an obarray.
2830 If it is one, return it. */
2832 Lisp_Object
2833 check_obarray (obarray)
2834 Lisp_Object obarray;
2836 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2838 /* If Vobarray is now invalid, force it to be valid. */
2839 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2841 obarray = wrong_type_argument (Qvectorp, obarray);
2843 return obarray;
2846 /* Intern the C string STR: return a symbol with that name,
2847 interned in the current obarray. */
2849 Lisp_Object
2850 intern (str)
2851 char *str;
2853 Lisp_Object tem;
2854 int len = strlen (str);
2855 Lisp_Object obarray;
2857 obarray = Vobarray;
2858 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2859 obarray = check_obarray (obarray);
2860 tem = oblookup (obarray, str, len, len);
2861 if (SYMBOLP (tem))
2862 return tem;
2863 return Fintern (make_string (str, len), obarray);
2866 /* Create an uninterned symbol with name STR. */
2868 Lisp_Object
2869 make_symbol (str)
2870 char *str;
2872 int len = strlen (str);
2874 return Fmake_symbol ((!NILP (Vpurify_flag)
2875 ? make_pure_string (str, len, len, 0)
2876 : make_string (str, len)));
2879 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2880 "Return the canonical symbol whose name is STRING.\n\
2881 If there is none, one is created by this function and returned.\n\
2882 A second optional argument specifies the obarray to use;\n\
2883 it defaults to the value of `obarray'.")
2884 (string, obarray)
2885 Lisp_Object string, obarray;
2887 register Lisp_Object tem, sym, *ptr;
2889 if (NILP (obarray)) obarray = Vobarray;
2890 obarray = check_obarray (obarray);
2892 CHECK_STRING (string, 0);
2894 tem = oblookup (obarray, XSTRING (string)->data,
2895 XSTRING (string)->size,
2896 STRING_BYTES (XSTRING (string)));
2897 if (!INTEGERP (tem))
2898 return tem;
2900 if (!NILP (Vpurify_flag))
2901 string = Fpurecopy (string);
2902 sym = Fmake_symbol (string);
2903 XSYMBOL (sym)->obarray = obarray;
2905 if ((XSTRING (string)->data[0] == ':')
2906 && EQ (obarray, initial_obarray))
2907 XSYMBOL (sym)->value = sym;
2909 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2910 if (SYMBOLP (*ptr))
2911 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2912 else
2913 XSYMBOL (sym)->next = 0;
2914 *ptr = sym;
2915 return sym;
2918 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2919 "Return the canonical symbol named NAME, or nil if none exists.\n\
2920 NAME may be a string or a symbol. If it is a symbol, that exact\n\
2921 symbol is searched for.\n\
2922 A second optional argument specifies the obarray to use;\n\
2923 it defaults to the value of `obarray'.")
2924 (name, obarray)
2925 Lisp_Object name, obarray;
2927 register Lisp_Object tem;
2928 struct Lisp_String *string;
2930 if (NILP (obarray)) obarray = Vobarray;
2931 obarray = check_obarray (obarray);
2933 if (!SYMBOLP (name))
2935 CHECK_STRING (name, 0);
2936 string = XSTRING (name);
2938 else
2939 string = XSYMBOL (name)->name;
2941 tem = oblookup (obarray, string->data, string->size, STRING_BYTES (string));
2942 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
2943 return Qnil;
2944 else
2945 return tem;
2948 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2949 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2950 The value is t if a symbol was found and deleted, nil otherwise.\n\
2951 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2952 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2953 OBARRAY defaults to the value of the variable `obarray'.")
2954 (name, obarray)
2955 Lisp_Object name, obarray;
2957 register Lisp_Object string, tem;
2958 int hash;
2960 if (NILP (obarray)) obarray = Vobarray;
2961 obarray = check_obarray (obarray);
2963 if (SYMBOLP (name))
2964 XSETSTRING (string, XSYMBOL (name)->name);
2965 else
2967 CHECK_STRING (name, 0);
2968 string = name;
2971 tem = oblookup (obarray, XSTRING (string)->data,
2972 XSTRING (string)->size,
2973 STRING_BYTES (XSTRING (string)));
2974 if (INTEGERP (tem))
2975 return Qnil;
2976 /* If arg was a symbol, don't delete anything but that symbol itself. */
2977 if (SYMBOLP (name) && !EQ (name, tem))
2978 return Qnil;
2980 XSYMBOL (tem)->obarray = Qnil;
2982 hash = oblookup_last_bucket_number;
2984 if (EQ (XVECTOR (obarray)->contents[hash], tem))
2986 if (XSYMBOL (tem)->next)
2987 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
2988 else
2989 XSETINT (XVECTOR (obarray)->contents[hash], 0);
2991 else
2993 Lisp_Object tail, following;
2995 for (tail = XVECTOR (obarray)->contents[hash];
2996 XSYMBOL (tail)->next;
2997 tail = following)
2999 XSETSYMBOL (following, XSYMBOL (tail)->next);
3000 if (EQ (following, tem))
3002 XSYMBOL (tail)->next = XSYMBOL (following)->next;
3003 break;
3008 return Qt;
3011 /* Return the symbol in OBARRAY whose names matches the string
3012 of SIZE characters (SIZE_BYTE bytes) at PTR.
3013 If there is no such symbol in OBARRAY, return nil.
3015 Also store the bucket number in oblookup_last_bucket_number. */
3017 Lisp_Object
3018 oblookup (obarray, ptr, size, size_byte)
3019 Lisp_Object obarray;
3020 register char *ptr;
3021 int size, size_byte;
3023 int hash;
3024 int obsize;
3025 register Lisp_Object tail;
3026 Lisp_Object bucket, tem;
3028 if (!VECTORP (obarray)
3029 || (obsize = XVECTOR (obarray)->size) == 0)
3031 obarray = check_obarray (obarray);
3032 obsize = XVECTOR (obarray)->size;
3034 /* This is sometimes needed in the middle of GC. */
3035 obsize &= ~ARRAY_MARK_FLAG;
3036 /* Combining next two lines breaks VMS C 2.3. */
3037 hash = hash_string (ptr, size_byte);
3038 hash %= obsize;
3039 bucket = XVECTOR (obarray)->contents[hash];
3040 oblookup_last_bucket_number = hash;
3041 if (XFASTINT (bucket) == 0)
3043 else if (!SYMBOLP (bucket))
3044 error ("Bad data in guts of obarray"); /* Like CADR error message */
3045 else
3046 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
3048 if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte
3049 && XSYMBOL (tail)->name->size == size
3050 && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte))
3051 return tail;
3052 else if (XSYMBOL (tail)->next == 0)
3053 break;
3055 XSETINT (tem, hash);
3056 return tem;
3059 static int
3060 hash_string (ptr, len)
3061 unsigned char *ptr;
3062 int len;
3064 register unsigned char *p = ptr;
3065 register unsigned char *end = p + len;
3066 register unsigned char c;
3067 register int hash = 0;
3069 while (p != end)
3071 c = *p++;
3072 if (c >= 0140) c -= 40;
3073 hash = ((hash<<3) + (hash>>28) + c);
3075 return hash & 07777777777;
3078 void
3079 map_obarray (obarray, fn, arg)
3080 Lisp_Object obarray;
3081 void (*fn) P_ ((Lisp_Object, Lisp_Object));
3082 Lisp_Object arg;
3084 register int i;
3085 register Lisp_Object tail;
3086 CHECK_VECTOR (obarray, 1);
3087 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
3089 tail = XVECTOR (obarray)->contents[i];
3090 if (SYMBOLP (tail))
3091 while (1)
3093 (*fn) (tail, arg);
3094 if (XSYMBOL (tail)->next == 0)
3095 break;
3096 XSETSYMBOL (tail, XSYMBOL (tail)->next);
3101 void
3102 mapatoms_1 (sym, function)
3103 Lisp_Object sym, function;
3105 call1 (function, sym);
3108 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
3109 "Call FUNCTION on every symbol in OBARRAY.\n\
3110 OBARRAY defaults to the value of `obarray'.")
3111 (function, obarray)
3112 Lisp_Object function, obarray;
3114 if (NILP (obarray)) obarray = Vobarray;
3115 obarray = check_obarray (obarray);
3117 map_obarray (obarray, mapatoms_1, function);
3118 return Qnil;
3121 #define OBARRAY_SIZE 1511
3123 void
3124 init_obarray ()
3126 Lisp_Object oblength;
3127 int hash;
3128 Lisp_Object *tem;
3130 XSETFASTINT (oblength, OBARRAY_SIZE);
3132 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
3133 Vobarray = Fmake_vector (oblength, make_number (0));
3134 initial_obarray = Vobarray;
3135 staticpro (&initial_obarray);
3136 /* Intern nil in the obarray */
3137 XSYMBOL (Qnil)->obarray = Vobarray;
3138 /* These locals are to kludge around a pyramid compiler bug. */
3139 hash = hash_string ("nil", 3);
3140 /* Separate statement here to avoid VAXC bug. */
3141 hash %= OBARRAY_SIZE;
3142 tem = &XVECTOR (Vobarray)->contents[hash];
3143 *tem = Qnil;
3145 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
3146 XSYMBOL (Qnil)->function = Qunbound;
3147 XSYMBOL (Qunbound)->value = Qunbound;
3148 XSYMBOL (Qunbound)->function = Qunbound;
3150 Qt = intern ("t");
3151 XSYMBOL (Qnil)->value = Qnil;
3152 XSYMBOL (Qnil)->plist = Qnil;
3153 XSYMBOL (Qt)->value = Qt;
3155 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
3156 Vpurify_flag = Qt;
3158 Qvariable_documentation = intern ("variable-documentation");
3159 staticpro (&Qvariable_documentation);
3161 read_buffer_size = 100 + MAX_MULTIBYTE_LENGTH;
3162 read_buffer = (char *) xmalloc (read_buffer_size);
3165 void
3166 defsubr (sname)
3167 struct Lisp_Subr *sname;
3169 Lisp_Object sym;
3170 sym = intern (sname->symbol_name);
3171 XSETSUBR (XSYMBOL (sym)->function, sname);
3174 #ifdef NOTDEF /* use fset in subr.el now */
3175 void
3176 defalias (sname, string)
3177 struct Lisp_Subr *sname;
3178 char *string;
3180 Lisp_Object sym;
3181 sym = intern (string);
3182 XSETSUBR (XSYMBOL (sym)->function, sname);
3184 #endif /* NOTDEF */
3186 /* Define an "integer variable"; a symbol whose value is forwarded
3187 to a C variable of type int. Sample call: */
3188 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3189 void
3190 defvar_int (namestring, address)
3191 char *namestring;
3192 int *address;
3194 Lisp_Object sym, val;
3195 sym = intern (namestring);
3196 val = allocate_misc ();
3197 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3198 XINTFWD (val)->intvar = address;
3199 XSYMBOL (sym)->value = val;
3202 /* Similar but define a variable whose value is T if address contains 1,
3203 NIL if address contains 0 */
3204 void
3205 defvar_bool (namestring, address)
3206 char *namestring;
3207 int *address;
3209 Lisp_Object sym, val;
3210 sym = intern (namestring);
3211 val = allocate_misc ();
3212 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3213 XBOOLFWD (val)->boolvar = address;
3214 XSYMBOL (sym)->value = val;
3215 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3218 /* Similar but define a variable whose value is the Lisp Object stored
3219 at address. Two versions: with and without gc-marking of the C
3220 variable. The nopro version is used when that variable will be
3221 gc-marked for some other reason, since marking the same slot twice
3222 can cause trouble with strings. */
3223 void
3224 defvar_lisp_nopro (namestring, address)
3225 char *namestring;
3226 Lisp_Object *address;
3228 Lisp_Object sym, val;
3229 sym = intern (namestring);
3230 val = allocate_misc ();
3231 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3232 XOBJFWD (val)->objvar = address;
3233 XSYMBOL (sym)->value = val;
3236 void
3237 defvar_lisp (namestring, address)
3238 char *namestring;
3239 Lisp_Object *address;
3241 defvar_lisp_nopro (namestring, address);
3242 staticpro (address);
3245 /* Similar but define a variable whose value is the Lisp Object stored in
3246 the current buffer. address is the address of the slot in the buffer
3247 that is current now. */
3249 void
3250 defvar_per_buffer (namestring, address, type, doc)
3251 char *namestring;
3252 Lisp_Object *address;
3253 Lisp_Object type;
3254 char *doc;
3256 Lisp_Object sym, val;
3257 int offset;
3258 extern struct buffer buffer_local_symbols;
3260 sym = intern (namestring);
3261 val = allocate_misc ();
3262 offset = (char *)address - (char *)current_buffer;
3264 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3265 XBUFFER_OBJFWD (val)->offset = offset;
3266 XSYMBOL (sym)->value = val;
3267 PER_BUFFER_SYMBOL (offset) = sym;
3268 PER_BUFFER_TYPE (offset) = type;
3270 if (PER_BUFFER_IDX (offset) == 0)
3271 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3272 slot of buffer_local_flags */
3273 abort ();
3277 /* Similar but define a variable whose value is the Lisp Object stored
3278 at a particular offset in the current kboard object. */
3280 void
3281 defvar_kboard (namestring, offset)
3282 char *namestring;
3283 int offset;
3285 Lisp_Object sym, val;
3286 sym = intern (namestring);
3287 val = allocate_misc ();
3288 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3289 XKBOARD_OBJFWD (val)->offset = offset;
3290 XSYMBOL (sym)->value = val;
3293 /* Record the value of load-path used at the start of dumping
3294 so we can see if the site changed it later during dumping. */
3295 static Lisp_Object dump_path;
3297 void
3298 init_lread ()
3300 char *normal;
3301 int turn_off_warning = 0;
3303 /* Compute the default load-path. */
3304 #ifdef CANNOT_DUMP
3305 normal = PATH_LOADSEARCH;
3306 Vload_path = decode_env_path (0, normal);
3307 #else
3308 if (NILP (Vpurify_flag))
3309 normal = PATH_LOADSEARCH;
3310 else
3311 normal = PATH_DUMPLOADSEARCH;
3313 /* In a dumped Emacs, we normally have to reset the value of
3314 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3315 uses ../lisp, instead of the path of the installed elisp
3316 libraries. However, if it appears that Vload_path was changed
3317 from the default before dumping, don't override that value. */
3318 if (initialized)
3320 if (! NILP (Fequal (dump_path, Vload_path)))
3322 Vload_path = decode_env_path (0, normal);
3323 if (!NILP (Vinstallation_directory))
3325 /* Add to the path the lisp subdir of the
3326 installation dir, if it exists. */
3327 Lisp_Object tem, tem1;
3328 tem = Fexpand_file_name (build_string ("lisp"),
3329 Vinstallation_directory);
3330 tem1 = Ffile_exists_p (tem);
3331 if (!NILP (tem1))
3333 if (NILP (Fmember (tem, Vload_path)))
3335 turn_off_warning = 1;
3336 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3339 else
3340 /* That dir doesn't exist, so add the build-time
3341 Lisp dirs instead. */
3342 Vload_path = nconc2 (Vload_path, dump_path);
3344 /* Add leim under the installation dir, if it exists. */
3345 tem = Fexpand_file_name (build_string ("leim"),
3346 Vinstallation_directory);
3347 tem1 = Ffile_exists_p (tem);
3348 if (!NILP (tem1))
3350 if (NILP (Fmember (tem, Vload_path)))
3351 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3354 /* Add site-list under the installation dir, if it exists. */
3355 tem = Fexpand_file_name (build_string ("site-lisp"),
3356 Vinstallation_directory);
3357 tem1 = Ffile_exists_p (tem);
3358 if (!NILP (tem1))
3360 if (NILP (Fmember (tem, Vload_path)))
3361 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3364 /* If Emacs was not built in the source directory,
3365 and it is run from where it was built, add to load-path
3366 the lisp, leim and site-lisp dirs under that directory. */
3368 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3370 Lisp_Object tem2;
3372 tem = Fexpand_file_name (build_string ("src/Makefile"),
3373 Vinstallation_directory);
3374 tem1 = Ffile_exists_p (tem);
3376 /* Don't be fooled if they moved the entire source tree
3377 AFTER dumping Emacs. If the build directory is indeed
3378 different from the source dir, src/Makefile.in and
3379 src/Makefile will not be found together. */
3380 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3381 Vinstallation_directory);
3382 tem2 = Ffile_exists_p (tem);
3383 if (!NILP (tem1) && NILP (tem2))
3385 tem = Fexpand_file_name (build_string ("lisp"),
3386 Vsource_directory);
3388 if (NILP (Fmember (tem, Vload_path)))
3389 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3391 tem = Fexpand_file_name (build_string ("leim"),
3392 Vsource_directory);
3394 if (NILP (Fmember (tem, Vload_path)))
3395 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3397 tem = Fexpand_file_name (build_string ("site-lisp"),
3398 Vsource_directory);
3400 if (NILP (Fmember (tem, Vload_path)))
3401 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3407 else
3409 /* NORMAL refers to the lisp dir in the source directory. */
3410 /* We used to add ../lisp at the front here, but
3411 that caused trouble because it was copied from dump_path
3412 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3413 It should be unnecessary. */
3414 Vload_path = decode_env_path (0, normal);
3415 dump_path = Vload_path;
3417 #endif
3419 #ifndef WINDOWSNT
3420 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3421 almost never correct, thereby causing a warning to be printed out that
3422 confuses users. Since PATH_LOADSEARCH is always overridden by the
3423 EMACSLOADPATH environment variable below, disable the warning on NT. */
3425 /* Warn if dirs in the *standard* path don't exist. */
3426 if (!turn_off_warning)
3428 Lisp_Object path_tail;
3430 for (path_tail = Vload_path;
3431 !NILP (path_tail);
3432 path_tail = XCDR (path_tail))
3434 Lisp_Object dirfile;
3435 dirfile = Fcar (path_tail);
3436 if (STRINGP (dirfile))
3438 dirfile = Fdirectory_file_name (dirfile);
3439 if (access (XSTRING (dirfile)->data, 0) < 0)
3440 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3441 XCAR (path_tail));
3445 #endif /* WINDOWSNT */
3447 /* If the EMACSLOADPATH environment variable is set, use its value.
3448 This doesn't apply if we're dumping. */
3449 #ifndef CANNOT_DUMP
3450 if (NILP (Vpurify_flag)
3451 && egetenv ("EMACSLOADPATH"))
3452 #endif
3453 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3455 Vvalues = Qnil;
3457 load_in_progress = 0;
3458 Vload_file_name = Qnil;
3460 load_descriptor_list = Qnil;
3462 Vstandard_input = Qt;
3463 Vloads_in_progress = Qnil;
3466 /* Print a warning, using format string FORMAT, that directory DIRNAME
3467 does not exist. Print it on stderr and put it in *Message*. */
3469 void
3470 dir_warning (format, dirname)
3471 char *format;
3472 Lisp_Object dirname;
3474 char *buffer
3475 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
3477 fprintf (stderr, format, XSTRING (dirname)->data);
3478 sprintf (buffer, format, XSTRING (dirname)->data);
3479 /* Don't log the warning before we've initialized!! */
3480 if (initialized)
3481 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3484 void
3485 syms_of_lread ()
3487 defsubr (&Sread);
3488 defsubr (&Sread_from_string);
3489 defsubr (&Sintern);
3490 defsubr (&Sintern_soft);
3491 defsubr (&Sunintern);
3492 defsubr (&Sload);
3493 defsubr (&Seval_buffer);
3494 defsubr (&Seval_region);
3495 defsubr (&Sread_char);
3496 defsubr (&Sread_char_exclusive);
3497 defsubr (&Sread_event);
3498 defsubr (&Sget_file_char);
3499 defsubr (&Smapatoms);
3501 DEFVAR_LISP ("obarray", &Vobarray,
3502 "Symbol table for use by `intern' and `read'.\n\
3503 It is a vector whose length ought to be prime for best results.\n\
3504 The vector's contents don't make sense if examined from Lisp programs;\n\
3505 to find all the symbols in an obarray, use `mapatoms'.");
3507 DEFVAR_LISP ("values", &Vvalues,
3508 "List of values of all expressions which were read, evaluated and printed.\n\
3509 Order is reverse chronological.");
3511 DEFVAR_LISP ("standard-input", &Vstandard_input,
3512 "Stream for read to get input from.\n\
3513 See documentation of `read' for possible values.");
3514 Vstandard_input = Qt;
3516 DEFVAR_LISP ("load-path", &Vload_path,
3517 "*List of directories to search for files to load.\n\
3518 Each element is a string (directory name) or nil (try default directory).\n\
3519 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3520 otherwise to default specified by file `epaths.h' when Emacs was built.");
3522 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3523 "Non-nil iff inside of `load'.");
3525 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3526 "An alist of expressions to be evalled when particular files are loaded.\n\
3527 Each element looks like (FILENAME FORMS...).\n\
3528 When `load' is run and the file-name argument is FILENAME,\n\
3529 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3530 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3531 with no directory specified, since that is how `load' is normally called.\n\
3532 An error in FORMS does not undo the load,\n\
3533 but does prevent execution of the rest of the FORMS.");
3534 Vafter_load_alist = Qnil;
3536 DEFVAR_LISP ("load-history", &Vload_history,
3537 "Alist mapping source file names to symbols and features.\n\
3538 Each alist element is a list that starts with a file name,\n\
3539 except for one element (optional) that starts with nil and describes\n\
3540 definitions evaluated from buffers not visiting files.\n\
3541 The remaining elements of each list are symbols defined as functions\n\
3542 or variables, and cons cells `(provide . FEATURE)', `(require . FEATURE)',\n\
3543 and `(autoload . SYMBOL)'.");
3544 Vload_history = Qnil;
3546 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3547 "Full name of file being loaded by `load'.");
3548 Vload_file_name = Qnil;
3550 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3551 "File name, including directory, of user's initialization file.\n\
3552 If the file loaded had extension `.elc' and there was a corresponding `.el'\n\
3553 file, this variable contains the name of the .el file, suitable for use\n\
3554 by functions like `custom-save-all' which edit the init file.");
3555 Vuser_init_file = Qnil;
3557 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
3558 "Used for internal purposes by `load'.");
3559 Vcurrent_load_list = Qnil;
3561 DEFVAR_LISP ("load-read-function", &Vload_read_function,
3562 "Function used by `load' and `eval-region' for reading expressions.\n\
3563 The default is nil, which means use the function `read'.");
3564 Vload_read_function = Qnil;
3566 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
3567 "Function called in `load' for loading an Emacs lisp source file.\n\
3568 This function is for doing code conversion before reading the source file.\n\
3569 If nil, loading is done without any code conversion.\n\
3570 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3571 FULLNAME is the full name of FILE.\n\
3572 See `load' for the meaning of the remaining arguments.");
3573 Vload_source_file_function = Qnil;
3575 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
3576 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3577 This is useful when the file being loaded is a temporary copy.");
3578 load_force_doc_strings = 0;
3580 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
3581 "Non-nil means `read' converts strings to unibyte whenever possible.\n\
3582 This is normally bound by `load' and `eval-buffer' to control `read',\n\
3583 and is not meant for users to change.");
3584 load_convert_to_unibyte = 0;
3586 DEFVAR_LISP ("source-directory", &Vsource_directory,
3587 "Directory in which Emacs sources were found when Emacs was built.\n\
3588 You cannot count on them to still be there!");
3589 Vsource_directory
3590 = Fexpand_file_name (build_string ("../"),
3591 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3593 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
3594 "List of files that were preloaded (when dumping Emacs).");
3595 Vpreloaded_file_list = Qnil;
3597 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
3598 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3599 Vbyte_boolean_vars = Qnil;
3601 DEFVAR_BOOL ("load-dangerous-libraries", &load_dangerous_libraries,
3602 "Non-nil means load dangerous compiled Lisp files.\n\
3603 Some versions of XEmacs use different byte codes than Emacs. These\n\
3604 incompatible byte codes can make Emacs crash when it tries to execute\n\
3605 them.");
3606 load_dangerous_libraries = 0;
3608 DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
3609 "Regular expression matching safe to load compiled Lisp files.\n\
3610 When Emacs loads a compiled Lisp file, it reads the first 512 bytes\n\
3611 from the file, and matches them against this regular expression.\n\
3612 When the regular expression matches, the file is considered to be safe\n\
3613 to load. See also `load-dangerous-libraries'.");
3614 Vbytecomp_version_regexp
3615 = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
3617 DEFVAR_LISP ("recursive-load-depth-limit", &Vrecursive_load_depth_limit,
3618 "Limit for depth of recursive loads.\n\
3619 Value should be either an integer > 0 specifying the limit, or nil for\n\
3620 no limit.");
3621 Vrecursive_load_depth_limit = make_number (10);
3623 /* Vsource_directory was initialized in init_lread. */
3625 load_descriptor_list = Qnil;
3626 staticpro (&load_descriptor_list);
3628 Qcurrent_load_list = intern ("current-load-list");
3629 staticpro (&Qcurrent_load_list);
3631 Qstandard_input = intern ("standard-input");
3632 staticpro (&Qstandard_input);
3634 Qread_char = intern ("read-char");
3635 staticpro (&Qread_char);
3637 Qget_file_char = intern ("get-file-char");
3638 staticpro (&Qget_file_char);
3640 Qbackquote = intern ("`");
3641 staticpro (&Qbackquote);
3642 Qcomma = intern (",");
3643 staticpro (&Qcomma);
3644 Qcomma_at = intern (",@");
3645 staticpro (&Qcomma_at);
3646 Qcomma_dot = intern (",.");
3647 staticpro (&Qcomma_dot);
3649 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
3650 staticpro (&Qinhibit_file_name_operation);
3652 Qascii_character = intern ("ascii-character");
3653 staticpro (&Qascii_character);
3655 Qfunction = intern ("function");
3656 staticpro (&Qfunction);
3658 Qload = intern ("load");
3659 staticpro (&Qload);
3661 Qload_file_name = intern ("load-file-name");
3662 staticpro (&Qload_file_name);
3664 staticpro (&dump_path);
3666 staticpro (&read_objects);
3667 read_objects = Qnil;
3668 staticpro (&seen_list);
3670 Vloads_in_progress = Qnil;
3671 staticpro (&Vloads_in_progress);