1
/* $NetBSD: tty.c,v 1.28 2009/02/06 19:53:23 sketch Exp $ */
1
/* $NetBSD: tty.c,v 1.41 2011/10/04 15:27:04 christos Exp $ */
4
4
* Copyright (c) 1992, 1993
57
59
typedef struct ttymap_t {
58
int nch, och; /* Internal and termio rep of chars */
60
Int nch, och; /* Internal and termio rep of chars */
59
61
el_action_t bind[3]; /* emacs, vi, and vi-cmd */
494
496
if (el->el_flags & EDIT_DISABLED)
499
if (!isatty(el->el_outfd)) {
501
(void) fprintf(el->el_errfile,
502
"tty_setup: isatty: %s\n", strerror(errno));
503
#endif /* DEBUG_TTY */
497
506
if (tty_getty(el, &el->el_tty.t_ed) == -1) {
499
508
(void) fprintf(el->el_errfile,
500
509
"tty_setup: tty_getty: %s\n", strerror(errno));
501
510
#endif /* DEBUG_TTY */
504
513
el->el_tty.t_ts = el->el_tty.t_ex = el->el_tty.t_ed;
549
558
"tty_setup: tty_setty: %s\n",
550
559
strerror(errno));
551
560
#endif /* DEBUG_TTY */
557
tty__setchar(&el->el_tty.t_ex, el->el_tty.t_c[EX_IO]);
560
565
el->el_tty.t_ed.c_iflag &= ~el->el_tty.t_t[ED_IO][MD_INP].t_clrmask;
561
566
el->el_tty.t_ed.c_iflag |= el->el_tty.t_t[ED_IO][MD_INP].t_setmask;
582
587
el->el_tty.t_vdisable = _POSIX_VDISABLE;
583
588
(void) memcpy(el->el_tty.t_t, ttyperm, sizeof(ttyperm_t));
584
589
(void) memcpy(el->el_tty.t_c, ttychar, sizeof(ttychar_t));
585
return (tty_setup(el));
590
return tty_setup(el);
893
898
unsigned char *t_n = el->el_tty.t_c[ED_IO];
894
899
unsigned char *t_o = el->el_tty.t_ed.c_cc;
895
unsigned char new[2], old[2];
896
901
const ttymap_t *tp;
897
902
el_action_t *map, *alt;
898
903
const el_action_t *dmap, *dalt;
911
for (tp = tty_map; tp->nch != -1; tp++) {
916
for (tp = tty_map; tp->nch != (Int)-1; tp++) {
912
917
new[0] = t_n[tp->nch];
913
918
old[0] = t_o[tp->och];
914
919
if (new[0] == old[0] && !force)
916
921
/* Put the old default binding back, and set the new binding */
917
key_clear(el, map, (char *)old);
918
map[old[0]] = dmap[old[0]];
919
key_clear(el, map, (char *)new);
922
keymacro_clear(el, map, old);
923
map[UC(old[0])] = dmap[UC(old[0])];
924
keymacro_clear(el, map, new);
920
925
/* MAP_VI == 1, MAP_EMACS == 0... */
921
map[new[0]] = tp->bind[el->el_map.type];
926
map[UC(new[0])] = tp->bind[el->el_map.type];
923
key_clear(el, alt, (char *)old);
924
alt[old[0]] = dalt[old[0]];
925
key_clear(el, alt, (char *)new);
926
alt[new[0]] = tp->bind[el->el_map.type + 1];
928
keymacro_clear(el, alt, old);
929
alt[UC(old[0])] = dalt[UC(old[0])];
930
keymacro_clear(el, alt, new);
931
alt[UC(new[0])] = tp->bind[el->el_map.type + 1];
939
944
if (el->el_tty.t_mode == ED_IO || el->el_tty.t_mode == QU_IO)
942
947
if (el->el_flags & EDIT_DISABLED)
945
950
if (tty_getty(el, &el->el_tty.t_ts) == -1) {
947
952
(void) fprintf(el->el_errfile, "tty_rawmode: tty_getty: %s\n",
948
953
strerror(errno));
949
954
#endif /* DEBUG_TTY */
953
958
* We always keep up with the eight bit setting and the speed of the
954
* tty. But only we only believe changes that are made to cooked mode!
959
* tty. But we only believe changes that are made to cooked mode!
956
961
el->el_tty.t_eight = tty__geteightbit(&el->el_tty.t_ts);
957
962
el->el_tty.t_speed = tty__getspeed(&el->el_tty.t_ts);
1092
1097
{ /* set tty in normal setup */
1094
1099
if (el->el_tty.t_mode == EX_IO)
1097
1102
if (el->el_flags & EDIT_DISABLED)
1100
1105
if (tty_setty(el, TCSADRAIN, &el->el_tty.t_ex) == -1) {
1101
1106
#ifdef DEBUG_TTY
1155
1160
if (el->el_tty.t_mode != QU_IO)
1157
1162
if (tty_setty(el, TCSADRAIN, &el->el_tty.t_ed) == -1) {
1158
1163
#ifdef DEBUG_TTY
1159
1164
(void) fprintf(el->el_errfile, "QuoteModeOff: tty_setty: %s\n",
1160
1165
strerror(errno));
1161
1166
#endif /* DEBUG_TTY */
1164
1169
el->el_tty.t_mode = ED_IO;
1174
tty_stty(EditLine *el, int argc __attribute__((__unused__)), const char **argv)
1179
tty_stty(EditLine *el, int argc __attribute__((__unused__)), const Char **argv)
1176
1181
const ttymodes_t *m;
1185
char name[EL_BUFSIZ];
1181
1186
struct termios *tios = &el->el_tty.t_ex;
1184
1189
if (argv == NULL)
1191
strncpy(name, ct_encode_string(*argv++, &el->el_scratch), sizeof(name));
1192
name[sizeof(name) - 1] = '\0';
1188
1194
while (argv && *argv && argv[0][0] == '-' && argv[0][2] == '\0')
1189
1195
switch (argv[0][1]) {
1210
1216
(void) fprintf(el->el_errfile,
1211
1217
"%s: Unknown switch `%c'.\n",
1212
1218
name, argv[0][1]);
1216
1222
if (!argv || !*argv) {
1218
int len = 0, st = 0, cu;
1224
size_t len = 0, st = 0, cu;
1219
1225
for (m = ttymodes; m->m_name; m++) {
1220
1226
if (m->m_type != i) {
1221
1227
(void) fprintf(el->el_outfile, "%s%s",
1239
1246
cu = strlen(m->m_name) + (x != '\0') + 1;
1241
if (len + cu >= el->el_term.t_size.h) {
1248
if (len + cu >= (size_t)el->el_terminal.t_size.h) {
1242
1249
(void) fprintf(el->el_outfile, "\n%*s",
1256
1263
(void) fprintf(el->el_outfile, "\n");
1259
1266
while (argv && (s = *argv++)) {
1272
1279
for (m = ttymodes; m->m_name; m++)
1273
if ((p ? strncmp(m->m_name, d, (size_t)(p - d)) :
1274
strcmp(m->m_name, d)) == 0 &&
1280
if ((p ? strncmp(m->m_name, ct_encode_string(d, &el->el_scratch), (size_t)(p - d)) :
1281
strcmp(m->m_name, ct_encode_string(d, &el->el_scratch))) == 0 &&
1275
1282
(p == NULL || m->m_type == MD_CHAR))
1278
1285
if (!m->m_name) {
1279
1286
(void) fprintf(el->el_errfile,
1280
"%s: Invalid argument `%s'.\n", name, d);
1287
"%s: Invalid argument `" FSTR "'.\n", name, d);
1284
1291
int c = ffs((int)m->m_value);
1285
int v = *++p ? parse__escape((const char **) &p) :
1292
int v = *++p ? parse__escape(&p) :
1286
1293
el->el_tty.t_vdisable;
1288
1296
c = tty__getcharindex(c);
1289
1297
assert(c != -1);
1298
tios->c_cc[c] = (cc_t)v;