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