~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« back to all changes in this revision

Viewing changes to cmd-line-utils/libedit/tty.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 $        */
2
2
 
3
3
/*-
4
4
 * Copyright (c) 1992, 1993
45
45
 */
46
46
#include <assert.h>
47
47
#include <errno.h>
 
48
#include <unistd.h>     /* for isatty */
 
49
#include <strings.h>    /* for ffs */
 
50
#include "el.h"
48
51
#include "tty.h"
49
 
#include "el.h"
50
52
 
51
53
typedef struct ttymodes_t {
52
54
        const char *m_name;
55
57
}          ttymodes_t;
56
58
 
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 */
60
62
} ttymap_t;
61
63
 
151
153
        {C_LNEXT, VLNEXT,
152
154
        {ED_QUOTED_INSERT, ED_QUOTED_INSERT, ED_UNASSIGNED}},
153
155
#endif /* VLNEXT */
154
 
        {-1, -1,
 
156
        {(Int)-1, (Int)-1,
155
157
        {ED_UNASSIGNED, ED_UNASSIGNED, ED_UNASSIGNED}}
156
158
};
157
159
 
492
494
        int rst = 1;
493
495
 
494
496
        if (el->el_flags & EDIT_DISABLED)
495
 
                return (0);
 
497
                return 0;
496
498
 
 
499
        if (!isatty(el->el_outfd)) {
 
500
#ifdef DEBUG_TTY
 
501
                (void) fprintf(el->el_errfile,
 
502
                    "tty_setup: isatty: %s\n", strerror(errno));
 
503
#endif /* DEBUG_TTY */
 
504
                return -1;
 
505
        }
497
506
        if (tty_getty(el, &el->el_tty.t_ed) == -1) {
498
507
#ifdef DEBUG_TTY
499
508
                (void) fprintf(el->el_errfile,
500
509
                    "tty_setup: tty_getty: %s\n", strerror(errno));
501
510
#endif /* DEBUG_TTY */
502
 
                return (-1);
 
511
                return -1;
503
512
        }
504
513
        el->el_tty.t_ts = el->el_tty.t_ex = el->el_tty.t_ed;
505
514
 
549
558
                            "tty_setup: tty_setty: %s\n",
550
559
                            strerror(errno));
551
560
#endif /* DEBUG_TTY */
552
 
                        return (-1);
 
561
                        return -1;
553
562
                }
554
563
        }
555
 
#ifdef notdef
556
 
        else
557
 
                tty__setchar(&el->el_tty.t_ex, el->el_tty.t_c[EX_IO]);
558
 
#endif
559
564
 
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;
571
576
 
572
577
        tty__setchar(&el->el_tty.t_ed, el->el_tty.t_c[ED_IO]);
573
578
        tty_bind_char(el, 1);
574
 
        return (0);
 
579
        return 0;
575
580
}
576
581
 
577
582
protected int
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);
586
591
}
587
592
 
588
593
 
608
613
 
609
614
        if ((spd = cfgetispeed(td)) == 0)
610
615
                spd = cfgetospeed(td);
611
 
        return (spd);
 
616
        return spd;
612
617
}
613
618
 
614
619
/* tty__getspeed():
892
897
 
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];
 
900
        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;
908
913
                dalt = NULL;
909
914
        }
910
915
 
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)
915
920
                        continue;
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];
922
927
                if (dalt) {
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];
927
932
                }
928
933
        }
929
934
}
937
942
{
938
943
 
939
944
        if (el->el_tty.t_mode == ED_IO || el->el_tty.t_mode == QU_IO)
940
 
                return (0);
 
945
                return 0;
941
946
 
942
947
        if (el->el_flags & EDIT_DISABLED)
943
 
                return (0);
 
948
                return 0;
944
949
 
945
950
        if (tty_getty(el, &el->el_tty.t_ts) == -1) {
946
951
#ifdef DEBUG_TTY
947
952
                (void) fprintf(el->el_errfile, "tty_rawmode: tty_getty: %s\n",
948
953
                    strerror(errno));
949
954
#endif /* DEBUG_TTY */
950
 
                return (-1);
 
955
                return -1;
951
956
        }
952
957
        /*
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!
955
960
         */
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);
1077
1082
                (void) fprintf(el->el_errfile, "tty_rawmode: tty_setty: %s\n",
1078
1083
                    strerror(errno));
1079
1084
#endif /* DEBUG_TTY */
1080
 
                return (-1);
 
1085
                return -1;
1081
1086
        }
1082
1087
        el->el_tty.t_mode = ED_IO;
1083
 
        return (0);
 
1088
        return 0;
1084
1089
}
1085
1090
 
1086
1091
 
1092
1097
{                               /* set tty in normal setup */
1093
1098
 
1094
1099
        if (el->el_tty.t_mode == EX_IO)
1095
 
                return (0);
 
1100
                return 0;
1096
1101
 
1097
1102
        if (el->el_flags & EDIT_DISABLED)
1098
 
                return (0);
 
1103
                return 0;
1099
1104
 
1100
1105
        if (tty_setty(el, TCSADRAIN, &el->el_tty.t_ex) == -1) {
1101
1106
#ifdef DEBUG_TTY
1103
1108
                    "tty_cookedmode: tty_setty: %s\n",
1104
1109
                    strerror(errno));
1105
1110
#endif /* DEBUG_TTY */
1106
 
                return (-1);
 
1111
                return -1;
1107
1112
        }
1108
1113
        el->el_tty.t_mode = EX_IO;
1109
 
        return (0);
 
