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