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