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