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