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