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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
/******************************************************************************
(c) 2002-2008 Christine Caulfield christine.caulfield@googlemail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <ctype.h>
#include <termios.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/fcntl.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include "dn_endian.h"
#include "dnlogin.h"
#include "tty.h"
/* The global state */
extern int termfd;
extern int exit_char;
extern int finished;
extern unsigned char char_attr[];
/* Input state buffers & variables */
static char terminators[32];
static char rahead_buf[128];
static int rahead_len=0;
static char input_buf[1024];
static int input_len=0;
static int input_pos=0;
static char prompt_buf[1024];
static char prompt_len=0;
static char line_start_pos=0;
static char esc_buf[132];
static int esc_len=0;
static int esc_state=0;
static int max_read_len = sizeof(input_buf);
static int echo = 1;
static int reading = 0;
static int insert_mode = 0;
static int echo_terminator = 0;
static int discard = 0;
static int interpret_escape = 0;
static int allow_edit = 0;
static int convert_uppercase = 0;
static char last_char;
/* in dnlogin.c */
extern int char_timeout;
extern int timeout_valid;
/* Output processors */
int (*send_input)(char *buf, int len, int term_pos, int flags);
int (*send_oob)(char oobchar, int discard);
void (*rahead_change)(int count);
/* Escape parser. See appx B of cterm.txt.
returns 0 if we have terminated the sequenec (or an error)
returns 1 if we are stll in an escape sequence */
/* Note that all the numbered rules fall through to the next rule */
static int parse_escape(char c, int *state)
{
switch (*state)
{
case 0:
if (c == '?' || c == ';')
{
*state = 10;
return 1;
}
if (c == 'O')
{
*state = 20;
return 1;
}
if (c == 'Y')
{
*state = 30;
return 1;
}
if (c == '[')
{
*state = 15;
return 1;
}
case 10:
if (c >= 32 && c <= 47)
{
*state = 10;
return 1;
}
if (c >= 48 && c <= 126)
{
*state = 0;
return 0;
}
case 15:
if (c >= 48 && c <= 63)
{
*state = 15;
return 1;
}
case 20:
if (c >= 32 && c <= 47)
{
*state = 20;
return 1;
}
if (c >= 64 && c <= 126)
{
*state = 0;
return 0;
}
case 30:
if (c >= 32 && c <= 126)
{
*state = 40;
return 1;
}
case 40:
if (c >= 32 && c <= 126)
{
*state = 0;
return 0;
}
break;
default:
*state = 0;
return 0;
}
return 0;
}
static void send_input_buffer(int flags)
{
char buf[1024];
memcpy(buf, input_buf, input_len);
// Assumes the terminator (CR) is the last char...
send_input(buf, input_len, input_len-1, flags);
input_len = input_pos = 0;
reading = 0;
DEBUG_TTY("clearing 'reading' flag\n");
echo = 1;
interpret_escape = 0;
}
/* Called when select() times out */
void tty_timeout()
{
DEBUG_TTY("timeout\n");
send_input_buffer(SEND_FLAG_TIMEOUT);
}
/* Raw write to terminal */
int tty_write(char *buf, int len)
{
int in_esc = 0; /* escapes can't straddle writes */
int esc_state = 0;
if (discard)
return len;
/* Ignore NULs */
if (len == 1 && *buf == 0)
return len;
/* FF is a special case (perhaps!) */
if (len == 1 && buf[0] == '\f')
{
last_char = '\f';
return write(termfd, "\033[H\033[2J", 7);
}
if (debug & DEBUG_FLAG_TTY2)
{
int i;
DEBUG_TTY2("Printing %d: ", len);
for (i=0; i<len; i++)
{
if (isgraph(buf[i]))
{
DEBUGLOG(DEBUG_FLAG_TTY2, "%d: %c ", i, buf[i]);
}
else
{
DEBUGLOG(DEBUG_FLAG_TTY2, "%d: 0x%02x ", i, (unsigned char)buf[i]);
}
}
DEBUGLOG(DEBUG_FLAG_TTY2, "\n");
}
write(termfd, buf, len);
if (len)
{
/* if interpret_escape is set then we don't include escape sequences
as possible "last_char"s
*/
if (!interpret_escape)
{
int i;
for (i=0; i<len; i++)
{
if (buf[i] == ESC)
in_esc = 1;
else
{
if (!in_esc && buf[i])
last_char = buf[i];
if (in_esc && !parse_escape(buf[i], &esc_state))
in_esc = 0;
}
}
}
else
{
last_char = buf[len-1];
}
DEBUG_TTY("Setting last_char to %02x, interpret_escape=%d\n", last_char, interpret_escape);
}
return len;
}
int tty_discard()
{
return discard;
}
void tty_format_cr()
{
char lf = '\n';
DEBUG_TTY("format_cr, last char was %x\n", last_char);
if (last_char == '\r')
tty_write(&lf, 1);
}
void tty_set_noecho()
{
echo = 0;
}
void tty_set_discard(int onoff)
{
discard = onoff;
}
void tty_set_uppercase(int onoff)
{
convert_uppercase = onoff;
}
void tty_allow_edit(int onoff)
{
allow_edit = onoff;
}
int tty_set_escape_proc(int onoff)
{
interpret_escape = onoff;
return interpret_escape;
}
int tty_get_input_count(void)
{
return input_len+rahead_len;
}
void tty_echo_terminator(int a)
{
DEBUG_TTY("echo terminators = %d\n", a);
echo_terminator = a;
}
void tty_set_default_terminators()
{
DEBUG_TTY("set default terminators\n");
/* All control chars except ^R ^U ^W, BS & HT */
/* ie 18, 21, 23, 8, 9 */
/* CC: also remove ^A(1) ^E(5) ^X(29) for line-editting.. CHECK!! */
memset(terminators, 0, sizeof(terminators));
terminators[0] = 0xDD;
terminators[1] = 0xFC;
terminators[2] = 0x5B;
terminators[3] = 0xDF;
}
void tty_set_terminators(char *buf, int len)
{
if (debug & DEBUG_FLAG_TTY) {
int i;
DEBUG_TTY("set terminators... %d bytes\n", len);
DEBUG_TTY("terms: ");
for (i=0; i<len; i++)
DEBUGLOG(DEBUG_FLAG_TTY, "%02x ", buf[i]);
DEBUGLOG(DEBUG_FLAG_TTY, "\n");
}
memset(terminators, 0, sizeof(terminators));
memcpy(terminators, buf, len);
}
void tty_start_read(char *prompt, int len, int promptlen)
{
int i;
DEBUG_TTY("start_read promptlen = %d, maxlen=%d\n",
promptlen, len);
if (promptlen) tty_write(prompt, promptlen);
if (len < 0) len = sizeof(input_buf);
/* Save the actual prompt in one buffer and the prefilled
data in the input buffer */
memcpy(prompt_buf, prompt, promptlen);
prompt_len = promptlen;
/* Work out the position of the cursor after the prompt (which may contain CRLF chars) */
line_start_pos = 0;
for (i = 0; i<prompt_len; i++)
if (prompt_buf[i] >= ' ')
line_start_pos++;
/* Prefilled data (eg a recalled command) */
memcpy(input_buf, prompt+promptlen, len-promptlen);
input_len = len-promptlen;
input_pos = input_len;
tty_write(input_buf, input_len);
reading = 1;
/* Now add in any typeahead */
if (rahead_len)
{
int copylen = rahead_len;
DEBUG_TTY("readahead = %d bytes\n", rahead_len);
/* Don't overflow the input buffer */
if (input_len + copylen > sizeof(input_buf))
copylen = sizeof(input_buf)-input_len;
rahead_len -= copylen;
tty_process_terminal(rahead_buf, copylen);
DEBUG_TTY("readahead now = %d bytes\n", rahead_len);
}
}
void tty_set_timeout(unsigned short to)
{
timeout_valid = 1;
char_timeout = to;
}
void tty_clear_typeahead()
{
rahead_len = 0;
}
void tty_set_maxlen(unsigned short len)
{
DEBUG_TTY("max_read_len now = %d \n", len);
max_read_len = len;
}
/* Set/Reset the local TTY mode */
int tty_setup(char *name, int setup)
{
struct termios new_term;
static struct termios old_term;
if (setup)
{
termfd = open(name, O_RDWR);
if (termfd == -1)
return -1;
tcgetattr(termfd, &old_term);
new_term = old_term;
new_term.c_iflag &= ~BRKINT;
new_term.c_iflag &= ~INLCR;
new_term.c_iflag |= IGNBRK;
new_term.c_oflag &= ~ONLCR;
new_term.c_cc[VMIN] = 1;
new_term.c_cc[VTIME] = 0;
new_term.c_lflag &= ~ICANON;
new_term.c_lflag &= ~ISIG;
new_term.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
tcsetattr(termfd, TCSANOW, &new_term);
}
else
{
tcsetattr(termfd, TCSANOW, &old_term);
close(termfd);
}
return 0;
}
static short is_terminator(char c)
{
short termind, msk, aux;
termind = c / 8;
aux = c % 8;
msk = (1 << aux);
DEBUG_TTY("is_terminator: %d: (byte=%x, msk=%x) %s\n", c, terminators[termind],msk,
(terminators[termind] & msk)?"Yes":"No");
if (terminators[termind] & msk)
{
return 1;
}
return 0;
}
/* Erase to end of line */
static void erase_eol(void)
{
tty_write("\033[0K", 4);
}
/* Move to column "hpos", where hpos starts at 0 */
static void move_cursor_abs(int hpos)
{
char buf[32];
sprintf(buf, "\r\033[%dC", hpos);
tty_write(buf, strlen(buf));
}
void tty_send_unread()
{
send_input_buffer(SEND_FLAG_UNREAD);
}
static void redraw_input_line(void)
{
if (!echo)
return;
move_cursor_abs(line_start_pos);
erase_eol();
tty_write(input_buf, input_len);
move_cursor_abs(line_start_pos + input_pos);
}
/* Input from keyboard */
int tty_process_terminal(char *buf, int len)
{
int i;
for (i=0; i<len; i++)
{
if (buf[i] == exit_char)
{
finished = 1;
return 0;
}
/* Swap LF for CR */
//CC: is this right??
if (buf[i] == '\n')
buf[i] = '\r';
/* I don't really understand this, but it's needed to make ^C work
as ^Y at the VMS command-line */
if (buf[i] == CTRL_C && (!char_attr[(int)buf[i]] & 3))
buf[i] = CTRL_Y;
/* Check for OOB */
if (char_attr[(int)buf[i]] & 3)
{
int oob_discard = (char_attr[(int)buf[i]]>>2)&1;
send_oob(buf[i], oob_discard);
if (oob_discard)
{
input_len = input_pos = 0;
}
continue;
}
/* Read not active, store in the read ahead buffer */
if (!reading)
{
if (rahead_len == 0)
rahead_change(1); /* tell CTERM */
if (rahead_len < sizeof(rahead_buf))
rahead_buf[rahead_len++] = buf[i];
continue;
}
/* Is it ESCAPE ? */
if (buf[i] == ESC && esc_len == 0 && interpret_escape)
{
esc_buf[esc_len++] = buf[i];
continue;
}
/* Terminators */
if (!esc_len && is_terminator(buf[i]))
{
if (echo && echo_terminator)
{
tty_write(&buf[i], 1);
}
input_buf[input_len++] = buf[i];
send_input_buffer(SEND_FLAG_TERMINATOR);
continue;
}
/* Still processing escape sequence */
if (esc_len)
{
esc_buf[esc_len++] = buf[i];
if (!parse_escape(buf[i], &esc_state))
{
int esc_done = 0;
if (allow_edit)
{
/* Process escape sequences */
if (strncmp(esc_buf, "\033[C", 3) == 0) /* Cursor RIGHT */
{
if (input_pos < input_len)
{
input_pos++;
if (echo) tty_write(esc_buf, esc_len);
}
esc_done = 1;
}
if (strncmp(esc_buf, "\033[D", 3) == 0) /* Cursor LEFT */
{
if (input_pos > 0)
{
input_pos--;
if (echo) tty_write(esc_buf, esc_len);
}
esc_done = 1;
}
}
/* If we didn't process it above, then just send it to
the host on the end of whatever is also in the buffer */
if (!esc_done)
{
memcpy(input_buf+input_len, esc_buf, esc_len);
send_input(input_buf, input_len+esc_len, input_len, SEND_FLAG_VALID_ESCAPE);
echo = 1;
}
esc_len = 0;
}
continue;
}
/* Process non-terminator control chars */
if ((buf[i] < ' ' || buf[i] == DEL) && allow_edit)
{
switch (buf[i])
{
case CTRL_B: // Up a line
break;
case CTRL_X: // delete input line
case CTRL_U: // delete input line
move_cursor_abs(line_start_pos);
if (echo) erase_eol();
input_pos = input_len = 0;
break;
case CTRL_H: // back to start of line
if (echo) move_cursor_abs(line_start_pos);
input_pos = 0;
break;
case CTRL_E: // Move to end of line
if (echo) move_cursor_abs(input_len+line_start_pos);
input_pos = input_len;
break;
case CTRL_A: /* switch overstrike/insert mode */
insert_mode = ~insert_mode;
break;
case CTRL_W: /* redraw */
redraw_input_line();
break;
case CTRL_M:
input_buf[input_len++] = buf[i];
send_input_buffer(SEND_FLAG_TERMINATOR);
input_len = input_pos = 0;
break;
case CTRL_O:
discard = ~discard;
break;
case DEL:
if (input_pos > 0)
{
input_pos--;
input_len--;
if (echo) tty_write("\033[D \033[D", 7);
if (input_pos != input_len)
{
memmove(input_buf+input_pos, input_buf+input_pos+1, input_len-input_pos);
redraw_input_line();
}
break;
}
}
continue;
}
if (convert_uppercase && isalpha(buf[i]))
buf[i] &= 0x5F;
/* echo if req'd, insert will redraw the whole line */
if (echo && !insert_mode)
tty_write(&buf[i], 1);
if (insert_mode && input_len > input_pos)
{
memmove(input_buf+input_pos+1, input_buf+input_pos, input_len-input_pos);
input_len++;
}
input_buf[input_pos++] = buf[i];
if (input_len < input_pos)
input_len = input_pos;
if (insert_mode)
redraw_input_line();
if (input_len >= max_read_len)
{
DEBUG_TTY("sending buffer, input_len=%d, max_read_len=%d\n", input_len, max_read_len);
send_input_buffer(SEND_FLAG_BUFFER_FULL);
}
}
return 0;
}
|