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 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
  
     | 
    
      /****************************************************************
 *								*
 * Copyright (c) 2001-2023 Fidelity National Information	*
 * Services, Inc. and/or its subsidiaries. All rights reserved.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/
#include "mdef.h"
#include <errno.h>
#include <wctype.h>
#include <wchar.h>
#include "gtm_string.h"
#include "gtm_poll.h"
#include "gtm_stdlib.h"
#include "io_params.h"
#include "io.h"
#include "trmdef.h"
#include "iotimer.h"
#include "gt_timer.h"
#include "iottdef.h"
#include "iott_edit.h"
#include "stringpool.h"
#include "gtmio.h"
#include "eintr_wrappers.h"
#include "send_msg.h"
#include "have_crit.h"
#include "deferred_events_queue.h"
#include "error.h"
#include "std_dev_outbndset.h"
#include "wake_alarm.h"
#include "min_max.h"
#include "svnames.h"
#include "op.h"
#include "util.h"
#ifdef UTF8_SUPPORTED
#include "gtm_icu_api.h"
#include "gtm_utf8.h"
#endif
GBLREF	boolean_t		gtm_utf8_mode, hup_on, prin_in_dev_failure, prin_out_dev_failure;
GBLREF	char			*KEY_BACKSPACE, *KEY_DC, *KEY_DOWN, *KEY_INSERT, *KEY_LEFT, *KEYPAD_LOCAL, *KEYPAD_XMIT, *KEY_RIGHT,
					*KEY_UP;
GBLREF	int			AUTO_RIGHT_MARGIN, EAT_NEWLINE_GLITCH;
GBLDEF	int			term_error_line;		/* record for cores */
GBLREF	int4			exi_condition;
GBLREF	io_pair			io_curr_device, io_std_device;
GBLREF	mval			dollar_zstatus;
GBLREF	mv_stent		*mv_chain;
GBLREF	spdesc			stringpool;
GBLREF	stack_frame		*frame_pointer;
GBLREF	unsigned char		*msp, *stackbase, *stacktop, *stackwarn;
GBLREF	volatile boolean_t	dollar_zininterrupt;
GBLREF	volatile int4		outofband;
LITREF	unsigned char		lower_to_upper_table[];
#ifdef UTF8_SUPPORTED
LITREF	UChar32		u32_line_term[];
#endif
#ifdef __MVS__
#	define SEND_KEYPAD_LOCAL
#else
#	define	SEND_KEYPAD_LOCAL					\
		if (edit_mode && NULL != KEYPAD_LOCAL && (keypad_len = STRLEN(KEYPAD_LOCAL)))	/* embedded assignment */	\
			DOWRITE(tt_ptr->fildes, KEYPAD_LOCAL, keypad_len);
