various fixes and gratuitous movements.
[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 if (c == '(')
1697 {
1698 Lisp_Object tmp;
1699 struct gcpro gcpro1;
1700 int ch;
1701
1702 /* Read the string itself. */
1703 tmp = read1 (readcharfun, &ch, 0);
1704 if (ch != 0 || !STRINGP (tmp))
1705 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1706 GCPRO1 (tmp);
1707 /* Read the intervals and their properties. */
1708 while (1)
1709 {
1710 Lisp_Object beg, end, plist;
1711
1712 beg = read1 (readcharfun, &ch, 0);
1713 if (ch == ')')
1714 break;
1715 if (ch == 0)
1716 end = read1 (readcharfun, &ch, 0);
1717 if (ch == 0)
1718 plist = read1 (readcharfun, &ch, 0);
1719 if (ch)
1720 Fsignal (Qinvalid_read_syntax,
1721 Fcons (build_string ("invalid string property list"),
1722 Qnil));
1723 Fset_text_properties (beg, end, plist, tmp);
1724 }
1725 UNGCPRO;
1726 return tmp;
1727 }
1728
1729 /* #@NUMBER is used to skip NUMBER following characters.
1730 That's used in .elc files to skip over doc strings
1731 and function definitions. */
1732 if (c == '@')
1733 {
1734 int i, nskip = 0;
1735
1736 /* Read a decimal integer. */
1737 while ((c = READCHAR) >= 0
1738 && c >= '0' && c <= '9')
1739 {
1740 nskip *= 10;
1741 nskip += c - '0';
1742 }
1743 if (c >= 0)
1744 UNREAD (c);
1745
1746 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1747 {
1748 /* If we are supposed to force doc strings into core right now,
1749 record the last string that we skipped,
1750 and record where in the file it comes from. */
1751
1752 /* But first exchange saved_doc_string
1753 with prev_saved_doc_string, so we save two strings. */
1754 {
1755 char *temp = saved_doc_string;
1756 int temp_size = saved_doc_string_size;
1757 file_offset temp_pos = saved_doc_string_position;
1758 int temp_len = saved_doc_string_length;
1759
1760 saved_doc_string = prev_saved_doc_string;
1761 saved_doc_string_size = prev_saved_doc_string_size;
1762 saved_doc_string_position = prev_saved_doc_string_position;
1763 saved_doc_string_length = prev_saved_doc_string_length;
1764
1765 prev_saved_doc_string = temp;
1766 prev_saved_doc_string_size = temp_size;
1767 prev_saved_doc_string_position = temp_pos;
1768 prev_saved_doc_string_length = temp_len;
1769 }
1770
1771 if (saved_doc_string_size == 0)
1772 {
1773 saved_doc_string_size = nskip + 100;
1774 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1775 }
1776 if (nskip > saved_doc_string_size)
1777 {
1778 saved_doc_string_size = nskip + 100;
1779 saved_doc_string = (char *) xrealloc (saved_doc_string,
1780 saved_doc_string_size);
1781 }
1782
1783 saved_doc_string_position = file_tell (instream);
1784
1785 /* Copy that many characters into saved_doc_string. */
1786 for (i = 0; i < nskip && c >= 0; i++)
1787 saved_doc_string[i] = c = READCHAR;
1788
1789 saved_doc_string_length = i;
1790 }
1791 else
1792 {
1793 /* Skip that many characters. */
1794 for (i = 0; i < nskip && c >= 0; i++)
1795 c = READCHAR;
1796 }
1797
1798 goto retry;
1799 }
1800 if (c == '$')
1801 return Vload_file_name;
1802 if (c == '\'')
1803 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1804 /* #:foo is the uninterned symbol named foo. */
1805 if (c == ':')
1806 {
1807 uninterned_symbol = 1;
1808 c = READCHAR;
1809 goto default_label;
1810 }
1811 /* Reader forms that can reuse previously read objects. */
1812 if (c >= '0' && c <= '9')
1813 {
1814 int n = 0;
1815 Lisp_Object tem;
1816
1817 /* Read a non-negative integer. */
1818 while (c >= '0' && c <= '9')
1819 {
1820 n *= 10;
1821 n += c - '0';
1822 c = READCHAR;
1823 }
1824 /* #n=object returns object, but associates it with n for #n#. */
1825 if (c == '=')
1826 {
1827 /* Make a placeholder for #n# to use temporarily */
1828 Lisp_Object placeholder;
1829 Lisp_Object cell;
1830
1831 placeholder = Fcons(Qnil, Qnil);
1832 cell = Fcons (make_number (n), placeholder);
1833 read_objects = Fcons (cell, read_objects);
1834
1835 /* Read the object itself. */
1836 tem = read0 (readcharfun);
1837
1838 /* Now put it everywhere the placeholder was... */
1839 substitute_object_in_subtree (tem, placeholder);
1840
1841 /* ...and #n# will use the real value from now on. */
1842 Fsetcdr (cell, tem);
1843
1844 return tem;
1845 }
1846 /* #n# returns a previously read object. */
1847 if (c == '#')
1848 {
1849 tem = Fassq (make_number (n), read_objects);
1850 if (CONSP (tem))
1851 return XCDR (tem);
1852 /* Fall through to error message. */
1853 }
1854 /* Fall through to error message. */
1855 }
1856
1857 UNREAD (c);
1858 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1859
1860 case ';':
1861 while ((c = READCHAR) >= 0 && c != '\n');
1862 goto retry;
1863
1864 case '\'':
1865 {
1866 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1867 }
1868
1869 case '`':
1870 if (first_in_list)
1871 goto default_label;
1872 else
1873 {
1874 Lisp_Object value;
1875
1876 new_backquote_flag = 1;
1877 value = read0 (readcharfun);
1878 new_backquote_flag = 0;
1879
1880 return Fcons (Qbackquote, Fcons (value, Qnil));
1881 }
1882
1883 case ',':
1884 if (new_backquote_flag)
1885 {
1886 Lisp_Object comma_type = Qnil;
1887 Lisp_Object value;
1888 int ch = READCHAR;
1889
1890 if (ch == '@')
1891 comma_type = Qcomma_at;
1892 else if (ch == '.')
1893 comma_type = Qcomma_dot;
1894 else
1895 {
1896 if (ch >= 0) UNREAD (ch);
1897 comma_type = Qcomma;
1898 }
1899
1900 new_backquote_flag = 0;
1901 value = read0 (readcharfun);
1902 new_backquote_flag = 1;
1903 return Fcons (comma_type, Fcons (value, Qnil));
1904 }
1905 else
1906 goto default_label;
1907
1908 case '?':
1909 {
1910 c = READCHAR;
1911 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1912
1913 if (c == '\\')
1914 c = read_escape (readcharfun, 0);
1915 else if (BASE_LEADING_CODE_P (c))
1916 c = read_multibyte (c, readcharfun);
1917
1918 return make_number (c);
1919 }
1920
1921 case '"':
1922 {
1923 register char *p = read_buffer;
1924 register char *end = read_buffer + read_buffer_size;
1925 register int c;
1926 /* Nonzero if we saw an escape sequence specifying
1927 a multibyte character. */
1928 int force_multibyte = 0;
1929 /* Nonzero if we saw an escape sequence specifying
1930 a single-byte character. */
1931 int force_singlebyte = 0;
1932 int cancel = 0;
1933 int nchars;
1934
1935 while ((c = READCHAR) >= 0
1936 && c != '\"')
1937 {
1938 if (end - p < MAX_LENGTH_OF_MULTI_BYTE_FORM)
1939 {
1940 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1941 p += new - read_buffer;
1942 read_buffer += new - read_buffer;
1943 end = read_buffer + read_buffer_size;
1944 }
1945
1946 if (c == '\\')
1947 {
1948 c = read_escape (readcharfun, 1);
1949
1950 /* C is -1 if \ newline has just been seen */
1951 if (c == -1)
1952 {
1953 if (p == read_buffer)
1954 cancel = 1;
1955 continue;
1956 }
1957
1958 /* If an escape specifies a non-ASCII single-byte character,
1959 this must be a unibyte string. */
1960 if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))
1961 && ! ASCII_BYTE_P ((c & ~CHAR_MODIFIER_MASK)))
1962 force_singlebyte = 1;
1963 }
1964
1965 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1966 {
1967 unsigned char workbuf[4];
1968 unsigned char *str = workbuf;
1969 int length;
1970
1971 /* Any modifiers for a multibyte character are invalid. */
1972 if (c & CHAR_MODIFIER_MASK)
1973 error ("Invalid modifier in string");
1974 length = non_ascii_char_to_string (c, workbuf, &str);
1975 if (length > 1)
1976 force_multibyte = 1;
1977
1978 bcopy (str, p, length);
1979 p += length;
1980 }
1981 else
1982 {
1983 /* Allow `\C- ' and `\C-?'. */
1984 if (c == (CHAR_CTL | ' '))
1985 c = 0;
1986 else if (c == (CHAR_CTL | '?'))
1987 c = 127;
1988
1989 if (c & CHAR_SHIFT)
1990 {
1991 /* Shift modifier is valid only with [A-Za-z]. */
1992 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
1993 c &= ~CHAR_SHIFT;
1994 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
1995 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
1996 }
1997
1998 if (c & CHAR_META)
1999 /* Move the meta bit to the right place for a string. */
2000 c = (c & ~CHAR_META) | 0x80;
2001 if (c & ~0xff)
2002 error ("Invalid modifier in string");
2003 *p++ = c;
2004 }
2005 }
2006 if (c < 0)
2007 return Fsignal (Qend_of_file, Qnil);
2008
2009 /* If purifying, and string starts with \ newline,
2010 return zero instead. This is for doc strings
2011 that we are really going to find in etc/DOC.nn.nn */
2012 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2013 return make_number (0);
2014
2015 if (force_multibyte)
2016 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
2017 else if (force_singlebyte)
2018 nchars = p - read_buffer;
2019 else if (load_convert_to_unibyte)
2020 {
2021 Lisp_Object string;
2022 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
2023 if (p - read_buffer != nchars)
2024 {
2025 string = make_multibyte_string (read_buffer, nchars,
2026 p - read_buffer);
2027 return Fstring_make_unibyte (string);
2028 }
2029 }
2030 else if (EQ (readcharfun, Qget_file_char)
2031 || EQ (readcharfun, Qlambda))
2032 /* Nowadays, reading directly from a file
2033 is used only for compiled Emacs Lisp files,
2034 and those always use the Emacs internal encoding.
2035 Meanwhile, Qlambda is used for reading dynamic byte code
2036 (compiled with byte-compile-dynamic = t). */
2037 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
2038 else
2039 /* In all other cases, if we read these bytes as
2040 separate characters, treat them as separate characters now. */
2041 nchars = p - read_buffer;
2042
2043 if (read_pure)
2044 return make_pure_string (read_buffer, nchars, p - read_buffer,
2045 (force_multibyte
2046 || (p - read_buffer != nchars)));
2047 return make_specified_string (read_buffer, nchars, p - read_buffer,
2048 (force_multibyte
2049 || (p - read_buffer != nchars)));
2050 }
2051
2052 case '.':
2053 {
2054 #ifdef LISP_FLOAT_TYPE
2055 /* If a period is followed by a number, then we should read it
2056 as a floating point number. Otherwise, it denotes a dotted
2057 pair. */
2058 int next_char = READCHAR;
2059 UNREAD (next_char);
2060
2061 if (! (next_char >= '0' && next_char <= '9'))
2062 #endif
2063 {
2064 *pch = c;
2065 return Qnil;
2066 }
2067
2068 /* Otherwise, we fall through! Note that the atom-reading loop
2069 below will now loop at least once, assuring that we will not
2070 try to UNREAD two characters in a row. */
2071 }
2072 default:
2073 default_label:
2074 if (c <= 040) goto retry;
2075 {
2076 register char *p = read_buffer;
2077 int quoted = 0;
2078
2079 {
2080 register char *end = read_buffer + read_buffer_size;
2081
2082 while (c > 040
2083 && !(c == '\"' || c == '\'' || c == ';' || c == '?'
2084 || c == '(' || c == ')'
2085 #ifndef LISP_FLOAT_TYPE
2086 /* If we have floating-point support, then we need
2087 to allow <digits><dot><digits>. */
2088 || c =='.'
2089 #endif /* not LISP_FLOAT_TYPE */
2090 || c == '[' || c == ']' || c == '#'
2091 ))
2092 {
2093 if (end - p < MAX_LENGTH_OF_MULTI_BYTE_FORM)
2094 {
2095 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2096 p += new - read_buffer;
2097 read_buffer += new - read_buffer;
2098 end = read_buffer + read_buffer_size;
2099 }
2100 if (c == '\\')
2101 {
2102 c = READCHAR;
2103 quoted = 1;
2104 }
2105
2106 if (! SINGLE_BYTE_CHAR_P (c))
2107 {
2108 unsigned char workbuf[4];
2109 unsigned char *str = workbuf;
2110 int length;
2111
2112 length = non_ascii_char_to_string (c, workbuf, &str);
2113
2114 bcopy (str, p, length);
2115 p += length;
2116 }
2117 else
2118 *p++ = c;
2119
2120 c = READCHAR;
2121 }
2122
2123 if (p == end)
2124 {
2125 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2126 p += new - read_buffer;
2127 read_buffer += new - read_buffer;
2128 /* end = read_buffer + read_buffer_size; */
2129 }
2130 *p = 0;
2131 if (c >= 0)
2132 UNREAD (c);
2133 }
2134
2135 if (!quoted && !uninterned_symbol)
2136 {
2137 register char *p1;
2138 register Lisp_Object val;
2139 p1 = read_buffer;
2140 if (*p1 == '+' || *p1 == '-') p1++;
2141 /* Is it an integer? */
2142 if (p1 != p)
2143 {
2144 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2145 #ifdef LISP_FLOAT_TYPE
2146 /* Integers can have trailing decimal points. */
2147 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2148 #endif
2149 if (p1 == p)
2150 /* It is an integer. */
2151 {
2152 #ifdef LISP_FLOAT_TYPE
2153 if (p1[-1] == '.')
2154 p1[-1] = '\0';
2155 #endif
2156 if (sizeof (int) == sizeof (EMACS_INT))
2157 XSETINT (val, atoi (read_buffer));
2158 else if (sizeof (long) == sizeof (EMACS_INT))
2159 XSETINT (val, atol (read_buffer));
2160 else
2161 abort ();
2162 return val;
2163 }
2164 }
2165 #ifdef LISP_FLOAT_TYPE
2166 if (isfloat_string (read_buffer))
2167 {
2168 /* Compute NaN and infinities using 0.0 in a variable,
2169 to cope with compilers that think they are smarter
2170 than we are. */
2171 double zero = 0.0;
2172
2173 double value;
2174
2175 /* Negate the value ourselves. This treats 0, NaNs,
2176 and infinity properly on IEEE floating point hosts,
2177 and works around a common bug where atof ("-0.0")
2178 drops the sign. */
2179 int negative = read_buffer[0] == '-';
2180
2181 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2182 returns 1, is if the input ends in e+INF or e+NaN. */
2183 switch (p[-1])
2184 {
2185 case 'F':
2186 value = 1.0 / zero;
2187 break;
2188 case 'N':
2189 value = zero / zero;
2190 break;
2191 default:
2192 value = atof (read_buffer + negative);
2193 break;
2194 }
2195
2196 return make_float (negative ? - value : value);
2197 }
2198 #endif
2199 }
2200
2201 if (uninterned_symbol)
2202 return make_symbol (read_buffer);
2203 else
2204 return intern (read_buffer);
2205 }
2206 }
2207 }
2208 \f
2209
2210 /* List of nodes we've seen during substitute_object_in_subtree. */
2211 static Lisp_Object seen_list;
2212
2213 static void
2214 substitute_object_in_subtree (object, placeholder)
2215 Lisp_Object object;
2216 Lisp_Object placeholder;
2217 {
2218 Lisp_Object check_object;
2219
2220 /* We haven't seen any objects when we start. */
2221 seen_list = Qnil;
2222
2223 /* Make all the substitutions. */
2224 check_object
2225 = substitute_object_recurse (object, placeholder, object);
2226
2227 /* Clear seen_list because we're done with it. */
2228 seen_list = Qnil;
2229
2230 /* The returned object here is expected to always eq the
2231 original. */
2232 if (!EQ (check_object, object))
2233 error ("Unexpected mutation error in reader");
2234 }
2235
2236 /* Feval doesn't get called from here, so no gc protection is needed. */
2237 #define SUBSTITUTE(get_val, set_val) \
2238 { \
2239 Lisp_Object old_value = get_val; \
2240 Lisp_Object true_value \
2241 = substitute_object_recurse (object, placeholder,\
2242 old_value); \
2243 \
2244 if (!EQ (old_value, true_value)) \
2245 { \
2246 set_val; \
2247 } \
2248 }
2249
2250 static Lisp_Object
2251 substitute_object_recurse (object, placeholder, subtree)
2252 Lisp_Object object;
2253 Lisp_Object placeholder;
2254 Lisp_Object subtree;
2255 {
2256 /* If we find the placeholder, return the target object. */
2257 if (EQ (placeholder, subtree))
2258 return object;
2259
2260 /* If we've been to this node before, don't explore it again. */
2261 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2262 return subtree;
2263
2264 /* If this node can be the entry point to a cycle, remember that
2265 we've seen it. It can only be such an entry point if it was made
2266 by #n=, which means that we can find it as a value in
2267 read_objects. */
2268 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2269 seen_list = Fcons (subtree, seen_list);
2270
2271 /* Recurse according to subtree's type.
2272 Every branch must return a Lisp_Object. */
2273 switch (XTYPE (subtree))
2274 {
2275 case Lisp_Vectorlike:
2276 {
2277 int i;
2278 int length = Flength(subtree);
2279 for (i = 0; i < length; i++)
2280 {
2281 Lisp_Object idx = make_number (i);
2282 SUBSTITUTE (Faref (subtree, idx),
2283 Faset (subtree, idx, true_value));
2284 }
2285 return subtree;
2286 }
2287
2288 case Lisp_Cons:
2289 {
2290 SUBSTITUTE (Fcar_safe (subtree),
2291 Fsetcar (subtree, true_value));
2292 SUBSTITUTE (Fcdr_safe (subtree),
2293 Fsetcdr (subtree, true_value));
2294 return subtree;
2295 }
2296
2297 case Lisp_String:
2298 {
2299 /* Check for text properties in each interval.
2300 substitute_in_interval contains part of the logic. */
2301
2302 INTERVAL root_interval = XSTRING (subtree)->intervals;
2303 Lisp_Object arg = Fcons (object, placeholder);
2304
2305 traverse_intervals (root_interval, 1, 0,
2306 &substitute_in_interval, arg);
2307
2308 return subtree;
2309 }
2310
2311 /* Other types don't recurse any further. */
2312 default:
2313 return subtree;
2314 }
2315 }
2316
2317 /* Helper function for substitute_object_recurse. */
2318 static void
2319 substitute_in_interval (interval, arg)
2320 INTERVAL interval;
2321 Lisp_Object arg;
2322 {
2323 Lisp_Object object = Fcar (arg);
2324 Lisp_Object placeholder = Fcdr (arg);
2325
2326 SUBSTITUTE(interval->plist, interval->plist = true_value);
2327 }
2328
2329 \f
2330 #ifdef LISP_FLOAT_TYPE
2331
2332 #define LEAD_INT 1
2333 #define DOT_CHAR 2
2334 #define TRAIL_INT 4
2335 #define E_CHAR 8
2336 #define EXP_INT 16
2337
2338 int
2339 isfloat_string (cp)
2340 register char *cp;
2341 {
2342 register int state;
2343
2344 char *start = cp;
2345
2346 state = 0;
2347 if (*cp == '+' || *cp == '-')
2348 cp++;
2349
2350 if (*cp >= '0' && *cp <= '9')
2351 {
2352 state |= LEAD_INT;
2353 while (*cp >= '0' && *cp <= '9')
2354 cp++;
2355 }
2356 if (*cp == '.')
2357 {
2358 state |= DOT_CHAR;
2359 cp++;
2360 }
2361 if (*cp >= '0' && *cp <= '9')
2362 {
2363 state |= TRAIL_INT;
2364 while (*cp >= '0' && *cp <= '9')
2365 cp++;
2366 }
2367 if (*cp == 'e' || *cp == 'E')
2368 {
2369 state |= E_CHAR;
2370 cp++;
2371 if (*cp == '+' || *cp == '-')
2372 cp++;
2373 }
2374
2375 if (*cp >= '0' && *cp <= '9')
2376 {
2377 state |= EXP_INT;
2378 while (*cp >= '0' && *cp <= '9')
2379 cp++;
2380 }
2381 else if (cp == start)
2382 ;
2383 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2384 {
2385 state |= EXP_INT;
2386 cp += 3;
2387 }
2388 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2389 {
2390 state |= EXP_INT;
2391 cp += 3;
2392 }
2393
2394 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2395 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2396 || state == (DOT_CHAR|TRAIL_INT)
2397 || state == (LEAD_INT|E_CHAR|EXP_INT)
2398 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2399 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2400 }
2401 #endif /* LISP_FLOAT_TYPE */
2402 \f
2403 static Lisp_Object
2404 read_vector (readcharfun, bytecodeflag)
2405 Lisp_Object readcharfun;
2406 int bytecodeflag;
2407 {
2408 register int i;
2409 register int size;
2410 register Lisp_Object *ptr;
2411 register Lisp_Object tem, item, vector;
2412 register struct Lisp_Cons *otem;
2413 Lisp_Object len;
2414
2415 tem = read_list (1, readcharfun);
2416 len = Flength (tem);
2417 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2418
2419 size = XVECTOR (vector)->size;
2420 ptr = XVECTOR (vector)->contents;
2421 for (i = 0; i < size; i++)
2422 {
2423 item = Fcar (tem);
2424 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2425 bytecode object, the docstring containing the bytecode and
2426 constants values must be treated as unibyte and passed to
2427 Fread, to get the actual bytecode string and constants vector. */
2428 if (bytecodeflag && load_force_doc_strings)
2429 {
2430 if (i == COMPILED_BYTECODE)
2431 {
2432 if (!STRINGP (item))
2433 error ("invalid byte code");
2434
2435 /* Delay handling the bytecode slot until we know whether
2436 it is lazily-loaded (we can tell by whether the
2437 constants slot is nil). */
2438 ptr[COMPILED_CONSTANTS] = item;
2439 item = Qnil;
2440 }
2441 else if (i == COMPILED_CONSTANTS)
2442 {
2443 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2444
2445 if (NILP (item))
2446 {
2447 /* Coerce string to unibyte (like string-as-unibyte,
2448 but without generating extra garbage and
2449 guaranteeing no change in the contents). */
2450 XSTRING (bytestr)->size = STRING_BYTES (XSTRING (bytestr));
2451 SET_STRING_BYTES (XSTRING (bytestr), -1);
2452
2453 item = Fread (bytestr);
2454 if (!CONSP (item))
2455 error ("invalid byte code");
2456
2457 otem = XCONS (item);
2458 bytestr = XCAR (item);
2459 item = XCDR (item);
2460 free_cons (otem);
2461 }
2462
2463 /* Now handle the bytecode slot. */
2464 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2465 }
2466 }
2467 ptr[i] = read_pure ? Fpurecopy (item) : item;
2468 otem = XCONS (tem);
2469 tem = Fcdr (tem);
2470 free_cons (otem);
2471 }
2472 return vector;
2473 }
2474
2475 /* FLAG = 1 means check for ] to terminate rather than ) and .
2476 FLAG = -1 means check for starting with defun
2477 and make structure pure. */
2478
2479 static Lisp_Object
2480 read_list (flag, readcharfun)
2481 int flag;
2482 register Lisp_Object readcharfun;
2483 {
2484 /* -1 means check next element for defun,
2485 0 means don't check,
2486 1 means already checked and found defun. */
2487 int defunflag = flag < 0 ? -1 : 0;
2488 Lisp_Object val, tail;
2489 register Lisp_Object elt, tem;
2490 struct gcpro gcpro1, gcpro2;
2491 /* 0 is the normal case.
2492 1 means this list is a doc reference; replace it with the number 0.
2493 2 means this list is a doc reference; replace it with the doc string. */
2494 int doc_reference = 0;
2495
2496 /* Initialize this to 1 if we are reading a list. */
2497 int first_in_list = flag <= 0;
2498
2499 val = Qnil;
2500 tail = Qnil;
2501
2502 while (1)
2503 {
2504 int ch;
2505 GCPRO2 (val, tail);
2506 elt = read1 (readcharfun, &ch, first_in_list);
2507 UNGCPRO;
2508
2509 first_in_list = 0;
2510
2511 /* While building, if the list starts with #$, treat it specially. */
2512 if (EQ (elt, Vload_file_name)
2513 && ! NILP (elt)
2514 && !NILP (Vpurify_flag))
2515 {
2516 if (NILP (Vdoc_file_name))
2517 /* We have not yet called Snarf-documentation, so assume
2518 this file is described in the DOC-MM.NN file
2519 and Snarf-documentation will fill in the right value later.
2520 For now, replace the whole list with 0. */
2521 doc_reference = 1;
2522 else
2523 /* We have already called Snarf-documentation, so make a relative
2524 file name for this file, so it can be found properly
2525 in the installed Lisp directory.
2526 We don't use Fexpand_file_name because that would make
2527 the directory absolute now. */
2528 elt = concat2 (build_string ("../lisp/"),
2529 Ffile_name_nondirectory (elt));
2530 }
2531 else if (EQ (elt, Vload_file_name)
2532 && ! NILP (elt)
2533 && load_force_doc_strings)
2534 doc_reference = 2;
2535
2536 if (ch)
2537 {
2538 if (flag > 0)
2539 {
2540 if (ch == ']')
2541 return val;
2542 Fsignal (Qinvalid_read_syntax,
2543 Fcons (make_string (") or . in a vector", 18), Qnil));
2544 }
2545 if (ch == ')')
2546 return val;
2547 if (ch == '.')
2548 {
2549 GCPRO2 (val, tail);
2550 if (!NILP (tail))
2551 XCDR (tail) = read0 (readcharfun);
2552 else
2553 val = read0 (readcharfun);
2554 read1 (readcharfun, &ch, 0);
2555 UNGCPRO;
2556 if (ch == ')')
2557 {
2558 if (doc_reference == 1)
2559 return make_number (0);
2560 if (doc_reference == 2)
2561 {
2562 /* Get a doc string from the file we are loading.
2563 If it's in saved_doc_string, get it from there. */
2564 int pos = XINT (XCDR (val));
2565 /* Position is negative for user variables. */
2566 if (pos < 0) pos = -pos;
2567 if (pos >= saved_doc_string_position
2568 && pos < (saved_doc_string_position
2569 + saved_doc_string_length))
2570 {
2571 int start = pos - saved_doc_string_position;
2572 int from, to;
2573
2574 /* Process quoting with ^A,
2575 and find the end of the string,
2576 which is marked with ^_ (037). */
2577 for (from = start, to = start;
2578 saved_doc_string[from] != 037;)
2579 {
2580 int c = saved_doc_string[from++];
2581 if (c == 1)
2582 {
2583 c = saved_doc_string[from++];
2584 if (c == 1)
2585 saved_doc_string[to++] = c;
2586 else if (c == '0')
2587 saved_doc_string[to++] = 0;
2588 else if (c == '_')
2589 saved_doc_string[to++] = 037;
2590 }
2591 else
2592 saved_doc_string[to++] = c;
2593 }
2594
2595 return make_string (saved_doc_string + start,
2596 to - start);
2597 }
2598 /* Look in prev_saved_doc_string the same way. */
2599 else if (pos >= prev_saved_doc_string_position
2600 && pos < (prev_saved_doc_string_position
2601 + prev_saved_doc_string_length))
2602 {
2603 int start = pos - prev_saved_doc_string_position;
2604 int from, to;
2605
2606 /* Process quoting with ^A,
2607 and find the end of the string,
2608 which is marked with ^_ (037). */
2609 for (from = start, to = start;
2610 prev_saved_doc_string[from] != 037;)
2611 {
2612 int c = prev_saved_doc_string[from++];
2613 if (c == 1)
2614 {
2615 c = prev_saved_doc_string[from++];
2616 if (c == 1)
2617 prev_saved_doc_string[to++] = c;
2618 else if (c == '0')
2619 prev_saved_doc_string[to++] = 0;
2620 else if (c == '_')
2621 prev_saved_doc_string[to++] = 037;
2622 }
2623 else
2624 prev_saved_doc_string[to++] = c;
2625 }
2626
2627 return make_string (prev_saved_doc_string + start,
2628 to - start);
2629 }
2630 else
2631 return get_doc_string (val, 0, 0);
2632 }
2633
2634 return val;
2635 }
2636 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2637 }
2638 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2639 }
2640 tem = (read_pure && flag <= 0
2641 ? pure_cons (elt, Qnil)
2642 : Fcons (elt, Qnil));
2643 if (!NILP (tail))
2644 XCDR (tail) = tem;
2645 else
2646 val = tem;
2647 tail = tem;
2648 if (defunflag < 0)
2649 defunflag = EQ (elt, Qdefun);
2650 else if (defunflag > 0)
2651 read_pure = 1;
2652 }
2653 }
2654 \f
2655 Lisp_Object Vobarray;
2656 Lisp_Object initial_obarray;
2657
2658 /* oblookup stores the bucket number here, for the sake of Funintern. */
2659
2660 int oblookup_last_bucket_number;
2661
2662 static int hash_string ();
2663 Lisp_Object oblookup ();
2664
2665 /* Get an error if OBARRAY is not an obarray.
2666 If it is one, return it. */
2667
2668 Lisp_Object
2669 check_obarray (obarray)
2670 Lisp_Object obarray;
2671 {
2672 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2673 {
2674 /* If Vobarray is now invalid, force it to be valid. */
2675 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2676
2677 obarray = wrong_type_argument (Qvectorp, obarray);
2678 }
2679 return obarray;
2680 }
2681
2682 /* Intern the C string STR: return a symbol with that name,
2683 interned in the current obarray. */
2684
2685 Lisp_Object
2686 intern (str)
2687 char *str;
2688 {
2689 Lisp_Object tem;
2690 int len = strlen (str);
2691 Lisp_Object obarray;
2692
2693 obarray = Vobarray;
2694 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2695 obarray = check_obarray (obarray);
2696 tem = oblookup (obarray, str, len, len);
2697 if (SYMBOLP (tem))
2698 return tem;
2699 return Fintern (make_string (str, len), obarray);
2700 }
2701
2702 /* Create an uninterned symbol with name STR. */
2703
2704 Lisp_Object
2705 make_symbol (str)
2706 char *str;
2707 {
2708 int len = strlen (str);
2709
2710 return Fmake_symbol ((!NILP (Vpurify_flag)
2711 ? make_pure_string (str, len, len, 0)
2712 : make_string (str, len)));
2713 }
2714 \f
2715 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2716 "Return the canonical symbol whose name is STRING.\n\
2717 If there is none, one is created by this function and returned.\n\
2718 A second optional argument specifies the obarray to use;\n\
2719 it defaults to the value of `obarray'.")
2720 (string, obarray)
2721 Lisp_Object string, obarray;
2722 {
2723 register Lisp_Object tem, sym, *ptr;
2724
2725 if (NILP (obarray)) obarray = Vobarray;
2726 obarray = check_obarray (obarray);
2727
2728 CHECK_STRING (string, 0);
2729
2730 tem = oblookup (obarray, XSTRING (string)->data,
2731 XSTRING (string)->size,
2732 STRING_BYTES (XSTRING (string)));
2733 if (!INTEGERP (tem))
2734 return tem;
2735
2736 if (!NILP (Vpurify_flag))
2737 string = Fpurecopy (string);
2738 sym = Fmake_symbol (string);
2739 XSYMBOL (sym)->obarray = obarray;
2740
2741 if ((XSTRING (string)->data[0] == ':')
2742 && EQ (obarray, initial_obarray))
2743 XSYMBOL (sym)->value = sym;
2744
2745 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2746 if (SYMBOLP (*ptr))
2747 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2748 else
2749 XSYMBOL (sym)->next = 0;
2750 *ptr = sym;
2751 return sym;
2752 }
2753
2754 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2755 "Return the canonical symbol named NAME, or nil if none exists.\n\
2756 NAME may be a string or a symbol. If it is a symbol, that exact\n\
2757 symbol is searched for.\n\
2758 A second optional argument specifies the obarray to use;\n\
2759 it defaults to the value of `obarray'.")
2760 (name, obarray)
2761 Lisp_Object name, obarray;
2762 {
2763 register Lisp_Object tem;
2764 struct Lisp_String *string;
2765
2766 if (NILP (obarray)) obarray = Vobarray;
2767 obarray = check_obarray (obarray);
2768
2769 if (!SYMBOLP (name))
2770 {
2771 CHECK_STRING (name, 0);
2772 string = XSTRING (name);
2773 }
2774 else
2775 string = XSYMBOL (name)->name;
2776
2777 tem = oblookup (obarray, string->data, string->size, STRING_BYTES (string));
2778 if (INTEGERP (tem) || (SYMBOLP (name) && !EQ (name, tem)))
2779 return Qnil;
2780 else
2781 return tem;
2782 }
2783 \f
2784 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2785 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2786 The value is t if a symbol was found and deleted, nil otherwise.\n\
2787 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2788 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2789 OBARRAY defaults to the value of the variable `obarray'.")
2790 (name, obarray)
2791 Lisp_Object name, obarray;
2792 {
2793 register Lisp_Object string, tem;
2794 int hash;
2795
2796 if (NILP (obarray)) obarray = Vobarray;
2797 obarray = check_obarray (obarray);
2798
2799 if (SYMBOLP (name))
2800 XSETSTRING (string, XSYMBOL (name)->name);
2801 else
2802 {
2803 CHECK_STRING (name, 0);
2804 string = name;
2805 }
2806
2807 tem = oblookup (obarray, XSTRING (string)->data,
2808 XSTRING (string)->size,
2809 STRING_BYTES (XSTRING (string)));
2810 if (INTEGERP (tem))
2811 return Qnil;
2812 /* If arg was a symbol, don't delete anything but that symbol itself. */
2813 if (SYMBOLP (name) && !EQ (name, tem))
2814 return Qnil;
2815
2816 XSYMBOL (tem)->obarray = Qnil;
2817
2818 hash = oblookup_last_bucket_number;
2819
2820 if (EQ (XVECTOR (obarray)->contents[hash], tem))
2821 {
2822 if (XSYMBOL (tem)->next)
2823 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
2824 else
2825 XSETINT (XVECTOR (obarray)->contents[hash], 0);
2826 }
2827 else
2828 {
2829 Lisp_Object tail, following;
2830
2831 for (tail = XVECTOR (obarray)->contents[hash];
2832 XSYMBOL (tail)->next;
2833 tail = following)
2834 {
2835 XSETSYMBOL (following, XSYMBOL (tail)->next);
2836 if (EQ (following, tem))
2837 {
2838 XSYMBOL (tail)->next = XSYMBOL (following)->next;
2839 break;
2840 }
2841 }
2842 }
2843
2844 return Qt;
2845 }
2846 \f
2847 /* Return the symbol in OBARRAY whose names matches the string
2848 of SIZE characters (SIZE_BYTE bytes) at PTR.
2849 If there is no such symbol in OBARRAY, return nil.
2850
2851 Also store the bucket number in oblookup_last_bucket_number. */
2852
2853 Lisp_Object
2854 oblookup (obarray, ptr, size, size_byte)
2855 Lisp_Object obarray;
2856 register char *ptr;
2857 int size, size_byte;
2858 {
2859 int hash;
2860 int obsize;
2861 register Lisp_Object tail;
2862 Lisp_Object bucket, tem;
2863
2864 if (!VECTORP (obarray)
2865 || (obsize = XVECTOR (obarray)->size) == 0)
2866 {
2867 obarray = check_obarray (obarray);
2868 obsize = XVECTOR (obarray)->size;
2869 }
2870 /* This is sometimes needed in the middle of GC. */
2871 obsize &= ~ARRAY_MARK_FLAG;
2872 /* Combining next two lines breaks VMS C 2.3. */
2873 hash = hash_string (ptr, size_byte);
2874 hash %= obsize;
2875 bucket = XVECTOR (obarray)->contents[hash];
2876 oblookup_last_bucket_number = hash;
2877 if (XFASTINT (bucket) == 0)
2878 ;
2879 else if (!SYMBOLP (bucket))
2880 error ("Bad data in guts of obarray"); /* Like CADR error message */
2881 else
2882 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
2883 {
2884 if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte
2885 && XSYMBOL (tail)->name->size == size
2886 && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte))
2887 return tail;
2888 else if (XSYMBOL (tail)->next == 0)
2889 break;
2890 }
2891 XSETINT (tem, hash);
2892 return tem;
2893 }
2894
2895 static int
2896 hash_string (ptr, len)
2897 unsigned char *ptr;
2898 int len;
2899 {
2900 register unsigned char *p = ptr;
2901 register unsigned char *end = p + len;
2902 register unsigned char c;
2903 register int hash = 0;
2904
2905 while (p != end)
2906 {
2907 c = *p++;
2908 if (c >= 0140) c -= 40;
2909 hash = ((hash<<3) + (hash>>28) + c);
2910 }
2911 return hash & 07777777777;
2912 }
2913 \f
2914 void
2915 map_obarray (obarray, fn, arg)
2916 Lisp_Object obarray;
2917 void (*fn) P_ ((Lisp_Object, Lisp_Object));
2918 Lisp_Object arg;
2919 {
2920 register int i;
2921 register Lisp_Object tail;
2922 CHECK_VECTOR (obarray, 1);
2923 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
2924 {
2925 tail = XVECTOR (obarray)->contents[i];
2926 if (SYMBOLP (tail))
2927 while (1)
2928 {
2929 (*fn) (tail, arg);
2930 if (XSYMBOL (tail)->next == 0)
2931 break;
2932 XSETSYMBOL (tail, XSYMBOL (tail)->next);
2933 }
2934 }
2935 }
2936
2937 void
2938 mapatoms_1 (sym, function)
2939 Lisp_Object sym, function;
2940 {
2941 call1 (function, sym);
2942 }
2943
2944 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
2945 "Call FUNCTION on every symbol in OBARRAY.\n\
2946 OBARRAY defaults to the value of `obarray'.")
2947 (function, obarray)
2948 Lisp_Object function, obarray;
2949 {
2950 if (NILP (obarray)) obarray = Vobarray;
2951 obarray = check_obarray (obarray);
2952
2953 map_obarray (obarray, mapatoms_1, function);
2954 return Qnil;
2955 }
2956
2957 #define OBARRAY_SIZE 1511
2958
2959 void
2960 init_obarray ()
2961 {
2962 Lisp_Object oblength;
2963 int hash;
2964 Lisp_Object *tem;
2965
2966 XSETFASTINT (oblength, OBARRAY_SIZE);
2967
2968 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
2969 Vobarray = Fmake_vector (oblength, make_number (0));
2970 initial_obarray = Vobarray;
2971 staticpro (&initial_obarray);
2972 /* Intern nil in the obarray */
2973 XSYMBOL (Qnil)->obarray = Vobarray;
2974 /* These locals are to kludge around a pyramid compiler bug. */
2975 hash = hash_string ("nil", 3);
2976 /* Separate statement here to avoid VAXC bug. */
2977 hash %= OBARRAY_SIZE;
2978 tem = &XVECTOR (Vobarray)->contents[hash];
2979 *tem = Qnil;
2980
2981 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
2982 XSYMBOL (Qnil)->function = Qunbound;
2983 XSYMBOL (Qunbound)->value = Qunbound;
2984 XSYMBOL (Qunbound)->function = Qunbound;
2985
2986 Qt = intern ("t");
2987 XSYMBOL (Qnil)->value = Qnil;
2988 XSYMBOL (Qnil)->plist = Qnil;
2989 XSYMBOL (Qt)->value = Qt;
2990
2991 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
2992 Vpurify_flag = Qt;
2993
2994 Qvariable_documentation = intern ("variable-documentation");
2995 staticpro (&Qvariable_documentation);
2996
2997 read_buffer_size = 100 + MAX_LENGTH_OF_MULTI_BYTE_FORM;
2998 read_buffer = (char *) malloc (read_buffer_size);
2999 }
3000 \f
3001 void
3002 defsubr (sname)
3003 struct Lisp_Subr *sname;
3004 {
3005 Lisp_Object sym;
3006 sym = intern (sname->symbol_name);
3007 XSETSUBR (XSYMBOL (sym)->function, sname);
3008 }
3009
3010 #ifdef NOTDEF /* use fset in subr.el now */
3011 void
3012 defalias (sname, string)
3013 struct Lisp_Subr *sname;
3014 char *string;
3015 {
3016 Lisp_Object sym;
3017 sym = intern (string);
3018 XSETSUBR (XSYMBOL (sym)->function, sname);
3019 }
3020 #endif /* NOTDEF */
3021
3022 /* Define an "integer variable"; a symbol whose value is forwarded
3023 to a C variable of type int. Sample call: */
3024 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3025 void
3026 defvar_int (namestring, address)
3027 char *namestring;
3028 int *address;
3029 {
3030 Lisp_Object sym, val;
3031 sym = intern (namestring);
3032 val = allocate_misc ();
3033 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3034 XINTFWD (val)->intvar = address;
3035 XSYMBOL (sym)->value = val;
3036 }
3037
3038 /* Similar but define a variable whose value is T if address contains 1,
3039 NIL if address contains 0 */
3040 void
3041 defvar_bool (namestring, address)
3042 char *namestring;
3043 int *address;
3044 {
3045 Lisp_Object sym, val;
3046 sym = intern (namestring);
3047 val = allocate_misc ();
3048 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3049 XBOOLFWD (val)->boolvar = address;
3050 XSYMBOL (sym)->value = val;
3051 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3052 }
3053
3054 /* Similar but define a variable whose value is the Lisp Object stored
3055 at address. Two versions: with and without gc-marking of the C
3056 variable. The nopro version is used when that variable will be
3057 gc-marked for some other reason, since marking the same slot twice
3058 can cause trouble with strings. */
3059 void
3060 defvar_lisp_nopro (namestring, address)
3061 char *namestring;
3062 Lisp_Object *address;
3063 {
3064 Lisp_Object sym, val;
3065 sym = intern (namestring);
3066 val = allocate_misc ();
3067 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3068 XOBJFWD (val)->objvar = address;
3069 XSYMBOL (sym)->value = val;
3070 }
3071
3072 void
3073 defvar_lisp (namestring, address)
3074 char *namestring;
3075 Lisp_Object *address;
3076 {
3077 defvar_lisp_nopro (namestring, address);
3078 staticpro (address);
3079 }
3080
3081 #ifndef standalone
3082
3083 /* Similar but define a variable whose value is the Lisp Object stored in
3084 the current buffer. address is the address of the slot in the buffer
3085 that is current now. */
3086
3087 void
3088 defvar_per_buffer (namestring, address, type, doc)
3089 char *namestring;
3090 Lisp_Object *address;
3091 Lisp_Object type;
3092 char *doc;
3093 {
3094 Lisp_Object sym, val;
3095 int offset;
3096 extern struct buffer buffer_local_symbols;
3097
3098 sym = intern (namestring);
3099 val = allocate_misc ();
3100 offset = (char *)address - (char *)current_buffer;
3101
3102 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3103 XBUFFER_OBJFWD (val)->offset = offset;
3104 XSYMBOL (sym)->value = val;
3105 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
3106 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
3107 if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0)
3108 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3109 slot of buffer_local_flags */
3110 abort ();
3111 }
3112
3113 #endif /* standalone */
3114
3115 /* Similar but define a variable whose value is the Lisp Object stored
3116 at a particular offset in the current kboard object. */
3117
3118 void
3119 defvar_kboard (namestring, offset)
3120 char *namestring;
3121 int offset;
3122 {
3123 Lisp_Object sym, val;
3124 sym = intern (namestring);
3125 val = allocate_misc ();
3126 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3127 XKBOARD_OBJFWD (val)->offset = offset;
3128 XSYMBOL (sym)->value = val;
3129 }
3130 \f
3131 /* Record the value of load-path used at the start of dumping
3132 so we can see if the site changed it later during dumping. */
3133 static Lisp_Object dump_path;
3134
3135 void
3136 init_lread ()
3137 {
3138 char *normal;
3139 int turn_off_warning = 0;
3140
3141 /* Compute the default load-path. */
3142 #ifdef CANNOT_DUMP
3143 normal = PATH_LOADSEARCH;
3144 Vload_path = decode_env_path (0, normal);
3145 #else
3146 if (NILP (Vpurify_flag))
3147 normal = PATH_LOADSEARCH;
3148 else
3149 normal = PATH_DUMPLOADSEARCH;
3150
3151 /* In a dumped Emacs, we normally have to reset the value of
3152 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3153 uses ../lisp, instead of the path of the installed elisp
3154 libraries. However, if it appears that Vload_path was changed
3155 from the default before dumping, don't override that value. */
3156 if (initialized)
3157 {
3158 if (! NILP (Fequal (dump_path, Vload_path)))
3159 {
3160 Vload_path = decode_env_path (0, normal);
3161 if (!NILP (Vinstallation_directory))
3162 {
3163 /* Add to the path the lisp subdir of the
3164 installation dir, if it exists. */
3165 Lisp_Object tem, tem1;
3166 tem = Fexpand_file_name (build_string ("lisp"),
3167 Vinstallation_directory);
3168 tem1 = Ffile_exists_p (tem);
3169 if (!NILP (tem1))
3170 {
3171 if (NILP (Fmember (tem, Vload_path)))
3172 {
3173 turn_off_warning = 1;
3174 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3175 }
3176 }
3177 else
3178 /* That dir doesn't exist, so add the build-time
3179 Lisp dirs instead. */
3180 Vload_path = nconc2 (Vload_path, dump_path);
3181
3182 /* Add leim under the installation dir, if it exists. */
3183 tem = Fexpand_file_name (build_string ("leim"),
3184 Vinstallation_directory);
3185 tem1 = Ffile_exists_p (tem);
3186 if (!NILP (tem1))
3187 {
3188 if (NILP (Fmember (tem, Vload_path)))
3189 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3190 }
3191
3192 /* Add site-list under the installation dir, if it exists. */
3193 tem = Fexpand_file_name (build_string ("site-lisp"),
3194 Vinstallation_directory);
3195 tem1 = Ffile_exists_p (tem);
3196 if (!NILP (tem1))
3197 {
3198 if (NILP (Fmember (tem, Vload_path)))
3199 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3200 }
3201
3202 /* If Emacs was not built in the source directory,
3203 and it is run from where it was built, add to load-path
3204 the lisp, leim and site-lisp dirs under that directory. */
3205
3206 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3207 {
3208 Lisp_Object tem2;
3209
3210 tem = Fexpand_file_name (build_string ("src/Makefile"),
3211 Vinstallation_directory);
3212 tem1 = Ffile_exists_p (tem);
3213
3214 /* Don't be fooled if they moved the entire source tree
3215 AFTER dumping Emacs. If the build directory is indeed
3216 different from the source dir, src/Makefile.in and
3217 src/Makefile will not be found together. */
3218 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3219 Vinstallation_directory);
3220 tem2 = Ffile_exists_p (tem);
3221 if (!NILP (tem1) && NILP (tem2))
3222 {
3223 tem = Fexpand_file_name (build_string ("lisp"),
3224 Vsource_directory);
3225
3226 if (NILP (Fmember (tem, Vload_path)))
3227 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3228
3229 tem = Fexpand_file_name (build_string ("leim"),
3230 Vsource_directory);
3231
3232 if (NILP (Fmember (tem, Vload_path)))
3233 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3234
3235 tem = Fexpand_file_name (build_string ("site-lisp"),
3236 Vsource_directory);
3237
3238 if (NILP (Fmember (tem, Vload_path)))
3239 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3240 }
3241 }
3242 }
3243 }
3244 }
3245 else
3246 {
3247 /* NORMAL refers to the lisp dir in the source directory. */
3248 /* We used to add ../lisp at the front here, but
3249 that caused trouble because it was copied from dump_path
3250 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3251 It should be unnecessary. */
3252 Vload_path = decode_env_path (0, normal);
3253 dump_path = Vload_path;
3254 }
3255 #endif
3256
3257 #ifndef WINDOWSNT
3258 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3259 almost never correct, thereby causing a warning to be printed out that
3260 confuses users. Since PATH_LOADSEARCH is always overridden by the
3261 EMACSLOADPATH environment variable below, disable the warning on NT. */
3262
3263 /* Warn if dirs in the *standard* path don't exist. */
3264 if (!turn_off_warning)
3265 {
3266 Lisp_Object path_tail;
3267
3268 for (path_tail = Vload_path;
3269 !NILP (path_tail);
3270 path_tail = XCDR (path_tail))
3271 {
3272 Lisp_Object dirfile;
3273 dirfile = Fcar (path_tail);
3274 if (STRINGP (dirfile))
3275 {
3276 dirfile = Fdirectory_file_name (dirfile);
3277 if (access (XSTRING (dirfile)->data, 0) < 0)
3278 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3279 XCAR (path_tail));
3280 }
3281 }
3282 }
3283 #endif /* WINDOWSNT */
3284
3285 /* If the EMACSLOADPATH environment variable is set, use its value.
3286 This doesn't apply if we're dumping. */
3287 #ifndef CANNOT_DUMP
3288 if (NILP (Vpurify_flag)
3289 && egetenv ("EMACSLOADPATH"))
3290 #endif
3291 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3292
3293 Vvalues = Qnil;
3294
3295 load_in_progress = 0;
3296 Vload_file_name = Qnil;
3297
3298 load_descriptor_list = Qnil;
3299
3300 Vstandard_input = Qt;
3301 }
3302
3303 /* Print a warning, using format string FORMAT, that directory DIRNAME
3304 does not exist. Print it on stderr and put it in *Message*. */
3305
3306 void
3307 dir_warning (format, dirname)
3308 char *format;
3309 Lisp_Object dirname;
3310 {
3311 char *buffer
3312 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
3313
3314 fprintf (stderr, format, XSTRING (dirname)->data);
3315 sprintf (buffer, format, XSTRING (dirname)->data);
3316 /* Don't log the warning before we've initialized!! */
3317 if (initialized)
3318 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3319 }
3320
3321 void
3322 syms_of_lread ()
3323 {
3324 defsubr (&Sread);
3325 defsubr (&Sread_from_string);
3326 defsubr (&Sintern);
3327 defsubr (&Sintern_soft);
3328 defsubr (&Sunintern);
3329 defsubr (&Sload);
3330 defsubr (&Seval_buffer);
3331 defsubr (&Seval_region);
3332 defsubr (&Sread_char);
3333 defsubr (&Sread_char_exclusive);
3334 defsubr (&Sread_event);
3335 defsubr (&Sget_file_char);
3336 defsubr (&Smapatoms);
3337
3338 DEFVAR_LISP ("obarray", &Vobarray,
3339 "Symbol table for use by `intern' and `read'.\n\
3340 It is a vector whose length ought to be prime for best results.\n\
3341 The vector's contents don't make sense if examined from Lisp programs;\n\
3342 to find all the symbols in an obarray, use `mapatoms'.");
3343
3344 DEFVAR_LISP ("values", &Vvalues,
3345 "List of values of all expressions which were read, evaluated and printed.\n\
3346 Order is reverse chronological.");
3347
3348 DEFVAR_LISP ("standard-input", &Vstandard_input,
3349 "Stream for read to get input from.\n\
3350 See documentation of `read' for possible values.");
3351 Vstandard_input = Qt;
3352
3353 DEFVAR_LISP ("load-path", &Vload_path,
3354 "*List of directories to search for files to load.\n\
3355 Each element is a string (directory name) or nil (try default directory).\n\
3356 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3357 otherwise to default specified by file `epaths.h' when Emacs was built.");
3358
3359 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3360 "Non-nil iff inside of `load'.");
3361
3362 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3363 "An alist of expressions to be evalled when particular files are loaded.\n\
3364 Each element looks like (FILENAME FORMS...).\n\
3365 When `load' is run and the file-name argument is FILENAME,\n\
3366 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3367 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3368 with no directory specified, since that is how `load' is normally called.\n\
3369 An error in FORMS does not undo the load,\n\
3370 but does prevent execution of the rest of the FORMS.");
3371 Vafter_load_alist = Qnil;
3372
3373 DEFVAR_LISP ("load-history", &Vload_history,
3374 "Alist mapping source file names to symbols and features.\n\
3375 Each alist element is a list that starts with a file name,\n\
3376 except for one element (optional) that starts with nil and describes\n\
3377 definitions evaluated from buffers not visiting files.\n\
3378 The remaining elements of each list are symbols defined as functions\n\
3379 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
3380 Vload_history = Qnil;
3381
3382 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3383 "Full name of file being loaded by `load'.");
3384 Vload_file_name = Qnil;
3385
3386 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3387 "File name, including directory, of user's initialization file.");
3388 Vuser_init_file = Qnil;
3389
3390 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
3391 "Used for internal purposes by `load'.");
3392 Vcurrent_load_list = Qnil;
3393
3394 DEFVAR_LISP ("load-read-function", &Vload_read_function,
3395 "Function used by `load' and `eval-region' for reading expressions.\n\
3396 The default is nil, which means use the function `read'.");
3397 Vload_read_function = Qnil;
3398
3399 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
3400 "Function called in `load' for loading an Emacs lisp source file.\n\
3401 This function is for doing code conversion before reading the source file.\n\
3402 If nil, loading is done without any code conversion.\n\
3403 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3404 FULLNAME is the full name of FILE.\n\
3405 See `load' for the meaning of the remaining arguments.");
3406 Vload_source_file_function = Qnil;
3407
3408 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
3409 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3410 This is useful when the file being loaded is a temporary copy.");
3411 load_force_doc_strings = 0;
3412
3413 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
3414 "Non-nil means `load' converts strings to unibyte whenever possible.\n\
3415 This is normally used in `load-with-code-conversion'\n\
3416 for loading non-compiled files.");
3417 load_convert_to_unibyte = 0;
3418
3419 DEFVAR_LISP ("source-directory", &Vsource_directory,
3420 "Directory in which Emacs sources were found when Emacs was built.\n\
3421 You cannot count on them to still be there!");
3422 Vsource_directory
3423 = Fexpand_file_name (build_string ("../"),
3424 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3425
3426 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
3427 "List of files that were preloaded (when dumping Emacs).");
3428 Vpreloaded_file_list = Qnil;
3429
3430 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
3431 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3432 Vbyte_boolean_vars = Qnil;
3433
3434 /* Vsource_directory was initialized in init_lread. */
3435
3436 load_descriptor_list = Qnil;
3437 staticpro (&load_descriptor_list);
3438
3439 Qcurrent_load_list = intern ("current-load-list");
3440 staticpro (&Qcurrent_load_list);
3441
3442 Qstandard_input = intern ("standard-input");
3443 staticpro (&Qstandard_input);
3444
3445 Qread_char = intern ("read-char");
3446 staticpro (&Qread_char);
3447
3448 Qget_file_char = intern ("get-file-char");
3449 staticpro (&Qget_file_char);
3450
3451 Qbackquote = intern ("`");
3452 staticpro (&Qbackquote);
3453 Qcomma = intern (",");
3454 staticpro (&Qcomma);
3455 Qcomma_at = intern (",@");
3456 staticpro (&Qcomma_at);
3457 Qcomma_dot = intern (",.");
3458 staticpro (&Qcomma_dot);
3459
3460 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
3461 staticpro (&Qinhibit_file_name_operation);
3462
3463 Qascii_character = intern ("ascii-character");
3464 staticpro (&Qascii_character);
3465
3466 Qfunction = intern ("function");
3467 staticpro (&Qfunction);
3468
3469 Qload = intern ("load");
3470 staticpro (&Qload);
3471
3472 Qload_file_name = intern ("load-file-name");
3473 staticpro (&Qload_file_name);
3474
3475 staticpro (&dump_path);
3476
3477 staticpro (&read_objects);
3478 read_objects = Qnil;
3479 staticpro (&seen_list);
3480
3481 }