summaryrefslogtreecommitdiffhomepage
path: root/src/bridge/ptyc_term_sgr.c
blob: 31517182d6486676b75af6478699c217f643c668 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*********************************************************/
/*  ptycon: a pty-console bridge                         */
/*  Copyright (C) 2016  Z. Gilboa                        */
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
/*********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntcon/ntcon.h>

#include <ptycon/ptycon.h>
#include "ptycon_bridge_impl.h"
#include "ptycon_driver_impl.h"

/* forward declarations: sgr control handlers */
static ptyc_term_handler	sgr_illegal_code;
static ptyc_term_handler	sgr_default;
static ptyc_term_handler	sgr_set_ansi_foreground_color;
static ptyc_term_handler	sgr_set_ansi_background_color;
static ptyc_term_handler	sgr_set_aixterm_foreground_color;
static ptyc_term_handler	sgr_set_aixterm_background_color;

static const uint16_t ansi_foreground_colors[10]  = {
	NT_FOREGROUND_BLACK,
	NT_FOREGROUND_RED,
	NT_FOREGROUND_GREEN,
	NT_FOREGROUND_YELLOW,
	NT_FOREGROUND_BLUE,
	NT_FOREGROUND_MAGENTA,
	NT_FOREGROUND_CYAN,
	NT_FOREGROUND_WHITE,
	0,
	NT_FOREGROUND_WHITE	/* default */
};


static const uint16_t ansi_background_colors[10]  = {
	NT_BACKGROUND_BLACK,
	NT_BACKGROUND_RED,
	NT_BACKGROUND_GREEN,
	NT_BACKGROUND_YELLOW,
	NT_BACKGROUND_BLUE,
	NT_BACKGROUND_MAGENTA,
	NT_BACKGROUND_CYAN,
	NT_BACKGROUND_WHITE,
	0,
	NT_BACKGROUND_BLACK	/* default */
};


static const uint16_t aixterm_foreground_colors[8]  = {
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_BLACK,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_RED,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_GREEN,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_YELLOW,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_BLUE,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_MAGENTA,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_CYAN,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_WHITE
};


static const uint16_t aixterm_background_colors[8]  = {
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_BLACK,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_RED,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_GREEN,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_YELLOW,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_BLUE,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_MAGENTA,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_CYAN,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_WHITE
};


