aboutsummaryrefslogtreecommitdiff
path: root/vt.c
blob: da211e8c9f8dd7254fb594b5b99407d1bfe256be (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "vt.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define LOG_MODULE "vt"
#define LOG_ENABLE_DBG 1
#include "log.h"
#include "csi.h"
#include "osc.h"
#include "grid.h"

/* https://vt100.net/emu/dec_ansi_parser */

enum state {
    STATE_SAME,       /* For state_transition */

    STATE_ANYWHERE,
    STATE_ESCAPE,
    STATE_GROUND,
    STATE_CSIENTRY,
    STATE_CSIPARAM,
    STATE_OSCSTRING,

    STATE_UTF8,
};

enum action {
    ACTION_NONE,      /* For state_transition */

    ACTION_IGNORE,
    ACTION_CLEAR,
    ACTION_EXECUTE,
    ACTION_PRINT,
    ACTION_PARAM,
    ACTION_COLLECT,
    ACTION_CSIDISPATCH,
    ACTION_OSCSTART,
    ACTION_OSCEND,
    ACTION_OSCPUT,

    ACTION_UTF8,
};

static const char *const state_names[] = {
    [STATE_SAME] = "no change",
    [STATE_ANYWHERE] = "anywhere",
    [STATE_ESCAPE] = "escape",
    [STATE_GROUND] = "ground",
    [STATE_CSIENTRY] = "CSI entry",
    [STATE_CSIPARAM] = "CSI param",
    [STATE_OSCSTRING] = "OSC string",

    [STATE_UTF8] = "UTF-8",
};

static const char *const action_names[] __attribute__((unused)) = {
    [ACTION_NONE] = "no action",
    [ACTION_IGNORE] = "ignore",
    [ACTION_CLEAR] = "clear",
    [ACTION_EXECUTE] = "execute",
    [ACTION_PRINT] = "print",
    [ACTION_PARAM] = "param",
    [ACTION_COLLECT] = "collect",
    [ACTION_CSIDISPATCH] = "CSI dispatch",
    [ACTION_OSCSTART] = "OSC start",
    [ACTION_OSCEND] = "OSC end",
    [ACTION_OSCPUT] = "OSC put",

    [ACTION_UTF8] = "begin UTF-8",
};

struct state_transition {
    enum action action;
    enum state state;
};

static const struct state_transition state_anywhere[256] = {
    [0x1b] = {.state = STATE_ESCAPE},
};

static const struct state_transition state_ground[256] = {
    [0x00 ... 0x17] = {.action = ACTION_EXECUTE},
    [0x20 ... 0x7f] = {.action = ACTION_PRINT},
    [0xc2 ... 0xdf] = {.action = ACTION_UTF8, .state = STATE_UTF8}, /* 2 chars */
    [0xe0 ... 0xef] = {.action = ACTION_UTF8, .state = STATE_UTF8}, /* 3 chars */
    [0xf0 ... 0xf4] = {.action = ACTION_UTF8, .state = STATE_UTF8}, /* 4 chars */
};

static const struct state_transition state_escape[256] = {
    [0x5b] = {.state = STATE_CSIENTRY},
    [0x5d] = {.state = STATE_OSCSTRING},
};

static const struct state_transition state_csientry[256] = {
    [0x30 ... 0x39] = {.action = ACTION_PARAM, .state = STATE_CSIPARAM},
    [0x3b] = {.action = ACTION_PARAM, .state = STATE_CSIPARAM},
    [0x3c ... 0x3f] = {.action = ACTION_COLLECT, .state = STATE_CSIPARAM},
    [0x40 ... 0x7e] = {.action = ACTION_CSIDISPATCH, .state = STATE_GROUND},
    [0x6d] = {.action = ACTION_CSIDISPATCH, .state = STATE_GROUND},
};

static const struct state_transition state_csiparam[256] = {
    [0x30 ... 0x39] = {.action = ACTION_PARAM},
    [0x3b] = {.action = ACTION_PARAM},
    [0x40 ... 0x7e] = {.action = ACTION_CSIDISPATCH, .state = STATE_GROUND},
    [0x6d] = {.action = ACTION_CSIDISPATCH, .state = STATE_GROUND},
};

static const struct state_transition state_ocsstring[256] = {
    [0x07] = {.state = STATE_GROUND},  /* Not in diagram */
    [0x20 ... 0x7f] = {.action = ACTION_OSCPUT},
};

static const struct state_transition* states[] = {
    [STATE_ANYWHERE] = state_anywhere,
    [STATE_ESCAPE] = state_escape,
    [STATE_GROUND] = state_ground,
    [STATE_CSIENTRY] = state_csientry,
    [STATE_CSIPARAM] = state_csiparam,
    [STATE_OSCSTRING] = state_ocsstring,
};

static const enum action entry_actions[] = {
    [STATE_SAME] = ACTION_NONE,
    [STATE_ANYWHERE] = ACTION_NONE,
    [STATE_ESCAPE] = ACTION_NONE,
    [STATE_GROUND] = ACTION_NONE,
    [STATE_CSIENTRY] = ACTION_CLEAR,
    [STATE_CSIPARAM] = ACTION_NONE,
    [STATE_OSCSTRING] = ACTION_OSCSTART,
    [STATE_UTF8] = ACTION_NONE,
};

static const enum action exit_actions[] = {
    [STATE_SAME] = ACTION_NONE,
    [STATE_ANYWHERE] = ACTION_NONE,
    [STATE_ESCAPE] = ACTION_NONE,
    [STATE_GROUND] = ACTION_NONE,
    [STATE_CSIENTRY] = ACTION_NONE,
    [STATE_CSIPARAM] = ACTION_NONE,
    [STATE_OSCSTRING] = ACTION_OSCEND,
    [STATE_UTF8] = ACTION_NONE,
};

static bool
action(struct terminal *term, enum action action, uint8_t c)
{
    switch (action) {
    case ACTION_NONE:
        break;

    case ACTION_IGNORE:
        break;

    case ACTION_EXECUTE:
        LOG_DBG("execute: 0x%02x", c);
        switch (c) {
        case '\r':
            grid_cursor_left(&term->grid, term->grid.cursor.col);
            break;

        case '\b':
            grid_cursor_left(&term->grid, 1);
            break;
        }

        return true;

    case ACTION_CLEAR:
        memset(&term->vt.params, 0, sizeof(term->vt.params));
        memset(&term->vt.intermediates, 0, sizeof(term->vt.intermediates));
        memset(&term->vt.osc, 0, sizeof(term->vt.osc));
        memset(&term->vt.utf8, 0, sizeof(term->vt.utf8));
        break;

    case ACTION_PRINT: {
        if (term->grid.print_needs_wrap)
            grid_cursor_to(&term->grid, term->grid.cursor.row + 1, 0);

        struct cell *cell = &term->grid.cells[term->grid.linear_cursor];

        cell->dirty = true;

        if (term->vt.utf8.idx > 0) {
            //LOG_DBG("print: UTF8: %.*s", (int)term->vt.utf8.idx, term->vt.utf8.data);
            memcpy(cell->c, term->vt.utf8.data, term->vt.utf8.idx);
            cell->c[term->vt.utf8.idx] = '\0';
        } else {
            //LOG_DBG("print: ASCII: %c", c);
            cell->c[0] = c;
            cell->c[1] = '\0';
        }

        cell->attrs = term->vt.attrs;

        if (term->grid.cursor.col < term->grid.cols - 1)
            grid_cursor_right(&term->grid, 1);
        else
            term->grid.print_needs_wrap = true;

        term->grid.dirty = true;
        break;
    }

    case ACTION_PARAM:{
        if (term->vt.params.idx == 0)
            term->vt.params.idx = 1;

        if (c == ';') {
            term->vt.params.idx++;
        } else if (c == ':') {
            if (term->vt.params.v[term->vt.params.idx - 1].sub.idx == 0)
                term->vt.params.v[term->vt.params.idx - 1].sub.idx = 1;
        } else {
            if (term->vt.params.v[term->vt.params.idx - 1].sub.idx > 0)
                term->vt.params.v[term->vt.params.idx - 1].sub.value[term->vt.params.v[term->vt.params.idx - 1].sub.idx] *= 10;
            else
                term->vt.params.v[term->vt.params.idx - 1].value *= 10;

            if (term->vt.params.v[term->vt.params.idx - 1].sub.idx > 0)
                term->vt.params.v[term->vt.params.idx - 1].sub.value[term->vt.params.v[term->vt.params.idx - 1].sub.idx] += c - '0';
            else
                term->vt.params.v[term->vt.params.idx - 1].value += c - '0';
        }
        break;
                }

    case ACTION_COLLECT:
        LOG_DBG("collect");
        term->vt.intermediates.data[term->vt.intermediates.idx++] = c;
        break;

    case ACTION_CSIDISPATCH:
        return csi_dispatch(term, c);

    case ACTION_OSCSTART:
        term->vt.osc.idx = 0;
        break;

    case ACTION_OSCPUT:
        term->vt.osc.data[term->vt.osc.idx++] = c;
        break;

    case ACTION_OSCEND:
        return osc_dispatch(term);

    case ACTION_UTF8:
        term->vt.utf8.idx = 0;
        if (c >= 0x2c && c <= 0xdf)
            term->vt.utf8.left = 2;
        else if (c >= 0xe0 && c <= 0xef)
            term->vt.utf8.left = 3;
        else
            term->vt.utf8.left = 4;
        //LOG_DBG("begin UTF-8 (%zu chars)", term->vt.utf8.left);
        term->vt.utf8.data[term->vt.utf8.idx++] = c;
        term->vt.utf8.left--;
        break;
    }

    return true;
}

static bool
process_utf8(struct terminal *term, uint8_t c)
{
    //LOG_DBG("UTF-8: 0x%02x", c);
    term->vt.utf8.data[term->vt.utf8.idx++] = c;
    term->vt.utf8.left--;

    if (term->vt.utf8.left == 0)
        term->vt.state = STATE_GROUND;
    return true;
}

void
vt_from_slave(struct terminal *term, const uint8_t *data, size_t len)
{
    //int cursor = term->grid.cursor;
    for (size_t i = 0; i < len; i++) {
        //LOG_DBG("input: 0x%02x", data[i]);
        enum state current_state = term->vt.state;

        const struct state_transition *transition = &state_anywhere[data[i]];
        if (transition->action == ACTION_NONE && transition->state == STATE_SAME) {
            if (current_state == STATE_UTF8) {
                if (!process_utf8(term, data[i]))
                    abort();
                if (current_state == STATE_UTF8)
                    continue;
                if (!action(term, ACTION_PRINT, 0))
                    abort();
                continue;
            }

            transition = &states[current_state][data[i]];
            if (transition->action == ACTION_NONE && transition->state == STATE_SAME) {
                LOG_ERR("unimplemented transition from %s: 0x%02x",
                        state_names[current_state], data[i]);
                abort();
            }
        }

        if (transition->state != STATE_SAME) {
            enum action exit_action = exit_actions[current_state];
            if (exit_action != ACTION_NONE && !action(term, exit_action, data[i]))
                abort();
        }

        if (!action(term, transition->action, data[i]))
            abort();

        if (transition->state != STATE_SAME) {
            /*
             * LOG_DBG("transition: %s -> %s", state_names[current_state],
             *         state_names[transition->state]);
             */
            term->vt.state = transition->state;

            enum action entry_action = entry_actions[transition->state];
            if (entry_action != ACTION_NONE && !action(term, entry_action, data[i]))
                abort();
        }
    }
}