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