| 1 |
#define _XOPEN_SOURCE 500 |
| 2 |
|
| 3 |
#include <stdbool.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
#define streq(a,b) (strcmp(a,b)==0) |
| 8 |
|
| 9 |
#include <termios.h> |
| 10 |
|
| 11 |
static char *getvalue(int *argip, int argc, char *argv[]) |
| 12 |
{ |
| 13 |
if(*argip >= argc) { |
| 14 |
fprintf(stderr, "Expected an option value\n"); |
| 15 |
exit(1); |
| 16 |
} |
| 17 |
|
| 18 |
return argv[(*argip)++]; |
| 19 |
} |
| 20 |
|
| 21 |
static int getchoice(int *argip, int argc, char *argv[], const char *options[]) |
| 22 |
{ |
| 23 |
const char *arg = getvalue(argip, argc, argv); |
| 24 |
|
| 25 |
int value = -1; |
| 26 |
while(options[++value]) |
| 27 |
if(streq(arg, options[value])) |
| 28 |
return value; |
| 29 |
|
| 30 |
fprintf(stderr, "Unrecognised option value %s\n", arg); |
| 31 |
exit(1); |
| 32 |
} |
| 33 |
|
| 34 |
typedef enum { |
| 35 |
OFF, |
| 36 |
ON, |
| 37 |
QUERY, |
| 38 |
} BoolQuery; |
| 39 |
|
| 40 |
static BoolQuery getboolq(int *argip, int argc, char *argv[]) |
| 41 |
{ |
| 42 |
return getchoice(argip, argc, argv, (const char *[]){"off", "on", "query", NULL}); |
| 43 |
} |
| 44 |
|
| 45 |
static char *helptext[] = { |
| 46 |
"reset", |
| 47 |
"s8c1t [off|on]", |
| 48 |
"keypad [app|num]", |
| 49 |
"screen [off|on|query]", |
| 50 |
"cursor [off|on|query]", |
| 51 |
"curblink [off|on|query]", |
| 52 |
"curshape [block|under|bar|query]", |
| 53 |
"mouse [off|click|clickdrag|motion]", |
| 54 |
"reportfocus [off|on|query]", |
| 55 |
"altscreen [off|on|query]", |
| 56 |
"bracketpaste [off|on|query]", |
| 57 |
"icontitle [STR]", |
| 58 |
"icon [STR]", |
| 59 |
"title [STR]", |
| 60 |
NULL |
| 61 |
}; |
| 62 |
|
| 63 |
static bool seticanon(bool icanon, bool echo) |
| 64 |
{ |
| 65 |
struct termios termios; |
| 66 |
|
| 67 |
tcgetattr(0, &termios); |
| 68 |
|
| 69 |
bool ret = (termios.c_lflag & ICANON); |
| 70 |
|
| 71 |
if(icanon) termios.c_lflag |= ICANON; |
| 72 |
else termios.c_lflag &= ~ICANON; |
| 73 |
|
| 74 |
if(echo) termios.c_lflag |= ECHO; |
| 75 |
else termios.c_lflag &= ~ECHO; |
| 76 |
|
| 77 |
tcsetattr(0, TCSANOW, &termios); |
| 78 |
|
| 79 |
return ret; |
| 80 |
} |
| 81 |
|
| 82 |
static void await_c1(unsigned char c1) |
| 83 |
{ |
| 84 |
unsigned char c; |
| 85 |
|
| 86 |
|
| 87 |
bool in_esc = false; |
| 88 |
while((c = getchar())) { |
| 89 |
if(c == c1) |
| 90 |
break; |
| 91 |
if(in_esc && c == (char)(c1 - 0x40)) |
| 92 |
break; |
| 93 |
if(!in_esc && c == 0x1b) |
| 94 |
in_esc = true; |
| 95 |
else |
| 96 |
in_esc = false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
static char *read_csi() |
| 101 |
{ |
| 102 |
await_c1(0x9B); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
char csi[32]; |
| 107 |
int i = 0; |
| 108 |
for(; i < sizeof(csi)-1; i++) { |
| 109 |
char c = csi[i] = getchar(); |
| 110 |
if(c >= 0x40 && c <= 0x7e) |
| 111 |
break; |
| 112 |
} |
| 113 |
csi[++i] = 0; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
return strdup(csi); |
| 118 |
} |
| 119 |
|
| 120 |
static char *read_dcs() |
| 121 |
{ |
| 122 |
await_c1(0x90); |
| 123 |
|
| 124 |
char dcs[32]; |
| 125 |
bool in_esc = false; |
| 126 |
int i = 0; |
| 127 |
for(; i < sizeof(dcs)-1; ) { |
| 128 |
char c = getchar(); |
| 129 |
if(c == 0x9c) |
| 130 |
break; |
| 131 |
if(in_esc && c == 0x5c) |
| 132 |
break; |
| 133 |
if(!in_esc && c == 0x1b) |
| 134 |
in_esc = true; |
| 135 |
else { |
| 136 |
dcs[i++] = c; |
| 137 |
in_esc = false; |
| 138 |
} |
| 139 |
} |
| 140 |
dcs[++i] = 0; |
| 141 |
|
| 142 |
return strdup(dcs); |
| 143 |
} |
| 144 |
|
| 145 |
static void usage(int exitcode) |
| 146 |
{ |
| 147 |
fprintf(stderr, "Control a libvterm-based terminal\n" |
| 148 |
"\n" |
| 149 |
"Options:\n"); |
| 150 |
|
| 151 |
for(char **p = helptext; *p; p++) |
| 152 |
fprintf(stderr, " %s\n", *p); |
| 153 |
|
| 154 |
exit(exitcode); |
| 155 |
} |
| 156 |
|
| 157 |
static bool query_dec_mode(int mode) |
| 158 |
{ |
| 159 |
printf("\x1b[?%d$p", mode); |
| 160 |
|
| 161 |
char *s = NULL; |
| 162 |
do { |
| 163 |
if(s) |
| 164 |
free(s); |
| 165 |
s = read_csi(); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
int reply_mode, reply_value; |
| 170 |
char reply_cmd; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if(sscanf(s, "?%d;%d$%c", &reply_mode, &reply_value, &reply_cmd) < 3) |
| 176 |
continue; |
| 177 |
if(reply_cmd != 'y') |
| 178 |
continue; |
| 179 |
|
| 180 |
if(reply_mode != mode) |
| 181 |
continue; |
| 182 |
|
| 183 |
free(s); |
| 184 |
|
| 185 |
if(reply_value == 1 || reply_value == 3) |
| 186 |
return true; |
| 187 |
if(reply_value == 2 || reply_value == 4) |
| 188 |
return false; |
| 189 |
|
| 190 |
printf("Unrecognised reply to DECRQM: %d\n", reply_value); |
| 191 |
return false; |
| 192 |
} while(1); |
| 193 |
} |
| 194 |
|
| 195 |
static void do_dec_mode(int mode, BoolQuery val, const char *name) |
| 196 |
{ |
| 197 |
switch(val) { |
| 198 |
case OFF: |
| 199 |
case ON: |
| 200 |
printf("\x1b[?%d%c", mode, val == ON ? 'h' : 'l'); |
| 201 |
break; |
| 202 |
|
| 203 |
case QUERY: |
| 204 |
if(query_dec_mode(mode)) |
| 205 |
printf("%s on\n", name); |
| 206 |
else |
| 207 |
printf("%s off\n", name); |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static int query_rqss_numeric(char *cmd) |
| 213 |
{ |
| 214 |
printf("\x1bP$q%s\x1b\\", cmd); |
| 215 |
|
| 216 |
char *s = NULL; |
| 217 |
do { |
| 218 |
if(s) |
| 219 |
free(s); |
| 220 |
s = read_dcs(); |
| 221 |
|
| 222 |
if(!s) |
| 223 |
return -1; |
| 224 |
if(strlen(s) < strlen(cmd)) |
| 225 |
return -1; |
| 226 |
if(strcmp(s + strlen(s) - strlen(cmd), cmd) != 0) { |
| 227 |
printf("No match\n"); |
| 228 |
continue; |
| 229 |
} |
| 230 |
|
| 231 |
if(s[0] != '1' || s[1] != '$' || s[2] != 'r') |
| 232 |
return -1; |
| 233 |
|
| 234 |
int num; |
| 235 |
if(sscanf(s + 3, "%d", &num) != 1) |
| 236 |
return -1; |
| 237 |
|
| 238 |
return num; |
| 239 |
} while(1); |
| 240 |
} |
| 241 |
|
| 242 |
bool wasicanon; |
| 243 |
|
| 244 |
void restoreicanon(void) |
| 245 |
{ |
| 246 |
seticanon(wasicanon, true); |
| 247 |
} |
| 248 |
|
| 249 |
int main(int argc, char *argv[]) |
| 250 |
{ |
| 251 |
int argi = 1; |
| 252 |
|
| 253 |
if(argc == 1) |
| 254 |
usage(0); |
| 255 |
|
| 256 |
wasicanon = seticanon(false, false); |
| 257 |
atexit(restoreicanon); |
| 258 |
|
| 259 |
while(argi < argc) { |
| 260 |
const char *arg = argv[argi++]; |
| 261 |
|
| 262 |
if(streq(arg, "reset")) { |
| 263 |
printf("\x1b" "c"); |
| 264 |
} |
| 265 |
else if(streq(arg, "s8c1t")) { |
| 266 |
switch(getchoice(&argi, argc, argv, (const char *[]){"off", "on", NULL})) { |
| 267 |
case 0: |
| 268 |
printf("\x1b F"); break; |
| 269 |
case 1: |
| 270 |
printf("\x1b G"); break; |
| 271 |
} |
| 272 |
} |
| 273 |
else if(streq(arg, "keypad")) { |
| 274 |
switch(getchoice(&argi, argc, argv, (const char *[]){"app", "num", NULL})) { |
| 275 |
case 0: |
| 276 |
printf("\x1b="); break; |
| 277 |
case 1: |
| 278 |
printf("\x1b>"); break; |
| 279 |
} |
| 280 |
} |
| 281 |
else if(streq(arg, "screen")) { |
| 282 |
do_dec_mode(5, getboolq(&argi, argc, argv), "screen"); |
| 283 |
} |
| 284 |
else if(streq(arg, "cursor")) { |
| 285 |
do_dec_mode(25, getboolq(&argi, argc, argv), "cursor"); |
| 286 |
} |
| 287 |
else if(streq(arg, "curblink")) { |
| 288 |
do_dec_mode(12, getboolq(&argi, argc, argv), "curblink"); |
| 289 |
} |
| 290 |
else if(streq(arg, "curshape")) { |
| 291 |
|
| 292 |
|
| 293 |
int shape = getchoice(&argi, argc, argv, (const char *[]){"block", "under", "bar", "query", NULL}); |
| 294 |
switch(shape) { |
| 295 |
case 3: |
| 296 |
shape = query_rqss_numeric(" q"); |
| 297 |
switch(shape) { |
| 298 |
case 1: case 2: |
| 299 |
printf("curshape block\n"); |
| 300 |
break; |
| 301 |
case 3: case 4: |
| 302 |
printf("curshape under\n"); |
| 303 |
break; |
| 304 |
case 5: case 6: |
| 305 |
printf("curshape bar\n"); |
| 306 |
break; |
| 307 |
} |
| 308 |
break; |
| 309 |
|
| 310 |
case 0: |
| 311 |
case 1: |
| 312 |
case 2: |
| 313 |
printf("\x1b[%d q", 1 + (shape * 2)); |
| 314 |
break; |
| 315 |
} |
| 316 |
} |
| 317 |
else if(streq(arg, "mouse")) { |
| 318 |
switch(getchoice(&argi, argc, argv, (const char *[]){"off", "click", "clickdrag", "motion", NULL})) { |
| 319 |
case 0: |
| 320 |
printf("\x1b[?1000l"); break; |
| 321 |
case 1: |
| 322 |
printf("\x1b[?1000h"); break; |
| 323 |
case 2: |
| 324 |
printf("\x1b[?1002h"); break; |
| 325 |
case 3: |
| 326 |
printf("\x1b[?1003h"); break; |
| 327 |
} |
| 328 |
} |
| 329 |
else if(streq(arg, "reportfocus")) { |
| 330 |
do_dec_mode(1004, getboolq(&argi, argc, argv), "reportfocus"); |
| 331 |
} |
| 332 |
else if(streq(arg, "altscreen")) { |
| 333 |
do_dec_mode(1049, getboolq(&argi, argc, argv), "altscreen"); |
| 334 |
} |
| 335 |
else if(streq(arg, "bracketpaste")) { |
| 336 |
do_dec_mode(2004, getboolq(&argi, argc, argv), "bracketpaste"); |
| 337 |
} |
| 338 |
else if(streq(arg, "icontitle")) { |
| 339 |
printf("\x1b]0;%s\a", getvalue(&argi, argc, argv)); |
| 340 |
} |
| 341 |
else if(streq(arg, "icon")) { |
| 342 |
printf("\x1b]1;%s\a", getvalue(&argi, argc, argv)); |
| 343 |
} |
| 344 |
else if(streq(arg, "title")) { |
| 345 |
printf("\x1b]2;%s\a", getvalue(&argi, argc, argv)); |
| 346 |
} |
| 347 |
else { |
| 348 |
fprintf(stderr, "Unrecognised command %s\n", arg); |
| 349 |
exit(1); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
return 0; |
| 354 |
} |