Add support for large files, 64-bit Solaris, system locale codings.
[bpt/emacs.git] / src / lread.c
index fce29ee..e08b4dd 100644 (file)
@@ -1,5 +1,5 @@
 /* Lisp parsing and input streams.
-   Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 1998
+   Copyright (C) 1985, 86, 87, 88, 89, 93, 94, 95, 97, 98, 1999
       Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -27,6 +27,7 @@ Boston, MA 02111-1307, USA.  */
 #include <sys/file.h>
 #include <errno.h>
 #include "lisp.h"
+#include "intervals.h"
 
 #ifndef standalone
 #include "buffer.h"
@@ -57,10 +58,6 @@ Boston, MA 02111-1307, USA.  */
 #endif
 
 #ifdef LISP_FLOAT_TYPE
-#ifdef STDC_HEADERS
-#include <stdlib.h>
-#endif
-
 #include <math.h>
 #endif /* LISP_FLOAT_TYPE */
 
@@ -72,6 +69,14 @@ Boston, MA 02111-1307, USA.  */
 #define O_RDONLY 0
 #endif
 
+#ifdef HAVE_FTELLO
+#define file_offset off_t
+#define file_tell ftello
+#else
+#define file_offset long
+#define file_tell ftell
+#endif
+
 extern int errno;
 
 Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list;
@@ -127,6 +132,9 @@ static int load_convert_to_unibyte;
    compiled) instead of readevalloop.  */
 Lisp_Object Vload_source_file_function;
 
+/* List of all DEFVAR_BOOL variables.  Used by the byte optimizer.  */
+Lisp_Object Vbyte_boolean_vars;
+
 /* List of descriptors now open for Fload.  */
 static Lisp_Object load_descriptor_list;
 
@@ -152,7 +160,7 @@ static int saved_doc_string_size;
 /* Length of actual data in saved_doc_string.  */
 static int saved_doc_string_length;
 /* This is the file position that string came from.  */
-static int saved_doc_string_position;
+static file_offset saved_doc_string_position;
 
 /* This contains the previous string skipped with #@.
    We copy it from saved_doc_string when a new string
@@ -163,7 +171,7 @@ static int prev_saved_doc_string_size;
 /* Length of actual data in prev_saved_doc_string.  */
 static int prev_saved_doc_string_length;
 /* This is the file position that string came from.  */
