File: inject.c

package info (click to toggle)
rote 0.2.8-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 332 kB
  • ctags: 110
  • sloc: ansic: 758; makefile: 118; sh: 50
file content (266 lines) | stat: -rw-r--r-- 7,925 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
LICENSE INFORMATION:
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation.

Please refer to the COPYING file for more information.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

Copyright (c) 2004 Bruno T. C. de Oliveira
*/


#include "rote.h"
#include "roteprivate.h"
#include "inject_csi.h"
#include <string.h>

static void cursor_line_down(RoteTerm *rt) {
   int i;
   rt->crow++;
   rt->curpos_dirty = true;
   if (rt->crow <= rt->pd->scrollbottom) return;

   /* must scroll the scrolling region up by 1 line, and put cursor on 
    * last line of it */
   rt->crow = rt->pd->scrollbottom;
   
   for (i = rt->pd->scrolltop; i < rt->pd->scrollbottom; i++) {
      rt->line_dirty[i] = true;
      memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
   }
      
   rt->line_dirty[rt->pd->scrollbottom] = true;

   /* clear last row of the scrolling region */
   for (i = 0; i < rt->cols; i++) {
      rt->cells[rt->pd->scrollbottom][i].ch = 0x20;
      rt->cells[rt->pd->scrollbottom][i].attr = 0x70;
   }

}

static void cursor_line_up(RoteTerm *rt) {
   int i;
   rt->crow--;
   rt->curpos_dirty = true;
   if (rt->crow >= rt->pd->scrolltop) return;

   /* must scroll the scrolling region up by 1 line, and put cursor on 
    * first line of it */
   rt->crow = rt->pd->scrolltop;
   
   for (i = rt->pd->scrollbottom; i > rt->pd->scrolltop; i--) {
      rt->line_dirty[i] = true;
      memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
   }
      
   rt->line_dirty[rt->pd->scrolltop] = true;

   /* clear first row of the scrolling region */
   for (i = 0; i < rt->cols; i++) {
      rt->cells[rt->pd->scrolltop][i].ch = 0x20;
      rt->cells[rt->pd->scrolltop][i].attr = 0x70;
   }

}

static inline void put_normal_char(RoteTerm *rt, char c) {
   if (rt->ccol >= rt->cols) {
      rt->ccol = 0;
      cursor_line_down(rt);
   }

   rt->cells[rt->crow][rt->ccol].ch = c;
   rt->cells[rt->crow][rt->ccol].attr = rt->curattr;
   rt->ccol++;

   rt->line_dirty[rt->crow] = true;
   rt->curpos_dirty = true;
}

static inline void put_graphmode_char(RoteTerm *rt, char c) {
   char nc;
   /* do some very pitiful translation to regular ascii chars */
   switch (c) {
      case 'j': case 'k': case 'l': case 'm': case 'n': case 't': 
                                    case 'u': case 'v': case 'w':
         nc = '+'; break;
      case 'x':
         nc = '|'; break;
      default:
         nc = '%';
   }

   put_normal_char(rt, nc);
}

static inline void new_escape_sequence(RoteTerm *rt) {
   rt->pd->escaped = true;
   rt->pd->esbuf_len = 0;
   rt->pd->esbuf[0] = '\0';
}

static inline void cancel_escape_sequence(RoteTerm *rt) {
   rt->pd->escaped = false;
   rt->pd->esbuf_len = 0;
   rt->pd->esbuf[0] = '\0';
}

static void handle_control_char(RoteTerm *rt, char c) {
   switch (c) {
      case '\r': rt->ccol = 0; break; /* carriage return */
      case '\n':  /* line feed */
         rt->ccol = 0; cursor_line_down(rt);
         rt->curpos_dirty = true;
         break;
      case '\b': /* backspace */
         if (rt->ccol > 0) rt->ccol--;
         rt->curpos_dirty = true;
         break;
      case '\t': /* tab */
         while (rt->ccol % 8) put_normal_char(rt, ' ');
         break;
      case '\x1B': /* begin escape sequence (aborting previous one if any) */
         new_escape_sequence(rt);
         break;
      case '\x0E': /* enter graphical character mode */
         rt->pd->graphmode = true;
         break;
      case '\x0F': /* exit graphical character mode */
         rt->pd->graphmode = false;
         break;
      case '\x9B': /* CSI character. Equivalent to ESC [ */
         new_escape_sequence(rt);
         rt->pd->esbuf[rt->pd->esbuf_len++] = '[';
         break;
      case '\x18': case '\x1A': /* these interrupt escape sequences */
         cancel_escape_sequence(rt);
         break;
      case '\a': /* bell */
         /* do nothing for now... maybe a visual bell would be nice? */
         break;
      #ifdef DEBUG
      default:
         fprintf(stderr, "Unrecognized control char: %d (^%c)\n", c, c + '@');
         break;
      #endif
   }
}

