/* Lisp parsing and input streams.
- Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 98, 1999
+ Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 98, 99, 2000, 2001
Free Software Foundation, Inc.
This file is part of GNU Emacs.
#include <config.h>
-/* The following feature selections should be in config.h, but that
- causes at best a host of warnings on some systems. */
-#undef _XOPEN_SOURCE /* Avoid warnings about redefinition
- in some cases. */
-#define _XOPEN_SOURCE 500 /* for Unix 98 ftello on GNU */
-#undef __EXTENSIONS__
-#define __EXTENSIONS__ /* Keep Solaris 2.6 happy with the
- above, else things we need are hidden. */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define O_RDONLY 0
#endif
-#ifdef HAVE_FTELLO
+#ifdef HAVE_FSEEKO
#define file_offset off_t
#define file_tell ftello
#else
#define file_tell ftell
#endif
+#ifndef USE_CRT_DLL
extern int errno;
+#endif
Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist;
Fread initializes this to zero, so we need not specbind it
or worry about what happens to it when there is an error. */
static int new_backquote_flag;
+
+/* A list of file names for files being loaded in Fload. Used to
+ check for recursive loads. */
+
+static Lisp_Object Vloads_in_progress;
+
+/* Limit of the depth of recursive loads. */
+
+Lisp_Object Vrecursive_load_depth_limit;
+
+/* Non-zero means load dangerous compiled Lisp files. */
+
+int load_dangerous_libraries;
+
+/* A regular expression used to detect files compiled with Emacs. */
+
+static Lisp_Object Vbytecomp_version_regexp;
+
+static void readevalloop P_ ((Lisp_Object, FILE*, Lisp_Object,
+ Lisp_Object (*) (), int,
+ Lisp_Object, Lisp_Object));
+static Lisp_Object load_unwind P_ ((Lisp_Object));
+static Lisp_Object load_descriptor_unwind P_ ((Lisp_Object));
+
\f
/* Handle unreading and rereading of characters.
Write READCHAR to read a character,
{
if (read_from_string_index >= read_from_string_limit)
c = -1;
- else if (STRING_MULTIBYTE (readcharfun))
+ else
FETCH_STRING_CHAR_ADVANCE (c, readcharfun,
read_from_string_index,
read_from_string_index_byte);
- else
- c = XSTRING (readcharfun)->data[read_from_string_index++];
return c;
}
{
register Lisp_Object val, delayed_switch_frame;
+#ifdef HAVE_WINDOW_SYSTEM
+ if (display_hourglass_p)
+ cancel_hourglass ();
+#endif
+
delayed_switch_frame = Qnil;
/* Read until we get an acceptable event. */
if (! NILP (delayed_switch_frame))
unread_switch_frame = delayed_switch_frame;
+#ifdef HAVE_WINDOW_SYSTEM
+ if (display_hourglass_p)
+ start_hourglass ();
+#endif
return val;
}
XSETINT (val, getc (instream));
return val;
}
-\f
-static void readevalloop ();
-static Lisp_Object load_unwind ();
-static Lisp_Object load_descriptor_unwind ();
-
-/* Non-zero means load dangerous compiled Lisp files. */
-
-int load_dangerous_libraries;
-
-/* A regular expression used to detect files compiled with Emacs. */
-
-static Lisp_Object Vbytecomp_version_regexp;
+\f
/* Value is non-zero if the file asswociated with file descriptor FD
is a compiled Lisp file that's safe to load. Only files compiled
with Emacs are safe to load. Files compiled with XEmacs can lead
}
+/* Callback for record_unwind_protect. Restore the old load list OLD,
+ after loading a file successfully. */
+
+static Lisp_Object
+record_load_unwind (old)
+ Lisp_Object old;
+{
+ return Vloads_in_progress = old;
+}
+
+
DEFUN ("load", Fload, Sload, 1, 5, 0,
"Execute a file of Lisp code named FILE.\n\
First try FILE with `.elc' appended, then try with `.el',\n\
/* 1 means we are loading a compiled file. */
int compiled = 0;
Lisp_Object handler;
+ int safe_p = 1;
char *fmode = "r";
#ifdef DOS_NT
fmode = "rt";
#endif /* DOS_NT */
- int safe_p = 1;
CHECK_STRING (file, 0);
UNGCPRO;
}
- if (fd < 0)
+ if (fd == -1)
{
if (NILP (noerror))
while (1)
return Qnil;
}
+ /* Tell startup.el whether or not we found the user's init file. */
if (EQ (Qt, Vuser_init_file))
Vuser_init_file = found;
- /* If FD is 0, that means openp found a magic file. */
- if (fd == 0)
+ /* If FD is -2, that means openp found a magic file. */
+ if (fd == -2)
{
if (NILP (Fequal (found, file)))
/* If FOUND is a different file name from FILE,
return call5 (handler, Qload, found, noerror, nomessage, Qt);
}
- /* Load .elc files directly, but not when they are
- remote and have no handler! */
- if (!bcmp (&(XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 4]),
- ".elc", 4)
- && fd != 0)
+ /* Check if we're stuck in a recursive load cycle.
+
+ 2000-09-21: It's not possible to just check for the file loaded
+ being a member of Vloads_in_progress. This fails because of the
+ way the byte compiler currently works; `provide's are not
+ evaluted, see font-lock.el/jit-lock.el as an example. This
+ leads to a certain amount of ``normal'' recursion.
+
+ Also, just loading a file recursively is not always an error in
+ the general case; the second load may do something different. */
+ if (INTEGERP (Vrecursive_load_depth_limit)
+ && XINT (Vrecursive_load_depth_limit) > 0)
{
- struct stat s1, s2;
- int result;
+ Lisp_Object len = Flength (Vloads_in_progress);
+ if (XFASTINT (len) > XFASTINT (Vrecursive_load_depth_limit))
+ Fsignal (Qerror, Fcons (build_string ("Recursive load suspected"),
+ Fcons (found, Vloads_in_progress)));
+ record_unwind_protect (record_load_unwind, Vloads_in_progress);
+ Vloads_in_progress = Fcons (found, Vloads_in_progress);
+ }
- if (!safe_to_load_p (fd))
+ if (!bcmp (&(XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 4]),
+ ".elc", 4))
+ /* Load .elc files directly, but not when they are
+ remote and have no handler! */
+ {
+ if (fd != -2)
{
- safe_p = 0;
- if (!load_dangerous_libraries)
- error ("File `%s' was not compiled in Emacs",
- XSTRING (found)->data);
- else if (!NILP (nomessage))
- message_with_string ("File `%s' not compiled in Emacs", found, 1);
- }
+ struct stat s1, s2;
+ int result;
- compiled = 1;
+ if (!safe_to_load_p (fd))
+ {
+ safe_p = 0;
+ if (!load_dangerous_libraries)
+ error ("File `%s' was not compiled in Emacs",
+ XSTRING (found)->data);
+ else if (!NILP (nomessage))
+ message_with_string ("File `%s' not compiled in Emacs", found, 1);
+ }
+
+ compiled = 1;
#ifdef DOS_NT
- fmode = "rb";
+ fmode = "rb";
#endif /* DOS_NT */
- stat ((char *)XSTRING (found)->data, &s1);
- XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 0;
- result = stat ((char *)XSTRING (found)->data, &s2);
- if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
- {
- /* Make the progress messages mention that source is newer. */
- newer = 1;
+ stat ((char *)XSTRING (found)->data, &s1);
+ XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 0;
+ result = stat ((char *)XSTRING (found)->data, &s2);
+ if (result >= 0 && (unsigned) s1.st_mtime < (unsigned) s2.st_mtime)
+ {
+ /* Make the progress messages mention that source is newer. */
+ newer = 1;
- /* If we won't print another message, mention this anyway. */
- if (! NILP (nomessage))
- message_with_string ("Source file `%s' newer than byte-compiled file",
- found, 1);
+ /* If we won't print another message, mention this anyway. */
+ if (! NILP (nomessage))
+ message_with_string ("Source file `%s' newer than byte-compiled file",
+ found, 1);
+ }
+ XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 'c';
}
- XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 'c';
}
else
{
- load_source:
-
/* We are loading a source file (*.el). */
if (!NILP (Vload_source_file_function))
{
- if (fd != 0)
+ Lisp_Object val;
+
+ if (fd >= 0)
emacs_close (fd);
- return call4 (Vload_source_file_function, found, file,
- NILP (noerror) ? Qnil : Qt,
- NILP (nomessage) ? Qnil : Qt);
+ val = call4 (Vload_source_file_function, found, file,
+ NILP (noerror) ? Qnil : Qt,
+ NILP (nomessage) ? Qnil : Qt);
+ return unbind_to (count, val);
}
}
else /* The typical case; compiled file newer than source file. */
message_with_string ("Loading %s...done", file, 1);
}
+
return Qt;
}
the file actually found should be stored as a Lisp string.
nil is stored there on failure.
- If the file we find is remote, return 0
+ If the file we find is remote, return -2
but store the found remote file name in *STOREPTR.
We do not check for remote files if EXEC_ONLY is nonzero. */
int want_size;
Lisp_Object filename;
struct stat st;
- struct gcpro gcpro1;
+ struct gcpro gcpro1, gcpro2, gcpro3;
+ Lisp_Object string;
- GCPRO1 (str);
+ string = filename = Qnil;
+ GCPRO3 (str, string, filename);
+
if (storeptr)
*storeptr = Qnil;
handler = Ffind_file_name_handler (filename, Qfile_exists_p);
if (! NILP (handler) && ! exec_only)
{
- Lisp_Object string;
int exists;
string = build_string (fn);
if (storeptr)
*storeptr = build_string (fn);
UNGCPRO;
- return 0;
+ return -2;
}
}
else
}
Lisp_Object
-unreadpure () /* Used as unwind-protect function in readevalloop */
+unreadpure (junk) /* Used as unwind-protect function in readevalloop */
+ Lisp_Object junk;
{
read_pure = 0;
return Qnil;
return Qnil;
}
+/* Signal an `end-of-file' error, if possible with file name
+ information. */
+
+static void
+end_of_file_error ()
+{
+ Lisp_Object data;
+
+ if (STRINGP (Vload_file_name))
+ data = Fcons (Vload_file_name, Qnil);
+ else
+ data = Qnil;
+
+ Fsignal (Qend_of_file, data);
+}
+
/* UNIBYTE specifies how to set load_convert_to_unibyte
for this invocation.
READFUN, if non-nil, is used instead of `read'. */
int count = specpdl_ptr - specpdl;
struct gcpro gcpro1;
struct buffer *b = 0;
+ int continue_reading_p;
if (BUFFERP (readcharfun))
b = XBUFFER (readcharfun);
LOADHIST_ATTACH (sourcename);
- while (1)
+ continue_reading_p = 1;
+ while (continue_reading_p)
{
if (b != 0 && NILP (b->name))
error ("Reading from killed buffer");
{
UNREAD (c);
read_objects = Qnil;
- if (! NILP (readfun))
- val = call1 (readfun, readcharfun);
+ if (!NILP (readfun))
+ {
+ val = call1 (readfun, readcharfun);
+
+ /* If READCHARFUN has set point to ZV, we should
+ stop reading, even if the form read sets point
+ to a different value when evaluated. */
+ if (BUFFERP (readcharfun))
+ {
+ struct buffer *b = XBUFFER (readcharfun);
+ if (BUF_PT (b) == BUF_ZV (b))
+ continue_reading_p = 0;
+ }
+ }
else if (! NILP (Vload_read_function))
val = call1 (Vload_read_function, readcharfun);
else
}
val = (*evalfun) (val);
+
if (printflag)
{
Vvalues = Fcons (val, Vvalues);
a function (call it with no arguments for each character,\n\
call it with a char as argument to push a char back)\n\
a string (takes text from string, starting at the beginning)\n\
- t (read text line using minibuffer and use it).")
+ t (read text line using minibuffer and use it, or read from\n\
+ standard input in batch mode).")
(stream)
Lisp_Object stream;
{
characters. */
unsigned char str[MAX_MULTIBYTE_LENGTH];
int len = 0;
+ int bytes;
str[len++] = c;
while ((c = READCHAR) >= 0xA0
&& len < MAX_MULTIBYTE_LENGTH)
str[len++] = c;
UNREAD (c);
- return STRING_CHAR (str, len);
+ if (UNIBYTE_STR_AS_MULTIBYTE_P (str, len, bytes))
+ return STRING_CHAR (str, len);
+ /* The byte sequence is not valid as multibyte. Unread all bytes
+ but the first one, and return the first byte. */
+ while (--len > 0)
+ UNREAD (str[len]);
+ return str[0];
}
/* Read a \-escape sequence, assuming we already read the `\'. */
Lisp_Object readcharfun;
int radix;
{
- int number, ndigits, invalid_p, c, sign;
+ int ndigits = 0, invalid_p, c, sign = 0;
+ EMACS_INT number = 0;
if (radix < 2 || radix > 36)
invalid_p = 1;
retry:
c = READCHAR;
- if (c < 0) return Fsignal (Qend_of_file, Qnil);
+ if (c < 0)
+ end_of_file_error ();
switch (c)
{
Lisp_Object beg, end, plist;
beg = read1 (readcharfun, &ch, 0);
+ end = plist = Qnil;
if (ch == ')')
break;
if (ch == 0)
case '?':
{
c = READCHAR;
- if (c < 0) return Fsignal (Qend_of_file, Qnil);
+ if (c < 0)
+ end_of_file_error ();
if (c == '\\')
c = read_escape (readcharfun, 0);
{
if (end - p < MAX_MULTIBYTE_LENGTH)
{
- char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
- p += new - read_buffer;
- read_buffer += new - read_buffer;
+ int offset = p - read_buffer;
+ read_buffer = (char *) xrealloc (read_buffer,
+ read_buffer_size *= 2);
+ p = read_buffer + offset;
end = read_buffer + read_buffer_size;
}
}
}
if (c < 0)
- return Fsignal (Qend_of_file, Qnil);
+ end_of_file_error ();
/* If purifying, and string starts with \ newline,
return zero instead. This is for doc strings
return make_number (0);
if (force_multibyte)
- nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
+ p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
+ p - read_buffer, &nchars);
else if (force_singlebyte)
nchars = p - read_buffer;
else if (load_convert_to_unibyte)
{
Lisp_Object string;
- nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
+ p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
+ p - read_buffer, &nchars);
if (p - read_buffer != nchars)
{
string = make_multibyte_string (read_buffer, nchars,
}
else if (EQ (readcharfun, Qget_file_char)
|| EQ (readcharfun, Qlambda))
- /* Nowadays, reading directly from a file
- is used only for compiled Emacs Lisp files,
- and those always use the Emacs internal encoding.
- Meanwhile, Qlambda is used for reading dynamic byte code
- (compiled with byte-compile-dynamic = t). */
- nchars = multibyte_chars_in_text (read_buffer, p - read_buffer);
+ /* Nowadays, reading directly from a file is used only for
+ compiled Emacs Lisp files, and those always use the
+ Emacs internal encoding. Meanwhile, Qlambda is used
+ for reading dynamic byte code (compiled with
+ byte-compile-dynamic = t). */
+ p = read_buffer + str_as_multibyte (read_buffer, end - read_buffer,
+ p - read_buffer, &nchars);
else
/* In all other cases, if we read these bytes as
separate characters, treat them as separate characters now. */
case '.':
{
- /* If a period is followed by a number, then we should read it
- as a floating point number. Otherwise, it denotes a dotted
- pair. */
int next_char = READCHAR;
UNREAD (next_char);
- if (! (next_char >= '0' && next_char <= '9'))
+ if (next_char <= 040
+ || index ("\"'`,(", next_char))
{
*pch = c;
return Qnil;
default_label:
if (c <= 040) goto retry;
{
- register char *p = read_buffer;
+ char *p = read_buffer;
int quoted = 0;
{
- register char *end = read_buffer + read_buffer_size;
+ char *end = read_buffer + read_buffer_size;
while (c > 040
- && !(c == '\"' || c == '\'' || c == ';' || c == '?'
+ && !(c == '\"' || c == '\'' || c == ';'
|| c == '(' || c == ')'
- || c == '[' || c == ']' || c == '#'
- ))
+ || c == '[' || c == ']' || c == '#'))
{
if (end - p < MAX_MULTIBYTE_LENGTH)
{
- register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
- p += new - read_buffer;
- read_buffer += new - read_buffer;
+ int offset = p - read_buffer;
+ read_buffer = (char *) xrealloc (read_buffer,
+ read_buffer_size *= 2);
+ p = read_buffer + offset;
end = read_buffer + read_buffer_size;
}
+
if (c == '\\')
{
c = READCHAR;
+ if (c == -1)
+ end_of_file_error ();
quoted = 1;
}
if (p == end)
{
- char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
- p += new - read_buffer;
- read_buffer += new - read_buffer;
-/* end = read_buffer + read_buffer_size; */
+ int offset = p - read_buffer;
+ read_buffer = (char *) xrealloc (read_buffer,
+ read_buffer_size *= 2);
+ p = read_buffer + offset;
+ end = read_buffer + read_buffer_size;
}
*p = 0;
if (c >= 0)
case Lisp_Vectorlike:
{
int i;
- int length = Flength(subtree);
+ int length = XINT (Flength(subtree));
for (i = 0; i < length; i++)
{
Lisp_Object idx = make_number (i);
XMISCTYPE (val) = Lisp_Misc_Buffer_Objfwd;
XBUFFER_OBJFWD (val)->offset = offset;
XSYMBOL (sym)->value = val;
- BUFFER_LOCAL_SYMBOL (offset) = sym;
- BUFFER_LOCAL_TYPE (offset) = type;
+ PER_BUFFER_SYMBOL (offset) = sym;
+ PER_BUFFER_TYPE (offset) = type;
- if (BUFFER_LOCAL_IDX (offset) == 0)
+ if (PER_BUFFER_IDX (offset) == 0)
/* Did a DEFVAR_PER_BUFFER without initializing the corresponding
slot of buffer_local_flags */
abort ();
load_descriptor_list = Qnil;
Vstandard_input = Qt;
+ Vloads_in_progress = Qnil;
}
/* Print a warning, using format string FORMAT, that directory DIRNAME
them.");
load_dangerous_libraries = 0;
- Vbytecomp_version_regexp = build_string ("^;;;.in Emacs version");
- staticpro (&Vbytecomp_version_regexp);
+ DEFVAR_LISP ("bytecomp-version-regexp", &Vbytecomp_version_regexp,
+ "Regular expression matching safe to load compiled Lisp files.\n\
+When Emacs loads a compiled Lisp file, it reads the first 512 bytes\n\
+from the file, and matches them against this regular expression.\n\
+When the regular expression matches, the file is considered to be safe\n\
+to load. See also `load-dangerous-libraries'.");
+ Vbytecomp_version_regexp
+ = build_string ("^;;;.\\(in Emacs version\\|bytecomp version FSF\\)");
+
+ DEFVAR_LISP ("recursive-load-depth-limit", &Vrecursive_load_depth_limit,
+ "Limit for depth of recursive loads.\n\
+Value should be either an integer > 0 specifying the limit, or nil for\n\
+no limit.");
+ Vrecursive_load_depth_limit = make_number (10);
/* Vsource_directory was initialized in init_lread. */
read_objects = Qnil;
staticpro (&seen_list);
+ Vloads_in_progress = Qnil;
+ staticpro (&Vloads_in_progress);
}