-static int prev_saved_doc_string_position;
+static file_offset prev_saved_doc_string_position;
 
 /* Nonzero means inside a new-style backquote
    with no surrounding parentheses.
@@ -187,7 +195,7 @@ readchar (readcharfun)
      Lisp_Object readcharfun;
 {
   Lisp_Object tem;
-  register int c, mpos;
+  register int c;
 
   if (BUFFERP (readcharfun))
     {
@@ -473,7 +481,7 @@ read_filtered_event (no_switch_frame, ascii_required, error_nonascii,
       /* Convert certain symbols to their ASCII equivalents.  */
       if (SYMBOLP (val))
        {
-         Lisp_Object tem, tem1, tem2;
+         Lisp_Object tem, tem1;
          tem = Fget (val, Qevent_symbol_element_mask);
          if (!NILP (tem))
            {
@@ -621,7 +629,7 @@ Return t if file exists.")
      since it would try to load a directory as a Lisp file */
   if (XSTRING (file)->size > 0)
     {
-      int size = XSTRING (file)->size;
+      int size = STRING_BYTES (XSTRING (file));
 
       GCPRO1 (file);
 
@@ -677,7 +685,7 @@ Return t if file exists.")
 
   /* Load .elc files directly, but not when they are
      remote and have no handler!  */
-  if (!bcmp (&(XSTRING (found)->data[XSTRING (found)->size - 4]),
+  if (!bcmp (&(XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 4]),
             ".elc", 4)
       && fd != 0)
     {
@@ -690,7 +698,7 @@ Return t if file exists.")
       fmode = "rb";
 #endif /* DOS_NT */
       stat ((char *)XSTRING (found)->data, &s1);
-      XSTRING (found)->data[XSTRING (found)->size - 1] = 0;
+      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)
        {
@@ -702,7 +710,7 @@ Return t if file exists.")
            message_with_string ("Source file `%s' newer than byte-compiled file",
                                 found, 1);
        }
-      XSTRING (found)->data[XSTRING (found)->size - 1] = 'c';
+      XSTRING (found)->data[STRING_BYTES (XSTRING (found)) - 1] = 'c';
     }
   else
     {
@@ -710,7 +718,7 @@ Return t if file exists.")
       if (!NILP (Vload_source_file_function))
        {
          if (fd != 0)
-           close (fd);
+           emacs_close (fd);
          return call4 (Vload_source_file_function, found, file,
                        NILP (noerror) ? Qnil : Qt,
                        NILP (nomessage) ? Qnil : Qt);
@@ -718,14 +726,14 @@ Return t if file exists.")
     }
 
 #ifdef WINDOWSNT
-  close (fd);
+  emacs_close (fd);
   stream = fopen ((char *) XSTRING (found)->data, fmode);
 #else  /* not WINDOWSNT */
   stream = fdopen (fd, fmode);
 #endif /* not WINDOWSNT */
   if (stream == 0)
     {
-      close (fd);
+      emacs_close (fd);
       error ("Failure to create stdio stream for %s", XSTRING (file)->data);
     }
 
@@ -745,8 +753,8 @@ Return t if file exists.")
 
   GCPRO1 (file);
   lispstream = Fcons (Qnil, Qnil);
-  XSETFASTINT (XCONS (lispstream)->car, (EMACS_UINT)stream >> 16);
-  XSETFASTINT (XCONS (lispstream)->cdr, (EMACS_UINT)stream & 0xffff);
+  XSETFASTINT (XCAR (lispstream), (EMACS_UINT)stream >> 16);
+  XSETFASTINT (XCDR (lispstream), (EMACS_UINT)stream & 0xffff);
   record_unwind_protect (load_unwind, lispstream);
   record_unwind_protect (load_descriptor_unwind, load_descriptor_list);
   specbind (Qload_file_name, found);
@@ -790,8 +798,8 @@ static Lisp_Object
 load_unwind (stream)  /* used as unwind-protect function in load */
      Lisp_Object stream;
 {
-  fclose ((FILE *) (XFASTINT (XCONS (stream)->car) << 16
-                   | XFASTINT (XCONS (stream)->cdr)));
+  fclose ((FILE *) (XFASTINT (XCAR (stream)) << 16
+                   | XFASTINT (XCDR (stream))));
   if (--load_in_progress < 0) load_in_progress = 0;
   return Qnil;
 }
@@ -812,8 +820,8 @@ close_load_descs ()
 {
 #ifndef WINDOWSNT
   Lisp_Object tail;
-  for (tail = load_descriptor_list; !NILP (tail); tail = XCONS (tail)->cdr)
-    close (XFASTINT (XCONS (tail)->car));
+  for (tail = load_descriptor_list; !NILP (tail); tail = XCDR (tail))
+    emacs_close (XFASTINT (XCAR (tail)));
 #endif
 }
 \f
@@ -893,7 +901,7 @@ openp (path, str, suffix, storeptr, exec_only)
 
       /* Calculate maximum size of any filename made from
         this path element/specified file name and any possible suffix.  */
-      want_size = strlen (suffix) + XSTRING (filename)->size + 1;
+      want_size = strlen (suffix) + STRING_BYTES (XSTRING (filename)) + 1;
       if (fn_size < want_size)
        fn = (char *) alloca (fn_size = 100 + want_size);
 
@@ -913,13 +921,14 @@ openp (path, str, suffix, storeptr, exec_only)
              && XSTRING (filename)->data[1] == ':')
            {
              strncpy (fn, XSTRING (filename)->data + 2,
-                      XSTRING (filename)->size - 2);
-             fn[XSTRING (filename)->size - 2] = 0;
+                      STRING_BYTES (XSTRING (filename)) - 2);
+             fn[STRING_BYTES (XSTRING (filename)) - 2] = 0;
            }
          else
            {
-             strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size);
-             fn[XSTRING (filename)->size] = 0;
+             strncpy (fn, XSTRING (filename)->data,
+                      STRING_BYTES (XSTRING (filename)));
+             fn[STRING_BYTES (XSTRING (filename))] = 0;
            }
 
          if (lsuffix != 0)  /* Bug happens on CCI if lsuffix is 0.  */
@@ -961,7 +970,7 @@ openp (path, str, suffix, storeptr, exec_only)
                  if (exec_only)
                    fd = (access (fn, X_OK) == 0) ? 1 : -1;
                  else
-                   fd = open (fn, O_RDONLY, 0);
+                   fd = emacs_open (fn, O_RDONLY, 0);
 
                  if (fd >= 0)
                    {
@@ -1492,8 +1501,10 @@ read_escape (readcharfun, stringp)
       c = READCHAR;
       if (c == '\\')
        c = read_escape (readcharfun, 0);
-      if ((c & 0177) == '?')
-       return 0177 | c;
+      if ((c & ~CHAR_MODIFIER_MASK) == '?')
+       return 0177 | (c & CHAR_MODIFIER_MASK);
+      else if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
+       return c | ctrl_modifier;
       /* ASCII control chars are made from letters (both cases),
         as well as the non-letters within 0100...0137.  */
       else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
@@ -1744,7 +1755,7 @@ read1 (readcharfun, pch, first_in_list)
              {
                char *temp = saved_doc_string;
                int temp_size = saved_doc_string_size;
-               int temp_pos = saved_doc_string_position;
+               file_offset temp_pos = saved_doc_string_position;
                int temp_len = saved_doc_string_length;
 
                saved_doc_string = prev_saved_doc_string;
@@ -1770,7 +1781,7 @@ read1 (readcharfun, pch, first_in_list)
                                                        saved_doc_string_size);
                }
 
-             saved_doc_string_position = ftell (instream);
+             saved_doc_string_position = file_tell (instream);
 
              /* Copy that many characters into saved_doc_string.  */
              for (i = 0; i < nskip && c >= 0; i++)
@@ -1897,8 +1908,6 @@ read1 (readcharfun, pch, first_in_list)
 
     case '?':
       {
-       register Lisp_Object val;
-
        c = READCHAR;
        if (c < 0) return Fsignal (Qend_of_file, Qnil);
 
@@ -1949,17 +1958,20 @@ read1 (readcharfun, pch, first_in_list)
 
                /* If an escape specifies a non-ASCII single-byte character,
                   this must be a unibyte string.  */
-               if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_META))
-                   && ! ASCII_BYTE_P (c))
+               if (SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))
+                   && ! ASCII_BYTE_P ((c & ~CHAR_MODIFIER_MASK)))
                  force_singlebyte = 1;
              }
 
