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