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