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