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