File: iott_rdone.c

package info (click to toggle)
fis-gtm 7.1-006-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,908 kB
  • sloc: ansic: 344,906; asm: 5,184; csh: 4,859; sh: 2,000; awk: 294; makefile: 73; sed: 13
file content (531 lines) | stat: -rw-r--r-- 14,940 bytes parent folder | download | duplicates (2)
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
/****************************************************************
 *								*
 * Copyright (c) 2001-2024 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_signal.h>
#include "gtm_string.h"
#include "gtm_poll.h"
#include "gtm_stdlib.h"

#include "io.h"
#include "iottdef.h"
#include "iott_edit.h"
#include "trmdef.h"
#include "iotimer.h"
#include "gt_timer.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 "svnames.h"
#include "op.h"
#include "util.h"
#ifdef UTF8_SUPPORTED
#include "gtm_icu_api.h"
#include "gtm_utf8.h"
#endif

GBLREF	bool			out_of_time;
GBLREF	boolean_t		gtm_utf8_mode, hup_on, prin_in_dev_failure, prin_out_dev_failure;
GBLREF	int4			exi_condition;
GBLREF	io_pair			io_curr_device, io_std_device;
GBLREF	mval			dollar_zstatus;
GBLREF	mv_stent		*mv_chain;
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[];

error_def(ERR_IOEOF);
error_def(ERR_NOPRINCIO);
error_def(ERR_TERMHANGUP);
error_def(ERR_ZINTRECURSEIO);

int	iott_rdone (mint *v, int4 msec_timeout)	/* timeout in milliseconds */
{
	ABS_TIME	cur_time, end_time;
	boolean_t	ch_set, first_time, ret = FALSE, timed, utf8_active, zint_restart;
	char		dc1, dc3;
	d_tt_struct	*tt_ptr;
	int		inchar_width, msk_in, msk_num, rdlen, selstat, status, utf8_more;
	io_desc		*io_ptr;
	mv_stent	*mv_zintdev;
	short int	i;
	TID		timer_id;
	tt_interrupt	*tt_state;
	uint4		mask;
	unsigned char	inbyte, *zb_ptr, *zb_top;
	unsigned char	more_buf[GTM_MB_LEN_MAX + 1], *more_ptr;	/* to build up multi byte for character */
	wint_t		inchar;
	int		poll_timeout;
	nfds_t		poll_nfds;
	struct pollfd	poll_fdlist[1];
#ifdef __MVS__
	wint_t		asc_inchar;
#endif

	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);
	assert(io_ptr->state == dev_open);
	iott_flush(io_curr_device.out);
	tt_ptr = (d_tt_struct*) io_ptr->dev_sp;
	timer_id = (TID) iott_rdone;
	*v = -1;
	dc1 = (char) 17;
	dc3 = (char) 19;
	mask = tt_ptr->term_ctrl;
	utf8_active = gtm_utf8_mode ? (CHSET_M != io_ptr->ichset) : FALSE;
	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);
		}
		assertpro(ttrdone == 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 = FALSE;
			mv_zintdev->mv_st_cont.mvs_zintdev.io_ptr = NULL;
			if (mv_chain == mv_zintdev)
				POP_MV_STENT();         /* pop if top of stack */
			end_time = tt_state->end_time;
			if (utf8_active)
			{
				utf8_more = tt_state->utf8_more;
				more_ptr = tt_state->more_ptr;
				memcpy(more_buf, tt_state->more_buf, SIZEOF(more_buf));
			}
			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)
	{
		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;
		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);
			}
		}
	}
	out_of_time = FALSE;
	if (NO_M_TIMEOUT == msec_timeout)
	{
		timed = FALSE;
		poll_timeout = 100 * MILLISECS_IN_SEC;
	} else
	{
		timed = TRUE;
		poll_timeout = msec_timeout;
		if (0 == msec_timeout)
		{
			if (!zint_restart)
				iott_mterm(io_ptr);
		} else
		{
			sys_get_curr_time(&cur_time);
			if (!zint_restart)
				add_int_to_abs_time(&cur_time, msec_timeout, &end_time);
		}
	}
	first_time = TRUE;
	do
	{
		if (outofband)
		{
			if (jobinterrupt == outofband)
			{	/* save state if jobinterrupt */
				tt_state = &tt_ptr->tt_state_save;
				tt_state->exp_length = 0;
				tt_state->who_saved = ttrdone;
				tt_state->end_time = end_time;
				if (utf8_active)
				{
					tt_state->utf8_more = utf8_more;
					tt_state->more_ptr = more_ptr;
					memcpy(tt_state->more_buf, more_buf, SIZEOF(more_buf));
				}
				tt_state->zb_ptr = zb_ptr;
				tt_state->zb_top = zb_top;
				PUSH_MV_STENT(MVST_ZINTDEV);	/* used as a flag only */
				mv_chain->mv_st_cont.mvs_zintdev.buffer_valid = FALSE;
				mv_chain->mv_st_cont.mvs_zintdev.io_ptr = io_ptr;
				tt_ptr->mupintr = TRUE;
			} else
			{
				if (timed && (0 == msec_timeout))
					iott_rterm(io_ptr);
			}
			REVERT_GTMIO_CH(&io_curr_device, ch_set);
			async_action(FALSE);
			break;
		}
		if (!first_time)
		{
			if (timed)
			{
				if (0 != msec_timeout)
				{
					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));
				}
			} else
			{
				poll_timeout = 100 * MILLISECS_IN_SEC;
			}
		} else
			first_time = FALSE;
		/* the checks for EINTR below are valid and should not be converted to EINTR
		 * wrapper macros, since the select/read is not retried on EINTR.
		 */
		poll_fdlist[0].fd = tt_ptr->fildes;
		poll_fdlist[0].events = POLLIN;
		poll_nfds = 1;
		selstat = poll(&poll_fdlist[0], poll_nfds, poll_timeout);
		if (0 > selstat)
		{
			if (EINTR != errno)
			{
				io_ptr->dollar.za = ZA_IO_ERR;
				if (timed && (0 == msec_timeout))
					iott_rterm(io_ptr);
				rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) errno);
				break;
			}
		} else if (0 == selstat)
		{
			if (timed)
			{
				wake_alarm(0, 0, NULL);	/* sets out_of_time to be true for zero as well as non-zero timeouts */
				break;
			}
			continue;	/* select() timeout; try again */
		} else if ((rdlen = (int)(read(tt_ptr->fildes, &inbyte, 1))) == 1)	/* This read is protected */
		{
			/* --------------------------------------------------
			 * set prin_in_dev_failure to FALSE to indicate that
			 * input device is working now.
			 * --------------------------------------------------
			 */
			prin_in_dev_failure = 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;
						/* No data to return */
						iott_readfl_badchar(NULL, NULL, 0,
								    (int)((more_ptr - more_buf)), more_buf, more_ptr, NULL);
						utf8_badchar((int)(more_ptr - more_buf), more_buf, more_ptr, 0, NULL); /* BADCHAR */
						break;
					}
				} else
				{
					more_ptr = more_buf;
					if (0 < (utf8_more = UTF8_MBFOLLOW(&inbyte)))	/* assignment */
					{
						if ((0 > utf8_more) || (GTM_MB_LEN_MAX < utf8_more))
						{	/* invalid character */
							io_ptr->dollar.za = ZA_IO_ERR;
							*more_ptr++ = inbyte;
							 /* No data to return */
							iott_readfl_badchar(NULL, NULL, 0, 1, more_buf, more_ptr, NULL);
							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++ = inbyte;
						UTF8_MBTOWC(more_buf, more_ptr, inchar);
						if (WEOF == inchar)
						{	/* invalid char */
							io_ptr->dollar.za = ZA_IO_ERR;
							 /* No data to return */
							iott_readfl_badchar(NULL, NULL, 0, 1, more_buf, more_ptr, NULL);
							utf8_badchar(1, more_buf, more_ptr, 0, NULL);   /* ERR_BADCHAR */
							break;
						}
					}
				}
				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 (INPUT_CHAR < ' '  &&  ((1 << INPUT_CHAR) & tt_ptr->enbld_outofbands.mask))
			{
				std_dev_outbndset(INPUT_CHAR);
				if (timed)
				{
					if (0 == msec_timeout)
				  		iott_rterm(io_ptr);
				}
				async_action(FALSE);
				ret = FALSE;
				break;
			}
			else if (   ((mask & TRM_ESCAPE) != 0)
				 && (inchar == NATIVE_ESC  ||  io_ptr->esc_state != START))
			{
				*v = INPUT_CHAR;
				ret = FALSE;
				do
				{
					if (zb_ptr >= zb_top UTF8_ONLY(|| (utf8_active && ASCII_MAX < inchar)))
					{
						/* -------------
						 * $zb overflow
						 * -------------
						 */
						io_ptr->dollar.za = 2;
						break;
					}
					*zb_ptr++ = (unsigned char)inchar;
					iott_escape(zb_ptr - 1, zb_ptr, io_ptr);
					/*	RESTORE_DOLLARZB(asc_inchar);	*/
					*(zb_ptr - 1) = (unsigned char)INPUT_CHAR;	/* need to store ASCII value	*/
					if (io_ptr->esc_state == FINI)
					{
						ret = TRUE;
						break;
					}
					if (io_ptr->esc_state == BADESC)
					{
						/* ------------------------------
						 * Escape sequence failed parse.
						 * ------------------------------
						 */
						io_ptr->dollar.za = 2;
						break;
					}
					DOREADRL(tt_ptr->fildes, &inbyte, 1, rdlen);
					inchar = inbyte;
					GETASCII(asc_inchar, inchar);
				} while (1 == rdlen);
				*zb_ptr++ = 0;
				if (rdlen != 1  &&  io_ptr->dollar.za == 0)
					io_ptr->dollar.za = ZA_IO_ERR;
				/* -------------------------------------------------
				 * End of escape sequence...do not process further.
				 * -------------------------------------------------
				 */
				break;
			} else
			{	/* may need to deal with terminators > ASCII_MAX and/or LS and PS if default_mask_term */
				ret = TRUE;
				if (!utf8_active || ASCII_MAX >= INPUT_CHAR)
				{
					msk_num = (uint4)INPUT_CHAR / NUM_BITS_IN_INT4;
					msk_in = (1 << ((uint4)INPUT_CHAR % NUM_BITS_IN_INT4));
				} else
					msk_num = msk_in = 0;		/* force to not match terminator */
				if ((!(msk_in & tt_ptr->mask_term.mask[msk_num]))  &&  (!(mask & TRM_NOECHO)))
				{
					status = iott_write_raw(tt_ptr->fildes,
						utf8_active ? (void *)&inchar : (void *)&inbyte, 1);
					if (0 >= status)
					{
						status = errno;
						io_ptr->dollar.za = ZA_IO_ERR;
						if (timed)
						{
							if (0 == msec_timeout)
								iott_rterm(io_ptr);
						}
						rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1)  status);
					}
				}
				break;
			}
		} else if (rdlen < 0)
		{
			if (errno != EINTR)
			{
				io_ptr->dollar.za = ZA_IO_ERR;
				if (timed && (0 == msec_timeout))
					iott_rterm(io_ptr);
				rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) errno);
				break;
			}
		} else
		/* ------------
		 * rdlen == 0
		 * ------------
		 */
		{	/* ---------------------------------------------------------
			 * select() says there's something to read, but
			 * read() found zero characters; assume connection dropped.
			 * ---------------------------------------------------------
			 */
			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;
				RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_IOEOF);
			} else
			{
				io_ptr->dollar.zeof = TRUE;
				io_ptr->dollar.za   = 0;
				if (io_ptr->error_handler.len > 0)
					RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_IOEOF);
			}
			break;
		}
	} while (!out_of_time);
	if (timed)
	{
		if (0 == msec_timeout)
			iott_rterm(io_ptr);
	}
	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);
		}
	}
	if (outofband && jobinterrupt != outofband)
	{
		io_ptr->dollar.za = ZA_IO_ERR;
		REVERT_GTMIO_CH(&io_curr_device, ch_set);
		return FALSE;
	}
	io_ptr->dollar.za = 0;
	if (ret  &&  io_ptr->esc_state != FINI)
	{
		*v = INPUT_CHAR;
		if ((TT_EDITING & tt_ptr->ext_cap) && !((TRM_PASTHRU|TRM_NOECHO) & mask))
		{	/* keep above test in sync with iott_readfl */
			assert(tt_ptr->recall_buff.addr);
			if (!utf8_active)
			{
				tt_ptr->recall_buff.addr[0] = INPUT_CHAR;
				tt_ptr->recall_buff.len = 1;
			}
#			ifdef UTF8_SUPPORTED
			else
			{
				memcpy(tt_ptr->recall_buff.addr, &INPUT_CHAR, SIZEOF(INPUT_CHAR));
				tt_ptr->recall_buff.len = SIZEOF(INPUT_CHAR);
			}
#			endif
			tt_ptr->recall_width = inchar_width;
		}
		/* 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));
		} else
			msk_num = msk_in = 0;		/* force no match to terminator */
		if (msk_in & tt_ptr->mask_term.mask[msk_num])
		{
			*zb_ptr++ = INPUT_CHAR;
			*zb_ptr++ = 0;
		}
		else
			io_ptr->dollar.zb[0] = '\0';
		if ((!(msk_in & tt_ptr->mask_term.mask[msk_num])) && (!(mask & TRM_NOECHO)))
		{
			if ((io_ptr->dollar.x += inchar_width) >= io_ptr->width && io_ptr->wrap)
			{
				++(io_ptr->dollar.y);
				if (io_ptr->length)
					io_ptr->dollar.y %= io_ptr->length;
				io_ptr->dollar.x %= io_ptr->width;
				if (io_ptr->dollar.x == 0)
					DOWRITE(tt_ptr->fildes, NATIVE_TTEOL, STRLEN(NATIVE_TTEOL));
			}
		}
	}
	memcpy(io_ptr->dollar.key, io_ptr->dollar.zb, (zb_ptr - io_ptr->dollar.zb));
	REVERT_GTMIO_CH(&io_curr_device, ch_set);
	return ret;
}