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