screen.c 26.5 KB
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
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa;  se  não, escreva para a Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA, 02111-1307, USA
 *
 * Este programa está nomeado como screen.c e possui 710 linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 * licinio@bb.com.br		(Licínio Luis Branco)
 * kraucer@bb.com.br		(Kraucer Fernandes Mazuco)
 * macmiranda@bb.com.br		(Marco Aurélio Caldas Miranda)
 *
 */

 #include "rx3270.h"
 #include <errno.h>

/*---[ Statics ]----------------------------------------------------------------------------------*/

/*---[ Implement ]--------------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270InputString                                  */
/*                                                                            */
/* Description: "type" informed string.                                       */
/*                                                                            */
/* Rexx Args:	NONE to send an Enter                                         */
/*                                                                            */
/* Rexx Args:	String to input                                               */
/*                                                                            */
/* Rexx Args:	Buffer position                                               */
/*				String to input                                               */
/*                                                                            */
/* Rexx Args:	New Cursor row                                                */
/*				New Cursor col                                                */
/*				String to input                                               */
/*                                                                            */
/* Rexx Args:   String to input                                               */
/*                                                                            */
/* Returns:	    None                                                          */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270InputString(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	char *str = 0;

	Trace("Status: %d",query_3270_terminal_status());

 	if(query_3270_terminal_status() != LIB3270_STATUS_BLANK)
		return RetValue(Retstr,EINVAL);

	switch(Argc)
	{
	case 0:
		lib3270_enter(NULL);
		break;

    case 1:
		str = GetStringArg(Argv);
		break;

	case 2:
        cursor_set_addr(atoi(Argv[0].strptr));
		str = GetStringArg(Argv+1);
        break;

    case 3:
        cursor_set_addr((atoi(Argv[0].strptr)-1) * ctlr_get_cols() + (atoi(Argv[1].strptr)-1));
		str = GetStringArg(Argv+2);
        break;

	default:
		return RXFUNC_BADCALL;
	}

	if(str)
	{
		Input_String((unsigned char *) str);
		ReleaseStringArg(str);
	}

	Trace("Status: %d",query_3270_terminal_status());

	return RetValue(Retstr,query_3270_terminal_status() == LIB3270_STATUS_BLANK ? 0 : EINVAL);
 }

 RexxReturnCode REXXENTRY rx3270FindFieldAttribute(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
	if(Argc != 1)
		return RXFUNC_BADCALL;

	return RetValue(Retstr,find_field_attribute(NULL,atoi(Argv[0].strptr)));
 }

 RexxReturnCode REXXENTRY rx3270FindFieldLength(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
	if(Argc != 1)
		return RXFUNC_BADCALL;

	return RetValue(Retstr,find_field_length(NULL,atoi(Argv[0].strptr)));
 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270SetCursorPosition                            */
/*                                                                            */
/* Description: Set cursor position.                                          */
/*                                                                            */
/* Rexx Args:   New cursor position                                           */
/*                                                                            */
/* Rexx Args:   New cursor row                                                */
/*              New cursor col                                             	  */
/*                                                                            */
/* Returns:	    Original cursor position                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/
RexxReturnCode REXXENTRY rx3270SetCursorPosition(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
{
    int rc = -1;

    switch(Argc)
    {
    case 0:
        rc = cursor_get_addr();
        break;

    case 1:
        rc = cursor_set_addr(atoi(Argv[0].strptr));
        break;

    case 2:
        rc = cursor_set_addr((atoi(Argv[0].strptr)-1) * ctlr_get_cols() + (atoi(Argv[1].strptr)-1));
        break;

    default:
		return RXFUNC_BADCALL;
    }

	ReturnValue(rc);
 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270QueryScreenAttribute                         */
/*                                                                            */
/* Description: Get Screen Atribute                                           */
/*                                                                            */
/* Rexx Args:   Screen attribute to get                                       */
/*              SCREEN_COLS to get the number of the colums in the screen     */
/*              SCREEN_ROWS to get the number of the rows in the screen       */
/*              CURSOR_ADDR to get the current cursor position                */
/*                                                                            */
/* Returns:	    Value of the selected attribute                               */
/*                                                                            */
/*----------------------------------------------------------------------------*/
RexxReturnCode REXXENTRY rx3270QueryScreenAttribute(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
{
	if(!g_ascii_strcasecmp(Argv[0].strptr,"SCREEN_COLS"))
	{
		ReturnValue(ctlr_get_cols());
	}
	else if(!g_ascii_strcasecmp(Argv[0].strptr,"SCREEN_ROWS"))
	{
		ReturnValue(ctlr_get_rows());
	}
	else if(!g_ascii_strcasecmp(Argv[0].strptr,"CURSOR_ADDR"))
	{
		ReturnValue(cursor_get_addr());
	}

    ReturnValue(-1);
}

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270GetCursorPosition                            */
/*                                                                            */
/* Description: Get cursor position.                                          */
/*                                                                            */
/* Rexx Args:   None                                                          */
/*                                                                            */
/* Returns:	    Current cursor position                                       */
/*                                                                            */
/*----------------------------------------------------------------------------*/
RexxReturnCode REXXENTRY rx3270GetCursorPosition(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
{
    if(Argc)
		return RXFUNC_BADCALL;

	ReturnValue(cursor_get_addr());
}

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270WaitForStringAt                              */
/*                                                                            */
/* Description: Wait for desired string at position                           */
/*                                                                            */
/* Rexx Args:	NONE to read the entire screen                                */
/*                                                                            */
/* Rexx Args:   Number of chars to read from cursor position                  */
/*                                                                            */
/* Rexx Args:   row                                                           */
/*				Col													  		  */
/*              string                                       				  */
/*				timeout (seconds)											  */
/*                                                                            */
/* Rexx Args:   row                                                           */
/*				Col													  		  */
/*              string                                       				  */
/*                                                                            */
/* Rexx Args:   string                                                        */
/*                                                                            */
/* Rexx Args:   string                                                        */
/*              timeout                                                       */
/*                                                                            */
/* Returns:	    0 if the string is in the screen or error code                */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270WaitForStringAt(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int start, sz, rows, cols, row, col, rc;
 	time_t end = time(0);
 	const char *key;
 	char *buffer;

	get_3270_terminal_size(hSession,&rows,&cols);

	switch(Argc)
	{
	case 1:	// 1 parameter, search for string in the entire screen with 1 second timeout
		row = col = 0;
		key = Argv[0].strptr;
		end++;
		sz = (rows*cols)+1;
		break;

	case 2:	// 2 parameters, search for string in the entire screen with supplied timeout
		row = col = 0;
		key = Argv[0].strptr;
		end += atoi(Argv[1].strptr);
		sz = (rows*cols)+1;
		break;

	case 3:	// 3 parameters assume default timeout
		row = atoi(Argv[0].strptr)-1;
		col = atoi(Argv[1].strptr)-1;
		key = Argv[2].strptr;
		sz = strlen(key);
		end += RX3270_DEFAULT_TIMEOUT;
		break;

	case 4:	// All parameters
		row = atoi(Argv[0].strptr)-1;
		col = atoi(Argv[1].strptr)-1;
		key = Argv[2].strptr;
		sz = strlen(key);
		end += atoi(Argv[3].strptr);
		Trace("Timeout: %d",atoi(Argv[3].strptr));
		break;

	default:
		return RXFUNC_BADCALL;
	}

	if(row < 0 || row > rows || col < 0 || col > cols)
	{
		ReturnValue(EINVAL);
	}

	start = ((row) * cols) + col;
	rc = ETIMEDOUT;

	buffer = malloc(sz+2);

	Trace("Waiting for %ld seconds (Default: %d)", (end-time(0)),RX3270_DEFAULT_TIMEOUT);

	RunPendingEvents(0);

	while( (rc == ETIMEDOUT) && (time(0) <= end) )
	{
		if(!CONNECTED)
		{
			Trace("Disconnected when waiting for \"%s\"",key);
			rc = ENOTCONN;
		}
		else if(IsHalted())
		{
			rc = ECANCELED;
		}
		else if(query_3270_terminal_status() == LIB3270_STATUS_BLANK)
		{
			screen_read(buffer,start,sz);
			*(buffer+sz) = 0;

			if(strstr(buffer,key))
				rc = 0;
		}

		RunPendingEvents(1);

	}

	Trace("Waitforstring(%s) exits with %d (%s) (Status: %d)",key,rc,rc == ETIMEDOUT ? "TIMEOUT" : strerror(rc),query_3270_terminal_status());

	free(buffer);

	ReturnValue(rc);

 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270ReadScreen                                   */
/*                                                                            */
/* Description: Read screen contents.                                         */
/*                                                                            */
/* Rexx Args:	NONE to read the entire screen                                */
/*                                                                            */
/* Rexx Args:   Number of chars to read from cursor position                  */
/*                                                                            */
/* Rexx Args:   Start position                                                */
/*              Number of chars to read                                       */
/*                                                                            */
/* Rexx Args:   Start Row                                                     */
/*				Start Col													  */
/*              Number of chars to read                                       */
/*                                                                            */
/* Returns:	    Screen contents                                               */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270ReadScreen(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int start, qtd, rows, cols, row, col;
 	char *buffer;

	switch(Argc)
	{
	case 0:	// Get entire screen
		get_3270_terminal_size(hSession,&rows,&cols);
		qtd = (rows*(cols+1)+1);
		buffer = malloc(qtd+2);

		Trace("Screen buffer size: %d (%dx%d)",qtd,rows,cols);

		memset(buffer,0,qtd+1);
		start = qtd = 0;
		for(row = 0; row < rows;row++)
		{
			screen_read(buffer+qtd,start,cols);
			qtd += cols;
			start += cols;
			Retstr->strptr[qtd++] = '\n';
		}
		buffer[qtd] = 0;

		Trace("Bytes read: %d",qtd);

		RetConvertedString(Retstr,buffer);

		free(buffer);
		return RXFUNC_OK;

	case 1:	// Just size, get current cursor position
		start	= 0;
		qtd 	= atoi(Argv[0].strptr);
		break;

	case 2:	// Use start position
		start	= atoi(Argv[0].strptr);
		qtd 	= atoi(Argv[1].strptr);
		break;

	case 3:	// Get start position from row/col
		get_3270_terminal_size(hSession,&rows,&cols);

		row = atoi(Argv[0].strptr)-1;
		col = atoi(Argv[1].strptr)-1;

		if(row < 0 || row > rows || col < 0 || col > cols)
		{
			ReturnString("");
		}

		start 	= (row * cols) + col;
		qtd 	= atoi(Argv[2].strptr);

		break;

	default:
		return RXFUNC_BADCALL;
	}

	if(qtd < 1)
		return RXFUNC_BADCALL;

	buffer = malloc(qtd+1);
	screen_read(buffer, start, qtd);

	RetConvertedString(Retstr,buffer);

	free(buffer);

    return RXFUNC_OK;

 }


/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270SendENTERKey                                 */
/*                                                                            */
/* Description: Send and "ENTER"        .                                     */
/*                                                                            */
/* Rexx Args:	None                                                          */
/*                                                                            */
/* Returns:	    None                                                          */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270SendENTERKey(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int rc = 0;

	if(Argc != 0)
		return RXFUNC_BADCALL;

	if(!PCONNECTED)
		rc = ENOTCONN;
	else if(query_3270_terminal_status() != LIB3270_STATUS_BLANK)
		rc = EINVAL;
	else
		rc = lib3270_enter(NULL);

	RunPendingEvents(1);

	ReturnValue(rc);
 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270SendPFKey                                    */
/*                                                                            */
/* Description: Activate a PF-Key action.                                     */
/*                                                                            */
/* Rexx Args:	Number of the key to activate (1 for PF1, 2, PF2, etc).       */
/*                                                                            */
/* Returns:	    None                                                          */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270SendPFKey(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int rc = 0;

	if(Argc != 1)
		return RXFUNC_BADCALL;

	Trace("%s: Status: %d Key: %s",__FUNCTION__,query_3270_terminal_status(),Argv[0].strptr);

	if(!PCONNECTED)
		rc = ENOTCONN;
	else if(query_3270_terminal_status() != LIB3270_STATUS_BLANK)
		rc = EINVAL;
	else
		rc = lib3270_pfkey(NULL,atoi(Argv[0].strptr));

	if(!rc)
	{
		// Key was sent, wait for screen changes
		RunPendingEvents(1);
	}

	ReturnValue(rc);
 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270WaitForCtrlrDone                             */
/*                                                                            */
/* Description: Wait for next screen                                          */
/*                                                                            */
/* Rexx Args:	Timeout in seconds  (None to default to 60 seconds)           */
/*                                                                            */
/* Returns:	    0 if ok or error code                                         */
/*                                                                            */
/*----------------------------------------------------------------------------*/

 RexxReturnCode REXXENTRY rx3270WaitForCtlrDone(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int 			last = query_counter(COUNTER_ID_CTLR_DONE);
 	int				rc  = 0;
 	int 			end = time(0);

	switch(Argc)
	{
	case 0:
		end += 60;
		break;

	case 1:
		end += atoi(Argv[0].strptr);
		break;

	default:
		return RXFUNC_BADCALL;
	}

	while(!rc && last == query_counter(COUNTER_ID_CTLR_DONE))
	{
		if(!CONNECTED)
			rc = ENOTCONN;
		else if(IsHalted())
			rc = ECANCELED;
		else if(time(0) > end)
			rc = ETIMEDOUT;
		RunPendingEvents(1);
	}

	ReturnValue(rc);
 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270SetVisible		                              */
/*                                                                            */
/* Description: Set visibility state of pw3270 main window                     */
/*                                                                            */
/* Rexx Args:	0 to hide window, now zero to show		                      */
/*                                                                            */
/* Returns:	    0 if ok or error code                                         */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270SetVisible(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int rc = ENOENT;

	if(Argc != 1)
		return RXFUNC_BADCALL;

	if(program_window)
	{
		rc = 0;
		if(atoi(Argv[0].strptr))
			gtk_widget_show(program_window);
		else
			gtk_widget_hide(program_window);
	}

	ReturnValue(rc);
 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270WaitForString	                              */
/*                                                                            */
/* Description: Wait for desired string at position                           */
/*                                                                            */
/* Rexx Args:	String to search                                              */
/*                                                                            */
/* Rexx Args:	Buffer position                                               */
/*				String to search                                              */
/*                                                                            */
/* Rexx Args:	Start row                                                     */
/*				Start col                                                     */
/*				String to search                                              */
/*                                                                            */
/* Returns:	    0 if ok or error code                                         */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270WaitForString(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int			pos = -1;
 	char		*str;
 	int			sz;
 	int			rc = ETIMEDOUT;
 	int			rows,cols,row,col;
 	char		*buffer;
	time_t 	end = time(0)+RX3270_DEFAULT_TIMEOUT;

	get_3270_terminal_size(hSession,&rows,&cols);

 	switch(Argc)
 	{
	case 1:	// Only string in any position
		str = GetStringArg(Argv);
		sz = (rows*cols);
		break;

	case 2:	// Just buffer position
		pos = atoi(Argv[0].strptr);
		str = GetStringArg(Argv+1);
		sz  = strlen(str);
		break;

	case 3:	// String att row/col

		row = atoi(Argv[0].strptr)-1;
		col = atoi(Argv[1].strptr)-1;

		if(row < 0 || row > rows || col < 0 || col > cols)
		{
			ReturnValue(EINVAL);
		}

		pos = (row * cols) + col;
		str = GetStringArg(Argv+2);
		sz	= strlen(str);
		break;

	default:
		return RXFUNC_BADCALL;
 	}

	buffer = malloc(sz+2);
	if(!buffer)
	{
		ReturnValue(ENOMEM);
	}

	RunPendingEvents(0);

	Trace("%s waiting for \"%s\"",__FUNCTION__,str);

	while(rc == ETIMEDOUT  && (time(0) <= end))
	{
		if(!CONNECTED)
		{
            rc = ENOTCONN;
		}
		else if(IsHalted())
		{
			rc = ECANCELED;
		}
		else if(query_3270_terminal_status() == LIB3270_STATUS_BLANK)
		{
			screen_read(buffer,pos,sz);
			*(buffer+(sz+1)) = 0;
			if(strstr(buffer,str))
				rc = 0;
		}

		RunPendingEvents(1);

	}

	Trace("%s returns %d",__FUNCTION__,rc);

	ReleaseStringArg(str);
	free(buffer);

	ReturnValue(rc);
 }

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270IsTerminalReady                              */
/*                                                                            */
/* Description: Check if the terminal is ready                                */
/*                                                                            */
/* Rexx Args:   None                                                          */
/*                                                                            */
/* Returns:	    Non zero if terminal is ready                                 */
/*                                                                            */
/*----------------------------------------------------------------------------*/
RexxReturnCode REXXENTRY rx3270IsTerminalReady(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
{
    if(Argc)
		return RXFUNC_BADCALL;

	if(!CONNECTED || query_3270_terminal_status() != LIB3270_STATUS_BLANK)
	{
		ReturnValue(0);
	}

	ReturnValue(1);
}

/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Rexx External Function: rx3270WaitForTerminalReady                         */
/*                                                                            */
/* Description: Wait for terminal status is ready                             */
/*                                                                            */
/* Rexx Args:	timeout                                                       */
/*                                                                            */
/* Rexx Args:	None for 1 second timeout.                                    */
/*                                                                            */
/* Returns:	    0 if ok or error code                                         */
/*                                                                            */
/*----------------------------------------------------------------------------*/
 RexxReturnCode REXXENTRY rx3270WaitForTerminalReady(PSZ Name, LONG Argc, RXSTRING Argv[],PSZ Queuename, PRXSTRING Retstr)
 {
 	int end = time(0);
 	int rc = ETIMEDOUT;

 	switch(Argc)
 	{
	case 0:
		end++;
		break;

	case 1:
		end += atoi(Argv[0].strptr);
		break;

	default:
		return RXFUNC_BADCALL;
 	}

	Trace("%s waiting for %d seconds",__FUNCTION__,(int) (end - time(0)));

	while( (rc == ETIMEDOUT) && (time(0) <= end) )
	{
		RunPendingEvents(TRUE);

		if(!CONNECTED)
            rc = ENOTCONN;
		else if(IsHalted())
			rc = ECANCELED;
		else if(query_3270_terminal_status() == LIB3270_STATUS_BLANK)
			rc = 0;
	}

	Trace("%s exits with rc=%d",__FUNCTION__,rc);

	ReturnValue(rc);
 }