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"
28 #define MAX_CSI_ES_PARAMS 32
30 /* interprets a 'set attribute' (SGR) CSI escape sequence */
31 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount) {
35 /* special case: reset attributes */
40 for (i = 0; i < pcount; i++) {
42 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
43 // 0 All attributes off
49 // 10 The ASCII character set is the current 7-bit
50 // display character set (default) - SCO Console only.
51 // 11 Map Hex 00-7F of the PC character set codes
52 // to the current 7-bit display character set
53 // - SCO Console only.
54 // 12 Map Hex 80-FF of the current character set to
55 // the current 7-bit display character set - SCO
60 // 27 Negative image off
61 // 28 Invisible image off
63 if (param[i] == 0) rt->curattr = 0x70;
64 else if (param[i] == 1 || param[i] == 2 || param[i] == 4) /* set bold */
65 ROTE_ATTR_MOD_BOLD(rt->curattr,1);
66 else if (param[i] == 5) /* set blink */
67 ROTE_ATTR_MOD_BLINK(rt->curattr,1);
68 else if (param[i] == 7 || param[i] == 27) { /* reverse video */
69 int fg = ROTE_ATTR_FG(rt->curattr);
70 int bg = ROTE_ATTR_BG(rt->curattr);
71 ROTE_ATTR_MOD_FG(rt->curattr, bg);
72 ROTE_ATTR_MOD_BG(rt->curattr, fg);
74 else if (param[i] == 8) rt->curattr = 0x0; /* invisible */
75 else if (param[i] == 22 || param[i] == 24) /* bold off */
76 ROTE_ATTR_MOD_BOLD(rt->curattr,0);
77 else if (param[i] == 25) /* blink off */
78 ROTE_ATTR_MOD_BLINK(rt->curattr,0);
79 else if (param[i] == 28) /* invisible off */
81 else if (param[i] >= 30 && param[i] <= 37) /* set fg */
82 ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
83 else if (param[i] >= 40 && param[i] <= 47) /* set bg */
84 ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
85 else if (param[i] == 39) /* reset foreground to default */
86 ROTE_ATTR_MOD_FG(rt->curattr, 7);
87 else if (param[i] == 49) /* reset background to default */
88 ROTE_ATTR_MOD_BG(rt->curattr, 0);
92 /* interprets an 'erase display' (ED) escape sequence */
93 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) {
95 int start_row, start_col, end_row, end_col;
98 if (pcount && param[0] == 2)
99 start_row = 0, start_col = 0, end_row = rt->rows - 1,
100 end_col = rt->cols - 1;
102 else if (pcount && param[0] == 1)
103 start_row = 0, start_col = 0, end_row = rt->crow,
106 else start_row = rt->crow, start_col = rt->ccol,
107 end_row = rt->rows - 1, end_col = rt->cols - 1;
110 for (r = start_row; r <= end_row; r++) {
111 rt->line_dirty[r] = true;
113 for (c = (r == start_row ? start_col : 0);
114 c <= (r == end_row ? end_col : rt->cols - 1);
116 rt->cells[r][c].ch = 0x20;
117 rt->cells[r][c].attr = rt->curattr;
122 /* interprets a 'move cursor' (CUP) escape sequence */
123 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) {
126 rt->crow = rt->ccol = 0;
129 else if (pcount < 2) return; /* malformed */
131 rt->crow = param[0] - 1; /* convert from 1-based to 0-based */
132 rt->ccol = param[1] - 1; /* convert from 1-based to 0-based */
134 rt->curpos_dirty = true;
136 clamp_cursor_to_bounds(rt);
139 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
140 * CPL, CHA, HPR, VPA, VPR, HPA */
141 static void interpret_csi_C(RoteTerm *rt, char verb,
142 int param[], int pcount) {
143 int n = (pcount && param[0] > 0) ? param[0] : 1;
146 case 'A': rt->crow -= n; break;
147 case 'B': case 'e': rt->crow += n; break;
148 case 'C': case 'a': rt->ccol += n; break;
149 case 'D': rt->ccol -= n; break;
150 case 'E': rt->crow += n; rt->ccol = 0; break;
151 case 'F': rt->crow -= n; rt->ccol = 0; break;
152 case 'G': case '`': rt->ccol = param[0] - 1; break;
153 case 'd': rt->crow = param[0] - 1; break;
156 rt->curpos_dirty = true;
157 clamp_cursor_to_bounds(rt);
160 /* Interpret the 'erase line' escape sequence */
161 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) {
162 int erase_start, erase_end, i;
163 int cmd = pcount ? param[0] : 0;
166 case 1: erase_start = 0; erase_end = rt->ccol; break;
167 case 2: erase_start = 0; erase_end = rt->cols - 1; break;
168 default: erase_start = rt->ccol; erase_end = rt->cols - 1; break;
171 for (i = erase_start; i <= erase_end; i++) {
172 rt->cells[rt->crow][i].ch = 0x20;
173 rt->cells[rt->crow][i].attr = rt->curattr;
176 rt->line_dirty[rt->crow] = true;
179 /* Interpret the 'insert blanks' sequence (ICH) */
180 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) {
181 int n = (pcount && param[0] > 0) ? param[0] : 1;
183 for (i = rt->cols - 1; i >= rt->ccol + n; i--)
184 rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
185 for (i = rt->ccol; i < rt->ccol + n; i++) {
186 rt->cells[rt->crow][i].ch = 0x20;
187 rt->cells[rt->crow][i].attr = rt->curattr;
190 rt->line_dirty[rt->crow] = true;
193 /* Interpret the 'delete chars' sequence (DCH) */
194 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount) {
195 int n = (pcount && param[0] > 0) ? param[0] : 1;
197 for (i = rt->ccol; i < rt->cols; i++) {
198 if (i + n < rt->cols)
199 rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
201 rt->cells[rt->crow][i].ch = 0x20;
202 rt->cells[rt->crow][i].attr = rt->curattr;
206 rt->line_dirty[rt->crow] = true;
209 /* Interpret an 'insert line' sequence (IL) */
210 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount) {
211 int n = (pcount && param[0] > 0) ? param[0] : 1;
214 for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--)
215 memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
217 for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
218 rt->line_dirty[i] = true;
219 for (j = 0; j < rt->cols; j++)
220 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
225 /* Interpret a 'delete line' sequence (DL) */
226 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount) {
227 int n = (pcount && param[0] > 0) ? param[0] : 1;
230 for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
231 rt->line_dirty[i] = true;
232 if (i + n <= rt->pd->scrollbottom)
233 memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
235 for (j = 0; j < rt->cols; j++)
236 rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
241 /* Interpret an 'erase characters' (ECH) sequence */
242 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount) {
243 int n = (pcount && param[0] > 0) ? param[0] : 1;
246 for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
247 rt->cells[rt->crow][i].ch = 0x20;
248 rt->cells[rt->crow][i].attr = rt->curattr;
251 rt->line_dirty[rt->crow] = true;
254 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
255 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) {
256 int newtop, newbottom;
260 newbottom = rt->rows - 1;
262 else if (pcount < 2) return; /* malformed */
264 newtop = param[0] - 1;
265 newbottom = param[1] - 1;
268 /* clamp to bounds */
269 if (newtop < 0) newtop = 0;
270 if (newtop >= rt->rows) newtop = rt->rows - 1;
271 if (newbottom < 0) newbottom = 0;
272 if (newbottom >= rt->rows) newbottom = rt->rows - 1;
274 /* check for range validity */
275 if (newtop > newbottom) return;
276 rt->pd->scrolltop = newtop;
277 rt->pd->scrollbottom = newbottom;
280 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount) {
281 rt->pd->saved_x = rt->ccol;
282 rt->pd->saved_y = rt->crow;
285 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount) {
286 rt->ccol = rt->pd->saved_x;
287 rt->crow = rt->pd->saved_y;
288 rt->curpos_dirty = true;
291 void rote_es_interpret_csi(RoteTerm *rt) {
292 static int csiparam[MAX_CSI_ES_PARAMS];
294 const char *p = rt->pd->esbuf + 1;
295 char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
297 if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
299 fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
304 /* parse numeric parameters */
305 while ((*p >= '0' && *p <= '9') || *p == ';') {
307 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
308 csiparam[param_count++] = 0;
311 if (param_count == 0) csiparam[param_count++] = 0;
312 csiparam[param_count - 1] *= 10;
313 csiparam[param_count - 1] += *p - '0';
319 /* delegate handling depending on command character (verb) */
322 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
326 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
329 case 'm': /* it's a 'set attribute' sequence */
330 interpret_csi_SGR(rt, csiparam, param_count); break;
331 case 'J': /* it's an 'erase display' sequence */
332 interpret_csi_ED(rt, csiparam, param_count); break;
333 case 'H': case 'f': /* it's a 'move cursor' sequence */
334 interpret_csi_CUP(rt, csiparam, param_count); break;
335 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
336 case 'e': case 'a': case 'd': case '`':
337 /* it is a 'relative move' */
338 interpret_csi_C(rt, verb, csiparam, param_count); break;
339 case 'K': /* erase line */
340 interpret_csi_EL(rt, csiparam, param_count); break;
341 case '@': /* insert characters */
342 interpret_csi_ICH(rt, csiparam, param_count); break;
343 case 'P': /* delete characters */
344 interpret_csi_DCH(rt, csiparam, param_count); break;
345 case 'L': /* insert lines */
346 interpret_csi_IL(rt, csiparam, param_count); break;
347 case 'M': /* delete lines */
348 interpret_csi_DL(rt, csiparam, param_count); break;
349 case 'X': /* erase chars */
350 interpret_csi_ECH(rt, csiparam, param_count); break;
351 case 'r': /* set scrolling region */
352 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
353 case 's': /* save cursor location */
354 interpret_csi_SAVECUR(rt, csiparam, param_count); break;
355 case 'u': /* restore cursor location */
356 interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
359 fprintf(stderr, "Unrecogized CSI: verb=%c <%s>\n",
360 verb, rt->pd->esbuf);