[mw-devel] [Git][milliways/mw][master] 2 commits: Handle escape sequences that turn out not to be valid

Alice Mitchell alice at sucs.org
Fri Mar 29 14:35:01 GMT 2019


Alice Mitchell pushed to branch master at milliways / mw


Commits:
ae7e7128 by Alice Mitchell at 2019-03-29T13:24:53Z
Handle escape sequences that turn out not to be valid

- - - - -
a881209c by Alice Mitchell at 2019-03-29T14:34:18Z
Add reset sequence &0& and more fixes to ignoring invalid sequences

- - - - -


2 changed files:

- src/client/colour.c
- src/client/main.c


Changes:

=====================================
src/client/colour.c
=====================================
@@ -11,6 +11,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <errno.h>
+#include <stdbool.h>
 #include "frl.h"
 #include "colour.h"
 #include "user.h"
@@ -54,6 +55,7 @@ int colour(const char *input, char *output, int outlen)
 {
 	int consume = 0;
 
+	if (outlen) output[0] = '\0';
 	/* original colour mode */
 	if (*input == 033) {
 		char text[3];
@@ -95,6 +97,7 @@ int colour(const char *input, char *output, int outlen)
 		if (strchr("-n", text[0]) &&
 		    strchr("-n", text[1]) )
 		{
+			output[i++] = '0';
 			output[i++] = 'm';
 			output[i] = '\0';
 			return(consume);
@@ -145,41 +148,47 @@ int colour(const char *input, char *output, int outlen)
 		output[i] = '\0';
 	}
 	if (*input == '&') {
+		bool fg_done = false;
 		/* first let us test this is a valid colour string */
 		const char *p = input + 1;
 		/* skip past all valid chars */
-		while (*p && strchr("0123456789AbBcCdDeEfF:", *p)) p++;
+		while (*p && strchr("0123456789aAbBcCdDeEfF:", *p)) p++;
 		/* not the ending we were expecting, ignore it then */
 		if (*p != '&') return 0;
+		/* empty pattern is not a pattern */
+		if (p == input + 1) return 0;
 
-		char hex[7];
-		int i = 0;
-		consume = 1;
-		while (i < 7 && input[consume] && strchr("0123456789AbBcCdDeEfF", input[consume]))
-			hex[i++] = input[consume++];
-		hex[i] = '\0';
-
-		if (i == 2) {
-			int col = hex_decode(hex, 2);
-			snprintf(output, outlen, "\033[38;5;%dm", col);
-		}
+		int outused = 0;
 
-		if (input[consume] == ':') {
+		do {
+			char hex[7];
+			int i = 0;
 			consume++;
-			i=0;
-			while (i < 7 && input[consume] && strchr("0123456789AbBcCdDeEfF", input[consume]))
+			while (i < 7 && input[consume] && strchr("0123456789aAbBcCdDeEfF", input[consume]))
 				hex[i++] = input[consume++];
 			hex[i] = '\0';
 
+			if (i == 1) {
+				int col = hex_decode(hex, 1);
+				if (col < 10)
+					snprintf(&output[outused], outlen-outused, "\033[%dm", col);
+			} else
 			if (i == 2) {
 				int col = hex_decode(hex, 2);
-				int off = strlen(output);
-				snprintf(&output[off], outlen-off, "\033[48;5;%dm", col);
+				snprintf(&output[outused], outlen-outused, "\033[%d;5;%dm", fg_done?48:38, col);
+				fg_done = true;
+			} else {
+				output[0] = '\0';
+				return 0;
 			}
+			outused = strlen(output);
+		} while(input[consume] == ':');
+
+		if (input[consume] != '&') {
+			output[0] = '\0';
+			return 0;
 		}
-		while (input[consume] && strchr("0123456789AbBcCdDeEfF:", input[consume]))
-			consume++;
-		if (input[consume] == '&') consume++;
+		consume++;
 	}
 	return(consume);
 }


=====================================
src/client/main.c
=====================================
@@ -1086,11 +1086,15 @@ void display_message(const char *text, int beeps, int newline)
 	if (UseRL && disable_rl(1)) charcount = 0;
 	
 	while (*text) {
+		int skip = 0;
 		if (*text == 033 || *text == '&') {
-			/* escape sequence, skip next two chars */
-
-			text += colour(text, colr, sizeof(colr));
+			/* possible escape sequence */
+			skip = colour(text, colr, sizeof(colr));
+		} 
 
+		if (skip) {
+			/* an escape sequence to be substituted */
+			text += skip;
 			if (colr[0] && !s_colouroff(user)) {
 				for (int j = 0; j < strlen(colr); j++)
 					outline[olen++] = colr[j];
@@ -1125,6 +1129,7 @@ void display_message(const char *text, int beeps, int newline)
 			if (!s_colouroff(user) && colr[0]) {
 			        outline[olen++] = 033;
 			        outline[olen++] = '[';
+			        outline[olen++] = '0';
 			        outline[olen++] = 'm';
 			}
 



View it on GitLab: https://projects.sucs.org/milliways/mw/compare/15a0fb4485dee17929cb033ab49c6322f9a9c729...a881209c21275f4a90ce186494fd313ef6896ef5

-- 
View it on GitLab: https://projects.sucs.org/milliways/mw/compare/15a0fb4485dee17929cb033ab49c6322f9a9c729...a881209c21275f4a90ce186494fd313ef6896ef5
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20190329/c8b1af52/attachment-0001.html>


More information about the mw-devel mailing list