c-lex.c (GETC): Redefine to call getch.
authorDave Brolley <brolley@cygnus.com>
Mon, 7 Jun 1999 11:12:38 +0000 (11:12 +0000)
committerDave Brolley <brolley@gcc.gnu.org>
Mon, 7 Jun 1999 11:12:38 +0000 (07:12 -0400)
Mon Jun  7 14:07:39 1999  Dave Brolley  <brolley@cygnus.com>
* c-lex.c (GETC): Redefine to call getch.
(UNGETC): Redefine to call put_back.
(putback_buffer): New structure type.
(putback): New static structure.
(getch): New function.
(put_back): New function.
(yylex): Replace unused bytes from bad multibyte character.

From-SVN: r27393

gcc/ChangeLog
gcc/c-lex.c

index cba11f2ab2e0200c7d36d1b7cb02fa0613fd2996..431ad8d5d327d89ede865048e9638ada5feaacab 100644 (file)
@@ -1,3 +1,13 @@
+Mon Jun  7 14:07:39 1999  Dave Brolley  <brolley@cygnus.com>
+
+       * c-lex.c (GETC): Redefine to call getch.
+       (UNGETC): Redefine to call put_back.
+       (putback_buffer): New structure type.
+       (putback): New static structure.
+       (getch): New function.
+       (put_back): New function.
+       (yylex): Replace unused bytes from bad multibyte character.
+
 Mon Jun  7 13:33:39 1999  Dave Brolley  <brolley@cygnus.com>
 
        * cpplib.c (do_define): Cast `alloca' return value.
index 27c65f3fa27f20f912a3b00b5f058f8f7b541de8..96ca224bf7c21106417a7c2cd478c84abadbe40b 100644 (file)
@@ -71,10 +71,47 @@ extern int yy_get_token ();
 
 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
-#else
-#define GETC() getc (finput)
-#define UNGETC(c) ungetc (c, finput)
-#endif
+
+#else /* ! USE_CPPLIB */
+
+#define GETC() getch ()
+#define UNGETC(c) put_back (c)
+
+struct putback_buffer {
+  char *buffer;
+  int   buffer_size;
+  int   index;
+};
+
+static struct putback_buffer putback = {NULL, 0, -1};
+
+static inline int
+getch ()
+{
+  if (putback.index != -1)
+    {
+      int ch = putback.buffer[putback.index];
+      --putback.index;
+      return ch;
+    }
+  return getc (finput);
+}
+
+static inline void
+put_back (ch)
+     int ch;
+{
+  if (ch != EOF)
+    {
+      if (putback.index == putback.buffer_size - 1)
+       {
+         putback.buffer_size += 16;
+         putback.buffer = xrealloc (putback.buffer, putback.buffer_size);
+       }
+      putback.buffer[++putback.index] = ch;
+    }
+}
+#endif /* ! USE_CPPLIB */
 
 /* the declaration found for the last IDENTIFIER token read in.
    yylex must look this up to detect typedefs, which get token type TYPENAME,
@@ -1972,12 +2009,17 @@ yylex ()
                else
                  {
                    if (char_len == -1)
-                     warning ("Ignoring invalid multibyte character");
-                   if (wide_flag)
-                     c = wc;
+                     {
+                       warning ("Ignoring invalid multibyte character");
+                       /* Replace all but the first byte.  */
+                       for (--i; i > 1; --i)
+                         UNGETC (token_buffer[i]);
+                       wc = token_buffer[1];
+                     }
 #ifdef MAP_CHARACTER
-                   else
-                     c = MAP_CHARACTER (c);
+                     c = MAP_CHARACTER (wc);
+#else
+                     c = wc;
 #endif
                  }
 #else /* ! MULTIBYTE_CHARS */
@@ -2095,20 +2137,24 @@ yylex ()
                    c = GETC ();
                  }
                if (char_len == -1)
-                 warning ("Ignoring invalid multibyte character");
-               else
                  {
-                   /* mbtowc sometimes needs an extra char before accepting */
-                   if (char_len <= i)
-                     UNGETC (c);
-                   if (! wide_flag)
-                     {
-                       p += (i + 1);
-                       c = GETC ();
-                       continue;
-                     }
-                   c = wc;
+                   warning ("Ignoring invalid multibyte character");
+                   /* Replace all except the first byte.  */
+                   UNGETC (c);
+                   for (--i; i > 0; --i)
+                     UNGETC (p[i]);
+                   char_len = 1;
+                 }
+               /* mbtowc sometimes needs an extra char before accepting */
+               if (char_len <= i)
+                 UNGETC (c);
+               if (! wide_flag)
+                 {
+                   p += (i + 1);
+                   c = GETC ();
+                   continue;
                  }
+               c = wc;
 #endif /* MULTIBYTE_CHARS */
              }