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