1114
        return 0;
1110
1115
}
1111
1116
 
1112
1117
 
1117
1122
tty_quotemode(EditLine *el)
1118
1123
{
1119
1124
        if (el->el_tty.t_mode == QU_IO)
1120
 
                return (0);
 
1125
                return 0;
1121
1126
 
1122
1127
        el->el_tty.t_qu = el->el_tty.t_ed;
1123
1128
 
1138
1143
                (void) fprintf(el->el_errfile, "QuoteModeOn: tty_setty: %s\n",
1139
1144
                    strerror(errno));
1140
1145
#endif /* DEBUG_TTY */
1141
 
                return (-1);
 
1146
                return -1;
1142
1147
        }
1143
1148
        el->el_tty.t_mode = QU_IO;
1144
 
        return (0);
 
1149
        return 0;
1145
1150
}
1146
1151
 
1147
1152
 
1153
1158
{
1154
1159
 
1155
1160
        if (el->el_tty.t_mode != QU_IO)
1156
 
                return (0);
 
1161
                return 0;
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 */
1162
 
                return (-1);
 
1167
                return -1;
1163
1168
        }
1164
1169
        el->el_tty.t_mode = ED_IO;
1165
 
        return (0);
 
1170
        return 0;
1166
1171
}
1167
1172
 
1168
1173
 
1171
1176
 */
1172
1177
protected int
1173
1178
/*ARGSUSED*/
1174
 
tty_stty(EditLine *el, int argc __attribute__((__unused__)), const char **argv)
 
1179
tty_stty(EditLine *el, int argc __attribute__((__unused__)), const Char **argv)
1175
1180
{
1176
1181
        const ttymodes_t *m;
1177
1182
        char x;
1178
1183
        int aflag = 0;
1179
 
        const char *s, *d;
1180
 
        const char *name;
 
1184
        const Char *s, *d;
 
1185
        char name[EL_BUFSIZ];
1181
1186
        struct termios *tios = &el->el_tty.t_ex;
1182
1187
        int z = EX_IO;
1183
1188
 
1184
1189
        if (argv == NULL)
1185
 
                return (-1);
1186
 
        name = *argv++;
 
1190
                return -1;
 
1191
        strncpy(name, ct_encode_string(*argv++, &el->el_scratch), sizeof(name));
 
1192
        name[sizeof(name) - 1] = '\0';
1187
1193
 
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]);
1213
 
                        return (-1);
 
1219
                        return -1;
1214
1220
                }
1215
1221
 
1216
1222
        if (!argv || !*argv) {
1217
1223
                int i = -1;
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",
1228
1234
                        if (i != -1) {
1229
1235
                            x = (el->el_tty.t_t[z][i].t_setmask & m->m_value)
1230
1236
                                ?  '+' : '\0';
1231
 
                            x = (el->el_tty.t_t[z][i].t_clrmask & m->m_value)
1232
 
                                ? '-' : x;
 
1237
 
 
1238
                            if (el->el_tty.t_t[z][i].t_clrmask & m->m_value)
 
1239
                                x = '-';
1233
1240
                        } else {
1234
1241
                            x = '\0';
1235
1242
                        }
1238
1245
 
1239
1246
                                cu = strlen(m->m_name) + (x != '\0') + 1;
1240
1247
 
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",
1243
 
                                            st, "");
 
1250
                                            (int)st, "");
1244
1251
                                        len = st + cu;
1245
1252
                                } else
1246
1253
                                        len += cu;
1254
1261
                        }
1255
1262
                }
1256
1263
                (void) fprintf(el->el_outfile, "\n");
1257
 
                return (0);
 
1264
                return 0;
1258
1265
        }
1259
1266
        while (argv && (s = *argv++)) {
1260
 
                const char *p;
 
1267
                const Char *p;
1261
1268
                switch (*s) {
1262
1269
                case '+':
1263
1270
                case '-':
1264
 
                        x = *s++;
 
1271
                        x = (char)*s++;
1265
1272
                        break;
1266
1273
                default:
1267
1274
                        x = '\0';
1268
1275
                        break;
1269
1276
                }
1270
1277
                d = s;
1271
 
                p = strchr(s, '=');
 
1278
                p = Strchr(s, '=');
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))
1276
1283
                                break;
1277
1284
 
1278
1285
                if (!m->m_name) {
1279
1286
                        (void) fprintf(el->el_errfile,
1280
 
                            "%s: Invalid argument `%s'.\n", name, d);
1281
 
                        return (-1);
 
1287
                            "%s: Invalid argument `" FSTR "'.\n", name, d);
 
1288
                        return -1;
1282
1289
                }
1283
1290
                if (p) {
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;
1287
 
                        assert(c-- != 0);
 
1294
                        assert(c != 0);
 
1295
                        c--;
1288
1296
                        c = tty__getcharindex(c);
1289
1297
                        assert(c != -1);
1290
 
                        tios->c_cc[c] = v;
 
1298
                        tios->c_cc[c] = (cc_t)v;
1291
1299
                        continue;
1292
1300
                }
1293
1301
                switch (x) {
1312
1320
                        (void) fprintf(el->el_errfile,
1313
1321
                            "tty_stty: tty_setty: %s\n", strerror(errno));
1314
1322
#endif /* DEBUG_TTY */
1315
 
                        return (-1);
 
1323
                        return -1;
1316
1324
                }
1317
1325
        }
1318
1326
 
1319
 
        return (0);
 
1327
        return 0;
1320
1328
}
1321
1329
 
1322
1330