static inline bool is_valid_csi_ender(char c) {
   return (c >= 'a' && c <= 'z') ||
          (c >= 'A' && c <= 'Z') ||
          c == '@' || c == '`';
}

static void try_interpret_escape_seq(RoteTerm *rt) {
   char firstchar = rt->pd->esbuf[0];
   char lastchar  = rt->pd->esbuf[rt->pd->esbuf_len-1];

   if (!firstchar) return;  /* too early to do anything */

   if (rt->pd->handler) {
      /* call custom handler */
      #ifdef DEBUG
      fprintf(stderr, "Calling custom handler for ES <%s>.\n", rt->pd->esbuf);
      #endif

      int answer = (*(rt->pd->handler))(rt, rt->pd->esbuf);
      if (answer == ROTE_HANDLERESULT_OK) {
         /* successfully handled */
         #ifdef DEBUG
         fprintf(stderr, "Handler returned OK. Done with escape sequence.\n");
         #endif

         cancel_escape_sequence(rt);
         return;
      }
      else if (answer == ROTE_HANDLERESULT_NOTYET) {
         /* handler might handle it when more characters are appended to 
          * it. So for now we don't interpret it */
         #ifdef DEBUG
         fprintf(stderr, "Handler returned NOTYET. Waiting for more chars.\n");
         #endif

         return;
      }
   
      /* If we got here then answer == ROTE_HANDLERESULT_NOWAY */
      /* handler said it can't handle that escape sequence,
       * but we can still try handling it ourselves, so 
       * we proceed normally. */
      #ifdef DEBUG
      fprintf(stderr, "Handler returned NOWAY. Trying our handlers.\n");
      #endif
   }

   /* interpret ESC-M as reverse line-feed */
   if (firstchar == 'M') {
      cursor_line_up(rt);
      cancel_escape_sequence(rt);
      return;
   }

   if (firstchar != '[' && firstchar != ']') {
      /* unrecognized escape sequence. Let's forget about it. */
      #ifdef DEBUG
      fprintf(stderr, "Unrecognized ES: <%s>\n", rt->pd->esbuf);
      #endif

      cancel_escape_sequence(rt);
      return;
   }

   if (firstchar == '[' && is_valid_csi_ender(lastchar)) {
      /* we have a csi escape sequence: interpret it */
      rote_es_interpret_csi(rt);
      cancel_escape_sequence(rt);
   }
   else if (firstchar == ']' && lastchar == '\a') {
      /* we have an xterm escape sequence: interpret it */

      /* rote_es_interpret_xterm_es(rt);     -- TODO!*/
      #ifdef DEBUG
      fprintf(stderr, "Ignored XTerm ES.\n");
      #endif
      cancel_escape_sequence(rt);
   }

   /* if the escape sequence took up all available space and could
    * not yet be parsed, abort it */
   if (rt->pd->esbuf_len + 1 >= ESEQ_BUF_SIZE) cancel_escape_sequence(rt);
}
   
void rote_vt_inject(RoteTerm *rt, const char *data, int len) {
   int i;
   for (i = 0; i < len; i++, data++) {
      if (*data == 0) continue;  /* completely ignore NUL */
      if (*data >= 1 && *data <= 31) {
         handle_control_char(rt, *data);
         continue;
      }

      if (rt->pd->escaped && rt->pd->esbuf_len < ESEQ_BUF_SIZE) {
         /* append character to ongoing escape sequence */
         rt->pd->esbuf[rt->pd->esbuf_len] = *data;
         rt->pd->esbuf[++rt->pd->esbuf_len] = 0;

         try_interpret_escape_seq(rt);
      }
      else if (rt->pd->graphmode)
         put_graphmode_char(rt, *data);
      else
         put_normal_char(rt, *data);
   }
}