Added copyright notices to files
[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       if (param[i] == 0) rt->curattr = 0x70;
52       else if (param[i] == 1 || param[i] == 2 || param[i] == 4)  /* set bold */
53          rt->curattr |= 0x80;
54       else if (param[i] == 5) rt->curattr |= 0x08;  /* set blink */
55       else if (param[i] == 7) rt->curattr = 0x07;   /* reverse video */
56       else if (param[i] >= 30 && param[i] <= 37)    /* set fg */
57          ROTE_ATTR_MOD_FG(rt->curattr, param[i] - 30);
58       else if (param[i] >= 40 && param[i] <= 47)    /* set bg */
59          ROTE_ATTR_MOD_BG(rt->curattr, param[i] - 40);
60       else if (param[i] == 39)  /* reset foreground to default */
61          ROTE_ATTR_MOD_FG(rt->curattr, 7);
62       else if (param[i] == 49)  /* reset background to default */
63          ROTE_ATTR_MOD_BG(rt->curattr, 0);
64    }
65 }
66
67 /* interprets an 'erase display' (ED) escape sequence */
68 static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) {
69    int r, c;
70    int start_row, start_col, end_row, end_col;
71
72    /* decide range */
73    if (pcount && param[0] == 2) 
74                       start_row = 0, start_col = 0, end_row = rt->rows - 1,
75                       end_col = rt->cols - 1;
76
77    else if (pcount && param[0] == 1)
78                       start_row = 0, start_col = 0, end_row = rt->crow,
79                       end_col = rt->ccol;
80
81    else start_row = rt->crow, start_col = rt->ccol,
82         end_row = rt->rows - 1, end_col = rt->cols - 1;
83
84    /* clean range */
85    for (r = start_row; r <= end_row; r++) {
86       rt->line_dirty[r] = true;
87
88       for (c = (r == start_row ? start_col : 0);
89                              c <= (r == end_row ? end_col : rt->cols - 1);
90                              c++) {
91          rt->cells[r][c].ch = 0x20;
92          rt->cells[r][c].attr = rt->curattr;
93       }
94    }
95 }
96
97 /* interprets a 'move cursor' (CUP) escape sequence */
98 static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) {
99    if (pcount == 0) {
100       /* special case */
101       rt->crow = rt->ccol = 0;
102       return;
103    }
104    else if (pcount < 2) return;  /* malformed */
105
106    rt->crow = param[0] - 1;  /* convert from 1-based to 0-based */
107    rt->ccol = param[1] - 1;  /* convert from 1-based to 0-based */
108
109    rt->curpos_dirty = true;
110
111    clamp_cursor_to_bounds(rt);
112 }
113
114 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
115  * CPL, CHA, HPR, VPA, VPR, HPA */
116 static void interpret_csi_C(RoteTerm *rt, char verb, 
117                                                 int param[], int pcount) {
118    int n = (pcount && param[0] > 0) ? param[0] : 1;
119
120    switch (verb) {
121       case 'A':           rt->crow -= n; break;
122       case 'B': case 'e': rt->crow += n; break;
123       case 'C': case 'a': rt->ccol += n; break;
124       case 'D':           rt->ccol -= n; break;
125       case 'E':           rt->crow += n; rt->ccol = 0; break;
126       case 'F':           rt->crow -= n; rt->ccol = 0; break;
127       case 'G': case '`': rt->ccol  = param[0] - 1; break;
128       case 'd':           rt->crow  = param[0] - 1; break;
129    }
130
131    rt->curpos_dirty = true;
132    clamp_cursor_to_bounds(rt);
133 }
134
135 /* Interpret the 'erase line' escape sequence */
136 static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) {
137    int erase_start, erase_end, i;
138    int cmd = pcount ? param[0] : 0;
139
140    switch (cmd) {
141       case 1:  erase_start = 0;           erase_end = rt->ccol;     break;
142       case 2:  erase_start = 0;           erase_end = rt->cols - 1; break;
143       default: erase_start = rt->ccol;    erase_end = rt->cols - 1; break;
144    }
145
146    for (i = erase_start; i <= erase_end; i++) {
147       rt->cells[rt->crow][i].ch = 0x20; 
148       rt->cells[rt->crow][i].attr = rt->curattr;
149    }
150
151    rt->line_dirty[rt->crow] = true;
152 }
153
154 /* Interpret the 'insert blanks' sequence (ICH) */
155 static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) {
156    int n = (pcount && param[0] > 0) ? param[0] : 1; 
157    int i;
158    for (i = rt->cols - 1; i >= rt->ccol + n; i--)
159       rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n];
160    for (i = rt->ccol; i < rt->ccol + n; i++) {
161       rt->cells[rt->crow][i].ch = 0x20;
162       rt->cells[rt->crow][i].attr = rt->curattr;
163    }
164
165    rt->line_dirty[rt->crow] = true;
166 }
167
168 /* Interpret the 'delete chars' sequence (DCH) */
169 static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount) {
170    int n = (pcount && param[0] > 0) ? param[0] : 1; 
171    int i;
172    for (i = rt->ccol; i < rt->cols; i++) {
173      if (i + n < rt->cols)
174          rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n];
175      else {
176          rt->cells[rt->crow][i].ch = 0x20;
177          rt->cells[rt->crow][i].attr = rt->curattr;
178      }
179    }
180
181    rt->line_dirty[rt->crow] = true;
182 }
183
184 /* Interpret an 'insert line' sequence (IL) */
185 static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount) {
186    int n = (pcount && param[0] > 0) ? param[0] : 1;
187    int i, j;
188
189    for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) 
190       memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
191
192    for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) {
193       rt->line_dirty[i] = true;
194       for (j = 0; j < rt->cols; j++) 
195          rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
196    }
197
198 }
199
200 /* Interpret a 'delete line' sequence (DL) */
201 static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount) {
202    int n = (pcount && param[0] > 0) ? param[0] : 1;
203    int i, j;
204
205    for (i = rt->crow; i <= rt->pd->scrollbottom; i++) {
206       rt->line_dirty[i] = true;
207       if (i + n <= rt->pd->scrollbottom)
208          memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
209       else {
210          for (j = 0; j < rt->cols; j++)
211             rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr;
212       }
213    }
214 }
215
216 /* Interpret an 'erase characters' (ECH) sequence */
217 static void interpret_csi_ECH(RoteTerm *rt, int param[], int pcount) {
218    int n = (pcount && param[0] > 0) ? param[0] : 1;
219    int i;
220
221    for (i = rt->ccol; i < rt->ccol + n && i < rt->cols; i++) {
222       rt->cells[rt->crow][i].ch = 0x20;
223       rt->cells[rt->crow][i].attr = rt->curattr;
224    }
225
226    rt->line_dirty[rt->crow] = true;
227 }
228         
229 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
230 static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) {
231    int newtop, newbottom;
232    
233    if (!pcount) {
234       newtop = 0;
235       newbottom = rt->rows - 1;
236    }
237    else if (pcount < 2) return; /* malformed */
238    else {
239       newtop = param[0] - 1;
240       newbottom = param[1] - 1;
241    }
242
243    /* clamp to bounds */
244    if (newtop < 0) newtop = 0;
245    if (newtop >= rt->rows) newtop = rt->rows - 1;
246    if (newbottom < 0) newbottom = 0;
247    if (newbottom >= rt->rows) newbottom = rt->rows - 1;
248
249    /* check for range validity */
250    if (newtop > newbottom) return;
251    rt->pd->scrolltop = newtop;
252    rt->pd->scrollbottom = newbottom;
253 }
254          
255 static void interpret_csi_SAVECUR(RoteTerm *rt, int param[], int pcount) {
256    rt->pd->saved_x = rt->ccol;
257    rt->pd->saved_y = rt->crow;
258 }
259
260 static void interpret_csi_RESTORECUR(RoteTerm *rt, int param[], int pcount) {
261    rt->ccol = rt->pd->saved_x;
262    rt->crow = rt->pd->saved_y;
263    rt->curpos_dirty = true;
264 }
265
266 void rote_es_interpret_csi(RoteTerm *rt) {
267    static int csiparam[MAX_CSI_ES_PARAMS];
268    int param_count = 0;
269    const char *p = rt->pd->esbuf + 1;
270    char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1];
271
272    if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
273       #ifdef DEBUG
274       fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf);
275       #endif
276       return; 
277    }
278
279    /* parse numeric parameters */
280    while ((*p >= '0' && *p <= '9') || *p == ';') {
281       if (*p == ';') {
282          if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
283          csiparam[param_count++] = 0;
284       }
285       else {
286          if (param_count == 0) csiparam[param_count++] = 0;
287          csiparam[param_count - 1] *= 10;
288          csiparam[param_count - 1] += *p - '0';
289       }
290
291       p++;
292    }
293
294    /* delegate handling depending on command character (verb) */
295    switch (verb) {
296       case 'm': /* it's a 'set attribute' sequence */
297          interpret_csi_SGR(rt, csiparam, param_count); break;
298       case 'J': /* it's an 'erase display' sequence */
299          interpret_csi_ED(rt, csiparam, param_count); break;
300       case 'H': case 'f': /* it's a 'move cursor' sequence */
301          interpret_csi_CUP(rt, csiparam, param_count); break;
302       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
303       case 'e': case 'a': case 'd': case '`':
304          /* it is a 'relative move' */
305          interpret_csi_C(rt, verb, csiparam, param_count); break;
306       case 'K': /* erase line */
307          interpret_csi_EL(rt, csiparam, param_count); break;
308       case '@': /* insert characters */
309          interpret_csi_ICH(rt, csiparam, param_count); break;
310       case 'P': /* delete characters */
311          interpret_csi_DCH(rt, csiparam, param_count); break;
312       case 'L': /* insert lines */
313          interpret_csi_IL(rt, csiparam, param_count); break;
314       case 'M': /* delete lines */
315          interpret_csi_DL(rt, csiparam, param_count); break;
316       case 'X': /* erase chars */
317          interpret_csi_ECH(rt, csiparam, param_count); break;
318       case 'r': /* set scrolling region */
319          interpret_csi_DECSTBM(rt, csiparam, param_count); break;
320       case 's': /* save cursor location */
321          interpret_csi_SAVECUR(rt, csiparam, param_count); break;
322       case 'u': /* restore cursor location */
323          interpret_csi_RESTORECUR(rt, csiparam, param_count); break;
324       #ifdef DEBUG
325       default:
326          fprintf(stderr, "Unrecogized CSI: <%s>\n", rt->pd->esbuf); break;
327       #endif
328    }
329 }
330