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