Put stdio.h, sys/types.h and sys/stat.h after config.h.
[bpt/emacs.git] / src / lread.c
CommitLineData
078e7b4a 1/* Lisp parsing and input streams.
73aa9704 2 Copyright (C) 1985, 1986, 1987, 1988, 1989,
3a22ee35 3 1993, 1994 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
22#include <stdio.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <sys/file.h>
12e94926 26#include <ctype.h>
18160b98 27#include <config.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"
6ba93f9d 53/* These are redefined (correctly, but differently) in values.h. */
23a71bd6
RS
54#undef INTBITS
55#undef LONGBITS
56#undef SHORTBITS
57#endif
23a71bd6 58
078e7b4a
JB
59#include <math.h>
60#endif /* LISP_FLOAT_TYPE */
61
8a1f1537 62Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
078e7b4a 63Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
c2225d00 64Lisp_Object Qascii_character, Qload;
7bd279cd
RS
65
66extern Lisp_Object Qevent_symbol_element_mask;
078e7b4a
JB
67
68/* non-zero if inside `load' */
69int load_in_progress;
70
71/* Search path for files to be loaded. */
72Lisp_Object Vload_path;
73
ae321d28
RS
74/* This is the user-visible association list that maps features to
75 lists of defs in their load files. */
76Lisp_Object Vload_history;
77
78/* This is useud to build the load history. */
79Lisp_Object Vcurrent_load_list;
80
d2c6be7f
RS
81/* List of descriptors now open for Fload. */
82static Lisp_Object load_descriptor_list;
83
078e7b4a
JB
84/* File for get_file_char to read from. Use by load */
85static FILE *instream;
86
87/* When nonzero, read conses in pure space */
88static int read_pure;
89
90/* For use within read-from-string (this reader is non-reentrant!!) */
91static int read_from_string_index;
92static int read_from_string_limit;
93\f
94/* Handle unreading and rereading of characters.
95 Write READCHAR to read a character,
96 UNREAD(c) to unread c to be read again. */
97
98#define READCHAR readchar (readcharfun)
99#define UNREAD(c) unreadchar (readcharfun, c)
100
101static int
102readchar (readcharfun)
103 Lisp_Object readcharfun;
104{
105 Lisp_Object tem;
106 register struct buffer *inbuffer;
107 register int c, mpos;
108
109 if (XTYPE (readcharfun) == Lisp_Buffer)
110 {
111 inbuffer = XBUFFER (readcharfun);
112
113 if (BUF_PT (inbuffer) >= BUF_ZV (inbuffer))
114 return -1;
115 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, BUF_PT (inbuffer));
116 SET_BUF_PT (inbuffer, BUF_PT (inbuffer) + 1);
117
118 return c;
119 }
120 if (XTYPE (readcharfun) == Lisp_Marker)
121 {
122 inbuffer = XMARKER (readcharfun)->buffer;
123
124 mpos = marker_position (readcharfun);
125
126 if (mpos > BUF_ZV (inbuffer) - 1)
127 return -1;
128 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, mpos);
129 if (mpos != BUF_GPT (inbuffer))
130 XMARKER (readcharfun)->bufpos++;
131 else
132 Fset_marker (readcharfun, make_number (mpos + 1),
133 Fmarker_buffer (readcharfun));
134 return c;
135 }
136 if (EQ (readcharfun, Qget_file_char))
137 return getc (instream);
138
139 if (XTYPE (readcharfun) == Lisp_String)
140 {
141 register int c;
142 /* This used to be return of a conditional expression,
143 but that truncated -1 to a char on VMS. */
144 if (read_from_string_index < read_from_string_limit)
145 c = XSTRING (readcharfun)->data[read_from_string_index++];
146 else
147 c = -1;
148 return c;
149 }
150
151 tem = call0 (readcharfun);
152
265a9e55 153 if (NILP (tem))
078e7b4a
JB
154 return -1;
155 return XINT (tem);
156}
157
158/* Unread the character C in the way appropriate for the stream READCHARFUN.
159 If the stream is a user function, call it with the char as argument. */
160
161static void
162unreadchar (readcharfun, c)
163 Lisp_Object readcharfun;
164 int c;
165{
92fddec9
KH
166 if (c == -1)
167 /* Don't back up the pointer if we're unreading the end-of-input mark,
168 since readchar didn't advance it when we read it. */
169 ;
170 else if (XTYPE (readcharfun) == Lisp_Buffer)
078e7b4a
JB
171 {
172 if (XBUFFER (readcharfun) == current_buffer)
173 SET_PT (point - 1);
174 else
175 SET_BUF_PT (XBUFFER (readcharfun), BUF_PT (XBUFFER (readcharfun)) - 1);
176 }
177 else if (XTYPE (readcharfun) == Lisp_Marker)
178 XMARKER (readcharfun)->bufpos--;
179 else if (XTYPE (readcharfun) == Lisp_String)
180 read_from_string_index--;
181 else if (EQ (readcharfun, Qget_file_char))
182 ungetc (c, instream);
183 else
184 call1 (readcharfun, make_number (c));
185}
186
187static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
188\f
189/* get a character from the tty */
190
3d9b22be
JB
191extern Lisp_Object read_char ();
192
f42be754
JB
193/* Read input events until we get one that's acceptable for our purposes.
194
195 If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
196 until we get a character we like, and then stuffed into
197 unread_switch_frame.
198
199 If ASCII_REQUIRED is non-zero, we check function key events to see
200 if the unmodified version of the symbol has a Qascii_character
201 property, and use that character, if present.
202
203 If ERROR_NONASCII is non-zero, we signal an error if the input we
204 get isn't an ASCII character with modifiers. If it's zero but
205 ASCII_REQUIRED is non-zero, we just re-read until we get an ASCII
206 character. */
207Lisp_Object
208read_filtered_event (no_switch_frame, ascii_required, error_nonascii)
209 int no_switch_frame, ascii_required, error_nonascii;
210{
211#ifdef standalone
212 return make_number (getchar ());
213#else
153a17b7
KH
214 register Lisp_Object val, delayed_switch_frame;
215
216 delayed_switch_frame = Qnil;
f42be754
JB
217
218 /* Read until we get an acceptable event. */
219 retry:
220 val = read_char (0, 0, 0, Qnil, 0);
221
6c82d689
KH
222 if (XTYPE (val) == Lisp_Buffer)
223 goto retry;
224
f42be754
JB
225 /* switch-frame events are put off until after the next ASCII
226 character. This is better than signalling an error just because
227 the last characters were typed to a separate minibuffer frame,
228 for example. Eventually, some code which can deal with
229 switch-frame events will read it and process it. */
230 if (no_switch_frame
231 && EVENT_HAS_PARAMETERS (val)
232 && EQ (EVENT_HEAD (val), Qswitch_frame))
233 {
234 delayed_switch_frame = val;
235 goto retry;
236 }
237
238 if (ascii_required)
239 {
240 /* Convert certain symbols to their ASCII equivalents. */
241 if (XTYPE (val) == Lisp_Symbol)
242 {
243 Lisp_Object tem, tem1, tem2;
244 tem = Fget (val, Qevent_symbol_element_mask);
245 if (!NILP (tem))
246 {
247 tem1 = Fget (Fcar (tem), Qascii_character);
248 /* Merge this symbol's modifier bits
249 with the ASCII equivalent of its basic code. */
250 if (!NILP (tem1))
251 XFASTINT (val) = XINT (tem1) | XINT (Fcar (Fcdr (tem)));
252 }
253 }
254
255 /* If we don't have a character now, deal with it appropriately. */
256 if (XTYPE (val) != Lisp_Int)
257 {
258 if (error_nonascii)
259 {
1ec84625 260 Vunread_command_events = Fcons (val, Qnil);
f42be754
JB
261 error ("Non-character input-event");
262 }
263 else
264 goto retry;
265 }
266 }
267
268 if (! NILP (delayed_switch_frame))
269 unread_switch_frame = delayed_switch_frame;
270
271 return val;
272#endif
273}
274
078e7b4a
JB
275DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0,
276 "Read a character from the command input (keyboard or macro).\n\
e51e47f7
JB
277It is returned as a number.\n\
278If the user generates an event which is not a character (i.e. a mouse\n\
e37c0805
JB
279click or function key event), `read-char' signals an error. As an\n\
280exception, switch-frame events are put off until non-ASCII events can\n\
281be read.\n\
282If you want to read non-character events, or ignore them, call\n\
283`read-event' or `read-char-exclusive' instead.")
078e7b4a
JB
284 ()
285{
f42be754 286 return read_filtered_event (1, 1, 1);
078e7b4a
JB
287}
288
078e7b4a
JB
289DEFUN ("read-event", Fread_event, Sread_event, 0, 0, 0,
290 "Read an event object from the input stream.")
291 ()
292{
f42be754 293 return read_filtered_event (0, 0, 0);
078e7b4a
JB
294}
295
296DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 0, 0,
297 "Read a character from the command input (keyboard or macro).\n\
298It is returned as a number. Non character events are ignored.")
299 ()
300{
f42be754 301 return read_filtered_event (1, 1, 0);
078e7b4a 302}
078e7b4a
JB
303
304DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
305 "Don't use this yourself.")
306 ()
307{
308 register Lisp_Object val;
309 XSET (val, Lisp_Int, getc (instream));
310 return val;
311}
312\f
313static void readevalloop ();
314static Lisp_Object load_unwind ();
d2c6be7f 315static Lisp_Object load_descriptor_unwind ();
078e7b4a
JB
316
317DEFUN ("load", Fload, Sload, 1, 4, 0,
318 "Execute a file of Lisp code named FILE.\n\
319First try FILE with `.elc' appended, then try with `.el',\n\
320 then try FILE unmodified.\n\
321This function searches the directories in `load-path'.\n\
322If optional second arg NOERROR is non-nil,\n\
323 report no error if FILE doesn't exist.\n\
324Print messages at start and end of loading unless\n\
325 optional third arg NOMESSAGE is non-nil.\n\
326If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
327 suffixes `.elc' or `.el' to the specified name FILE.\n\
328Return t if file exists.")
329 (str, noerror, nomessage, nosuffix)
330 Lisp_Object str, noerror, nomessage, nosuffix;
331{
332 register FILE *stream;
333 register int fd = -1;
334 register Lisp_Object lispstream;
335 register FILE **ptr;
336 int count = specpdl_ptr - specpdl;
337 Lisp_Object temp;
338 struct gcpro gcpro1;
339 Lisp_Object found;
51ac6f83
RS
340 /* 1 means inhibit the message at the beginning. */
341 int nomessage1 = 0;
c2225d00 342 Lisp_Object handler;
23a71bd6
RS
343#ifdef MSDOS
344 char *dosmode = "rt";
345#endif
078e7b4a
JB
346
347 CHECK_STRING (str, 0);
348 str = Fsubstitute_in_file_name (str);
349
c2225d00 350 /* If file name is magic, call the handler. */
a617e913 351 handler = Ffind_file_name_handler (str, Qload);
c2225d00 352 if (!NILP (handler))
8a2c760a 353 return call5 (handler, Qload, str, noerror, nomessage, nosuffix);
c2225d00 354
078e7b4a
JB
355 /* Avoid weird lossage with null string as arg,
356 since it would try to load a directory as a Lisp file */
357 if (XSTRING (str)->size > 0)
358 {
5a6e5452 359 GCPRO1 (str);
265a9e55 360 fd = openp (Vload_path, str, !NILP (nosuffix) ? "" : ".elc:.el:",
078e7b4a 361 &found, 0);
5a6e5452 362 UNGCPRO;
078e7b4a
JB
363 }
364
365 if (fd < 0)
366 {
265a9e55 367 if (NILP (noerror))
078e7b4a
JB
368 while (1)
369 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
370 Fcons (str, Qnil)));
371 else
372 return Qnil;
373 }
374
375 if (!bcmp (&(XSTRING (found)->data[XSTRING (found)->size - 4]),
376 ".elc", 4))
377 {
378 struct stat s1, s2;
379 int result;
380
23a71bd6
RS
381#ifdef MSDOS
382 dosmode = "rb";
383#endif
4ff37b08 384 stat ((char *)XSTRING (found)->data, &s1);
078e7b4a 385 XSTRING (found)->data[XSTRING (found)->size - 1] = 0;
4ff37b08 386 result = stat ((char *)XSTRING (found)->data, &s2);
078e7b4a 387 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
51ac6f83
RS
388 {
389 message ("Source file `%s' newer than byte-compiled file",
390 XSTRING (found)->data);
391 /* Don't immediately overwrite this message. */
392 if (!noninteractive)
393 nomessage1 = 1;
394 }
078e7b4a
JB
395 XSTRING (found)->data[XSTRING (found)->size - 1] = 'c';
396 }
397
23a71bd6
RS
398#ifdef MSDOS
399 close (fd);
400 stream = fopen ((char *) XSTRING (found)->data, dosmode);
401#else
078e7b4a 402 stream = fdopen (fd, "r");
23a71bd6 403#endif
078e7b4a
JB
404 if (stream == 0)
405 {
406 close (fd);
407 error ("Failure to create stdio stream for %s", XSTRING (str)->data);
408 }
409
51ac6f83 410 if (NILP (nomessage) && !nomessage1)
078e7b4a
JB
411 message ("Loading %s...", XSTRING (str)->data);
412
413 GCPRO1 (str);
414 /* We may not be able to store STREAM itself as a Lisp_Object pointer
415 since that is guaranteed to work only for data that has been malloc'd.
416 So malloc a full-size pointer, and record the address of that pointer. */
417 ptr = (FILE **) xmalloc (sizeof (FILE *));
418 *ptr = stream;
419 XSET (lispstream, Lisp_Internal_Stream, (int) ptr);
420 record_unwind_protect (load_unwind, lispstream);
d2c6be7f
RS
421 record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
422 load_descriptor_list
423 = Fcons (make_number (fileno (stream)), load_descriptor_list);
078e7b4a 424 load_in_progress++;
ae321d28 425 readevalloop (Qget_file_char, stream, str, Feval, 0);
078e7b4a
JB
426 unbind_to (count, Qnil);
427
428 /* Run any load-hooks for this file. */
429 temp = Fassoc (str, Vafter_load_alist);
265a9e55 430 if (!NILP (temp))
078e7b4a
JB
431 Fprogn (Fcdr (temp));
432 UNGCPRO;
433
265a9e55 434 if (!noninteractive && NILP (nomessage))
078e7b4a
JB
435 message ("Loading %s...done", XSTRING (str)->data);
436 return Qt;
437}
438
439static Lisp_Object
440load_unwind (stream) /* used as unwind-protect function in load */
441 Lisp_Object stream;
442{
443 fclose (*(FILE **) XSTRING (stream));
9ac0d9e0 444 xfree (XPNTR (stream));
078e7b4a
JB
445 if (--load_in_progress < 0) load_in_progress = 0;
446 return Qnil;
447}
448
d2c6be7f
RS
449static Lisp_Object
450load_descriptor_unwind (oldlist)
451 Lisp_Object oldlist;
452{
453 load_descriptor_list = oldlist;
454}
455
456/* Close all descriptors in use for Floads.
457 This is used when starting a subprocess. */
458
459void
460close_load_descs ()
461{
462 Lisp_Object tail;
463 for (tail = load_descriptor_list; !NILP (tail); tail = XCONS (tail)->cdr)
464 close (XFASTINT (XCONS (tail)->car));
465}
078e7b4a
JB
466\f
467static int
468complete_filename_p (pathname)
469 Lisp_Object pathname;
470{
471 register unsigned char *s = XSTRING (pathname)->data;
472 return (*s == '/'
473#ifdef ALTOS
474 || *s == '@'
475#endif
476#ifdef VMS
477 || index (s, ':')
478#endif /* VMS */
23a71bd6
RS
479#ifdef MSDOS /* MW, May 1993 */
480 || (s[0] != '\0' && s[1] == ':' && s[2] == '/')
481#endif
078e7b4a
JB
482 );
483}
484
485/* Search for a file whose name is STR, looking in directories
486 in the Lisp list PATH, and trying suffixes from SUFFIX.
487 SUFFIX is a string containing possible suffixes separated by colons.
488 On success, returns a file descriptor. On failure, returns -1.
489
490 EXEC_ONLY nonzero means don't open the files,
491 just look for one that is executable. In this case,
492 returns 1 on success.
493
494 If STOREPTR is nonzero, it points to a slot where the name of
495 the file actually found should be stored as a Lisp string.
496 Nil is stored there on failure. */
497
498int
499openp (path, str, suffix, storeptr, exec_only)
500 Lisp_Object path, str;
501 char *suffix;
502 Lisp_Object *storeptr;
503 int exec_only;
504{
505 register int fd;
506 int fn_size = 100;
507 char buf[100];
508 register char *fn = buf;
509 int absolute = 0;
510 int want_size;
511 register Lisp_Object filename;
512 struct stat st;
5a6e5452 513 struct gcpro gcpro1;
078e7b4a 514
5a6e5452 515 GCPRO1 (str);
078e7b4a
JB
516 if (storeptr)
517 *storeptr = Qnil;
518
519 if (complete_filename_p (str))
520 absolute = 1;
521
265a9e55 522 for (; !NILP (path); path = Fcdr (path))
078e7b4a
JB
523 {
524 char *nsuffix;
525
526 filename = Fexpand_file_name (str, Fcar (path));
527 if (!complete_filename_p (filename))
528 /* If there are non-absolute elts in PATH (eg ".") */
529 /* Of course, this could conceivably lose if luser sets
530 default-directory to be something non-absolute... */
531 {
532 filename = Fexpand_file_name (filename, current_buffer->directory);
533 if (!complete_filename_p (filename))
534 /* Give up on this path element! */
535 continue;
536 }
537
538 /* Calculate maximum size of any filename made from
539 this path element/specified file name and any possible suffix. */
540 want_size = strlen (suffix) + XSTRING (filename)->size + 1;
541 if (fn_size < want_size)
542 fn = (char *) alloca (fn_size = 100 + want_size);
543
544 nsuffix = suffix;
545
546 /* Loop over suffixes. */
547 while (1)
548 {
549 char *esuffix = (char *) index (nsuffix, ':');
550 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
551
552 /* Concatenate path element/specified name with the suffix. */
553 strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size);
554 fn[XSTRING (filename)->size] = 0;
555 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
556 strncat (fn, nsuffix, lsuffix);
557
558 /* Ignore file if it's a directory. */
559 if (stat (fn, &st) >= 0
560 && (st.st_mode & S_IFMT) != S_IFDIR)
561 {
562 /* Check that we can access or open it. */
563 if (exec_only)
564 fd = (access (fn, X_OK) == 0) ? 1 : -1;
565 else
92bab011 566 fd = open (fn, 0, 0);
078e7b4a
JB
567
568 if (fd >= 0)
569 {
570 /* We succeeded; return this descriptor and filename. */
571 if (storeptr)
572 *storeptr = build_string (fn);
5a6e5452 573 RETURN_UNGCPRO (fd);
078e7b4a
JB
574 }
575 }
576
577 /* Advance to next suffix. */
578 if (esuffix == 0)
579 break;
580 nsuffix += lsuffix + 1;
581 }
5a6e5452
KH
582 if (absolute)
583 RETURN_UNGCPRO (-1);
078e7b4a
JB
584 }
585
5a6e5452 586 RETURN_UNGCPRO (-1);
078e7b4a
JB
587}
588
589\f
ae321d28
RS
590/* Merge the list we've accumulated of globals from the current input source
591 into the load_history variable. The details depend on whether
592 the source has an associated file name or not. */
593
594static void
595build_load_history (stream, source)
596 FILE *stream;
597 Lisp_Object source;
598{
599 register Lisp_Object tail, prev, newelt;
600 register Lisp_Object tem, tem2;
601 register int foundit, loading;
602
8a1f1537
RS
603 /* Don't bother recording anything for preloaded files. */
604 if (!NILP (Vpurify_flag))
605 return;
606
ae321d28
RS
607 loading = stream || !NARROWED;
608
609 tail = Vload_history;
610 prev = Qnil;
611 foundit = 0;
612 while (!NILP (tail))
613 {
614 tem = Fcar (tail);
615
616 /* Find the feature's previous assoc list... */
617 if (!NILP (Fequal (source, Fcar (tem))))
618 {
619 foundit = 1;
620
621 /* If we're loading, remove it. */
622 if (loading)
623 {
624 if (NILP (prev))
625 Vload_history = Fcdr (tail);
626 else
627 Fsetcdr (prev, Fcdr (tail));
628 }
629
630 /* Otherwise, cons on new symbols that are not already members. */
631 else
632 {
633 tem2 = Vcurrent_load_list;
634
635 while (CONSP (tem2))
636 {
637 newelt = Fcar (tem2);
638
639 if (NILP (Fmemq (newelt, tem)))
640 Fsetcar (tail, Fcons (Fcar (tem),
641 Fcons (newelt, Fcdr (tem))));
642
643 tem2 = Fcdr (tem2);
644 QUIT;
645 }
646 }
647 }
648 else
649 prev = tail;
650 tail = Fcdr (tail);
651 QUIT;
652 }
653
8a1f1537
RS
654 /* If we're loading, cons the new assoc onto the front of load-history,
655 the most-recently-loaded position. Also do this if we didn't find
656 an existing member for the current source. */
657 if (loading || !foundit)
658 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
659 Vload_history);
ae321d28
RS
660}
661
078e7b4a
JB
662Lisp_Object
663unreadpure () /* Used as unwind-protect function in readevalloop */
664{
665 read_pure = 0;
666 return Qnil;
667}
668
669static void
ae321d28 670readevalloop (readcharfun, stream, sourcename, evalfun, printflag)
078e7b4a 671 Lisp_Object readcharfun;
ae321d28
RS
672 FILE *stream;
673 Lisp_Object sourcename;
078e7b4a
JB
674 Lisp_Object (*evalfun) ();
675 int printflag;
676{
677 register int c;
678 register Lisp_Object val;
679 int count = specpdl_ptr - specpdl;
8a1f1537 680 struct gcpro gcpro1;
49cf7ff4
RS
681 struct buffer *b = 0;
682
683 if (BUFFERP (readcharfun))
684 b = XBUFFER (readcharfun);
685 else if (MARKERP (readcharfun))
686 b = XMARKER (readcharfun)->buffer;
078e7b4a
JB
687
688 specbind (Qstandard_input, readcharfun);
8a1f1537 689 specbind (Qcurrent_load_list, Qnil);
078e7b4a 690
8a1f1537 691 GCPRO1 (sourcename);
ae321d28 692
ae321d28
RS
693 LOADHIST_ATTACH (sourcename);
694
078e7b4a
JB
695 while (1)
696 {
49cf7ff4
RS
697 if (b != 0 && NILP (b->name))
698 error ("Reading from killed buffer");
699
078e7b4a
JB
700 instream = stream;
701 c = READCHAR;
702 if (c == ';')
703 {
704 while ((c = READCHAR) != '\n' && c != -1);
705 continue;
706 }
707 if (c < 0) break;
708 if (c == ' ' || c == '\t' || c == '\n' || c == '\f') continue;
709
265a9e55 710 if (!NILP (Vpurify_flag) && c == '(')
078e7b4a
JB
711 {
712 record_unwind_protect (unreadpure, Qnil);
713 val = read_list (-1, readcharfun);
714 unbind_to (count + 1, Qnil);
715 }
716 else
717 {
718 UNREAD (c);
719 val = read0 (readcharfun);
720 }
721
722 val = (*evalfun) (val);
723 if (printflag)
724 {
725 Vvalues = Fcons (val, Vvalues);
726 if (EQ (Vstandard_output, Qt))
727 Fprin1 (val, Qnil);
728 else
729 Fprint (val, Qnil);
730 }
731 }
732
ae321d28 733 build_load_history (stream, sourcename);
ae321d28
RS
734 UNGCPRO;
735
078e7b4a
JB
736 unbind_to (count, Qnil);
737}
738
739#ifndef standalone
740
e5d77022 741DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 2, "",
94b304d7
RS
742 "Execute the current buffer as Lisp code.\n\
743Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
744BUFFER is the buffer to evaluate (nil means use current buffer).\n\
745PRINTFLAG controls printing of output:\n\
228d4b1c
JA
746nil means discard it; anything else is stream for print.\n\
747\n\
748If there is no error, point does not move. If there is an error,\n\
749point remains at the end of the last character read from the buffer.")
750 (bufname, printflag)
751 Lisp_Object bufname, printflag;
752{
753 int count = specpdl_ptr - specpdl;
754 Lisp_Object tem, buf;
755
dfdb645c 756 if (NILP (bufname))
228d4b1c
JA
757 buf = Fcurrent_buffer ();
758 else
759 buf = Fget_buffer (bufname);
dfdb645c 760 if (NILP (buf))
228d4b1c
JA
761 error ("No such buffer.");
762
dfdb645c 763 if (NILP (printflag))
228d4b1c
JA
764 tem = Qsymbolp;
765 else
766 tem = printflag;
767 specbind (Qstandard_output, tem);
768 record_unwind_protect (save_excursion_restore, save_excursion_save ());
769 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
ae321d28 770 readevalloop (buf, 0, XBUFFER (buf)->filename, Feval, !NILP (printflag));
cb09ab7a 771 unbind_to (count, Qnil);
228d4b1c
JA
772
773 return Qnil;
774}
775
776#if 0
078e7b4a
JB
777DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
778 "Execute the current buffer as Lisp code.\n\
779Programs can pass argument PRINTFLAG which controls printing of output:\n\
780nil means discard it; anything else is stream for print.\n\
781\n\
782If there is no error, point does not move. If there is an error,\n\
783point remains at the end of the last character read from the buffer.")
784 (printflag)
785 Lisp_Object printflag;
786{
787 int count = specpdl_ptr - specpdl;
ae321d28
RS
788 Lisp_Object tem, cbuf;
789
790 cbuf = Fcurrent_buffer ()
078e7b4a 791
265a9e55 792 if (NILP (printflag))
078e7b4a
JB
793 tem = Qsymbolp;
794 else
795 tem = printflag;
796 specbind (Qstandard_output, tem);
797 record_unwind_protect (save_excursion_restore, save_excursion_save ());
798 SET_PT (BEGV);
ae321d28 799 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
078e7b4a
JB
800 return unbind_to (count, Qnil);
801}
228d4b1c 802#endif
078e7b4a
JB
803
804DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r",
805 "Execute the region as Lisp code.\n\
806When called from programs, expects two arguments,\n\
807giving starting and ending indices in the current buffer\n\
808of the text to be executed.\n\
809Programs can pass third argument PRINTFLAG which controls output:\n\
810nil means discard it; anything else is stream for printing it.\n\
811\n\
812If there is no error, point does not move. If there is an error,\n\
813point remains at the end of the last character read from the buffer.")
814 (b, e, printflag)
815 Lisp_Object b, e, printflag;
816{
817 int count = specpdl_ptr - specpdl;
ae321d28
RS
818 Lisp_Object tem, cbuf;
819
820 cbuf = Fcurrent_buffer ();
078e7b4a 821
265a9e55 822 if (NILP (printflag))
078e7b4a
JB
823 tem = Qsymbolp;
824 else
825 tem = printflag;
826 specbind (Qstandard_output, tem);
827
265a9e55 828 if (NILP (printflag))
078e7b4a
JB
829 record_unwind_protect (save_excursion_restore, save_excursion_save ());
830 record_unwind_protect (save_restriction_restore, save_restriction_save ());
831
832 /* This both uses b and checks its type. */
833 Fgoto_char (b);
834 Fnarrow_to_region (make_number (BEGV), e);
ae321d28 835 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
078e7b4a
JB
836
837 return unbind_to (count, Qnil);
838}
839
840#endif /* standalone */
841\f
842DEFUN ("read", Fread, Sread, 0, 1, 0,
843 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
844If STREAM is nil, use the value of `standard-input' (which see).\n\
845STREAM or the value of `standard-input' may be:\n\
846 a buffer (read from point and advance it)\n\
847 a marker (read from where it points and advance it)\n\
848 a function (call it with no arguments for each character,\n\
849 call it with a char as argument to push a char back)\n\
850 a string (takes text from string, starting at the beginning)\n\
851 t (read text line using minibuffer and use it).")
852 (readcharfun)
853 Lisp_Object readcharfun;
854{
855 extern Lisp_Object Fread_minibuffer ();
856
265a9e55 857 if (NILP (readcharfun))
078e7b4a
JB
858 readcharfun = Vstandard_input;
859 if (EQ (readcharfun, Qt))
860 readcharfun = Qread_char;
861
862#ifndef standalone
863 if (EQ (readcharfun, Qread_char))
864 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
865#endif
866
867 if (XTYPE (readcharfun) == Lisp_String)
868 return Fcar (Fread_from_string (readcharfun, Qnil, Qnil));
869
870 return read0 (readcharfun);
871}
872
873DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
874 "Read one Lisp expression which is represented as text by STRING.\n\
875Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
876START and END optionally delimit a substring of STRING from which to read;\n\
877 they default to 0 and (length STRING) respectively.")
878 (string, start, end)
879 Lisp_Object string, start, end;
880{
881 int startval, endval;
882 Lisp_Object tem;
883
884 CHECK_STRING (string,0);
885
265a9e55 886 if (NILP (end))
078e7b4a
JB
887 endval = XSTRING (string)->size;
888 else
889 { CHECK_NUMBER (end,2);
890 endval = XINT (end);
891 if (endval < 0 || endval > XSTRING (string)->size)
892 args_out_of_range (string, end);
893 }
894
265a9e55 895 if (NILP (start))
078e7b4a
JB
896 startval = 0;
897 else
898 { CHECK_NUMBER (start,1);
899 startval = XINT (start);
900 if (startval < 0 || startval > endval)
901 args_out_of_range (string, start);
902 }
903
904 read_from_string_index = startval;
905 read_from_string_limit = endval;
906
907 tem = read0 (string);
908 return Fcons (tem, make_number (read_from_string_index));
909}
910\f
911/* Use this for recursive reads, in contexts where internal tokens are not allowed. */
912
913static Lisp_Object
914read0 (readcharfun)
915 Lisp_Object readcharfun;
916{
917 register Lisp_Object val;
918 char c;
919
920 val = read1 (readcharfun);
921 if (XTYPE (val) == Lisp_Internal)
922 {
923 c = XINT (val);
924 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil));
925 }
926
927 return val;
928}
929\f
930static int read_buffer_size;
931static char *read_buffer;
932
933static int
934read_escape (readcharfun)
935 Lisp_Object readcharfun;
936{
937 register int c = READCHAR;
938 switch (c)
939 {
940 case 'a':
265a9e55 941 return '\007';
078e7b4a
JB
942 case 'b':
943 return '\b';
f405a585
RS
944 case 'd':
945 return 0177;
078e7b4a
JB
946 case 'e':
947 return 033;
948 case 'f':
949 return '\f';
950 case 'n':
951 return '\n';
952 case 'r':
953 return '\r';
954 case 't':
955 return '\t';
956 case 'v':
957 return '\v';
958 case '\n':
959 return -1;
960
961 case 'M':
962 c = READCHAR;
963 if (c != '-')
964 error ("Invalid escape character syntax");
965 c = READCHAR;
966 if (c == '\\')
967 c = read_escape (readcharfun);
7bd279cd 968 return c | meta_modifier;
f405a585
RS
969
970 case 'S':
971 c = READCHAR;
972 if (c != '-')
973 error ("Invalid escape character syntax");
974 c = READCHAR;
975 if (c == '\\')
976 c = read_escape (readcharfun);
7bd279cd
RS
977 return c | shift_modifier;
978
979 case 'H':
980 c = READCHAR;
981 if (c != '-')
982 error ("Invalid escape character syntax");
983 c = READCHAR;
984 if (c == '\\')
985 c = read_escape (readcharfun);
986 return c | hyper_modifier;
987
988 case 'A':
989 c = READCHAR;
990 if (c != '-')
991 error ("Invalid escape character syntax");
992 c = READCHAR;
993 if (c == '\\')
994 c = read_escape (readcharfun);
995 return c | alt_modifier;
996
997 case 's':
998 c = READCHAR;
999 if (c != '-')
1000 error ("Invalid escape character syntax");
1001 c = READCHAR;
1002 if (c == '\\')
1003 c = read_escape (readcharfun);
1004 return c | super_modifier;
078e7b4a
JB
1005
1006 case 'C':
1007 c = READCHAR;
1008 if (c != '-')
1009 error ("Invalid escape character syntax");
1010 case '^':
1011 c = READCHAR;
1012 if (c == '\\')
1013 c = read_escape (readcharfun);
f405a585
RS
1014 if ((c & 0177) == '?')
1015 return 0177 | c;
1016 /* ASCII control chars are made from letters (both cases),
1017 as well as the non-letters within 0100...0137. */
1018 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1019 return (c & (037 | ~0177));
1020 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1021 return (c & (037 | ~0177));
078e7b4a 1022 else
7bd279cd 1023 return c | ctrl_modifier;
078e7b4a
JB
1024
1025 case '0':
1026 case '1':
1027 case '2':
1028 case '3':
1029 case '4':
1030 case '5':
1031 case '6':
1032 case '7':
1033 /* An octal escape, as in ANSI C. */
1034 {
1035 register int i = c - '0';
1036 register int count = 0;
1037 while (++count < 3)
1038 {
1039 if ((c = READCHAR) >= '0' && c <= '7')
1040 {
1041 i *= 8;
1042 i += c - '0';
1043 }
1044 else
1045 {
1046 UNREAD (c);
1047 break;
1048 }
1049 }
1050 return i;
1051 }
1052
1053 case 'x':
1054 /* A hex escape, as in ANSI C. */
1055 {
1056 int i = 0;
1057 while (1)
1058 {
1059 c = READCHAR;
1060 if (c >= '0' && c <= '9')
1061 {
1062 i *= 16;
1063 i += c - '0';
1064 }
1065 else if ((c >= 'a' && c <= 'f')
1066 || (c >= 'A' && c <= 'F'))
1067 {
1068 i *= 16;
1069 if (c >= 'a' && c <= 'f')
1070 i += c - 'a' + 10;
1071 else
1072 i += c - 'A' + 10;
1073 }
1074 else
1075 {
1076 UNREAD (c);
1077 break;
1078 }
1079 }
1080 return i;
1081 }
1082
1083 default:
1084 return c;
1085 }
1086}
1087
1088static Lisp_Object
1089read1 (readcharfun)
1090 register Lisp_Object readcharfun;
1091{
1092 register int c;
1093
1094 retry:
1095
1096 c = READCHAR;
1097 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1098
1099 switch (c)
1100 {
1101 case '(':
1102 return read_list (0, readcharfun);
1103
1104 case '[':
1105 return read_vector (readcharfun);
1106
1107 case ')':
1108 case ']':
078e7b4a
JB
1109 {
1110 register Lisp_Object val;
1111 XSET (val, Lisp_Internal, c);
1112 return val;
1113 }
1114
1115 case '#':
200f684e
RS
1116 c = READCHAR;
1117 if (c == '[')
1118 {
1119 /* Accept compiled functions at read-time so that we don't have to
1120 build them using function calls. */
748ef62f
RS
1121 Lisp_Object tmp;
1122 tmp = read_vector (readcharfun);
1123 return Fmake_byte_code (XVECTOR (tmp)->size,
1124 XVECTOR (tmp)->contents);
200f684e 1125 }
748ef62f
RS
1126#ifdef USE_TEXT_PROPERTIES
1127 if (c == '(')
1128 {
1129 Lisp_Object tmp;
1130 struct gcpro gcpro1;
1131
1132 /* Read the string itself. */
1133 tmp = read1 (readcharfun);
1134 if (XTYPE (tmp) != Lisp_String)
1135 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1136 GCPRO1 (tmp);
1137 /* Read the intervals and their properties. */
1138 while (1)
1139 {
1140 Lisp_Object beg, end, plist;
1141
1142 beg = read1 (readcharfun);
1143 if (XTYPE (beg) == Lisp_Internal)
1144 {
1145 if (XINT (beg) == ')')
1146 break;
1147 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("invalid string property list", 28), Qnil));
1148 }
1149 end = read1 (readcharfun);
1150 if (XTYPE (end) == Lisp_Internal)
1151 Fsignal (Qinvalid_read_syntax,
1152 Fcons (make_string ("invalid string property list", 28), Qnil));
1153
1154 plist = read1 (readcharfun);
1155 if (XTYPE (plist) == Lisp_Internal)
1156 Fsignal (Qinvalid_read_syntax,
1157 Fcons (make_string ("invalid string property list", 28), Qnil));
1158 Fset_text_properties (beg, end, plist, tmp);
1159 }
1160 UNGCPRO;
1161 return tmp;
1162 }
1163#endif
200f684e 1164 UNREAD (c);
748ef62f 1165 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
078e7b4a
JB
1166
1167 case ';':
1168 while ((c = READCHAR) >= 0 && c != '\n');
1169 goto retry;
1170
1171 case '\'':
1172 {
1173 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1174 }
1175
1176 case '?':
1177 {
1178 register Lisp_Object val;
1179
1180 c = READCHAR;
1181 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1182
1183 if (c == '\\')
1184 XSET (val, Lisp_Int, read_escape (readcharfun));
1185 else
1186 XSET (val, Lisp_Int, c);
1187
1188 return val;
1189 }
1190
1191 case '\"':
1192 {
1193 register char *p = read_buffer;
1194 register char *end = read_buffer + read_buffer_size;
1195 register int c;
1196 int cancel = 0;
1197
1198 while ((c = READCHAR) >= 0
1199 && c != '\"')
1200 {
1201 if (p == end)
1202 {
1203 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1204 p += new - read_buffer;
1205 read_buffer += new - read_buffer;
1206 end = read_buffer + read_buffer_size;
1207 }
1208 if (c == '\\')
1209 c = read_escape (readcharfun);
1210 /* c is -1 if \ newline has just been seen */
f405a585 1211 if (c == -1)
078e7b4a
JB
1212 {
1213 if (p == read_buffer)
1214 cancel = 1;
1215 }
1216 else
f943104a 1217 {
988c2f83
RS
1218 /* Allow `\C- ' and `\C-?'. */
1219 if (c == (CHAR_CTL | ' '))
1220 c = 0;
1221 else if (c == (CHAR_CTL | '?'))
1222 c = 127;
1223
f943104a
KH
1224 if (c & CHAR_META)
1225 /* Move the meta bit to the right place for a string. */
1226 c = (c & ~CHAR_META) | 0x80;
1227 if (c & ~0xff)
1228 error ("Invalid modifier in string");
1229 *p++ = c;
1230 }
078e7b4a
JB
1231 }
1232 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1233
1234 /* If purifying, and string starts with \ newline,
1235 return zero instead. This is for doc strings
08564963 1236 that we are really going to find in etc/DOC.nn.nn */
265a9e55 1237 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
078e7b4a
JB
1238 return make_number (0);
1239
1240 if (read_pure)
1241 return make_pure_string (read_buffer, p - read_buffer);
1242 else
1243 return make_string (read_buffer, p - read_buffer);
1244 }
1245
109d300c
JB
1246 case '.':
1247 {
1248#ifdef LISP_FLOAT_TYPE
1249 /* If a period is followed by a number, then we should read it
1250 as a floating point number. Otherwise, it denotes a dotted
1251 pair. */
1252 int next_char = READCHAR;
1253 UNREAD (next_char);
1254
1255 if (! isdigit (next_char))
1256#endif
1257 {
1258 register Lisp_Object val;
1259 XSET (val, Lisp_Internal, c);
1260 return val;
1261 }
1262
1263 /* Otherwise, we fall through! Note that the atom-reading loop
1264 below will now loop at least once, assuring that we will not
1265 try to UNREAD two characters in a row. */
1266 }
078e7b4a
JB
1267 default:
1268 if (c <= 040) goto retry;
1269 {
1270 register char *p = read_buffer;
481c6336 1271 int quoted = 0;
078e7b4a
JB
1272
1273 {
1274 register char *end = read_buffer + read_buffer_size;
1275
1276 while (c > 040 &&
1277 !(c == '\"' || c == '\'' || c == ';' || c == '?'
1278 || c == '(' || c == ')'
109d300c
JB
1279#ifndef LISP_FLOAT_TYPE
1280 /* If we have floating-point support, then we need
1281 to allow <digits><dot><digits>. */
078e7b4a
JB
1282 || c =='.'
1283#endif /* not LISP_FLOAT_TYPE */
1284 || c == '[' || c == ']' || c == '#'
1285 ))
1286 {
1287 if (p == end)
1288 {
1289 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1290 p += new - read_buffer;
1291 read_buffer += new - read_buffer;
1292 end = read_buffer + read_buffer_size;
1293 }
1294 if (c == '\\')
481c6336
RS
1295 {
1296 c = READCHAR;
1297 quoted = 1;
1298 }
078e7b4a
JB
1299 *p++ = c;
1300 c = READCHAR;
1301 }
1302
1303 if (p == end)
1304 {
1305 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1306 p += new - read_buffer;
1307 read_buffer += new - read_buffer;
1308/* end = read_buffer + read_buffer_size; */
1309 }
1310 *p = 0;
1311 if (c >= 0)
1312 UNREAD (c);
1313 }
1314
481c6336
RS
1315 if (!quoted)
1316 {
1317 register char *p1;
1318 register Lisp_Object val;
1319 p1 = read_buffer;
1320 if (*p1 == '+' || *p1 == '-') p1++;
1321 /* Is it an integer? */
1322 if (p1 != p)
1323 {
1324 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
dbc4e1c1 1325#ifdef LISP_FLOAT_TYPE
481c6336
RS
1326 /* Integers can have trailing decimal points. */
1327 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
dbc4e1c1 1328#endif
481c6336
RS
1329 if (p1 == p)
1330 /* It is an integer. */
1331 {
dbc4e1c1 1332#ifdef LISP_FLOAT_TYPE
481c6336
RS
1333 if (p1[-1] == '.')
1334 p1[-1] = '\0';
dbc4e1c1 1335#endif
481c6336
RS
1336 XSET (val, Lisp_Int, atoi (read_buffer));
1337 return val;
1338 }
1339 }
078e7b4a 1340#ifdef LISP_FLOAT_TYPE
481c6336
RS
1341 if (isfloat_string (read_buffer))
1342 return make_float (atof (read_buffer));
078e7b4a 1343#endif
481c6336 1344 }
078e7b4a
JB
1345
1346 return intern (read_buffer);
1347 }
1348 }
1349}
1350\f
1351#ifdef LISP_FLOAT_TYPE
1352
078e7b4a
JB
1353#define LEAD_INT 1
1354#define DOT_CHAR 2
1355#define TRAIL_INT 4
1356#define E_CHAR 8
1357#define EXP_INT 16
1358
1359int
1360isfloat_string (cp)
1361 register char *cp;
1362{
1363 register state;
1364
1365 state = 0;
1366 if (*cp == '+' || *cp == '-')
1367 cp++;
1368
1369 if (isdigit(*cp))
1370 {
1371 state |= LEAD_INT;
1372 while (isdigit (*cp))
1373 cp ++;
1374 }
1375 if (*cp == '.')
1376 {
1377 state |= DOT_CHAR;
1378 cp++;
1379 }
1380 if (isdigit(*cp))
1381 {
1382 state |= TRAIL_INT;
1383 while (isdigit (*cp))
1384 cp++;
1385 }
1386 if (*cp == 'e')
1387 {
1388 state |= E_CHAR;
1389 cp++;
1390 }
1391 if ((*cp == '+') || (*cp == '-'))
1392 cp++;
1393
1394 if (isdigit (*cp))
1395 {
1396 state |= EXP_INT;
1397 while (isdigit (*cp))
1398 cp++;
1399 }
1400 return (*cp == 0
1401 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
151bdc83 1402 || state == (DOT_CHAR|TRAIL_INT)
078e7b4a 1403 || state == (LEAD_INT|E_CHAR|EXP_INT)
151bdc83
JB
1404 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
1405 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
078e7b4a
JB
1406}
1407#endif /* LISP_FLOAT_TYPE */
1408\f
1409static Lisp_Object
1410read_vector (readcharfun)
1411 Lisp_Object readcharfun;
1412{
1413 register int i;
1414 register int size;
1415 register Lisp_Object *ptr;
1416 register Lisp_Object tem, vector;
1417 register struct Lisp_Cons *otem;
1418 Lisp_Object len;
1419
1420 tem = read_list (1, readcharfun);
1421 len = Flength (tem);
1422 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
1423
1424
1425 size = XVECTOR (vector)->size;
1426 ptr = XVECTOR (vector)->contents;
1427 for (i = 0; i < size; i++)
1428 {
1429 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
1430 otem = XCONS (tem);
1431 tem = Fcdr (tem);
1432 free_cons (otem);
1433 }
1434 return vector;
1435}
1436
1437/* flag = 1 means check for ] to terminate rather than ) and .
1438 flag = -1 means check for starting with defun
1439 and make structure pure. */
1440
1441static Lisp_Object
1442read_list (flag, readcharfun)
1443 int flag;
1444 register Lisp_Object readcharfun;
1445{
1446 /* -1 means check next element for defun,
1447 0 means don't check,
1448 1 means already checked and found defun. */
1449 int defunflag = flag < 0 ? -1 : 0;
1450 Lisp_Object val, tail;
1451 register Lisp_Object elt, tem;
1452 struct gcpro gcpro1, gcpro2;
1453
1454 val = Qnil;
1455 tail = Qnil;
1456
1457 while (1)
1458 {
1459 GCPRO2 (val, tail);
1460 elt = read1 (readcharfun);
1461 UNGCPRO;
1462 if (XTYPE (elt) == Lisp_Internal)
1463 {
1464 if (flag > 0)
1465 {
1466 if (XINT (elt) == ']')
1467 return val;
1468 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil));
1469 }
1470 if (XINT (elt) == ')')
1471 return val;
1472 if (XINT (elt) == '.')
1473 {
1474 GCPRO2 (val, tail);
265a9e55 1475 if (!NILP (tail))
078e7b4a
JB
1476 XCONS (tail)->cdr = read0 (readcharfun);
1477 else
1478 val = read0 (readcharfun);
1479 elt = read1 (readcharfun);
1480 UNGCPRO;
1481 if (XTYPE (elt) == Lisp_Internal && XINT (elt) == ')')
1482 return val;
1483 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
1484 }
1485 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
1486 }
1487 tem = (read_pure && flag <= 0
1488 ? pure_cons (elt, Qnil)
1489 : Fcons (elt, Qnil));
265a9e55 1490 if (!NILP (tail))
078e7b4a
JB
1491 XCONS (tail)->cdr = tem;
1492 else
1493 val = tem;
1494 tail = tem;
1495 if (defunflag < 0)
1496 defunflag = EQ (elt, Qdefun);
1497 else if (defunflag > 0)
1498 read_pure = 1;
1499 }
1500}
1501\f
1502Lisp_Object Vobarray;
1503Lisp_Object initial_obarray;
1504
1505Lisp_Object
1506check_obarray (obarray)
1507 Lisp_Object obarray;
1508{
1509 while (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0)
1510 {
1511 /* If Vobarray is now invalid, force it to be valid. */
1512 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
1513
1514 obarray = wrong_type_argument (Qvectorp, obarray);
1515 }
1516 return obarray;
1517}
1518
1519static int hash_string ();
1520Lisp_Object oblookup ();
1521
1522Lisp_Object
1523intern (str)
1524 char *str;
1525{
1526 Lisp_Object tem;
1527 int len = strlen (str);
153a17b7 1528 Lisp_Object obarray;
078e7b4a 1529
153a17b7 1530 obarray = Vobarray;
078e7b4a
JB
1531 if (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0)
1532 obarray = check_obarray (obarray);
1533 tem = oblookup (obarray, str, len);
1534 if (XTYPE (tem) == Lisp_Symbol)
1535 return tem;
265a9e55 1536 return Fintern ((!NILP (Vpurify_flag)
078e7b4a
JB
1537 ? make_pure_string (str, len)
1538 : make_string (str, len)),
1539 obarray);
1540}
1541
1542DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
1543 "Return the canonical symbol whose name is STRING.\n\
1544If there is none, one is created by this function and returned.\n\
1545A second optional argument specifies the obarray to use;\n\
1546it defaults to the value of `obarray'.")
1547 (str, obarray)
1548 Lisp_Object str, obarray;
1549{
1550 register Lisp_Object tem, sym, *ptr;
1551
265a9e55 1552 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
1553 obarray = check_obarray (obarray);
1554
1555 CHECK_STRING (str, 0);
1556
1557 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1558 if (XTYPE (tem) != Lisp_Int)
1559 return tem;
1560
265a9e55 1561 if (!NILP (Vpurify_flag))
078e7b4a
JB
1562 str = Fpurecopy (str);
1563 sym = Fmake_symbol (str);
1564
1565 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
1566 if (XTYPE (*ptr) == Lisp_Symbol)
1567 XSYMBOL (sym)->next = XSYMBOL (*ptr);
1568 else
1569 XSYMBOL (sym)->next = 0;
1570 *ptr = sym;
1571 return sym;
1572}
1573
1574DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
1575 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
1576A second optional argument specifies the obarray to use;\n\
1577it defaults to the value of `obarray'.")
1578 (str, obarray)
1579 Lisp_Object str, obarray;
1580{
1581 register Lisp_Object tem;
1582
265a9e55 1583 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
1584 obarray = check_obarray (obarray);
1585
1586 CHECK_STRING (str, 0);
1587
1588 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1589 if (XTYPE (tem) != Lisp_Int)
1590 return tem;
1591 return Qnil;
1592}
1593
1594Lisp_Object
1595oblookup (obarray, ptr, size)
1596 Lisp_Object obarray;
1597 register char *ptr;
1598 register int size;
1599{
1600 int hash, obsize;
1601 register Lisp_Object tail;
1602 Lisp_Object bucket, tem;
1603
7c79a684
RS
1604 if (XTYPE (obarray) != Lisp_Vector
1605 || (obsize = XVECTOR (obarray)->size) == 0)
078e7b4a
JB
1606 {
1607 obarray = check_obarray (obarray);
1608 obsize = XVECTOR (obarray)->size;
1609 }
1610 /* Combining next two lines breaks VMS C 2.3. */
1611 hash = hash_string (ptr, size);
1612 hash %= obsize;
1613 bucket = XVECTOR (obarray)->contents[hash];
1614 if (XFASTINT (bucket) == 0)
1615 ;
1616 else if (XTYPE (bucket) != Lisp_Symbol)
1617 error ("Bad data in guts of obarray"); /* Like CADR error message */
1618 else for (tail = bucket; ; XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next))
1619 {
1620 if (XSYMBOL (tail)->name->size == size &&
1621 !bcmp (XSYMBOL (tail)->name->data, ptr, size))
1622 return tail;
1623 else if (XSYMBOL (tail)->next == 0)
1624 break;
1625 }
1626 XSET (tem, Lisp_Int, hash);
1627 return tem;
1628}
1629
1630static int
1631hash_string (ptr, len)
1632 unsigned char *ptr;
1633 int len;
1634{
1635 register unsigned char *p = ptr;
1636 register unsigned char *end = p + len;
1637 register unsigned char c;
1638 register int hash = 0;
1639
1640 while (p != end)
1641 {
1642 c = *p++;
1643 if (c >= 0140) c -= 40;
1644 hash = ((hash<<3) + (hash>>28) + c);
1645 }
1646 return hash & 07777777777;
1647}
1648
1649void
1650map_obarray (obarray, fn, arg)
1651 Lisp_Object obarray;
1652 int (*fn) ();
1653 Lisp_Object arg;
1654{
1655 register int i;
1656 register Lisp_Object tail;
1657 CHECK_VECTOR (obarray, 1);
1658 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
1659 {
1660 tail = XVECTOR (obarray)->contents[i];
1661 if (XFASTINT (tail) != 0)
1662 while (1)
1663 {
1664 (*fn) (tail, arg);
1665 if (XSYMBOL (tail)->next == 0)
1666 break;
1667 XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next);
1668 }
1669 }
1670}
1671
1672mapatoms_1 (sym, function)
1673 Lisp_Object sym, function;
1674{
1675 call1 (function, sym);
1676}
1677
1678DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
1679 "Call FUNCTION on every symbol in OBARRAY.\n\
1680OBARRAY defaults to the value of `obarray'.")
1681 (function, obarray)
1682 Lisp_Object function, obarray;
1683{
1684 Lisp_Object tem;
1685
265a9e55 1686 if (NILP (obarray)) obarray = Vobarray;
078e7b4a
JB
1687 obarray = check_obarray (obarray);
1688
1689 map_obarray (obarray, mapatoms_1, function);
1690 return Qnil;
1691}
1692
5e88a39e 1693#define OBARRAY_SIZE 1511
078e7b4a
JB
1694
1695void
1696init_obarray ()
1697{
1698 Lisp_Object oblength;
1699 int hash;
1700 Lisp_Object *tem;
1701
1702 XFASTINT (oblength) = OBARRAY_SIZE;
1703
1704 Qnil = Fmake_symbol (make_pure_string ("nil", 3));
1705 Vobarray = Fmake_vector (oblength, make_number (0));
1706 initial_obarray = Vobarray;
1707 staticpro (&initial_obarray);
1708 /* Intern nil in the obarray */
1709 /* These locals are to kludge around a pyramid compiler bug. */
1710 hash = hash_string ("nil", 3);
1711 /* Separate statement here to avoid VAXC bug. */
1712 hash %= OBARRAY_SIZE;
1713 tem = &XVECTOR (Vobarray)->contents[hash];
1714 *tem = Qnil;
1715
1716 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7));
1717 XSYMBOL (Qnil)->function = Qunbound;
1718 XSYMBOL (Qunbound)->value = Qunbound;
1719 XSYMBOL (Qunbound)->function = Qunbound;
1720
1721 Qt = intern ("t");
1722 XSYMBOL (Qnil)->value = Qnil;
1723 XSYMBOL (Qnil)->plist = Qnil;
1724 XSYMBOL (Qt)->value = Qt;
1725
1726 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
1727 Vpurify_flag = Qt;
1728
1729 Qvariable_documentation = intern ("variable-documentation");
1730
1731 read_buffer_size = 100;
1732 read_buffer = (char *) malloc (read_buffer_size);
1733}
1734\f
1735void
1736defsubr (sname)
1737 struct Lisp_Subr *sname;
1738{
1739 Lisp_Object sym;
1740 sym = intern (sname->symbol_name);
1741 XSET (XSYMBOL (sym)->function, Lisp_Subr, sname);
1742}
1743
1744#ifdef NOTDEF /* use fset in subr.el now */
1745void
1746defalias (sname, string)
1747 struct Lisp_Subr *sname;
1748 char *string;
1749{
1750 Lisp_Object sym;
1751 sym = intern (string);
1752 XSET (XSYMBOL (sym)->function, Lisp_Subr, sname);
1753}
1754#endif /* NOTDEF */
1755
078e7b4a
JB
1756/* Define an "integer variable"; a symbol whose value is forwarded
1757 to a C variable of type int. Sample call: */
1758 /* DEFVARINT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
1759
1760void
e9e00ff2 1761defvar_int (namestring, address)
078e7b4a
JB
1762 char *namestring;
1763 int *address;
078e7b4a
JB
1764{
1765 Lisp_Object sym;
1766 sym = intern (namestring);
1767 XSET (XSYMBOL (sym)->value, Lisp_Intfwd, address);
1768}
1769
1770/* Similar but define a variable whose value is T if address contains 1,
1771 NIL if address contains 0 */
1772
1773void
e9e00ff2 1774defvar_bool (namestring, address)
078e7b4a
JB
1775 char *namestring;
1776 int *address;
078e7b4a
JB
1777{
1778 Lisp_Object sym;
1779 sym = intern (namestring);
1780 XSET (XSYMBOL (sym)->value, Lisp_Boolfwd, address);
1781}
1782
1783/* Similar but define a variable whose value is the Lisp Object stored at address. */
1784
1785void
e9e00ff2 1786defvar_lisp (namestring, address)
078e7b4a
JB
1787 char *namestring;
1788 Lisp_Object *address;
078e7b4a
JB
1789{
1790 Lisp_Object sym;
1791 sym = intern (namestring);
1792 XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address);
1793 staticpro (address);
1794}
1795
1796/* Similar but don't request gc-marking of the C variable.
1797 Used when that variable will be gc-marked for some other reason,
1798 since marking the same slot twice can cause trouble with strings. */
1799
1800void
e9e00ff2 1801defvar_lisp_nopro (namestring, address)
078e7b4a
JB
1802 char *namestring;
1803 Lisp_Object *address;
078e7b4a
JB
1804{
1805 Lisp_Object sym;
1806 sym = intern (namestring);
1807 XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address);
1808}
1809
1810#ifndef standalone
1811
1812/* Similar but define a variable whose value is the Lisp Object stored in
1813 the current buffer. address is the address of the slot in the buffer that is current now. */
1814
1815void
4360b0c6 1816defvar_per_buffer (namestring, address, type, doc)
078e7b4a
JB
1817 char *namestring;
1818 Lisp_Object *address;
4360b0c6 1819 Lisp_Object type;
078e7b4a
JB
1820 char *doc;
1821{
1822 Lisp_Object sym;
1823 int offset;
1824 extern struct buffer buffer_local_symbols;
1825
1826 sym = intern (namestring);
1827 offset = (char *)address - (char *)current_buffer;
1828
1829 XSET (XSYMBOL (sym)->value, Lisp_Buffer_Objfwd,
1830 (Lisp_Object *) offset);
1831 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
4360b0c6 1832 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
078e7b4a
JB
1833 if (*(int *)(offset + (char *)&buffer_local_flags) == 0)
1834 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
1835 slot of buffer_local_flags */
1836 abort ();
1837}
1838
1839#endif /* standalone */
1840\f
279499f0 1841init_lread ()
078e7b4a 1842{
46947372 1843 char *normal;
078e7b4a 1844
279499f0 1845 /* Compute the default load-path. */
46947372
JB
1846#ifdef CANNOT_DUMP
1847 normal = PATH_LOADSEARCH;
e065a56e 1848 Vload_path = decode_env_path (0, normal);
46947372
JB
1849#else
1850 if (NILP (Vpurify_flag))
1851 normal = PATH_LOADSEARCH;
279499f0 1852 else
46947372 1853 normal = PATH_DUMPLOADSEARCH;
279499f0 1854
46947372
JB
1855 /* In a dumped Emacs, we normally have to reset the value of
1856 Vload_path from PATH_LOADSEARCH, since the value that was dumped
1857 uses ../lisp, instead of the path of the installed elisp
1858 libraries. However, if it appears that Vload_path was changed
1859 from the default before dumping, don't override that value. */
4746118a
JB
1860 if (initialized)
1861 {
1862 Lisp_Object dump_path;
46947372 1863
e065a56e 1864 dump_path = decode_env_path (0, PATH_DUMPLOADSEARCH);
4746118a 1865 if (! NILP (Fequal (dump_path, Vload_path)))
80667d53
RS
1866 {
1867 Vload_path = decode_env_path (0, normal);
74180aa4 1868 if (!NILP (Vinstallation_directory))
80667d53 1869 {
74180aa4 1870 /* Add to the path the lisp subdir of the
3a3056e5
RS
1871 installation dir, if it exists. */
1872 Lisp_Object tem, tem1;
74180aa4
RS
1873 tem = Fexpand_file_name (build_string ("lisp"),
1874 Vinstallation_directory);
3a3056e5
RS
1875 tem1 = Ffile_exists_p (tem);
1876 if (!NILP (tem1))
1877 {
1878 if (NILP (Fmember (tem, Vload_path)))
1879 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
1880 }
1881 else
1882 /* That dir doesn't exist, so add the build-time
1883 Lisp dirs instead. */
1884 Vload_path = nconc2 (Vload_path, dump_path);
80667d53
RS
1885 }
1886 }
4746118a
JB
1887 }
1888 else
e065a56e 1889 Vload_path = decode_env_path (0, normal);
46947372 1890#endif
279499f0 1891
078e7b4a 1892 /* Warn if dirs in the *standard* path don't exist. */
46947372
JB
1893 {
1894 Lisp_Object path_tail;
078e7b4a 1895
46947372
JB
1896 for (path_tail = Vload_path;
1897 !NILP (path_tail);
1898 path_tail = XCONS (path_tail)->cdr)
1899 {
1900 Lisp_Object dirfile;
1901 dirfile = Fcar (path_tail);
1902 if (XTYPE (dirfile) == Lisp_String)
1903 {
1904 dirfile = Fdirectory_file_name (dirfile);
1905 if (access (XSTRING (dirfile)->data, 0) < 0)
3a3056e5
RS
1906 fprintf (stderr,
1907 "Warning: Lisp directory `%s' does not exist.\n",
3917910a 1908 XSTRING (Fcar (path_tail))->data);
46947372
JB
1909 }
1910 }
1911 }
1912
1913 /* If the EMACSLOADPATH environment variable is set, use its value.
1914 This doesn't apply if we're dumping. */
1915 if (NILP (Vpurify_flag)
1916 && egetenv ("EMACSLOADPATH"))
279499f0 1917 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
279499f0
JB
1918
1919 Vvalues = Qnil;
1920
078e7b4a 1921 load_in_progress = 0;
d2c6be7f
RS
1922
1923 load_descriptor_list = Qnil;
078e7b4a
JB
1924}
1925
1926void
279499f0 1927syms_of_lread ()
078e7b4a
JB
1928{
1929 defsubr (&Sread);
1930 defsubr (&Sread_from_string);
1931 defsubr (&Sintern);
1932 defsubr (&Sintern_soft);
1933 defsubr (&Sload);
228d4b1c 1934 defsubr (&Seval_buffer);
078e7b4a
JB
1935 defsubr (&Seval_region);
1936 defsubr (&Sread_char);
1937 defsubr (&Sread_char_exclusive);
078e7b4a 1938 defsubr (&Sread_event);
078e7b4a
JB
1939 defsubr (&Sget_file_char);
1940 defsubr (&Smapatoms);
1941
1942 DEFVAR_LISP ("obarray", &Vobarray,
1943 "Symbol table for use by `intern' and `read'.\n\
1944It is a vector whose length ought to be prime for best results.\n\
1945The vector's contents don't make sense if examined from Lisp programs;\n\
1946to find all the symbols in an obarray, use `mapatoms'.");
1947
1948 DEFVAR_LISP ("values", &Vvalues,
1949 "List of values of all expressions which were read, evaluated and printed.\n\
1950Order is reverse chronological.");
1951
1952 DEFVAR_LISP ("standard-input", &Vstandard_input,
1953 "Stream for read to get input from.\n\
1954See documentation of `read' for possible values.");
1955 Vstandard_input = Qt;
1956
1957 DEFVAR_LISP ("load-path", &Vload_path,
1958 "*List of directories to search for files to load.\n\
1959Each element is a string (directory name) or nil (try default directory).\n\
1960Initialized based on EMACSLOADPATH environment variable, if any,\n\
692f86ad 1961otherwise to default specified by file `paths.h' when Emacs was built.");
078e7b4a
JB
1962
1963 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
1964 "Non-nil iff inside of `load'.");
1965
1966 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
1967 "An alist of expressions to be evalled when particular files are loaded.\n\
1968Each element looks like (FILENAME FORMS...).\n\
1969When `load' is run and the file-name argument is FILENAME,\n\
1970the FORMS in the corresponding element are executed at the end of loading.\n\n\
1971FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
1972with no directory specified, since that is how `load' is normally called.\n\
1973An error in FORMS does not undo the load,\n\
1974but does prevent execution of the rest of the FORMS.");
1975 Vafter_load_alist = Qnil;
1976
ae321d28
RS
1977 DEFVAR_LISP ("load-history", &Vload_history,
1978 "Alist mapping source file names to symbols and features.\n\
1979Each alist element is a list that starts with a file name,\n\
1980except for one element (optional) that starts with nil and describes\n\
1981definitions evaluated from buffers not visiting files.\n\
1982The remaining elements of each list are symbols defined as functions\n\
1983or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
1984 Vload_history = Qnil;
1985
8a1f1537
RS
1986 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
1987 "Used for internal purposes by `load'.");
ae321d28
RS
1988 Vcurrent_load_list = Qnil;
1989
d2c6be7f
RS
1990 load_descriptor_list = Qnil;
1991 staticpro (&load_descriptor_list);
1992
8a1f1537
RS
1993 Qcurrent_load_list = intern ("current-load-list");
1994 staticpro (&Qcurrent_load_list);
1995
078e7b4a
JB
1996 Qstandard_input = intern ("standard-input");
1997 staticpro (&Qstandard_input);
1998
1999 Qread_char = intern ("read-char");
2000 staticpro (&Qread_char);
2001
2002 Qget_file_char = intern ("get-file-char");
2003 staticpro (&Qget_file_char);
7bd279cd
RS
2004
2005 Qascii_character = intern ("ascii-character");
2006 staticpro (&Qascii_character);
c2225d00
RS
2007
2008 Qload = intern ("load");
2009 staticpro (&Qload);
078e7b4a 2010}