File: vt.c

package info (click to toggle)
termrec 0.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,148 kB
  • sloc: ansic: 8,430; makefile: 181; perl: 16; sh: 15
file content (317 lines) | stat: -rw-r--r-- 8,118 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
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
#include <stdio.h>
#include <stdarg.h>
#include <wchar.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include "tty.h"
#include "wcwidth.h"

static const char* color_names[16]=
{
    "black", "red", "green", "brown", "blue", "magenta", "cyan", "lt.gray",
    "dk.gray", "lt.red", "lime", "yellow", "lt.blue", "pink", "aqua", "white",
};

static int describe_color(char *b, uint32_t c)
{
    switch (c&VT100_ATTR_COLOR_TYPE)
    {
    case 1<<24:
        return sprintf(b, "%s", color_names[c&0xf]);
    case 2<<24:
        return sprintf(b, "C%u", c&0xff);
    case 3<<24:
        return sprintf(b, "#%06x", c&0xffffff);
    default:
        return sprintf(b, "bad_color:%x", c&VT100_ATTR_COLOR_MASK);
    }
}

static const char* describe_attr(uint64_t attr)
{
    static char buf[128];
    char *b = buf;

    *buf=0;
#define A(f, name) if (attr&VT100_ATTR_##f) b+=sprintf(b, " " name);
    A(BOLD,      "bold");
    A(DIM,       "dim");
    A(ITALIC,    "italic");
    A(UNDERLINE, "underline");
    A(BLINK,     "blink");
    A(INVERSE,   "inverse");
    A(STRIKE,    "strike");
    A(CJK,       "cjk");
#undef A
    if (attr & VT100_ATTR_COLOR_MASK)
        b+=sprintf(b, " fg="), b+=describe_color(b, attr);
    if (attr>>32 & VT100_ATTR_COLOR_MASK)
        b+=sprintf(b, " bg="), b+=describe_color(b, attr>>32);
#define INVALID_ATTRS 0x7000000080000000
    if (attr & INVALID_ATTRS)
        b+=sprintf(b, "bad_attr:%"PRIx64, attr & INVALID_ATTRS);

    return *buf? buf+1 : buf;
}

static void tl_char(tty vt, int x, int y, ucs ch, uint64_t attr, int width)
{
    if (width == 1)
        printf("== char U+%04X attr=%s at %d,%d\n", ch, describe_attr(attr), x, y);
    else if (width == 2)
        printf("== char U+%04X attr=%s at %d,%d, CJK\n", ch, describe_attr(attr), x, y);
    else
        printf("== char U+%04X attr=%s at %d,%d, bad width %d!\n", ch, describe_attr(attr), x, y, width);
}

static void tl_comb(tty vt, int x, int y, ucs ch, uint64_t attr)
{
    printf("== comb U+%04X attr=%s at %d,%d\n", ch, describe_attr(attr), x, y);
}

static void tl_cursor(tty vt, int x, int y)
{
    printf("== cursor to %d,%d\n", x, y);
}

static void tl_clear(tty vt, int x, int y, int len)
{
    printf("== clear from %d,%d len %d\n", x, y, len);
}

static void tl_scroll(tty vt, int nl)
{
    printf("== scroll by %d lines\n", nl);
}

static void tl_flag(tty vt, int f, int v)
{
    printf("== flag %d (%s) set to %d\n", f,
        (v==VT100_FLAG_CURSOR)?"cursor visibility":
        (v==VT100_FLAG_KPAD)?"keypad mode":
        "unknown", v);
}

static void tl_osc(tty vt, int cmd, const char *str)
{
    printf("== osc %d: \"%s\"\n", cmd, str);
}

static void tl_resize(tty vt, int sx, int sy)
{
    printf("== resize to %dx%d\n", sx, sy);
}

static void tl_bell(tty vt)
{
    printf("== bell\n");
}

static void tl_free(tty vt)
{
    printf("== free\n");
}

static void dump(tty vt)
{
    int x,y;
    uint64_t attr;

    if (vt->title)
        x=vt->sx-printf("+===[%s]", vt->title);
    else
        x=vt->sx-printf("+");
    while (x-->=0)
        putchar('=');
    printf("+\n");

    attr=0;
    for (y=0; y<vt->sy; y++)
    {
        printf("|");
        for (x=0; x<vt->sx; x++)
        {
#define SCR vt->scr[x+y*vt->sx]
            if (SCR.attr!=attr)
                printf("{%s}", describe_attr(attr=SCR.attr));
            if (SCR.ch>=' ' && SCR.ch<127)
                printf("%c", SCR.ch);
            else if (SCR.ch == VT100_CJK_RIGHT)
                printf("[CJK]");
            else
                printf("[%04X]", SCR.ch);
            if (SCR.comb)
            {
                int ci=SCR.comb;
                char pref='<';
                while (ci)
                {
                    printf("%c%04X", pref, vt->combs[ci].ch);
                    pref=' ';
                    ci=vt->combs[ci].next;
                }
                printf(">");
            }
        }
        printf("|\n");
    }

    x=vt->sx-printf("+-[ cursor at %d,%d ]", vt->cx, vt->cy);
    while (x-->=0)
        putchar('-');
    printf("+\n");
}

