3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License (LGPL) as published by the Free Software Foundation.
7 Please refer to the COPYING file for more information.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 Copyright (c) 2004 Bruno T. C. de Oliveira
22 #include "inject_csi.h"
23 #include "roteprivate.h"
27 #define MAX_CSI_ES_PARAMS 32
29 static inline void clamp_cursor_to_bounds(RoteTerm *rt) {
30 if (rt->crow < 0) rt->curpos_dirty = true, rt->crow = 0;
31 if (rt->ccol < 0) rt->curpos_dirty = true, rt->ccol = 0;
33 if (rt->crow >= rt->rows)
34 rt->curpos_dirty = true, rt->crow = rt->rows - 1;
36 if (rt->ccol >= rt->cols)
37 rt->curpos_dirty = true, rt->ccol = rt->cols - 1;
40 /* interprets a 'set attribute' (SGR) CSI escape sequence */
41 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount) {
45 /* special case: reset attributes */
50 for (i = 0; i < pcount; i++) {
52 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
53 // 0 All attributes off
59 // 10 The ASCII character set is the current 7-bit
60 // display character set (default) - SCO Console only.
61 // 11 Map Hex 00-7F of the PC character set codes
62 // to the current 7-bit display character set
63 // - SCO Console only.
64 // 12 Map Hex 80-FF of the current character set to
65 // the current 7-bit display character set - SCO
70 // 27 Negative image off
71 // 28 Invisible image off
73 if (param[i] == 0) rt->curattr = 0x70;
74 else if (param[i] == 1 || param[i] == 2 || param[i] == 4) /* set bold */
75 ROTE_ATTR_MOD_BOLD(rt->curattr,1);
76 else if (param[i] == 5) /* set blink */
77 ROTE_ATTR_MOD_BLINK(rt->curattr,1);
78 else if (param[i] == 7) rt->curattr = 0x07; /* reverse video */
79 else if (param[i] == 8) rt->curattr = 0x0; /* invisible */
80 else if (param[i] == 22 || param[i] == 24) /* bold off */
81 ROTE_ATTR_MOD_BOLD(rt->curattr,0);
82 else if (param[i] == 25) /* blink off */
83 ROTE_ATTR_MOD_BLINK(rt->curattr,0);
84 else if (param[i] == 27) /* negative off */
86 else if (param[i] == 28) /* invisible off */
88 else if (param[i] >= 30 && param[i] <= 37) /* set fg */
89 ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
90 else if (param[i] >= 40 && param[i] <= 47) /* set bg */
91 ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
92 else if (param[i] == 39) /* reset foreground to default */
93 ROTE_ATTR_MOD_FG(rt->curattr, 7);
94 else if (param[i] == 49) /* reset background to default */
95 ROTE_ATTR_MOD_BG(rt->curattr, 0);
99 /* interprets an 'erase display' (ED) escape sequence */
100 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) {
102 int start_row, start_col, end_row, end_col;
105 if (pcount && param[0] == 2)
106 start_row = 0, start_col = 0, end_row = rt->rows - 1,
107 end_col = rt->cols - 1;
109 else if (pcount && param[0] == 1)
110 start_row = 0, start_col = 0, end_row = rt->crow,
113 else start_row = rt->crow, start_col = rt->ccol,
114 end_row = rt->rows - 1, end_col = rt->cols - 1;
117 for (r = start_row; r <= end_row; r++) {
118 rt->line_dirty[r] = true;
120 for (c = (r == start_row ? start_col : 0);
121 c <= (r == end_row ? end_col : rt->cols - 1);
123 rt->cells[r][c].ch = 0x20;
124 rt->cells[r][c].attr = rt->curattr;
129 /* interprets a 'move cursor' (CUP) escape sequence */
130 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) {
133 rt->crow = rt->ccol = 0;
136 else if (pcount < 2) return; /* malformed */
138 rt->crow = param[0] - 1; /* convert from 1-based to 0-based */
139 rt->ccol = param[1] - 1; /* convert from 1-based to 0-based */
141 rt->curpos_dirty = true;
143 clamp_cursor_to_bounds(rt);
146 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
147 * CPL, CHA, HPR, VPA, VPR, HPA */
148 static void interpret_csi_C(RoteTerm *rt, char verb,
149 int param[], int pcount) {
150 int n = (pcount && param[0] > 0) ? param[0] : 1;
153 case 'A': rt->crow -= n; break;
154 case 'B': case 'e': rt->crow += n; break;
155 case 'C': case 'a': rt->ccol += n; break;
156 case 'D': rt->ccol -= n; break;
157 case 'E': rt->crow += n; rt->ccol = 0; break;
158 case 'F': rt->crow -= n; rt->ccol = 0; break;
159 case 'G': case '`': rt->ccol = param[0] - 1; break;
160 case 'd': rt->crow = param[0] - 1; break;
163 rt->curpos_dirty = true;
164 clamp_cursor_to_bounds(rt);
167 /* Interpret the 'erase line' escape sequence */
168 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) {
169 int erase_start, erase_end, i;
170 int cmd = pcount ? param[0] : 0;
173 case 1: erase_start = 0; erase_end = rt->ccol; break;
174 case 2: erase_start = 0; erase_end = rt->cols - 1; break;
175 default: erase_start = rt->ccol; erase_end = rt->cols - 1; break;
178 for (i = erase_start; i <= erase_end; i++) {
179 rt->cells[rt->crow][i].ch = 0x20;
180 rt->cells[rt->crow][i].attr = rt->curattr;
183 rt->line_dirty[rt->crow] = true;
186 /* Interpret the 'insert blanks' sequence (ICH) */
187 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) {
188 int n = (pcount && param[0] > 0) ? param[0] : 1;
190 for (i = rt->cols - 1; i >= rt->ccol + n; i--)
191 rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
192 for (i = rt->ccol; i < rt->ccol + n; i++) {
193 rt->cells[rt->crow][i].ch = 0x20;
194 rt->cells[rt->crow][i].attr = rt->curattr;
197 rt->line_dirty[rt->crow] = true;
200 /* Interpret the 'delete chars' sequence (DCH) */
201 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount) {
202 int n = (pcount && param[0] > 0) ? param[0] : 1;
204 for (i = rt->ccol; i < rt->cols; i++) {
205 if (i + n < rt->cols)
206 rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
208 rt->cells[rt->crow][i].ch = 0x20;
209 rt->cells[rt->crow][i].attr = rt->curattr;
213 rt->line_dirty[rt->crow] = true;
216 /* Interpret an 'insert line' sequence (IL) */
217 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount) {
218 int n = (pcount && param[0] > 0) ? param[0] : 1;
221 for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--)
222 memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
224 for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
225 rt->line_dirty[i] = true;
226 for (j = 0; j < rt->cols; j++)
227 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
232 /* Interpret a 'delete line' sequence (DL) */
233 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount) {
234 int n = (pcount && param[0] > 0) ? param[0] : 1;
237 for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
238 rt->line_dirty[i] = true;
239 if (i + n <= rt->pd->scrollbottom)
240 memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
242 for (j = 0; j < rt->cols; j++)
243 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
248 /* Interpret an 'erase characters' (ECH) sequence */
249 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount) {
250 int n = (pcount && param[0] > 0) ? param[0] : 1;
253 for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
254 rt->cells[rt->crow][i].ch = 0x20;
255 rt->cells[rt->crow][i].attr = rt->curattr;
258 rt->line_dirty[rt->crow] = true;
261 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
262 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) {
263 int newtop, newbottom;
267 newbottom = rt->rows - 1;
269 else if (pcount < 2) return; /* malformed */
271 newtop = param[0] - 1;
272 newbottom = param[1] - 1;
275 /* clamp to bounds */
276 if (newtop < 0) newtop = 0;
277 if (newtop >= rt->rows) newtop = rt->rows - 1;
278 if (newbottom < 0) newbottom = 0;
279 if (newbottom >= rt->rows) newbottom = rt->rows - 1;
281 /* check for range validity */
282 if (newtop > newbottom) return;
283 rt->pd->scrolltop = newtop;
284 rt->pd->scrollbottom = newbottom;
287 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount) {
288 rt->pd->saved_x = rt->ccol;
289 rt->pd->saved_y = rt->crow;
292 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount) {
293 rt->ccol = rt->pd->saved_x;
294 rt->crow = rt->pd->saved_y;
295 rt->curpos_dirty = true;
298 void rote_es_interpret_csi(RoteTerm *rt) {
299 static int csiparam[MAX_CSI_ES_PARAMS];
301 const char *p = rt->pd->esbuf + 1;
302 char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
304 if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
306 fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
311 /* parse numeric parameters */
312 while ((*p >= '0' && *p <= '9') || *p == ';') {
314 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
315 csiparam[param_count++] = 0;
318 if (param_count == 0) csiparam[param_count++] = 0;
319 csiparam[param_count - 1] *= 10;
320 csiparam[param_count - 1] += *p - '0';
326 /* delegate handling depending on command character (verb) */
328 case 'm': /* it's a 'set attribute' sequence */
329 interpret_csi_SGR(rt, csiparam, param_count); break;
330 case 'J': /* it's an 'erase display' sequence */
331 interpret_csi_ED(rt, csiparam, param_count); break;
332 case 'H': case 'f': /* it's a 'move cursor' sequence */
333 interpret_csi_CUP(rt, csiparam, param_count); break;
334 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
335 case 'e': case 'a': case 'd': case '`':
336 /* it is a 'relative move' */
337 interpret_csi_C(rt, verb, csiparam, param_count); break;
338 case 'K': /* erase line */
339 interpret_csi_EL(rt, csiparam, param_count); break;
340 case '@': /* insert characters */
341 interpret_csi_ICH(rt, csiparam, param_count); break;
342 case 'P': /* delete characters */
343 interpret_csi_DCH(rt, csiparam, param_count); break;
344 case 'L': /* insert lines */
345 interpret_csi_IL(rt, csiparam, param_count); break;
346 case 'M': /* delete lines */
347 interpret_csi_DL(rt, csiparam, param_count); break;
348 case 'X': /* erase chars */
349 interpret_csi_ECH(rt, csiparam, param_count); break;
350 case 'r': /* set scrolling region */
351 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
352 case 's': /* save cursor location */
353 interpret_csi_SAVECUR(rt, csiparam, param_count); break;
354 case 'u': /* restore cursor location */
355 interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
358 fprintf(stderr, "Unrecogized CSI: <%s>\n", rt->pd->esbuf); break;