ptyc_term_handler * const ptyc_sgr_handlers[PTYC_SGR_ARRAY_SIZE] = {
	sgr_default,				/* 0 */		/* default */
	sgr_illegal_code,			/* 1 */		/* bold    */
	sgr_illegal_code,			/* 2 */
	sgr_illegal_code,			/* 3 */
	sgr_illegal_code,			/* 4 */		/* underlined */
	sgr_illegal_code,			/* 5 */		/* blink      */
	sgr_illegal_code,			/* 6 */
	sgr_illegal_code,			/* 7 */		/* inverse   */
	sgr_illegal_code,			/* 8 */		/* invisible */
	sgr_illegal_code,			/* 9 */
	sgr_illegal_code,			/* 10 */
	sgr_illegal_code,			/* 11 */
	sgr_illegal_code,			/* 12 */
	sgr_illegal_code,			/* 13 */
	sgr_illegal_code,			/* 14 */
	sgr_illegal_code,			/* 15 */
	sgr_illegal_code,			/* 16 */
	sgr_illegal_code,			/* 17 */
	sgr_illegal_code,			/* 18 */
	sgr_illegal_code,			/* 19 */
	sgr_illegal_code,			/* 20 */
	sgr_illegal_code,			/* 21 */
	sgr_illegal_code,			/* 22 */	/* normal */
	sgr_illegal_code,			/* 23 */
	sgr_illegal_code,			/* 24 */	/* not underlined */
	sgr_illegal_code,			/* 25 */	/* steady         */
	sgr_illegal_code,			/* 26 */
	sgr_illegal_code,			/* 27 */	/* positive */
	sgr_illegal_code,			/* 28 */	/* visible  */
	sgr_illegal_code,			/* 29 */
	sgr_set_ansi_foreground_color,		/* 30 */	/* foreground: black   */
	sgr_set_ansi_foreground_color,		/* 31 */	/* foreground: red     */
	sgr_set_ansi_foreground_color,		/* 32 */	/* foreground: green   */
	sgr_set_ansi_foreground_color,		/* 33 */	/* foreground: yellow  */
	sgr_set_ansi_foreground_color,		/* 34 */	/* foreground: blue    */
	sgr_set_ansi_foreground_color,		/* 35 */	/* foreground: magenta */
	sgr_set_ansi_foreground_color,		/* 36 */	/* foreground: cyan    */
	sgr_set_ansi_foreground_color,		/* 37 */	/* foreground: white   */
	sgr_illegal_code,			/* 38 */
	sgr_set_ansi_foreground_color,		/* 39 */	/* foreground: default */
	sgr_set_ansi_background_color,		/* 40 */	/* background: black   */
	sgr_set_ansi_background_color,		/* 41 */	/* background: red     */
	sgr_set_ansi_background_color,		/* 42 */	/* background: green   */
	sgr_set_ansi_background_color,		/* 43 */	/* background: yellow  */
	sgr_set_ansi_background_color,		/* 44 */	/* background: blue    */
	sgr_set_ansi_background_color,		/* 45 */	/* background: magenta */
	sgr_set_ansi_background_color,		/* 46 */	/* background: cyan    */
	sgr_set_ansi_background_color,		/* 47 */	/* background: white   */
	sgr_illegal_code,			/* 48 */
	sgr_set_ansi_background_color,		/* 49 */	/* background: default */
	sgr_illegal_code,			/* 50 */
	sgr_illegal_code,			/* 51 */
	sgr_illegal_code,			/* 52 */
	sgr_illegal_code,			/* 53 */
	sgr_illegal_code,			/* 54 */
	sgr_illegal_code,			/* 55 */
	sgr_illegal_code,			/* 56 */
	sgr_illegal_code,			/* 57 */
	sgr_illegal_code,			/* 58 */
	sgr_illegal_code,			/* 59 */
	sgr_illegal_code,			/* 60 */
	sgr_illegal_code,			/* 61 */
	sgr_illegal_code,			/* 62 */
	sgr_illegal_code,			/* 63 */
	sgr_illegal_code,			/* 64 */
	sgr_illegal_code,			/* 65 */
	sgr_illegal_code,			/* 66 */
	sgr_illegal_code,			/* 67 */
	sgr_illegal_code,			/* 68 */
	sgr_illegal_code,			/* 69 */
	sgr_illegal_code,			/* 70 */
	sgr_illegal_code,			/* 71 */
	sgr_illegal_code,			/* 72 */
	sgr_illegal_code,			/* 73 */
	sgr_illegal_code,			/* 74 */
	sgr_illegal_code,			/* 75 */
	sgr_illegal_code,			/* 76 */
	sgr_illegal_code,			/* 77 */
	sgr_illegal_code,			/* 78 */
	sgr_illegal_code,			/* 79 */
	sgr_illegal_code,			/* 80 */
	sgr_illegal_code,			/* 81 */
	sgr_illegal_code,			/* 82 */
	sgr_illegal_code,			/* 83 */
	sgr_illegal_code,			/* 84 */
	sgr_illegal_code,			/* 85 */
	sgr_illegal_code,			/* 86 */
	sgr_illegal_code,			/* 87 */
	sgr_illegal_code,			/* 88 */
	sgr_illegal_code,			/* 89 */
	sgr_set_aixterm_foreground_color,	/* 90 */	/* foreground: bright black   */
	sgr_set_aixterm_foreground_color,	/* 91 */	/* foreground: bright red     */
	sgr_set_aixterm_foreground_color,	/* 92 */	/* foreground: bright green   */
	sgr_set_aixterm_foreground_color,	/* 93 */	/* foreground: bright yellow  */
	sgr_set_aixterm_foreground_color,	/* 94 */	/* foreground: bright blue    */
	sgr_set_aixterm_foreground_color,	/* 95 */	/* foreground: bright magenta */
	sgr_set_aixterm_foreground_color,	/* 96 */	/* foreground: bright cyan    */
	sgr_set_aixterm_foreground_color,	/* 97 */	/* foreground: bright white   */
	sgr_illegal_code,			/* 98 */
	sgr_illegal_code,			/* 99 */
	sgr_set_aixterm_background_color,	/* 100 */	/* background: bright black   */
	sgr_set_aixterm_background_color,	/* 101 */	/* background: bright red     */
	sgr_set_aixterm_background_color,	/* 102 */	/* background: bright green   */
	sgr_set_aixterm_background_color,	/* 103 */	/* background: bright yellow  */
	sgr_set_aixterm_background_color,	/* 104 */	/* background: bright blue    */
	sgr_set_aixterm_background_color,	/* 105 */	/* background: bright magenta */
	sgr_set_aixterm_background_color,	/* 106 */	/* background: bright cyan    */
	sgr_set_aixterm_background_color,	/* 107 */	/* background: bright white   */
	sgr_illegal_code
};


static void * __fastcall sgr_illegal_code(struct ptyc_term_ctx * tctx)
{
	(void)tctx;
	return 0;
}


static void * __fastcall sgr_default(struct ptyc_term_ctx * tctx)
{
	tctx->background = NT_BACKGROUND_BLACK;
	tctx->foreground = NT_FOREGROUND_WHITE;

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_ansi_foreground_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 30;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->foreground = ansi_foreground_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_ansi_background_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 40;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->background = ansi_background_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_aixterm_foreground_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 90;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->foreground = aixterm_foreground_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_aixterm_background_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 100;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->background = ansi_background_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}