Commit fa8bd1ff authored by Alice Mitchell's avatar Alice Mitchell

defer outputting a colour sequence

If we dont alter the output string until we have a valid escape sequence
then we dont accidentally forget the current acting colour when bad
sequences are found
parent a881209c
Pipeline #548 passed with stage
in 8 minutes and 11 seconds
......@@ -51,9 +51,11 @@ static int hex_decode(const char *input, int len)
}
/* return colour code sequence */
int colour(const char *input, char *output, int outlen)
int colour(const char *input, char *result, int reslen)
{
int consume = 0;
char output[1024];
int outlen = sizeof(output);
if (outlen) output[0] = '\0';
/* original colour mode */
......@@ -81,10 +83,10 @@ int colour(const char *input, char *output, int outlen)
if (i>=0 && i < COLOUR_LIMIT) {
if (colour_chart[i] != NULL)
copy_in(output, outlen, colour_chart[i]);
copy_in(result, reslen, colour_chart[i]);
else
if (colour_chart[0] != NULL)
copy_in(output, outlen, colour_chart[0]);
copy_in(result, reslen, colour_chart[0]);
}
return consume;
}
......@@ -100,6 +102,7 @@ int colour(const char *input, char *output, int outlen)
output[i++] = '0';
output[i++] = 'm';
output[i] = '\0';
copy_in(result, reslen, output);
return(consume);
}
......@@ -146,6 +149,7 @@ int colour(const char *input, char *output, int outlen)
output[i++] = 'm';
output[i] = '\0';
copy_in(result, reslen, output);
}
if (*input == '&') {
bool fg_done = false;
......@@ -178,18 +182,17 @@ int colour(const char *input, char *output, int outlen)
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;
}
consume++;
}
copy_in(result, reslen, output);
return(consume);
}
......
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