Skip to content
GitLab
Explore
Sign in
Commits on Source (2)
Handle escape sequences that turn out not to be valid
· ae7e7128
Alice Mitchell
authored
Mar 29, 2019
ae7e7128
Add reset sequence &0& and more fixes to ignoring invalid sequences
· a881209c
Alice Mitchell
authored
Mar 29, 2019
a881209c
Hide whitespace changes
Inline
Side-by-side
src/client/colour.c
View file @
a881209c
...
...
@@ -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
(
"0123456789
a
AbBcCdDeEfF:"
,
*
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
View file @
a881209c
...
...
@@ -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'
;
}
...
...