-           if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_META)))
+           if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
              {
                unsigned char workbuf[4];
                unsigned char *str = workbuf;
                int length;
 
+               /* Any modifiers for a multibyte character are invalid.  */
+               if (c & CHAR_MODIFIER_MASK)
+                 error ("Invalid modifier in string");
                length = non_ascii_char_to_string (c, workbuf, &str);
                if (length > 1)
                  force_multibyte = 1;
@@ -1975,6 +1987,15 @@ read1 (readcharfun, pch, first_in_list)
                else if (c == (CHAR_CTL | '?'))
                  c = 127;
 
+               if (c & CHAR_SHIFT)
+                 {
+                   /* Shift modifier is valid only with [A-Za-z].  */
+                   if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
+                     c &= ~CHAR_SHIFT;
+                   else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
+                     c = (c & ~CHAR_SHIFT) - ('a' - 'A');
+                 }
+
                if (c & CHAR_META)
                  /* Move the meta bit to the right place for a string.  */
                  c = (c & ~CHAR_META) | 0x80;
@@ -2437,8 +2458,8 @@ read_vector (readcharfun, bytecodeflag)
                    error ("invalid byte code");
 
                  otem = XCONS (item);
-                 bytestr = XCONS (item)->car;
-                 item = XCONS (item)->cdr;
+                 bytestr = XCAR (item);
+                 item = XCDR (item);
                  free_cons (otem);
                }
 
