public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Cygwin: console: Revise color setting codes in legacy console mode.
@ 2020-02-04 10:49 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2020-02-04 10:49 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=ac1ccc29e1e2abb57cb956c2dfd2c4c34943cfdd

commit ac1ccc29e1e2abb57cb956c2dfd2c4c34943cfdd
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Sat Feb 1 13:28:39 2020 +0900

    Cygwin: console: Revise color setting codes in legacy console mode.
    
    - With this patch, foreground color and background color are allowed
      to be set simultaneously by 24 bit color escape sequence such as
      ESC[38;2;0;0;255;48;2;128;128;0m in legacy console mode.

Diff:
---
 winsup/cygwin/fhandler.h          |  2 +-
 winsup/cygwin/fhandler_console.cc | 47 ++++++++++++++++++++++-----------------
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 1b477f6..9270c83 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1832,7 +1832,7 @@ enum ansi_intensity
 #define gotrparen 9
 #define eatpalette 10
 #define endpalette 11
-#define MAXARGS 10
+#define MAXARGS 16
 
 enum cltype
 {
diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc
index f88d247..38eed05 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -1952,7 +1952,7 @@ fhandler_console::char_command (char c)
   switch (c)
     {
     case 'm':   /* Set Graphics Rendition */
-       for (int i = 0; i <= con.nargs; i++)
+       for (int i = 0; i < con.nargs; i++)
 	 switch (con.args[i])
 	   {
 	     case 0:    /* normal color */
@@ -2020,38 +2020,39 @@ fhandler_console::char_command (char c)
 	       con.fg = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
 	       break;
 	     case 38:
-	       if (con.nargs < 1)
+	       if (con.nargs < i + 2)
 		 /* Sequence error (abort) */
 		 break;
-	       switch (con.args[1])
+	       switch (con.args[i + 1])
 		 {
 		 case 2:
-		   if (con.nargs != 4)
+		   if (con.nargs < i + 5)
 		     /* Sequence error (abort) */
 		     break;
-		   r = con.args[2];
-		   g = con.args[3];
-		   b = con.args[4];
+		   r = con.args[i + 2];
+		   g = con.args[i + 3];
+		   b = con.args[i + 4];
 		   r = r < (95 + 1) / 2 ? 0 : r > 255 ? 5 : (r - 55 + 20) / 40;
 		   g = g < (95 + 1) / 2 ? 0 : g > 255 ? 5 : (g - 55 + 20) / 40;
 		   b = b < (95 + 1) / 2 ? 0 : b > 255 ? 5 : (b - 55 + 20) / 40;
 		   con.fg = table256[16 + r*36 + g*6 + b];
+		   i += 4;
 		   break;
 		 case 5:
-		   if (con.nargs != 2)
+		   if (con.nargs < i + 3)
 		     /* Sequence error (abort) */
 		     break;
 		   {
-		     int idx = con.args[2];
+		     int idx = con.args[i + 2];
 		     if (idx < 0)
 		       idx = 0;
 		     if (idx > 255)
 		       idx = 255;
 		     con.fg = table256[idx];
+		     i += 2;
 		   }
 		   break;
 		 }
-	       i += con.nargs;
 	       break;
 	     case 39:
 	       con.fg = con.default_color & FOREGROUND_ATTR_MASK;
@@ -2081,38 +2082,39 @@ fhandler_console::char_command (char c)
 	       con.bg = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
 	       break;
 	     case 48:
-	       if (con.nargs < 1)
+	       if (con.nargs < i + 2)
 		 /* Sequence error (abort) */
 		 break;
-	       switch (con.args[1])
+	       switch (con.args[i + 1])
 		 {
 		 case 2:
-		   if (con.nargs != 4)
+		   if (con.nargs < i + 5)
 		     /* Sequence error (abort) */
 		     break;
-		   r = con.args[2];
-		   g = con.args[3];
-		   b = con.args[4];
+		   r = con.args[i + 2];
+		   g = con.args[i + 3];
+		   b = con.args[i + 4];
 		   r = r < (95 + 1) / 2 ? 0 : r > 255 ? 5 : (r - 55 + 20) / 40;
 		   g = g < (95 + 1) / 2 ? 0 : g > 255 ? 5 : (g - 55 + 20) / 40;
 		   b = b < (95 + 1) / 2 ? 0 : b > 255 ? 5 : (b - 55 + 20) / 40;
 		   con.bg = table256[16 + r*36 + g*6 + b] << 4;
+		   i += 4;
 		   break;
 		 case 5:
-		   if (con.nargs != 2)
+		   if (con.nargs < i + 3)
 		     /* Sequence error (abort) */
 		     break;
 		   {
-		     int idx = con.args[2];
+		     int idx = con.args[i + 2];
 		     if (idx < 0)
 		       idx = 0;
 		     if (idx > 255)
 		       idx = 255;
 		     con.bg = table256[idx] << 4;
+		     i += 2;
 		   }
 		   break;
 		 }
-	       i += con.nargs;
 	       break;
 	     case 49:
 	       con.bg = con.default_color & BACKGROUND_ATTR_MASK;
@@ -2806,7 +2808,7 @@ fhandler_console::write (const void *vsrc, size_t len)
 	    {
 	      src++;
 	      con.nargs++;
-	      if (con.nargs >= MAXARGS)
+	      if (con.nargs > MAXARGS)
 		con.nargs--;
 	    }
 	  else if (*src == ' ')
@@ -2819,6 +2821,9 @@ fhandler_console::write (const void *vsrc, size_t len)
 	    con.state = gotcommand;
 	  break;
 	case gotcommand:
+	  con.nargs ++;
+	  if (con.nargs > MAXARGS)
+	    con.nargs--;
 	  char_command (*src++);
 	  con.state = normal;
 	  break;
@@ -2871,6 +2876,8 @@ fhandler_console::write (const void *vsrc, size_t len)
 	    {
 	      con.state = gotarg1;
 	      con.nargs++;
+	      if (con.nargs > MAXARGS)
+		con.nargs--;
 	      src++;
 	    }
 	  else if (isalpha (*src))


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-02-04 10:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-04 10:49 [newlib-cygwin] Cygwin: console: Revise color setting codes in legacy console mode Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).