summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Faure <billiob@gmail.com>2020-07-02 22:25:43 +0200
committerBoris Faure <billiob@gmail.com>2020-07-02 22:33:34 +0200
commit2245782dd45728619b8ba024aaf3055e6e0cc1db (patch)
tree599b982d303d60e734a0787291f0018b7faba3eb
parent5a34cfdf63d76db917ec2579adaba4329b709cab (diff)
termptyesc: handle xterm color format starting with "rgb:"
-rw-r--r--src/bin/termptyesc.c107
-rw-r--r--tests/tests.results1
-rwxr-xr-xtests/xterm-colors-rgb.sh80
3 files changed, 188 insertions, 0 deletions
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index 932b782..f4f7940 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -3762,6 +3762,98 @@ _xterm_parse_color_sharp(Eina_Unicode *p,
3762 return 0; 3762 return 0;
3763} 3763}
3764 3764
3765static int
3766_xterm_parse_color_rgbi(Eina_Unicode *p EINA_UNUSED,
3767 unsigned char *r EINA_UNUSED,
3768 unsigned char *g EINA_UNUSED,
3769 unsigned char *b EINA_UNUSED,
3770 int len EINA_UNUSED)
3771{
3772 return -1;
3773}
3774
3775/* returns len read or -1 in case of error */
3776static int
3777_xterm_parse_hex(Eina_Unicode *p, unsigned char *c, int len)
3778{
3779 int l = 0;
3780 int v = 0;
3781 int i;
3782
3783 i = _eina_unicode_to_hex(p[0]);
3784 if (i < 0) return -1;
3785 p++;
3786 l++;
3787 len--;
3788 if (len <= 0)
3789 goto end;
3790 v = i;
3791
3792 i = _eina_unicode_to_hex(p[0]);
3793 if (i < 0) goto end;
3794 p++;
3795 l++;
3796 len--;
3797 if (len <= 0)
3798 goto end;
3799 v = v * 16 + i;
3800
3801 i = _eina_unicode_to_hex(p[0]);
3802 if (i < 0) goto end;
3803 p++;
3804 l++;
3805 len--;
3806 if (len <= 0)
3807 goto end;
3808
3809 i = _eina_unicode_to_hex(p[0]);
3810 if (i < 0) goto end;
3811 p++;
3812 l++;
3813 len--;
3814 if (len <= 0)
3815 goto end;
3816
3817end:
3818 if (l == 1)
3819 v <<= 4;
3820 *c = v;
3821 if (l > 0)
3822 return l;
3823 return -1;
3824}
3825
3826static int
3827_xterm_parse_color_rgb(Eina_Unicode *p,
3828 unsigned char *r, unsigned char *g, unsigned char *b,
3829 int len)
3830{
3831 int l;
3832
3833 /* parse r */
3834 l = _xterm_parse_hex(p, r, len);
3835 if (l <= 0)
3836 return -1;
3837 p += l;
3838 if (p[0] != '/')
3839 return -1;
3840 p++;
3841 /* parse g */
3842 l = _xterm_parse_hex(p, g, len);
3843 if (l <= 0)
3844 return -1;
3845 p += l;
3846 if (p[0] != '/')
3847 return -1;
3848 p++;
3849 /* parse b */
3850 l = _xterm_parse_hex(p, b, len);
3851 if (l <= 0)
3852 return -1;
3853
3854 return 0;
3855}
3856
3765 3857
3766static int 3858static int
3767_xterm_parse_color(Termpty *ty, Eina_Unicode **ptr, 3859_xterm_parse_color(Termpty *ty, Eina_Unicode **ptr,
@@ -3777,6 +3869,21 @@ _xterm_parse_color(Termpty *ty, Eina_Unicode **ptr,
3777 if (_xterm_parse_color_sharp(p, r, g, b, len)) 3869 if (_xterm_parse_color_sharp(p, r, g, b, len))
3778 goto err; 3870 goto err;
3779 } 3871 }
3872 else if (len > 5 && p[0] == 'r' && p[1] == 'g' && p[2] == 'b')
3873 {
3874 if (p[3] == 'i' && p[4] == ':')
3875 {
3876 if (_xterm_parse_color_rgbi(p+5, r, g, b, len-5))
3877 goto err;
3878 }
3879 else if (p[3] == ':')
3880 {
3881 if (_xterm_parse_color_rgb(p+4, r, g, b, len-4))
3882 goto err;
3883 }
3884 else
3885 goto err;
3886 }
3780 else 3887 else
3781 goto err; 3888 goto err;
3782 3889
diff --git a/tests/tests.results b/tests/tests.results
index 369da82..1d33d20 100644
--- a/tests/tests.results
+++ b/tests/tests.results
@@ -146,3 +146,4 @@ crash_empty_osc.sh b87272896ce7be9856253b32be1bef14
146xterm-osc-10.sh b8c23c9c5482b1e9c30d8a261edc29f0 146xterm-osc-10.sh b8c23c9c5482b1e9c30d8a261edc29f0
147xterm-osc-11.sh 3e02038964b78d948fb599c996bf370d 147xterm-osc-11.sh 3e02038964b78d948fb599c996bf370d
148xterm-colors-sharp.sh 79d6f72df04237d76a0fa3e722dcec5b 148xterm-colors-sharp.sh 79d6f72df04237d76a0fa3e722dcec5b
149xterm-colors-rgb.sh d9b55817ef8428343105b44dabd535a8
diff --git a/tests/xterm-colors-rgb.sh b/tests/xterm-colors-rgb.sh
new file mode 100755
index 0000000..06ee96d
--- /dev/null
+++ b/tests/xterm-colors-rgb.sh
@@ -0,0 +1,80 @@
1#!/bin/sh
2
3# char width: 7
4# char height: 15
5
6# set color
7printf '\033[0;31;3m'
8
9# clear screen
10printf '\033[2J'
11
12# move to 0; 0
13printf '\033[0;0H'
14
15# set color
16printf '\033[0m'
17
18printf 'The purpose of computing is insight, not numbers.\r\n'
19printf 'Richard Hamming\r\n'
20
21# valid colors
22# one nibble per color
23printf '\033]10;rgb:f/0/f\007'
24# two nibble per color
25printf '\033]10;rgb:ff/00/ff\007'
26# three nibble per color
27printf '\033]10;rgb:fff/000/fff\007'
28# four nibble per color
29printf '\033]10;rgb:FFFF/0000/FFFF\007'
30
31##
32# invalid
33##
34
35# not hex
36# one nibble per color
37printf '\033]10;rgb:g/0/f\007'
38printf '\033]10;rgb:f/g/f\007'
39printf '\033]10;rgb:f/0/g\007'
40# two nibble per color
41printf '\033]10;rgb:gf/00/ff\007'
42printf '\033]10;rgb:fg/00/ff\007'
43printf '\033]10;rgb:ff/g0/ff\007'
44printf '\033]10;rgb:ff/0g/ff\007'
45printf '\033]10;rgb:ff/00/gf\007'
46printf '\033]10;rgb:ff/00/fg\007'
47# three nibble per color
48printf '\033]10;rgb:gff/000/fff\007'
49printf '\033]10;rgb:fgf/000/fff\007'
50printf '\033]10;rgb:ffg/000/fff\007'
51printf '\033]10;rgb:fff/g00/fff\007'
52printf '\033]10;rgb:fff/0g0/fff\007'
53printf '\033]10;rgb:fff/00g/fff\007'
54printf '\033]10;rgb:fff/000/gff\007'
55printf '\033]10;rgb:fff/000/fgf\007'
56printf '\033]10;rgb:fff/000/ffg\007'
57# four nibble per color
58printf '\033]10;rgb:gFFF/0000/FFFF\007'
59printf '\033]10;rgb:FgFF/0000/FFFF\007'
60printf '\033]10;rgb:FFgF/0000/FFFF\007'
61printf '\033]10;rgb:FFFg/0000/FFFF\007'
62printf '\033]10;rgb:FFFF/g000/FFFF\007'
63printf '\033]10;rgb:FFFF/0g00/FFFF\007'
64printf '\033]10;rgb:FFFF/00g0/FFFF\007'
65printf '\033]10;rgb:FFFF/000g/FFFF\007'
66printf '\033]10;rgb:FFFF/0000/gFFF\007'
67printf '\033]10;rgb:FFFF/0000/FgFF\007'
68printf '\033]10;rgb:FFFF/0000/FFgF\007'
69printf '\033]10;rgb:FFFF/0000/FFFg\007'
70
71# too short
72printf '\033]10;rgb:/F/F\007'
73printf '\033]10;rgb:F//F\007'
74printf '\033]10;rgb:F/F/\007'
75# too long
76printf '\033]10;rgb:FFFFF/0000/FFFF\007'
77printf '\033]10;rgb:FFFF/00000/FFFF\007'
78printf '\033]10;rgb:FFFF/0000/FFFFF\007'
79printf '\033]10;rgb:FFFF/0000/FFFF/\007'
80printf '\033]10;rgb:FFFF/0000/FFFF/0000\007'