#define SX vt->sx
#define SY vt->sy
#define CX vt->cx
#define CY vt->cy
#define FREECOMB vt->combs[0].next
#define NCOMBS   vt->combs[0].ch

static const char* validate(tty vt)
{
    if (SX<2 || SY<1)
        return "screen too small";
    if (CX<0 || CX>SX || CY<0 || CY>=SY)
        return "cursor out of bounds";

    int SS=SX*SY;
    if (!vt->combs)
    {
        for (int i=0;i<SS;i++)
            if (vt->scr[i].comb)
                return "combining when there should be none";
    }
    else
    {
        uint8_t *tc=calloc(NCOMBS, 1);
        uint32_t j;
        if (!tc)
            return "OUT OF MEMORY";
        tc[0]=1;
        // check used chains
        for (int i=0;i<SS;i++)
            for (j=vt->scr[i].comb; j; j=vt->combs[j].next)
            {
                if (j>=NCOMBS)
                    return free(tc), "combining out of table";
                if (tc[j])
                    return free(tc), "combining chain loop";
                tc[j]=1;
            }
        // check free space
        for (j=FREECOMB; j<NCOMBS; j=vt->combs[j].next)
        {
            if (tc[j])
                return free(tc), "free combining used or looped";
            tc[j]=1;
        }
        if (j!=NCOMBS)
            return free(tc), "bad termination of free combining chain";
        for (j=0; j<NCOMBS; j++)
            if (!tc[j])
                return free(tc), "combining slot neither used nor free";
        free(tc);
    }

    for (int y=0;y<SY;y++)
        for (int x=0;x<SX;x++)
        {
            attrchar *ac = &vt->scr[y*SX+x];
            switch (mk_wcwidth(ac->ch))
            {
            case 0:
                if (ac->ch)
                    return "width 0 character as primary";
                break;
            case 1:
                if (ac->ch == VT100_CJK_RIGHT)
                {
                    if (!(ac->attr & VT100_ATTR_CJK))
                        return "right half but attr says not CJK";
                    if (x<=0)
                        return "right half at left edge of screen";
                    if (!(ac[-1].attr & VT100_ATTR_CJK))
                        return "right half but prev attr not CJK";
                }
                else if (ac->attr & VT100_ATTR_CJK)
                    return "width 1 but attr says CJK";
                break;
            case 2:
                if (!(ac->attr & VT100_ATTR_CJK))
                    return "width 2 but attr says not CJK";
                if (x+1>=SX)
                    return "width 2 at right edge of screen";
                if (ac[1].ch != VT100_CJK_RIGHT)
                    return "width 2 but no right half";
                break;
            default:
                return "control character on screen";
            }
        }

    return 0;
}

#define BUFFER_SIZE 65536

int main(int argc, char **argv)
{
    tty vt;
    char buf[BUFFER_SIZE];
    int len;
    bool dump_flag=false;
    bool abort_flag=false;

    vt = tty_init(20, 5, 1);

    while (1)
        switch (getopt(argc, argv, "eda"))
        {
        case -1:
            goto run;
        case ':':
        case '?':
            exit(1);
        case 'e':
            vt->l_char=tl_char;
            vt->l_comb=tl_comb;
            vt->l_cursor=tl_cursor;
            vt->l_clear=tl_clear;
            vt->l_scroll=tl_scroll;
            vt->l_flag=tl_flag;
            vt->l_osc=tl_osc;
            vt->l_resize=tl_resize;
            vt->l_bell=tl_bell;
            vt->l_free=tl_free;
            break;
        case 'd':
            dump_flag=true;
            break;
        case 'a':
            abort_flag=true;
        }
run:
    while ((len=read(0, buf, BUFFER_SIZE))>0)
    {
        tty_write(vt, buf, len);
        const char *inv=validate(vt);
        if (inv)
        {
            printf("!!! tty invalid: %s !!!\n", inv);
            if (abort_flag)
                abort();
            break;
        }
    }

    if (dump_flag)
        dump(vt);

    tty_free(vt);

    return 0;
}