Limited number of iterations in rote_vt_update to avoid infinite loop with ever-avail...
[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) 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 */
85         rt->curattr = 0x70;
86       else if (param[i] == 28) /* invisible off */
87         rt->curattr = 0x70;
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);
96    }
97 }
98
99 /* interprets an 'erase display' (ED) escape sequence */
100 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) {
101    int r, c;
102    int start_row, start_col, end_row, end_col;
103
104    /* decide range */
105    if (pcount && param[0] == 2) 
106                       start_row = 0, start_col = 0, end_row = rt->rows - 1,
107                       end_col = rt->cols - 1;
108
109    else if (pcount && param[0] == 1)
110                       start_row = 0, start_col = 0, end_row = rt->crow,
111                       end_col = rt->ccol;
112
113    else start_row = rt->crow, start_col = rt->ccol,
114         end_row = rt->rows - 1, end_col = rt->cols - 1;
115
116    /* clean range */
117    for (r = start_row; r <= end_row; r++) {
118       rt->line_dirty[r] = true;
119
120       for (c = (r == start_row ? start_col : 0);
121                              c <= (r == end_row ? end_col : rt->cols - 1);
122                              c++) {
123          rt->cells[r][c].ch = 0x20;
124          rt->cells[r][c].attr = rt->curattr;
125       }
126    }
127 }
128
129 /* interprets a 'move cursor' (CUP) escape sequence */
130 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) {
131    if (pcount == 0) {
132       /* special case */
133       rt->crow = rt->ccol = 0;
134       return;
135    }
136    else if (pcount < 2) return;  /* malformed */
137
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 */
140
141    rt->curpos_dirty = true;
142
143    clamp_cursor_to_bounds(rt);
144 }
145
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;
151
152    switch (verb) {
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;
161    }
162
163    rt->curpos_dirty = true;
164    clamp_cursor_to_bounds(rt);
165 }
166
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;
171
172    switch (cmd) {
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;
176    }
177
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;
181    }
182
183    rt->line_dirty[rt->crow] = true;
184 }
185
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; 
189    int i;
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;
195    }
196
197    rt->line_dirty[rt->crow] = true;
198 }
199
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; 
203    int i;
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];
207      else {
208          rt->cells[rt->crow][i].ch = 0x20;
209          rt->cells[rt->crow][i].attr = rt->curattr;
210      }
211    }
212
213    rt->line_dirty[rt->crow] = true;
214 }
215
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;
219    int i, j;
220
221    for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) 
222       memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
223
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;
228    }
229
230 }
231
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;
235    int i, j;
236
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);
241       else {
242          for (j = 0; j < rt->cols; j++)
243             rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
244       }
245    }
246 }
247
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;
251    int i;
252
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;
256    }
257
258    rt->line_dirty[rt->crow] = true;
259 }
260         
261 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
262 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) {
263    int newtop, newbottom;
264    
265    if (!pcount) {
266       newtop = 0;
267       newbottom = rt->rows - 1;
268    }
269    else if (pcount < 2) return; /* malformed */
270    else {
271       newtop = param[0] - 1;
272       newbottom = param[1] - 1;
273    }
274
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;
280
281    /* check for range validity */
282    if (newtop > newbottom) return;
283    rt->pd->scrolltop = newtop;
284    rt->pd->scrollbottom = newbottom;
285 }
286          
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;
290 }
291
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;
296 }
297
298 void rote_es_interpret_csi(RoteTerm *rt) {
299    static int csiparam[MAX_CSI_ES_PARAMS];
300    int param_count = 0;
301    const char *p = rt->pd->esbuf + 1;
302    char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
303
304    if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
305       #ifdef DEBUG
306       fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
307       #endif
308       return; 
309    }
310
311    /* parse numeric parameters */
312    while ((*p >= '0' && *p <= '9') || *p == ';') {
313       if (*p == ';') {
314          if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
315          csiparam[param_count++] = 0;
316       }
317       else {
318          if (param_count == 0) csiparam[param_count++] = 0;
319          csiparam[param_count - 1] *= 10;
320          csiparam[param_count - 1] += *p - '0';
321       }
322
323       p++;
324    }
325
326    /* delegate handling depending on command character (verb) */
327    switch (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;
356       #ifdef DEBUG
357       default:
358          fprintf(stderr, "Unrecogized CSI: <%s>\n", rt->pd->esbuf); break;
359       #endif
360    }
361 }
362