Applied Phil Endecott's patch to improve reverse-video escape-sequence handling
[apps/madtty.git] / inject_csi.c
1 /*
2 LICENSE INFORMATION:
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.
6
7 Please refer to the COPYING file for more information.
8
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.
13
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
17
18 Copyright (c) 2004 Bruno T. C. de Oliveira
19 */
20
21
22 #include "inject_csi.h"
23 #include "roteprivate.h"
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define MAX_CSI_ES_PARAMS 32
28    
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;
32
33    if (rt->crow >= rt->rows) 
34       rt->curpos_dirty = true, rt->crow = rt->rows - 1;
35
36    if (rt->ccol >= rt->cols)
37       rt->curpos_dirty = true, rt->ccol = rt->cols - 1;
38 }
39
40 /* interprets a 'set attribute' (SGR) CSI escape sequence */
41 static void interpret_csi_SGR(RoteTerm *rt, int param[], int pcount) {
42    int i;
43
44    if (pcount == 0) {
45       /* special case: reset attributes */
46       rt->curattr = 0x70;
47       return;
48    }
49
50    for (i = 0; i < pcount; i++) {
51
52 // From http://vt100.net/docs/vt510-rm/SGR table 5-16
53 // 0    All attributes off
54 // 1    Bold
55 // 4    Underline
56 // 5    Blinking
57 // 7    Negative image
58 // 8    Invisible image
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
66 //      Console only.
67 // 22   Bold off
68 // 24   Underline off
69 // 25   Blinking off
70 // 27   Negative image off
71 // 28   Invisible image off
72
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 || param[i] == 27) { /* reverse video */
79         int fg = ROTE_ATTR_FG(rt->curattr);
80         int bg = ROTE_ATTR_BG(rt->curattr);
81         ROTE_ATTR_MOD_FG(rt->curattr, bg);
82         ROTE_ATTR_MOD_BG(rt->curattr, fg);
83       }
84       else if (param[i] == 8) rt->curattr = 0x0;    /* invisible */
85       else if (param[i] == 22 || param[i] == 24) /* bold off */
86         ROTE_ATTR_MOD_BOLD(rt->curattr,0);
87       else if (param[i] == 25) /* blink off */
88         ROTE_ATTR_MOD_BLINK(rt->curattr,0);
89       else if (param[i] == 28) /* invisible off */
90         rt->curattr = 0x70;
91       else if (param[i] >= 30 && param[i] <= 37)    /* set fg */
92          ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
93       else if (param[i] >= 40 && param[i] <= 47)    /* set bg */
94          ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
95       else if (param[i] == 39)  /* reset foreground to default */
96          ROTE_ATTR_MOD_FG(rt->curattr, 7);
97       else if (param[i] == 49)  /* reset background to default */
98          ROTE_ATTR_MOD_BG(rt->curattr, 0);
99    }
100 }
101
102 /* interprets an 'erase display' (ED) escape sequence */
103 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) {
104    int r, c;
105    int start_row, start_col, end_row, end_col;
106
107    /* decide range */
108    if (pcount && param[0] == 2) 
109                       start_row = 0, start_col = 0, end_row = rt->rows - 1,
110                       end_col = rt->cols - 1;
111
112    else if (pcount && param[0] == 1)
113                       start_row = 0, start_col = 0, end_row = rt->crow,
114                       end_col = rt->ccol;
115
116    else start_row = rt->crow, start_col = rt->ccol,
117         end_row = rt->rows - 1, end_col = rt->cols - 1;
118
119    /* clean range */
120    for (r = start_row; r <= end_row; r++) {
121       rt->line_dirty[r] = true;
122
123       for (c = (r == start_row ? start_col : 0);
124                              c <= (r == end_row ? end_col : rt->cols - 1);
125                              c++) {
126          rt->cells[r][c].ch = 0x20;
127          rt->cells[r][c].attr = rt->curattr;
128       }
129    }
130 }
131
132 /* interprets a 'move cursor' (CUP) escape sequence */
133 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) {
134    if (pcount == 0) {
135       /* special case */
136       rt->crow = rt->ccol = 0;
137       return;
138    }
139    else if (pcount < 2) return;  /* malformed */
140
141    rt->crow = param[0] - 1;  /* convert from 1-based to 0-based */
142    rt->ccol = param[1] - 1;  /* convert from 1-based to 0-based */
143
144    rt->curpos_dirty = true;
145
146    clamp_cursor_to_bounds(rt);
147 }
148
149 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
150  * CPL, CHA, HPR, VPA, VPR, HPA */
151 static void interpret_csi_C(RoteTerm *rt, char verb, 
152                                                 int param[], int pcount) {
153    int n = (pcount && param[0] > 0) ? param[0] : 1;
154
155    switch (verb) {
156       case 'A':           rt->crow -= n; break;
157       case 'B': case 'e': rt->crow += n; break;
158       case 'C': case 'a': rt->ccol += n; break;
159       case 'D':           rt->ccol -= n; break;
160       case 'E':           rt->crow += n; rt->ccol = 0; break;
161       case 'F':           rt->crow -= n; rt->ccol = 0; break;
162       case 'G': case '`': rt->ccol  = param[0] - 1; break;
163       case 'd':           rt->crow  = param[0] - 1; break;
164    }
165
166    rt->curpos_dirty = true;
167    clamp_cursor_to_bounds(rt);
168 }
169
170 /* Interpret the 'erase line' escape sequence */
171 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) {
172    int erase_start, erase_end, i;
173    int cmd = pcount ? param[0] : 0;
174
175    switch (cmd) {
176       case 1:  erase_start = 0;           erase_end = rt->ccol;     break;
177       case 2:  erase_start = 0;           erase_end = rt->cols - 1; break;
178       default: erase_start = rt->ccol;    erase_end = rt->cols - 1; break;
179    }
180
181    for (i = erase_start; i <= erase_end; i++) {
182       rt->cells[rt->crow][i].ch = 0x20; 
183       rt->cells[rt->crow][i].attr = rt->curattr;
184    }
185
186    rt->line_dirty[rt->crow] = true;
187 }
188
189 /* Interpret the 'insert blanks' sequence (ICH) */
190 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) {
191    int n = (pcount && param[0] > 0) ? param[0] : 1; 
192    int i;
193    for (i = rt->cols - 1; i >= rt->ccol + n; i--)
194       rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
195    for (i = rt->ccol; i < rt->ccol + n; i++) {
196       rt->cells[rt->crow][i].ch = 0x20;
197       rt->cells[rt->crow][i].attr = rt->curattr;
198    }
199
200    rt->line_dirty[rt->crow] = true;
201 }
202
203 /* Interpret the 'delete chars' sequence (DCH) */
204 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount) {
205    int n = (pcount && param[0] > 0) ? param[0] : 1; 
206    int i;
207    for (i = rt->ccol; i < rt->cols; i++) {
208      if (i + n < rt->cols)
209          rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
210      else {
211          rt->cells[rt->crow][i].ch = 0x20;
212          rt->cells[rt->crow][i].attr = rt->curattr;
213      }
214    }
215
216    rt->line_dirty[rt->crow] = true;
217 }
218
219 /* Interpret an 'insert line' sequence (IL) */
220 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount) {
221    int n = (pcount && param[0] > 0) ? param[0] : 1;
222    int i, j;
223
224    for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) 
225       memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
226
227    for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
228       rt->line_dirty[i] = true;
229       for (j = 0; j < rt->cols; j++) 
230          rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
231    }
232
233 }
234
235 /* Interpret a 'delete line' sequence (DL) */
236 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount) {
237    int n = (pcount && param[0] > 0) ? param[0] : 1;
238    int i, j;
239
240    for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
241       rt->line_dirty[i] = true;
242       if (i + n <= rt->pd->scrollbottom)
243          memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
244       else {
245          for (j = 0; j < rt->cols; j++)
246             rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
247       }
248    }
249 }
250
251 /* Interpret an 'erase characters' (ECH) sequence */
252 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount) {
253    int n = (pcount && param[0] > 0) ? param[0] : 1;
254    int i;
255
256    for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
257       rt->cells[rt->crow][i].ch = 0x20;
258       rt->cells[rt->crow][i].attr = rt->curattr;
259    }
260
261    rt->line_dirty[rt->crow] = true;
262 }
263         
264 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
265 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) {
266    int newtop, newbottom;
267    
268    if (!pcount) {
269       newtop = 0;
270       newbottom = rt->rows - 1;
271    }
272    else if (pcount < 2) return; /* malformed */
273    else {
274       newtop = param[0] - 1;
275       newbottom = param[1] - 1;
276    }
277
278    /* clamp to bounds */
279    if (newtop < 0) newtop = 0;
280    if (newtop >= rt->rows) newtop = rt->rows - 1;
281    if (newbottom < 0) newbottom = 0;
282    if (newbottom >= rt->rows) newbottom = rt->rows - 1;
283
284    /* check for range validity */
285    if (newtop > newbottom) return;
286    rt->pd->scrolltop = newtop;
287    rt->pd->scrollbottom = newbottom;
288 }
289          
290 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount) {
291    rt->pd->saved_x = rt->ccol;
292    rt->pd->saved_y = rt->crow;
293 }
294
295 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount) {
296    rt->ccol = rt->pd->saved_x;
297    rt->crow = rt->pd->saved_y;
298    rt->curpos_dirty = true;
299 }
300
301 void rote_es_interpret_csi(RoteTerm *rt) {
302    static int csiparam[MAX_CSI_ES_PARAMS];
303    int param_count = 0;
304    const char *p = rt->pd->esbuf + 1;
305    char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
306
307    if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
308       #ifdef DEBUG
309       fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
310       #endif
311       return; 
312    }
313
314    /* parse numeric parameters */
315    while ((*p >= '0' && *p <= '9') || *p == ';') {
316       if (*p == ';') {
317          if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
318          csiparam[param_count++] = 0;
319       }
320       else {
321          if (param_count == 0) csiparam[param_count++] = 0;
322          csiparam[param_count - 1] *= 10;
323          csiparam[param_count - 1] += *p - '0';
324       }
325
326       p++;
327    }
328
329    /* delegate handling depending on command character (verb) */
330    switch (verb) {
331       case 'm': /* it's a 'set attribute' sequence */
332          interpret_csi_SGR(rt, csiparam, param_count); break;
333       case 'J': /* it's an 'erase display' sequence */
334          interpret_csi_ED(rt, csiparam, param_count); break;
335       case 'H': case 'f': /* it's a 'move cursor' sequence */
336          interpret_csi_CUP(rt, csiparam, param_count); break;
337       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
338       case 'e': case 'a': case 'd': case '`':
339          /* it is a 'relative move' */
340          interpret_csi_C(rt, verb, csiparam, param_count); break;
341       case 'K': /* erase line */
342          interpret_csi_EL(rt, csiparam, param_count); break;
343       case '@': /* insert characters */
344          interpret_csi_ICH(rt, csiparam, param_count); break;
345       case 'P': /* delete characters */
346          interpret_csi_DCH(rt, csiparam, param_count); break;
347       case 'L': /* insert lines */
348          interpret_csi_IL(rt, csiparam, param_count); break;
349       case 'M': /* delete lines */
350          interpret_csi_DL(rt, csiparam, param_count); break;
351       case 'X': /* erase chars */
352          interpret_csi_ECH(rt, csiparam, param_count); break;
353       case 'r': /* set scrolling region */
354          interpret_csi_DECSTBM(rt, csiparam, param_count); break;
355       case 's': /* save cursor location */
356          interpret_csi_SAVECUR(rt, csiparam, param_count); break;
357       case 'u': /* restore cursor location */
358          interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
359       #ifdef DEBUG
360       default:
361          fprintf(stderr, "Unrecogized CSI: <%s>\n", rt->pd->esbuf); break;
362       #endif
363    }
364 }
365