deprecated eval-when situations
[bpt/guile.git] / libguile / read.c
index 980769b..ecf27ff 100644 (file)
@@ -1,5 +1,5 @@
-/* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2003, 2004, 2006,
- *   2007, 2008, 2009, 2010, 2011, 2012, 2014 Free Software Foundation, Inc.
+/* Copyright (C) 1995-1997, 1999-2001, 2003, 2004, 2006-2012, 2014
+ *   Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -411,6 +411,11 @@ static SCM
 maybe_annotate_source (SCM x, SCM port, scm_t_read_opts *opts,
                        long line, int column)
 {
+  /* This condition can be caused by a user calling
+     set-port-column!.  */
+  if (line < 0 || column < 0)
+    return x;
+
   if (opts->record_positions_p)
     scm_i_set_source_properties_x (x, line, column, SCM_FILENAME (port));
   return x;
@@ -635,6 +640,9 @@ scm_read_string_like_syntax (int chr, SCM port, scm_t_read_opts *opts)
               goto str_eof;
             case '|':
             case '\\':
+            case '(':  /* Accept "\(" for use at the beginning of lines
+                         in multiline strings to avoid confusing emacs
+                         lisp modes.  */
               break;
             case '\n':
               if (opts->hungry_eol_escapes_p)
@@ -2022,8 +2030,6 @@ scm_get_hash_procedure (int c)
     }
 }
 
-#define SCM_ENCODING_SEARCH_SIZE (500)
-
 static int
 is_encoding_char (char c)
 {
@@ -2033,9 +2039,20 @@ is_encoding_char (char c)
   return strchr ("_-.:/,+=()", c) != NULL;
 }
 
-/* Search the first few hundred characters of a file for an Emacs-like coding
-   declaration.  Returns either NULL or a string whose storage has been
-   allocated with `scm_gc_malloc ()'.  */
+/* Maximum size of an encoding name.  This is a bit more than the
+   longest name listed at
+   <http://www.iana.org/assignments/character-sets> ("ISO-2022-JP-2", 13
+   characters.)  */
+#define ENCODING_NAME_MAX_SIZE 20
+
+/* Number of bytes at the beginning or end of a file that are scanned
+   for a "coding:" declaration.  */
+#define SCM_ENCODING_SEARCH_SIZE (500 + ENCODING_NAME_MAX_SIZE)
+
+
+/* Search the SCM_ENCODING_SEARCH_SIZE bytes of a file for an Emacs-like
+   coding declaration.  Returns either NULL or a string whose storage
+   has been allocated with `scm_gc_malloc'.  */
 char *
 scm_i_scan_for_encoding (SCM port)
 {
@@ -2094,8 +2111,8 @@ scm_i_scan_for_encoding (SCM port)
       if ((pos = strstr(pos, "coding")) == NULL)
         return NULL;
 
-      pos += strlen("coding");
-      if (pos - header >= SCM_ENCODING_SEARCH_SIZE || 
+      pos += strlen ("coding");
+      if (pos - header >= SCM_ENCODING_SEARCH_SIZE ||
           (*pos == ':' || *pos == '='))
         {
           pos ++;
@@ -2104,10 +2121,17 @@ scm_i_scan_for_encoding (SCM port)
     }
 
   /* skip spaces */
-  while (pos - header <= SCM_ENCODING_SEARCH_SIZE && 
+  while (pos - header <= SCM_ENCODING_SEARCH_SIZE &&
         (*pos == ' ' || *pos == '\t'))
     pos ++;
 
+  if (pos - header >= SCM_ENCODING_SEARCH_SIZE - ENCODING_NAME_MAX_SIZE)
+    /* We found the "coding:" string, but there is probably not enough
+       room to store an encoding name in its entirety, so ignore it.
+       This makes sure we do not end up returning a truncated encoding
+       name.  */
+    return NULL;
+
   /* grab the next token */
   encoding_start = pos;
   i = 0;