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