*** empty log message ***
[bpt/emacs.git] / src / lread.c
CommitLineData
078e7b4a
JB
1/* Lisp parsing and input streams.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <stdio.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <sys/file.h>
25#undef NULL
26#include "config.h"
27#include "lisp.h"
28
29#ifndef standalone
30#include "buffer.h"
31#include "paths.h"
32#include "commands.h"
33#endif
34
35#ifdef lint
36#include <sys/inode.h>
37#endif /* lint */
38
39#ifndef X_OK
40#define X_OK 01
41#endif
42
43#ifdef LISP_FLOAT_TYPE
44#include <math.h>
45#endif /* LISP_FLOAT_TYPE */
46
47Lisp_Object Qread_char, Qget_file_char, Qstandard_input;
48Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
49
50/* non-zero if inside `load' */
51int load_in_progress;
52
53/* Search path for files to be loaded. */
54Lisp_Object Vload_path;
55
56/* File for get_file_char to read from. Use by load */
57static FILE *instream;
58
59/* When nonzero, read conses in pure space */
60static int read_pure;
61
62/* For use within read-from-string (this reader is non-reentrant!!) */
63static int read_from_string_index;
64static int read_from_string_limit;
65\f
66/* Handle unreading and rereading of characters.
67 Write READCHAR to read a character,
68 UNREAD(c) to unread c to be read again. */
69
70#define READCHAR readchar (readcharfun)
71#define UNREAD(c) unreadchar (readcharfun, c)
72
73static int
74readchar (readcharfun)
75 Lisp_Object readcharfun;
76{
77 Lisp_Object tem;
78 register struct buffer *inbuffer;
79 register int c, mpos;
80
81 if (XTYPE (readcharfun) == Lisp_Buffer)
82 {
83 inbuffer = XBUFFER (readcharfun);
84
85 if (BUF_PT (inbuffer) >= BUF_ZV (inbuffer))
86 return -1;
87 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, BUF_PT (inbuffer));
88 SET_BUF_PT (inbuffer, BUF_PT (inbuffer) + 1);
89
90 return c;
91 }
92 if (XTYPE (readcharfun) == Lisp_Marker)
93 {
94 inbuffer = XMARKER (readcharfun)->buffer;
95
96 mpos = marker_position (readcharfun);
97
98 if (mpos > BUF_ZV (inbuffer) - 1)
99 return -1;
100 c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, mpos);
101 if (mpos != BUF_GPT (inbuffer))
102 XMARKER (readcharfun)->bufpos++;
103 else
104 Fset_marker (readcharfun, make_number (mpos + 1),
105 Fmarker_buffer (readcharfun));
106 return c;
107 }
108 if (EQ (readcharfun, Qget_file_char))
109 return getc (instream);
110
111 if (XTYPE (readcharfun) == Lisp_String)
112 {
113 register int c;
114 /* This used to be return of a conditional expression,
115 but that truncated -1 to a char on VMS. */
116 if (read_from_string_index < read_from_string_limit)
117 c = XSTRING (readcharfun)->data[read_from_string_index++];
118 else
119 c = -1;
120 return c;
121 }
122
123 tem = call0 (readcharfun);
124
125 if (NULL (tem))
126 return -1;
127 return XINT (tem);
128}
129
130/* Unread the character C in the way appropriate for the stream READCHARFUN.
131 If the stream is a user function, call it with the char as argument. */
132
133static void
134unreadchar (readcharfun, c)
135 Lisp_Object readcharfun;
136 int c;
137{
138 if (XTYPE (readcharfun) == Lisp_Buffer)
139 {
140 if (XBUFFER (readcharfun) == current_buffer)
141 SET_PT (point - 1);
142 else
143 SET_BUF_PT (XBUFFER (readcharfun), BUF_PT (XBUFFER (readcharfun)) - 1);
144 }
145 else if (XTYPE (readcharfun) == Lisp_Marker)
146 XMARKER (readcharfun)->bufpos--;
147 else if (XTYPE (readcharfun) == Lisp_String)
148 read_from_string_index--;
149 else if (EQ (readcharfun, Qget_file_char))
150 ungetc (c, instream);
151 else
152 call1 (readcharfun, make_number (c));
153}
154
155static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
156\f
157/* get a character from the tty */
158
159DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0,
160 "Read a character from the command input (keyboard or macro).\n\
161It is returned as a number.")
162 ()
163{
164 register Lisp_Object val;
165
166#ifndef standalone
167 val = read_char (0);
168 if (XTYPE (val) != Lisp_Int)
169 {
170 unread_command_char = val;
171 error ("Object read was not a character");
172 }
173#else
174 val = getchar ();
175#endif
176
177 return val;
178}
179
180#ifdef HAVE_X_WINDOWS
181DEFUN ("read-event", Fread_event, Sread_event, 0, 0, 0,
182 "Read an event object from the input stream.")
183 ()
184{
185 register Lisp_Object val;
186
187 val = read_char (0);
188 return val;
189}
190
191DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 0, 0,
192 "Read a character from the command input (keyboard or macro).\n\
193It is returned as a number. Non character events are ignored.")
194 ()
195{
196 register Lisp_Object val;
197
198#ifndef standalone
199 val = read_char (0);
200 while (XTYPE (val) != Lisp_Int)
201 val = read_char (0);
202#else
203 val = getchar ();
204#endif
205
206 return val;
207}
208#endif /* HAVE_X_WINDOWS */
209
210DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
211 "Don't use this yourself.")
212 ()
213{
214 register Lisp_Object val;
215 XSET (val, Lisp_Int, getc (instream));
216 return val;
217}
218\f
219static void readevalloop ();
220static Lisp_Object load_unwind ();
221
222DEFUN ("load", Fload, Sload, 1, 4, 0,
223 "Execute a file of Lisp code named FILE.\n\
224First try FILE with `.elc' appended, then try with `.el',\n\
225 then try FILE unmodified.\n\
226This function searches the directories in `load-path'.\n\
227If optional second arg NOERROR is non-nil,\n\
228 report no error if FILE doesn't exist.\n\
229Print messages at start and end of loading unless\n\
230 optional third arg NOMESSAGE is non-nil.\n\
231If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
232 suffixes `.elc' or `.el' to the specified name FILE.\n\
233Return t if file exists.")
234 (str, noerror, nomessage, nosuffix)
235 Lisp_Object str, noerror, nomessage, nosuffix;
236{
237 register FILE *stream;
238 register int fd = -1;
239 register Lisp_Object lispstream;
240 register FILE **ptr;
241 int count = specpdl_ptr - specpdl;
242 Lisp_Object temp;
243 struct gcpro gcpro1;
244 Lisp_Object found;
245
246 CHECK_STRING (str, 0);
247 str = Fsubstitute_in_file_name (str);
248
249 /* Avoid weird lossage with null string as arg,
250 since it would try to load a directory as a Lisp file */
251 if (XSTRING (str)->size > 0)
252 {
253 fd = openp (Vload_path, str, !NULL (nosuffix) ? "" : ".elc:.el:",
254 &found, 0);
255 }
256
257 if (fd < 0)
258 {
259 if (NULL (noerror))
260 while (1)
261 Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
262 Fcons (str, Qnil)));
263 else
264 return Qnil;
265 }
266
267 if (!bcmp (&(XSTRING (found)->data[XSTRING (found)->size - 4]),
268 ".elc", 4))
269 {
270 struct stat s1, s2;
271 int result;
272
273 stat (XSTRING (found)->data, &s1);
274 XSTRING (found)->data[XSTRING (found)->size - 1] = 0;
275 result = stat (XSTRING (found)->data, &s2);
276 if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
277 message ("Source file `%s' newer than byte-compiled file",
278 XSTRING (found)->data);
279 XSTRING (found)->data[XSTRING (found)->size - 1] = 'c';
280 }
281
282 stream = fdopen (fd, "r");
283 if (stream == 0)
284 {
285 close (fd);
286 error ("Failure to create stdio stream for %s", XSTRING (str)->data);
287 }
288
289 if (NULL (nomessage))
290 message ("Loading %s...", XSTRING (str)->data);
291
292 GCPRO1 (str);
293 /* We may not be able to store STREAM itself as a Lisp_Object pointer
294 since that is guaranteed to work only for data that has been malloc'd.
295 So malloc a full-size pointer, and record the address of that pointer. */
296 ptr = (FILE **) xmalloc (sizeof (FILE *));
297 *ptr = stream;
298 XSET (lispstream, Lisp_Internal_Stream, (int) ptr);
299 record_unwind_protect (load_unwind, lispstream);
300 load_in_progress++;
301 readevalloop (Qget_file_char, stream, Feval, 0);
302 unbind_to (count, Qnil);
303
304 /* Run any load-hooks for this file. */
305 temp = Fassoc (str, Vafter_load_alist);
306 if (!NULL (temp))
307 Fprogn (Fcdr (temp));
308 UNGCPRO;
309
310 if (!noninteractive && NULL (nomessage))
311 message ("Loading %s...done", XSTRING (str)->data);
312 return Qt;
313}
314
315static Lisp_Object
316load_unwind (stream) /* used as unwind-protect function in load */
317 Lisp_Object stream;
318{
319 fclose (*(FILE **) XSTRING (stream));
320 free (XPNTR (stream));
321 if (--load_in_progress < 0) load_in_progress = 0;
322 return Qnil;
323}
324
325\f
326static int
327complete_filename_p (pathname)
328 Lisp_Object pathname;
329{
330 register unsigned char *s = XSTRING (pathname)->data;
331 return (*s == '/'
332#ifdef ALTOS
333 || *s == '@'
334#endif
335#ifdef VMS
336 || index (s, ':')
337#endif /* VMS */
338 );
339}
340
341/* Search for a file whose name is STR, looking in directories
342 in the Lisp list PATH, and trying suffixes from SUFFIX.
343 SUFFIX is a string containing possible suffixes separated by colons.
344 On success, returns a file descriptor. On failure, returns -1.
345
346 EXEC_ONLY nonzero means don't open the files,
347 just look for one that is executable. In this case,
348 returns 1 on success.
349
350 If STOREPTR is nonzero, it points to a slot where the name of
351 the file actually found should be stored as a Lisp string.
352 Nil is stored there on failure. */
353
354int
355openp (path, str, suffix, storeptr, exec_only)
356 Lisp_Object path, str;
357 char *suffix;
358 Lisp_Object *storeptr;
359 int exec_only;
360{
361 register int fd;
362 int fn_size = 100;
363 char buf[100];
364 register char *fn = buf;
365 int absolute = 0;
366 int want_size;
367 register Lisp_Object filename;
368 struct stat st;
369
370 if (storeptr)
371 *storeptr = Qnil;
372
373 if (complete_filename_p (str))
374 absolute = 1;
375
376 for (; !NULL (path); path = Fcdr (path))
377 {
378 char *nsuffix;
379
380 filename = Fexpand_file_name (str, Fcar (path));
381 if (!complete_filename_p (filename))
382 /* If there are non-absolute elts in PATH (eg ".") */
383 /* Of course, this could conceivably lose if luser sets
384 default-directory to be something non-absolute... */
385 {
386 filename = Fexpand_file_name (filename, current_buffer->directory);
387 if (!complete_filename_p (filename))
388 /* Give up on this path element! */
389 continue;
390 }
391
392 /* Calculate maximum size of any filename made from
393 this path element/specified file name and any possible suffix. */
394 want_size = strlen (suffix) + XSTRING (filename)->size + 1;
395 if (fn_size < want_size)
396 fn = (char *) alloca (fn_size = 100 + want_size);
397
398 nsuffix = suffix;
399
400 /* Loop over suffixes. */
401 while (1)
402 {
403 char *esuffix = (char *) index (nsuffix, ':');
404 int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
405
406 /* Concatenate path element/specified name with the suffix. */
407 strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size);
408 fn[XSTRING (filename)->size] = 0;
409 if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */
410 strncat (fn, nsuffix, lsuffix);
411
412 /* Ignore file if it's a directory. */
413 if (stat (fn, &st) >= 0
414 && (st.st_mode & S_IFMT) != S_IFDIR)
415 {
416 /* Check that we can access or open it. */
417 if (exec_only)
418 fd = (access (fn, X_OK) == 0) ? 1 : -1;
419 else
420 fd = open (fn, 0, 0);
421
422 if (fd >= 0)
423 {
424 /* We succeeded; return this descriptor and filename. */
425 if (storeptr)
426 *storeptr = build_string (fn);
427 return fd;
428 }
429 }
430
431 /* Advance to next suffix. */
432 if (esuffix == 0)
433 break;
434 nsuffix += lsuffix + 1;
435 }
436 if (absolute) return -1;
437 }
438
439 return -1;
440}
441
442\f
443Lisp_Object
444unreadpure () /* Used as unwind-protect function in readevalloop */
445{
446 read_pure = 0;
447 return Qnil;
448}
449
450static void
451readevalloop (readcharfun, stream, evalfun, printflag)
452 Lisp_Object readcharfun;
453 FILE *stream;
454 Lisp_Object (*evalfun) ();
455 int printflag;
456{
457 register int c;
458 register Lisp_Object val;
459 int count = specpdl_ptr - specpdl;
460
461 specbind (Qstandard_input, readcharfun);
462
463 while (1)
464 {
465 instream = stream;
466 c = READCHAR;
467 if (c == ';')
468 {
469 while ((c = READCHAR) != '\n' && c != -1);
470 continue;
471 }
472 if (c < 0) break;
473 if (c == ' ' || c == '\t' || c == '\n' || c == '\f') continue;
474
475 if (!NULL (Vpurify_flag) && c == '(')
476 {
477 record_unwind_protect (unreadpure, Qnil);
478 val = read_list (-1, readcharfun);
479 unbind_to (count + 1, Qnil);
480 }
481 else
482 {
483 UNREAD (c);
484 val = read0 (readcharfun);
485 }
486
487 val = (*evalfun) (val);
488 if (printflag)
489 {
490 Vvalues = Fcons (val, Vvalues);
491 if (EQ (Vstandard_output, Qt))
492 Fprin1 (val, Qnil);
493 else
494 Fprint (val, Qnil);
495 }
496 }
497
498 unbind_to (count, Qnil);
499}
500
501#ifndef standalone
502
503DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
504 "Execute the current buffer as Lisp code.\n\
505Programs can pass argument PRINTFLAG which controls printing of output:\n\
506nil means discard it; anything else is stream for print.\n\
507\n\
508If there is no error, point does not move. If there is an error,\n\
509point remains at the end of the last character read from the buffer.")
510 (printflag)
511 Lisp_Object printflag;
512{
513 int count = specpdl_ptr - specpdl;
514 Lisp_Object tem;
515
516 if (NULL (printflag))
517 tem = Qsymbolp;
518 else
519 tem = printflag;
520 specbind (Qstandard_output, tem);
521 record_unwind_protect (save_excursion_restore, save_excursion_save ());
522 SET_PT (BEGV);
523 readevalloop (Fcurrent_buffer (), 0, Feval, !NULL (printflag));
524 return unbind_to (count, Qnil);
525}
526
527DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r",
528 "Execute the region as Lisp code.\n\
529When called from programs, expects two arguments,\n\
530giving starting and ending indices in the current buffer\n\
531of the text to be executed.\n\
532Programs can pass third argument PRINTFLAG which controls output:\n\
533nil means discard it; anything else is stream for printing it.\n\
534\n\
535If there is no error, point does not move. If there is an error,\n\
536point remains at the end of the last character read from the buffer.")
537 (b, e, printflag)
538 Lisp_Object b, e, printflag;
539{
540 int count = specpdl_ptr - specpdl;
541 Lisp_Object tem;
542
543 if (NULL (printflag))
544 tem = Qsymbolp;
545 else
546 tem = printflag;
547 specbind (Qstandard_output, tem);
548
549 if (NULL (printflag))
550 record_unwind_protect (save_excursion_restore, save_excursion_save ());
551 record_unwind_protect (save_restriction_restore, save_restriction_save ());
552
553 /* This both uses b and checks its type. */
554 Fgoto_char (b);
555 Fnarrow_to_region (make_number (BEGV), e);
556 readevalloop (Fcurrent_buffer (), 0, Feval, !NULL (printflag));
557
558 return unbind_to (count, Qnil);
559}
560
561#endif /* standalone */
562\f
563DEFUN ("read", Fread, Sread, 0, 1, 0,
564 "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
565If STREAM is nil, use the value of `standard-input' (which see).\n\
566STREAM or the value of `standard-input' may be:\n\
567 a buffer (read from point and advance it)\n\
568 a marker (read from where it points and advance it)\n\
569 a function (call it with no arguments for each character,\n\
570 call it with a char as argument to push a char back)\n\
571 a string (takes text from string, starting at the beginning)\n\
572 t (read text line using minibuffer and use it).")
573 (readcharfun)
574 Lisp_Object readcharfun;
575{
576 extern Lisp_Object Fread_minibuffer ();
577
578 if (NULL (readcharfun))
579 readcharfun = Vstandard_input;
580 if (EQ (readcharfun, Qt))
581 readcharfun = Qread_char;
582
583#ifndef standalone
584 if (EQ (readcharfun, Qread_char))
585 return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
586#endif
587
588 if (XTYPE (readcharfun) == Lisp_String)
589 return Fcar (Fread_from_string (readcharfun, Qnil, Qnil));
590
591 return read0 (readcharfun);
592}
593
594DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
595 "Read one Lisp expression which is represented as text by STRING.\n\
596Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
597START and END optionally delimit a substring of STRING from which to read;\n\
598 they default to 0 and (length STRING) respectively.")
599 (string, start, end)
600 Lisp_Object string, start, end;
601{
602 int startval, endval;
603 Lisp_Object tem;
604
605 CHECK_STRING (string,0);
606
607 if (NULL (end))
608 endval = XSTRING (string)->size;
609 else
610 { CHECK_NUMBER (end,2);
611 endval = XINT (end);
612 if (endval < 0 || endval > XSTRING (string)->size)
613 args_out_of_range (string, end);
614 }
615
616 if (NULL (start))
617 startval = 0;
618 else
619 { CHECK_NUMBER (start,1);
620 startval = XINT (start);
621 if (startval < 0 || startval > endval)
622 args_out_of_range (string, start);
623 }
624
625 read_from_string_index = startval;
626 read_from_string_limit = endval;
627
628 tem = read0 (string);
629 return Fcons (tem, make_number (read_from_string_index));
630}
631\f
632/* Use this for recursive reads, in contexts where internal tokens are not allowed. */
633
634static Lisp_Object
635read0 (readcharfun)
636 Lisp_Object readcharfun;
637{
638 register Lisp_Object val;
639 char c;
640
641 val = read1 (readcharfun);
642 if (XTYPE (val) == Lisp_Internal)
643 {
644 c = XINT (val);
645 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil));
646 }
647
648 return val;
649}
650\f
651static int read_buffer_size;
652static char *read_buffer;
653
654static int
655read_escape (readcharfun)
656 Lisp_Object readcharfun;
657{
658 register int c = READCHAR;
659 switch (c)
660 {
661 case 'a':
662 return '\a';
663 case 'b':
664 return '\b';
665 case 'e':
666 return 033;
667 case 'f':
668 return '\f';
669 case 'n':
670 return '\n';
671 case 'r':
672 return '\r';
673 case 't':
674 return '\t';
675 case 'v':
676 return '\v';
677 case '\n':
678 return -1;
679
680 case 'M':
681 c = READCHAR;
682 if (c != '-')
683 error ("Invalid escape character syntax");
684 c = READCHAR;
685 if (c == '\\')
686 c = read_escape (readcharfun);
687 return c | 0200;
688
689 case 'C':
690 c = READCHAR;
691 if (c != '-')
692 error ("Invalid escape character syntax");
693 case '^':
694 c = READCHAR;
695 if (c == '\\')
696 c = read_escape (readcharfun);
697 if (c == '?')
698 return 0177;
699 else
700 return (c & (0200 | 037));
701
702 case '0':
703 case '1':
704 case '2':
705 case '3':
706 case '4':
707 case '5':
708 case '6':
709 case '7':
710 /* An octal escape, as in ANSI C. */
711 {
712 register int i = c - '0';
713 register int count = 0;
714 while (++count < 3)
715 {
716 if ((c = READCHAR) >= '0' && c <= '7')
717 {
718 i *= 8;
719 i += c - '0';
720 }
721 else
722 {
723 UNREAD (c);
724 break;
725 }
726 }
727 return i;
728 }
729
730 case 'x':
731 /* A hex escape, as in ANSI C. */
732 {
733 int i = 0;
734 while (1)
735 {
736 c = READCHAR;
737 if (c >= '0' && c <= '9')
738 {
739 i *= 16;
740 i += c - '0';
741 }
742 else if ((c >= 'a' && c <= 'f')
743 || (c >= 'A' && c <= 'F'))
744 {
745 i *= 16;
746 if (c >= 'a' && c <= 'f')
747 i += c - 'a' + 10;
748 else
749 i += c - 'A' + 10;
750 }
751 else
752 {
753 UNREAD (c);
754 break;
755 }
756 }
757 return i;
758 }
759
760 default:
761 return c;
762 }
763}
764
765static Lisp_Object
766read1 (readcharfun)
767 register Lisp_Object readcharfun;
768{
769 register int c;
770
771 retry:
772
773 c = READCHAR;
774 if (c < 0) return Fsignal (Qend_of_file, Qnil);
775
776 switch (c)
777 {
778 case '(':
779 return read_list (0, readcharfun);
780
781 case '[':
782 return read_vector (readcharfun);
783
784 case ')':
785 case ']':
786 case '.':
787 {
788 register Lisp_Object val;
789 XSET (val, Lisp_Internal, c);
790 return val;
791 }
792
793 case '#':
794 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
795
796 case ';':
797 while ((c = READCHAR) >= 0 && c != '\n');
798 goto retry;
799
800 case '\'':
801 {
802 return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
803 }
804
805 case '?':
806 {
807 register Lisp_Object val;
808
809 c = READCHAR;
810 if (c < 0) return Fsignal (Qend_of_file, Qnil);
811
812 if (c == '\\')
813 XSET (val, Lisp_Int, read_escape (readcharfun));
814 else
815 XSET (val, Lisp_Int, c);
816
817 return val;
818 }
819
820 case '\"':
821 {
822 register char *p = read_buffer;
823 register char *end = read_buffer + read_buffer_size;
824 register int c;
825 int cancel = 0;
826
827 while ((c = READCHAR) >= 0
828 && c != '\"')
829 {
830 if (p == end)
831 {
832 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
833 p += new - read_buffer;
834 read_buffer += new - read_buffer;
835 end = read_buffer + read_buffer_size;
836 }
837 if (c == '\\')
838 c = read_escape (readcharfun);
839 /* c is -1 if \ newline has just been seen */
840 if (c < 0)
841 {
842 if (p == read_buffer)
843 cancel = 1;
844 }
845 else
846 *p++ = c;
847 }
848 if (c < 0) return Fsignal (Qend_of_file, Qnil);
849
850 /* If purifying, and string starts with \ newline,
851 return zero instead. This is for doc strings
852 that we are really going to find in etc/DOC.nn.nn */
853 if (!NULL (Vpurify_flag) && NULL (Vdoc_file_name) && cancel)
854 return make_number (0);
855
856 if (read_pure)
857 return make_pure_string (read_buffer, p - read_buffer);
858 else
859 return make_string (read_buffer, p - read_buffer);
860 }
861
862 default:
863 if (c <= 040) goto retry;
864 {
865 register char *p = read_buffer;
866
867 {
868 register char *end = read_buffer + read_buffer_size;
869
870 while (c > 040 &&
871 !(c == '\"' || c == '\'' || c == ';' || c == '?'
872 || c == '(' || c == ')'
873#ifndef LISP_FLOAT_TYPE /* we need to see <number><dot><number> */
874 || c =='.'
875#endif /* not LISP_FLOAT_TYPE */
876 || c == '[' || c == ']' || c == '#'
877 ))
878 {
879 if (p == end)
880 {
881 register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
882 p += new - read_buffer;
883 read_buffer += new - read_buffer;
884 end = read_buffer + read_buffer_size;
885 }
886 if (c == '\\')
887 c = READCHAR;
888 *p++ = c;
889 c = READCHAR;
890 }
891
892 if (p == end)
893 {
894 char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
895 p += new - read_buffer;
896 read_buffer += new - read_buffer;
897/* end = read_buffer + read_buffer_size; */
898 }
899 *p = 0;
900 if (c >= 0)
901 UNREAD (c);
902 }
903
904 /* Is it an integer? */
905 {
906 register char *p1;
907 register Lisp_Object val;
908 p1 = read_buffer;
909 if (*p1 == '+' || *p1 == '-') p1++;
910 if (p1 != p)
911 {
912 while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
913 if (p1 == p)
914 /* It is. */
915 {
916 XSET (val, Lisp_Int, atoi (read_buffer));
917 return val;
918 }
919 }
920#ifdef LISP_FLOAT_TYPE
921 if (isfloat_string (read_buffer))
922 return make_float (atof (read_buffer));
923#endif
924 }
925
926 return intern (read_buffer);
927 }
928 }
929}
930\f
931#ifdef LISP_FLOAT_TYPE
932
933#include <ctype.h>
934#define LEAD_INT 1
935#define DOT_CHAR 2
936#define TRAIL_INT 4
937#define E_CHAR 8
938#define EXP_INT 16
939
940int
941isfloat_string (cp)
942 register char *cp;
943{
944 register state;
945
946 state = 0;
947 if (*cp == '+' || *cp == '-')
948 cp++;
949
950 if (isdigit(*cp))
951 {
952 state |= LEAD_INT;
953 while (isdigit (*cp))
954 cp ++;
955 }
956 if (*cp == '.')
957 {
958 state |= DOT_CHAR;
959 cp++;
960 }
961 if (isdigit(*cp))
962 {
963 state |= TRAIL_INT;
964 while (isdigit (*cp))
965 cp++;
966 }
967 if (*cp == 'e')
968 {
969 state |= E_CHAR;
970 cp++;
971 }
972 if ((*cp == '+') || (*cp == '-'))
973 cp++;
974
975 if (isdigit (*cp))
976 {
977 state |= EXP_INT;
978 while (isdigit (*cp))
979 cp++;
980 }
981 return (*cp == 0
982 && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT)
983 || state == (LEAD_INT|E_CHAR|EXP_INT)
984 || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT)));
985}
986#endif /* LISP_FLOAT_TYPE */
987\f
988static Lisp_Object
989read_vector (readcharfun)
990 Lisp_Object readcharfun;
991{
992 register int i;
993 register int size;
994 register Lisp_Object *ptr;
995 register Lisp_Object tem, vector;
996 register struct Lisp_Cons *otem;
997 Lisp_Object len;
998
999 tem = read_list (1, readcharfun);
1000 len = Flength (tem);
1001 vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
1002
1003
1004 size = XVECTOR (vector)->size;
1005 ptr = XVECTOR (vector)->contents;
1006 for (i = 0; i < size; i++)
1007 {
1008 ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
1009 otem = XCONS (tem);
1010 tem = Fcdr (tem);
1011 free_cons (otem);
1012 }
1013 return vector;
1014}
1015
1016/* flag = 1 means check for ] to terminate rather than ) and .
1017 flag = -1 means check for starting with defun
1018 and make structure pure. */
1019
1020static Lisp_Object
1021read_list (flag, readcharfun)
1022 int flag;
1023 register Lisp_Object readcharfun;
1024{
1025 /* -1 means check next element for defun,
1026 0 means don't check,
1027 1 means already checked and found defun. */
1028 int defunflag = flag < 0 ? -1 : 0;
1029 Lisp_Object val, tail;
1030 register Lisp_Object elt, tem;
1031 struct gcpro gcpro1, gcpro2;
1032
1033 val = Qnil;
1034 tail = Qnil;
1035
1036 while (1)
1037 {
1038 GCPRO2 (val, tail);
1039 elt = read1 (readcharfun);
1040 UNGCPRO;
1041 if (XTYPE (elt) == Lisp_Internal)
1042 {
1043 if (flag > 0)
1044 {
1045 if (XINT (elt) == ']')
1046 return val;
1047 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil));
1048 }
1049 if (XINT (elt) == ')')
1050 return val;
1051 if (XINT (elt) == '.')
1052 {
1053 GCPRO2 (val, tail);
1054 if (!NULL (tail))
1055 XCONS (tail)->cdr = read0 (readcharfun);
1056 else
1057 val = read0 (readcharfun);
1058 elt = read1 (readcharfun);
1059 UNGCPRO;
1060 if (XTYPE (elt) == Lisp_Internal && XINT (elt) == ')')
1061 return val;
1062 return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
1063 }
1064 return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
1065 }
1066 tem = (read_pure && flag <= 0
1067 ? pure_cons (elt, Qnil)
1068 : Fcons (elt, Qnil));
1069 if (!NULL (tail))
1070 XCONS (tail)->cdr = tem;
1071 else
1072 val = tem;
1073 tail = tem;
1074 if (defunflag < 0)
1075 defunflag = EQ (elt, Qdefun);
1076 else if (defunflag > 0)
1077 read_pure = 1;
1078 }
1079}
1080\f
1081Lisp_Object Vobarray;
1082Lisp_Object initial_obarray;
1083
1084Lisp_Object
1085check_obarray (obarray)
1086 Lisp_Object obarray;
1087{
1088 while (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0)
1089 {
1090 /* If Vobarray is now invalid, force it to be valid. */
1091 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
1092
1093 obarray = wrong_type_argument (Qvectorp, obarray);
1094 }
1095 return obarray;
1096}
1097
1098static int hash_string ();
1099Lisp_Object oblookup ();
1100
1101Lisp_Object
1102intern (str)
1103 char *str;
1104{
1105 Lisp_Object tem;
1106 int len = strlen (str);
1107 Lisp_Object obarray = Vobarray;
1108
1109 if (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0)
1110 obarray = check_obarray (obarray);
1111 tem = oblookup (obarray, str, len);
1112 if (XTYPE (tem) == Lisp_Symbol)
1113 return tem;
1114 return Fintern ((!NULL (Vpurify_flag)
1115 ? make_pure_string (str, len)
1116 : make_string (str, len)),
1117 obarray);
1118}
1119
1120DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
1121 "Return the canonical symbol whose name is STRING.\n\
1122If there is none, one is created by this function and returned.\n\
1123A second optional argument specifies the obarray to use;\n\
1124it defaults to the value of `obarray'.")
1125 (str, obarray)
1126 Lisp_Object str, obarray;
1127{
1128 register Lisp_Object tem, sym, *ptr;
1129
1130 if (NULL (obarray)) obarray = Vobarray;
1131 obarray = check_obarray (obarray);
1132
1133 CHECK_STRING (str, 0);
1134
1135 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1136 if (XTYPE (tem) != Lisp_Int)
1137 return tem;
1138
1139 if (!NULL (Vpurify_flag))
1140 str = Fpurecopy (str);
1141 sym = Fmake_symbol (str);
1142
1143 ptr = &XVECTOR (obarray)->contents[XINT (tem)];
1144 if (XTYPE (*ptr) == Lisp_Symbol)
1145 XSYMBOL (sym)->next = XSYMBOL (*ptr);
1146 else
1147 XSYMBOL (sym)->next = 0;
1148 *ptr = sym;
1149 return sym;
1150}
1151
1152DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
1153 "Return the canonical symbol whose name is STRING, or nil if none exists.\n\
1154A second optional argument specifies the obarray to use;\n\
1155it defaults to the value of `obarray'.")
1156 (str, obarray)
1157 Lisp_Object str, obarray;
1158{
1159 register Lisp_Object tem;
1160
1161 if (NULL (obarray)) obarray = Vobarray;
1162 obarray = check_obarray (obarray);
1163
1164 CHECK_STRING (str, 0);
1165
1166 tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
1167 if (XTYPE (tem) != Lisp_Int)
1168 return tem;
1169 return Qnil;
1170}
1171
1172Lisp_Object
1173oblookup (obarray, ptr, size)
1174 Lisp_Object obarray;
1175 register char *ptr;
1176 register int size;
1177{
1178 int hash, obsize;
1179 register Lisp_Object tail;
1180 Lisp_Object bucket, tem;
1181
1182 if (XTYPE (obarray) != Lisp_Vector ||
1183 (obsize = XVECTOR (obarray)->size) == 0)
1184 {
1185 obarray = check_obarray (obarray);
1186 obsize = XVECTOR (obarray)->size;
1187 }
1188 /* Combining next two lines breaks VMS C 2.3. */
1189 hash = hash_string (ptr, size);
1190 hash %= obsize;
1191 bucket = XVECTOR (obarray)->contents[hash];
1192 if (XFASTINT (bucket) == 0)
1193 ;
1194 else if (XTYPE (bucket) != Lisp_Symbol)
1195 error ("Bad data in guts of obarray"); /* Like CADR error message */
1196 else for (tail = bucket; ; XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next))
1197 {
1198 if (XSYMBOL (tail)->name->size == size &&
1199 !bcmp (XSYMBOL (tail)->name->data, ptr, size))
1200 return tail;
1201 else if (XSYMBOL (tail)->next == 0)
1202 break;
1203 }
1204 XSET (tem, Lisp_Int, hash);
1205 return tem;
1206}
1207
1208static int
1209hash_string (ptr, len)
1210 unsigned char *ptr;
1211 int len;
1212{
1213 register unsigned char *p = ptr;
1214 register unsigned char *end = p + len;
1215 register unsigned char c;
1216 register int hash = 0;
1217
1218 while (p != end)
1219 {
1220 c = *p++;
1221 if (c >= 0140) c -= 40;
1222 hash = ((hash<<3) + (hash>>28) + c);
1223 }
1224 return hash & 07777777777;
1225}
1226
1227void
1228map_obarray (obarray, fn, arg)
1229 Lisp_Object obarray;
1230 int (*fn) ();
1231 Lisp_Object arg;
1232{
1233 register int i;
1234 register Lisp_Object tail;
1235 CHECK_VECTOR (obarray, 1);
1236 for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
1237 {
1238 tail = XVECTOR (obarray)->contents[i];
1239 if (XFASTINT (tail) != 0)
1240 while (1)
1241 {
1242 (*fn) (tail, arg);
1243 if (XSYMBOL (tail)->next == 0)
1244 break;
1245 XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next);
1246 }
1247 }
1248}
1249
1250mapatoms_1 (sym, function)
1251 Lisp_Object sym, function;
1252{
1253 call1 (function, sym);
1254}
1255
1256DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
1257 "Call FUNCTION on every symbol in OBARRAY.\n\
1258OBARRAY defaults to the value of `obarray'.")
1259 (function, obarray)
1260 Lisp_Object function, obarray;
1261{
1262 Lisp_Object tem;
1263
1264 if (NULL (obarray)) obarray = Vobarray;
1265 obarray = check_obarray (obarray);
1266
1267 map_obarray (obarray, mapatoms_1, function);
1268 return Qnil;
1269}
1270
1271#define OBARRAY_SIZE 509
1272
1273void
1274init_obarray ()
1275{
1276 Lisp_Object oblength;
1277 int hash;
1278 Lisp_Object *tem;
1279
1280 XFASTINT (oblength) = OBARRAY_SIZE;
1281
1282 Qnil = Fmake_symbol (make_pure_string ("nil", 3));
1283 Vobarray = Fmake_vector (oblength, make_number (0));
1284 initial_obarray = Vobarray;
1285 staticpro (&initial_obarray);
1286 /* Intern nil in the obarray */
1287 /* These locals are to kludge around a pyramid compiler bug. */
1288 hash = hash_string ("nil", 3);
1289 /* Separate statement here to avoid VAXC bug. */
1290 hash %= OBARRAY_SIZE;
1291 tem = &XVECTOR (Vobarray)->contents[hash];
1292 *tem = Qnil;
1293
1294 Qunbound = Fmake_symbol (make_pure_string ("unbound", 7));
1295 XSYMBOL (Qnil)->function = Qunbound;
1296 XSYMBOL (Qunbound)->value = Qunbound;
1297 XSYMBOL (Qunbound)->function = Qunbound;
1298
1299 Qt = intern ("t");
1300 XSYMBOL (Qnil)->value = Qnil;
1301 XSYMBOL (Qnil)->plist = Qnil;
1302 XSYMBOL (Qt)->value = Qt;
1303
1304 /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */
1305 Vpurify_flag = Qt;
1306
1307 Qvariable_documentation = intern ("variable-documentation");
1308
1309 read_buffer_size = 100;
1310 read_buffer = (char *) malloc (read_buffer_size);
1311}
1312\f
1313void
1314defsubr (sname)
1315 struct Lisp_Subr *sname;
1316{
1317 Lisp_Object sym;
1318 sym = intern (sname->symbol_name);
1319 XSET (XSYMBOL (sym)->function, Lisp_Subr, sname);
1320}
1321
1322#ifdef NOTDEF /* use fset in subr.el now */
1323void
1324defalias (sname, string)
1325 struct Lisp_Subr *sname;
1326 char *string;
1327{
1328 Lisp_Object sym;
1329 sym = intern (string);
1330 XSET (XSYMBOL (sym)->function, Lisp_Subr, sname);
1331}
1332#endif /* NOTDEF */
1333
1334/* New replacement for DefIntVar; it ignores the doc string argument
1335 on the assumption that make-docfile will handle that. */
1336/* Define an "integer variable"; a symbol whose value is forwarded
1337 to a C variable of type int. Sample call: */
1338 /* DEFVARINT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */
1339
1340void
1341defvar_int (namestring, address, doc)
1342 char *namestring;
1343 int *address;
1344 char *doc;
1345{
1346 Lisp_Object sym;
1347 sym = intern (namestring);
1348 XSET (XSYMBOL (sym)->value, Lisp_Intfwd, address);
1349}
1350
1351/* Similar but define a variable whose value is T if address contains 1,
1352 NIL if address contains 0 */
1353
1354void
1355defvar_bool (namestring, address, doc)
1356 char *namestring;
1357 int *address;
1358 char *doc;
1359{
1360 Lisp_Object sym;
1361 sym = intern (namestring);
1362 XSET (XSYMBOL (sym)->value, Lisp_Boolfwd, address);
1363}
1364
1365/* Similar but define a variable whose value is the Lisp Object stored at address. */
1366
1367void
1368defvar_lisp (namestring, address, doc)
1369 char *namestring;
1370 Lisp_Object *address;
1371 char *doc;
1372{
1373 Lisp_Object sym;
1374 sym = intern (namestring);
1375 XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address);
1376 staticpro (address);
1377}
1378
1379/* Similar but don't request gc-marking of the C variable.
1380 Used when that variable will be gc-marked for some other reason,
1381 since marking the same slot twice can cause trouble with strings. */
1382
1383void
1384defvar_lisp_nopro (namestring, address, doc)
1385 char *namestring;
1386 Lisp_Object *address;
1387 char *doc;
1388{
1389 Lisp_Object sym;
1390 sym = intern (namestring);
1391 XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address);
1392}
1393
1394#ifndef standalone
1395
1396/* Similar but define a variable whose value is the Lisp Object stored in
1397 the current buffer. address is the address of the slot in the buffer that is current now. */
1398
1399void
1400defvar_per_buffer (namestring, address, doc)
1401 char *namestring;
1402 Lisp_Object *address;
1403 char *doc;
1404{
1405 Lisp_Object sym;
1406 int offset;
1407 extern struct buffer buffer_local_symbols;
1408
1409 sym = intern (namestring);
1410 offset = (char *)address - (char *)current_buffer;
1411
1412 XSET (XSYMBOL (sym)->value, Lisp_Buffer_Objfwd,
1413 (Lisp_Object *) offset);
1414 *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
1415 if (*(int *)(offset + (char *)&buffer_local_flags) == 0)
1416 /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
1417 slot of buffer_local_flags */
1418 abort ();
1419}
1420
1421#endif /* standalone */
1422\f
1423init_read ()
1424{
1425 char *normal = PATH_LOADSEARCH;
1426 Lisp_Object normal_path;
1427
1428 /* Warn if dirs in the *standard* path don't exist. */
1429 normal_path = decode_env_path ("", normal);
1430 for (; !NULL (normal_path); normal_path = XCONS (normal_path)->cdr)
1431 {
1432 Lisp_Object dirfile;
1433 dirfile = Fcar (normal_path);
1434 if (!NULL (dirfile))
1435 {
1436 dirfile = Fdirectory_file_name (dirfile);
1437 if (access (XSTRING (dirfile)->data, 0) < 0)
1438 printf ("Warning: lisp library (%s) does not exist.\n",
1439 XSTRING (Fcar (normal_path))->data);
1440 }
1441 }
1442
1443 Vvalues = Qnil;
1444
1445 Vload_path = decode_env_path ("EMACSLOADPATH", normal);
1446#ifndef CANNOT_DUMP
1447 if (!NULL (Vpurify_flag))
1448 Vload_path = Fcons (build_string ("../lisp"), Vload_path);
1449#endif /* not CANNOT_DUMP */
1450 load_in_progress = 0;
1451}
1452
1453void
1454syms_of_read ()
1455{
1456 defsubr (&Sread);
1457 defsubr (&Sread_from_string);
1458 defsubr (&Sintern);
1459 defsubr (&Sintern_soft);
1460 defsubr (&Sload);
1461 defsubr (&Seval_current_buffer);
1462 defsubr (&Seval_region);
1463 defsubr (&Sread_char);
1464 defsubr (&Sread_char_exclusive);
1465#ifdef HAVE_X_WINDOWS
1466 defsubr (&Sread_event);
1467#endif /* HAVE_X_WINDOWS */
1468 defsubr (&Sget_file_char);
1469 defsubr (&Smapatoms);
1470
1471 DEFVAR_LISP ("obarray", &Vobarray,
1472 "Symbol table for use by `intern' and `read'.\n\
1473It is a vector whose length ought to be prime for best results.\n\
1474The vector's contents don't make sense if examined from Lisp programs;\n\
1475to find all the symbols in an obarray, use `mapatoms'.");
1476
1477 DEFVAR_LISP ("values", &Vvalues,
1478 "List of values of all expressions which were read, evaluated and printed.\n\
1479Order is reverse chronological.");
1480
1481 DEFVAR_LISP ("standard-input", &Vstandard_input,
1482 "Stream for read to get input from.\n\
1483See documentation of `read' for possible values.");
1484 Vstandard_input = Qt;
1485
1486 DEFVAR_LISP ("load-path", &Vload_path,
1487 "*List of directories to search for files to load.\n\
1488Each element is a string (directory name) or nil (try default directory).\n\
1489Initialized based on EMACSLOADPATH environment variable, if any,\n\
1490otherwise to default specified in by file `paths.h' when Emacs was built.");
1491
1492 DEFVAR_BOOL ("load-in-progress", &load_in_progress,
1493 "Non-nil iff inside of `load'.");
1494
1495 DEFVAR_LISP ("after-load-alist", &Vafter_load_alist,
1496 "An alist of expressions to be evalled when particular files are loaded.\n\
1497Each element looks like (FILENAME FORMS...).\n\
1498When `load' is run and the file-name argument is FILENAME,\n\
1499the FORMS in the corresponding element are executed at the end of loading.\n\n\
1500FILENAME must match exactly! Normally FILENAME is the name of a library,\n\
1501with no directory specified, since that is how `load' is normally called.\n\
1502An error in FORMS does not undo the load,\n\
1503but does prevent execution of the rest of the FORMS.");
1504 Vafter_load_alist = Qnil;
1505
1506 Qstandard_input = intern ("standard-input");
1507 staticpro (&Qstandard_input);
1508
1509 Qread_char = intern ("read-char");
1510 staticpro (&Qread_char);
1511
1512 Qget_file_char = intern ("get-file-char");
1513 staticpro (&Qget_file_char);
1514}