#endif
/* dc1 & dc3 have the same value in ASCII and EBCDIC */
static readonly char		dc1 = 17;
static readonly char		dc3 = 19;
static readonly unsigned char	eraser[3] = { NATIVE_BS, NATIVE_SP, NATIVE_BS };
error_def(ERR_IOEOF);
error_def(ERR_NOPRINCIO);
error_def(ERR_TERMHANGUP);
error_def(ERR_ZINTRECURSEIO);
#ifdef UTF8_SUPPORTED
/* Maintenance of $ZB on a badchar error and returning partial data (if any) */
void iott_readfl_badchar(mval *vmvalptr, wint_t *dataptr32, int datalen,
			 int delimlen, unsigned char *delimptr, unsigned char *strend, unsigned char *buffer_start)
{
	int		 i, len, tmplen;
	unsigned char   *delimend, *outptr, *outtop;
	wint_t		*curptr32;
	io_desc		*iod;
	if (0 < datalen && NULL != dataptr32)
	{       /* Return how much input we got */
		if (gtm_utf8_mode)
		{
			outptr = buffer_start;
			outtop = ((unsigned char *)dataptr32);
			curptr32 = dataptr32;
			for (i = 0; i < datalen && outptr < outtop; i++, curptr32++)
				outptr = UTF8_WCTOMB(*curptr32, outptr);
			vmvalptr->str.len = INTCAST(outptr - buffer_start);
		} else
			vmvalptr->str.len = datalen;
		vmvalptr->str.addr = (char *)buffer_start;
		if (IS_AT_END_OF_STRINGPOOL(buffer_start, 0))
			stringpool.free += vmvalptr->str.len;	/* The BADCHAR error after this won't do this for us */
	}
	if (NULL != strend && NULL != delimptr)
	{       /* First find the end of the delimiter (max of 4 bytes) */
		if (0 == delimlen)
		{
			for (delimend = delimptr; 4 >= delimlen && delimend < strend; ++delimend, ++delimlen)
			{
				if (UTF8_VALID(delimend, strend, tmplen))
					break;
			}
		}
		if (0 < delimlen)
		{       /* Set $ZB with the failing badchar */
			iod = io_curr_device.in;
			memcpy(iod->dollar.zb, delimptr, MIN(delimlen, ESC_LEN - 1));
			iod->dollar.zb[MIN(delimlen, ESC_LEN - 1)] = '\0';
			memcpy(iod->dollar.key, delimptr, MIN(delimlen, DD_BUFLEN - 1));
			iod->dollar.key[MIN(delimlen, DD_BUFLEN - 1)] = '\0';
		}
	}
}
#endif
int	iott_readfl(mval *v, int4 length, int4 msec_timeout)	/* timeout in milliseconds */
{
	ABS_TIME	cur_time, end_time;
	boolean_t	buffer_moved, ch_set, edit_mode, empterm, escape_edit, insert_mode, nonzerotimeout, ret, timed, utf8_active,
				zint_restart;
	d_tt_struct	*tt_ptr;
	int		backspace, delete, down, i, insert_key, ioptr_width, exp_length, keypad_len, left, msk_in, msk_num,
				rdlen, right, save_errno, selstat, status, up, utf8_more;
	int		delchar_width;		/* display width of deleted char */
	int		delta_width;		/* display width change for replaced char */
	int		dx, dx_start;		/* local dollar X, starting value */
	int		dx_instr, dx_outlen;	/* wcwidth of string to insert point, whole string */
	int		dx_prev, dx_cur, dx_next;/* wcwidth of string to char BEFORE, AT and AFTER the insert point */
	int		inchar_width;		/* display width of inchar */
	int		instr;			/* insert point in input string */
	int		outlen;			/* total characters in line so far */
	io_desc		*io_ptr;
	io_terminator	outofbands;
	io_termmask	mask_term;
	mv_stent	*mvc, *mv_zintdev;
	tt_interrupt	*tt_state;
	uint4		mask;
	unsigned char	inbyte, *outptr, *outtop, *zb_ptr, *zb_top;
	unsigned char	more_buf[GTM_MB_LEN_MAX + 1], *more_ptr;	/* to build up multi byte for character */
	unsigned char	*current_ptr;		/* insert next character into buffer here */
	unsigned char	*buffer_start;		/* beginning of non UTF8 buffer */
	wint_t		inchar, *current_32_ptr, *buffer_32_start, switch_char;
	int		poll_timeout, save_poll_timeout;
	nfds_t		poll_nfds;
	struct pollfd	poll_fdlist[1];
#ifdef __MVS__
	wint_t		asc_inchar;
#endif
	assert(stringpool.free >= stringpool.base);
	assert(stringpool.free <= stringpool.top);
	io_ptr = io_curr_device.in;
	if (ERR_TERMHANGUP == error_condition)
	{
		TERMHUP_NOPRINCIO_CHECK(FALSE);				/* FALSE for READ */
		io_ptr->dollar.za = ZA_IO_ERR;
		return FALSE;
	}
	ESTABLISH_RET_GTMIO_CH(&io_curr_device, -1, ch_set);
	tt_ptr = (d_tt_struct *)(io_ptr->dev_sp);
	assert(dev_open == io_ptr->state);
	iott_flush(io_curr_device.out);
	insert_mode = !(TT_NOINSERT & tt_ptr->ext_cap);	/* get initial mode */
	empterm	= (TT_EMPTERM & tt_ptr->ext_cap);
	ioptr_width = io_ptr->width;
	utf8_active = gtm_utf8_mode ? (CHSET_M != io_ptr->ichset) : FALSE;
	/* if utf8_active, need room for multi byte characters plus wint_t buffer */
	exp_length = utf8_active ? (int)(((SIZEOF(wint_t) * length) + (GTM_MB_LEN_MAX * length) + SIZEOF(gtm_int64_t))) : length;
	zint_restart = FALSE;
	if (tt_ptr->mupintr)
	{	/* restore state to before job interrupt */
		tt_state = &tt_ptr->tt_state_save;
		assertpro(ttwhichinvalid != tt_state->who_saved);
		if (dollar_zininterrupt)
		{
			tt_ptr->mupintr = FALSE;
			tt_state->who_saved = ttwhichinvalid;
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_ZINTRECURSEIO);
		}
		assert(length == tt_state->length);
		assertpro(ttread == tt_state->who_saved);	/* ZINTRECURSEIO should have caught */
		mv_zintdev = io_find_mvstent(io_ptr, FALSE);
		if (NULL != mv_zintdev && mv_zintdev->mv_st_cont.mvs_zintdev.buffer_valid)
		{	/* looks good so use it */
			buffer_start = (unsigned char *)mv_zintdev->mv_st_cont.mvs_zintdev.curr_sp_buffer.addr;
			current_ptr = buffer_start;
			mv_zintdev->mv_st_cont.mvs_zintdev.buffer_valid = FALSE;
			mv_zintdev->mv_st_cont.mvs_zintdev.io_ptr = NULL;
			if (mv_chain == mv_zintdev)
				POP_MV_STENT();		/* pop if top of stack */
			buffer_moved = (buffer_start != tt_state->buffer_start);
			if (utf8_active)
			{	/* need to properly align U32 buffer */
				assert(exp_length == tt_state->exp_length);
				buffer_32_start = (wint_t *)ROUND_UP2((INTPTR_T)(buffer_start + (GTM_MB_LEN_MAX * length)),
							SIZEOF(gtm_int64_t));
				if (buffer_moved && (((unsigned char *)buffer_32_start - buffer_start)
						!= ((unsigned char *)tt_state->buffer_32_start - tt_state->buffer_start)))
					memmove(buffer_32_start, buffer_start + ((unsigned char *)tt_state->buffer_32_start
						- tt_state->buffer_start), (SIZEOF(wint_t) * length));
				current_32_ptr = buffer_32_start;
				utf8_more = tt_state->utf8_more;
				more_ptr = tt_state->more_ptr;
				memcpy(more_buf, tt_state->more_buf, SIZEOF(more_buf));
			}
			instr = tt_state->instr;
			outlen = tt_state->outlen;
			dx = tt_state->dx;
			dx_start = tt_state->dx_start;
			dx_instr = tt_state->dx_instr;
			dx_outlen = tt_state->dx_outlen;
			insert_mode = tt_state->insert_mode;
			end_time = tt_state->end_time;
			zb_ptr = tt_state->zb_ptr;
			zb_top = tt_state->zb_top;
			tt_state->who_saved = ttwhichinvalid;
			tt_ptr->mupintr = FALSE;
			zint_restart = TRUE;
		}
	}
	if (!zint_restart)
	{
		ENSURE_STP_FREE_SPACE(exp_length);
		buffer_start = current_ptr = stringpool.free;
		if (utf8_active)
		{
			buffer_32_start = (wint_t *)ROUND_UP2((INTPTR_T)(stringpool.free + (GTM_MB_LEN_MAX * length)),
					SIZEOF(gtm_int64_t));
			current_32_ptr = buffer_32_start;
		}
		instr = outlen = 0;
		dx_instr = dx_outlen = 0;
		utf8_more = 0;
		/* ---------------------------------------------------------
		 * zb_ptr is used to fill-in the value of $zb as we go
		 * If we drop-out with error or otherwise permaturely,
		 * consider $zb to be null.
		 * ---------------------------------------------------------
		 */
		zb_ptr = io_ptr->dollar.zb;
		zb_top = zb_ptr + SIZEOF(io_ptr->dollar.zb) - 1;
		*zb_ptr = 0;
		io_ptr->esc_state = START;
		io_ptr->dollar.za = 0;
		io_ptr->dollar.zeof = FALSE;
		dx_start = (int)io_ptr->dollar.x;
	}
	v->str.len = 0;
	ret = TRUE;
	mask = tt_ptr->term_ctrl;
	mask_term = tt_ptr->mask_term;
	/* keep test in next line in sync with test in iott_rdone.c */
	edit_mode = (0 != (TT_EDITING & tt_ptr->ext_cap) && !((TRM_NOECHO|TRM_PASTHRU) & mask));
	if (!zint_restart)
	{
		if (mask & TRM_NOTYPEAHD)
			TCFLUSH(tt_ptr->fildes, TCIFLUSH, status);
		if (mask & TRM_READSYNC)
		{
			DOWRITERC(tt_ptr->fildes, &dc1, 1, status);
			if (0 != status)
			{
				io_ptr->dollar.za = ZA_IO_ERR;
				rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) status);
			}
		}
	}
	if (edit_mode)
	{	/* remove ESC and editing control characters from terminator list */
		mask_term.mask[ESC / NUM_BITS_IN_INT4] &= ~(1 << ESC);
		mask_term.mask[EDIT_SOL / NUM_BITS_IN_INT4] &= ~(1 << EDIT_SOL);
		mask_term.mask[EDIT_EOL / NUM_BITS_IN_INT4] &= ~(1 << EDIT_EOL);
		mask_term.mask[EDIT_DEOL / NUM_BITS_IN_INT4] &= ~(1 << EDIT_DEOL);
		mask_term.mask[EDIT_DELETE / NUM_BITS_IN_INT4] &= ~(1 << EDIT_DELETE);
		mask_term.mask[EDIT_LEFT / NUM_BITS_IN_INT4] &= ~(1 << EDIT_LEFT);
		mask_term.mask[EDIT_RIGHT / NUM_BITS_IN_INT4] &= ~(1 << EDIT_RIGHT);
		mask_term.mask[EDIT_ERASE / NUM_BITS_IN_INT4] &= ~(1 << EDIT_ERASE);
		if (!zint_restart)
		{
			/* to turn keypad on if possible */
#ifndef __MVS__
			if (NULL != KEYPAD_XMIT && (keypad_len = STRLEN(KEYPAD_XMIT)))	/* embedded assignment */
			{
				DOWRITERC(tt_ptr->fildes, KEYPAD_XMIT, keypad_len, status);
				if (0 != status)
				{
					io_ptr->dollar.za = ZA_IO_ERR;
					rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) status);
				}
			}
