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