]> code.delx.au - gnu-emacs/blob - src/lread.c
(XMISCTYPE): New macro.
[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 return c | shift_modifier;
1008
1009 case 'H':
1010 c = READCHAR;
1011 if (c != '-')
1012 error ("Invalid escape character syntax");
1013 c = READCHAR;
1014 if (c == '\\')
1015 c = read_escape (readcharfun);
1016 return c | hyper_modifier;
1017
1018 case 'A':
1019 c = READCHAR;
1020 if (c != '-')
1021 error ("Invalid escape character syntax");
1022 c = READCHAR;
1023 if (c == '\\')
1024 c = read_escape (readcharfun);
1025 return c | alt_modifier;
1026
1027 case 's':
1028 c = READCHAR;
1029 if (c != '-')
1030 error ("Invalid escape character syntax");
1031 c = READCHAR;
1032 if (c == '\\')
1033 c = read_escape (readcharfun);
1034 return c | super_modifier;
1035
1036 case 'C':
1037 c = READCHAR;
1038 if (c != '-')
1039 error ("Invalid escape character syntax");
1040 case '^':
1041 c = READCHAR;
1042 if (c == '\\')
1043 c = read_escape (readcharfun);
1044 if ((c & 0177) == '?')
1045 return 0177 | c;
1046 /* ASCII control chars are made from letters (both cases),
1047 as well as the non-letters within 0100...0137. */
1048 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1049 return (c & (037 | ~0177));
1050 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1051 return (c & (037 | ~0177));
1052 else
1053 return c | ctrl_modifier;
1054
1055 case '0':
1056 case '1':
1057 case '2':
1058 case '3':
1059 case '4':
1060 case '5':
1061 case '6':
1062 case '7':
1063 /* An octal escape, as in ANSI C. */
1064 {
1065 register int i = c - '0';
1066 register int count = 0;
1067 while (++count < 3)
1068 {
1069 if ((c = READCHAR) >= '0' && c <= '7')
1070 {
1071 i *= 8;
1072 i += c - '0';
1073 }
1074 else
1075 {
1076 UNREAD (c);
1077 break;
1078 }
1079 }
1080 return i;
1081 }
1082
1083 case 'x':
1084 /* A hex escape, as in ANSI C. */
1085 {
1086 int i = 0;
1087 while (1)
1088 {
1089 c = READCHAR;
1090 if (c >= '0' && c <= '9')
1091 {
1092 i *= 16;
1093 i += c - '0';
1094 }
1095 else if ((c >= 'a' && c <= 'f')
1096 || (c >= 'A' && c <= 'F'))
1097 {
1098 i *= 16;
1099 if (c >= 'a' && c <= 'f')
1100 i += c - 'a' + 10;
1101 else
1102 i += c - 'A' + 10;
1103 }
1104 else
1105 {
1106 UNREAD (c);
1107 break;
1108 }
1109 }
1110 return i;
1111 }
1112
1113 default:
1114 return c;
1115 }
1116 }
1117
1118 /* If the next token is ')' or ']' or '.', we store that character
1119 in *PCH and the return value is not interesting. Else, we store
1120 zero in *PCH and we read and return one lisp object. */
1121 static Lisp_Object
1122 read1 (readcharfun, pch)
1123 register Lisp_Object readcharfun;
1124 char *pch;
1125 {
1126 register int c;
1127 *pch = 0;
1128
1129 retry:
1130
1131 c = READCHAR;
1132 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1133
1134 switch (c)
1135 {
1136 case '(':
1137 return read_list (0, readcharfun);
1138
1139 case '[':
1140 return read_vector (readcharfun);
1141
1142 case ')':
1143 case ']':
1144 {
1145 *pch = c;
1146 return Qnil;
1147 }
1148
1149 case '#':
1150 c = READCHAR;
1151 if (c == '[')
1152 {
1153 /* Accept compiled functions at read-time so that we don't have to
1154 build them using function calls. */
1155 Lisp_Object tmp;
1156 tmp = read_vector (readcharfun);
1157 return Fmake_byte_code (XVECTOR (tmp)->size,
1158 XVECTOR (tmp)->contents);
1159 }
1160 #ifdef USE_TEXT_PROPERTIES
1161 if (c == '(')
1162 {
1163 Lisp_Object tmp;
1164 struct gcpro gcpro1;
1165 char ch;
1166
1167 /* Read the string itself. */
1168 tmp = read1 (readcharfun, &ch);
1169 if (ch != 0 || !STRINGP (tmp))
1170 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1171 GCPRO1 (tmp);
1172 /* Read the intervals and their properties. */
1173 while (1)
1174 {
1175 Lisp_Object beg, end, plist;
1176
1177 beg = read1 (readcharfun, &ch);
1178 if (ch == ')')
1179 break;
1180 if (ch == 0)
1181 end = read1 (readcharfun, &ch);
1182 if (ch == 0)
1183 plist = read1 (readcharfun, &ch);
1184 if (ch)
1185 Fsignal (Qinvalid_read_syntax,
1186 Fcons (build_string ("invalid string property list"),
1187 Qnil));
1188 Fset_text_properties (beg, end, plist, tmp);
1189 }
1190 UNGCPRO;
1191 return tmp;
1192 }
1193 #endif
1194 /* #@NUMBER is used to skip NUMBER following characters.
1195 That's used in .elc files to skip over doc strings
1196 and function definitions. */
1197 if (c == '@')
1198 {
1199 int i, nskip = 0;
1200
1201 /* Read a decimal integer. */
1202 while ((c = READCHAR) >= 0
1203 && c >= '0' && c <= '9')
1204 {
1205 nskip *= 10;
1206 nskip += c - '0';
1207 }
1208 if (c >= 0)
1209 UNREAD (c);
1210
1211 /* Skip that many characters. */
1212 for (i = 0; i < nskip && c >= 0; i++)
1213 c = READCHAR;
1214 goto retry;
1215 }
1216 if (c == '$')
1217 return Vload_file_name;
1218
1219 UNREAD (c);
1220 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1221
1222 case ';':
1223 while ((c = READCHAR) >= 0 && c != '\n');
1224 goto retry;
1225
1226 case '\'':
1227 {
1228 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1229 }
1230
1231 case '?':
1232 {
1233 register Lisp_Object val;
1234
1235 c = READCHAR;
1236 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1237
1238 if (c == '\\')
1239 XSETINT (val, read_escape (readcharfun));
1240 else
1241 XSETINT (val, c);
1242
1243 return val;
1244 }
1245
1246 case '\"':
1247 {
1248 register char *p = read_buffer;
1249 register char *end = read_buffer + read_buffer_size;
1250 register int c;
1251 int cancel = 0;
1252
1253 while ((c = READCHAR) >= 0
1254 && c != '\"')
1255 {
1256 if (p == end)
1257 {
1258 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1259 p += new - read_buffer;
1260 read_buffer += new - read_buffer;
1261 end = read_buffer + read_buffer_size;
1262 }
1263 if (c == '\\')
1264 c = read_escape (readcharfun);
1265 /* c is -1 if \ newline has just been seen */
1266 if (c == -1)
1267 {
1268 if (p == read_buffer)
1269 cancel = 1;
1270 }
1271 else
1272 {
1273 /* Allow `\C- ' and `\C-?'. */
1274 if (c == (CHAR_CTL | ' '))
1275 c = 0;
1276 else if (c == (CHAR_CTL | '?'))
1277 c = 127;
1278
1279 if (c & CHAR_META)
1280 /* Move the meta bit to the right place for a string. */
1281 c = (c & ~CHAR_META) | 0x80;
1282 if (c & ~0xff)
1283 error ("Invalid modifier in string");
1284 *p++ = c;
1285 }
1286 }
1287 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1288
1289 /* If purifying, and string starts with \ newline,
1290 return zero instead. This is for doc strings
1291 that we are really going to find in etc/DOC.nn.nn */
1292 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
1293 return make_number (0);
1294
1295 if (read_pure)
1296 return make_pure_string (read_buffer, p - read_buffer);
1297 else
1298 return make_string (read_buffer, p - read_buffer);
1299 }
1300
1301 case '.':
1302 {
1303 #ifdef LISP_FLOAT_TYPE
1304 /* If a period is followed by a number, then we should read it
1305 as a floating point number. Otherwise, it denotes a dotted
1306 pair. */
1307 int next_char = READCHAR;
1308 UNREAD (next_char);
1309
1310 if (! (next_char >= '0' && next_char <= '9'))
1311 #endif
1312 {
1313 *pch = c;
1314 return Qnil;
1315 }
1316
1317 /* Otherwise, we fall through! Note that the atom-reading loop
1318 below will now loop at least once, assuring that we will not
1319 try to UNREAD two characters in a row. */
1320 }
1321 default:
1322 if (c <= 040) goto retry;
1323 {
1324 register char *p = read_buffer;
1325 int quoted = 0;
1326
1327 {
1328 register char *end = read_buffer + read_buffer_size;
1329
1330 while (c > 040 &&
1331 !(c == '\"' || c == '\'' || c == ';' || c == '?'
1332 || c == '(' || c == ')'
1333 #ifndef LISP_FLOAT_TYPE
1334 /* If we have floating-point support, then we need
1335 to allow <digits><dot><digits>. */
1336 || c =='.'
1337 #endif /* not LISP_FLOAT_TYPE */
1338 || c == '[' || c == ']' || c == '#'
1339 ))
1340 {
1341 if (p == end)
1342 {
1343 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1344 p += new - read_buffer;
1345 read_buffer += new - read_buffer;
1346 end = read_buffer + read_buffer_size;
1347 }
1348 if (c == '\\')
1349 {
1350 c = READCHAR;
1351 quoted = 1;
1352 }
1353 *p++ = c;
1354 c = READCHAR;
1355 }
1356
1357 if (p == end)
1358 {
1359 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1360 p += new - read_buffer;
1361 read_buffer += new - read_buffer;
1362 /* end = read_buffer + read_buffer_size; */
1363 }
1364 *p = 0;
1365 if (c >= 0)
1366 UNREAD (c);
1367 }
1368
1369 if (!quoted)
1370 {
1371 register char *p1;
1372 register Lisp_Object val;
1373 p1 = read_buffer;
1374 if (*p1 == '+' || *p1 == '-') p1++;
1375 /* Is it an integer? */
1376 if (p1 != p)
1377 {
1378 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
1379 #ifdef LISP_FLOAT_TYPE
1380 /* Integers can have trailing decimal points. */
1381 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
1382 #endif
1383 if (p1 == p)
1384 /* It is an integer. */
1385 {
1386 #ifdef LISP_FLOAT_TYPE
1387 if (p1[-1] == '.')
1388 p1[-1] = '\0';
1389 #endif
1390 XSETINT (val, atoi (read_buffer));
1391 return val;
1392 }
1393 }
1394 #ifdef LISP_FLOAT_TYPE
1395 if (isfloat_string (read_buffer))
1396 return make_float (atof (read_buffer));
1397 #endif
1398 }
1399
1400 return intern (read_buffer);
1401 }
1402 }
1403 }
1404 \f
1405 #ifdef LISP_FLOAT_TYPE
1406
1407 #define LEAD_INT 1
1408 #define DOT_CHAR 2
1409 #define TRAIL_INT 4
1410 #define E_CHAR 8
1411 #define EXP_INT 16
1412
1413 int
1414 isfloat_string (cp)
1415 register char *cp;
1416 {
1417 register state;
1418
1419 state = 0;
1420 if (*cp == '+' || *cp == '-')
1421 cp++;
1422
1423 if (*cp >= '0' && *cp <= '9')
1424 {
1425 state |= LEAD_INT;
1426 while (*cp >= '0' && *cp <= '9')
1427 cp++;
1428 }
1429 if (*cp == '.')
1430 {
1431 state |= DOT_CHAR;
1432 cp++;
1433 }
1434 if (*cp >= '0' && *cp <= '9')
1435 {
1436 state |= TRAIL_INT;
1437 while (*cp >= '0' && *cp <= '9')
1438 cp++;
1439 }
1440 if (*cp == 'e')
1441 {
1442 state |= E_CHAR;
1443 cp++;
1444 }
1445 if ((*cp == '+') || (*cp == '-'))
1446 cp++;
1447
1448 if (*cp >= '0' && *cp <= '9')
1449 {
1450 state |= EXP_INT;
1451 while (*cp >= '0' && *cp <= '9')
1452 cp++;
1453 }
1454 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
1455 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
1456 || state == (DOT_CHAR|TRAIL_INT)
1457 || state == (LEAD_INT|E_CHAR|EXP_INT)
1458 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
1459 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
1460 }
1461 #endif /* LISP_FLOAT_TYPE */
1462 \f
1463 static Lisp_Object
1464 read_vector (readcharfun)
1465 Lisp_Object readcharfun;
1466 {
1467 register int i;
1468 register int size;
1469 register Lisp_Object *ptr;
1470 register Lisp_Object tem, vector;
1471 register struct Lisp_Cons *otem;
1472 Lisp_Object len;
1473
1474 tem = read_list (1, readcharfun);
1475 len = Flength (tem);
1476 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
1477
1478
1479 size = XVECTOR (vector)->size;
1480 ptr = XVECTOR (vector)->contents;
1481 for (i = 0; i < size; i++)
1482 {
1483 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
1484 otem = XCONS (tem);
1485 tem = Fcdr (tem);
1486 free_cons (otem);
1487 }
1488 return vector;
1489 }
1490
1491 /* flag = 1 means check for ] to terminate rather than ) and .
1492 flag = -1 means check for starting with defun
1493 and make structure pure. */
1494
1495 static Lisp_Object
1496 read_list (flag, readcharfun)
1497 int flag;
1498 register Lisp_Object readcharfun;
1499 {
1500 /* -1 means check next element for defun,
1501 0 means don't check,
1502 1 means already checked and found defun. */
1503 int defunflag = flag < 0 ? -1 : 0;
1504 Lisp_Object val, tail;
1505 register Lisp_Object elt, tem;
1506 struct gcpro gcpro1, gcpro2;
1507 int cancel = 0;
1508
1509 val = Qnil;
1510 tail = Qnil;
1511
1512 while (1)
1513 {
1514 char ch;
1515 GCPRO2 (val, tail);
1516 elt = read1 (readcharfun, &ch);
1517 UNGCPRO;
1518
1519 /* If purifying, and the list starts with #$,
1520 return 0 instead. This is a doc string reference
1521 and it will be replaced anyway by Snarf-documentation,
1522 so don't waste pure space with it. */
1523 if (EQ (elt, Vload_file_name)
1524 && !NILP (Vpurify_flag) && NILP (Vdoc_file_name))
1525 cancel = 1;
1526
1527 if (ch)
1528 {
1529 if (flag > 0)
1530 {
1531 if (ch == ']')
1532 return val;
1533 Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil));
1534 }
1535 if (ch == ')')
1536 return val;
1537 if (ch == '.')
1538 {
1539 GCPRO2 (val, tail);
1540 if (!NILP (tail))
1541 XCONS (tail)->cdr = read0 (readcharfun);
1542 else
1543 val = read0 (readcharfun);
1544 read1 (readcharfun, &ch);
1545 UNGCPRO;
1546 if (ch == ')')
1547 return (cancel ? make_number (0) : val);
1548 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
1549 }
1550 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
1551 }
1552 tem = (read_pure && flag <= 0
1553 ? pure_cons (elt, Qnil)
1554 : Fcons (elt, Qnil));
1555 if (!NILP (tail))
1556 XCONS (tail)->cdr = tem;
1557 else
1558 val = tem;
1559 tail = tem;
1560 if (defunflag < 0)
1561 defunflag = EQ (elt, Qdefun);
1562 else if (defunflag > 0)
1563 read_pure = 1;
1564 }
1565 }
1566 \f
1567 Lisp_Object Vobarray;
1568 Lisp_Object initial_obarray;
1569
1570 /* oblookup stores the bucket number here, for the sake of Funintern. */
1571
1572 int oblookup_last_bucket_number;
1573
1574 static int hash_string ();
1575 Lisp_Object oblookup ();
1576
1577 /* Get an error if OBARRAY is not an obarray.
1578 If it is one, return it. */
1579
1580 Lisp_Object
1581 check_obarray (obarray)
1582 Lisp_Object obarray;
1583 {
1584 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
1585 {
1586 /* If Vobarray is now invalid, force it to be valid. */
1587 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
1588
1589 obarray = wrong_type_argument (Qvectorp, obarray);
1590 }
1591 return obarray;
1592 }
1593
1594 /* Intern the C string STR: return a symbol with that name,
1595 interned in the current obarray. */
1596
1597 Lisp_Object
1598 intern (str)
1599 char *str;
1600 {
1601 Lisp_Object tem;
1602 int len = strlen (str);
1603 Lisp_Object obarray;
1604
1605 obarray = Vobarray;
1606 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
1607 obarray = check_obarray (obarray);
1608 tem = oblookup (obarray, str, len);
1609 if (SYMBOLP (tem))
1610 return tem;
1611 return Fintern ((!NILP (Vpurify_flag)
1612 ? make_pure_string (str, len)
1613 : make_string (str, len)),
1614 obarray);
1615 }
1616 \f
1617 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
1618 "Return the canonical symbol whose name is STRING.\n\
1619 If there is none, one is created by this function and returned.\n\
1620 A second optional argument specifies the obarray to use;\n\
1621 it defaults to the value of `obarray'.")
1622 (str, obarray)
1623 Lisp_Object str, obarray;
1624 {
1625 register Lisp_Object tem, sym, *ptr;
1626
1627 if (NILP (obarray)) obarray = Vobarray;
1628 obarray = check_obarray (obarray);
1629
1630 CHECK_STRING (str, 0);
1631
1632 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1633 if (!INTEGERP (tem))
1634 return tem;
1635
1636 if (!NILP (Vpurify_flag))
1637 str = Fpurecopy (str);
1638 sym = Fmake_symbol (str);
1639
1640 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
1641 if (SYMBOLP (*ptr))
1642 XSYMBOL (sym)->next = XSYMBOL (*ptr);
1643 else
1644 XSYMBOL (sym)->next = 0;
1645 *ptr = sym;
1646 return sym;
1647 }
1648
1649 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
1650 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
1651 A second optional argument specifies the obarray to use;\n\
1652 it defaults to the value of `obarray'.")
1653 (str, obarray)
1654 Lisp_Object str, obarray;
1655 {
1656 register Lisp_Object tem;
1657
1658 if (NILP (obarray)) obarray = Vobarray;
1659 obarray = check_obarray (obarray);
1660
1661 CHECK_STRING (str, 0);
1662
1663 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1664 if (!INTEGERP (tem))
1665 return tem;
1666 return Qnil;
1667 }
1668 \f
1669 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
1670 "Delete the symbol named NAME, if any, from OBARRAY.\n\
1671 The value is t if a symbol was found and deleted, nil otherwise.\n\
1672 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
1673 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
1674 OBARRAY defaults to the value of the variable `obarray'.")
1675 (name, obarray)
1676 Lisp_Object name, obarray;
1677 {
1678 register Lisp_Object string, tem;
1679 int hash;
1680
1681 if (NILP (obarray)) obarray = Vobarray;
1682 obarray = check_obarray (obarray);
1683
1684 if (SYMBOLP (name))
1685 XSETSTRING (string, XSYMBOL (name)->name);
1686 else
1687 {
1688 CHECK_STRING (name, 0);
1689 string = name;
1690 }
1691
1692 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
1693 if (INTEGERP (tem))
1694 return Qnil;
1695 /* If arg was a symbol, don't delete anything but that symbol itself. */
1696 if (SYMBOLP (name) && !EQ (name, tem))
1697 return Qnil;
1698
1699 hash = oblookup_last_bucket_number;
1700
1701 if (EQ (XVECTOR (obarray)->contents[hash], tem))
1702 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
1703 else
1704 {
1705 Lisp_Object tail, following;
1706
1707 for (tail = XVECTOR (obarray)->contents[hash];
1708 XSYMBOL (tail)->next;
1709 tail = following)
1710 {
1711 XSETSYMBOL (following, XSYMBOL (tail)->next);
1712 if (EQ (following, tem))
1713 {
1714 XSYMBOL (tail)->next = XSYMBOL (following)->next;
1715 break;
1716 }
1717 }
1718 }
1719
1720 return Qt;
1721 }
1722 \f
1723 /* Return the symbol in OBARRAY whose names matches the string
1724 of SIZE characters at PTR. If there is no such symbol in OBARRAY,
1725 return nil.
1726
1727 Also store the bucket number in oblookup_last_bucket_number. */
1728
1729 Lisp_Object
1730 oblookup (obarray, ptr, size, hashp)
1731 Lisp_Object obarray;
1732 register char *ptr;
1733 register int size;
1734 int *hashp;
1735 {
1736 int hash;
1737 int obsize;
1738 register Lisp_Object tail;
1739 Lisp_Object bucket, tem;
1740
1741 if (!VECTORP (obarray)
1742 || (obsize = XVECTOR (obarray)->size) == 0)
1743 {
1744 obarray = check_obarray (obarray);
1745 obsize = XVECTOR (obarray)->size;
1746 }
1747 /* Combining next two lines breaks VMS C 2.3. */
1748 hash = hash_string (ptr, size);
1749 hash %= obsize;
1750 bucket = XVECTOR (obarray)->contents[hash];
1751 oblookup_last_bucket_number = hash;
1752 if (XFASTINT (bucket) == 0)
1753 ;
1754 else if (!SYMBOLP (bucket))
1755 error ("Bad data in guts of obarray"); /* Like CADR error message */
1756 else
1757 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
1758 {
1759 if (XSYMBOL (tail)->name->size == size
1760 && !bcmp (XSYMBOL (tail)->name->data, ptr, size))
1761 return tail;
1762 else if (XSYMBOL (tail)->next == 0)
1763 break;
1764 }
1765 XSETINT (tem, hash);
1766 return tem;
1767 }
1768
1769 static int
1770 hash_string (ptr, len)
1771 unsigned char *ptr;
1772 int len;
1773 {
1774 register unsigned char *p = ptr;
1775 register unsigned char *end = p + len;
1776 register unsigned char c;
1777 register int hash = 0;
1778
1779 while (p != end)
1780 {
1781 c = *p++;
1782 if (c >= 0140) c -= 40;
1783 hash = ((hash<<3) + (hash>>28) + c);
1784 }
1785 return hash & 07777777777;
1786 }
1787 \f
1788 void
1789 map_obarray (obarray, fn, arg)
1790 Lisp_Object obarray;
1791 int (*fn) ();
1792 Lisp_Object arg;
1793 {
1794 register int i;
1795 register Lisp_Object tail;
1796 CHECK_VECTOR (obarray, 1);
1797 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
1798 {
1799 tail = XVECTOR (obarray)->contents[i];
1800 if (XFASTINT (tail) != 0)
1801 while (1)
1802 {
1803 (*fn) (tail, arg);
1804 if (XSYMBOL (tail)->next == 0)
1805 break;
1806 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1807 }
1808 }
1809 }
1810
1811 mapatoms_1 (sym, function)
1812 Lisp_Object sym, function;
1813 {
1814 call1 (function, sym);
1815 }
1816
1817 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
1818 "Call FUNCTION on every symbol in OBARRAY.\n\
1819 OBARRAY defaults to the value of `obarray'.")
1820 (function, obarray)
1821 Lisp_Object function, obarray;
1822 {
1823 Lisp_Object tem;
1824
1825 if (NILP (obarray)) obarray = Vobarray;
1826 obarray = check_obarray (obarray);
1827
1828 map_obarray (obarray, mapatoms_1, function);
1829 return Qnil;
1830 }
1831
1832 #define OBARRAY_SIZE 1511
1833
1834 void
1835 init_obarray ()
1836 {
1837 Lisp_Object oblength;
1838 int hash;
1839 Lisp_Object *tem;
1840
1841 XSETFASTINT (oblength, OBARRAY_SIZE);
1842
1843 Qnil = Fmake_symbol (make_pure_string ("nil", 3));
1844 Vobarray = Fmake_vector (oblength, make_number (0));
1845 initial_obarray = Vobarray;
1846 staticpro (&initial_obarray);
1847 /* Intern nil in the obarray */
1848 /* These locals are to kludge around a pyramid compiler bug. */
1849 hash = hash_string ("nil", 3);
1850 /* Separate statement here to avoid VAXC bug. */
1851 hash %= OBARRAY_SIZE;
1852 tem = &XVECTOR (Vobarray)->contents[hash];
1853 *tem = Qnil;
1854
1855 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7));
1856 XSYMBOL (Qnil)->function = Qunbound;
1857 XSYMBOL (Qunbound)->value = Qunbound;
1858 XSYMBOL (Qunbound)->function = Qunbound;
1859
1860 Qt = intern ("t");
1861 XSYMBOL (Qnil)->value = Qnil;
1862 XSYMBOL (Qnil)->plist = Qnil;
1863 XSYMBOL (Qt)->value = Qt;
1864
1865 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
1866 Vpurify_flag = Qt;
1867
1868 Qvariable_documentation = intern ("variable-documentation");
1869
1870 read_buffer_size = 100;
1871 read_buffer = (char *) malloc (read_buffer_size);
1872 }
1873 \f
1874 void
1875 defsubr (sname)
1876 struct Lisp_Subr *sname;
1877 {
1878 Lisp_Object sym;
1879 sym = intern (sname->symbol_name);
1880 XSETSUBR (XSYMBOL (sym)->function, sname);
1881 }
1882
1883 #ifdef NOTDEF /* use fset in subr.el now */
1884 void
1885 defalias (sname, string)
1886 struct Lisp_Subr *sname;
1887 char *string;
1888 {
1889 Lisp_Object sym;
1890 sym = intern (string);
1891 XSETSUBR (XSYMBOL (sym)->function, sname);
1892 }
1893 #endif /* NOTDEF */
1894
1895 /* Define an "integer variable"; a symbol whose value is forwarded
1896 to a C variable of type int. Sample call: */
1897 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
1898 void
1899 defvar_int (namestring, address)
1900 char *namestring;
1901 int *address;
1902 {
1903 Lisp_Object sym, val;
1904 sym = intern (namestring);
1905 val = allocate_misc ();
1906 XMISCTYPE (val) = Lisp_Misc_Intfwd;
1907 XINTFWD (val)->intvar = address;
1908 XSYMBOL (sym)->value = val;
1909 }
1910
1911 /* Similar but define a variable whose value is T if address contains 1,
1912 NIL if address contains 0 */
1913 void
1914 defvar_bool (namestring, address)
1915 char *namestring;
1916 int *address;
1917 {
1918 Lisp_Object sym, val;
1919 sym = intern (namestring);
1920 val = allocate_misc ();
1921 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
1922 XBOOLFWD (val)->boolvar = address;
1923 XSYMBOL (sym)->value = val;
1924 }
1925
1926 /* Similar but define a variable whose value is the Lisp Object stored
1927 at address. Two versions: with and without gc-marking of the C
1928 variable. The nopro version is used when that variable will be
1929 gc-marked for some other reason, since marking the same slot twice
1930 can cause trouble with strings. */
1931 void
1932 defvar_lisp_nopro (namestring, address)
1933 char *namestring;
1934 Lisp_Object *address;
1935 {
1936 Lisp_Object sym, val;
1937 sym = intern (namestring);
1938 val = allocate_misc ();
1939 XMISCTYPE (val) = Lisp_Misc_Objfwd;
1940 XOBJFWD (val)->objvar = address;
1941 XSYMBOL (sym)->value = val;
1942 }
1943
1944 void
1945 defvar_lisp (namestring, address)
1946 char *namestring;
1947 Lisp_Object *address;
1948 {
1949 defvar_lisp_nopro (namestring, address);
1950 staticpro (address);
1951 }
1952
1953 #ifndef standalone
1954
1955 /* Similar but define a variable whose value is the Lisp Object stored in
1956 the current buffer. address is the address of the slot in the buffer
1957 that is current now. */
1958
1959 void
1960 defvar_per_buffer (namestring, address, type, doc)
1961 char *namestring;
1962 Lisp_Object *address;
1963 Lisp_Object type;
1964 char *doc;
1965 {
1966 Lisp_Object sym, val;
1967 int offset;
1968 extern struct buffer buffer_local_symbols;
1969
1970 sym = intern (namestring);
1971 val = allocate_misc ();
1972 offset = (char *)address - (char *)current_buffer;
1973
1974 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
1975 XBUFFER_OBJFWD (val)->offset = offset;
1976 XSYMBOL (sym)->value = val;
1977 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
1978 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
1979 if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0)
1980 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
1981 slot of buffer_local_flags */
1982 abort ();
1983 }
1984
1985 #endif /* standalone */
1986
1987 /* Similar but define a variable whose value is the Lisp Object stored
1988 at a particular offset in the current kboard object. */
1989
1990 void
1991 defvar_kboard (namestring, offset)
1992 char *namestring;
1993 int offset;
1994 {
1995 Lisp_Object sym, val;
1996 sym = intern (namestring);
1997 val = allocate_misc ();
1998 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
1999 XKBOARD_OBJFWD (val)->offset = offset;
2000 XSYMBOL (sym)->value = val;
2001 }
2002 \f
2003 init_lread ()
2004 {
2005 char *normal;
2006
2007 /* Compute the default load-path. */
2008 #ifdef CANNOT_DUMP
2009 normal = PATH_LOADSEARCH;
2010 Vload_path = decode_env_path (0, normal);
2011 #else
2012 if (NILP (Vpurify_flag))
2013 normal = PATH_LOADSEARCH;
2014 else
2015 normal = PATH_DUMPLOADSEARCH;
2016
2017 /* In a dumped Emacs, we normally have to reset the value of
2018 Vload_path from PATH_LOADSEARCH, since the value that was dumped
2019 uses ../lisp, instead of the path of the installed elisp
2020 libraries. However, if it appears that Vload_path was changed
2021 from the default before dumping, don't override that value. */
2022 if (initialized)
2023 {
2024 Lisp_Object dump_path;
2025
2026 dump_path = decode_env_path (0, PATH_DUMPLOADSEARCH);
2027 if (! NILP (Fequal (dump_path, Vload_path)))
2028 {
2029 Vload_path = decode_env_path (0, normal);
2030 if (!NILP (Vinstallation_directory))
2031 {
2032 /* Add to the path the lisp subdir of the
2033 installation dir, if it exists. */
2034 Lisp_Object tem, tem1;
2035 tem = Fexpand_file_name (build_string ("lisp"),
2036 Vinstallation_directory);
2037 tem1 = Ffile_exists_p (tem);
2038 if (!NILP (tem1))
2039 {
2040 if (NILP (Fmember (tem, Vload_path)))
2041 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2042 }
2043 else
2044 /* That dir doesn't exist, so add the build-time
2045 Lisp dirs instead. */
2046 Vload_path = nconc2 (Vload_path, dump_path);
2047
2048 /* Add site-list under the installation dir, if it exists. */
2049 tem = Fexpand_file_name (build_string ("site-lisp"),
2050 Vinstallation_directory);
2051 tem1 = Ffile_exists_p (tem);
2052 if (!NILP (tem1))
2053 {
2054 if (NILP (Fmember (tem, Vload_path)))
2055 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2056 }
2057 }
2058 }
2059 }
2060 else
2061 Vload_path = decode_env_path (0, normal);
2062 #endif
2063
2064 #ifndef WINDOWSNT
2065 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
2066 almost never correct, thereby causing a warning to be printed out that
2067 confuses users. Since PATH_LOADSEARCH is always overriden by the
2068 EMACSLOADPATH environment variable below, disable the warning on NT. */
2069
2070 /* Warn if dirs in the *standard* path don't exist. */
2071 {
2072 Lisp_Object path_tail;
2073
2074 for (path_tail = Vload_path;
2075 !NILP (path_tail);
2076 path_tail = XCONS (path_tail)->cdr)
2077 {
2078 Lisp_Object dirfile;
2079 dirfile = Fcar (path_tail);
2080 if (STRINGP (dirfile))
2081 {
2082 dirfile = Fdirectory_file_name (dirfile);
2083 if (access (XSTRING (dirfile)->data, 0) < 0)
2084 fprintf (stderr,
2085 "Warning: Lisp directory `%s' does not exist.\n",
2086 XSTRING (Fcar (path_tail))->data);
2087 }
2088 }
2089 }
2090 #endif /* WINDOWSNT */
2091
2092 /* If the EMACSLOADPATH environment variable is set, use its value.
2093 This doesn't apply if we're dumping. */
2094 if (NILP (Vpurify_flag)
2095 && egetenv ("EMACSLOADPATH"))
2096 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
2097
2098 Vvalues = Qnil;
2099
2100 load_in_progress = 0;
2101
2102 load_descriptor_list = Qnil;
2103 }
2104
2105 void
2106 syms_of_lread ()
2107 {
2108 defsubr (&Sread);
2109 defsubr (&Sread_from_string);
2110 defsubr (&Sintern);
2111 defsubr (&Sintern_soft);
2112 defsubr (&Sunintern);
2113 defsubr (&Sload);
2114 defsubr (&Seval_buffer);
2115 defsubr (&Seval_region);
2116 defsubr (&Sread_char);
2117 defsubr (&Sread_char_exclusive);
2118 defsubr (&Sread_event);
2119 defsubr (&Sget_file_char);
2120 defsubr (&Smapatoms);
2121
2122 DEFVAR_LISP ("obarray", &Vobarray,
2123 "Symbol table for use by `intern' and `read'.\n\
2124 It is a vector whose length ought to be prime for best results.\n\
2125 The vector's contents don't make sense if examined from Lisp programs;\n\
2126 to find all the symbols in an obarray, use `mapatoms'.");
2127
2128 DEFVAR_LISP ("values", &Vvalues,
2129 "List of values of all expressions which were read, evaluated and printed.\n\
2130 Order is reverse chronological.");
2131
2132 DEFVAR_LISP ("standard-input", &Vstandard_input,
2133 "Stream for read to get input from.\n\
2134 See documentation of `read' for possible values.");
2135 Vstandard_input = Qt;
2136
2137 DEFVAR_LISP ("load-path", &Vload_path,
2138 "*List of directories to search for files to load.\n\
2139 Each element is a string (directory name) or nil (try default directory).\n\
2140 Initialized based on EMACSLOADPATH environment variable, if any,\n\
2141 otherwise to default specified by file `paths.h' when Emacs was built.");
2142
2143 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
2144 "Non-nil iff inside of `load'.");
2145
2146 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
2147 "An alist of expressions to be evalled when particular files are loaded.\n\
2148 Each element looks like (FILENAME FORMS...).\n\
2149 When `load' is run and the file-name argument is FILENAME,\n\
2150 the FORMS in the corresponding element are executed at the end of loading.\n\n\
2151 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
2152 with no directory specified, since that is how `load' is normally called.\n\
2153 An error in FORMS does not undo the load,\n\
2154 but does prevent execution of the rest of the FORMS.");
2155 Vafter_load_alist = Qnil;
2156
2157 DEFVAR_LISP ("load-history", &Vload_history,
2158 "Alist mapping source file names to symbols and features.\n\
2159 Each alist element is a list that starts with a file name,\n\
2160 except for one element (optional) that starts with nil and describes\n\
2161 definitions evaluated from buffers not visiting files.\n\
2162 The remaining elements of each list are symbols defined as functions\n\
2163 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
2164 Vload_history = Qnil;
2165
2166 DEFVAR_LISP ("load-file-name", &Vload_file_name,
2167 "Full name of file being loaded by `load'.");
2168 Vload_file_name = Qnil;
2169
2170 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
2171 "Used for internal purposes by `load'.");
2172 Vcurrent_load_list = Qnil;
2173
2174 DEFVAR_LISP ("load-read-function", &Vload_read_function,
2175 "Function used by `load' and `eval-region' for reading expressions.\n\
2176 The default is nil, which means use the function `read'.");
2177 Vload_read_function = Qnil;
2178
2179 load_descriptor_list = Qnil;
2180 staticpro (&load_descriptor_list);
2181
2182 Qcurrent_load_list = intern ("current-load-list");
2183 staticpro (&Qcurrent_load_list);
2184
2185 Qstandard_input = intern ("standard-input");
2186 staticpro (&Qstandard_input);
2187
2188 Qread_char = intern ("read-char");
2189 staticpro (&Qread_char);
2190
2191 Qget_file_char = intern ("get-file-char");
2192 staticpro (&Qget_file_char);
2193
2194 Qascii_character = intern ("ascii-character");
2195 staticpro (&Qascii_character);
2196
2197 Qload = intern ("load");
2198 staticpro (&Qload);
2199
2200 Qload_file_name = intern ("load-file-name");
2201 staticpro (&Qload_file_name);
2202 }