/*
 * RunHost
 *
 * USAGE: boolean = 'db/RunHost'(hostport,tags,hostargs)
 *
 * REQUIREMENTS:
 *  rexxreqtools.library
 *	REXX:dos/error
 *
 * RunHost let user launch the execution of any program in background
 * waiting for its message port...
 *
 * $(C): (1995, Rocco Coluccelli, Bologna)
 * $VER: RunHost 1.01 (13.Dec.1995)
 */

SIGNAL ON halt
SIGNAL ON break_c
SIGNAL ON syntax
SIGNAL ON failure

ADDRESS COMMAND

hostport = ARG(1)
IF hostport = '' THEN EXIT 0

IF SHOW('P',hostport) THEN EXIT 1

nl = '0a'x

/*
 *	Try to find the host in the database
 */
hostlist = 'REXX:db/hosts.lst'

IF OPEN('db',hostlist,'r') THEN
DO
	bl_sx = '/*' hostport || nl; bl_dx = nl || '*/' || nl
	PARSE VALUE READCH('db',65530) WITH sx (bl_sx) hostcmd (bl_dx) dx
	CALL CLOSE('db')

	IF Launch(ARG(3)) THEN EXIT 1

	/*
	 *	The command line is wrong, remove it from the database...
	 */
	IF hostcmd ~= '' THEN
		IF OPEN('db',hostlist,'w') THEN
		DO
			CALL WRITECH('db',sx || dx)
			CALL CLOSE('db')
		END
END

/*
 *	Add a new entry into the database
 */
lib = "rexxreqtools.library"; IF ~SHOW('L',lib) THEN CALL ADDLIB(lib,0,-30)

str.1 = 'Show host for:'
str.2 = 'Edit the command line.' || nl || 'All arguments will be added at the end.'
str.3 = 'Run Host'

/*
 *	TAGS:
 *		'opt.PUBSCR=' public screen name
 */
INTERPRET ARG(2)

/*
 *	Tags for reqtools requesters
 */
CALL rtFreeFileBuffer()
rt.CENTRE = 'rtez_flags=ezreqf_centertext'
rt.PUBSCR = 'rt_pubscrname=' || opt.PUBSCR

/*
 *	Ask for the program path
 */
hostpath = rtFileRequest('SYS:',,str.1 hostport,,rt.PUBSCR)
IF hostpath = '' | ~EXISTS(hostpath) THEN EXIT 0

/*
 *	Prepare the command line
 */
hostcmd = rtGetString('Run <>NIL:' hostpath,str.2,str.3,,rt.CENTRE rt.PUBSCR)
IF ~Launch(ARG(3)) THEN EXIT 0

/*
 *	Add line to the database
 */
'Echo >>'hostlist '"/** 'hostport'*n'hostcmd'*n***/"'

EXIT 1

syntax:
failure:
PARSE SOURCE INFLN
CALL 'dos/error'(RC,SIGL,INFLN,SOURCELINE(SIGL))

halt:
break_c:
	EXIT 0


Launch:
	IF hostcmd = '' THEN RETURN 0
	hostcmd ARG(1)
	'WaitForPort' hostport

RETURN SHOW('P',hostport)