#endif
			dx_start = (dx_start + ioptr_width) % ioptr_width;	/* normalize within width */
		}
	}
	if (!zint_restart)
		dx = dx_start;
	nonzerotimeout = FALSE;
	if (NO_M_TIMEOUT == msec_timeout)
	{
		timed = FALSE;
		poll_timeout = 100 * MILLISECS_IN_SEC;
	} else
	{
		timed = TRUE;
		poll_timeout = msec_timeout;
		if (!msec_timeout)
		{
			if (!zint_restart)
				iott_mterm(io_ptr);
		} else
		{
			nonzerotimeout = TRUE;
   			sys_get_curr_time(&cur_time);
			if (!zint_restart)
				add_int_to_abs_time(&cur_time, msec_timeout, &end_time);
		}
	}
	do
	{
		if (outofband)
		{
			if (jobinterrupt == outofband)
			{	/* save state if jobinterrupt */
				tt_state = &tt_ptr->tt_state_save;
				tt_state->who_saved = ttread;
				tt_state->length = length;
				tt_state->buffer_start = buffer_start;
				PUSH_MV_STENT(MVST_ZINTDEV);
				mv_chain->mv_st_cont.mvs_zintdev.curr_sp_buffer.addr = (char *)buffer_start;
				mv_chain->mv_st_cont.mvs_zintdev.curr_sp_buffer.len = exp_length;
				mv_chain->mv_st_cont.mvs_zintdev.buffer_valid = TRUE;
				mv_chain->mv_st_cont.mvs_zintdev.io_ptr = io_ptr;
				if (utf8_active)
				{
					tt_state->exp_length = exp_length;
					tt_state->buffer_32_start = buffer_32_start;
					tt_state->utf8_more = utf8_more;
					tt_state->more_ptr = more_ptr;
					memcpy(tt_state->more_buf, more_buf, SIZEOF(more_buf));
				}
				if (IS_AT_END_OF_STRINGPOOL(buffer_start, 0))
					stringpool.free += exp_length;	/* reserve space */
				tt_state->instr = instr;
				tt_state->outlen = outlen;
				tt_state->dx = dx;
				tt_state->dx_start = dx_start;
				tt_state->dx_instr = dx_instr;
				tt_state->dx_outlen = dx_outlen;
				tt_state->insert_mode = insert_mode;
				tt_state->end_time = end_time;
				tt_state->zb_ptr = zb_ptr;
				tt_state->zb_top = zb_top;
				tt_ptr->mupintr = TRUE;
				REVERT_GTMIO_CH(&io_curr_device, ch_set);
			} else
			{
				instr = outlen = 0;
				SEND_KEYPAD_LOCAL
				if (!msec_timeout)
					iott_rterm(io_ptr);
			}
			async_action(FALSE);
			break;
		}
		errno = 0;
		/* the checks for EINTR below are valid and should not be converted to EINTR
		 * wrapper macros, since the poll/read is not retried on EINTR.
		 */
		poll_fdlist[0].fd = tt_ptr->fildes;
		poll_fdlist[0].events = POLLIN;
		poll_nfds = 1;
		save_poll_timeout = poll_timeout;	/* take a copy and pass it because poll() below might change it */
		selstat = poll(&poll_fdlist[0], poll_nfds, save_poll_timeout);
		if (selstat < 0)
		{
			if (EINTR != errno)
			{
				term_error_line = __LINE__;
				goto term_error;
			}
		} else if (0 == selstat)
		{
			if (timed)
			{
				ret = FALSE;
				break;
			}
			continue;	/* poll() timeout; keep going */
		} else if (0 < (rdlen = (int)(read(tt_ptr->fildes, &inbyte, 1))))	/* This read is protected */
		{
			/* set prin_in_dev_failure to FALSE to indicate input device is working now */
			prin_in_dev_failure = FALSE;
			if (tt_ptr->canonical)
			{
				if (0 == inbyte)
				{
					/* --------------------------------------
					 * This means that the device has hungup
					 * --------------------------------------
					 */
					io_ptr->dollar.zeof = TRUE;
					io_ptr->dollar.x = 0;
					io_ptr->dollar.za = ZA_IO_ERR;
					io_ptr->dollar.y++;
					tt_ptr->discard_lf = FALSE;
					if (io_ptr->error_handler.len > 0)
						RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_IOEOF);
					break;
				} else
					io_ptr->dollar.zeof = FALSE;
			}
#ifdef UTF8_SUPPORTED
			if (utf8_active)
			{
				if (tt_ptr->discard_lf)
				{	/* saw CR last time so ignore following LF */
					tt_ptr->discard_lf = FALSE;
					if (NATIVE_LF == inbyte)
						continue;
				}
				if (utf8_more)
				{	/* needed extra bytes */
					*more_ptr++ = inbyte;
					if (--utf8_more)
						continue;	/* get next byte */
					UTF8_MBTOWC(more_buf, more_ptr, inchar);
					if (WEOF == inchar)
					{	/* invalid char */
						io_ptr->dollar.za = ZA_IO_ERR;
						iott_readfl_badchar(v, buffer_32_start, outlen,
								    (int)(more_ptr - more_buf), more_buf, more_ptr, buffer_start);
						utf8_badchar((int)(more_ptr - more_buf), more_buf, more_ptr, 0, NULL); /* BADCHAR */
						break;
					}
				} else if (0 < (utf8_more = UTF8_MBFOLLOW(&inbyte)))	/* assignment */
				{
					more_ptr = more_buf;
					if (0 > utf8_more)
					{	/* invalid character */
						io_ptr->dollar.za = ZA_IO_ERR;
						*more_ptr++ = inbyte;
						iott_readfl_badchar(v, buffer_32_start, outlen,
								1, more_buf, more_ptr, buffer_start);
						utf8_badchar(1, more_buf, more_ptr, 0, NULL);	/* ERR_BADCHAR */
						break;
					} else if (GTM_MB_LEN_MAX < utf8_more)
					{	/* too big to be valid */
						io_ptr->dollar.za = ZA_IO_ERR;
						*more_ptr++ = inbyte;
						iott_readfl_badchar(v, buffer_32_start, outlen,
								1, more_buf, more_ptr, buffer_start);
						utf8_badchar(1, more_buf, more_ptr, 0, NULL);	/* ERR_BADCHAR */
						break;
					} else
					{
						*more_ptr++ = inbyte;
						continue;	/* get next byte */
					}
				} else
				{	/* single byte */
					more_ptr = more_buf;
					*more_ptr++ = inbyte;
					UTF8_MBTOWC(more_buf, more_ptr, inchar);
					if (WEOF == inchar)
					{	/* invalid char */
						io_ptr->dollar.za = ZA_IO_ERR;
						iott_readfl_badchar(v, buffer_32_start, outlen,
								1, more_buf, more_ptr, buffer_start);
						utf8_badchar(1, more_buf, more_ptr, 0, NULL);	/* ERR_BADCHAR */
						break;
					}
				}
				if (!tt_ptr->done_1st_read)
				{
					tt_ptr->done_1st_read = TRUE;
					if (BOM_CODEPOINT == inchar)
						continue;
				}
				if (mask & TRM_CONVERT)
					inchar = u_toupper(inchar);
				GTM_IO_WCWIDTH(inchar, inchar_width);
			} else
			{
#endif
				if (mask & TRM_CONVERT)
					NATIVE_CVT2UPPER(inbyte, inbyte);
				inchar = inbyte;
				inchar_width = 1;
#ifdef UTF8_SUPPORTED
			}
#endif
			GETASCII(asc_inchar,inchar);
			if (!edit_mode && (dx >= ioptr_width) && io_ptr->wrap && !(mask & TRM_NOECHO))
			{
				DOWRITE(tt_ptr->fildes, NATIVE_TTEOL, STRLEN(NATIVE_TTEOL));
				dx = 0;
			}
			if ((' ' > INPUT_CHAR) && (tt_ptr->enbld_outofbands.mask & (1 << INPUT_CHAR)))
			{	/* ctrap supercedes editing so check first */
				instr = outlen = 0;
				io_ptr->dollar.za = ZA_IO_ERR;
				std_dev_outbndset(INPUT_CHAR);	/* it needs ASCII?	*/
				SEND_KEYPAD_LOCAL
				if (!msec_timeout)
					iott_rterm(io_ptr);
				outofband = ctrap;
				async_action(FALSE);
				break;
			}
			if (((0 != (mask & TRM_ESCAPE)) || edit_mode)
			     && ((NATIVE_ESC == inchar) || (START != io_ptr->esc_state)))
			{
				if (zb_ptr >= zb_top UTF8_ONLY(|| (utf8_active && ASCII_MAX < inchar)))
				{	/* $zb overflow or not ASCII in utf8 mode */
					io_ptr->dollar.za = 2;
					break;
				}
				*zb_ptr++ = (unsigned char)inchar;
				iott_escape(zb_ptr - 1, zb_ptr, io_ptr);
				*(zb_ptr - 1) = (unsigned char)INPUT_CHAR;     /* need to store ASCII value    */
				if (FINI == io_ptr->esc_state && !edit_mode)
					break;
				if (BADESC == io_ptr->esc_state)
				{	/* Escape sequence failed parse */
					io_ptr->dollar.za = 2;
					break;
				}
				/* --------------------------------------------------------------------
				 * In escape sequence...do not process further, but get next character
				 * --------------------------------------------------------------------
				 */
			} else
			{	/* SIMPLIFY THIS! */
				if (!utf8_active || ASCII_MAX >= INPUT_CHAR)
				{	/* may need changes to allow terminator > MAX_ASCII and/or LS and PS if default_mask_term */
					msk_num = (uint4)INPUT_CHAR / NUM_BITS_IN_INT4;
					msk_in = (1 << ((uint4)INPUT_CHAR % NUM_BITS_IN_INT4));
					if (msk_in & mask_term.mask[msk_num])
					{
						*zb_ptr++ = (unsigned char)INPUT_CHAR;
						if (utf8_active && ASCII_CR == INPUT_CHAR)
							tt_ptr->discard_lf = TRUE;
						break;
					}
				} else if (utf8_active && tt_ptr->default_mask_term && (u32_line_term[U32_LT_NL] == INPUT_CHAR ||
					u32_line_term[U32_LT_LS] == INPUT_CHAR || u32_line_term[U32_LT_PS] == INPUT_CHAR))
				{	/* UTF and default terminators and UTF terminators above ASCII_MAX */
					zb_ptr = UTF8_WCTOMB(INPUT_CHAR, zb_ptr);
					break;
				}
				assert(0 <= instr);
				assert(!edit_mode || 0 <= dx);
				assert(outlen >= instr);
				/* For most of the terminal the 'kbs' string capability is a byte in length. It means that it is
				  Not treated as escape sequence. So explicitly check if the input corresponds to the 'kbs' */
				if ((((int)inchar == tt_ptr->ttio_struct->c_cc[VERASE]) ||
				    (empterm && (NULL != KEY_BACKSPACE) && ('\0' == KEY_BACKSPACE[1])
				     && (inchar == KEY_BACKSPACE[0])))
				    && !(mask & TRM_PASTHRU))
				{
					if (0 < instr && (edit_mode || 0 < dx))
					{
						dx_prev = compute_dx(BUFF_ADDR(0), instr - 1, ioptr_width, dx_start);
						delchar_width = dx_instr - dx_prev;
						if (edit_mode)
						{
							if (!(mask & TRM_NOECHO))
								move_cursor_left(dx, delchar_width);
							dx = (dx - delchar_width + ioptr_width) % ioptr_width;
						} else
							dx -= delchar_width;
						instr--;
						dx_instr -= delchar_width;
						STORE_OFF(' ', outlen);
						outlen--;
						if (!(mask & TRM_NOECHO) && edit_mode)
						{
							IOTT_COMBINED_CHAR_CHECK;
						}
						MOVE_BUFF(instr, BUFF_ADDR(instr + 1), outlen - instr);
						if (!(mask & TRM_NOECHO))
						{
							if (!edit_mode)
							{
								for (i = 0; i < delchar_width; i++)
								{
									DOWRITERC(tt_ptr->fildes, eraser, SIZEOF(eraser), status);
									if (0 != status)
										break;
								}
							} else
							{	/* First write spaces on all the display columns that
								 * the current string occupied. Then overwrite that
								 * with the new string. This way we are guaranteed all
								 * display columns are clean.
								 */
								status = write_str_spaces(dx_outlen - dx_instr, dx, FALSE);
								if (0 == status)
									status = write_str(BUFF_ADDR(instr),
											outlen - instr, dx, FALSE, FALSE);
							}
							if (0 != status)
							{
								term_error_line = __LINE__;
								goto term_error;
							}
						}
						dx_outlen = compute_dx(BUFF_ADDR(0), outlen, ioptr_width, dx_start);
					} else if (empterm && (0 == outlen))
					{
						assert(zb_ptr == io_ptr->dollar.zb);
						*zb_ptr++ = (unsigned char)INPUT_CHAR;
						break;
					}
				} else
				{
					if (!edit_mode)
						switch_char = 'A';	/* force to default case */
					else
						switch_char = inchar;
					switch (switch_char)
					{
						case EDIT_SOL:	/* ctrl A  start of line */
						{
							int	num_lines_above;
							int	num_chars_left;
							num_lines_above = (dx_instr + dx_start) /
										ioptr_width;
							num_chars_left = dx - dx_start;
							if (!(mask & TRM_NOECHO))
							{
								if (0 != move_cursor(tt_ptr->fildes, num_lines_above,
										num_chars_left))
								{
									term_error_line = __LINE__;
									goto term_error;
								}
							}
							instr = dx_instr = 0;
							dx = dx_start;
							break;
						}
						case EDIT_EOL:	/* ctrl E  end of line */
						{
							int	num_lines_above;
							int	num_chars_left;
							num_lines_above =
								(dx_instr + dx_start) / ioptr_width -
									(dx_outlen + dx_start) / ioptr_width;
							/* For some reason, a CURSOR_DOWN ("\n") seems to reposition the cursor
							 * at the beginning of the next line rather than maintain the vertical
							 * position. Therefore if we are moving down, we need to calculate
							 * the num_chars_left differently to accommodate this.
							 */
							if (0 <= num_lines_above)
								num_chars_left = dx - (dx_outlen + dx_start) % ioptr_width;
							else
								num_chars_left = - ((dx_outlen + dx_start) % ioptr_width);
							if (!(mask & TRM_NOECHO))
							{
								if (0 != move_cursor(tt_ptr->fildes, num_lines_above,
										num_chars_left))
								{
									term_error_line = __LINE__;
									goto term_error;
								}
							}
							instr = outlen;
							dx_instr = dx_outlen;
							dx = (dx_outlen + dx_start) % ioptr_width;
							break;
						}
						case EDIT_LEFT:	/* ctrl B  left one */
						{
							if (instr != 0)
							{
								dx_prev = compute_dx(BUFF_ADDR(0), instr - 1,
												ioptr_width, dx_start);
								inchar_width = dx_instr - dx_prev;
								if (!(mask & TRM_NOECHO))
								{
									if (0 != move_cursor_left(dx, inchar_width))
									{
										term_error_line = __LINE__;
										goto term_error;
									}
								}
								instr--;
								dx = (dx - inchar_width + ioptr_width) % ioptr_width;
								dx_instr -= inchar_width;
							}
							break;
						}
						case EDIT_RIGHT:	/* ctrl F  right one */
						{
							if (instr < outlen)
							{
								dx_next = compute_dx(BUFF_ADDR(0), instr + 1,
												ioptr_width, dx_start);
								inchar_width = dx_next - dx_instr;
								if (!(mask & TRM_NOECHO))
								{
									if (0 != move_cursor_right(dx, inchar_width))
									{
										term_error_line = __LINE__;
										goto term_error;
									}
								}
								instr++;
								dx = (dx + inchar_width) % ioptr_width;
								dx_instr += inchar_width;
							}
							break;
						}
						case EDIT_DEOL:	/* ctrl K  delete to end of line */
						{
							if (!(mask & TRM_NOECHO))
							{
								if (0 != write_str_spaces(dx_outlen - dx_instr, dx, FALSE))
								{
									term_error_line = __LINE__;
									goto term_error;
								}
							}
							SET_BUFF(instr, ' ', outlen - instr);
							outlen = instr;
							dx_outlen = dx_instr;
							break;
						}
						case EDIT_ERASE:	/* ctrl U  delete whole line */
						{
							int	num_lines_above;
							int	num_chars_left;
							num_lines_above = (dx_instr + dx_start) /
											ioptr_width;
							num_chars_left = dx - dx_start;
							SET_BUFF(0, ' ', outlen);
							if (!(mask & TRM_NOECHO))
							{
								status = move_cursor(tt_ptr->fildes,
									num_lines_above, num_chars_left);
								if (0 != status
									|| 0 != write_str_spaces(dx_outlen, dx_start, FALSE))
								{
									term_error_line = __LINE__;
									goto term_error;
								}
							}
							instr = 0;
							outlen = 0;
							dx = dx_start;
							dx_instr = dx_outlen = 0;
							break;
						}
						case EDIT_DELETE:	/* ctrl D delete char */
						{
							if (instr < outlen)
							{
								STORE_OFF(' ', outlen);
								outlen--;
								if (!(mask & TRM_NOECHO))
								{
									IOTT_COMBINED_CHAR_CHECK;
								}
								MOVE_BUFF(instr, BUFF_ADDR(instr + 1), outlen - instr);
								if (!(mask & TRM_NOECHO))
								{	/* First write spaces on all the display columns that
									 * the current string occupied. Then overwrite that
									 * with the new string. This way we are guaranteed all
									 * display columns are clean.
									 */
									if (0 != write_str_spaces(dx_outlen - dx_instr, dx, FALSE))
									{
										term_error_line = __LINE__;
										goto term_error;
									}
									if (0 != write_str(BUFF_ADDR(instr), outlen - instr,
												dx, FALSE, FALSE))
									{
										term_error_line = __LINE__;
										goto term_error;
									}
								}
								dx_outlen = compute_dx(BUFF_ADDR(0), outlen,
												ioptr_width, dx_start);
							}
							break;
						}
						default:
						{
							if (outlen > instr)
							{
								if (insert_mode)
									MOVE_BUFF(instr + 1, BUFF_ADDR(instr), outlen - instr)
								else if (edit_mode && !insert_mode)
								{	/* only needed if edit && !insert_mode && not at end */
									GTM_IO_WCWIDTH(GET_OFF(instr), delchar_width);
									delta_width = inchar_width - delchar_width;
								}
							}
							STORE_OFF(inchar, instr);
							if (!(mask & TRM_NOECHO))
							{
								if (!edit_mode)
								{
									term_error_line = __LINE__;
									status = iott_write_raw(tt_ptr->fildes,
											BUFF_ADDR(instr), 1);
									if (0 <= status)
									    status = 0;
									else
									    status = errno;
								} else if (instr == outlen)
								{
									term_error_line = __LINE__;
									status = write_str(BUFF_ADDR(instr), 1, dx, FALSE, FALSE);
								} else
								{	/* First write spaces on all the display columns that the
									 * current string occupied. Then overwrite that with the
									 * new string. This way we are guaranteed all display
									 * columns are clean. Note that this space overwrite is
									 * needed even in case of insert mode because due to
									 * differing wide-character alignments before and after
									 * the insert, it is possible that a column might be left
									 * empty in the post insert write of the new string even
									 * though it had something displayed before.
									 */
									term_error_line = __LINE__;
									status = write_str_spaces(dx_outlen - dx_instr, dx, FALSE);
									if (0 == status)
									{
										term_error_line = __LINE__;
										status = write_str(BUFF_ADDR(instr),
											outlen - instr + (insert_mode ? 1 : 0),
											dx, FALSE, FALSE);
									}
								}
								if (0 != status)
									goto term_error;
							}
							if (insert_mode || instr == outlen)
								outlen++;
							instr++;
							if (edit_mode)
							{	/* Compute value of dollarx at the new cursor position */
								dx_cur = compute_dx(BUFF_ADDR(0), instr, ioptr_width, dx_start);
								inchar_width = dx_cur - dx_instr;
								if (!(mask & TRM_NOECHO))
								{
									term_error_line = __LINE__;
									status = move_cursor_right(dx, inchar_width);
									if (0 != status)
										goto term_error;
								}
							}
							if (!edit_mode)
							{
								dx += inchar_width;
								dx_instr += inchar_width;
								dx_outlen += inchar_width;
							} else if (insert_mode || instr >= outlen)
							{	/* at end  or insert */
								dx = (dx + inchar_width) % ioptr_width;
								dx_instr = dx_cur;
								dx_outlen = compute_dx(BUFF_ADDR(0), outlen,
											ioptr_width, dx_start);
							} else
							{	/* replaced character */
								dx = (dx + inchar_width) % ioptr_width;
								dx_instr = dx_cur;
								dx_outlen = compute_dx(BUFF_ADDR(0), outlen,
											ioptr_width, dx_start);
							}
							break;
						}
					}
				}
			}
			/* Ensure that the actual display position of the current character matches the computed value */
			assert(!edit_mode || dx_instr == compute_dx(BUFF_ADDR(0), instr, ioptr_width, dx_start));
			assert(!edit_mode || dx_outlen == compute_dx(BUFF_ADDR(0), outlen, ioptr_width, dx_start));
		} else if (0 == rdlen)
		{
			if (0 < selstat)
			{	/* this should be the only possibility */
				io_ptr->dollar.zeof = TRUE;
				io_ptr->dollar.x = 0;
				io_ptr->dollar.za = 0;
				io_ptr->dollar.y++;
				ISSUE_NOPRINCIO_IF_NEEDED(io_ptr, FALSE, FALSE);	/* FALSE, FALSE: READ, not socket*/
				if (io_ptr->dollar.zeof)
				{
					io_ptr->dollar.za = ZA_IO_ERR;
					SEND_KEYPAD_LOCAL
					RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_IOEOF);
				} else
				{
					io_ptr->dollar.zeof = TRUE;
					io_ptr->dollar.za = 0;
					SEND_KEYPAD_LOCAL
					if (0 < io_ptr->error_handler.len)
						RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_IOEOF);
				}
				break;
			}
			if (0 == errno)
			{	/* eof */
				io_ptr->dollar.zeof = TRUE;
				io_ptr->dollar.x = 0;
				io_ptr->dollar.za = 0;
				io_ptr->dollar.y++;
				SEND_KEYPAD_LOCAL
				if (0 < io_ptr->error_handler.len)
					RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_IOEOF);
				break;
			}
		} else if (EINTR != errno)	/* rdlen < 0 */
		{
			term_error_line = __LINE__;
			goto term_error;
		}
		if (FINI == io_ptr->esc_state)
		{
			int zb_len = (int)(zb_ptr - io_ptr->dollar.zb);
			escape_edit = FALSE;
			/* The arbitrary value -1 signifies inequality in case KEY_* is NULL */
			down = (NULL != KEY_DOWN) ? strncmp((const char *)io_ptr->dollar.zb, KEY_DOWN, zb_len) : -1;
			up = (NULL != KEY_UP) ? strncmp((const char *)io_ptr->dollar.zb, KEY_UP, zb_len) : -1;
			right = (NULL != KEY_RIGHT) ? strncmp((const char *)io_ptr->dollar.zb, KEY_RIGHT, zb_len) : -1;
			left = (NULL != KEY_LEFT) ? strncmp((const char *)io_ptr->dollar.zb, KEY_LEFT, zb_len) : -1;
			backspace = (KEY_BACKSPACE != NULL) ? strncmp((const char *)io_ptr->dollar.zb, KEY_BACKSPACE, zb_len) : -1;
			delete = (KEY_DC != NULL) ? strncmp((const char *)io_ptr->dollar.zb, KEY_DC, zb_len) : -1;
			insert_key = (KEY_INSERT != NULL && '\0' != KEY_INSERT[0])
				?  strncmp((const char *)io_ptr->dollar.zb, KEY_INSERT, zb_len) : -1;
			if (0 == backspace || 0 == delete)
			{
				if ((0 == backspace) && (instr > 0))
				{	/* Move one character to the left */
					dx_prev = compute_dx(BUFF_ADDR(0), instr - 1, ioptr_width, dx_start);
					delchar_width = dx_instr - dx_prev;
					if (!(mask & TRM_NOECHO))
					{
						term_error_line = __LINE__;
						status = move_cursor_left(dx, delchar_width);
					}
					dx = (dx - delchar_width + ioptr_width) % ioptr_width;
					instr--;
					dx_instr -= delchar_width;
				}
				if (instr != outlen)
				{
					STORE_OFF(' ', outlen);
					outlen--;
					if (!(mask & TRM_NOECHO))
					{
						IOTT_COMBINED_CHAR_CHECK;
					}
					MOVE_BUFF(instr, BUFF_ADDR(instr + 1), outlen - instr);
					if (!(mask & TRM_NOECHO))
					{	/* First write spaces on all the display columns that the current string occupied.
						 * Then overwrite that with the new string. This way we are guaranteed all
						 * display columns are clean.
						 */
						if (0 == status)
						{
							term_error_line = __LINE__;
							status = write_str_spaces(dx_outlen - dx_instr, dx, FALSE);
						}
						if (0 == status)
						{
							term_error_line = __LINE__;
							status = write_str(BUFF_ADDR(instr), outlen - instr, dx, FALSE, FALSE);
						}
						if (0 != status)
							goto term_error;
					}
					dx_outlen = compute_dx(BUFF_ADDR(0), outlen, ioptr_width, dx_start);
				} else if (empterm && 0 == outlen)
				{
					assert(instr == 0);
					break;
				}
				escape_edit = TRUE;
			}
			if (up == 0  ||  down == 0)
			{	/* move cursor to start of field */
				if (0 < instr)
				{
					if (0 != move_cursor(tt_ptr->fildes, ((dx_instr + dx_start) / ioptr_width),
						(dx - dx_start)))
					{
						term_error_line = __LINE__;
						goto term_error;
					}
				}
				instr = (int)tt_ptr->recall_buff.len;
				if (length < instr)
					instr = length;	/* restrict to length of read */
				if (0 != instr)
				{	/* need to blank old output first */
					SET_BUFF(instr, ' ', outlen);
					if (0 != write_str_spaces(dx_outlen, dx_start, FALSE))
					{
						term_error_line = __LINE__;
						goto term_error;
					}
					MOVE_BUFF(0, tt_ptr->recall_buff.addr, instr);
					if (0 != write_str(BUFF_ADDR(0), instr, dx_start, TRUE, FALSE))
					{
						term_error_line = __LINE__;
						goto term_error;
					}
				}
				dx_instr = dx_outlen = tt_ptr->recall_width;
				dx = (unsigned)(dx_instr + dx_start) % ioptr_width;
				outlen = instr;
				escape_edit = TRUE;
			} else if (   !(mask & TRM_NOECHO)
				 && !(right == 0  &&  instr == outlen)
				 && !(left == 0   &&  instr == 0))
			{
				if (right == 0)
				{
					dx_next = compute_dx(BUFF_ADDR(0), instr + 1, ioptr_width, dx_start);
					inchar_width = dx_next - dx_instr;
					if (0 != move_cursor_right(dx, inchar_width))
					{
						term_error_line = __LINE__;
						goto term_error;
					}
					instr++;
					dx = (dx + inchar_width) % ioptr_width;
					dx_instr += inchar_width;
				}
				if (left == 0)
				{
					dx_prev = compute_dx(BUFF_ADDR(0), instr - 1, ioptr_width, dx_start);
					inchar_width = dx_instr - dx_prev;
					if (0 != move_cursor_left(dx, inchar_width))
					{
						term_error_line = __LINE__;
						goto term_error;
					}
					instr--;
					dx = (dx - inchar_width + ioptr_width) % ioptr_width;
					dx_instr -= inchar_width;
				}
			}
			if (0 == insert_key)
				insert_mode = !insert_mode;	/* toggle */
			if (0 == right || 0 == left || 0 == insert_key)
				escape_edit = TRUE;
			if (escape_edit || (0 == (TRM_ESCAPE & mask)))
			{	/* reset dollar zb if editing function or not trm_escape */
				memset(io_ptr->dollar.zb, '\0', SIZEOF(io_ptr->dollar.zb));
				io_ptr->esc_state = START;
				zb_ptr = io_ptr->dollar.zb;
				zb_top = zb_ptr + SIZEOF(io_ptr->dollar.zb) - 1;
			} else
				break;	/* not edit function and TRM_ESCAPE */
		}
		if (nonzerotimeout)
		{
			sys_get_curr_time(&cur_time);
			cur_time = sub_abs_time(&end_time, &cur_time);
			if (0 > cur_time.at_sec)
			{
				ret = FALSE;
				break;
			}
			poll_timeout = (long)((cur_time.at_sec * MILLISECS_IN_SEC) +
					DIVIDE_ROUND_UP((gtm_tv_usec_t)cur_time.at_usec, MICROSECS_IN_MSEC));
		}
	} while (outlen < length);
	*zb_ptr++ = 0;
	memcpy(io_ptr->dollar.key, io_ptr->dollar.zb, (zb_ptr - io_ptr->dollar.zb));
	if (!msec_timeout)
	{
		iott_rterm(io_ptr);
		if (0 == outlen && ((io_ptr->dollar.zb + 1) == zb_ptr)) /* No input and no delimiter seen */
			ret = FALSE;
	}
	if (mask & TRM_READSYNC)
	{
		DOWRITERC(tt_ptr->fildes, &dc3, 1, status);
		if (0 != status)
		{
			io_ptr->dollar.za = ZA_IO_ERR;
			rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) status);
		}
	}
	SEND_KEYPAD_LOCAL	/* to turn keypad off if possible */
	if (outofband && (jobinterrupt != outofband))
	{
		v->str.len = 0;
		io_ptr->dollar.za = ZA_IO_ERR;
		REVERT_GTMIO_CH(&io_curr_device, ch_set);
		return(FALSE);
	}
