-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr_ansi.rh
69 lines (61 loc) · 1.15 KB
/
pr_ansi.rh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Compile (to pr_ansi.h):
* $ ragel -C -G2 pr_ansi.rh
* For graphviz output:
* $ ragel -V -p pr_ansi.rh | dot -T png -o out.png
*/
%%{
machine term_support;
main := (
"xterm" |
"rxvt" |
"screen" |
"Eterm" |
"ansi" 0 |
"pcansi" 0 |
"linux" 0
) @{ res = 1; fbreak; };
}%%
static int terminal_supported( char const* p ) {
int res = 0;
int cs = 0;
%%{
machine term_support;
write data nofinal noerror;
write init;
write exec noend;
}%%
(void)term_support_en_main;
return res;
}
%%{
machine ansi_escape;
fg = "3" digit;
bg = "4" digit;
md = digit;
opt = fg | bg | md;
action flush {
if( p > prev )
w( prev, p-prev );
prev = p;
}
action ansi {
cprint_doansi( info, prev, p+1-prev );
prev = p+1;
}
esc = 27 >flush "[" opt ( ";" opt )* "m" @ansi;
main := (any | esc)* %/flush ;
}%%
static void write_ansi( cprint_info* info, char const* p, size_t n ) {
int cs = 0;
char const* pe = p + n;
char const* eof = pe;
char const* prev = p;
%%{
machine ansi_escape;
write data nofinal noerror;
write init;
write exec;
}%%
(void)ansi_escape_en_main;
}