Debian Codegraph @

libvterm/bin/vterm-dump.c

raw symbols SCIP
1 // Require getopt(3)
2 #define _XOPEN_SOURCE
3
4 #include <stdio.h>
5 #include <string.h>
6 #define streq(a,b) (strcmp(a,b)==0)
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 #include "vterm.h"
15
16 static const char *special_begin = "{";
17 static const char *special_end = "}";
18
19 static int parser_text(const char bytes[], size_t len, void *user)
20 {
21 unsigned char *b = (unsigned char *)bytes;
22
23 int i;
24 for(i = 0; i < len; /* none */) {
25 if(b[i] < 0x20) // C0
26 break;
27 else if(b[i] < 0x80) // ASCII
28 i++;
29 else if(b[i] < 0xa0) // C1
30 break;
31 else if(b[i] < 0xc0) // UTF-8 continuation
32 break;
33 else if(b[i] < 0xe0) { // UTF-8 2-byte
34 // 2-byte UTF-8
35 if(len < i+2) break;
36 i += 2;
37 }
38 else if(b[i] < 0xf0) { // UTF-8 3-byte
39 if(len < i+3) break;
40 i += 3;
41 }
42 else if(b[i] < 0xf8) { // UTF-8 4-byte
43 if(len < i+4) break;
44 i += 4;
45 }
46 else // otherwise invalid
47 break;
48 }
49
50 printf("%.*s", i, b);
51 return i;
52 }
53
54 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
55 static const char *name_c0[] = {
56 "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "LS0", "LS1",
57 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
58 };
59 static const char *name_c1[] = {
60 NULL, NULL, "BPH", "NBH", NULL, "NEL", "SSA", "ESA", "HTS", "HTJ", "VTS", "PLD", "PLU", "RI", "SS2", "SS3",
61 "DCS", "PU1", "PU2", "STS", "CCH", "MW", "SPA", "EPA", "SOS", NULL, "SCI", "CSI", "ST", "OSC", "PM", "APC",
62 };
63
64 static int parser_control(unsigned char control, void *user)
65 {
66 if(control < 0x20)
67 printf("%s%s%s", special_begin, name_c0[control], special_end);
68 else if(control >= 0x80 && control < 0xa0 && name_c1[control - 0x80])
69 printf("%s%s%s", special_begin, name_c1[control - 0x80], special_end);
70 else
71 printf("%sCONTROL 0x%02x%s", special_begin, control, special_end);
72
73 if(control == 0x0a)
74 printf("\n");
75 return 1;
76 }
77
78 static int parser_escape(const char bytes[], size_t len, void *user)
79 {
80 if(bytes[0] >= 0x20 && bytes[0] < 0x30) {
81 if(len < 2)
82 return -1;
83 len = 2;
84 }
85 else {
86 len = 1;
87 }
88
89 printf("%sESC %.*s%s", special_begin, (int)len, bytes, special_end);
90
91 return len;
92 }
93
94 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
95 static const char *name_csi_plain[] = {
96 "ICH", "CUU", "CUD", "CUF", "CUB", "CNL", "CPL", "CHA", "CUP", "CHT", "ED", "EL", "IL", "DL", "EF", "EA",
97 "DCH", "SSE", "CPR", "SU", "SD", "NP", "PP", "CTC", "ECH", "CVT", "CBT", "SRS", "PTX", "SDS", "SIMD",NULL,
98 "HPA", "HPR", "REP", "DA", "VPA", "VPR", "HVP", "TBC", "SM", "MC", "HPB", "VPB", "RM", "SGR", "DSR", "DAQ",
99 };
100
101 /*0 4 8 B */
102 static const int newline_csi_plain[] = {
103 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
106 };
107
108 static int parser_csi(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user)
109 {
110 const char *name = NULL;
111 if(!leader && !intermed && command < 0x70)
112 name = name_csi_plain[command - 0x40];
113 else if(leader && streq(leader, "?") && !intermed) {
114 /* DEC */
115 switch(command) {
116 case 'h': name = "DECSM"; break;
117 case 'l': name = "DECRM"; break;
118 }
119 if(name)
120 leader = NULL;
121 }
122
123 if(!leader && !intermed && command < 0x70 && newline_csi_plain[command - 0x40])
124 printf("\n");
125
126 if(name)
127 printf("%s%s", special_begin, name);
128 else
129 printf("%sCSI", special_begin);
130
131 if(leader && leader[0])
132 printf(" %s", leader);
133
134 for(int i = 0; i < argcount; i++) {
135 printf(i ? "," : " ");
136
137 if(args[i] == CSI_ARG_MISSING)
138 printf("*");
139 else {
140 while(CSI_ARG_HAS_MORE(args[i]))
141 printf("%ld+", CSI_ARG(args[i++]));
142 printf("%ld", CSI_ARG(args[i]));
143 }
144 }
145
146 if(intermed && intermed[0])
147 printf(" %s", intermed);
148
149 if(name)
150 printf("%s", special_end);
151 else
152 printf(" %c%s", command, special_end);
153
154 return 1;
155 }
156
157 static int parser_osc(const char *command, size_t cmdlen, void *user)
158 {
159 printf("%sOSC %.*s%s", special_begin, (int)cmdlen, command, special_end);
160
161 return 1;
162 }
163
164 static int parser_dcs(const char *command, size_t cmdlen, void *user)
165 {
166 printf("%sDCS %.*s%s", special_begin, (int)cmdlen, command, special_end);
167
168 return 1;
169 }
170
171 static VTermParserCallbacks parser_cbs = {
172 .text = &parser_text,
173 .control = &parser_control,
174 .escape = &parser_escape,
175 .csi = &parser_csi,
176 .osc = &parser_osc,
177 .dcs = &parser_dcs,
178 };
179
180 int main(int argc, char *argv[])
181 {
182 int use_colour = isatty(1);
183
184 int opt;
185 while((opt = getopt(argc, argv, "c")) != -1) {
186 switch(opt) {
187 case 'c': use_colour = 1; break;
188 }
189 }
190
191 const char *file = argv[optind++];
192
193 int fd;
194 if(!file || streq(file, "-"))
195 fd = 0; // stdin
196 else {
197 fd = open(file, O_RDONLY);
198 if(fd == -1) {
199 fprintf(stderr, "Cannot open %s - %s\n", file, strerror(errno));
200 exit(1);
201 }
202 }
203
204 if(use_colour) {
205 special_begin = "\x1b[7m{";
206 special_end = "}\x1b[m";
207 }
208
209 /* Size matters not for the parser */
210 VTerm *vt = vterm_new(25, 80);
211 vterm_set_utf8(vt, 1);
212 vterm_parser_set_callbacks(vt, &parser_cbs, NULL);
213
214 int len;
215 char buffer[1024];
216 while((len = read(fd, buffer, sizeof(buffer))) > 0) {
217 vterm_input_write(vt, buffer, len);
218 }
219
220 printf("\n");
221
222 close(fd);
223 vterm_free(vt);
224
225 return 0;
226 }