-
Notifications
You must be signed in to change notification settings - Fork 0
/
xterm2ansi.c
143 lines (126 loc) · 3.55 KB
/
xterm2ansi.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "xterm2ansi.h"
#define CSI_MAX_ARGS 16
#define CSI_MAX_LEN 64
typedef struct {
int args [CSI_MAX_ARGS];
int num_args;
char buffer[CSI_MAX_LEN];
int len;
} CSI_code;
enum state {
STATE_NORMAL,
STATE_ESCAPE,
STATE_CSI,
};
void csi_conv_xterm_ansi(CSI_code csi_in, CSI_code *csi_out) {
int color;
int diff = 0;
for (int i = 0; i < csi_in.num_args; i++) {
switch (csi_in.args[i]) {
case 48: /* background */
diff = 10;
case 38: /* foreground */
if (csi_in.args[i + 1] == 5) {
color = xterm_to_ansi(csi_in.args[i + 2]);
csi_out->args[csi_out->num_args++] = color + diff;
i += 2;
} else if (csi_in.args[i + 1] == 2) {
color = rgb_to_ansi(csi_in.args[i + 2],
csi_in.args[i + 3],
csi_in.args[i + 4]);
csi_out->args[csi_out->num_args++] = color + diff;
i += 4;
}
break;
default:
csi_out->args[csi_out->num_args++] = csi_in.args[i];
break;
}
}
}
void parse_csi(CSI_code *csi) {
char *cur = csi->buffer;
char *end = NULL;
long int num;
while (cur < csi->buffer + csi->len) {
num = strtol(cur, &end, 10);
if (end == cur) {
num = 0;
}
cur = end;
csi->args[csi->num_args++] = num;
if (csi->num_args >= CSI_MAX_ARGS) {
break;
}
cur++;
}
}
void csi_print(CSI_code csi, FILE *stream) {
fputc('\033', stream);
fputc('[', stream);
for (int i = 0; i < csi.num_args; i++) {
fprintf(stream, "%d", csi.args[i]);
if (i < csi.num_args - 1) {
fputc(';', stream);
}
}
fputc('m', stream);
}
void put_conv_chr(char c, FILE *stream) {
static enum state state = STATE_NORMAL;
static CSI_code csi_in, csi_out;
switch (state) {
case STATE_NORMAL:
if (c == '\033') {
state = STATE_ESCAPE;
} else {
fputc(c, stream);
}
break;
case STATE_ESCAPE:
if (c == '[') {
state = STATE_CSI;
csi_in.num_args = 0;
csi_in.len = 0;
} else {
state = STATE_NORMAL;
fputc('\033', stream);
fputc(c, stream);
}
break;
case STATE_CSI:
csi_in.buffer[csi_in.len++] = c;
// NOTE: probably odd to process on max length?
if (c == 'm' || csi_in.len >= CSI_MAX_LEN) {
state = STATE_NORMAL;
csi_in.buffer[csi_in.len] = '\0';
parse_csi(&csi_in);
csi_out.num_args = 0;
csi_out.len = 0;
csi_conv_xterm_ansi(csi_in, &csi_out);
csi_print(csi_out, stream);
}
break;
}
}
int main(int argc, char *argv[]) {
FILE *input;
if (argc > 1) {
input = fopen(argv[1], "r");
if (input == NULL) {
fprintf(stderr, "Error: Could not open input file %s\n", argv[1]);
return EXIT_FAILURE;
}
} else {
input = stdin;
}
char c;
while ((c = fgetc(input)) != EOF) {
put_conv_chr(c, stdout);
}
return EXIT_SUCCESS;
}