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
|
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
struct {
char key;
char *chars;
} map[] = {
{ '1', "_ -1?!,.:;\"'<=>()_" },
{ '2', "Cabc2ABC" },
{ '3', "Fdef3DEF" },
{ '4', "Ighi4GHI" },
{ '5', "Ljkl5JKL" },
{ '6', "Omno6MNO" },
{ '7', "Spqrs7PQRS" },
{ '8', "Vtuv8TUV" },
{ '9', "Zwxyz9WXYZ" },
{ '0', "*+&0@/#*" },
};
char next_char(char key, char current)
{
int i;
char *next;
for(i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
if(key == map[i].key) {
next = strchr(map[i].chars, current);
if(next && next[1])
return next[1];
return map[i].chars[1];
}
}
return key;
}
char prev_char(char key, char current)
{
int i;
char *next;
for(i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
if(key == map[i].key) {
next = strchr(map[i].chars+1, current);
if(next && next[-1])
return next[-1];
return map[i].chars[1];
}
}
return key;
}
int readtty_main(int argc, char *argv[])
{
int c;
//int flags;
char buf[1];
int res;
struct termios ttyarg;
struct termios savedttyarg;
int nonblock = 0;
int timeout = 0;
int flush = 0;
int phone = 0;
char *accept = NULL;
char *rejectstring = NULL;
char last_char_in = 0;
char current_char = 0;
char *exit_string = NULL;
int exit_match = 0;
do {
c = getopt(argc, argv, "nt:fa:r:pe:");
if (c == EOF)
break;
switch (c) {
case 't':
timeout = atoi(optarg);
break;
case 'n':
nonblock = 1;
break;
case 'f':
flush = 1;
break;
case 'a':
accept = optarg;
break;
case 'r':
rejectstring = optarg;
break;
case 'p':
phone = 1;
break;
case 'e':
exit_string = optarg;
break;
case '?':
fprintf(stderr, "%s: invalid option -%c\n",
argv[0], optopt);
exit(1);
}
} while (1);
if(flush)
tcflush(STDIN_FILENO, TCIFLUSH);
ioctl(STDIN_FILENO, TCGETS , &savedttyarg) ; /* set changed tty arguments */
ttyarg = savedttyarg;
ttyarg.c_cc[VMIN] = (timeout > 0 || nonblock) ? 0 : 1; /* minimum of 0 chars */
ttyarg.c_cc[VTIME] = timeout; /* wait max 15/10 sec */
ttyarg.c_iflag = BRKINT | ICRNL;
ttyarg.c_lflag &= ~(ECHO | ICANON);
ioctl(STDIN_FILENO, TCSETS , &ttyarg);
while (1) {
res = read(STDIN_FILENO, buf, 1);
if(res <= 0) {
if(phone) {
if(current_char) {
write(STDERR_FILENO, ¤t_char, 1);
write(STDOUT_FILENO, ¤t_char, 1);
if(exit_string && current_char == exit_string[exit_match]) {
exit_match++;
if(exit_string[exit_match] == '\0')
break;
}
else
exit_match = 0;
current_char = 0;
}
continue;
}
break;
}
if(accept && strchr(accept, buf[0]) == NULL) {
if(rejectstring) {
write(STDOUT_FILENO, rejectstring, strlen(rejectstring));
break;
}
if(flush)
tcflush(STDIN_FILENO, TCIFLUSH);
continue;
}
if(phone) {
//if(!isprint(buf[0])) {
// fprintf(stderr, "got unprintable character 0x%x\n", buf[0]);
//}
if(buf[0] == '\0') {
if(current_char) {
current_char = prev_char(last_char_in, current_char);
write(STDERR_FILENO, ¤t_char, 1);
write(STDERR_FILENO, "\b", 1);
}
continue;
}
if(current_char && buf[0] != last_char_in) {
write(STDERR_FILENO, ¤t_char, 1);
write(STDOUT_FILENO, ¤t_char, 1);
if(exit_string && current_char == exit_string[exit_match]) {
exit_match++;
if(exit_string[exit_match] == '\0')
break;
}
else
exit_match = 0;
current_char = 0;
}
last_char_in = buf[0];
current_char = next_char(last_char_in, current_char);
write(STDERR_FILENO, ¤t_char, 1);
write(STDERR_FILENO, "\b", 1);
continue;
}
write(STDOUT_FILENO, buf, 1);
break;
}
ioctl(STDIN_FILENO, TCSETS , &savedttyarg) ; /* set changed tty arguments */
return 0;
}
|