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