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