@@ -2530,7 +2551,7 @@ read_list (flag, readcharfun)
            {
              GCPRO2 (val, tail);
              if (!NILP (tail))
-               XCONS (tail)->cdr = read0 (readcharfun);
+               XCDR (tail) = read0 (readcharfun);
              else
                val = read0 (readcharfun);
              read1 (readcharfun, &ch, 0);
@@ -2543,7 +2564,7 @@ read_list (flag, readcharfun)
                    {
                      /* Get a doc string from the file we are loading.
                         If it's in saved_doc_string, get it from there.  */
-                     int pos = XINT (XCONS (val)->cdr);
+                     int pos = XINT (XCDR (val));
                      /* Position is negative for user variables.  */
                      if (pos < 0) pos = -pos;
                      if (pos >= saved_doc_string_position
@@ -2623,7 +2644,7 @@ read_list (flag, readcharfun)
             ? pure_cons (elt, Qnil)
             : Fcons (elt, Qnil));
       if (!NILP (tail))
-       XCONS (tail)->cdr = tem;
+       XCDR (tail) = tem;
       else
        val = tem;
       tail = tem;
@@ -2921,8 +2942,6 @@ OBARRAY defaults to the value of `obarray'.")
   (function, obarray)
      Lisp_Object function, obarray;
 {
-  Lisp_Object tem;
-
   if (NILP (obarray)) obarray = Vobarray;
   obarray = check_obarray (obarray);
 
@@ -3024,6 +3043,7 @@ defvar_bool (namestring, address)
   XMISCTYPE (val) = Lisp_Misc_Boolfwd;
   XBOOLFWD (val)->boolvar = address;
   XSYMBOL (sym)->value = val;
+  Vbyte_boolean_vars = Fcons (sym, Vbyte_boolean_vars);
 }
 
 /* Similar but define a variable whose value is the Lisp Object stored
@@ -3113,11 +3133,6 @@ init_lread ()
   char *normal;
   int turn_off_warning = 0;
 
-#ifdef HAVE_SETLOCALE
-  /* Make sure numbers are parsed as we expect.  */
-  setlocale (LC_NUMERIC, "C");
-#endif /* HAVE_SETLOCALE */
-
   /* Compute the default load-path.  */
 #ifdef CANNOT_DUMP
   normal = PATH_LOADSEARCH;
@@ -3247,7 +3262,7 @@ init_lread ()
 
       for (path_tail = Vload_path;
           !NILP (path_tail);
-          path_tail = XCONS (path_tail)->cdr)
+          path_tail = XCDR (path_tail))
        {
          Lisp_Object dirfile;
          dirfile = Fcar (path_tail);
@@ -3256,7 +3271,7 @@ init_lread ()
              dirfile = Fdirectory_file_name (dirfile);
              if (access (XSTRING (dirfile)->data, 0) < 0)
                dir_warning ("Warning: Lisp directory `%s' does not exist.\n",
-                            XCONS (path_tail)->car);
+                            XCAR (path_tail));
            }
        }
     }
@@ -3407,6 +3422,10 @@ You cannot count on them to still be there!");
      "List of files that were preloaded (when dumping Emacs).");
   Vpreloaded_file_list = Qnil;
 
+  DEFVAR_LISP ("byte-boolean-vars", &Vbyte_boolean_vars,
+     "List of all DEFVAR_BOOL variables, used by the byte code optimizer.");
+  Vbyte_boolean_vars = Qnil;
+
   /* Vsource_directory was initialized in init_lread.  */
 
   load_descriptor_list = Qnil;