(Fload): Calculate bytes of filename correctly.
[bpt/emacs.git] / src / lread.c
1 /* Lisp parsing and input streams.
2 Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <errno.h>
29 #include "lisp.h"
30 #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 = STRING_BYTES (XSTRING (file));
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[STRING_BYTES (XSTRING (found)) - 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[STRING_BYTES (XSTRING (found)) - 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[STRING_BYTES (XSTRING (found)) - 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) + STRING_BYTES (XSTRING (filename)) + 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 STRING_BYTES (XSTRING (filename)) - 2);
921 fn[STRING_BYTES (XSTRING (filename)) - 2] = 0;
922 }
923 else
924 {
925 strncpy (fn, XSTRING (filename)->data,
926 STRING_BYTES (XSTRING (filename)));
927 fn[STRING_BYTES (XSTRING (filename))] = 0;
928 }
929
930 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
931 strncat (fn, nsuffix, lsuffix);
932
933 /* Check that the file exists and is not a directory. */
934 if (absolute)
935 handler = Qnil;
936 else
937 handler = Ffind_file_name_handler (filename, Qfile_exists_p);
938 if (! NILP (handler) && ! exec_only)
939 {
940 Lisp_Object string;
941 int exists;
942
943 string = build_string (fn);
944 exists = ! NILP (exec_only ? Ffile_executable_p (string)
945 : Ffile_readable_p (string));
946 if (exists
947 && ! NILP (Ffile_directory_p (build_string (fn))))
948 exists = 0;
949
950 if (exists)
951 {
952 /* We succeeded; return this descriptor and filename. */
953 if (storeptr)
954 *storeptr = build_string (fn);
955 UNGCPRO;
956 return 0;
957 }
958 }
959 else
960 {
961 int exists = (stat (fn, &st) >= 0
962 && (st.st_mode & S_IFMT) != S_IFDIR);
963 if (exists)
964 {
965 /* Check that we can access or open it. */
966 if (exec_only)
967 fd = (access (fn, X_OK) == 0) ? 1 : -1;
968 else
969 fd = open (fn, O_RDONLY, 0);
970
971 if (fd >= 0)
972 {
973 /* We succeeded; return this descriptor and filename. */
974 if (storeptr)
975 *storeptr = build_string (fn);
976 UNGCPRO;
977 return fd;
978 }
979 }
980 }
981
982 /* Advance to next suffix. */
983 if (esuffix == 0)
984 break;
985 nsuffix += lsuffix + 1;
986 }
987 if (absolute)
988 break;
989 }
990
991 UNGCPRO;
992 return -1;
993 }
994
995 \f
996 /* Merge the list we've accumulated of globals from the current input source
997 into the load_history variable. The details depend on whether
998 the source has an associated file name or not. */
999
1000 static void
1001 build_load_history (stream, source)
1002 FILE *stream;
1003 Lisp_Object source;
1004 {
1005 register Lisp_Object tail, prev, newelt;
1006 register Lisp_Object tem, tem2;
1007 register int foundit, loading;
1008
1009 loading = stream || !NARROWED;
1010
1011 tail = Vload_history;
1012 prev = Qnil;
1013 foundit = 0;
1014 while (!NILP (tail))
1015 {
1016 tem = Fcar (tail);
1017
1018 /* Find the feature's previous assoc list... */
1019 if (!NILP (Fequal (source, Fcar (tem))))
1020 {
1021 foundit = 1;
1022
1023 /* If we're loading, remove it. */
1024 if (loading)
1025 {
1026 if (NILP (prev))
1027 Vload_history = Fcdr (tail);
1028 else
1029 Fsetcdr (prev, Fcdr (tail));
1030 }
1031
1032 /* Otherwise, cons on new symbols that are not already members. */
1033 else
1034 {
1035 tem2 = Vcurrent_load_list;
1036
1037 while (CONSP (tem2))
1038 {
1039 newelt = Fcar (tem2);
1040
1041 if (NILP (Fmemq (newelt, tem)))
1042 Fsetcar (tail, Fcons (Fcar (tem),
1043 Fcons (newelt, Fcdr (tem))));
1044
1045 tem2 = Fcdr (tem2);
1046 QUIT;
1047 }
1048 }
1049 }
1050 else
1051 prev = tail;
1052 tail = Fcdr (tail);
1053 QUIT;
1054 }
1055
1056 /* If we're loading, cons the new assoc onto the front of load-history,
1057 the most-recently-loaded position. Also do this if we didn't find
1058 an existing member for the current source. */
1059 if (loading || !foundit)
1060 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
1061 Vload_history);
1062 }
1063
1064 Lisp_Object
1065 unreadpure () /* Used as unwind-protect function in readevalloop */
1066 {
1067 read_pure = 0;
1068 return Qnil;
1069 }
1070
1071 static Lisp_Object
1072 readevalloop_1 (old)
1073 Lisp_Object old;
1074 {
1075 load_convert_to_unibyte = ! NILP (old);
1076 return Qnil;
1077 }
1078
1079 /* UNIBYTE specifies how to set load_convert_to_unibyte
1080 for this invocation.
1081 READFUN, if non-nil, is used instead of `read'. */
1082
1083 static void
1084 readevalloop (readcharfun, stream, sourcename, evalfun, printflag, unibyte, readfun)
1085 Lisp_Object readcharfun;
1086 FILE *stream;
1087 Lisp_Object sourcename;
1088 Lisp_Object (*evalfun) ();
1089 int printflag;
1090 Lisp_Object unibyte, readfun;
1091 {
1092 register int c;
1093 register Lisp_Object val;
1094 int count = specpdl_ptr - specpdl;
1095 struct gcpro gcpro1;
1096 struct buffer *b = 0;
1097
1098 if (BUFFERP (readcharfun))
1099 b = XBUFFER (readcharfun);
1100 else if (MARKERP (readcharfun))
1101 b = XMARKER (readcharfun)->buffer;
1102
1103 specbind (Qstandard_input, readcharfun);
1104 specbind (Qcurrent_load_list, Qnil);
1105 record_unwind_protect (readevalloop_1, load_convert_to_unibyte ? Qt : Qnil);
1106 load_convert_to_unibyte = !NILP (unibyte);
1107
1108 readchar_backlog = -1;
1109
1110 GCPRO1 (sourcename);
1111
1112 LOADHIST_ATTACH (sourcename);
1113
1114 while (1)
1115 {
1116 if (b != 0 && NILP (b->name))
1117 error ("Reading from killed buffer");
1118
1119 instream = stream;
1120 c = READCHAR;
1121 if (c == ';')
1122 {
1123 while ((c = READCHAR) != '\n' && c != -1);
1124 continue;
1125 }
1126 if (c < 0) break;
1127
1128 /* Ignore whitespace here, so we can detect eof. */
1129 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
1130 continue;
1131
1132 if (!NILP (Vpurify_flag) && c == '(')
1133 {
1134 int count1 = specpdl_ptr - specpdl;
1135 record_unwind_protect (unreadpure, Qnil);
1136 val = read_list (-1, readcharfun);
1137 unbind_to (count1, Qnil);
1138 }
1139 else
1140 {
1141 UNREAD (c);
1142 read_objects = Qnil;
1143 if (! NILP (readfun))
1144 val = call1 (readfun, readcharfun);
1145 else if (! NILP (Vload_read_function))
1146 val = call1 (Vload_read_function, readcharfun);
1147 else
1148 val = read0 (readcharfun);
1149 }
1150
1151 val = (*evalfun) (val);
1152 if (printflag)
1153 {
1154 Vvalues = Fcons (val, Vvalues);
1155 if (EQ (Vstandard_output, Qt))
1156 Fprin1 (val, Qnil);
1157 else
1158 Fprint (val, Qnil);
1159 }
1160 }
1161
1162 build_load_history (stream, sourcename);
1163 UNGCPRO;
1164
1165 unbind_to (count, Qnil);
1166 }
1167
1168 #ifndef standalone
1169
1170 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 5, "",
1171 "Execute the current buffer as Lisp code.\n\
1172 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
1173 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
1174 PRINTFLAG controls printing of output:\n\
1175 nil means discard it; anything else is stream for print.\n\
1176 \n\
1177 If the optional third argument FILENAME is non-nil,\n\
1178 it specifies the file name to use for `load-history'.\n\
1179 The optional fourth argument UNIBYTE specifies `load-convert-to-unibyte'\n\
1180 for this invocation.\n\
1181 \n\
1182 The optional fifth argument DO-ALLOW-PRINT, if not-nil, specifies that\n\
1183 `print' and related functions should work normally even if PRINTFLAG is nil.\n\
1184 \n\
1185 This function preserves the position of point.")
1186 (buffer, printflag, filename, unibyte, do_allow_print)
1187 Lisp_Object buffer, printflag, filename, unibyte, do_allow_print;
1188 {
1189 int count = specpdl_ptr - specpdl;
1190 Lisp_Object tem, buf;
1191
1192 if (NILP (buffer))
1193 buf = Fcurrent_buffer ();
1194 else
1195 buf = Fget_buffer (buffer);
1196 if (NILP (buf))
1197 error ("No such buffer");
1198
1199 if (NILP (printflag) && NILP (do_allow_print))
1200 tem = Qsymbolp;
1201 else
1202 tem = printflag;
1203
1204 if (NILP (filename))
1205 filename = XBUFFER (buf)->filename;
1206
1207 specbind (Qstandard_output, tem);
1208 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1209 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
1210 readevalloop (buf, 0, filename, Feval, !NILP (printflag), unibyte, Qnil);
1211 unbind_to (count, Qnil);
1212
1213 return Qnil;
1214 }
1215
1216 #if 0
1217 XDEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
1218 "Execute the current buffer as Lisp code.\n\
1219 Programs can pass argument PRINTFLAG which controls printing of output:\n\
1220 nil means discard it; anything else is stream for print.\n\
1221 \n\
1222 If there is no error, point does not move. If there is an error,\n\
1223 point remains at the end of the last character read from the buffer.")
1224 (printflag)
1225 Lisp_Object printflag;
1226 {
1227 int count = specpdl_ptr - specpdl;
1228 Lisp_Object tem, cbuf;
1229
1230 cbuf = Fcurrent_buffer ()
1231
1232 if (NILP (printflag))
1233 tem = Qsymbolp;
1234 else
1235 tem = printflag;
1236 specbind (Qstandard_output, tem);
1237 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1238 SET_PT (BEGV);
1239 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1240 !NILP (printflag), Qnil, Qnil);
1241 return unbind_to (count, Qnil);
1242 }
1243 #endif
1244
1245 DEFUN ("eval-region", Feval_region, Seval_region, 2, 4, "r",
1246 "Execute the region as Lisp code.\n\
1247 When called from programs, expects two arguments,\n\
1248 giving starting and ending indices in the current buffer\n\
1249 of the text to be executed.\n\
1250 Programs can pass third argument PRINTFLAG which controls output:\n\
1251 nil means discard it; anything else is stream for printing it.\n\
1252 Also the fourth argument READ-FUNCTION, if non-nil, is used\n\
1253 instead of `read' to read each expression. It gets one argument\n\
1254 which is the input stream for reading characters.\n\
1255 \n\
1256 This function does not move point.")
1257 (start, end, printflag, read_function)
1258 Lisp_Object start, end, printflag, read_function;
1259 {
1260 int count = specpdl_ptr - specpdl;
1261 Lisp_Object tem, cbuf;
1262
1263 cbuf = Fcurrent_buffer ();
1264
1265 if (NILP (printflag))
1266 tem = Qsymbolp;
1267 else
1268 tem = printflag;
1269 specbind (Qstandard_output, tem);
1270
1271 if (NILP (printflag))
1272 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1273 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1274
1275 /* This both uses start and checks its type. */
1276 Fgoto_char (start);
1277 Fnarrow_to_region (make_number (BEGV), end);
1278 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval,
1279 !NILP (printflag), Qnil, read_function);
1280
1281 return unbind_to (count, Qnil);
1282 }
1283
1284 #endif /* standalone */
1285 \f
1286 DEFUN ("read", Fread, Sread, 0, 1, 0,
1287 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1288 If STREAM is nil, use the value of `standard-input' (which see).\n\
1289 STREAM or the value of `standard-input' may be:\n\
1290 a buffer (read from point and advance it)\n\
1291 a marker (read from where it points and advance it)\n\
1292 a function (call it with no arguments for each character,\n\
1293 call it with a char as argument to push a char back)\n\
1294 a string (takes text from string, starting at the beginning)\n\
1295 t (read text line using minibuffer and use it).")
1296 (stream)
1297 Lisp_Object stream;
1298 {
1299 extern Lisp_Object Fread_minibuffer ();
1300
1301 if (NILP (stream))
1302 stream = Vstandard_input;
1303 if (EQ (stream, Qt))
1304 stream = Qread_char;
1305
1306 readchar_backlog = -1;
1307 new_backquote_flag = 0;
1308 read_objects = Qnil;
1309
1310 #ifndef standalone
1311 if (EQ (stream, Qread_char))
1312 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1313 #endif
1314
1315 if (STRINGP (stream))
1316 return Fcar (Fread_from_string (stream, Qnil, Qnil));
1317
1318 return read0 (stream);
1319 }
1320
1321 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1322 "Read one Lisp expression which is represented as text by STRING.\n\
1323 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1324 START and END optionally delimit a substring of STRING from which to read;\n\
1325 they default to 0 and (length STRING) respectively.")
1326 (string, start, end)
1327 Lisp_Object string, start, end;
1328 {
1329 int startval, endval;
1330 Lisp_Object tem;
1331
1332 CHECK_STRING (string,0);
1333
1334 if (NILP (end))
1335 endval = XSTRING (string)->size;
1336 else
1337 {
1338 CHECK_NUMBER (end, 2);
1339 endval = XINT (end);
1340 if (endval < 0 || endval > XSTRING (string)->size)
1341 args_out_of_range (string, end);
1342 }
1343
1344 if (NILP (start))
1345 startval = 0;
1346 else
1347 {
1348 CHECK_NUMBER (start, 1);
1349 startval = XINT (start);
1350 if (startval < 0 || startval > endval)
1351 args_out_of_range (string, start);
1352 }
1353
1354 read_from_string_index = startval;
1355 read_from_string_index_byte = string_char_to_byte (string, startval);
1356 read_from_string_limit = endval;
1357
1358 new_backquote_flag = 0;
1359 read_objects = Qnil;
1360
1361 tem = read0 (string);
1362 return Fcons (tem, make_number (read_from_string_index));
1363 }
1364 \f
1365 /* Use this for recursive reads, in contexts where internal tokens
1366 are not allowed. */
1367
1368 static Lisp_Object
1369 read0 (readcharfun)
1370 Lisp_Object readcharfun;
1371 {
1372 register Lisp_Object val;
1373 int c;
1374
1375 val = read1 (readcharfun, &c, 0);
1376 if (c)
1377 Fsignal (Qinvalid_read_syntax, Fcons (Fmake_string (make_number (1),
1378 make_number (c)),
1379 Qnil));
1380
1381 return val;
1382 }
1383 \f
1384 static int read_buffer_size;
1385 static char *read_buffer;
1386
1387 /* Read multibyte form and return it as a character. C is a first
1388 byte of multibyte form, and rest of them are read from
1389 READCHARFUN. */
1390
1391 static int
1392 read_multibyte (c, readcharfun)
1393 register int c;
1394 Lisp_Object readcharfun;
1395 {
1396 /* We need the actual character code of this multibyte
1397 characters. */
1398 unsigned char str[MAX_LENGTH_OF_MULTI_BYTE_FORM];
1399 int len = 0;
1400
1401 str[len++] = c;
1402 while ((c = READCHAR) >= 0xA0
1403 && len < MAX_LENGTH_OF_MULTI_BYTE_FORM)
1404 str[len++] = c;
1405 UNREAD (c);
1406 return STRING_CHAR (str, len);
1407 }
1408
1409 /* Read a \-escape sequence, assuming we already read the `\'. */
1410
1411 static int
1412 read_escape (readcharfun, stringp)
1413 Lisp_Object readcharfun;
1414 int stringp;
1415 {
1416 register int c = READCHAR;
1417 switch (c)
1418 {
1419 case -1:
1420 error ("End of file");
1421
1422 case 'a':
1423 return '\007';
1424 case 'b':
1425 return '\b';
1426 case 'd':
1427 return 0177;
1428 case 'e':
1429 return 033;
1430 case 'f':
1431 return '\f';
1432 case 'n':
1433 return '\n';
1434 case 'r':
1435 return '\r';
1436 case 't':
1437 return '\t';
1438 case 'v':
1439 return '\v';
1440 case '\n':
1441 return -1;
1442 case ' ':
1443 if (stringp)
1444 return -1;
1445 return ' ';
1446
1447 case 'M':
1448 c = READCHAR;
1449 if (c != '-')
1450 error ("Invalid escape character syntax");
1451 c = READCHAR;
1452 if (c == '\\')
1453 c = read_escape (readcharfun, 0);
1454 return c | meta_modifier;
1455
1456 case 'S':
1457 c = READCHAR;
1458 if (c != '-')
1459 error ("Invalid escape character syntax");
1460 c = READCHAR;
1461 if (c == '\\')
1462 c = read_escape (readcharfun, 0);
1463 return c | shift_modifier;
1464
1465 case 'H':
1466 c = READCHAR;
1467 if (c != '-')
1468 error ("Invalid escape character syntax");
1469 c = READCHAR;
1470 if (c == '\\')
1471 c = read_escape (readcharfun, 0);
1472 return c | hyper_modifier;
1473
1474 case 'A':
1475 c = READCHAR;
1476 if (c != '-')
1477 error ("Invalid escape character syntax");
1478 c = READCHAR;
1479 if (c == '\\')
1480 c = read_escape (readcharfun, 0);
1481 return c | alt_modifier;
1482
1483 case 's':
1484 c = READCHAR;
1485 if (c != '-')
1486 error ("Invalid escape character syntax");
1487 c = READCHAR;
1488 if (c == '\\')
1489 c = read_escape (readcharfun, 0);
1490 return c | super_modifier;
1491
1492 case 'C':
1493 c = READCHAR;
1494 if (c != '-')
1495 error ("Invalid escape character syntax");
1496 case '^':
1497 c = READCHAR;
1498 if (c == '\\')
1499 c = read_escape (readcharfun, 0);
1500 if ((c & ~CHAR_MODIFIER_MASK) == '?')
1501 return 0177 | (c & CHAR_MODIFIER_MASK);
1502 else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1503 return c | ctrl_modifier;
1504 /* ASCII control chars are made from letters (both cases),
1505 as well as the non-letters within 0100...0137. */
1506 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1507 return (c & (037 | ~0177));
1508 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1509 return (c & (037 | ~0177));
1510 else
1511 return c | ctrl_modifier;
1512
1513 case '0':
1514 case '1':
1515 case '2':
1516 case '3':
1517 case '4':
1518 case '5':
1519 case '6':
1520 case '7':
1521 /* An octal escape, as in ANSI C. */
1522 {
1523 register int i = c - '0';
1524 register int count = 0;
1525 while (++count < 3)
1526 {
1527 if ((c = READCHAR) >= '0' && c <= '7')
1528 {
1529 i *= 8;
1530 i += c - '0';
1531 }
1532 else
1533 {
1534 UNREAD (c);
1535 break;
1536 }
1537 }
1538 return i;
1539 }
1540
1541 case 'x':
1542 /* A hex escape, as in ANSI C. */
1543 {
1544 int i = 0;
1545 while (1)
1546 {
1547 c = READCHAR;
1548 if (c >= '0' && c <= '9')
1549 {
1550 i *= 16;
1551 i += c - '0';
1552 }
1553 else if ((c >= 'a' && c <= 'f')
1554 || (c >= 'A' && c <= 'F'))
1555 {
1556 i *= 16;
1557 if (c >= 'a' && c <= 'f')
1558 i += c - 'a' + 10;
1559 else
1560 i += c - 'A' + 10;
1561 }
1562 else
1563 {
1564 UNREAD (c);
1565 break;
1566 }
1567 }
1568 return i;
1569 }
1570
1571 default:
1572 if (BASE_LEADING_CODE_P (c))
1573 c = read_multibyte (c, readcharfun);
1574 return c;
1575 }
1576 }
1577
1578 /* If the next token is ')' or ']' or '.', we store that character
1579 in *PCH and the return value is not interesting. Else, we store
1580 zero in *PCH and we read and return one lisp object.
1581
1582 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1583
1584 static Lisp_Object
1585 read1 (readcharfun, pch, first_in_list)
1586 register Lisp_Object readcharfun;
1587 int *pch;
1588 int first_in_list;
1589 {
1590 register int c;
1591 int uninterned_symbol = 0;
1592
1593 *pch = 0;
1594
1595 retry:
1596
1597 c = READCHAR;
1598 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1599
1600 switch (c)
1601 {
1602 case '(':
1603 return read_list (0, readcharfun);
1604
1605 case '[':
1606 return read_vector (readcharfun, 0);
1607
1608 case ')':
1609 case ']':
1610 {
1611 *pch = c;
1612 return Qnil;
1613 }
1614
1615 case '#':
1616 c = READCHAR;
1617 if (c == '^')
1618 {
1619 c = READCHAR;
1620 if (c == '[')
1621 {
1622 Lisp_Object tmp;
1623 tmp = read_vector (readcharfun, 0);
1624 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1625 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1626 error ("Invalid size char-table");
1627 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1628 XCHAR_TABLE (tmp)->top = Qt;
1629 return tmp;
1630 }
1631 else if (c == '^')
1632 {
1633 c = READCHAR;
1634 if (c == '[')
1635 {
1636 Lisp_Object tmp;
1637 tmp = read_vector (readcharfun, 0);
1638 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
1639 error ("Invalid size char-table");
1640 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1641 XCHAR_TABLE (tmp)->top = Qnil;
1642 return tmp;
1643 }
1644 Fsignal (Qinvalid_read_syntax,
1645 Fcons (make_string ("#^^", 3), Qnil));
1646 }
1647 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1648 }
1649 if (c == '&')
1650 {
1651 Lisp_Object length;
1652 length = read1 (readcharfun, pch, first_in_list);
1653 c = READCHAR;
1654 if (c == '"')
1655 {
1656 Lisp_Object tmp, val;
1657 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1658 / BITS_PER_CHAR);
1659
1660 UNREAD (c);
1661 tmp = read1 (readcharfun, pch, first_in_list);
1662 if (size_in_chars != XSTRING (tmp)->size
1663 /* We used to print 1 char too many
1664 when the number of bits was a multiple of 8.
1665 Accept such input in case it came from an old version. */
1666 && ! (XFASTINT (length)
1667 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1668 Fsignal (Qinvalid_read_syntax,
1669 Fcons (make_string ("#&...", 5), Qnil));
1670
1671 val = Fmake_bool_vector (length, Qnil);
1672 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1673 size_in_chars);
1674 /* Clear the extraneous bits in the last byte. */
1675 if (XINT (length) != size_in_chars * BITS_PER_CHAR)
1676 XBOOL_VECTOR (val)->data[size_in_chars - 1]
1677 &= (1 << (XINT (length) % BITS_PER_CHAR)) - 1;
1678 return val;
1679 }
1680 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1681 Qnil));
1682 }
1683 if (c == '[')
1684 {
1685 /* Accept compiled functions at read-time so that we don't have to
1686 build them using function calls. */
1687 Lisp_Object tmp;
1688 tmp = read_vector (readcharfun, 1);
1689 return Fmake_byte_code (XVECTOR (tmp)->size,
1690 XVECTOR (tmp)->contents);
1691 }
1692 #ifdef USE_TEXT_PROPERTIES
1693 if (c == '(')
1694 {
1695 Lisp_Object tmp;
1696 struct gcpro gcpro1;
1697 int ch;
1698
1699 /* Read the string itself. */
1700 tmp = read1 (readcharfun, &ch, 0);
1701 if (ch != 0 || !STRINGP (tmp))
1702 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1703 GCPRO1 (tmp);
1704 /* Read the intervals and their properties. */
1705 while (1)
1706 {
1707 Lisp_Object beg, end, plist;
1708
1709 beg = read1 (readcharfun, &ch, 0);
1710 if (ch == ')')
1711 break;
1712 if (ch == 0)
1713 end = read1 (readcharfun, &ch, 0);
1714 if (ch == 0)
1715 plist = read1 (readcharfun, &ch, 0);
1716 if (ch)
1717 Fsignal (Qinvalid_read_syntax,
1718 Fcons (build_string ("invalid string property list"),
1719 Qnil));
1720 Fset_text_properties (beg, end, plist, tmp);
1721 }
1722 UNGCPRO;
1723 return tmp;
1724 }
1725 #endif
1726 /* #@NUMBER is used to skip NUMBER following characters.
1727 That's used in .elc files to skip over doc strings
1728 and function definitions. */
1729 if (c == '@')
1730 {
1731 int i, nskip = 0;
1732
1733 /* Read a decimal integer. */
1734 while ((c = READCHAR) >= 0
1735 && c >= '0' && c <= '9')
1736 {
1737 nskip *= 10;
1738 nskip += c - '0';
1739 }
1740 if (c >= 0)
1741 UNREAD (c);
1742
1743 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1744 {
1745 /* If we are supposed to force doc strings into core right now,
1746 record the last string that we skipped,
1747 and record where in the file it comes from. */
1748
1749 /* But first exchange saved_doc_string
1750 with prev_saved_doc_string, so we save two strings. */
1751 {
1752 char *temp = saved_doc_string;
1753 int temp_size = saved_doc_string_size;
1754 int temp_pos = saved_doc_string_position;
1755 int temp_len = saved_doc_string_length;
1756
1757 saved_doc_string = prev_saved_doc_string;
1758 saved_doc_string_size = prev_saved_doc_string_size;
1759 saved_doc_string_position = prev_saved_doc_string_position;
1760 saved_doc_string_length = prev_saved_doc_string_length;
1761
1762 prev_saved_doc_string = temp;
1763 prev_saved_doc_string_size = temp_size;
1764 prev_saved_doc_string_position = temp_pos;
1765 prev_saved_doc_string_length = temp_len;
1766 }
1767
1768 if (saved_doc_string_size == 0)
1769 {
1770 saved_doc_string_size = nskip + 100;
1771 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1772 }
1773 if (nskip > saved_doc_string_size)
1774 {
1775 saved_doc_string_size = nskip + 100;
1776 saved_doc_string = (char *) xrealloc (saved_doc_string,
1777 saved_doc_string_size);
1778 }
1779
1780 saved_doc_string_position = ftell (instream);
1781
1782 /* Copy that many characters into saved_doc_string. */
1783 for (i = 0; i < nskip && c >= 0; i++)
1784 saved_doc_string[i] = c = READCHAR;
1785
1786 saved_doc_string_length = i;
1787 }
1788 else
1789 {
1790 /* Skip that many characters. */
1791 for (i = 0; i < nskip && c >= 0; i++)
1792 c = READCHAR;
1793 }
1794
1795 goto retry;
1796 }
1797 if (c == '$')
1798 return Vload_file_name;
1799 if (c == '\'')
1800 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1801 /* #:foo is the uninterned symbol named foo. */
1802 if (c == ':')
1803 {
1804 uninterned_symbol = 1;
1805 c = READCHAR;
1806 goto default_label;
1807 }
1808 /* Reader forms that can reuse previously read objects. */
1809 if (c >= '0' && c <= '9')
1810 {
1811 int n = 0;
1812 Lisp_Object tem;
1813
1814 /* Read a non-negative integer. */
1815 while (c >= '0' && c <= '9')
1816 {
1817 n *= 10;
1818 n += c - '0';
1819 c = READCHAR;
1820 }
1821 /* #n=object returns object, but associates it with n for #n#. */
1822 if (c == '=')
1823 {
1824 /* Make a placeholder for #n# to use temporarily */
1825 Lisp_Object placeholder;
1826 Lisp_Object cell;
1827
1828 placeholder = Fcons(Qnil, Qnil);
1829 cell = Fcons (make_number (n), placeholder);
1830 read_objects = Fcons (cell, read_objects);
1831
1832 /* Read the object itself. */
1833 tem = read0 (readcharfun);
1834
1835 /* Now put it everywhere the placeholder was... */
1836 substitute_object_in_subtree (tem, placeholder);
1837
1838 /* ...and #n# will use the real value from now on. */
1839 Fsetcdr (cell, tem);
1840
1841 return tem;
1842 }
1843 /* #n# returns a previously read object. */
1844 if (c == '#')
1845 {
1846 tem = Fassq (make_number (n), read_objects);
1847 if (CONSP (tem))
1848 return XCDR (tem);
1849 /* Fall through to error message. */
1850 }
1851 /* Fall through to error message. */
1852 }
1853
1854 UNREAD (c);
1855 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1856
1857 case ';':
1858 while ((c = READCHAR) >= 0 && c != '\n');
1859 goto retry;
1860
1861 case '\'':
1862 {
1863 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1864 }
1865
1866 case '`':
1867 if (first_in_list)
1868 goto default_label;
1869 else
1870 {
1871 Lisp_Object value;
1872
1873 new_backquote_flag = 1;
1874 value = read0 (readcharfun);
1875 new_backquote_flag = 0;
1876
1877 return Fcons (Qbackquote, Fcons (value, Qnil));
1878 }
1879
1880 case ',':
1881 if (new_backquote_flag)
1882 {
1883 Lisp_Object comma_type = Qnil;
1884 Lisp_Object value;
1885 int ch = READCHAR;
1886
1887 if (ch == '@')
1888 comma_type = Qcomma_at;
1889 else if (ch == '.')
1890 comma_type = Qcomma_dot;
1891 else
1892 {
1893 if (ch >= 0) UNREAD (ch);
1894 comma_type = Qcomma;
1895 }
1896
1897 new_backquote_flag = 0;
1898 value = read0 (readcharfun);
1899 new_backquote_flag = 1;
1900 return Fcons (comma_type, Fcons (value, Qnil));
1901 }
1902 else
1903 goto default_label;
1904
1905 case '?':
1906 {
1907 c = READCHAR;
1908 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1909
1910 if (c == '\\')
1911 c = read_escape (readcharfun, 0);
1912 else if (BASE_LEADING_CODE_P (c))
1913 c = read_multibyte (c, readcharfun);
1914
1915 return make_number (c);
1916 }
1917
1918 case '"':
1919 {
1920 register char *p = read_buffer;
1921 register char *end = read_buffer + read_buffer_size;
1922 register int c;
1923 /* Nonzero if we saw an escape sequence specifying
1924 a multibyte character. */
1925 int force_multibyte = 0;
1926 /* Nonzero if we saw an escape sequence specifying
1927 a single-byte character. */
1928 int force_singlebyte = 0;
1929 int cancel = 0;
1930 int nchars;
1931
1932 while ((c = READCHAR) >= 0
1933 && c != '\"')
1934 {
1935 if (end - p < MAX_LENGTH_OF_MULTI_BYTE_FORM)
1936 {
1937 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1938 p += new - read_buffer;
1939 read_buffer += new - read_buffer;
1940 end = read_buffer + read_buffer_size;
1941 }
1942
1943 if (c == '\\')
1944 {
1945 c = read_escape (readcharfun, 1);
1946
1947 /* C is -1 if \ newline has just been seen */
1948 if (c == -1)
1949 {
1950 if (p == read_buffer)
1951 cancel = 1;
1952 continue;
1953 }
1954
1955 /* If an escape specifies a non-ASCII single-byte character,
1956 this must be a unibyte string. */
1957 if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))
1958 && ! ASCII_BYTE_P ((c & ~CHAR_MODIFIER_MASK)))
1959 force_singlebyte = 1;
1960 }
1961
1962 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
1963 {
1964 unsigned char workbuf[4];
1965 unsigned char *str = workbuf;
1966 int length;
1967
1968 /* Any modifiers for a multibyte character are invalid. */
1969 if (c & CHAR_MODIFIER_MASK)
1970 error ("Invalid modifier in string");
1971 length = non_ascii_char_to_string (c, workbuf, &str);
1972 if (length > 1)
1973 force_multibyte = 1;
1974
1975 bcopy (str, p, length);
1976 p += length;
1977 }
1978 else
1979 {
1980 /* Allow `\C- ' and `\C-?'. */
1981 if (c == (CHAR_CTL | ' '))
1982 c = 0;
1983 else if (c == (CHAR_CTL | '?'))
1984 c = 127;
1985
1986 if (c & CHAR_SHIFT)
1987 {
1988 /* Shift modifier is valid only with [A-Za-z]. */
1989 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
1990 c &= ~CHAR_SHIFT;
1991 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
1992 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
1993 }
1994
1995 if (c & CHAR_META)
1996 /* Move the meta bit to the right place for a string. */
1997 c = (c & ~CHAR_META) | 0x80;
1998 if (c & ~0xff)
1999 error ("Invalid modifier in string");
2000 *p++ = c;
2001 }
2002 }
2003 if (c < 0)
2004 return Fsignal (Qend_of_file, Qnil);
2005
2006 /* If purifying, and string starts with \ newline,
2007 return zero instead. This is for doc strings
2008 that we are really going to find in etc/DOC.nn.nn */
2009 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
2010 return make_number (0);
2011
2012 if (force_multibyte)
2013 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
2014 else if (force_singlebyte)
2015 nchars = p - read_buffer;
2016 else if (load_convert_to_unibyte)
2017 {
2018 Lisp_Object string;
2019 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
2020 if (p - read_buffer != nchars)
2021 {
2022 string = make_multibyte_string (read_buffer, nchars,
2023 p - read_buffer);
2024 return Fstring_make_unibyte (string);
2025 }
2026 }
2027 else if (EQ (readcharfun, Qget_file_char)
2028 || EQ (readcharfun, Qlambda))
2029 /* Nowadays, reading directly from a file
2030 is used only for compiled Emacs Lisp files,
2031 and those always use the Emacs internal encoding.
2032 Meanwhile, Qlambda is used for reading dynamic byte code
2033 (compiled with byte-compile-dynamic = t). */
2034 nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
2035 else
2036 /* In all other cases, if we read these bytes as
2037 separate characters, treat them as separate characters now. */
2038 nchars = p - read_buffer;
2039
2040 if (read_pure)
2041 return make_pure_string (read_buffer, nchars, p - read_buffer,
2042 (force_multibyte
2043 || (p - read_buffer != nchars)));
2044 return make_specified_string (read_buffer, nchars, p - read_buffer,
2045 (force_multibyte
2046 || (p - read_buffer != nchars)));
2047 }
2048
2049 case '.':
2050 {
2051 #ifdef LISP_FLOAT_TYPE
2052 /* If a period is followed by a number, then we should read it
2053 as a floating point number. Otherwise, it denotes a dotted
2054 pair. */
2055 int next_char = READCHAR;
2056 UNREAD (next_char);
2057
2058 if (! (next_char >= '0' && next_char <= '9'))
2059 #endif
2060 {
2061 *pch = c;
2062 return Qnil;
2063 }
2064
2065 /* Otherwise, we fall through! Note that the atom-reading loop
2066 below will now loop at least once, assuring that we will not
2067 try to UNREAD two characters in a row. */
2068 }
2069 default:
2070 default_label:
2071 if (c <= 040) goto retry;
2072 {
2073 register char *p = read_buffer;
2074 int quoted = 0;
2075
2076 {
2077 register char *end = read_buffer + read_buffer_size;
2078
2079 while (c > 040
2080 && !(c == '\"' || c == '\'' || c == ';' || c == '?'
2081 || c == '(' || c == ')'
2082 #ifndef LISP_FLOAT_TYPE
2083 /* If we have floating-point support, then we need
2084 to allow <digits><dot><digits>. */
2085 || c =='.'
2086 #endif /* not LISP_FLOAT_TYPE */
2087 || c == '[' || c == ']' || c == '#'
2088 ))
2089 {
2090 if (end - p < MAX_LENGTH_OF_MULTI_BYTE_FORM)
2091 {
2092 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2093 p += new - read_buffer;
2094 read_buffer += new - read_buffer;
2095 end = read_buffer + read_buffer_size;
2096 }
2097 if (c == '\\')
2098 {
2099 c = READCHAR;
2100 quoted = 1;
2101 }
2102
2103 if (! SINGLE_BYTE_CHAR_P (c))
2104 {
2105 unsigned char workbuf[4];
2106 unsigned char *str = workbuf;
2107 int length;
2108
2109 length = non_ascii_char_to_string (c, workbuf, &str);
2110
2111 bcopy (str, p, length);
2112 p += length;
2113 }
2114 else
2115 *p++ = c;
2116
2117 c = READCHAR;
2118 }
2119
2120 if (p == end)
2121 {
2122 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
2123 p += new - read_buffer;
2124 read_buffer += new - read_buffer;
2125 /* end = read_buffer + read_buffer_size; */
2126 }
2127 *p = 0;
2128 if (c >= 0)
2129 UNREAD (c);
2130 }
2131
2132 if (!quoted && !uninterned_symbol)
2133 {
2134 register char *p1;
2135 register Lisp_Object val;
2136 p1 = read_buffer;
2137 if (*p1 == '+' || *p1 == '-') p1++;
2138 /* Is it an integer? */
2139 if (p1 != p)
2140 {
2141 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
2142 #ifdef LISP_FLOAT_TYPE
2143 /* Integers can have trailing decimal points. */
2144 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
2145 #endif
2146 if (p1 == p)
2147 /* It is an integer. */
2148 {
2149 #ifdef LISP_FLOAT_TYPE
2150 if (p1[-1] == '.')
2151 p1[-1] = '\0';
2152 #endif
2153 if (sizeof (int) == sizeof (EMACS_INT))
2154 XSETINT (val, atoi (read_buffer));
2155 else if (sizeof (long) == sizeof (EMACS_INT))
2156 XSETINT (val, atol (read_buffer));
2157 else
2158 abort ();
2159 return val;
2160 }
2161 }
2162 #ifdef LISP_FLOAT_TYPE
2163 if (isfloat_string (read_buffer))
2164 {
2165 /* Compute NaN and infinities using 0.0 in a variable,
2166 to cope with compilers that think they are smarter
2167 than we are. */
2168 double zero = 0.0;
2169
2170 double value;
2171
2172 /* Negate the value ourselves. This treats 0, NaNs,
2173 and infinity properly on IEEE floating point hosts,
2174 and works around a common bug where atof ("-0.0")
2175 drops the sign. */
2176 int negative = read_buffer[0] == '-';
2177
2178 /* The only way p[-1] can be 'F' or 'N', after isfloat_string
2179 returns 1, is if the input ends in e+INF or e+NaN. */
2180 switch (p[-1])
2181 {
2182 case 'F':
2183 value = 1.0 / zero;
2184 break;
2185 case 'N':
2186 value = zero / zero;
2187 break;
2188 default:
2189 value = atof (read_buffer + negative);
2190 break;
2191 }
2192
2193 return make_float (negative ? - value : value);
2194 }
2195 #endif
2196 }
2197
2198 if (uninterned_symbol)
2199 return make_symbol (read_buffer);
2200 else
2201 return intern (read_buffer);
2202 }
2203 }
2204 }
2205 \f
2206
2207 /* List of nodes we've seen during substitute_object_in_subtree. */
2208 static Lisp_Object seen_list;
2209
2210 static void
2211 substitute_object_in_subtree (object, placeholder)
2212 Lisp_Object object;
2213 Lisp_Object placeholder;
2214 {
2215 Lisp_Object check_object;
2216
2217 /* We haven't seen any objects when we start. */
2218 seen_list = Qnil;
2219
2220 /* Make all the substitutions. */
2221 check_object
2222 = substitute_object_recurse (object, placeholder, object);
2223
2224 /* Clear seen_list because we're done with it. */
2225 seen_list = Qnil;
2226
2227 /* The returned object here is expected to always eq the
2228 original. */
2229 if (!EQ (check_object, object))
2230 error ("Unexpected mutation error in reader");
2231 }
2232
2233 /* Feval doesn't get called from here, so no gc protection is needed. */
2234 #define SUBSTITUTE(get_val, set_val) \
2235 { \
2236 Lisp_Object old_value = get_val; \
2237 Lisp_Object true_value \
2238 = substitute_object_recurse (object, placeholder,\
2239 old_value); \
2240 \
2241 if (!EQ (old_value, true_value)) \
2242 { \
2243 set_val; \
2244 } \
2245 }
2246
2247 static Lisp_Object
2248 substitute_object_recurse (object, placeholder, subtree)
2249 Lisp_Object object;
2250 Lisp_Object placeholder;
2251 Lisp_Object subtree;
2252 {
2253 /* If we find the placeholder, return the target object. */
2254 if (EQ (placeholder, subtree))
2255 return object;
2256
2257 /* If we've been to this node before, don't explore it again. */
2258 if (!EQ (Qnil, Fmemq (subtree, seen_list)))
2259 return subtree;
2260
2261 /* If this node can be the entry point to a cycle, remember that
2262 we've seen it. It can only be such an entry point if it was made
2263 by #n=, which means that we can find it as a value in
2264 read_objects. */
2265 if (!EQ (Qnil, Frassq (subtree, read_objects)))
2266 seen_list = Fcons (subtree, seen_list);
2267
2268 /* Recurse according to subtree's type.
2269 Every branch must return a Lisp_Object. */
2270 switch (XTYPE (subtree))
2271 {
2272 case Lisp_Vectorlike:
2273 {
2274 int i;
2275 int length = Flength(subtree);
2276 for (i = 0; i < length; i++)
2277 {
2278 Lisp_Object idx = make_number (i);
2279 SUBSTITUTE (Faref (subtree, idx),
2280 Faset (subtree, idx, true_value));
2281 }
2282 return subtree;
2283 }
2284
2285 case Lisp_Cons:
2286 {
2287 SUBSTITUTE (Fcar_safe (subtree),
2288 Fsetcar (subtree, true_value));
2289 SUBSTITUTE (Fcdr_safe (subtree),
2290 Fsetcdr (subtree, true_value));
2291 return subtree;
2292 }
2293
2294 #ifdef USE_TEXT_PROPERTIES
2295 case Lisp_String:
2296 {
2297 /* Check for text properties in each interval.
2298 substitute_in_interval contains part of the logic. */
2299
2300 INTERVAL root_interval = XSTRING (subtree)->intervals;
2301 Lisp_Object arg = Fcons (object, placeholder);
2302
2303 traverse_intervals (root_interval, 1, 0,
2304 &substitute_in_interval, arg);
2305
2306 return subtree;
2307 }
2308 #endif /* defined USE_TEXT_PROPERTIES */
2309
2310 /* Other types don't recurse any further. */
2311 default:
2312 return subtree;
2313 }
2314 }
2315
2316 /* Helper function for substitute_object_recurse. */
2317 static void
2318 substitute_in_interval (interval, arg)
2319 INTERVAL interval;
2320 Lisp_Object arg;
2321 {
2322 Lisp_Object object = Fcar (arg);
2323 Lisp_Object placeholder = Fcdr (arg);
2324
2325 SUBSTITUTE(interval->plist, interval->plist = true_value);
2326 }
2327
2328 \f
2329 #ifdef LISP_FLOAT_TYPE
2330
2331 #define LEAD_INT 1
2332 #define DOT_CHAR 2
2333 #define TRAIL_INT 4
2334 #define E_CHAR 8
2335 #define EXP_INT 16
2336
2337 int
2338 isfloat_string (cp)
2339 register char *cp;
2340 {
2341 register int state;
2342
2343 char *start = cp;
2344
2345 state = 0;
2346 if (*cp == '+' || *cp == '-')
2347 cp++;
2348
2349 if (*cp >= '0' && *cp <= '9')
2350 {
2351 state |= LEAD_INT;
2352 while (*cp >= '0' && *cp <= '9')
2353 cp++;
2354 }
2355 if (*cp == '.')
2356 {
2357 state |= DOT_CHAR;
2358 cp++;
2359 }
2360 if (*cp >= '0' && *cp <= '9')
2361 {
2362 state |= TRAIL_INT;
2363 while (*cp >= '0' && *cp <= '9')
2364 cp++;
2365 }
2366 if (*cp == 'e' || *cp == 'E')
2367 {
2368 state |= E_CHAR;
2369 cp++;
2370 if (*cp == '+' || *cp == '-')
2371 cp++;
2372 }
2373
2374 if (*cp >= '0' && *cp <= '9')
2375 {
2376 state |= EXP_INT;
2377 while (*cp >= '0' && *cp <= '9')
2378 cp++;
2379 }
2380 else if (cp == start)
2381 ;
2382 else if (cp[-1] == '+' && cp[0] == 'I' && cp[1] == 'N' && cp[2] == 'F')
2383 {
2384 state |= EXP_INT;
2385 cp += 3;
2386 }
2387 else if (cp[-1] == '+' && cp[0] == 'N' && cp[1] == 'a' && cp[2] == 'N')
2388 {
2389 state |= EXP_INT;
2390 cp += 3;
2391 }
2392
2393 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
2394 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
2395 || state == (DOT_CHAR|TRAIL_INT)
2396 || state == (LEAD_INT|E_CHAR|EXP_INT)
2397 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
2398 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
2399 }
2400 #endif /* LISP_FLOAT_TYPE */
2401 \f
2402 static Lisp_Object
2403 read_vector (readcharfun, bytecodeflag)
2404 Lisp_Object readcharfun;
2405 int bytecodeflag;
2406 {
2407 register int i;
2408 register int size;
2409 register Lisp_Object *ptr;
2410 register Lisp_Object tem, item, vector;
2411 register struct Lisp_Cons *otem;
2412 Lisp_Object len;
2413
2414 tem = read_list (1, readcharfun);
2415 len = Flength (tem);
2416 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
2417
2418 size = XVECTOR (vector)->size;
2419 ptr = XVECTOR (vector)->contents;
2420 for (i = 0; i < size; i++)
2421 {
2422 item = Fcar (tem);
2423 /* If `load-force-doc-strings' is t when reading a lazily-loaded
2424 bytecode object, the docstring containing the bytecode and
2425 constants values must be treated as unibyte and passed to
2426 Fread, to get the actual bytecode string and constants vector. */
2427 if (bytecodeflag && load_force_doc_strings)
2428 {
2429 if (i == COMPILED_BYTECODE)
2430 {
2431 if (!STRINGP (item))
2432 error ("invalid byte code");
2433
2434 /* Delay handling the bytecode slot until we know whether
2435 it is lazily-loaded (we can tell by whether the
2436 constants slot is nil). */
2437 ptr[COMPILED_CONSTANTS] = item;
2438 item = Qnil;
2439 }
2440 else if (i == COMPILED_CONSTANTS)
2441 {
2442 Lisp_Object bytestr = ptr[COMPILED_CONSTANTS];
2443
2444 if (NILP (item))
2445 {
2446 /* Coerce string to unibyte (like string-as-unibyte,
2447 but without generating extra garbage and
2448 guaranteeing no change in the contents). */
2449 XSTRING (bytestr)->size = STRING_BYTES (XSTRING (bytestr));
2450 SET_STRING_BYTES (XSTRING (bytestr), -1);
2451
2452 item = Fread (bytestr);
2453 if (!CONSP (item))
2454 error ("invalid byte code");
2455
2456 otem = XCONS (item);
2457 bytestr = XCAR (item);
2458 item = XCDR (item);
2459 free_cons (otem);
2460 }
2461
2462 /* Now handle the bytecode slot. */
2463 ptr[COMPILED_BYTECODE] = read_pure ? Fpurecopy (bytestr) : bytestr;
2464 }
2465 }
2466 ptr[i] = read_pure ? Fpurecopy (item) : item;
2467 otem = XCONS (tem);
2468 tem = Fcdr (tem);
2469 free_cons (otem);
2470 }
2471 return vector;
2472 }
2473
2474 /* FLAG = 1 means check for ] to terminate rather than ) and .
2475 FLAG = -1 means check for starting with defun
2476 and make structure pure. */
2477
2478 static Lisp_Object
2479 read_list (flag, readcharfun)
2480 int flag;
2481 register Lisp_Object readcharfun;
2482 {
2483 /* -1 means check next element for defun,
2484 0 means don't check,
2485 1 means already checked and found defun. */
2486 int defunflag = flag < 0 ? -1 : 0;
2487 Lisp_Object val, tail;
2488 register Lisp_Object elt, tem;
2489 struct gcpro gcpro1, gcpro2;
2490 /* 0 is the normal case.
2491 1 means this list is a doc reference; replace it with the number 0.
2492 2 means this list is a doc reference; replace it with the doc string. */
2493 int doc_reference = 0;
2494
2495 /* Initialize this to 1 if we are reading a list. */
2496 int first_in_list = flag <= 0;
2497
2498 val = Qnil;
2499 tail = Qnil;
2500
2501 while (1)
2502 {
2503 int ch;
2504 GCPRO2 (val, tail);
2505 elt = read1 (readcharfun, &ch, first_in_list);
2506 UNGCPRO;
2507
2508 first_in_list = 0;
2509
2510 /* While building, if the list starts with #$, treat it specially. */
2511 if (EQ (elt, Vload_file_name)
2512 && ! NILP (elt)
2513 && !NILP (Vpurify_flag))
2514 {
2515 if (NILP (Vdoc_file_name))
2516 /* We have not yet called Snarf-documentation, so assume
2517 this file is described in the DOC-MM.NN file
2518 and Snarf-documentation will fill in the right value later.
2519 For now, replace the whole list with 0. */
2520 doc_reference = 1;
2521 else
2522 /* We have already called Snarf-documentation, so make a relative
2523 file name for this file, so it can be found properly
2524 in the installed Lisp directory.
2525 We don't use Fexpand_file_name because that would make
2526 the directory absolute now. */
2527 elt = concat2 (build_string ("../lisp/"),
2528 Ffile_name_nondirectory (elt));
2529 }
2530 else if (EQ (elt, Vload_file_name)
2531 && ! NILP (elt)
2532 && load_force_doc_strings)
2533 doc_reference = 2;
2534
2535 if (ch)
2536 {
2537 if (flag > 0)
2538 {
2539 if (ch == ']')
2540 return val;
2541 Fsignal (Qinvalid_read_syntax,
2542 Fcons (make_string (") or . in a vector", 18), Qnil));
2543 }
2544 if (ch == ')')
2545 return val;
2546 if (ch == '.')
2547 {
2548 GCPRO2 (val, tail);
2549 if (!NILP (tail))
2550 XCDR (tail) = read0 (readcharfun);
2551 else
2552 val = read0 (readcharfun);
2553 read1 (readcharfun, &ch, 0);
2554 UNGCPRO;
2555 if (ch == ')')
2556 {
2557 if (doc_reference == 1)
2558 return make_number (0);
2559 if (doc_reference == 2)
2560 {
2561 /* Get a doc string from the file we are loading.
2562 If it's in saved_doc_string, get it from there. */
2563 int pos = XINT (XCDR (val));
2564 /* Position is negative for user variables. */
2565 if (pos < 0) pos = -pos;
2566 if (pos >= saved_doc_string_position
2567 && pos < (saved_doc_string_position
2568 + saved_doc_string_length))
2569 {
2570 int start = pos - saved_doc_string_position;
2571 int from, to;
2572
2573 /* Process quoting with ^A,
2574 and find the end of the string,
2575 which is marked with ^_ (037). */
2576 for (from = start, to = start;
2577 saved_doc_string[from] != 037;)
2578 {
2579 int c = saved_doc_string[from++];
2580 if (c == 1)
2581 {
2582 c = saved_doc_string[from++];
2583 if (c == 1)
2584 saved_doc_string[to++] = c;
2585 else if (c == '0')
2586 saved_doc_string[to++] = 0;
2587 else if (c == '_')
2588 saved_doc_string[to++] = 037;
2589 }
2590 else
2591 saved_doc_string[to++] = c;
2592 }
2593
2594 return make_string (saved_doc_string + start,
2595 to - start);
2596 }
2597 /* Look in prev_saved_doc_string the same way. */
2598 else if (pos >= prev_saved_doc_string_position
2599 && pos < (prev_saved_doc_string_position
2600 + prev_saved_doc_string_length))
2601 {
2602 int start = pos - prev_saved_doc_string_position;
2603 int from, to;
2604
2605 /* Process quoting with ^A,
2606 and find the end of the string,
2607 which is marked with ^_ (037). */
2608 for (from = start, to = start;
2609 prev_saved_doc_string[from] != 037;)
2610 {
2611 int c = prev_saved_doc_string[from++];
2612 if (c == 1)
2613 {
2614 c = prev_saved_doc_string[from++];
2615 if (c == 1)
2616 prev_saved_doc_string[to++] = c;
2617 else if (c == '0')
2618 prev_saved_doc_string[to++] = 0;
2619 else if (c == '_')
2620 prev_saved_doc_string[to++] = 037;
2621 }
2622 else
2623 prev_saved_doc_string[to++] = c;
2624 }
2625
2626 return make_string (prev_saved_doc_string + start,
2627 to - start);
2628 }
2629 else
2630 return get_doc_string (val, 0, 0);
2631 }
2632
2633 return val;
2634 }
2635 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2636 }
2637 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2638 }
2639 tem = (read_pure && flag <= 0
2640 ? pure_cons (elt, Qnil)
2641 : Fcons (elt, Qnil));
2642 if (!NILP (tail))
2643 XCDR (tail) = tem;
2644 else
2645 val = tem;
2646 tail = tem;
2647 if (defunflag < 0)
2648 defunflag = EQ (elt, Qdefun);
2649 else if (defunflag > 0)
2650 read_pure = 1;
2651 }
2652 }
2653 \f
2654 Lisp_Object Vobarray;
2655 Lisp_Object initial_obarray;
2656
2657 /* oblookup stores the bucket number here, for the sake of Funintern. */
2658
2659 int oblookup_last_bucket_number;
2660
2661 static int hash_string ();
2662 Lisp_Object oblookup ();
2663
2664 /* Get an error if OBARRAY is not an obarray.
2665 If it is one, return it. */
2666
2667 Lisp_Object
2668 check_obarray (obarray)
2669 Lisp_Object obarray;
2670 {
2671 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2672 {
2673 /* If Vobarray is now invalid, force it to be valid. */
2674 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2675
2676 obarray = wrong_type_argument (Qvectorp, obarray);
2677 }
2678 return obarray;
2679 }
2680
2681 /* Intern the C string STR: return a symbol with that name,
2682 interned in the current obarray. */
2683
2684 Lisp_Object
2685 intern (str)
2686 char *str;
2687 {
2688 Lisp_Object tem;
2689 int len = strlen (str);
2690 Lisp_Object obarray;
2691
2692 obarray = Vobarray;
2693 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2694 obarray = check_obarray (obarray);
2695 tem = oblookup (obarray, str, len, len);
2696 if (SYMBOLP (tem))
2697 return tem;
2698 return Fintern (make_string (str, len), obarray);
2699 }
2700
2701 /* Create an uninterned symbol with name STR. */
2702
2703 Lisp_Object
2704 make_symbol (str)
2705 char *str;
2706 {
2707 int len = strlen (str);
2708
2709 return Fmake_symbol ((!NILP (Vpurify_flag)
2710 ? make_pure_string (str, len, len, 0)
2711 : make_string (str, len)));
2712 }
2713 \f
2714 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2715 "Return the canonical symbol whose name is STRING.\n\
2716 If there is none, one is created by this function and returned.\n\
2717 A second optional argument specifies the obarray to use;\n\
2718 it defaults to the value of `obarray'.")
2719 (string, obarray)
2720 Lisp_Object string, obarray;
2721 {
2722 register Lisp_Object tem, sym, *ptr;
2723
2724 if (NILP (obarray)) obarray = Vobarray;
2725 obarray = check_obarray (obarray);
2726
2727 CHECK_STRING (string, 0);
2728
2729 tem = oblookup (obarray, XSTRING (string)->data,
2730 XSTRING (string)->size,
2731 STRING_BYTES (XSTRING (string)));
2732 if (!INTEGERP (tem))
2733 return tem;
2734
2735 if (!NILP (Vpurify_flag))
2736 string = Fpurecopy (string);
2737 sym = Fmake_symbol (string);
2738 XSYMBOL (sym)->obarray = obarray;
2739
2740 if ((XSTRING (string)->data[0] == ':')
2741 && EQ (obarray, initial_obarray))
2742 XSYMBOL (sym)->value = sym;
2743
2744 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2745 if (SYMBOLP (*ptr))
2746 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2747 else
2748 XSYMBOL (sym)->next = 0;
2749 *ptr = sym;
2750 return sym;
2751 }
2752
2753 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2754 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
2755 A second optional argument specifies the obarray to use;\n\
2756 it defaults to the value of `obarray'.")
2757 (string, obarray)
2758 Lisp_Object string, obarray;
2759 {
2760 register Lisp_Object tem;
2761
2762 if (NILP (obarray)) obarray = Vobarray;
2763 obarray = check_obarray (obarray);
2764
2765 CHECK_STRING (string, 0);
2766
2767 tem = oblookup (obarray, XSTRING (string)->data,
2768 XSTRING (string)->size,
2769 STRING_BYTES (XSTRING (string)));
2770 if (!INTEGERP (tem))
2771 return tem;
2772 return Qnil;
2773 }
2774 \f
2775 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2776 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2777 The value is t if a symbol was found and deleted, nil otherwise.\n\
2778 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2779 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2780 OBARRAY defaults to the value of the variable `obarray'.")
2781 (name, obarray)
2782 Lisp_Object name, obarray;
2783 {
2784 register Lisp_Object string, tem;
2785 int hash;
2786
2787 if (NILP (obarray)) obarray = Vobarray;
2788 obarray = check_obarray (obarray);
2789
2790 if (SYMBOLP (name))
2791 XSETSTRING (string, XSYMBOL (name)->name);
2792 else
2793 {
2794 CHECK_STRING (name, 0);
2795 string = name;
2796 }
2797
2798 tem = oblookup (obarray, XSTRING (string)->data,
2799 XSTRING (string)->size,
2800 STRING_BYTES (XSTRING (string)));
2801 if (INTEGERP (tem))
2802 return Qnil;
2803 /* If arg was a symbol, don't delete anything but that symbol itself. */
2804 if (SYMBOLP (name) && !EQ (name, tem))
2805 return Qnil;
2806
2807 XSYMBOL (tem)->obarray = Qnil;
2808
2809 hash = oblookup_last_bucket_number;
2810
2811 if (EQ (XVECTOR (obarray)->contents[hash], tem))
2812 {
2813 if (XSYMBOL (tem)->next)
2814 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
2815 else
2816 XSETINT (XVECTOR (obarray)->contents[hash], 0);
2817 }
2818 else
2819 {
2820 Lisp_Object tail, following;
2821
2822 for (tail = XVECTOR (obarray)->contents[hash];
2823 XSYMBOL (tail)->next;
2824 tail = following)
2825 {
2826 XSETSYMBOL (following, XSYMBOL (tail)->next);
2827 if (EQ (following, tem))
2828 {
2829 XSYMBOL (tail)->next = XSYMBOL (following)->next;
2830 break;
2831 }
2832 }
2833 }
2834
2835 return Qt;
2836 }
2837 \f
2838 /* Return the symbol in OBARRAY whose names matches the string
2839 of SIZE characters (SIZE_BYTE bytes) at PTR.
2840 If there is no such symbol in OBARRAY, return nil.
2841
2842 Also store the bucket number in oblookup_last_bucket_number. */
2843
2844 Lisp_Object
2845 oblookup (obarray, ptr, size, size_byte)
2846 Lisp_Object obarray;
2847 register char *ptr;
2848 int size, size_byte;
2849 {
2850 int hash;
2851 int obsize;
2852 register Lisp_Object tail;
2853 Lisp_Object bucket, tem;
2854
2855 if (!VECTORP (obarray)
2856 || (obsize = XVECTOR (obarray)->size) == 0)
2857 {
2858 obarray = check_obarray (obarray);
2859 obsize = XVECTOR (obarray)->size;
2860 }
2861 /* This is sometimes needed in the middle of GC. */
2862 obsize &= ~ARRAY_MARK_FLAG;
2863 /* Combining next two lines breaks VMS C 2.3. */
2864 hash = hash_string (ptr, size_byte);
2865 hash %= obsize;
2866 bucket = XVECTOR (obarray)->contents[hash];
2867 oblookup_last_bucket_number = hash;
2868 if (XFASTINT (bucket) == 0)
2869 ;
2870 else if (!SYMBOLP (bucket))
2871 error ("Bad data in guts of obarray"); /* Like CADR error message */
2872 else
2873 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
2874 {
2875 if (STRING_BYTES (XSYMBOL (tail)->name) == size_byte
2876 && XSYMBOL (tail)->name->size == size
2877 && !bcmp (XSYMBOL (tail)->name->data, ptr, size_byte))
2878 return tail;
2879 else if (XSYMBOL (tail)->next == 0)
2880 break;
2881 }
2882 XSETINT (tem, hash);
2883 return tem;
2884 }
2885
2886 static int
2887 hash_string (ptr, len)
2888 unsigned char *ptr;
2889 int len;
2890 {
2891 register unsigned char *p = ptr;
2892 register unsigned char *end = p + len;
2893 register unsigned char c;
2894 register int hash = 0;
2895
2896 while (p != end)
2897 {
2898 c = *p++;
2899 if (c >= 0140) c -= 40;
2900 hash = ((hash<<3) + (hash>>28) + c);
2901 }
2902 return hash & 07777777777;
2903 }
2904 \f
2905 void
2906 map_obarray (obarray, fn, arg)
2907 Lisp_Object obarray;
2908 void (*fn) P_ ((Lisp_Object, Lisp_Object));
2909 Lisp_Object arg;
2910 {
2911 register int i;
2912 register Lisp_Object tail;
2913 CHECK_VECTOR (obarray, 1);
2914 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
2915 {
2916 tail = XVECTOR (obarray)->contents[i];
2917 if (SYMBOLP (tail))
2918 while (1)
2919 {
2920 (*fn) (tail, arg);
2921 if (XSYMBOL (tail)->next == 0)
2922 break;
2923 XSETSYMBOL (tail, XSYMBOL (tail)->next);
2924 }
2925 }
2926 }
2927
2928 void
2929 mapatoms_1 (sym, function)
2930 Lisp_Object sym, function;
2931 {
2932 call1 (function, sym);
2933 }
2934
2935 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
2936 "Call FUNCTION on every symbol in OBARRAY.\n\
2937 OBARRAY defaults to the value of `obarray'.")
2938 (function, obarray)
2939 Lisp_Object function, obarray;
2940 {
2941 if (NILP (obarray)) obarray = Vobarray;
2942 obarray = check_obarray (obarray);
2943
2944 map_obarray (obarray, mapatoms_1, function);
2945 return Qnil;
2946 }
2947
2948 #define OBARRAY_SIZE 1511
2949
2950 void
2951 init_obarray ()
2952 {
2953 Lisp_Object oblength;
2954 int hash;
2955 Lisp_Object *tem;
2956
2957 XSETFASTINT (oblength, OBARRAY_SIZE);
2958
2959 Qnil = Fmake_symbol (make_pure_string ("nil", 3, 3, 0));
2960 Vobarray = Fmake_vector (oblength, make_number (0));
2961 initial_obarray = Vobarray;
2962 staticpro (&initial_obarray);
2963 /* Intern nil in the obarray */
2964 XSYMBOL (Qnil)->obarray = Vobarray;
2965 /* These locals are to kludge around a pyramid compiler bug. */
2966 hash = hash_string ("nil", 3);
2967 /* Separate statement here to avoid VAXC bug. */
2968 hash %= OBARRAY_SIZE;
2969 tem = &XVECTOR (Vobarray)->contents[hash];
2970 *tem = Qnil;
2971
2972 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7, 7, 0));
2973 XSYMBOL (Qnil)->function = Qunbound;
2974 XSYMBOL (Qunbound)->value = Qunbound;
2975 XSYMBOL (Qunbound)->function = Qunbound;
2976
2977 Qt = intern ("t");
2978 XSYMBOL (Qnil)->value = Qnil;
2979 XSYMBOL (Qnil)->plist = Qnil;
2980 XSYMBOL (Qt)->value = Qt;
2981
2982 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
2983 Vpurify_flag = Qt;
2984
2985 Qvariable_documentation = intern ("variable-documentation");
2986 staticpro (&Qvariable_documentation);
2987
2988 read_buffer_size = 100 + MAX_LENGTH_OF_MULTI_BYTE_FORM;
2989 read_buffer = (char *) malloc (read_buffer_size);
2990 }
2991 \f
2992 void
2993 defsubr (sname)
2994 struct Lisp_Subr *sname;
2995 {
2996 Lisp_Object sym;
2997 sym = intern (sname->symbol_name);
2998 XSETSUBR (XSYMBOL (sym)->function, sname);
2999 }
3000
3001 #ifdef NOTDEF /* use fset in subr.el now */
3002 void
3003 defalias (sname, string)
3004 struct Lisp_Subr *sname;
3005 char *string;
3006 {
3007 Lisp_Object sym;
3008 sym = intern (string);
3009 XSETSUBR (XSYMBOL (sym)->function, sname);
3010 }
3011 #endif /* NOTDEF */
3012
3013 /* Define an "integer variable"; a symbol whose value is forwarded
3014 to a C variable of type int. Sample call: */
3015 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
3016 void
3017 defvar_int (namestring, address)
3018 char *namestring;
3019 int *address;
3020 {
3021 Lisp_Object sym, val;
3022 sym = intern (namestring);
3023 val = allocate_misc ();
3024 XMISCTYPE (val) = Lisp_Misc_Intfwd;
3025 XINTFWD (val)->intvar = address;
3026 XSYMBOL (sym)->value = val;
3027 }
3028
3029 /* Similar but define a variable whose value is T if address contains 1,
3030 NIL if address contains 0 */
3031 void
3032 defvar_bool (namestring, address)
3033 char *namestring;
3034 int *address;
3035 {
3036 Lisp_Object sym, val;
3037 sym = intern (namestring);
3038 val = allocate_misc ();
3039 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
3040 XBOOLFWD (val)->boolvar = address;
3041 XSYMBOL (sym)->value = val;
3042 Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
3043 }
3044
3045 /* Similar but define a variable whose value is the Lisp Object stored
3046 at address. Two versions: with and without gc-marking of the C
3047 variable. The nopro version is used when that variable will be
3048 gc-marked for some other reason, since marking the same slot twice
3049 can cause trouble with strings. */
3050 void
3051 defvar_lisp_nopro (namestring, address)
3052 char *namestring;
3053 Lisp_Object *address;
3054 {
3055 Lisp_Object sym, val;
3056 sym = intern (namestring);
3057 val = allocate_misc ();
3058 XMISCTYPE (val) = Lisp_Misc_Objfwd;
3059 XOBJFWD (val)->objvar = address;
3060 XSYMBOL (sym)->value = val;
3061 }
3062
3063 void
3064 defvar_lisp (namestring, address)
3065 char *namestring;
3066 Lisp_Object *address;
3067 {
3068 defvar_lisp_nopro (namestring, address);
3069 staticpro (address);
3070 }
3071
3072 #ifndef standalone
3073
3074 /* Similar but define a variable whose value is the Lisp Object stored in
3075 the current buffer. address is the address of the slot in the buffer
3076 that is current now. */
3077
3078 void
3079 defvar_per_buffer (namestring, address, type, doc)
3080 char *namestring;
3081 Lisp_Object *address;
3082 Lisp_Object type;
3083 char *doc;
3084 {
3085 Lisp_Object sym, val;
3086 int offset;
3087 extern struct buffer buffer_local_symbols;
3088
3089 sym = intern (namestring);
3090 val = allocate_misc ();
3091 offset = (char *)address - (char *)current_buffer;
3092
3093 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
3094 XBUFFER_OBJFWD (val)->offset = offset;
3095 XSYMBOL (sym)->value = val;
3096 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
3097 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
3098 if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0)
3099 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
3100 slot of buffer_local_flags */
3101 abort ();
3102 }
3103
3104 #endif /* standalone */
3105
3106 /* Similar but define a variable whose value is the Lisp Object stored
3107 at a particular offset in the current kboard object. */
3108
3109 void
3110 defvar_kboard (namestring, offset)
3111 char *namestring;
3112 int offset;
3113 {
3114 Lisp_Object sym, val;
3115 sym = intern (namestring);
3116 val = allocate_misc ();
3117 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
3118 XKBOARD_OBJFWD (val)->offset = offset;
3119 XSYMBOL (sym)->value = val;
3120 }
3121 \f
3122 /* Record the value of load-path used at the start of dumping
3123 so we can see if the site changed it later during dumping. */
3124 static Lisp_Object dump_path;
3125
3126 void
3127 init_lread ()
3128 {
3129 char *normal;
3130 int turn_off_warning = 0;
3131
3132 #ifdef HAVE_SETLOCALE
3133 /* Make sure numbers are parsed as we expect. */
3134 setlocale (LC_NUMERIC, "C");
3135 #endif /* HAVE_SETLOCALE */
3136
3137 /* Compute the default load-path. */
3138 #ifdef CANNOT_DUMP
3139 normal = PATH_LOADSEARCH;
3140 Vload_path = decode_env_path (0, normal);
3141 #else
3142 if (NILP (Vpurify_flag))
3143 normal = PATH_LOADSEARCH;
3144 else
3145 normal = PATH_DUMPLOADSEARCH;
3146
3147 /* In a dumped Emacs, we normally have to reset the value of
3148 Vload_path from PATH_LOADSEARCH, since the value that was dumped
3149 uses ../lisp, instead of the path of the installed elisp
3150 libraries. However, if it appears that Vload_path was changed
3151 from the default before dumping, don't override that value. */
3152 if (initialized)
3153 {
3154 if (! NILP (Fequal (dump_path, Vload_path)))
3155 {
3156 Vload_path = decode_env_path (0, normal);
3157 if (!NILP (Vinstallation_directory))
3158 {
3159 /* Add to the path the lisp subdir of the
3160 installation dir, if it exists. */
3161 Lisp_Object tem, tem1;
3162 tem = Fexpand_file_name (build_string ("lisp"),
3163 Vinstallation_directory);
3164 tem1 = Ffile_exists_p (tem);
3165 if (!NILP (tem1))
3166 {
3167 if (NILP (Fmember (tem, Vload_path)))
3168 {
3169 turn_off_warning = 1;
3170 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3171 }
3172 }
3173 else
3174 /* That dir doesn't exist, so add the build-time
3175 Lisp dirs instead. */
3176 Vload_path = nconc2 (Vload_path, dump_path);
3177
3178 /* Add leim under the installation dir, if it exists. */
3179 tem = Fexpand_file_name (build_string ("leim"),
3180 Vinstallation_directory);
3181 tem1 = Ffile_exists_p (tem);
3182 if (!NILP (tem1))
3183 {
3184 if (NILP (Fmember (tem, Vload_path)))
3185 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3186 }
3187
3188 /* Add site-list under the installation dir, if it exists. */
3189 tem = Fexpand_file_name (build_string ("site-lisp"),
3190 Vinstallation_directory);
3191 tem1 = Ffile_exists_p (tem);
3192 if (!NILP (tem1))
3193 {
3194 if (NILP (Fmember (tem, Vload_path)))
3195 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3196 }
3197
3198 /* If Emacs was not built in the source directory,
3199 and it is run from where it was built, add to load-path
3200 the lisp, leim and site-lisp dirs under that directory. */
3201
3202 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
3203 {
3204 Lisp_Object tem2;
3205
3206 tem = Fexpand_file_name (build_string ("src/Makefile"),
3207 Vinstallation_directory);
3208 tem1 = Ffile_exists_p (tem);
3209
3210 /* Don't be fooled if they moved the entire source tree
3211 AFTER dumping Emacs. If the build directory is indeed
3212 different from the source dir, src/Makefile.in and
3213 src/Makefile will not be found together. */
3214 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
3215 Vinstallation_directory);
3216 tem2 = Ffile_exists_p (tem);
3217 if (!NILP (tem1) && NILP (tem2))
3218 {
3219 tem = Fexpand_file_name (build_string ("lisp"),
3220 Vsource_directory);
3221
3222 if (NILP (Fmember (tem, Vload_path)))
3223 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3224
3225 tem = Fexpand_file_name (build_string ("leim"),
3226 Vsource_directory);
3227
3228 if (NILP (Fmember (tem, Vload_path)))
3229 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3230
3231 tem = Fexpand_file_name (build_string ("site-lisp"),
3232 Vsource_directory);
3233
3234 if (NILP (Fmember (tem, Vload_path)))
3235 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
3236 }
3237 }
3238 }
3239 }
3240 }
3241 else
3242 {
3243 /* NORMAL refers to the lisp dir in the source directory. */
3244 /* We used to add ../lisp at the front here, but
3245 that caused trouble because it was copied from dump_path
3246 into Vload_path, aboe, when Vinstallation_directory was non-nil.
3247 It should be unnecessary. */
3248 Vload_path = decode_env_path (0, normal);
3249 dump_path = Vload_path;
3250 }
3251 #endif
3252
3253 #ifndef WINDOWSNT
3254 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
3255 almost never correct, thereby causing a warning to be printed out that
3256 confuses users. Since PATH_LOADSEARCH is always overridden by the
3257 EMACSLOADPATH environment variable below, disable the warning on NT. */
3258
3259 /* Warn if dirs in the *standard* path don't exist. */
3260 if (!turn_off_warning)
3261 {
3262 Lisp_Object path_tail;
3263
3264 for (path_tail = Vload_path;
3265 !NILP (path_tail);
3266 path_tail = XCDR (path_tail))
3267 {
3268 Lisp_Object dirfile;
3269 dirfile = Fcar (path_tail);
3270 if (STRINGP (dirfile))
3271 {
3272 dirfile = Fdirectory_file_name (dirfile);
3273 if (access (XSTRING (dirfile)->data, 0) < 0)
3274 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
3275 XCAR (path_tail));
3276 }
3277 }
3278 }
3279 #endif /* WINDOWSNT */
3280
3281 /* If the EMACSLOADPATH environment variable is set, use its value.
3282 This doesn't apply if we're dumping. */
3283 #ifndef CANNOT_DUMP
3284 if (NILP (Vpurify_flag)
3285 && egetenv ("EMACSLOADPATH"))
3286 #endif
3287 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
3288
3289 Vvalues = Qnil;
3290
3291 load_in_progress = 0;
3292 Vload_file_name = Qnil;
3293
3294 load_descriptor_list = Qnil;
3295
3296 Vstandard_input = Qt;
3297 }
3298
3299 /* Print a warning, using format string FORMAT, that directory DIRNAME
3300 does not exist. Print it on stderr and put it in *Message*. */
3301
3302 void
3303 dir_warning (format, dirname)
3304 char *format;
3305 Lisp_Object dirname;
3306 {
3307 char *buffer
3308 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
3309
3310 fprintf (stderr, format, XSTRING (dirname)->data);
3311 sprintf (buffer, format, XSTRING (dirname)->data);
3312 /* Don't log the warning before we've initialized!! */
3313 if (initialized)
3314 message_dolog (buffer, strlen (buffer), 0, STRING_MULTIBYTE (dirname));
3315 }
3316
3317 void
3318 syms_of_lread ()
3319 {
3320 defsubr (&Sread);
3321 defsubr (&Sread_from_string);
3322 defsubr (&Sintern);
3323 defsubr (&Sintern_soft);
3324 defsubr (&Sunintern);
3325 defsubr (&Sload);
3326 defsubr (&Seval_buffer);
3327 defsubr (&Seval_region);
3328 defsubr (&Sread_char);
3329 defsubr (&Sread_char_exclusive);
3330 defsubr (&Sread_event);
3331 defsubr (&Sget_file_char);
3332 defsubr (&Smapatoms);
3333
3334 DEFVAR_LISP ("obarray", &Vobarray,
3335 "Symbol table for use by `intern' and `read'.\n\
3336 It is a vector whose length ought to be prime for best results.\n\
3337 The vector's contents don't make sense if examined from Lisp programs;\n\
3338 to find all the symbols in an obarray, use `mapatoms'.");
3339
3340 DEFVAR_LISP ("values", &Vvalues,
3341 "List of values of all expressions which were read, evaluated and printed.\n\
3342 Order is reverse chronological.");
3343
3344 DEFVAR_LISP ("standard-input", &Vstandard_input,
3345 "Stream for read to get input from.\n\
3346 See documentation of `read' for possible values.");
3347 Vstandard_input = Qt;
3348
3349 DEFVAR_LISP ("load-path", &Vload_path,
3350 "*List of directories to search for files to load.\n\
3351 Each element is a string (directory name) or nil (try default directory).\n\
3352 Initialized based on EMACSLOADPATH environment variable, if any,\n\
3353 otherwise to default specified by file `epaths.h' when Emacs was built.");
3354
3355 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
3356 "Non-nil iff inside of `load'.");
3357
3358 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
3359 "An alist of expressions to be evalled when particular files are loaded.\n\
3360 Each element looks like (FILENAME FORMS...).\n\
3361 When `load' is run and the file-name argument is FILENAME,\n\
3362 the FORMS in the corresponding element are executed at the end of loading.\n\n\
3363 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
3364 with no directory specified, since that is how `load' is normally called.\n\
3365 An error in FORMS does not undo the load,\n\
3366 but does prevent execution of the rest of the FORMS.");
3367 Vafter_load_alist = Qnil;
3368
3369 DEFVAR_LISP ("load-history", &Vload_history,
3370 "Alist mapping source file names to symbols and features.\n\
3371 Each alist element is a list that starts with a file name,\n\
3372 except for one element (optional) that starts with nil and describes\n\
3373 definitions evaluated from buffers not visiting files.\n\
3374 The remaining elements of each list are symbols defined as functions\n\
3375 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
3376 Vload_history = Qnil;
3377
3378 DEFVAR_LISP ("load-file-name", &Vload_file_name,
3379 "Full name of file being loaded by `load'.");
3380 Vload_file_name = Qnil;
3381
3382 DEFVAR_LISP ("user-init-file", &Vuser_init_file,
3383 "File name, including directory, of user's initialization file.");
3384 Vuser_init_file = Qnil;
3385
3386 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
3387 "Used for internal purposes by `load'.");
3388 Vcurrent_load_list = Qnil;
3389
3390 DEFVAR_LISP ("load-read-function", &Vload_read_function,
3391 "Function used by `load' and `eval-region' for reading expressions.\n\
3392 The default is nil, which means use the function `read'.");
3393 Vload_read_function = Qnil;
3394
3395 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
3396 "Function called in `load' for loading an Emacs lisp source file.\n\
3397 This function is for doing code conversion before reading the source file.\n\
3398 If nil, loading is done without any code conversion.\n\
3399 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
3400 FULLNAME is the full name of FILE.\n\
3401 See `load' for the meaning of the remaining arguments.");
3402 Vload_source_file_function = Qnil;
3403
3404 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
3405 "Non-nil means `load' should force-load all dynamic doc strings.\n\
3406 This is useful when the file being loaded is a temporary copy.");
3407 load_force_doc_strings = 0;
3408
3409 DEFVAR_BOOL ("load-convert-to-unibyte", &load_convert_to_unibyte,
3410 "Non-nil means `load' converts strings to unibyte whenever possible.\n\
3411 This is normally used in `load-with-code-conversion'\n\
3412 for loading non-compiled files.");
3413 load_convert_to_unibyte = 0;
3414
3415 DEFVAR_LISP ("source-directory", &Vsource_directory,
3416 "Directory in which Emacs sources were found when Emacs was built.\n\
3417 You cannot count on them to still be there!");
3418 Vsource_directory
3419 = Fexpand_file_name (build_string ("../"),
3420 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
3421
3422 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
3423 "List of files that were preloaded (when dumping Emacs).");
3424 Vpreloaded_file_list = Qnil;
3425
3426 DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
3427 "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
3428 Vbyte_boolean_vars = Qnil;
3429
3430 /* Vsource_directory was initialized in init_lread. */
3431
3432 load_descriptor_list = Qnil;
3433 staticpro (&load_descriptor_list);
3434
3435 Qcurrent_load_list = intern ("current-load-list");
3436 staticpro (&Qcurrent_load_list);
3437
3438 Qstandard_input = intern ("standard-input");
3439 staticpro (&Qstandard_input);
3440
3441 Qread_char = intern ("read-char");
3442 staticpro (&Qread_char);
3443
3444 Qget_file_char = intern ("get-file-char");
3445 staticpro (&Qget_file_char);
3446
3447 Qbackquote = intern ("`");
3448 staticpro (&Qbackquote);
3449 Qcomma = intern (",");
3450 staticpro (&Qcomma);
3451 Qcomma_at = intern (",@");
3452 staticpro (&Qcomma_at);
3453 Qcomma_dot = intern (",.");
3454 staticpro (&Qcomma_dot);
3455
3456 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
3457 staticpro (&Qinhibit_file_name_operation);
3458
3459 Qascii_character = intern ("ascii-character");
3460 staticpro (&Qascii_character);
3461
3462 Qfunction = intern ("function");
3463 staticpro (&Qfunction);
3464
3465 Qload = intern ("load");
3466 staticpro (&Qload);
3467
3468 Qload_file_name = intern ("load-file-name");
3469 staticpro (&Qload_file_name);
3470
3471 staticpro (&dump_path);
3472
3473 staticpro (&read_objects);
3474 read_objects = Qnil;
3475 staticpro (&seen_list);
3476
3477 }