#ifdef UTF8_SUPPORTED
	if (utf8_active)
	{
		outptr = buffer_start;
		outtop = ((unsigned char *)buffer_32_start);
		current_32_ptr = buffer_32_start;
		for (i = 0; i < outlen && outptr < outtop; i++, current_32_ptr++)
			outptr = UTF8_WCTOMB(*current_32_ptr, outptr);
		v->str.len = INTCAST(outptr - buffer_start);
	} else
#endif
		v->str.len = outlen;
	v->str.addr = (char *)buffer_start;
	if (edit_mode)
	{	/* store in recall buffer */
		if ((BUFF_CHAR_SIZE * outlen) > tt_ptr->recall_size)
		{
			if (tt_ptr->recall_buff.addr)
				free(tt_ptr->recall_buff.addr);
			tt_ptr->recall_size = (int)(BUFF_CHAR_SIZE * outlen);
			tt_ptr->recall_buff.addr = malloc(tt_ptr->recall_size);
		}
		tt_ptr->recall_width = dx_outlen;
		tt_ptr->recall_buff.len = outlen;
		memcpy(tt_ptr->recall_buff.addr, BUFF_ADDR(0), BUFF_CHAR_SIZE * outlen);
	}
	if (!(mask & TRM_NOECHO))
	{
		if ((io_ptr->dollar.x += dx_outlen) >= ioptr_width && io_ptr->wrap)
		{
			io_ptr->dollar.y += (io_ptr->dollar.x / ioptr_width);
			if (io_ptr->length)
				io_ptr->dollar.y %= io_ptr->length;
			io_ptr->dollar.x %= ioptr_width;
			if (0 == io_ptr->dollar.x)
				DOWRITE(tt_ptr->fildes, NATIVE_TTEOL, STRLEN(NATIVE_TTEOL));
		}
	}
	REVERT_GTMIO_CH(&io_curr_device, ch_set);
	return ((short)ret);
term_error:
	save_errno = errno;
	io_ptr->dollar.za = ZA_IO_ERR;
	tt_ptr->discard_lf = FALSE;
	SEND_KEYPAD_LOCAL	/* to turn keypad off if possible */
	if (!msec_timeout)
		iott_rterm(io_ptr);
	rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) save_errno);
	return FALSE;
}
 
     |