Commit | Line | Data |
---|---|---|
078e7b4a | 1 | /* Lisp parsing and input streams. |
08564963 | 2 | Copyright (C) 1985, 1986, 1987, 1988, 1989, 1992 Free Software Foundation, Inc. |
078e7b4a JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
46947372 | 8 | the Free Software Foundation; either version 2, or (at your option) |
078e7b4a JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the 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 | ||
47 | Lisp_Object Qread_char, Qget_file_char, Qstandard_input; | |
48 | Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist; | |
49 | ||
50 | /* non-zero if inside `load' */ | |
51 | int load_in_progress; | |
52 | ||
53 | /* Search path for files to be loaded. */ | |
54 | Lisp_Object Vload_path; | |
55 | ||
56 | /* File for get_file_char to read from. Use by load */ | |
57 | static FILE *instream; | |
58 | ||
59 | /* When nonzero, read conses in pure space */ | |
60 | static int read_pure; | |
61 | ||
62 | /* For use within read-from-string (this reader is non-reentrant!!) */ | |
63 | static int read_from_string_index; | |
64 | static 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 | ||
73 | static int | |
74 | readchar (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 | ||
265a9e55 | 125 | if (NILP (tem)) |
078e7b4a JB |
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 | ||
133 | static void | |
134 | unreadchar (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 | ||
155 | static Lisp_Object read0 (), read1 (), read_list (), read_vector (); | |
156 | \f | |
157 | /* get a character from the tty */ | |
158 | ||
159 | DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0, | |
160 | "Read a character from the command input (keyboard or macro).\n\ | |
161 | It 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 | |
181 | DEFUN ("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 | ||
191 | DEFUN ("read-char-exclusive", Fread_char_exclusive, Sread_char_exclusive, 0, 0, 0, | |
192 | "Read a character from the command input (keyboard or macro).\n\ | |
193 | It 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 | ||
210 | DEFUN ("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 | |
219 | static void readevalloop (); | |
220 | static Lisp_Object load_unwind (); | |
221 | ||
222 | DEFUN ("load", Fload, Sload, 1, 4, 0, | |
223 | "Execute a file of Lisp code named FILE.\n\ | |
224 | First try FILE with `.elc' appended, then try with `.el',\n\ | |
225 | then try FILE unmodified.\n\ | |
226 | This function searches the directories in `load-path'.\n\ | |
227 | If optional second arg NOERROR is non-nil,\n\ | |
228 | report no error if FILE doesn't exist.\n\ | |
229 | Print messages at start and end of loading unless\n\ | |
230 | optional third arg NOMESSAGE is non-nil.\n\ | |
231 | If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\ | |
232 | suffixes `.elc' or `.el' to the specified name FILE.\n\ | |
233 | Return 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 | { | |
265a9e55 | 253 | fd = openp (Vload_path, str, !NILP (nosuffix) ? "" : ".elc:.el:", |
078e7b4a JB |
254 | &found, 0); |
255 | } | |
256 | ||
257 | if (fd < 0) | |
258 | { | |
265a9e55 | 259 | if (NILP (noerror)) |
078e7b4a JB |
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 | ||
265a9e55 | 289 | if (NILP (nomessage)) |
078e7b4a JB |
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); | |
265a9e55 | 306 | if (!NILP (temp)) |
078e7b4a JB |
307 | Fprogn (Fcdr (temp)); |
308 | UNGCPRO; | |
309 | ||
265a9e55 | 310 | if (!noninteractive && NILP (nomessage)) |
078e7b4a JB |
311 | message ("Loading %s...done", XSTRING (str)->data); |
312 | return Qt; | |
313 | } | |
314 | ||
315 | static Lisp_Object | |
316 | load_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 | |
326 | static int | |
327 | complete_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 | ||
354 | int | |
355 | openp (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 | ||
265a9e55 | 376 | for (; !NILP (path); path = Fcdr (path)) |
078e7b4a JB |
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 | |
443 | Lisp_Object | |
444 | unreadpure () /* Used as unwind-protect function in readevalloop */ | |
445 | { | |
446 | read_pure = 0; | |
447 | return Qnil; | |
448 | } | |
449 | ||
450 | static void | |
451 | readevalloop (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 | ||
265a9e55 | 475 | if (!NILP (Vpurify_flag) && c == '(') |
078e7b4a JB |
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 | ||
94b304d7 RS |
503 | DEFUN ("eval-buffer", Feval_buffer, Seval_buffer, 0, 1, "", |
504 | "Execute the current buffer as Lisp code.\n\ | |
505 | Programs can pass two arguments, BUFFER and PRINTFLAG.\n\ | |
506 | BUFFER is the buffer to evaluate (nil means use current buffer).\n\ | |
507 | PRINTFLAG controls printing of output:\n\ | |
228d4b1c JA |
508 | nil means discard it; anything else is stream for print.\n\ |
509 | \n\ | |
510 | If there is no error, point does not move. If there is an error,\n\ | |
511 | point remains at the end of the last character read from the buffer.") | |
512 | (bufname, printflag) | |
513 | Lisp_Object bufname, printflag; | |
514 | { | |
515 | int count = specpdl_ptr - specpdl; | |
516 | Lisp_Object tem, buf; | |
517 | ||
dfdb645c | 518 | if (NILP (bufname)) |
228d4b1c JA |
519 | buf = Fcurrent_buffer (); |
520 | else | |
521 | buf = Fget_buffer (bufname); | |
dfdb645c | 522 | if (NILP (buf)) |
228d4b1c JA |
523 | error ("No such buffer."); |
524 | ||
dfdb645c | 525 | if (NILP (printflag)) |
228d4b1c JA |
526 | tem = Qsymbolp; |
527 | else | |
528 | tem = printflag; | |
529 | specbind (Qstandard_output, tem); | |
530 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
531 | BUF_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf))); | |
dfdb645c | 532 | readevalloop (buf, 0, Feval, !NILP (printflag)); |
228d4b1c JA |
533 | unbind_to (count); |
534 | ||
535 | return Qnil; | |
536 | } | |
537 | ||
538 | #if 0 | |
078e7b4a JB |
539 | DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "", |
540 | "Execute the current buffer as Lisp code.\n\ | |
541 | Programs can pass argument PRINTFLAG which controls printing of output:\n\ | |
542 | nil means discard it; anything else is stream for print.\n\ | |
543 | \n\ | |
544 | If there is no error, point does not move. If there is an error,\n\ | |
545 | point remains at the end of the last character read from the buffer.") | |
546 | (printflag) | |
547 | Lisp_Object printflag; | |
548 | { | |
549 | int count = specpdl_ptr - specpdl; | |
550 | Lisp_Object tem; | |
551 | ||
265a9e55 | 552 | if (NILP (printflag)) |
078e7b4a JB |
553 | tem = Qsymbolp; |
554 | else | |
555 | tem = printflag; | |
556 | specbind (Qstandard_output, tem); | |
557 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
558 | SET_PT (BEGV); | |
265a9e55 | 559 | readevalloop (Fcurrent_buffer (), 0, Feval, !NILP (printflag)); |
078e7b4a JB |
560 | return unbind_to (count, Qnil); |
561 | } | |
228d4b1c | 562 | #endif |
078e7b4a JB |
563 | |
564 | DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r", | |
565 | "Execute the region as Lisp code.\n\ | |
566 | When called from programs, expects two arguments,\n\ | |
567 | giving starting and ending indices in the current buffer\n\ | |
568 | of the text to be executed.\n\ | |
569 | Programs can pass third argument PRINTFLAG which controls output:\n\ | |
570 | nil means discard it; anything else is stream for printing it.\n\ | |
571 | \n\ | |
572 | If there is no error, point does not move. If there is an error,\n\ | |
573 | point remains at the end of the last character read from the buffer.") | |
574 | (b, e, printflag) | |
575 | Lisp_Object b, e, printflag; | |
576 | { | |
577 | int count = specpdl_ptr - specpdl; | |
578 | Lisp_Object tem; | |
579 | ||
265a9e55 | 580 | if (NILP (printflag)) |
078e7b4a JB |
581 | tem = Qsymbolp; |
582 | else | |
583 | tem = printflag; | |
584 | specbind (Qstandard_output, tem); | |
585 | ||
265a9e55 | 586 | if (NILP (printflag)) |
078e7b4a JB |
587 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); |
588 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); | |
589 | ||
590 | /* This both uses b and checks its type. */ | |
591 | Fgoto_char (b); | |
592 | Fnarrow_to_region (make_number (BEGV), e); | |
265a9e55 | 593 | readevalloop (Fcurrent_buffer (), 0, Feval, !NILP (printflag)); |
078e7b4a JB |
594 | |
595 | return unbind_to (count, Qnil); | |
596 | } | |
597 | ||
598 | #endif /* standalone */ | |
599 | \f | |
600 | DEFUN ("read", Fread, Sread, 0, 1, 0, | |
601 | "Read one Lisp expression as text from STREAM, return as Lisp object.\n\ | |
602 | If STREAM is nil, use the value of `standard-input' (which see).\n\ | |
603 | STREAM or the value of `standard-input' may be:\n\ | |
604 | a buffer (read from point and advance it)\n\ | |
605 | a marker (read from where it points and advance it)\n\ | |
606 | a function (call it with no arguments for each character,\n\ | |
607 | call it with a char as argument to push a char back)\n\ | |
608 | a string (takes text from string, starting at the beginning)\n\ | |
609 | t (read text line using minibuffer and use it).") | |
610 | (readcharfun) | |
611 | Lisp_Object readcharfun; | |
612 | { | |
613 | extern Lisp_Object Fread_minibuffer (); | |
614 | ||
265a9e55 | 615 | if (NILP (readcharfun)) |
078e7b4a JB |
616 | readcharfun = Vstandard_input; |
617 | if (EQ (readcharfun, Qt)) | |
618 | readcharfun = Qread_char; | |
619 | ||
620 | #ifndef standalone | |
621 | if (EQ (readcharfun, Qread_char)) | |
622 | return Fread_minibuffer (build_string ("Lisp expression: "), Qnil); | |
623 | #endif | |
624 | ||
625 | if (XTYPE (readcharfun) == Lisp_String) | |
626 | return Fcar (Fread_from_string (readcharfun, Qnil, Qnil)); | |
627 | ||
628 | return read0 (readcharfun); | |
629 | } | |
630 | ||
631 | DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0, | |
632 | "Read one Lisp expression which is represented as text by STRING.\n\ | |
633 | Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\ | |
634 | START and END optionally delimit a substring of STRING from which to read;\n\ | |
635 | they default to 0 and (length STRING) respectively.") | |
636 | (string, start, end) | |
637 | Lisp_Object string, start, end; | |
638 | { | |
639 | int startval, endval; | |
640 | Lisp_Object tem; | |
641 | ||
642 | CHECK_STRING (string,0); | |
643 | ||
265a9e55 | 644 | if (NILP (end)) |
078e7b4a JB |
645 | endval = XSTRING (string)->size; |
646 | else | |
647 | { CHECK_NUMBER (end,2); | |
648 | endval = XINT (end); | |
649 | if (endval < 0 || endval > XSTRING (string)->size) | |
650 | args_out_of_range (string, end); | |
651 | } | |
652 | ||
265a9e55 | 653 | if (NILP (start)) |
078e7b4a JB |
654 | startval = 0; |
655 | else | |
656 | { CHECK_NUMBER (start,1); | |
657 | startval = XINT (start); | |
658 | if (startval < 0 || startval > endval) | |
659 | args_out_of_range (string, start); | |
660 | } | |
661 | ||
662 | read_from_string_index = startval; | |
663 | read_from_string_limit = endval; | |
664 | ||
665 | tem = read0 (string); | |
666 | return Fcons (tem, make_number (read_from_string_index)); | |
667 | } | |
668 | \f | |
669 | /* Use this for recursive reads, in contexts where internal tokens are not allowed. */ | |
670 | ||
671 | static Lisp_Object | |
672 | read0 (readcharfun) | |
673 | Lisp_Object readcharfun; | |
674 | { | |
675 | register Lisp_Object val; | |
676 | char c; | |
677 | ||
678 | val = read1 (readcharfun); | |
679 | if (XTYPE (val) == Lisp_Internal) | |
680 | { | |
681 | c = XINT (val); | |
682 | return Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil)); | |
683 | } | |
684 | ||
685 | return val; | |
686 | } | |
687 | \f | |
688 | static int read_buffer_size; | |
689 | static char *read_buffer; | |
690 | ||
691 | static int | |
692 | read_escape (readcharfun) | |
693 | Lisp_Object readcharfun; | |
694 | { | |
695 | register int c = READCHAR; | |
696 | switch (c) | |
697 | { | |
698 | case 'a': | |
265a9e55 | 699 | return '\007'; |
078e7b4a JB |
700 | case 'b': |
701 | return '\b'; | |
702 | case 'e': | |
703 | return 033; | |
704 | case 'f': | |
705 | return '\f'; | |
706 | case 'n': | |
707 | return '\n'; | |
708 | case 'r': | |
709 | return '\r'; | |
710 | case 't': | |
711 | return '\t'; | |
712 | case 'v': | |
713 | return '\v'; | |
714 | case '\n': | |
715 | return -1; | |
716 | ||
717 | case 'M': | |
718 | c = READCHAR; | |
719 | if (c != '-') | |
720 | error ("Invalid escape character syntax"); | |
721 | c = READCHAR; | |
722 | if (c == '\\') | |
723 | c = read_escape (readcharfun); | |
724 | return c | 0200; | |
725 | ||
726 | case 'C': | |
727 | c = READCHAR; | |
728 | if (c != '-') | |
729 | error ("Invalid escape character syntax"); | |
730 | case '^': | |
731 | c = READCHAR; | |
732 | if (c == '\\') | |
733 | c = read_escape (readcharfun); | |
734 | if (c == '?') | |
735 | return 0177; | |
736 | else | |
737 | return (c & (0200 | 037)); | |
738 | ||
739 | case '0': | |
740 | case '1': | |
741 | case '2': | |
742 | case '3': | |
743 | case '4': | |
744 | case '5': | |
745 | case '6': | |
746 | case '7': | |
747 | /* An octal escape, as in ANSI C. */ | |
748 | { | |
749 | register int i = c - '0'; | |
750 | register int count = 0; | |
751 | while (++count < 3) | |
752 | { | |
753 | if ((c = READCHAR) >= '0' && c <= '7') | |
754 | { | |
755 | i *= 8; | |
756 | i += c - '0'; | |
757 | } | |
758 | else | |
759 | { | |
760 | UNREAD (c); | |
761 | break; | |
762 | } | |
763 | } | |
764 | return i; | |
765 | } | |
766 | ||
767 | case 'x': | |
768 | /* A hex escape, as in ANSI C. */ | |
769 | { | |
770 | int i = 0; | |
771 | while (1) | |
772 | { | |
773 | c = READCHAR; | |
774 | if (c >= '0' && c <= '9') | |
775 | { | |
776 | i *= 16; | |
777 | i += c - '0'; | |
778 | } | |
779 | else if ((c >= 'a' && c <= 'f') | |
780 | || (c >= 'A' && c <= 'F')) | |
781 | { | |
782 | i *= 16; | |
783 | if (c >= 'a' && c <= 'f') | |
784 | i += c - 'a' + 10; | |
785 | else | |
786 | i += c - 'A' + 10; | |
787 | } | |
788 | else | |
789 | { | |
790 | UNREAD (c); | |
791 | break; | |
792 | } | |
793 | } | |
794 | return i; | |
795 | } | |
796 | ||
797 | default: | |
798 | return c; | |
799 | } | |
800 | } | |
801 | ||
802 | static Lisp_Object | |
803 | read1 (readcharfun) | |
804 | register Lisp_Object readcharfun; | |
805 | { | |
806 | register int c; | |
807 | ||
808 | retry: | |
809 | ||
810 | c = READCHAR; | |
811 | if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
812 | ||
813 | switch (c) | |
814 | { | |
815 | case '(': | |
816 | return read_list (0, readcharfun); | |
817 | ||
818 | case '[': | |
819 | return read_vector (readcharfun); | |
820 | ||
821 | case ')': | |
822 | case ']': | |
823 | case '.': | |
824 | { | |
825 | register Lisp_Object val; | |
826 | XSET (val, Lisp_Internal, c); | |
827 | return val; | |
828 | } | |
829 | ||
830 | case '#': | |
200f684e RS |
831 | c = READCHAR; |
832 | if (c == '[') | |
833 | { | |
834 | /* Accept compiled functions at read-time so that we don't have to | |
835 | build them using function calls. */ | |
836 | Lisp_Object tmp = read_vector (readcharfun); | |
837 | return Fmake_byte_code (XVECTOR(tmp)->size, XVECTOR (tmp)->contents); | |
838 | } | |
839 | UNREAD (c); | |
078e7b4a JB |
840 | return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil)); |
841 | ||
842 | case ';': | |
843 | while ((c = READCHAR) >= 0 && c != '\n'); | |
844 | goto retry; | |
845 | ||
846 | case '\'': | |
847 | { | |
848 | return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil)); | |
849 | } | |
850 | ||
851 | case '?': | |
852 | { | |
853 | register Lisp_Object val; | |
854 | ||
855 | c = READCHAR; | |
856 | if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
857 | ||
858 | if (c == '\\') | |
859 | XSET (val, Lisp_Int, read_escape (readcharfun)); | |
860 | else | |
861 | XSET (val, Lisp_Int, c); | |
862 | ||
863 | return val; | |
864 | } | |
865 | ||
866 | case '\"': | |
867 | { | |
868 | register char *p = read_buffer; | |
869 | register char *end = read_buffer + read_buffer_size; | |
870 | register int c; | |
871 | int cancel = 0; | |
872 | ||
873 | while ((c = READCHAR) >= 0 | |
874 | && c != '\"') | |
875 | { | |
876 | if (p == end) | |
877 | { | |
878 | char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
879 | p += new - read_buffer; | |
880 | read_buffer += new - read_buffer; | |
881 | end = read_buffer + read_buffer_size; | |
882 | } | |
883 | if (c == '\\') | |
884 | c = read_escape (readcharfun); | |
885 | /* c is -1 if \ newline has just been seen */ | |
886 | if (c < 0) | |
887 | { | |
888 | if (p == read_buffer) | |
889 | cancel = 1; | |
890 | } | |
891 | else | |
892 | *p++ = c; | |
893 | } | |
894 | if (c < 0) return Fsignal (Qend_of_file, Qnil); | |
895 | ||
896 | /* If purifying, and string starts with \ newline, | |
897 | return zero instead. This is for doc strings | |
08564963 | 898 | that we are really going to find in etc/DOC.nn.nn */ |
265a9e55 | 899 | if (!NILP (Vpurify_flag) && NILP (Vdoc_file_name) && cancel) |
078e7b4a JB |
900 | return make_number (0); |
901 | ||
902 | if (read_pure) | |
903 | return make_pure_string (read_buffer, p - read_buffer); | |
904 | else | |
905 | return make_string (read_buffer, p - read_buffer); | |
906 | } | |
907 | ||
908 | default: | |
909 | if (c <= 040) goto retry; | |
910 | { | |
911 | register char *p = read_buffer; | |
912 | ||
913 | { | |
914 | register char *end = read_buffer + read_buffer_size; | |
915 | ||
916 | while (c > 040 && | |
917 | !(c == '\"' || c == '\'' || c == ';' || c == '?' | |
918 | || c == '(' || c == ')' | |
919 | #ifndef LISP_FLOAT_TYPE /* we need to see <number><dot><number> */ | |
920 | || c =='.' | |
921 | #endif /* not LISP_FLOAT_TYPE */ | |
922 | || c == '[' || c == ']' || c == '#' | |
923 | )) | |
924 | { | |
925 | if (p == end) | |
926 | { | |
927 | register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
928 | p += new - read_buffer; | |
929 | read_buffer += new - read_buffer; | |
930 | end = read_buffer + read_buffer_size; | |
931 | } | |
932 | if (c == '\\') | |
933 | c = READCHAR; | |
934 | *p++ = c; | |
935 | c = READCHAR; | |
936 | } | |
937 | ||
938 | if (p == end) | |
939 | { | |
940 | char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2); | |
941 | p += new - read_buffer; | |
942 | read_buffer += new - read_buffer; | |
943 | /* end = read_buffer + read_buffer_size; */ | |
944 | } | |
945 | *p = 0; | |
946 | if (c >= 0) | |
947 | UNREAD (c); | |
948 | } | |
949 | ||
950 | /* Is it an integer? */ | |
951 | { | |
952 | register char *p1; | |
953 | register Lisp_Object val; | |
954 | p1 = read_buffer; | |
955 | if (*p1 == '+' || *p1 == '-') p1++; | |
956 | if (p1 != p) | |
957 | { | |
958 | while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++; | |
959 | if (p1 == p) | |
960 | /* It is. */ | |
961 | { | |
962 | XSET (val, Lisp_Int, atoi (read_buffer)); | |
963 | return val; | |
964 | } | |
965 | } | |
966 | #ifdef LISP_FLOAT_TYPE | |
967 | if (isfloat_string (read_buffer)) | |
968 | return make_float (atof (read_buffer)); | |
969 | #endif | |
970 | } | |
971 | ||
972 | return intern (read_buffer); | |
973 | } | |
974 | } | |
975 | } | |
976 | \f | |
977 | #ifdef LISP_FLOAT_TYPE | |
978 | ||
979 | #include <ctype.h> | |
980 | #define LEAD_INT 1 | |
981 | #define DOT_CHAR 2 | |
982 | #define TRAIL_INT 4 | |
983 | #define E_CHAR 8 | |
984 | #define EXP_INT 16 | |
985 | ||
986 | int | |
987 | isfloat_string (cp) | |
988 | register char *cp; | |
989 | { | |
990 | register state; | |
991 | ||
992 | state = 0; | |
993 | if (*cp == '+' || *cp == '-') | |
994 | cp++; | |
995 | ||
996 | if (isdigit(*cp)) | |
997 | { | |
998 | state |= LEAD_INT; | |
999 | while (isdigit (*cp)) | |
1000 | cp ++; | |
1001 | } | |
1002 | if (*cp == '.') | |
1003 | { | |
1004 | state |= DOT_CHAR; | |
1005 | cp++; | |
1006 | } | |
1007 | if (isdigit(*cp)) | |
1008 | { | |
1009 | state |= TRAIL_INT; | |
1010 | while (isdigit (*cp)) | |
1011 | cp++; | |
1012 | } | |
1013 | if (*cp == 'e') | |
1014 | { | |
1015 | state |= E_CHAR; | |
1016 | cp++; | |
1017 | } | |
1018 | if ((*cp == '+') || (*cp == '-')) | |
1019 | cp++; | |
1020 | ||
1021 | if (isdigit (*cp)) | |
1022 | { | |
1023 | state |= EXP_INT; | |
1024 | while (isdigit (*cp)) | |
1025 | cp++; | |
1026 | } | |
1027 | return (*cp == 0 | |
1028 | && (state == (LEAD_INT|DOT_CHAR|TRAIL_INT) | |
1029 | || state == (LEAD_INT|E_CHAR|EXP_INT) | |
1030 | || state == (LEAD_INT|DOT_CHAR|TRAIL_INT|E_CHAR|EXP_INT))); | |
1031 | } | |
1032 | #endif /* LISP_FLOAT_TYPE */ | |
1033 | \f | |
1034 | static Lisp_Object | |
1035 | read_vector (readcharfun) | |
1036 | Lisp_Object readcharfun; | |
1037 | { | |
1038 | register int i; | |
1039 | register int size; | |
1040 | register Lisp_Object *ptr; | |
1041 | register Lisp_Object tem, vector; | |
1042 | register struct Lisp_Cons *otem; | |
1043 | Lisp_Object len; | |
1044 | ||
1045 | tem = read_list (1, readcharfun); | |
1046 | len = Flength (tem); | |
1047 | vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil)); | |
1048 | ||
1049 | ||
1050 | size = XVECTOR (vector)->size; | |
1051 | ptr = XVECTOR (vector)->contents; | |
1052 | for (i = 0; i < size; i++) | |
1053 | { | |
1054 | ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem); | |
1055 | otem = XCONS (tem); | |
1056 | tem = Fcdr (tem); | |
1057 | free_cons (otem); | |
1058 | } | |
1059 | return vector; | |
1060 | } | |
1061 | ||
1062 | /* flag = 1 means check for ] to terminate rather than ) and . | |
1063 | flag = -1 means check for starting with defun | |
1064 | and make structure pure. */ | |
1065 | ||
1066 | static Lisp_Object | |
1067 | read_list (flag, readcharfun) | |
1068 | int flag; | |
1069 | register Lisp_Object readcharfun; | |
1070 | { | |
1071 | /* -1 means check next element for defun, | |
1072 | 0 means don't check, | |
1073 | 1 means already checked and found defun. */ | |
1074 | int defunflag = flag < 0 ? -1 : 0; | |
1075 | Lisp_Object val, tail; | |
1076 | register Lisp_Object elt, tem; | |
1077 | struct gcpro gcpro1, gcpro2; | |
1078 | ||
1079 | val = Qnil; | |
1080 | tail = Qnil; | |
1081 | ||
1082 | while (1) | |
1083 | { | |
1084 | GCPRO2 (val, tail); | |
1085 | elt = read1 (readcharfun); | |
1086 | UNGCPRO; | |
1087 | if (XTYPE (elt) == Lisp_Internal) | |
1088 | { | |
1089 | if (flag > 0) | |
1090 | { | |
1091 | if (XINT (elt) == ']') | |
1092 | return val; | |
1093 | return Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil)); | |
1094 | } | |
1095 | if (XINT (elt) == ')') | |
1096 | return val; | |
1097 | if (XINT (elt) == '.') | |
1098 | { | |
1099 | GCPRO2 (val, tail); | |
265a9e55 | 1100 | if (!NILP (tail)) |
078e7b4a JB |
1101 | XCONS (tail)->cdr = read0 (readcharfun); |
1102 | else | |
1103 | val = read0 (readcharfun); | |
1104 | elt = read1 (readcharfun); | |
1105 | UNGCPRO; | |
1106 | if (XTYPE (elt) == Lisp_Internal && XINT (elt) == ')') | |
1107 | return val; | |
1108 | return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil)); | |
1109 | } | |
1110 | return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil)); | |
1111 | } | |
1112 | tem = (read_pure && flag <= 0 | |
1113 | ? pure_cons (elt, Qnil) | |
1114 | : Fcons (elt, Qnil)); | |
265a9e55 | 1115 | if (!NILP (tail)) |
078e7b4a JB |
1116 | XCONS (tail)->cdr = tem; |
1117 | else | |
1118 | val = tem; | |
1119 | tail = tem; | |
1120 | if (defunflag < 0) | |
1121 | defunflag = EQ (elt, Qdefun); | |
1122 | else if (defunflag > 0) | |
1123 | read_pure = 1; | |
1124 | } | |
1125 | } | |
1126 | \f | |
1127 | Lisp_Object Vobarray; | |
1128 | Lisp_Object initial_obarray; | |
1129 | ||
1130 | Lisp_Object | |
1131 | check_obarray (obarray) | |
1132 | Lisp_Object obarray; | |
1133 | { | |
1134 | while (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0) | |
1135 | { | |
1136 | /* If Vobarray is now invalid, force it to be valid. */ | |
1137 | if (EQ (Vobarray, obarray)) Vobarray = initial_obarray; | |
1138 | ||
1139 | obarray = wrong_type_argument (Qvectorp, obarray); | |
1140 | } | |
1141 | return obarray; | |
1142 | } | |
1143 | ||
1144 | static int hash_string (); | |
1145 | Lisp_Object oblookup (); | |
1146 | ||
1147 | Lisp_Object | |
1148 | intern (str) | |
1149 | char *str; | |
1150 | { | |
1151 | Lisp_Object tem; | |
1152 | int len = strlen (str); | |
1153 | Lisp_Object obarray = Vobarray; | |
1154 | ||
1155 | if (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0) | |
1156 | obarray = check_obarray (obarray); | |
1157 | tem = oblookup (obarray, str, len); | |
1158 | if (XTYPE (tem) == Lisp_Symbol) | |
1159 | return tem; | |
265a9e55 | 1160 | return Fintern ((!NILP (Vpurify_flag) |
078e7b4a JB |
1161 | ? make_pure_string (str, len) |
1162 | : make_string (str, len)), | |
1163 | obarray); | |
1164 | } | |
1165 | ||
1166 | DEFUN ("intern", Fintern, Sintern, 1, 2, 0, | |
1167 | "Return the canonical symbol whose name is STRING.\n\ | |
1168 | If there is none, one is created by this function and returned.\n\ | |
1169 | A second optional argument specifies the obarray to use;\n\ | |
1170 | it defaults to the value of `obarray'.") | |
1171 | (str, obarray) | |
1172 | Lisp_Object str, obarray; | |
1173 | { | |
1174 | register Lisp_Object tem, sym, *ptr; | |
1175 | ||
265a9e55 | 1176 | if (NILP (obarray)) obarray = Vobarray; |
078e7b4a JB |
1177 | obarray = check_obarray (obarray); |
1178 | ||
1179 | CHECK_STRING (str, 0); | |
1180 | ||
1181 | tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size); | |
1182 | if (XTYPE (tem) != Lisp_Int) | |
1183 | return tem; | |
1184 | ||
265a9e55 | 1185 | if (!NILP (Vpurify_flag)) |
078e7b4a JB |
1186 | str = Fpurecopy (str); |
1187 | sym = Fmake_symbol (str); | |
1188 | ||
1189 | ptr = &XVECTOR (obarray)->contents[XINT (tem)]; | |
1190 | if (XTYPE (*ptr) == Lisp_Symbol) | |
1191 | XSYMBOL (sym)->next = XSYMBOL (*ptr); | |
1192 | else | |
1193 | XSYMBOL (sym)->next = 0; | |
1194 | *ptr = sym; | |
1195 | return sym; | |
1196 | } | |
1197 | ||
1198 | DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0, | |
1199 | "Return the canonical symbol whose name is STRING, or nil if none exists.\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; | |
1206 | ||
265a9e55 | 1207 | if (NILP (obarray)) obarray = Vobarray; |
078e7b4a JB |
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 | return Qnil; | |
1216 | } | |
1217 | ||
1218 | Lisp_Object | |
1219 | oblookup (obarray, ptr, size) | |
1220 | Lisp_Object obarray; | |
1221 | register char *ptr; | |
1222 | register int size; | |
1223 | { | |
1224 | int hash, obsize; | |
1225 | register Lisp_Object tail; | |
1226 | Lisp_Object bucket, tem; | |
1227 | ||
1228 | if (XTYPE (obarray) != Lisp_Vector || | |
1229 | (obsize = XVECTOR (obarray)->size) == 0) | |
1230 | { | |
1231 | obarray = check_obarray (obarray); | |
1232 | obsize = XVECTOR (obarray)->size; | |
1233 | } | |
1234 | /* Combining next two lines breaks VMS C 2.3. */ | |
1235 | hash = hash_string (ptr, size); | |
1236 | hash %= obsize; | |
1237 | bucket = XVECTOR (obarray)->contents[hash]; | |
1238 | if (XFASTINT (bucket) == 0) | |
1239 | ; | |
1240 | else if (XTYPE (bucket) != Lisp_Symbol) | |
1241 | error ("Bad data in guts of obarray"); /* Like CADR error message */ | |
1242 | else for (tail = bucket; ; XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next)) | |
1243 | { | |
1244 | if (XSYMBOL (tail)->name->size == size && | |
1245 | !bcmp (XSYMBOL (tail)->name->data, ptr, size)) | |
1246 | return tail; | |
1247 | else if (XSYMBOL (tail)->next == 0) | |
1248 | break; | |
1249 | } | |
1250 | XSET (tem, Lisp_Int, hash); | |
1251 | return tem; | |
1252 | } | |
1253 | ||
1254 | static int | |
1255 | hash_string (ptr, len) | |
1256 | unsigned char *ptr; | |
1257 | int len; | |
1258 | { | |
1259 | register unsigned char *p = ptr; | |
1260 | register unsigned char *end = p + len; | |
1261 | register unsigned char c; | |
1262 | register int hash = 0; | |
1263 | ||
1264 | while (p != end) | |
1265 | { | |
1266 | c = *p++; | |
1267 | if (c >= 0140) c -= 40; | |
1268 | hash = ((hash<<3) + (hash>>28) + c); | |
1269 | } | |
1270 | return hash & 07777777777; | |
1271 | } | |
1272 | ||
1273 | void | |
1274 | map_obarray (obarray, fn, arg) | |
1275 | Lisp_Object obarray; | |
1276 | int (*fn) (); | |
1277 | Lisp_Object arg; | |
1278 | { | |
1279 | register int i; | |
1280 | register Lisp_Object tail; | |
1281 | CHECK_VECTOR (obarray, 1); | |
1282 | for (i = XVECTOR (obarray)->size - 1; i >= 0; i--) | |
1283 | { | |
1284 | tail = XVECTOR (obarray)->contents[i]; | |
1285 | if (XFASTINT (tail) != 0) | |
1286 | while (1) | |
1287 | { | |
1288 | (*fn) (tail, arg); | |
1289 | if (XSYMBOL (tail)->next == 0) | |
1290 | break; | |
1291 | XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next); | |
1292 | } | |
1293 | } | |
1294 | } | |
1295 | ||
1296 | mapatoms_1 (sym, function) | |
1297 | Lisp_Object sym, function; | |
1298 | { | |
1299 | call1 (function, sym); | |
1300 | } | |
1301 | ||
1302 | DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0, | |
1303 | "Call FUNCTION on every symbol in OBARRAY.\n\ | |
1304 | OBARRAY defaults to the value of `obarray'.") | |
1305 | (function, obarray) | |
1306 | Lisp_Object function, obarray; | |
1307 | { | |
1308 | Lisp_Object tem; | |
1309 | ||
265a9e55 | 1310 | if (NILP (obarray)) obarray = Vobarray; |
078e7b4a JB |
1311 | obarray = check_obarray (obarray); |
1312 | ||
1313 | map_obarray (obarray, mapatoms_1, function); | |
1314 | return Qnil; | |
1315 | } | |
1316 | ||
1317 | #define OBARRAY_SIZE 509 | |
1318 | ||
1319 | void | |
1320 | init_obarray () | |
1321 | { | |
1322 | Lisp_Object oblength; | |
1323 | int hash; | |
1324 | Lisp_Object *tem; | |
1325 | ||
1326 | XFASTINT (oblength) = OBARRAY_SIZE; | |
1327 | ||
1328 | Qnil = Fmake_symbol (make_pure_string ("nil", 3)); | |
1329 | Vobarray = Fmake_vector (oblength, make_number (0)); | |
1330 | initial_obarray = Vobarray; | |
1331 | staticpro (&initial_obarray); | |
1332 | /* Intern nil in the obarray */ | |
1333 | /* These locals are to kludge around a pyramid compiler bug. */ | |
1334 | hash = hash_string ("nil", 3); | |
1335 | /* Separate statement here to avoid VAXC bug. */ | |
1336 | hash %= OBARRAY_SIZE; | |
1337 | tem = &XVECTOR (Vobarray)->contents[hash]; | |
1338 | *tem = Qnil; | |
1339 | ||
1340 | Qunbound = Fmake_symbol (make_pure_string ("unbound", 7)); | |
1341 | XSYMBOL (Qnil)->function = Qunbound; | |
1342 | XSYMBOL (Qunbound)->value = Qunbound; | |
1343 | XSYMBOL (Qunbound)->function = Qunbound; | |
1344 | ||
1345 | Qt = intern ("t"); | |
1346 | XSYMBOL (Qnil)->value = Qnil; | |
1347 | XSYMBOL (Qnil)->plist = Qnil; | |
1348 | XSYMBOL (Qt)->value = Qt; | |
1349 | ||
1350 | /* Qt is correct even if CANNOT_DUMP. loadup.el will set to nil at end. */ | |
1351 | Vpurify_flag = Qt; | |
1352 | ||
1353 | Qvariable_documentation = intern ("variable-documentation"); | |
1354 | ||
1355 | read_buffer_size = 100; | |
1356 | read_buffer = (char *) malloc (read_buffer_size); | |
1357 | } | |
1358 | \f | |
1359 | void | |
1360 | defsubr (sname) | |
1361 | struct Lisp_Subr *sname; | |
1362 | { | |
1363 | Lisp_Object sym; | |
1364 | sym = intern (sname->symbol_name); | |
1365 | XSET (XSYMBOL (sym)->function, Lisp_Subr, sname); | |
1366 | } | |
1367 | ||
1368 | #ifdef NOTDEF /* use fset in subr.el now */ | |
1369 | void | |
1370 | defalias (sname, string) | |
1371 | struct Lisp_Subr *sname; | |
1372 | char *string; | |
1373 | { | |
1374 | Lisp_Object sym; | |
1375 | sym = intern (string); | |
1376 | XSET (XSYMBOL (sym)->function, Lisp_Subr, sname); | |
1377 | } | |
1378 | #endif /* NOTDEF */ | |
1379 | ||
1380 | /* New replacement for DefIntVar; it ignores the doc string argument | |
1381 | on the assumption that make-docfile will handle that. */ | |
1382 | /* Define an "integer variable"; a symbol whose value is forwarded | |
1383 | to a C variable of type int. Sample call: */ | |
1384 | /* DEFVARINT ("indent-tabs-mode", &indent_tabs_mode, "Documentation"); */ | |
1385 | ||
1386 | void | |
1387 | defvar_int (namestring, address, doc) | |
1388 | char *namestring; | |
1389 | int *address; | |
1390 | char *doc; | |
1391 | { | |
1392 | Lisp_Object sym; | |
1393 | sym = intern (namestring); | |
1394 | XSET (XSYMBOL (sym)->value, Lisp_Intfwd, address); | |
1395 | } | |
1396 | ||
1397 | /* Similar but define a variable whose value is T if address contains 1, | |
1398 | NIL if address contains 0 */ | |
1399 | ||
1400 | void | |
1401 | defvar_bool (namestring, address, doc) | |
1402 | char *namestring; | |
1403 | int *address; | |
1404 | char *doc; | |
1405 | { | |
1406 | Lisp_Object sym; | |
1407 | sym = intern (namestring); | |
1408 | XSET (XSYMBOL (sym)->value, Lisp_Boolfwd, address); | |
1409 | } | |
1410 | ||
1411 | /* Similar but define a variable whose value is the Lisp Object stored at address. */ | |
1412 | ||
1413 | void | |
1414 | defvar_lisp (namestring, address, doc) | |
1415 | char *namestring; | |
1416 | Lisp_Object *address; | |
1417 | char *doc; | |
1418 | { | |
1419 | Lisp_Object sym; | |
1420 | sym = intern (namestring); | |
1421 | XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address); | |
1422 | staticpro (address); | |
1423 | } | |
1424 | ||
1425 | /* Similar but don't request gc-marking of the C variable. | |
1426 | Used when that variable will be gc-marked for some other reason, | |
1427 | since marking the same slot twice can cause trouble with strings. */ | |
1428 | ||
1429 | void | |
1430 | defvar_lisp_nopro (namestring, address, doc) | |
1431 | char *namestring; | |
1432 | Lisp_Object *address; | |
1433 | char *doc; | |
1434 | { | |
1435 | Lisp_Object sym; | |
1436 | sym = intern (namestring); | |
1437 | XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address); | |
1438 | } | |
1439 | ||
1440 | #ifndef standalone | |
1441 | ||
1442 | /* Similar but define a variable whose value is the Lisp Object stored in | |
1443 | the current buffer. address is the address of the slot in the buffer that is current now. */ | |
1444 | ||
1445 | void | |
1446 | defvar_per_buffer (namestring, address, doc) | |
1447 | char *namestring; | |
1448 | Lisp_Object *address; | |
1449 | char *doc; | |
1450 | { | |
1451 | Lisp_Object sym; | |
1452 | int offset; | |
1453 | extern struct buffer buffer_local_symbols; | |
1454 | ||
1455 | sym = intern (namestring); | |
1456 | offset = (char *)address - (char *)current_buffer; | |
1457 | ||
1458 | XSET (XSYMBOL (sym)->value, Lisp_Buffer_Objfwd, | |
1459 | (Lisp_Object *) offset); | |
1460 | *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym; | |
1461 | if (*(int *)(offset + (char *)&buffer_local_flags) == 0) | |
1462 | /* Did a DEFVAR_PER_BUFFER without initializing the corresponding | |
1463 | slot of buffer_local_flags */ | |
1464 | abort (); | |
1465 | } | |
1466 | ||
1467 | #endif /* standalone */ | |
1468 | \f | |
279499f0 | 1469 | init_lread () |
078e7b4a | 1470 | { |
46947372 | 1471 | char *normal; |
078e7b4a | 1472 | |
279499f0 | 1473 | /* Compute the default load-path. */ |
46947372 JB |
1474 | #ifdef CANNOT_DUMP |
1475 | normal = PATH_LOADSEARCH; | |
e065a56e | 1476 | Vload_path = decode_env_path (0, normal); |
46947372 JB |
1477 | #else |
1478 | if (NILP (Vpurify_flag)) | |
1479 | normal = PATH_LOADSEARCH; | |
279499f0 | 1480 | else |
46947372 | 1481 | normal = PATH_DUMPLOADSEARCH; |
279499f0 | 1482 | |
46947372 JB |
1483 | /* In a dumped Emacs, we normally have to reset the value of |
1484 | Vload_path from PATH_LOADSEARCH, since the value that was dumped | |
1485 | uses ../lisp, instead of the path of the installed elisp | |
1486 | libraries. However, if it appears that Vload_path was changed | |
1487 | from the default before dumping, don't override that value. */ | |
4746118a JB |
1488 | if (initialized) |
1489 | { | |
1490 | Lisp_Object dump_path; | |
46947372 | 1491 | |
e065a56e | 1492 | dump_path = decode_env_path (0, PATH_DUMPLOADSEARCH); |
4746118a | 1493 | if (! NILP (Fequal (dump_path, Vload_path))) |
e065a56e | 1494 | Vload_path = decode_env_path (0, normal); |
4746118a JB |
1495 | } |
1496 | else | |
e065a56e | 1497 | Vload_path = decode_env_path (0, normal); |
46947372 | 1498 | #endif |
279499f0 | 1499 | |
078e7b4a | 1500 | /* Warn if dirs in the *standard* path don't exist. */ |
46947372 JB |
1501 | { |
1502 | Lisp_Object path_tail; | |
078e7b4a | 1503 | |
46947372 JB |
1504 | for (path_tail = Vload_path; |
1505 | !NILP (path_tail); | |
1506 | path_tail = XCONS (path_tail)->cdr) | |
1507 | { | |
1508 | Lisp_Object dirfile; | |
1509 | dirfile = Fcar (path_tail); | |
1510 | if (XTYPE (dirfile) == Lisp_String) | |
1511 | { | |
1512 | dirfile = Fdirectory_file_name (dirfile); | |
1513 | if (access (XSTRING (dirfile)->data, 0) < 0) | |
1514 | printf ("Warning: lisp library (%s) does not exist.\n", | |
1515 | XSTRING (Fcar (path_tail))->data); | |
1516 | } | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | /* If the EMACSLOADPATH environment variable is set, use its value. | |
1521 | This doesn't apply if we're dumping. */ | |
1522 | if (NILP (Vpurify_flag) | |
1523 | && egetenv ("EMACSLOADPATH")) | |
279499f0 | 1524 | Vload_path = decode_env_path ("EMACSLOADPATH", normal); |
279499f0 JB |
1525 | |
1526 | Vvalues = Qnil; | |
1527 | ||
078e7b4a JB |
1528 | load_in_progress = 0; |
1529 | } | |
1530 | ||
1531 | void | |
279499f0 | 1532 | syms_of_lread () |
078e7b4a JB |
1533 | { |
1534 | defsubr (&Sread); | |
1535 | defsubr (&Sread_from_string); | |
1536 | defsubr (&Sintern); | |
1537 | defsubr (&Sintern_soft); | |
1538 | defsubr (&Sload); | |
228d4b1c | 1539 | defsubr (&Seval_buffer); |
078e7b4a JB |
1540 | defsubr (&Seval_region); |
1541 | defsubr (&Sread_char); | |
1542 | defsubr (&Sread_char_exclusive); | |
1543 | #ifdef HAVE_X_WINDOWS | |
1544 | defsubr (&Sread_event); | |
1545 | #endif /* HAVE_X_WINDOWS */ | |
1546 | defsubr (&Sget_file_char); | |
1547 | defsubr (&Smapatoms); | |
1548 | ||
1549 | DEFVAR_LISP ("obarray", &Vobarray, | |
1550 | "Symbol table for use by `intern' and `read'.\n\ | |
1551 | It is a vector whose length ought to be prime for best results.\n\ | |
1552 | The vector's contents don't make sense if examined from Lisp programs;\n\ | |
1553 | to find all the symbols in an obarray, use `mapatoms'."); | |
1554 | ||
1555 | DEFVAR_LISP ("values", &Vvalues, | |
1556 | "List of values of all expressions which were read, evaluated and printed.\n\ | |
1557 | Order is reverse chronological."); | |
1558 | ||
1559 | DEFVAR_LISP ("standard-input", &Vstandard_input, | |
1560 | "Stream for read to get input from.\n\ | |
1561 | See documentation of `read' for possible values."); | |
1562 | Vstandard_input = Qt; | |
1563 | ||
1564 | DEFVAR_LISP ("load-path", &Vload_path, | |
1565 | "*List of directories to search for files to load.\n\ | |
1566 | Each element is a string (directory name) or nil (try default directory).\n\ | |
1567 | Initialized based on EMACSLOADPATH environment variable, if any,\n\ | |
1568 | otherwise to default specified in by file `paths.h' when Emacs was built."); | |
1569 | ||
1570 | DEFVAR_BOOL ("load-in-progress", &load_in_progress, | |
1571 | "Non-nil iff inside of `load'."); | |
1572 | ||
1573 | DEFVAR_LISP ("after-load-alist", &Vafter_load_alist, | |
1574 | "An alist of expressions to be evalled when particular files are loaded.\n\ | |
1575 | Each element looks like (FILENAME FORMS...).\n\ | |
1576 | When `load' is run and the file-name argument is FILENAME,\n\ | |
1577 | the FORMS in the corresponding element are executed at the end of loading.\n\n\ | |
1578 | FILENAME must match exactly! Normally FILENAME is the name of a library,\n\ | |
1579 | with no directory specified, since that is how `load' is normally called.\n\ | |
1580 | An error in FORMS does not undo the load,\n\ | |
1581 | but does prevent execution of the rest of the FORMS."); | |
1582 | Vafter_load_alist = Qnil; | |
1583 | ||
1584 | Qstandard_input = intern ("standard-input"); | |
1585 | staticpro (&Qstandard_input); | |
1586 | ||
1587 | Qread_char = intern ("read-char"); | |
1588 | staticpro (&Qread_char); | |
1589 | ||
1590 | Qget_file_char = intern ("get-file-char"); | |
1591 | staticpro (&Qget_file_char); | |
1592 | } |