]> code.delx.au - gnu-emacs/blob - src/lread.c
(Feval_region): Doc correction (point does not move).
[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 This function does not move point.")
1023 (start, end, printflag)
1024 Lisp_Object start, end, printflag;
1025 {
1026 int count = specpdl_ptr - specpdl;
1027 Lisp_Object tem, cbuf;
1028
1029 cbuf = Fcurrent_buffer ();
1030
1031 if (NILP (printflag))
1032 tem = Qsymbolp;
1033 else
1034 tem = printflag;
1035 specbind (Qstandard_output, tem);
1036
1037 if (NILP (printflag))
1038 record_unwind_protect (save_excursion_restore, save_excursion_save ());
1039 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1040
1041 /* This both uses start and checks its type. */
1042 Fgoto_char (start);
1043 Fnarrow_to_region (make_number (BEGV), end);
1044 readevalloop (cbuf, 0, XBUFFER (cbuf)->filename, Feval, !NILP (printflag));
1045
1046 return unbind_to (count, Qnil);
1047 }
1048
1049 #endif /* standalone */
1050 \f
1051 DEFUN ("read", Fread, Sread, 0, 1, 0,
1052 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
1053 If STREAM is nil, use the value of `standard-input' (which see).\n\
1054 STREAM or the value of `standard-input' may be:\n\
1055 a buffer (read from point and advance it)\n\
1056 a marker (read from where it points and advance it)\n\
1057 a function (call it with no arguments for each character,\n\
1058 call it with a char as argument to push a char back)\n\
1059 a string (takes text from string, starting at the beginning)\n\
1060 t (read text line using minibuffer and use it).")
1061 (stream)
1062 Lisp_Object stream;
1063 {
1064 extern Lisp_Object Fread_minibuffer ();
1065
1066 if (NILP (stream))
1067 stream = Vstandard_input;
1068 if (EQ (stream, Qt))
1069 stream = Qread_char;
1070
1071 new_backquote_flag = 0;
1072 read_objects = Qnil;
1073
1074 #ifndef standalone
1075 if (EQ (stream, Qread_char))
1076 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
1077 #endif
1078
1079 if (STRINGP (stream))
1080 return Fcar (Fread_from_string (stream, Qnil, Qnil));
1081
1082 return read0 (stream);
1083 }
1084
1085 DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
1086 "Read one Lisp expression which is represented as text by STRING.\n\
1087 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
1088 START and END optionally delimit a substring of STRING from which to read;\n\
1089 they default to 0 and (length STRING) respectively.")
1090 (string, start, end)
1091 Lisp_Object string, start, end;
1092 {
1093 int startval, endval;
1094 Lisp_Object tem;
1095
1096 CHECK_STRING (string,0);
1097
1098 if (NILP (end))
1099 endval = XSTRING (string)->size;
1100 else
1101 { CHECK_NUMBER (end,2);
1102 endval = XINT (end);
1103 if (endval < 0 || endval > XSTRING (string)->size)
1104 args_out_of_range (string, end);
1105 }
1106
1107 if (NILP (start))
1108 startval = 0;
1109 else
1110 { CHECK_NUMBER (start,1);
1111 startval = XINT (start);
1112 if (startval < 0 || startval > endval)
1113 args_out_of_range (string, start);
1114 }
1115
1116 read_from_string_index = startval;
1117 read_from_string_limit = endval;
1118
1119 new_backquote_flag = 0;
1120 read_objects = Qnil;
1121
1122 tem = read0 (string);
1123 return Fcons (tem, make_number (read_from_string_index));
1124 }
1125 \f
1126 /* Use this for recursive reads, in contexts where internal tokens
1127 are not allowed. */
1128 static Lisp_Object
1129 read0 (readcharfun)
1130 Lisp_Object readcharfun;
1131 {
1132 register Lisp_Object val;
1133 char c;
1134
1135 val = read1 (readcharfun, &c, 0);
1136 if (c)
1137 Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil));
1138
1139 return val;
1140 }
1141 \f
1142 static int read_buffer_size;
1143 static char *read_buffer;
1144
1145 /* Read multibyte form and return it as a character. C is a first
1146 byte of multibyte form, and rest of them are read from
1147 READCHARFUN. */
1148 static int
1149 read_multibyte (c, readcharfun)
1150 register int c;
1151 Lisp_Object readcharfun;
1152 {
1153 /* We need the actual character code of this multibyte
1154 characters. */
1155 unsigned char str[MAX_LENGTH_OF_MULTI_BYTE_FORM];
1156 int len = 0;
1157
1158 str[len++] = c;
1159 while ((c = READCHAR) >= 0xA0
1160 && len < MAX_LENGTH_OF_MULTI_BYTE_FORM)
1161 str[len++] = c;
1162 UNREAD (c);
1163 return STRING_CHAR (str, len);
1164 }
1165
1166 static int
1167 read_escape (readcharfun)
1168 Lisp_Object readcharfun;
1169 {
1170 register int c = READCHAR;
1171 switch (c)
1172 {
1173 case -1:
1174 error ("End of file");
1175
1176 case 'a':
1177 return '\007';
1178 case 'b':
1179 return '\b';
1180 case 'd':
1181 return 0177;
1182 case 'e':
1183 return 033;
1184 case 'f':
1185 return '\f';
1186 case 'n':
1187 return '\n';
1188 case 'r':
1189 return '\r';
1190 case 't':
1191 return '\t';
1192 case 'v':
1193 return '\v';
1194 case '\n':
1195 return -1;
1196
1197 case 'M':
1198 c = READCHAR;
1199 if (c != '-')
1200 error ("Invalid escape character syntax");
1201 c = READCHAR;
1202 if (c == '\\')
1203 c = read_escape (readcharfun);
1204 return c | meta_modifier;
1205
1206 case 'S':
1207 c = READCHAR;
1208 if (c != '-')
1209 error ("Invalid escape character syntax");
1210 c = READCHAR;
1211 if (c == '\\')
1212 c = read_escape (readcharfun);
1213 return c | shift_modifier;
1214
1215 case 'H':
1216 c = READCHAR;
1217 if (c != '-')
1218 error ("Invalid escape character syntax");
1219 c = READCHAR;
1220 if (c == '\\')
1221 c = read_escape (readcharfun);
1222 return c | hyper_modifier;
1223
1224 case 'A':
1225 c = READCHAR;
1226 if (c != '-')
1227 error ("Invalid escape character syntax");
1228 c = READCHAR;
1229 if (c == '\\')
1230 c = read_escape (readcharfun);
1231 return c | alt_modifier;
1232
1233 case 's':
1234 c = READCHAR;
1235 if (c != '-')
1236 error ("Invalid escape character syntax");
1237 c = READCHAR;
1238 if (c == '\\')
1239 c = read_escape (readcharfun);
1240 return c | super_modifier;
1241
1242 case 'C':
1243 c = READCHAR;
1244 if (c != '-')
1245 error ("Invalid escape character syntax");
1246 case '^':
1247 c = READCHAR;
1248 if (c == '\\')
1249 c = read_escape (readcharfun);
1250 if ((c & 0177) == '?')
1251 return 0177 | c;
1252 /* ASCII control chars are made from letters (both cases),
1253 as well as the non-letters within 0100...0137. */
1254 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
1255 return (c & (037 | ~0177));
1256 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
1257 return (c & (037 | ~0177));
1258 else
1259 return c | ctrl_modifier;
1260
1261 case '0':
1262 case '1':
1263 case '2':
1264 case '3':
1265 case '4':
1266 case '5':
1267 case '6':
1268 case '7':
1269 /* An octal escape, as in ANSI C. */
1270 {
1271 register int i = c - '0';
1272 register int count = 0;
1273 while (++count < 3)
1274 {
1275 if ((c = READCHAR) >= '0' && c <= '7')
1276 {
1277 i *= 8;
1278 i += c - '0';
1279 }
1280 else
1281 {
1282 UNREAD (c);
1283 break;
1284 }
1285 }
1286 return i;
1287 }
1288
1289 case 'x':
1290 /* A hex escape, as in ANSI C. */
1291 {
1292 int i = 0;
1293 while (1)
1294 {
1295 c = READCHAR;
1296 if (c >= '0' && c <= '9')
1297 {
1298 i *= 16;
1299 i += c - '0';
1300 }
1301 else if ((c >= 'a' && c <= 'f')
1302 || (c >= 'A' && c <= 'F'))
1303 {
1304 i *= 16;
1305 if (c >= 'a' && c <= 'f')
1306 i += c - 'a' + 10;
1307 else
1308 i += c - 'A' + 10;
1309 }
1310 else
1311 {
1312 UNREAD (c);
1313 break;
1314 }
1315 }
1316 return i;
1317 }
1318
1319 default:
1320 if (BASE_LEADING_CODE_P (c))
1321 c = read_multibyte (c, readcharfun);
1322 return c;
1323 }
1324 }
1325
1326 /* If the next token is ')' or ']' or '.', we store that character
1327 in *PCH and the return value is not interesting. Else, we store
1328 zero in *PCH and we read and return one lisp object.
1329
1330 FIRST_IN_LIST is nonzero if this is the first element of a list. */
1331
1332 static Lisp_Object
1333 read1 (readcharfun, pch, first_in_list)
1334 register Lisp_Object readcharfun;
1335 char *pch;
1336 int first_in_list;
1337 {
1338 register int c;
1339 int uninterned_symbol = 0;
1340
1341 *pch = 0;
1342
1343 retry:
1344
1345 c = READCHAR;
1346 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1347
1348 switch (c)
1349 {
1350 case '(':
1351 return read_list (0, readcharfun);
1352
1353 case '[':
1354 return read_vector (readcharfun);
1355
1356 case ')':
1357 case ']':
1358 {
1359 *pch = c;
1360 return Qnil;
1361 }
1362
1363 case '#':
1364 c = READCHAR;
1365 if (c == '^')
1366 {
1367 c = READCHAR;
1368 if (c == '[')
1369 {
1370 Lisp_Object tmp;
1371 tmp = read_vector (readcharfun);
1372 if (XVECTOR (tmp)->size < CHAR_TABLE_STANDARD_SLOTS
1373 || XVECTOR (tmp)->size > CHAR_TABLE_STANDARD_SLOTS + 10)
1374 error ("Invalid size char-table");
1375 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1376 XCHAR_TABLE (tmp)->top = Qt;
1377 return tmp;
1378 }
1379 else if (c == '^')
1380 {
1381 c = READCHAR;
1382 if (c == '[')
1383 {
1384 Lisp_Object tmp;
1385 tmp = read_vector (readcharfun);
1386 if (XVECTOR (tmp)->size != SUB_CHAR_TABLE_STANDARD_SLOTS)
1387 error ("Invalid size char-table");
1388 XSETCHAR_TABLE (tmp, XCHAR_TABLE (tmp));
1389 XCHAR_TABLE (tmp)->top = Qnil;
1390 return tmp;
1391 }
1392 Fsignal (Qinvalid_read_syntax,
1393 Fcons (make_string ("#^^", 3), Qnil));
1394 }
1395 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#^", 2), Qnil));
1396 }
1397 if (c == '&')
1398 {
1399 Lisp_Object length;
1400 length = read1 (readcharfun, pch, first_in_list);
1401 c = READCHAR;
1402 if (c == '"')
1403 {
1404 Lisp_Object tmp, val;
1405 int size_in_chars = ((XFASTINT (length) + BITS_PER_CHAR - 1)
1406 / BITS_PER_CHAR);
1407
1408 UNREAD (c);
1409 tmp = read1 (readcharfun, pch, first_in_list);
1410 if (size_in_chars != XSTRING (tmp)->size
1411 /* We used to print 1 char too many
1412 when the number of bits was a multiple of 8.
1413 Accept such input in case it came from an old version. */
1414 && ! (XFASTINT (length)
1415 == (XSTRING (tmp)->size - 1) * BITS_PER_CHAR))
1416 Fsignal (Qinvalid_read_syntax,
1417 Fcons (make_string ("#&...", 5), Qnil));
1418
1419 val = Fmake_bool_vector (length, Qnil);
1420 bcopy (XSTRING (tmp)->data, XBOOL_VECTOR (val)->data,
1421 size_in_chars);
1422 return val;
1423 }
1424 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#&...", 5),
1425 Qnil));
1426 }
1427 if (c == '[')
1428 {
1429 /* Accept compiled functions at read-time so that we don't have to
1430 build them using function calls. */
1431 Lisp_Object tmp;
1432 tmp = read_vector (readcharfun);
1433 return Fmake_byte_code (XVECTOR (tmp)->size,
1434 XVECTOR (tmp)->contents);
1435 }
1436 #ifdef USE_TEXT_PROPERTIES
1437 if (c == '(')
1438 {
1439 Lisp_Object tmp;
1440 struct gcpro gcpro1;
1441 char ch;
1442
1443 /* Read the string itself. */
1444 tmp = read1 (readcharfun, &ch, 0);
1445 if (ch != 0 || !STRINGP (tmp))
1446 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1447 GCPRO1 (tmp);
1448 /* Read the intervals and their properties. */
1449 while (1)
1450 {
1451 Lisp_Object beg, end, plist;
1452
1453 beg = read1 (readcharfun, &ch, 0);
1454 if (ch == ')')
1455 break;
1456 if (ch == 0)
1457 end = read1 (readcharfun, &ch, 0);
1458 if (ch == 0)
1459 plist = read1 (readcharfun, &ch, 0);
1460 if (ch)
1461 Fsignal (Qinvalid_read_syntax,
1462 Fcons (build_string ("invalid string property list"),
1463 Qnil));
1464 Fset_text_properties (beg, end, plist, tmp);
1465 }
1466 UNGCPRO;
1467 return tmp;
1468 }
1469 #endif
1470 /* #@NUMBER is used to skip NUMBER following characters.
1471 That's used in .elc files to skip over doc strings
1472 and function definitions. */
1473 if (c == '@')
1474 {
1475 int i, nskip = 0;
1476
1477 /* Read a decimal integer. */
1478 while ((c = READCHAR) >= 0
1479 && c >= '0' && c <= '9')
1480 {
1481 nskip *= 10;
1482 nskip += c - '0';
1483 }
1484 if (c >= 0)
1485 UNREAD (c);
1486
1487 #ifndef DOS_NT /* I don't know if filepos works right on MSDOS and Windoze. */
1488 if (load_force_doc_strings && EQ (readcharfun, Qget_file_char))
1489 {
1490 /* If we are supposed to force doc strings into core right now,
1491 record the last string that we skipped,
1492 and record where in the file it comes from. */
1493 if (saved_doc_string_size == 0)
1494 {
1495 saved_doc_string_size = nskip + 100;
1496 saved_doc_string = (char *) xmalloc (saved_doc_string_size);
1497 }
1498 if (nskip > saved_doc_string_size)
1499 {
1500 saved_doc_string_size = nskip + 100;
1501 saved_doc_string = (char *) xrealloc (saved_doc_string,
1502 saved_doc_string_size);
1503 }
1504
1505 saved_doc_string_position = ftell (instream);
1506
1507 /* Copy that many characters into saved_doc_string. */
1508 for (i = 0; i < nskip && c >= 0; i++)
1509 saved_doc_string[i] = c = READCHAR;
1510
1511 saved_doc_string_length = i;
1512 }
1513 else
1514 #endif /* not DOS_NT */
1515 {
1516 /* Skip that many characters. */
1517 for (i = 0; i < nskip && c >= 0; i++)
1518 c = READCHAR;
1519 }
1520
1521 goto retry;
1522 }
1523 if (c == '$')
1524 return Vload_file_name;
1525 if (c == '\'')
1526 return Fcons (Qfunction, Fcons (read0 (readcharfun), Qnil));
1527 /* #:foo is the uninterned symbol named foo. */
1528 if (c == ':')
1529 {
1530 uninterned_symbol = 1;
1531 c = READCHAR;
1532 goto default_label;
1533 }
1534 /* Reader forms that can reuse previously read objects. */
1535 if (c >= '0' && c <= '9')
1536 {
1537 int n = 0;
1538 Lisp_Object tem;
1539
1540 /* Read a non-negative integer. */
1541 while (c >= '0' && c <= '9')
1542 {
1543 n *= 10;
1544 n += c - '0';
1545 c = READCHAR;
1546 }
1547 /* #n=object returns object, but associates it with n for #n#. */
1548 if (c == '=')
1549 {
1550 tem = read0 (readcharfun);
1551 read_objects = Fcons (Fcons (make_number (n), tem), read_objects);
1552 return tem;
1553 }
1554 /* #n# returns a previously read object. */
1555 if (c == '#')
1556 {
1557 tem = Fassq (make_number (n), read_objects);
1558 if (CONSP (tem))
1559 return XCDR (tem);
1560 /* Fall through to error message. */
1561 }
1562 /* Fall through to error message. */
1563 }
1564
1565 UNREAD (c);
1566 Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
1567
1568 case ';':
1569 while ((c = READCHAR) >= 0 && c != '\n');
1570 goto retry;
1571
1572 case '\'':
1573 {
1574 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
1575 }
1576
1577 case '`':
1578 if (first_in_list)
1579 goto default_label;
1580 else
1581 {
1582 Lisp_Object value;
1583
1584 new_backquote_flag = 1;
1585 value = read0 (readcharfun);
1586 new_backquote_flag = 0;
1587
1588 return Fcons (Qbackquote, Fcons (value, Qnil));
1589 }
1590
1591 case ',':
1592 if (new_backquote_flag)
1593 {
1594 Lisp_Object comma_type = Qnil;
1595 Lisp_Object value;
1596 int ch = READCHAR;
1597
1598 if (ch == '@')
1599 comma_type = Qcomma_at;
1600 else if (ch == '.')
1601 comma_type = Qcomma_dot;
1602 else
1603 {
1604 if (ch >= 0) UNREAD (ch);
1605 comma_type = Qcomma;
1606 }
1607
1608 new_backquote_flag = 0;
1609 value = read0 (readcharfun);
1610 new_backquote_flag = 1;
1611 return Fcons (comma_type, Fcons (value, Qnil));
1612 }
1613 else
1614 goto default_label;
1615
1616 case '?':
1617 {
1618 register Lisp_Object val;
1619
1620 c = READCHAR;
1621 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1622
1623 if (c == '\\')
1624 c = read_escape (readcharfun);
1625 else if (BASE_LEADING_CODE_P (c))
1626 c = read_multibyte (c, readcharfun);
1627 XSETINT (val, c);
1628
1629 return val;
1630 }
1631
1632 case '\"':
1633 {
1634 register char *p = read_buffer;
1635 register char *end = read_buffer + read_buffer_size;
1636 register int c;
1637 int cancel = 0;
1638
1639 while ((c = READCHAR) >= 0
1640 && c != '\"')
1641 {
1642 if (p == end)
1643 {
1644 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1645 p += new - read_buffer;
1646 read_buffer += new - read_buffer;
1647 end = read_buffer + read_buffer_size;
1648 }
1649 if (c == '\\')
1650 {
1651 c = read_escape (readcharfun);
1652 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_META)))
1653 {
1654 unsigned char workbuf[4];
1655 unsigned char *str = workbuf;
1656 int length;
1657
1658 length = non_ascii_char_to_string (c, workbuf, &str);
1659
1660 if (p + length > end)
1661 {
1662 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1663 p += new - read_buffer;
1664 read_buffer += new - read_buffer;
1665 end = read_buffer + read_buffer_size;
1666 }
1667
1668 bcopy (str, p, length);
1669 p += length;
1670 continue;
1671 }
1672 }
1673 /* c is -1 if \ newline has just been seen */
1674 if (c == -1)
1675 {
1676 if (p == read_buffer)
1677 cancel = 1;
1678 }
1679 else
1680 {
1681 /* Allow `\C- ' and `\C-?'. */
1682 if (c == (CHAR_CTL | ' '))
1683 c = 0;
1684 else if (c == (CHAR_CTL | '?'))
1685 c = 127;
1686
1687 if (c & CHAR_META)
1688 /* Move the meta bit to the right place for a string. */
1689 c = (c & ~CHAR_META) | 0x80;
1690 if (c & ~0xff)
1691 error ("Invalid modifier in string");
1692 *p++ = c;
1693 }
1694 }
1695 if (c < 0) return Fsignal (Qend_of_file, Qnil);
1696
1697 /* If purifying, and string starts with \ newline,
1698 return zero instead. This is for doc strings
1699 that we are really going to find in etc/DOC.nn.nn */
1700 if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel)
1701 return make_number (0);
1702
1703 if (read_pure)
1704 return make_pure_string (read_buffer, p - read_buffer);
1705 else
1706 return make_string (read_buffer, p - read_buffer);
1707 }
1708
1709 case '.':
1710 {
1711 #ifdef LISP_FLOAT_TYPE
1712 /* If a period is followed by a number, then we should read it
1713 as a floating point number. Otherwise, it denotes a dotted
1714 pair. */
1715 int next_char = READCHAR;
1716 UNREAD (next_char);
1717
1718 if (! (next_char >= '0' && next_char <= '9'))
1719 #endif
1720 {
1721 *pch = c;
1722 return Qnil;
1723 }
1724
1725 /* Otherwise, we fall through! Note that the atom-reading loop
1726 below will now loop at least once, assuring that we will not
1727 try to UNREAD two characters in a row. */
1728 }
1729 default:
1730 default_label:
1731 if (c <= 040) goto retry;
1732 {
1733 register char *p = read_buffer;
1734 int quoted = 0;
1735
1736 {
1737 register char *end = read_buffer + read_buffer_size;
1738
1739 while (c > 040 &&
1740 !(c == '\"' || c == '\'' || c == ';' || c == '?'
1741 || c == '(' || c == ')'
1742 #ifndef LISP_FLOAT_TYPE
1743 /* If we have floating-point support, then we need
1744 to allow <digits><dot><digits>. */
1745 || c =='.'
1746 #endif /* not LISP_FLOAT_TYPE */
1747 || c == '[' || c == ']' || c == '#'
1748 ))
1749 {
1750 if (p == end)
1751 {
1752 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1753 p += new - read_buffer;
1754 read_buffer += new - read_buffer;
1755 end = read_buffer + read_buffer_size;
1756 }
1757 if (c == '\\')
1758 {
1759 c = READCHAR;
1760 quoted = 1;
1761 }
1762 *p++ = c;
1763 c = READCHAR;
1764 }
1765
1766 if (p == end)
1767 {
1768 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
1769 p += new - read_buffer;
1770 read_buffer += new - read_buffer;
1771 /* end = read_buffer + read_buffer_size; */
1772 }
1773 *p = 0;
1774 if (c >= 0)
1775 UNREAD (c);
1776 }
1777
1778 if (!quoted && !uninterned_symbol)
1779 {
1780 register char *p1;
1781 register Lisp_Object val;
1782 p1 = read_buffer;
1783 if (*p1 == '+' || *p1 == '-') p1++;
1784 /* Is it an integer? */
1785 if (p1 != p)
1786 {
1787 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
1788 #ifdef LISP_FLOAT_TYPE
1789 /* Integers can have trailing decimal points. */
1790 if (p1 > read_buffer && p1 < p && *p1 == '.') p1++;
1791 #endif
1792 if (p1 == p)
1793 /* It is an integer. */
1794 {
1795 #ifdef LISP_FLOAT_TYPE
1796 if (p1[-1] == '.')
1797 p1[-1] = '\0';
1798 #endif
1799 if (sizeof (int) == sizeof (EMACS_INT))
1800 XSETINT (val, atoi (read_buffer));
1801 else if (sizeof (long) == sizeof (EMACS_INT))
1802 XSETINT (val, atol (read_buffer));
1803 else
1804 abort ();
1805 return val;
1806 }
1807 }
1808 #ifdef LISP_FLOAT_TYPE
1809 if (isfloat_string (read_buffer))
1810 return make_float (atof (read_buffer));
1811 #endif
1812 }
1813
1814 if (uninterned_symbol)
1815 return make_symbol (read_buffer);
1816 else
1817 return intern (read_buffer);
1818 }
1819 }
1820 }
1821 \f
1822 #ifdef LISP_FLOAT_TYPE
1823
1824 #define LEAD_INT 1
1825 #define DOT_CHAR 2
1826 #define TRAIL_INT 4
1827 #define E_CHAR 8
1828 #define EXP_INT 16
1829
1830 int
1831 isfloat_string (cp)
1832 register char *cp;
1833 {
1834 register state;
1835
1836 state = 0;
1837 if (*cp == '+' || *cp == '-')
1838 cp++;
1839
1840 if (*cp >= '0' && *cp <= '9')
1841 {
1842 state |= LEAD_INT;
1843 while (*cp >= '0' && *cp <= '9')
1844 cp++;
1845 }
1846 if (*cp == '.')
1847 {
1848 state |= DOT_CHAR;
1849 cp++;
1850 }
1851 if (*cp >= '0' && *cp <= '9')
1852 {
1853 state |= TRAIL_INT;
1854 while (*cp >= '0' && *cp <= '9')
1855 cp++;
1856 }
1857 if (*cp == 'e' || *cp == 'E')
1858 {
1859 state |= E_CHAR;
1860 cp++;
1861 if (*cp == '+' || *cp == '-')
1862 cp++;
1863 }
1864
1865 if (*cp >= '0' && *cp <= '9')
1866 {
1867 state |= EXP_INT;
1868 while (*cp >= '0' && *cp <= '9')
1869 cp++;
1870 }
1871 return (((*cp == 0) || (*cp == ' ') || (*cp == '\t') || (*cp == '\n') || (*cp == '\r') || (*cp == '\f'))
1872 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
1873 || state == (DOT_CHAR|TRAIL_INT)
1874 || state == (LEAD_INT|E_CHAR|EXP_INT)
1875 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)
1876 || state == (DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
1877 }
1878 #endif /* LISP_FLOAT_TYPE */
1879 \f
1880 static Lisp_Object
1881 read_vector (readcharfun)
1882 Lisp_Object readcharfun;
1883 {
1884 register int i;
1885 register int size;
1886 register Lisp_Object *ptr;
1887 register Lisp_Object tem, vector;
1888 register struct Lisp_Cons *otem;
1889 Lisp_Object len;
1890
1891 tem = read_list (1, readcharfun);
1892 len = Flength (tem);
1893 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
1894
1895
1896 size = XVECTOR (vector)->size;
1897 ptr = XVECTOR (vector)->contents;
1898 for (i = 0; i < size; i++)
1899 {
1900 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
1901 otem = XCONS (tem);
1902 tem = Fcdr (tem);
1903 free_cons (otem);
1904 }
1905 return vector;
1906 }
1907
1908 /* flag = 1 means check for ] to terminate rather than ) and .
1909 flag = -1 means check for starting with defun
1910 and make structure pure. */
1911
1912 static Lisp_Object
1913 read_list (flag, readcharfun)
1914 int flag;
1915 register Lisp_Object readcharfun;
1916 {
1917 /* -1 means check next element for defun,
1918 0 means don't check,
1919 1 means already checked and found defun. */
1920 int defunflag = flag < 0 ? -1 : 0;
1921 Lisp_Object val, tail;
1922 register Lisp_Object elt, tem;
1923 struct gcpro gcpro1, gcpro2;
1924 /* 0 is the normal case.
1925 1 means this list is a doc reference; replace it with the number 0.
1926 2 means this list is a doc reference; replace it with the doc string. */
1927 int doc_reference = 0;
1928
1929 /* Initialize this to 1 if we are reading a list. */
1930 int first_in_list = flag <= 0;
1931
1932 val = Qnil;
1933 tail = Qnil;
1934
1935 while (1)
1936 {
1937 char ch;
1938 GCPRO2 (val, tail);
1939 elt = read1 (readcharfun, &ch, first_in_list);
1940 UNGCPRO;
1941
1942 first_in_list = 0;
1943
1944 /* While building, if the list starts with #$, treat it specially. */
1945 if (EQ (elt, Vload_file_name)
1946 && ! NILP (elt)
1947 && !NILP (Vpurify_flag))
1948 {
1949 if (NILP (Vdoc_file_name))
1950 /* We have not yet called Snarf-documentation, so assume
1951 this file is described in the DOC-MM.NN file
1952 and Snarf-documentation will fill in the right value later.
1953 For now, replace the whole list with 0. */
1954 doc_reference = 1;
1955 else
1956 /* We have already called Snarf-documentation, so make a relative
1957 file name for this file, so it can be found properly
1958 in the installed Lisp directory.
1959 We don't use Fexpand_file_name because that would make
1960 the directory absolute now. */
1961 elt = concat2 (build_string ("../lisp/"),
1962 Ffile_name_nondirectory (elt));
1963 }
1964 else if (EQ (elt, Vload_file_name)
1965 && ! NILP (elt)
1966 && load_force_doc_strings)
1967 doc_reference = 2;
1968
1969 if (ch)
1970 {
1971 if (flag > 0)
1972 {
1973 if (ch == ']')
1974 return val;
1975 Fsignal (Qinvalid_read_syntax,
1976 Fcons (make_string (") or . in a vector", 18), Qnil));
1977 }
1978 if (ch == ')')
1979 return val;
1980 if (ch == '.')
1981 {
1982 GCPRO2 (val, tail);
1983 if (!NILP (tail))
1984 XCONS (tail)->cdr = read0 (readcharfun);
1985 else
1986 val = read0 (readcharfun);
1987 read1 (readcharfun, &ch, 0);
1988 UNGCPRO;
1989 if (ch == ')')
1990 {
1991 if (doc_reference == 1)
1992 return make_number (0);
1993 if (doc_reference == 2)
1994 {
1995 /* Get a doc string from the file we are loading.
1996 If it's in saved_doc_string, get it from there. */
1997 int pos = XINT (XCONS (val)->cdr);
1998 if (pos >= saved_doc_string_position
1999 && pos < (saved_doc_string_position
2000 + saved_doc_string_length))
2001 {
2002 int start = pos - saved_doc_string_position;
2003 int from, to;
2004
2005 /* Process quoting with ^A,
2006 and find the end of the string,
2007 which is marked with ^_ (037). */
2008 for (from = start, to = start;
2009 saved_doc_string[from] != 037;)
2010 {
2011 int c = saved_doc_string[from++];
2012 if (c == 1)
2013 {
2014 c = saved_doc_string[from++];
2015 if (c == 1)
2016 saved_doc_string[to++] = c;
2017 else if (c == '0')
2018 saved_doc_string[to++] = 0;
2019 else if (c == '_')
2020 saved_doc_string[to++] = 037;
2021 }
2022 else
2023 saved_doc_string[to++] = c;
2024 }
2025
2026 return make_string (saved_doc_string + start,
2027 to - start);
2028 }
2029 else
2030 return read_doc_string (val);
2031 }
2032
2033 return val;
2034 }
2035 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
2036 }
2037 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
2038 }
2039 tem = (read_pure && flag <= 0
2040 ? pure_cons (elt, Qnil)
2041 : Fcons (elt, Qnil));
2042 if (!NILP (tail))
2043 XCONS (tail)->cdr = tem;
2044 else
2045 val = tem;
2046 tail = tem;
2047 if (defunflag < 0)
2048 defunflag = EQ (elt, Qdefun);
2049 else if (defunflag > 0)
2050 read_pure = 1;
2051 }
2052 }
2053 \f
2054 Lisp_Object Vobarray;
2055 Lisp_Object initial_obarray;
2056
2057 /* oblookup stores the bucket number here, for the sake of Funintern. */
2058
2059 int oblookup_last_bucket_number;
2060
2061 static int hash_string ();
2062 Lisp_Object oblookup ();
2063
2064 /* Get an error if OBARRAY is not an obarray.
2065 If it is one, return it. */
2066
2067 Lisp_Object
2068 check_obarray (obarray)
2069 Lisp_Object obarray;
2070 {
2071 while (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2072 {
2073 /* If Vobarray is now invalid, force it to be valid. */
2074 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
2075
2076 obarray = wrong_type_argument (Qvectorp, obarray);
2077 }
2078 return obarray;
2079 }
2080
2081 /* Intern the C string STR: return a symbol with that name,
2082 interned in the current obarray. */
2083
2084 Lisp_Object
2085 intern (str)
2086 char *str;
2087 {
2088 Lisp_Object tem;
2089 int len = strlen (str);
2090 Lisp_Object obarray;
2091
2092 obarray = Vobarray;
2093 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
2094 obarray = check_obarray (obarray);
2095 tem = oblookup (obarray, str, len);
2096 if (SYMBOLP (tem))
2097 return tem;
2098 return Fintern (make_string (str, len), obarray);
2099 }
2100
2101 /* Create an uninterned symbol with name STR. */
2102
2103 Lisp_Object
2104 make_symbol (str)
2105 char *str;
2106 {
2107 int len = strlen (str);
2108
2109 return Fmake_symbol ((!NILP (Vpurify_flag)
2110 ? make_pure_string (str, len)
2111 : make_string (str, len)));
2112 }
2113 \f
2114 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
2115 "Return the canonical symbol whose name is STRING.\n\
2116 If there is none, one is created by this function and returned.\n\
2117 A second optional argument specifies the obarray to use;\n\
2118 it defaults to the value of `obarray'.")
2119 (string, obarray)
2120 Lisp_Object string, obarray;
2121 {
2122 register Lisp_Object tem, sym, *ptr;
2123
2124 if (NILP (obarray)) obarray = Vobarray;
2125 obarray = check_obarray (obarray);
2126
2127 CHECK_STRING (string, 0);
2128
2129 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
2130 if (!INTEGERP (tem))
2131 return tem;
2132
2133 if (!NILP (Vpurify_flag))
2134 string = Fpurecopy (string);
2135 sym = Fmake_symbol (string);
2136 XSYMBOL (sym)->obarray = obarray;
2137
2138 if (XSTRING (string)->data[0] == ':')
2139 XSYMBOL (sym)->value = sym;
2140
2141 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
2142 if (SYMBOLP (*ptr))
2143 XSYMBOL (sym)->next = XSYMBOL (*ptr);
2144 else
2145 XSYMBOL (sym)->next = 0;
2146 *ptr = sym;
2147 return sym;
2148 }
2149
2150 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
2151 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
2152 A second optional argument specifies the obarray to use;\n\
2153 it defaults to the value of `obarray'.")
2154 (string, obarray)
2155 Lisp_Object string, obarray;
2156 {
2157 register Lisp_Object tem;
2158
2159 if (NILP (obarray)) obarray = Vobarray;
2160 obarray = check_obarray (obarray);
2161
2162 CHECK_STRING (string, 0);
2163
2164 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
2165 if (!INTEGERP (tem))
2166 return tem;
2167 return Qnil;
2168 }
2169 \f
2170 DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
2171 "Delete the symbol named NAME, if any, from OBARRAY.\n\
2172 The value is t if a symbol was found and deleted, nil otherwise.\n\
2173 NAME may be a string or a symbol. If it is a symbol, that symbol\n\
2174 is deleted, if it belongs to OBARRAY--no other symbol is deleted.\n\
2175 OBARRAY defaults to the value of the variable `obarray'.")
2176 (name, obarray)
2177 Lisp_Object name, obarray;
2178 {
2179 register Lisp_Object string, tem;
2180 int hash;
2181
2182 if (NILP (obarray)) obarray = Vobarray;
2183 obarray = check_obarray (obarray);
2184
2185 if (SYMBOLP (name))
2186 XSETSTRING (string, XSYMBOL (name)->name);
2187 else
2188 {
2189 CHECK_STRING (name, 0);
2190 string = name;
2191 }
2192
2193 tem = oblookup (obarray, XSTRING (string)->data, XSTRING (string)->size);
2194 if (INTEGERP (tem))
2195 return Qnil;
2196 /* If arg was a symbol, don't delete anything but that symbol itself. */
2197 if (SYMBOLP (name) && !EQ (name, tem))
2198 return Qnil;
2199
2200 hash = oblookup_last_bucket_number;
2201
2202 if (EQ (XVECTOR (obarray)->contents[hash], tem))
2203 {
2204 if (XSYMBOL (tem)->next)
2205 XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
2206 else
2207 XSETINT (XVECTOR (obarray)->contents[hash], 0);
2208 }
2209 else
2210 {
2211 Lisp_Object tail, following;
2212
2213 for (tail = XVECTOR (obarray)->contents[hash];
2214 XSYMBOL (tail)->next;
2215 tail = following)
2216 {
2217 XSETSYMBOL (following, XSYMBOL (tail)->next);
2218 if (EQ (following, tem))
2219 {
2220 XSYMBOL (tail)->next = XSYMBOL (following)->next;
2221 break;
2222 }
2223 }
2224 }
2225
2226 return Qt;
2227 }
2228 \f
2229 /* Return the symbol in OBARRAY whose names matches the string
2230 of SIZE characters at PTR. If there is no such symbol in OBARRAY,
2231 return nil.
2232
2233 Also store the bucket number in oblookup_last_bucket_number. */
2234
2235 Lisp_Object
2236 oblookup (obarray, ptr, size)
2237 Lisp_Object obarray;
2238 register char *ptr;
2239 register int size;
2240 {
2241 int hash;
2242 int obsize;
2243 register Lisp_Object tail;
2244 Lisp_Object bucket, tem;
2245
2246 if (!VECTORP (obarray)
2247 || (obsize = XVECTOR (obarray)->size) == 0)
2248 {
2249 obarray = check_obarray (obarray);
2250 obsize = XVECTOR (obarray)->size;
2251 }
2252 /* This is sometimes needed in the middle of GC. */
2253 obsize &= ~ARRAY_MARK_FLAG;
2254 /* Combining next two lines breaks VMS C 2.3. */
2255 hash = hash_string (ptr, size);
2256 hash %= obsize;
2257 bucket = XVECTOR (obarray)->contents[hash];
2258 oblookup_last_bucket_number = hash;
2259 if (XFASTINT (bucket) == 0)
2260 ;
2261 else if (!SYMBOLP (bucket))
2262 error ("Bad data in guts of obarray"); /* Like CADR error message */
2263 else
2264 for (tail = bucket; ; XSETSYMBOL (tail, XSYMBOL (tail)->next))
2265 {
2266 if (XSYMBOL (tail)->name->size == size
2267 && !bcmp (XSYMBOL (tail)->name->data, ptr, size))
2268 return tail;
2269 else if (XSYMBOL (tail)->next == 0)
2270 break;
2271 }
2272 XSETINT (tem, hash);
2273 return tem;
2274 }
2275
2276 static int
2277 hash_string (ptr, len)
2278 unsigned char *ptr;
2279 int len;
2280 {
2281 register unsigned char *p = ptr;
2282 register unsigned char *end = p + len;
2283 register unsigned char c;
2284 register int hash = 0;
2285
2286 while (p != end)
2287 {
2288 c = *p++;
2289 if (c >= 0140) c -= 40;
2290 hash = ((hash<<3) + (hash>>28) + c);
2291 }
2292 return hash & 07777777777;
2293 }
2294 \f
2295 void
2296 map_obarray (obarray, fn, arg)
2297 Lisp_Object obarray;
2298 void (*fn) P_ ((Lisp_Object, Lisp_Object));
2299 Lisp_Object arg;
2300 {
2301 register int i;
2302 register Lisp_Object tail;
2303 CHECK_VECTOR (obarray, 1);
2304 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
2305 {
2306 tail = XVECTOR (obarray)->contents[i];
2307 if (SYMBOLP (tail))
2308 while (1)
2309 {
2310 (*fn) (tail, arg);
2311 if (XSYMBOL (tail)->next == 0)
2312 break;
2313 XSETSYMBOL (tail, XSYMBOL (tail)->next);
2314 }
2315 }
2316 }
2317
2318 void
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 void
2518 init_lread ()
2519 {
2520 char *normal;
2521 int turn_off_warning = 0;
2522
2523 #ifdef HAVE_SETLOCALE
2524 /* Make sure numbers are parsed as we expect. */
2525 setlocale (LC_NUMERIC, "C");
2526 #endif /* HAVE_SETLOCALE */
2527
2528 /* Compute the default load-path. */
2529 #ifdef CANNOT_DUMP
2530 normal = PATH_LOADSEARCH;
2531 Vload_path = decode_env_path (0, normal);
2532 #else
2533 if (NILP (Vpurify_flag))
2534 normal = PATH_LOADSEARCH;
2535 else
2536 normal = PATH_DUMPLOADSEARCH;
2537
2538 /* In a dumped Emacs, we normally have to reset the value of
2539 Vload_path from PATH_LOADSEARCH, since the value that was dumped
2540 uses ../lisp, instead of the path of the installed elisp
2541 libraries. However, if it appears that Vload_path was changed
2542 from the default before dumping, don't override that value. */
2543 if (initialized)
2544 {
2545 if (! NILP (Fequal (dump_path, Vload_path)))
2546 {
2547 Vload_path = decode_env_path (0, normal);
2548 if (!NILP (Vinstallation_directory))
2549 {
2550 /* Add to the path the lisp subdir of the
2551 installation dir, if it exists. */
2552 Lisp_Object tem, tem1;
2553 tem = Fexpand_file_name (build_string ("lisp"),
2554 Vinstallation_directory);
2555 tem1 = Ffile_exists_p (tem);
2556 if (!NILP (tem1))
2557 {
2558 if (NILP (Fmember (tem, Vload_path)))
2559 {
2560 turn_off_warning = 1;
2561 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2562 }
2563 }
2564 else
2565 /* That dir doesn't exist, so add the build-time
2566 Lisp dirs instead. */
2567 Vload_path = nconc2 (Vload_path, dump_path);
2568
2569 /* Add leim under the installation dir, if it exists. */
2570 tem = Fexpand_file_name (build_string ("leim"),
2571 Vinstallation_directory);
2572 tem1 = Ffile_exists_p (tem);
2573 if (!NILP (tem1))
2574 {
2575 if (NILP (Fmember (tem, Vload_path)))
2576 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2577 }
2578
2579 /* Add site-list under the installation dir, if it exists. */
2580 tem = Fexpand_file_name (build_string ("site-lisp"),
2581 Vinstallation_directory);
2582 tem1 = Ffile_exists_p (tem);
2583 if (!NILP (tem1))
2584 {
2585 if (NILP (Fmember (tem, Vload_path)))
2586 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2587 }
2588
2589 /* If Emacs was not built in the source directory,
2590 and it is run from where it was built, add to load-path
2591 the lisp, leim and site-lisp dirs under that directory. */
2592
2593 if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
2594 {
2595 Lisp_Object tem2;
2596
2597 tem = Fexpand_file_name (build_string ("src/Makefile"),
2598 Vinstallation_directory);
2599 tem1 = Ffile_exists_p (tem);
2600
2601 /* Don't be fooled if they moved the entire source tree
2602 AFTER dumping Emacs. If the build directory is indeed
2603 different from the source dir, src/Makefile.in and
2604 src/Makefile will not be found together. */
2605 tem = Fexpand_file_name (build_string ("src/Makefile.in"),
2606 Vinstallation_directory);
2607 tem2 = Ffile_exists_p (tem);
2608 if (!NILP (tem1) && NILP (tem2))
2609 {
2610 tem = Fexpand_file_name (build_string ("lisp"),
2611 Vsource_directory);
2612
2613 if (NILP (Fmember (tem, Vload_path)))
2614 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2615
2616 tem = Fexpand_file_name (build_string ("leim"),
2617 Vsource_directory);
2618
2619 if (NILP (Fmember (tem, Vload_path)))
2620 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2621
2622 tem = Fexpand_file_name (build_string ("site-lisp"),
2623 Vsource_directory);
2624
2625 if (NILP (Fmember (tem, Vload_path)))
2626 Vload_path = nconc2 (Vload_path, Fcons (tem, Qnil));
2627 }
2628 }
2629 }
2630 }
2631 }
2632 else
2633 {
2634 /* NORMAL refers to the lisp dir in the source directory. */
2635 /* We used to add ../lisp at the front here, but
2636 that caused trouble because it was copied from dump_path
2637 into Vload_path, aboe, when Vinstallation_directory was non-nil.
2638 It should be unnecessary. */
2639 Vload_path = decode_env_path (0, normal);
2640 dump_path = Vload_path;
2641 }
2642 #endif
2643
2644 #ifndef WINDOWSNT
2645 /* When Emacs is invoked over network shares on NT, PATH_LOADSEARCH is
2646 almost never correct, thereby causing a warning to be printed out that
2647 confuses users. Since PATH_LOADSEARCH is always overridden by the
2648 EMACSLOADPATH environment variable below, disable the warning on NT. */
2649
2650 /* Warn if dirs in the *standard* path don't exist. */
2651 if (!turn_off_warning)
2652 {
2653 Lisp_Object path_tail;
2654
2655 for (path_tail = Vload_path;
2656 !NILP (path_tail);
2657 path_tail = XCONS (path_tail)->cdr)
2658 {
2659 Lisp_Object dirfile;
2660 dirfile = Fcar (path_tail);
2661 if (STRINGP (dirfile))
2662 {
2663 dirfile = Fdirectory_file_name (dirfile);
2664 if (access (XSTRING (dirfile)->data, 0) < 0)
2665 dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
2666 XCONS (path_tail)->car);
2667 }
2668 }
2669 }
2670 #endif /* WINDOWSNT */
2671
2672 /* If the EMACSLOADPATH environment variable is set, use its value.
2673 This doesn't apply if we're dumping. */
2674 #ifndef CANNOT_DUMP
2675 if (NILP (Vpurify_flag)
2676 && egetenv ("EMACSLOADPATH"))
2677 #endif
2678 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
2679
2680 Vvalues = Qnil;
2681
2682 load_in_progress = 0;
2683 Vload_file_name = Qnil;
2684
2685 load_descriptor_list = Qnil;
2686 }
2687
2688 /* Print a warning, using format string FORMAT, that directory DIRNAME
2689 does not exist. Print it on stderr and put it in *Message*. */
2690
2691 void
2692 dir_warning (format, dirname)
2693 char *format;
2694 Lisp_Object dirname;
2695 {
2696 char *buffer
2697 = (char *) alloca (XSTRING (dirname)->size + strlen (format) + 5);
2698
2699 fprintf (stderr, format, XSTRING (dirname)->data);
2700 sprintf (buffer, format, XSTRING (dirname)->data);
2701 message_dolog (buffer, strlen (buffer), 0);
2702 }
2703
2704 void
2705 syms_of_lread ()
2706 {
2707 defsubr (&Sread);
2708 defsubr (&Sread_from_string);
2709 defsubr (&Sintern);
2710 defsubr (&Sintern_soft);
2711 defsubr (&Sunintern);
2712 defsubr (&Sload);
2713 defsubr (&Seval_buffer);
2714 defsubr (&Seval_region);
2715 defsubr (&Sread_char);
2716 defsubr (&Sread_char_exclusive);
2717 defsubr (&Sread_event);
2718 defsubr (&Sget_file_char);
2719 defsubr (&Smapatoms);
2720
2721 DEFVAR_LISP ("obarray", &Vobarray,
2722 "Symbol table for use by `intern' and `read'.\n\
2723 It is a vector whose length ought to be prime for best results.\n\
2724 The vector's contents don't make sense if examined from Lisp programs;\n\
2725 to find all the symbols in an obarray, use `mapatoms'.");
2726
2727 DEFVAR_LISP ("values", &Vvalues,
2728 "List of values of all expressions which were read, evaluated and printed.\n\
2729 Order is reverse chronological.");
2730
2731 DEFVAR_LISP ("standard-input", &Vstandard_input,
2732 "Stream for read to get input from.\n\
2733 See documentation of `read' for possible values.");
2734 Vstandard_input = Qt;
2735
2736 DEFVAR_LISP ("load-path", &Vload_path,
2737 "*List of directories to search for files to load.\n\
2738 Each element is a string (directory name) or nil (try default directory).\n\
2739 Initialized based on EMACSLOADPATH environment variable, if any,\n\
2740 otherwise to default specified by file `paths.h' when Emacs was built.");
2741
2742 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
2743 "Non-nil iff inside of `load'.");
2744
2745 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
2746 "An alist of expressions to be evalled when particular files are loaded.\n\
2747 Each element looks like (FILENAME FORMS...).\n\
2748 When `load' is run and the file-name argument is FILENAME,\n\
2749 the FORMS in the corresponding element are executed at the end of loading.\n\n\
2750 FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
2751 with no directory specified, since that is how `load' is normally called.\n\
2752 An error in FORMS does not undo the load,\n\
2753 but does prevent execution of the rest of the FORMS.");
2754 Vafter_load_alist = Qnil;
2755
2756 DEFVAR_LISP ("load-history", &Vload_history,
2757 "Alist mapping source file names to symbols and features.\n\
2758 Each alist element is a list that starts with a file name,\n\
2759 except for one element (optional) that starts with nil and describes\n\
2760 definitions evaluated from buffers not visiting files.\n\
2761 The remaining elements of each list are symbols defined as functions\n\
2762 or variables, and cons cells `(provide . FEATURE)' and `(require . FEATURE)'.");
2763 Vload_history = Qnil;
2764
2765 DEFVAR_LISP ("load-file-name", &Vload_file_name,
2766 "Full name of file being loaded by `load'.");
2767 Vload_file_name = Qnil;
2768
2769 DEFVAR_LISP ("current-load-list", &Vcurrent_load_list,
2770 "Used for internal purposes by `load'.");
2771 Vcurrent_load_list = Qnil;
2772
2773 DEFVAR_LISP ("load-read-function", &Vload_read_function,
2774 "Function used by `load' and `eval-region' for reading expressions.\n\
2775 The default is nil, which means use the function `read'.");
2776 Vload_read_function = Qnil;
2777
2778 DEFVAR_LISP ("load-source-file-function", &Vload_source_file_function,
2779 "Function called in `load' for loading an Emacs lisp source file.\n\
2780 This function is for doing code conversion before reading the source file.\n\
2781 If nil, loading is done without any code conversion.\n\
2782 Arguments are FULLNAME, FILE, NOERROR, NOMESSAGE, where\n\
2783 FULLNAME is the full name of FILE.\n\
2784 See `load' for the meaning of the remaining arguments.");
2785 Vload_source_file_function = Qnil;
2786
2787 DEFVAR_BOOL ("load-force-doc-strings", &load_force_doc_strings,
2788 "Non-nil means `load' should force-load all dynamic doc strings.\n\
2789 This is useful when the file being loaded is a temporary copy.");
2790 load_force_doc_strings = 0;
2791
2792 DEFVAR_LISP ("source-directory", &Vsource_directory,
2793 "Directory in which Emacs sources were found when Emacs was built.\n\
2794 You cannot count on them to still be there!");
2795 Vsource_directory
2796 = Fexpand_file_name (build_string ("../"),
2797 Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
2798
2799 DEFVAR_LISP ("preloaded-file-list", &Vpreloaded_file_list,
2800 "List of files that were preloaded (when dumping Emacs).");
2801 Vpreloaded_file_list = Qnil;
2802
2803 /* Vsource_directory was initialized in init_lread. */
2804
2805 load_descriptor_list = Qnil;
2806 staticpro (&load_descriptor_list);
2807
2808 Qcurrent_load_list = intern ("current-load-list");
2809 staticpro (&Qcurrent_load_list);
2810
2811 Qstandard_input = intern ("standard-input");
2812 staticpro (&Qstandard_input);
2813
2814 Qread_char = intern ("read-char");
2815 staticpro (&Qread_char);
2816
2817 Qget_file_char = intern ("get-file-char");
2818 staticpro (&Qget_file_char);
2819
2820 Qbackquote = intern ("`");
2821 staticpro (&Qbackquote);
2822 Qcomma = intern (",");
2823 staticpro (&Qcomma);
2824 Qcomma_at = intern (",@");
2825 staticpro (&Qcomma_at);
2826 Qcomma_dot = intern (",.");
2827 staticpro (&Qcomma_dot);
2828
2829 Qinhibit_file_name_operation = intern ("inhibit-file-name-operation");
2830 staticpro (&Qinhibit_file_name_operation);
2831
2832 Qascii_character = intern ("ascii-character");
2833 staticpro (&Qascii_character);
2834
2835 Qfunction = intern ("function");
2836 staticpro (&Qfunction);
2837
2838 Qload = intern ("load");
2839 staticpro (&Qload);
2840
2841 Qload_file_name = intern ("load-file-name");
2842 staticpro (&Qload_file_name);
2843
2844 staticpro (&dump_path);
2845
2846 staticpro (&read_objects);
2847 read_objects = Qnil;
2848 }