Commit a881209c authored by Alice Mitchell's avatar Alice Mitchell

Add reset sequence &0& and more fixes to ignoring invalid sequences

parent ae7e7128
Pipeline #547 passed with stage
in 8 minutes and 13 seconds
......@@ -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"
......@@ -96,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);
......@@ -146,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);
}
......
......@@ -1129,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';
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment