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