(read1): Round size of bool-vector properly.
[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 (compiled; note, source file is newer)...done",
514 XSTRING (file)->data);
515 else if (compiled)
516 message ("Loading %s (compiled)...done", 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 If the directory starts with /:, remove that. */
640 if (XSTRING (filename)->size > 2
641 && XSTRING (filename)->data[0] == '/'
642 && XSTRING (filename)->data[1] == ':')
643 {
644 strncpy (fn, XSTRING (filename)->data + 2,
645 XSTRING (filename)->size - 2);
646 fn[XSTRING (filename)->size - 2] = 0;
647 }
648 else
649 {
650 strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size);
651 fn[XSTRING (filename)->size] = 0;
652 }
653
654 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
655 strncat (fn, nsuffix, lsuffix);
656
657 /* Ignore file if it's a directory. */
658 if (stat (fn, &st) >= 0
659 && (st.st_mode & S_IFMT) != S_IFDIR)
660 {
661 /* Check that we can access or open it. */
662 if (exec_only)
663 fd = (access (fn, X_OK) == 0) ? 1 : -1;
664 else
665 fd = open (fn, O_RDONLY, 0);
666
667 if (fd >= 0)
668 {
669 /* We succeeded; return this descriptor and filename. */
670 if (storeptr)
671 *storeptr = build_string (fn);
672 UNGCPRO;
673 return fd;
674 }
675 }
676
677 /* Advance to next suffix. */
678 if (esuffix == 0)
679 break;
680 nsuffix += lsuffix + 1;
681 }
682 if (absolute)
683 break;
684 }
685
686 UNGCPRO;
687 return -1;
688 }
689
690 \f
691 /* Merge the list we've accumulated of globals from the current input source
692 into the load_history variable. The details depend on whether
693 the source has an associated file name or not. */
694
695 static void
696 build_load_history (stream, source)
697 FILE *stream;
698 Lisp_Object source;
699 {
700 register Lisp_Object tail, prev, newelt;
701 register Lisp_Object tem, tem2;
702 register int foundit, loading;
703
704 /* Don't bother recording anything for preloaded files. */
705 if (!NILP (Vpurify_flag))
706 return;
707
708 loading = stream || !NARROWED;
709
710 tail = Vload_history;
711 prev = Qnil;
712 foundit = 0;
713 while (!NILP (tail))
714 {
715 tem = Fcar (tail);
716
717 /* Find the feature's previous assoc list... */
718 if (!NILP (Fequal (source, Fcar (tem))))
719 {
720 foundit = 1;
721
722 /* If we're loading, remove it. */
723 if (loading)
724 {
725 if (NILP (prev))
726 Vload_history = Fcdr (tail);
727 else
728 Fsetcdr (prev, Fcdr (tail));
729 }
730
731 /* Otherwise, cons on new symbols that are not already members. */
732 else
733 {
734 tem2 = Vcurrent_load_list;
735
736 while (CONSP (tem2))
737 {
738 newelt = Fcar (tem2);
739
740 if (NILP (Fmemq (newelt, tem)))
741 Fsetcar (tail, Fcons (Fcar (tem),
742 Fcons (newelt, Fcdr (tem))));
743
744 tem2 = Fcdr (tem2);
745 QUIT;
746 }
747 }
748 }
749 else
750 prev = tail;
751 tail = Fcdr (tail);
752 QUIT;
753 }
754
755 /* If we're loading, cons the new assoc onto the front of load-history,
756 the most-recently-loaded position. Also do this if we didn't find
757 an existing member for the current source. */
758 if (loading || !foundit)
759 Vload_history = Fcons (Fnreverse (Vcurrent_load_list),
760 Vload_history);
761 }
762
763 Lisp_Object
764 unreadpure () /* Used as unwind-protect function in readevalloop */
765 {
766 read_pure = 0;
767 return Qnil;
768 }
769
770 static void
771 readevalloop (readcharfun, stream, sourcename, evalfun, printflag)
772 Lisp_Object readcharfun;
773 FILE *stream;
774 Lisp_Object sourcename;
775 Lisp_Object (*evalfun) ();
776 int printflag;
777 {
778 register int c;
779 register Lisp_Object val;
780 int count = specpdl_ptr - specpdl;
781 struct gcpro gcpro1;
782 struct buffer *b = 0;
783
784 if (BUFFERP (readcharfun))
785 b = XBUFFER (readcharfun);
786 else if (MARKERP (readcharfun))
787 b = XMARKER (readcharfun)->buffer;
788
789 specbind (Qstandard_input, readcharfun);
790 specbind (Qcurrent_load_list, Qnil);
791
792 GCPRO1 (sourcename);
793
794 LOADHIST_ATTACH (sourcename);
795
796 while (1)
797 {
798 if (b != 0 && NILP (b->name))
799 error ("Reading from killed buffer");
800
801 instream = stream;
802 c = READCHAR;
803 if (c == ';')
804 {
805 while ((c = READCHAR) != '\n' && c != -1);
806 continue;
807 }
808 if (c < 0) break;
809
810 /* Ignore whitespace here, so we can detect eof. */
811 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
812 continue;
813
814 if (!NILP (Vpurify_flag) && c == '(')
815 {
816 int count1 = specpdl_ptr - specpdl;
817 record_unwind_protect (unreadpure, Qnil);
818 val = read_list (-1, readcharfun);
819 unbind_to (count1, Qnil);
820 }
821 else
822 {
823 UNREAD (c);
824 read_objects = Qnil;
825 if (NILP (Vload_read_function))
826 val = read0 (readcharfun);
827 else
828 val = call1 (Vload_read_function, readcharfun);
829 }
830
831 val = (*evalfun) (val);
832 if (printflag)
833 {
834 Vvalues = Fcons (val, Vvalues);
835 if (EQ (Vstandard_output, Qt))
836 Fprin1 (val, Qnil);
837 else
838 Fprint (val, Qnil);
839 }
840 }
841
842 build_load_history (stream, sourcename);
843 UNGCPRO;
844
845 unbind_to (count, Qnil);
846 }
847
848 #ifndef standalone
849
850 DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 2, "",
851 "Execute the current buffer as Lisp code.\n\
852 Programs can pass two arguments, BUFFER and PRINTFLAG.\n\
853 BUFFER is the buffer to evaluate (nil means use current buffer).\n\
854 PRINTFLAG controls printing of output:\n\
855 nil means discard it; anything else is stream for print.\n\
856 \n\
857 If there is no error, point does not move. If there is an error,\n\
858 point remains at the end of the last character read from the buffer.")
859 (buffer, printflag)
860 Lisp_Object buffer, printflag;
861 {
862 int count = specpdl_ptr - specpdl;
863 Lisp_Object tem, buf;
864
865 if (NILP (buffer))
866 buf = Fcurrent_buffer ();
867 else
868 buf = Fget_buffer (buffer);
869 if (NILP (buf))
870 error ("No such buffer.");
871
872 if (NILP (printflag))
873 tem = Qsymbolp;
874 else
875 tem = printflag;
876 specbind (Qstandard_output, tem);
877 record_unwind_protect (save_excursion_restore, save_excursion_save ());
878 BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
879 readevalloop (buf, 0, XBUFFER (buf)->filename, Feval, !NILP (printflag));
880 unbind_to (count, Qnil);
881
882 return Qnil;
883 }
884
885 #if 0
886 DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
887 "Execute the current buffer as Lisp code.\n\
888 Programs can pass argument PRINTFLAG which controls printing of output:\n\
889 nil means discard it; anything else is stream for print.\n\
890 \n\
891 If there is no error, point does not move. If there is an error,\n\
892 point remains at the end of the last character read from the buffer.")
893 (printflag)
894 Lisp_Object printflag;
895 {
896 int count = specpdl_ptr - specpdl;
897 Lisp_Object tem, cbuf;
898
899 cbuf = Fcurrent_buffer ()
900
901 if (NILP (printflag))
902 tem = Qsymbolp;
903 else
904 tem = printflag;
905 specbind (Qstandard_output, tem);
906 record_unwind_protect (save_excursion_restore, save_excursion_save ());
907 SET_PT (BEGV);
908 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
909 return unbind_to (count, Qnil);
910 }
911 #endif
912
913 DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r",
914 "Execute the region as Lisp code.\n\
915 When called from programs, expects two arguments,\n\
916 giving starting and ending indices in the current buffer\n\
917 of the text to be executed.\n\
918 Programs can pass third argument PRINTFLAG which controls output:\n\
919 nil means discard it; anything else is stream for printing it.\n\
920 \n\
921 If there is no error, point does not move. If there is an error,\n\
922 point remains at the end of the last character read from the buffer.")
923 (start, end, printflag)
924 Lisp_Object start, end, printflag;
925 {
926 int count = specpdl_ptr - specpdl;
927 Lisp_Object tem, cbuf;
928
929 cbuf = Fcurrent_buffer ();
930
931 if (NILP (printflag))
932 tem = Qsymbolp;
933 else
934 tem = printflag;
935 specbind (Qstandard_output, tem);
936
937 if (NILP (printflag))
938 record_unwind_protect (save_excursion_restore, save_excursion_save ());
939 record_unwind_protect (save_restriction_restore, save_restriction_save ());
940
941 /* This both uses start and checks its type. */
942 Fgoto_char (start);
943 Fnarrow_to_region (make_number (BEGV), end);
944 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
945
946 return unbind_to (count, Qnil);
947 }
948
949 #endif /* standalone */
950 \f
951 DEFUN ("read", Fread, Sread, 0, 1, 0,
952 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
953 If STREAM is nil, use the value of `standard-input' (which see).\n\
954 STREAM or the value of `standard-input' may be:\n\
955 a buffer (read from point and advance it)\n\
956 a marker (read from where it points and advance it)\n\
957 a function (call it with no arguments for each character,\n\
958 call it with a char as argument to push a char back)\n\
959 a string (takes text from string, starting at the beginning)\n\
960 t (read text line using minibuffer and use it).")
961 (stream)
962 Lisp_Object stream;
963 {
964 extern Lisp_Object Fread_minibuffer ();
965
966 if (NILP (stream))
967 stream = Vstandard_input;
968 if (EQ (stream, Qt))
969 stream = Qread_char;
970
971 new_backquote_flag = 0;
972 read_objects = Qnil;
973
974 #ifndef standalone
975 if (EQ (stream, Qread_char))
976 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
977 #endif
978
979 if (STRINGP (stream))
980 return Fcar (Fread_from_string (stream, Qnil, Qnil));
981
982 return read0 (stream);
983 }
984
985 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
986 "Read one Lisp expression which is represented as text by STRING.\n\
987 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
988 START and END optionally delimit a substring of STRING from which to read;\n\
989 they default to 0 and (length STRING) respectively.")
990 (string, start, end)
991 Lisp_Object string, start, end;
992 {
993 int startval, endval;
994 Lisp_Object tem;
995
996 CHECK_STRING (string,0);
997
998 if (NILP (end))
999 endval = XSTRING (string)->size;
1000 else
1001 { CHECK_NUMBER (end,2);
1002 endval = XINT (end);
1003 if (endval < 0 || endval > XSTRING (string)->size)
1004 args_out_of_range (string, end);
1005 }
1006
1007 if (NILP (start))
1008 startval = 0;
1009 else
1010 { CHECK_NUMBER (start,1);
1011 startval = XINT (start);
1012 if (startval < 0 || startval > endval)
1013 args_out_of_range (string, start);
1014 }
1015
1016 read_from_string_index = startval;
1017 read_from_string_limit = endval;
1018
1019 new_backquote_flag = 0;
1020 read_objects = Qnil;
1021
1022 tem = read0 (string);
1023 return Fcons (tem, make_number (read_from_string_index));
1024 }
1025 \f
1026 /* Use this for recursive reads, in contexts where internal tokens
1027 are not allowed. */
1028 static Lisp_Object
1029 read0 (readcharfun)
1030 Lisp_Object readcharfun;
1031 {
1032 register Lisp_Object val;
1033 char c;
1034
1035 val = read1 (readcharfun, &c, 0);
1036 if (c)
1037 Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil));
1038
1039 return val;
1040 }
1041 \f
1042 static int read_buffer_size;
1043 static char *read_buffer;
1044
1045 static int
1046 read_escape (readcharfun)
1047 Lisp_Object readcharfun;
1048 {
1049 register int c = READCHAR;
1050 switch (c)
1051 {
1052 case -1:
1053 error ("End of file");
1054
1055 case 'a':
1056 return '\007';
1057 case 'b':
1058 return '\b';
1059 case 'd':
1060 return 0177;
1061 case 'e':
1062 return 033;
1063 case 'f':
1064 return '\f';
1065 case 'n':
1066 return '\n';
1067 case 'r':
1068 return '\r';
1069 case 't':
1070 return '\t';
1071 case 'v':
1072 return '\v';
1073 case '\n':
1074 return -1;
1075
1076 case 'M':
1077 c = READCHAR;
1078 if (c != '-')
1079 error ("Invalid escape character syntax");
1080 c = READCHAR;
1081 if (c == '\\')
1082 c = read_escape (readcharfun);
1083 return c | meta_modifier;
1084
1085 case 'S':
1086 c = READCHAR;
1087 if (c != '-')
1088 error ("Invalid escape character syntax");
1089 c = READCHAR;
1090 if (c == '\\')
1091 c = read_escape (readcharfun);
1092 return c | shift_modifier;
1093
1094 case 'H':
1095 c = READCHAR;
1096 if (c != '-')
1097 error ("Invalid escape character syntax");
1098 c = READCHAR;
1099 if (c == '\\')
1100 c = read_escape (readcharfun);
1101 return c | hyper_modifier;
1102
1103 case 'A':
1104 c = READCHAR;
1105 if (c != '-')
1106 error ("Invalid escape character syntax");
1107 c = READCHAR;
1108 if (c == '\\')
1109 c = read_escape (readcharfun);
1110 return c | alt_modifier;
1111
1112 case 's':
1113 c = READCHAR;
1114 if (c != '-')
1115 error ("Invalid escape character syntax");
1116 c = READCHAR;
1117 if (c == '\\')
1118 c = read_escape (readcharfun);
1119 return c | super_modifier;
1120
1121 case 'C':
1122 c = READCHAR;
1123 if (c != '-')
1124 error ("Invalid escape character syntax");
1125 case '^':
1126 c = READCHAR;
1127 if (c == '\\')
1128 c = read_escape (readcharfun);
1129 if ((c & 0177) == '?')
1130 return 0177 | c;
1131 /* ASCII control chars are made from letters (both cases),
1132 as well as the non-letters within 0100...0137. */
1133 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1134 return (c & (037 | ~0177));
1135 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1136 return (c & (037 | ~0177));
1137 else
1138 return c | ctrl_modifier;
1139
1140 case '0':
1141 case '1':
1142 case '2':
1143 case '3':
1144 case '4':
1145 case '5':
1146 case '6':
1147 case '7':
1148 /* An octal escape, as in ANSI C. */
1149 {
1150 register int i = c - '0';
1151 register int count = 0;
1152 while (++count < 3)
1153 {
1154 if ((c = READCHAR) >= '0' && c <= '7')
1155 {
1156 i *= 8;
1157 i += c - '0';
1158 }
1159 else
1160 {
1161 UNREAD (c);
1162 break;
1163 }
1164 }
1165 return i;
1166 }
1167
1168 case 'x':
1169 /* A hex escape, as in ANSI C. */
1170 {
1171 int i = 0;
1172 while (1)
1173 {
1174 c = READCHAR;
1175 if (c >= '0' && c <= '9')
1176 {
1177 i *= 16;
1178 i += c - '0';
1179 }
1180 else if ((c >= 'a' && c <= 'f')
1181 || (c >= 'A' && c <= 'F'))
1182 {
1183 i *= 16;
1184 if (c >= 'a' && c <= 'f')
1185 i += c - 'a' + 10;
1186 else
1187 i += c - 'A' + 10;
1188 }
1189 else
1190 {
1191 UNREAD (c);
1192 break;
1193 }
1194 }
1195 return i;
1196 }
1197
1198 default:
1199 return c;
1200 }
1201 }
1202
1203 /* If the next token is ')' or ']' or '.', we store that character
1204 in *PCH and the return value is not interesting. Else, we store
1205 zero in *PCH and we read and return one lisp object.
1206
1207 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1208
1209 static Lisp_Object
1210 read1 (readcharfun, pch, first_in_list)
1211 register Lisp_Object readcharfun;
1212 char *pch;
1213 int first_in_list;
1214 {
1215 register int c;
1216 int uninterned_symbol = 0;
1217
1218 *pch = 0;
1219
1220 retry:
1221
1222 c = READCHAR;
1223 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1224
1225 switch (c)
1226 {
1227 case '(':
1228 return read_list (0, readcharfun);
1229
1230 case '[':
1231 return read_vector (readcharfun);
1232
1233 case ')':
1234 case ']':
1235 {
1236 *pch = c;
1237 return Qnil;
1238 }
1239
1240 case '#':
1241 c = READCHAR;
1242 if (c == '^')
1243 {
1244 c = READCHAR;
1245 if (c == '[')
1246 {
1247 Lisp_Object tmp;
1248 tmp = read_vector (readcharfun);
1249 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1250 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1251 error ("Invalid size char-table");
1252 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1253 return tmp;
1254 }
1255 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1256 }
1257 if (c == '&')
1258 {
1259 Lisp_Object length;
1260 length = read1 (readcharfun, pch, first_in_list);
1261 c = READCHAR;
1262 if (c == '"')
1263 {
1264 Lisp_Object tmp, val;
1265 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1266 / BITS_PER_CHAR);
1267
1268 UNREAD (c);
1269 tmp = read1 (readcharfun, pch, first_in_list);
1270 if (size_in_chars != XSTRING (tmp)->size
1271 /* We used to print 1 char too many
1272 when the number of bits was a multiple of 8.
1273 Accept such input in case it came from an old version. */
1274 && ! (XFASTINT (length)
1275 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1276 Fsignal (Qinvalid_read_syntax,
1277 Fcons (make_string ("#&...", 5), Qnil));
1278
1279 val = Fmake_bool_vector (length, Qnil);
1280 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1281 size_in_chars);
1282 return val;
1283 }
1284 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1285 Qnil));
1286 }
1287 if (c == '[')
1288 {
1289 /* Accept compiled functions at read-time so that we don't have to
1290 build them using function calls. */
1291 Lisp_Object tmp;
1292 tmp = read_vector (readcharfun);
1293 return Fmake_byte_code (XVECTOR (tmp)->size,
1294 XVECTOR (tmp)->contents);
1295 }
1296 #ifdef USE_TEXT_PROPERTIES
1297 if (c == '(')
1298 {
1299 Lisp_Object tmp;
1300 struct gcpro gcpro1;
1301 char ch;
1302
1303 /* Read the string itself. */
1304 tmp = read1 (readcharfun, &ch, 0);
1305 if (ch != 0 || !STRINGP (tmp))
1306 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1307 GCPRO1 (tmp);
1308 /* Read the intervals and their properties. */
1309 while (1)
1310 {
1311 Lisp_Object beg, end, plist;
1312
1313 beg = read1 (readcharfun, &ch, 0);
1314 if (ch == ')')
1315 break;
1316 if (ch == 0)
1317 end = read1 (readcharfun, &ch, 0);
1318 if (ch == 0)
1319 plist = read1 (readcharfun, &ch, 0);
1320 if (ch)
1321 Fsignal (Qinvalid_read_syntax,
1322 Fcons (build_string ("invalid string property list"),
1323 Qnil));
1324 Fset_text_properties (beg, end, plist, tmp);
1325 }
1326 UNGCPRO;
1327 return tmp;
1328 }
1329 #endif
1330 /* #@NUMBER is used to skip NUMBER following characters.
1331 That's used in .elc files to skip over doc strings
1332 and function definitions. */
1333 if (c == '@')
1334 {
1335 int i, nskip = 0;
1336
1337 /* Read a decimal integer. */
1338 while ((c = READCHAR) >= 0
1339 && c >= '0' && c <= '9')
1340 {
1341 nskip *= 10;
1342 nskip += c - '0';
1343 }
1344 if (c >= 0)
1345 UNREAD (c);
1346
1347 #ifndef DOS_NT /* I don't know if filepos works right on MSDOS and Windoze. */
1348 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1349 {
1350 /* If we are supposed to force doc strings into core right now,
1351 record the last string that we skipped,
1352 and record where in the file it comes from. */
1353 if (saved_doc_string_size == 0)
1354 {
1355 saved_doc_string_size = nskip + 100;
1356 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1357 }
1358 if (nskip > saved_doc_string_size)
1359 {
1360 saved_doc_string_size = nskip + 100;
1361 saved_doc_string = (char *) xrealloc (saved_doc_string,
1362 saved_doc_string_size);
1363 }
1364
1365 saved_doc_string_position = ftell (instream);
1366
1367 /* Copy that many characters into saved_doc_string. */
1368 for (i = 0; i < nskip && c >= 0; i++)
1369 saved_doc_string[i] = c = READCHAR;
1370
1371 saved_doc_string_length = i;
1372 }
1373 else
1374 #endif /* not DOS_NT */
1375 {
1376 /* Skip that many characters. */
1377 for (i = 0; i < nskip && c >= 0; i++)
1378 c = READCHAR;
1379 }
1380 goto retry;
1381 }
1382 if (c == '$')
1383 return Vload_file_name;
1384 if (c == '\'')
1385 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1386 /* #:foo is the uninterned symbol named foo. */
1387 if (c == ':')
1388 {
1389 uninterned_symbol = 1;
1390 c = READCHAR;
1391 goto default_label;
1392 }
1393 /* Reader forms that can reuse previously read objects. */
1394 if (c >= '0' && c <= '9')
1395 {
1396 int n = 0;
1397 Lisp_Object tem;
1398
1399 /* Read a non-negative integer. */
1400 while (c >= '0' && c <= '9')
1401 {
1402 n *= 10;
1403 n += c - '0';
1404 c = READCHAR;
1405 }
1406 /* #n=object returns object, but associates it with n for #n#. */
1407 if (c == '=')
1408 {
1409 tem = read0 (readcharfun);
1410 read_objects = Fcons (Fcons (make_number (n), tem), read_objects);
1411 return tem;
1412 }
1413 /* #n# returns a previously read object. */
1414 if (c == '#')
1415 {
1416 tem = Fassq (make_number (n), read_objects);
1417 if (CONSP (tem))
1418 return XCDR (tem);
1419 /* Fall through to error message. */
1420 }
1421 /* Fall through to error message. */
1422 }
1423
1424 UNREAD (c);
1425 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1426
1427 case ';':
1428 while ((c = READCHAR) >= 0 && c != '\n');
1429 goto retry;
1430
1431 case '\'':
1432 {
1433 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1434 }
1435
1436 case '`':
1437 if (first_in_list)
1438 goto default_label;
1439 else
1440 {
1441 Lisp_Object value;
1442
1443 new_backquote_flag = 1;
1444 value = read0 (readcharfun);
1445 new_backquote_flag = 0;
1446
1447 return Fcons (Qbackquote, Fcons (value, Qnil));
1448 }
1449
1450 case ',':
1451 if (new_backquote_flag)
1452 {
1453 Lisp_Object comma_type = Qnil;
1454 Lisp_Object value;
1455 int ch = READCHAR;
1456
1457 if (ch == '@')
1458 comma_type = Qcomma_at;
1459 else if (ch == '.')
1460 comma_type = Qcomma_dot;
1461 else
1462 {
1463 if (ch >= 0) UNREAD (ch);
1464 comma_type = Qcomma;
1465 }
1466
1467 new_backquote_flag = 0;
1468 value = read0 (readcharfun);
1469 new_backquote_flag = 1;
1470 return Fcons (comma_type, Fcons (value, Qnil));
1471 }
1472 else
1473 goto default_label;
1474
1475 case '?':
1476 {
1477 register Lisp_Object val;
1478
1479 c = READCHAR;
1480 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1481
1482 if (c == '\\')
1483 XSETINT (val, read_escape (readcharfun));
1484 else
1485 XSETINT (val, c);
1486
1487 return val;
1488 }
1489
1490 case '\"':
1491 {
1492 register char *p = read_buffer;
1493 register char *end = read_buffer + read_buffer_size;
1494 register int c;
1495 int cancel = 0;
1496
1497 while ((c = READCHAR) >= 0
1498 && c != '\"')
1499 {
1500 if (p == end)
1501 {
1502 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1503 p += new - read_buffer;
1504 read_buffer += new - read_buffer;
1505 end = read_buffer + read_buffer_size;
1506 }
1507 if (c == '\\')
1508 c = read_escape (readcharfun);
1509 /* c is -1 if \ newline has just been seen */
1510 if (c == -1)
1511 {
1512 if (p == read_buffer)
1513 cancel = 1;
1514 }
1515 else
1516 {
1517 /* Allow `\C- ' and `\C-?'. */
1518 if (c == (CHAR_CTL | ' '))
1519 c = 0;
1520 else if (c == (CHAR_CTL | '?'))
1521 c = 127;
1522
1523 if (c & CHAR_META)
1524 /* Move the meta bit to the right place for a string. */
1525 c = (c & ~CHAR_META) | 0x80;
1526 if (c & ~0xff)
1527 error ("Invalid modifier in string");
1528 *p++ = c;
1529 }
1530 }
1531 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1532
1533 /* If purifying, and string starts with \ newline,
1534 return zero instead. This is for doc strings
1535 that we are really going to find in etc/DOC.nn.nn */
1536 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
1537 return make_number (0);
1538
1539 if (read_pure)
1540 return make_pure_string (read_buffer, p - read_buffer);
1541 else
1542 return make_string (read_buffer, p - read_buffer);
1543 }
1544
1545 case '.':
1546 {
1547 #ifdef LISP_FLOAT_TYPE
1548 /* If a period is followed by a number, then we should read it
1549 as a floating point number. Otherwise, it denotes a dotted
1550 pair. */
1551 int next_char = READCHAR;
1552 UNREAD (next_char);
1553
1554 if (! (next_char >= '0' && next_char <= '9'))
1555 #endif
1556 {
1557 *pch = c;
1558 return Qnil;
1559 }
1560
1561 /* Otherwise, we fall through! Note that the atom-reading loop
1562 below will now loop at least once, assuring that we will not
1563 try to UNREAD two characters in a row. */
1564 }
1565 default:
1566 default_label:
1567 if (c <= 040) goto retry;
1568 {
1569 register char *p = read_buffer;
1570 int quoted = 0;
1571
1572 {
1573 register char *end = read_buffer + read_buffer_size;
1574
1575 while (c > 040 &&
1576 !(c == '\"' || c == '\'' || c == ';' || c == '?'
1577 || c == '(' || c == ')'
1578 #ifndef LISP_FLOAT_TYPE
1579 /* If we have floating-point support, then we need
1580 to allow <digits><dot><digits>. */
1581 || c =='.'
1582 #endif /* not LISP_FLOAT_TYPE */
1583 || c == '[' || c == ']' || c == '#'
1584 ))
1585 {
1586 if (p == end)
1587 {
1588 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1589 p += new - read_buffer;
1590 read_buffer += new - read_buffer;
1591 end = read_buffer + read_buffer_size;
1592 }
1593 if (c == '\\')
1594 {
1595 c = READCHAR;
1596 quoted = 1;
1597 }
1598 *p++ = c;
1599 c = READCHAR;
1600 }
1601
1602 if (p == end)
1603 {
1604 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1605 p += new - read_buffer;
1606 read_buffer += new - read_buffer;
1607 /* end = read_buffer + read_buffer_size; */
1608 }
1609 *p = 0;
1610 if (c >= 0)
1611 UNREAD (c);
1612 }
1613
1614 if (!quoted && !uninterned_symbol)
1615 {
1616 register char *p1;
1617 register Lisp_Object val;
1618 p1 = read_buffer;
1619 if (*p1 == '+' || *p1 == '-') p1++;
1620 /* Is it an integer? */
1621 if (p1 != p)
1622 {
1623 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
1624 #ifdef LISP_FLOAT_TYPE
1625 /* Integers can have trailing decimal points. */
1626 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
1627 #endif
1628 if (p1 == p)
1629 /* It is an integer. */
1630 {
1631 #ifdef LISP_FLOAT_TYPE
1632 if (p1[-1] == '.')
1633 p1[-1] = '\0';
1634 #endif
1635 if (sizeof (int) == sizeof (EMACS_INT))
1636 XSETINT (val, atoi (read_buffer));
1637 else if (sizeof (long) == sizeof (EMACS_INT))
1638 XSETINT (val, atol (read_buffer));
1639 else
1640 abort ();
1641 return val;
1642 }
1643 }
1644 #ifdef LISP_FLOAT_TYPE
1645 if (isfloat_string (read_buffer))
1646 return make_float (atof (read_buffer));
1647 #endif
1648 }
1649
1650 if (uninterned_symbol)
1651 return make_symbol (read_buffer);
1652 else
1653 return intern (read_buffer);
1654 }
1655 }
1656 }
1657 \f
1658 #ifdef LISP_FLOAT_TYPE
1659
1660 #define LEAD_INT 1
1661 #define DOT_CHAR 2
1662 #define TRAIL_INT 4
1663 #define E_CHAR 8
1664 #define EXP_INT 16
1665
1666 int
1667 isfloat_string (cp)
1668 register char *cp;
1669 {
1670 register state;
1671
1672 state = 0;
1673 if (*cp == '+' || *cp == '-')
1674 cp++;
1675
1676 if (*cp >= '0' && *cp <= '9')
1677 {
1678 state |= LEAD_INT;
1679 while (*cp >= '0' && *cp <= '9')
1680 cp++;
1681 }
1682 if (*cp == '.')
1683 {
1684 state |= DOT_CHAR;
1685 cp++;
1686 }
1687 if (*cp >= '0' && *cp <= '9')
1688 {
1689 state |= TRAIL_INT;
1690 while (*cp >= '0' && *cp <= '9')
1691 cp++;
1692 }
1693 if (*cp == 'e' || *cp == 'E')
1694 {
1695 state |= E_CHAR;
1696 cp++;
1697 if (*cp == '+' || *cp == '-')
1698 cp++;
1699 }
1700
1701 if (*cp >= '0' && *cp <= '9')
1702 {
1703 state |= EXP_INT;
1704 while (*cp >= '0' && *cp <= '9')
1705 cp++;
1706 }
1707 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
1708 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
1709 || state == (DOT_CHAR|TRAIL_INT)
1710 || state == (LEAD_INT|E_CHAR|EXP_INT)
1711 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
1712 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
1713 }
1714 #endif /* LISP_FLOAT_TYPE */
1715 \f
1716 static Lisp_Object
1717 read_vector (readcharfun)
1718 Lisp_Object readcharfun;
1719 {
1720 register int i;
1721 register int size;
1722 register Lisp_Object *ptr;
1723 register Lisp_Object tem, vector;
1724 register struct Lisp_Cons *otem;
1725 Lisp_Object len;
1726
1727 tem = read_list (1, readcharfun);
1728 len = Flength (tem);
1729 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
1730
1731
1732 size = XVECTOR (vector)->size;
1733 ptr = XVECTOR (vector)->contents;
1734 for (i = 0; i < size; i++)
1735 {
1736 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
1737 otem = XCONS (tem);
1738 tem = Fcdr (tem);
1739 free_cons (otem);
1740 }
1741 return vector;
1742 }
1743
1744 /* flag = 1 means check for ] to terminate rather than ) and .
1745 flag = -1 means check for starting with defun
1746 and make structure pure. */
1747
1748 static Lisp_Object
1749 read_list (flag, readcharfun)
1750 int flag;
1751 register Lisp_Object readcharfun;
1752 {
1753 /* -1 means check next element for defun,
1754 0 means don't check,
1755 1 means already checked and found defun. */
1756 int defunflag = flag < 0 ? -1 : 0;
1757 Lisp_Object val, tail;
1758 register Lisp_Object elt, tem;
1759 struct gcpro gcpro1, gcpro2;
1760 /* 0 is the normal case.
1761 1 means this list is a doc reference; replace it with the number 0.
1762 2 means this list is a doc reference; replace it with the doc string. */
1763 int doc_reference = 0;
1764
1765 /* Initialize this to 1 if we are reading a list. */
1766 int first_in_list = flag <= 0;
1767
1768 val = Qnil;
1769 tail = Qnil;
1770
1771 while (1)
1772 {
1773 char ch;
1774 GCPRO2 (val, tail);
1775 elt = read1 (readcharfun, &ch, first_in_list);
1776 UNGCPRO;
1777
1778 first_in_list = 0;
1779
1780 /* While building, if the list starts with #$, treat it specially. */
1781 if (EQ (elt, Vload_file_name)
1782 && !NILP (Vpurify_flag))
1783 {
1784 if (NILP (Vdoc_file_name))
1785 /* We have not yet called Snarf-documentation, so assume
1786 this file is described in the DOC-MM.NN file
1787 and Snarf-documentation will fill in the right value later.
1788 For now, replace the whole list with 0. */
1789 doc_reference = 1;
1790 else
1791 /* We have already called Snarf-documentation, so make a relative
1792 file name for this file, so it can be found properly
1793 in the installed Lisp directory.
1794 We don't use Fexpand_file_name because that would make
1795 the directory absolute now. */
1796 elt = concat2 (build_string ("../lisp/"),
1797 Ffile_name_nondirectory (elt));
1798 }
1799 else if (EQ (elt, Vload_file_name)
1800 && load_force_doc_strings)
1801 doc_reference = 2;
1802
1803 if (ch)
1804 {
1805 if (flag > 0)
1806 {
1807 if (ch == ']')
1808 return val;
1809 Fsignal (Qinvalid_read_syntax,
1810 Fcons (make_string (") or . in a vector", 18), Qnil));
1811 }
1812 if (ch == ')')
1813 return val;
1814 if (ch == '.')
1815 {
1816 GCPRO2 (val, tail);
1817 if (!NILP (tail))
1818 XCONS (tail)->cdr = read0 (readcharfun);
1819 else
1820 val = read0 (readcharfun);
1821 read1 (readcharfun, &ch, 0);
1822 UNGCPRO;
1823 if (ch == ')')
1824 {
1825 if (doc_reference == 1)
1826 return make_number (0);
1827 if (doc_reference == 2)
1828 {
1829 /* Get a doc string from the file we are loading.
1830 If it's in saved_doc_string, get it from there. */
1831 int pos = XINT (XCONS (val)->cdr);
1832 if (pos >= saved_doc_string_position
1833 && pos < (saved_doc_string_position
1834 + saved_doc_string_length))
1835 {
1836 int start = pos - saved_doc_string_position;
1837 int from, to;
1838
1839 /* Process quoting with ^A,
1840 and find the end of the string,
1841 which is marked with ^_ (037). */
1842 for (from = start, to = start;
1843 saved_doc_string[from] != 037;)
1844 {
1845 int c = saved_doc_string[from++];
1846 if (c == 1)
1847 {
1848 c = saved_doc_string[from++];
1849 if (c == 1)
1850 saved_doc_string[to++] = c;
1851 else if (c == '0')
1852 saved_doc_string[to++] = 0;
1853 else if (c == '_')
1854 saved_doc_string[to++] = 037;
1855 }
1856 else
1857 saved_doc_string[to++] = c;
1858 }
1859
1860 return make_string (saved_doc_string + start,
1861 to - start);
1862 }
1863 else
1864 return read_doc_string (val);
1865 }
1866
1867 return val;
1868 }
1869 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
1870 }
1871 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
1872 }
1873 tem = (read_pure && flag <= 0
1874 ? pure_cons (elt, Qnil)
1875 : Fcons (elt, Qnil));
1876 if (!NILP (tail))
1877 XCONS (tail)->cdr = tem;
1878 else
1879 val = tem;
1880 tail = tem;
1881 if (defunflag < 0)
1882 defunflag = EQ (elt, Qdefun);
1883 else if (defunflag > 0)
1884 read_pure = 1;
1885 }
1886 }
1887 \f
1888 Lisp_Object Vobarray;
1889 Lisp_Object initial_obarray;
1890
1891 /* oblookup stores the bucket number here, for the sake of Funintern. */
1892
1893 int oblookup_last_bucket_number;
1894
1895 static int hash_string ();
1896 Lisp_Object oblookup ();
1897
1898 /* Get an error if OBARRAY is not an obarray.
1899 If it is one, return it. */
1900
1901 Lisp_Object
1902 check_obarray (obarray)
1903 Lisp_Object obarray;
1904 {
1905 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
1906 {
1907 /* If Vobarray is now invalid, force it to be valid. */
1908 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
1909
1910 obarray = wrong_type_argument (Qvectorp, obarray);
1911 }
1912 return obarray;
1913 }
1914
1915 /* Intern the C string STR: return a symbol with that name,
1916 interned in the current obarray. */
1917
1918 Lisp_Object
1919 intern (str)
1920 char *str;
1921 {
1922 Lisp_Object tem;
1923 int len = strlen (str);
1924 Lisp_Object obarray;
1925
1926 obarray = Vobarray;
1927 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
1928 obarray = check_obarray (obarray);
1929 tem = oblookup (obarray, str, len);
1930 if (SYMBOLP (tem))
1931 return tem;
1932 return Fintern ((!NILP (Vpurify_flag)
1933 ? make_pure_string (str, len)
1934 : make_string (str, len)),
1935 obarray);
1936 }
1937
1938 /* Create an uninterned symbol with name STR. */
1939
1940 Lisp_Object
1941 make_symbol (str)
1942 char *str;
1943 {
1944 int len = strlen (str);
1945
1946 return Fmake_symbol ((!NILP (Vpurify_flag)
1947 ? make_pure_string (str, len)
1948 : make_string (str, len)));
1949 }
1950 \f
1951 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
1952 "Return the canonical symbol whose name is STRING.\n\
1953 If there is none, one is created by this function and returned.\n\
1954 A second optional argument specifies the obarray to use;\n\
1955 it defaults to the value of `obarray'.")
1956 (string, obarray)
1957 Lisp_Object string, obarray;
1958 {
1959 register Lisp_Object tem, sym, *ptr;
1960
1961 if (NILP (obarray)) obarray = Vobarray;
1962 obarray = check_obarray (obarray);
1963
1964 CHECK_STRING (string, 0);
1965
1966 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
1967 if (!INTEGERP (tem))
1968 return tem;
1969
1970 if (!NILP (Vpurify_flag))
1971 string = Fpurecopy (string);
1972 sym = Fmake_symbol (string);
1973 XSYMBOL (sym)->obarray = obarray;
1974
1975 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
1976 if (SYMBOLP (*ptr))
1977 XSYMBOL (sym)->next = XSYMBOL (*ptr);
1978 else
1979 XSYMBOL (sym)->next = 0;
1980 *ptr = sym;
1981 return sym;
1982 }
1983
1984 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
1985 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
1986 A second optional argument specifies the obarray to use;\n\
1987 it defaults to the value of `obarray'.")
1988 (string, obarray)
1989 Lisp_Object string, obarray;
1990 {
1991 register Lisp_Object tem;
1992
1993 if (NILP (obarray)) obarray = Vobarray;
1994 obarray = check_obarray (obarray);
1995
1996 CHECK_STRING (string, 0);
1997
1998 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
1999 if (!INTEGERP (tem))
2000 return tem;
2001 return Qnil;
2002 }
2003 \f
2004 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2005 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2006 The value is t if a symbol was found and deleted, nil otherwise.\n\
2007 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2008 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2009 OBARRAY defaults to the value of the variable `obarray'.")
2010 (name, obarray)
2011 Lisp_Object name, obarray;
2012 {
2013 register Lisp_Object string, tem;
2014 int hash;
2015
2016 if (NILP (obarray)) obarray = Vobarray;
2017 obarray = check_obarray (obarray);
2018
2019 if (SYMBOLP (name))
2020 XSETSTRING (string, XSYMBOL (name)->name);
2021 else
2022 {
2023 CHECK_STRING (name, 0);
2024 string = name;
2025 }
2026
2027 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
2028 if (INTEGERP (tem))
2029 return Qnil;
2030 /* If arg was a symbol, don't delete anything but that symbol itself. */
2031 if (SYMBOLP (name) && !EQ (name, tem))
2032 return Qnil;
2033
2034 hash = oblookup_last_bucket_number;
2035
2036 if (EQ (XVECTOR (obarray)->contents[hash], tem))
2037 {
2038 if (XSYMBOL (tem)->next)
2039 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
2040 else
2041 XSETINT (XVECTOR (obarray)->contents[hash], 0);
2042 }
2043 else
2044 {
2045 Lisp_Object tail, following;
2046
2047 for (tail = XVECTOR (obarray)->contents[hash];
2048 XSYMBOL (tail)->next;
2049 tail = following)
2050 {
2051 XSETSYMBOL (following, XSYMBOL (tail)->next);
2052 if (EQ (following, tem))
2053 {
2054 XSYMBOL (tail)->next = XSYMBOL (following)->next;
2055 break;
2056 }
2057 }
2058 }
2059
2060 return Qt;
2061 }
2062 \f
2063 /* Return the symbol in OBARRAY whose names matches the string
2064 of SIZE characters at PTR. If there is no such symbol in OBARRAY,
2065 return nil.
2066
2067 Also store the bucket number in oblookup_last_bucket_number. */
2068
2069 Lisp_Object
2070 oblookup (obarray, ptr, size)
2071 Lisp_Object obarray;
2072 register char *ptr;
2073 register int size;
2074 {
2075 int hash;
2076 int obsize;
2077 register Lisp_Object tail;
2078 Lisp_Object bucket, tem;
2079
2080 if (!VECTORP (obarray)
2081 || (obsize = XVECTOR (obarray)->size) == 0)
2082 {
2083 obarray = check_obarray (obarray);
2084 obsize = XVECTOR (obarray)->size;
2085 }
2086 /* This is sometimes needed in the middle of GC. */
2087 obsize &= ~ARRAY_MARK_FLAG;
2088 /* Combining next two lines breaks VMS C 2.3. */
2089 hash = hash_string (ptr, size);
2090 hash %= obsize;
2091 bucket = XVECTOR (obarray)->contents[hash];
2092 oblookup_last_bucket_number = hash;
2093 if (XFASTINT (bucket) == 0)
2094 ;
2095 else if (!SYMBOLP (bucket))
2096 error ("Bad data in guts of obarray"); /* Like CADR error message */
2097 else
2098 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
2099 {
2100 if (XSYMBOL (tail)->name->size == size
2101 && !bcmp (XSYMBOL (tail)->name->data, ptr, size))
2102 return tail;
2103 else if (XSYMBOL (tail)->next == 0)
2104 break;
2105 }
2106 XSETINT (tem, hash);
2107 return tem;
2108 }
2109
2110 static int
2111 hash_string (ptr, len)
2112 unsigned char *ptr;
2113 int len;
2114 {
2115 register unsigned char *p = ptr;
2116 register unsigned char *end = p + len;
2117 register unsigned char c;
2118 register int hash = 0;
2119
2120 while (p != end)
2121 {
2122 c = *p++;
2123 if (c >= 0140) c -= 40;
2124 hash = ((hash<<3) + (hash>>28) + c);
2125 }
2126 return hash & 07777777777;
2127 }
2128 \f
2129 void
2130 map_obarray (obarray, fn, arg)
2131 Lisp_Object obarray;
2132 int (*fn) ();
2133 Lisp_Object arg;
2134 {
2135 register int i;
2136 register Lisp_Object tail;
2137 CHECK_VECTOR (obarray, 1);
2138 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
2139 {
2140 tail = XVECTOR (obarray)->contents[i];
2141 if (XFASTINT (tail) != 0)
2142 while (1)
2143 {
2144 (*fn) (tail, arg);
2145 if (XSYMBOL (tail)->next == 0)
2146 break;
2147 XSETSYMBOL (tail, XSYMBOL (tail)->next);
2148 }
2149 }
2150 }
2151
2152 mapatoms_1 (sym, function)
2153 Lisp_Object sym, function;
2154 {
2155 call1 (function, sym);
2156 }
2157
2158 DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
2159 "Call FUNCTION on every symbol in OBARRAY.\n\
2160 OBARRAY defaults to the value of `obarray'.")
2161 (function, obarray)
2162 Lisp_Object function, obarray;
2163 {
2164 Lisp_Object tem;
2165
2166 if (NILP (obarray)) obarray = Vobarray;
2167 obarray = check_obarray (obarray);
2168
2169 map_obarray (obarray, mapatoms_1, function);
2170 return Qnil;
2171 }
2172
2173 #define OBARRAY_SIZE 1511
2174
2175 void
2176 init_obarray ()
2177 {
2178 Lisp_Object oblength;
2179 int hash;
2180 Lisp_Object *tem;
2181
2182 XSETFASTINT (oblength, OBARRAY_SIZE);
2183
2184 Qnil = Fmake_symbol (make_pure_string ("nil", 3));
2185 Vobarray = Fmake_vector (oblength, make_number (0));
2186 initial_obarray = Vobarray;
2187 staticpro (&initial_obarray);
2188 /* Intern nil in the obarray */
2189 XSYMBOL (Qnil)->obarray = Vobarray;
2190 /* These locals are to kludge around a pyramid compiler bug. */
2191 hash = hash_string ("nil", 3);
2192 /* Separate statement here to avoid VAXC bug. */
2193 hash %= OBARRAY_SIZE;
2194 tem = &XVECTOR (Vobarray)->contents[hash];
2195 *tem = Qnil;
2196
2197 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7));
2198 XSYMBOL (Qnil)->function = Qunbound;
2199 XSYMBOL (Qunbound)->value = Qunbound;
2200 XSYMBOL (Qunbound)->function = Qunbound;
2201
2202 Qt = intern ("t");
2203 XSYMBOL (Qnil)->value = Qnil;
2204 XSYMBOL (Qnil)->plist = Qnil;
2205 XSYMBOL (Qt)->value = Qt;
2206
2207 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
2208 Vpurify_flag = Qt;
2209
2210 Qvariable_documentation = intern ("variable-documentation");
2211 staticpro (&Qvariable_documentation);
2212
2213 read_buffer_size = 100;
2214 read_buffer = (char *) malloc (read_buffer_size);
2215 }
2216 \f
2217 void
2218 defsubr (sname)
2219 struct Lisp_Subr *sname;
2220 {
2221 Lisp_Object sym;
2222 sym = intern (sname->symbol_name);
2223 XSETSUBR (XSYMBOL (sym)->function, sname);
2224 }
2225
2226 #ifdef NOTDEF /* use fset in subr.el now */
2227 void
2228 defalias (sname, string)
2229 struct Lisp_Subr *sname;
2230 char *string;
2231 {
2232 Lisp_Object sym;
2233 sym = intern (string);
2234 XSETSUBR (XSYMBOL (sym)->function, sname);
2235 }
2236 #endif /* NOTDEF */
2237
2238 /* Define an "integer variable"; a symbol whose value is forwarded
2239 to a C variable of type int. Sample call: */
2240 /* DEFVAR_INT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
2241 void
2242 defvar_int (namestring, address)
2243 char *namestring;
2244 int *address;
2245 {
2246 Lisp_Object sym, val;
2247 sym = intern (namestring);
2248 val = allocate_misc ();
2249 XMISCTYPE (val) = Lisp_Misc_Intfwd;
2250 XINTFWD (val)->intvar = address;
2251 XSYMBOL (sym)->value = val;
2252 }
2253
2254 /* Similar but define a variable whose value is T if address contains 1,
2255 NIL if address contains 0 */
2256 void
2257 defvar_bool (namestring, address)
2258 char *namestring;
2259 int *address;
2260 {
2261 Lisp_Object sym, val;
2262 sym = intern (namestring);
2263 val = allocate_misc ();
2264 XMISCTYPE (val) = Lisp_Misc_Boolfwd;
2265 XBOOLFWD (val)->boolvar = address;
2266 XSYMBOL (sym)->value = val;
2267 }
2268
2269 /* Similar but define a variable whose value is the Lisp Object stored
2270 at address. Two versions: with and without gc-marking of the C
2271 variable. The nopro version is used when that variable will be
2272 gc-marked for some other reason, since marking the same slot twice
2273 can cause trouble with strings. */
2274 void
2275 defvar_lisp_nopro (namestring, address)
2276 char *namestring;
2277 Lisp_Object *address;
2278 {
2279 Lisp_Object sym, val;
2280 sym = intern (namestring);
2281 val = allocate_misc ();
2282 XMISCTYPE (val) = Lisp_Misc_Objfwd;
2283 XOBJFWD (val)->objvar = address;
2284 XSYMBOL (sym)->value = val;
2285 }
2286
2287 void
2288 defvar_lisp (namestring, address)
2289 char *namestring;
2290 Lisp_Object *address;
2291 {
2292 defvar_lisp_nopro (namestring, address);
2293 staticpro (address);
2294 }
2295
2296 #ifndef standalone
2297
2298 /* Similar but define a variable whose value is the Lisp Object stored in
2299 the current buffer. address is the address of the slot in the buffer
2300 that is current now. */
2301
2302 void
2303 defvar_per_buffer (namestring, address, type, doc)
2304 char *namestring;
2305 Lisp_Object *address;
2306 Lisp_Object type;
2307 char *doc;
2308 {
2309 Lisp_Object sym, val;
2310 int offset;
2311 extern struct buffer buffer_local_symbols;
2312
2313 sym = intern (namestring);
2314 val = allocate_misc ();
2315 offset = (char *)address - (char *)current_buffer;
2316
2317 XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
2318 XBUFFER_OBJFWD (val)->offset = offset;
2319 XSYMBOL (sym)->value = val;
2320 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
2321 *(Lisp_Object *)(offset + (char *)&buffer_local_types) = type;
2322 if (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags)) == 0)
2323 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
2324 slot of buffer_local_flags */
2325 abort ();
2326 }
2327
2328 #endif /* standalone */
2329
2330 /* Similar but define a variable whose value is the Lisp Object stored
2331 at a particular offset in the current kboard object. */
2332
2333 void
2334 defvar_kboard (namestring, offset)
2335 char *namestring;
2336 int offset;
2337 {
2338 Lisp_Object sym, val;
2339 sym = intern (namestring);
2340 val = allocate_misc ();
2341 XMISCTYPE (val) = Lisp_Misc_Kboard_Objfwd;
2342 XKBOARD_OBJFWD (val)->offset = offset;
2343 XSYMBOL (sym)->value = val;
2344 }
2345 \f
2346 /* Record the value of load-path used at the start of dumping
2347 so we can see if the site changed it later during dumping. */
2348 static Lisp_Object dump_path;
2349
2350 init_lread ()
2351 {
2352 char *normal;
2353 int turn_off_warning = 0;
2354
2355 #ifdef HAVE_SETLOCALE
2356 /* Make sure numbers are parsed as we expect. */
2357 setlocale (LC_NUMERIC, "C");
2358 #endif /* HAVE_SETLOCALE */
2359
2360 /* Compute the default load-path. */
2361 #ifdef CANNOT_DUMP
2362 normal = PATH_LOADSEARCH;
2363 Vload_path = decode_env_path (0, normal);
2364 #else
2365 if (NILP (Vpurify_flag))
2366 normal = PATH_LOADSEARCH;
2367 else
2368 normal = PATH_DUMPLOADSEARCH;
2369
2370 /* In a dumped Emacs, we normally have to reset the value of
2371 Vload_path from PATH_LOADSEARCH, since the value that was dumped
2372 uses ../lisp, instead of the path of the installed elisp
2373 libraries. However, if it appears that Vload_path was changed
2374 from the default before dumping, don't override that value. */
2375 if (initialized)
2376 {
2377 if (! NILP (Fequal (dump_path, Vload_path)))
2378 {
2379 Vload_path = decode_env_path (0, normal);
2380 if (!NILP (Vinstallation_directory))
2381 {
2382 /* Add to the path the lisp subdir of the
2383 installation dir, if it exists. */
2384 Lisp_Object tem, tem1;
2385 tem = Fexpand_file_name (build_string ("lisp"),
2386 Vinstallation_directory);
2387 tem1 = Ffile_exists_p (tem);
2388 if (!NILP (tem1))
2389 {
2390 if (NILP (Fmember (tem, Vload_path)))
2391 {
2392 turn_off_warning = 1;
2393 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2394 }
2395 }
2396 else
2397 /* That dir doesn't exist, so add the build-time
2398 Lisp dirs instead. */
2399 Vload_path = nconc2 (Vload_path, dump_path);
2400
2401 /* Add site-list under the installation dir, if it exists. */
2402 tem = Fexpand_file_name (build_string ("site-lisp"),
2403 Vinstallation_directory);
2404 tem1 = Ffile_exists_p (tem);
2405 if (!NILP (tem1))
2406 {
2407 if (NILP (Fmember (tem, Vload_path)))
2408 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2409 }
2410 }
2411 }
2412 }
2413 else
2414 {
2415 /* ../lisp refers to the build directory.
2416 NORMAL refers to the lisp dir in the source directory. */
2417 Vload_path = Fcons (build_string ("../lisp"),
2418 decode_env_path (0, normal));
2419 dump_path = Vload_path;
2420 }
2421 #endif
2422
2423 #ifndef WINDOWSNT
2424 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
2425 almost never correct, thereby causing a warning to be printed out that
2426 confuses users. Since PATH_LOADSEARCH is always overridden by the
2427 EMACSLOADPATH environment variable below, disable the warning on NT. */
2428
2429 /* Warn if dirs in the *standard* path don't exist. */
2430 if (!turn_off_warning)
2431 {
2432 Lisp_Object path_tail;
2433
2434 for (path_tail = Vload_path;
2435 !NILP (path_tail);
2436 path_tail = XCONS (path_tail)->cdr)
2437 {
2438 Lisp_Object dirfile;
2439 dirfile = Fcar (path_tail);
2440 if (STRINGP (dirfile))
2441 {
2442 dirfile = Fdirectory_file_name (dirfile);
2443 if (access (XSTRING (dirfile)->data, 0) < 0)
2444 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
2445 XCONS (path_tail)->car);
2446 }
2447 }
2448 }
2449 #endif /* WINDOWSNT */
2450
2451 /* If the EMACSLOADPATH environment variable is set, use its value.
2452 This doesn't apply if we're dumping. */
2453 #ifndef CANNOT_DUMP
2454 if (NILP (Vpurify_flag)
2455 && egetenv ("EMACSLOADPATH"))
2456 #endif
2457 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
2458
2459 Vvalues = Qnil;
2460
2461 load_in_progress = 0;
2462 Vload_file_name = Qnil;
2463
2464 load_descriptor_list = Qnil;
2465 }
2466
2467 /* Print a warning, using format string FORMAT, that directory DIRNAME
2468 does not exist. Print it on stderr and put it in *Message*. */
2469
2470 dir_warning (format, dirname)
2471 char *format;
2472 Lisp_Object dirname;
2473 {
2474 char *buffer
2475 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
2476
2477 fprintf (stderr, format, XSTRING (dirname)->data);
2478 sprintf (buffer, format, XSTRING (dirname)->data);
2479 message_dolog (buffer, strlen (buffer), 0);
2480 }
2481
2482 void
2483 syms_of_lread ()
2484 {
2485 defsubr (&Sread);
2486 defsubr (&Sread_from_string);
2487 defsubr (&Sintern);
2488 defsubr (&Sintern_soft);
2489 defsubr (&Sunintern);
2490 defsubr (&Sload);
2491 defsubr (&Seval_buffer);
2492 defsubr (&Seval_region);
2493 defsubr (&Sread_char);
2494 defsubr (&Sread_char_exclusive);
2495 defsubr (&Sread_event);
2496 defsubr (&Sget_file_char);
2497 defsubr (&Smapatoms);
2498
2499 DEFVAR_LISP ("obarray", &Vobarray,
2500 "Symbol table for use by `intern' and `read'.\n\
2501 It is a vector whose length ought to be prime for best results.\n\
2502 The vector's contents don't make sense if examined from Lisp programs;\n\
2503 to find all the symbols in an obarray, use `mapatoms'.");
2504
2505 DEFVAR_LISP ("values", &Vvalues,
2506 "List of values of all expressions which were read, evaluated and printed.\n\
2507 Order is reverse chronological.");
2508
2509 DEFVAR_LISP ("standard-input", &Vstandard_input,
2510 "Stream for read to get input from.\n\
2511 See documentation of `read' for possible values.");
2512 Vstandard_input = Qt;
2513
2514 DEFVAR_LISP ("load-path", &Vload_path,
2515 "*List of directories to search for files to load.\n\
2516 Each element is a string (directory name) or nil (try default directory).\n\
2517 Initialized based on EMACSLOADPATH environment variable, if any,\n\
2518 otherwise to default specified by file `paths.h' when Emacs was built.");
2519
2520 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
2521 "Non-nil iff inside of `load'.");
2522
2523 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
2524 "An alist of expressions to be evalled when particular files are loaded.\n\
2525 Each element looks like (FILENAME FORMS...).\n\
2526 When `load' is run and the file-name argument is FILENAME,\n\
2527 the FORMS in the corresponding element are executed at the end of loading.\n\n\
2528 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
2529 with no directory specified, since that is how `load' is normally called.\n\
2530 An error in FORMS does not undo the load,\n\
2531 but does prevent execution of the rest of the FORMS.");
2532 Vafter_load_alist = Qnil;
2533
2534 DEFVAR_LISP ("load-history", &Vload_history,
2535 "Alist mapping source file names to symbols and features.\n\
2536 Each alist element is a list that starts with a file name,\n\
2537 except for one element (optional) that starts with nil and describes\n\
2538 definitions evaluated from buffers not visiting files.\n\
2539 The remaining elements of each list are symbols defined as functions\n\
2540 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
2541 Vload_history = Qnil;
2542
2543 DEFVAR_LISP ("load-file-name", &Vload_file_name,
2544 "Full name of file being loaded by `load'.");
2545 Vload_file_name = Qnil;
2546
2547 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
2548 "Used for internal purposes by `load'.");
2549 Vcurrent_load_list = Qnil;
2550
2551 DEFVAR_LISP ("load-read-function", &Vload_read_function,
2552 "Function used by `load' and `eval-region' for reading expressions.\n\
2553 The default is nil, which means use the function `read'.");
2554 Vload_read_function = Qnil;
2555
2556 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
2557 "Non-nil means `load' should force-load all dynamic doc strings.\n\
2558 This is useful when the file being loaded is a temporary copy.");
2559 load_force_doc_strings = 0;
2560
2561 DEFVAR_LISP ("source-directory", &Vsource_directory,
2562 "Directory in which Emacs sources were found when Emacs was built.\n\
2563 You cannot count on them to still be there!");
2564 Vsource_directory
2565 = Fexpand_file_name (build_string ("../"),
2566 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
2567
2568 /* Vsource_directory was initialized in init_lread. */
2569
2570 load_descriptor_list = Qnil;
2571 staticpro (&load_descriptor_list);
2572
2573 Qcurrent_load_list = intern ("current-load-list");
2574 staticpro (&Qcurrent_load_list);
2575
2576 Qstandard_input = intern ("standard-input");
2577 staticpro (&Qstandard_input);
2578
2579 Qread_char = intern ("read-char");
2580 staticpro (&Qread_char);
2581
2582 Qget_file_char = intern ("get-file-char");
2583 staticpro (&Qget_file_char);
2584
2585 Qbackquote = intern ("`");
2586 staticpro (&Qbackquote);
2587 Qcomma = intern (",");
2588 staticpro (&Qcomma);
2589 Qcomma_at = intern (",@");
2590 staticpro (&Qcomma_at);
2591 Qcomma_dot = intern (",.");
2592 staticpro (&Qcomma_dot);
2593
2594 Qascii_character = intern ("ascii-character");
2595 staticpro (&Qascii_character);
2596
2597 Qfunction = intern ("function");
2598 staticpro (&Qfunction);
2599
2600 Qload = intern ("load");
2601 staticpro (&Qload);
2602
2603 Qload_file_name = intern ("load-file-name");
2604 staticpro (&Qload_file_name);
2605
2606 staticpro (&dump_path);
2607
2608 staticpro (&read_objects);
2609 read_objects = Qnil;
2610 }