/* chain.c - another *** gangway - spawn (&exit) made easy for RSX DECUS-C */


/*
	status = chain ( command ) ; assumes event flag 2 : spawns MCR...

	status:		directive status word : panic if it is not 1
			1 == IS_SUC

	command:	command line to task
			string (char *)
			may be lower case
			no > prompt will be given by mcr when
				command completes.
			exit()
*/

/*	function:

			spawn the task
			exit() which normally closes C files,
				releases devices etc.
*/

/* read cx.doc for spwn() */


#include <cx.h>				/* define IS_SUC */
#define EFN 2				/* we use this event flag # */

int	chain(command)
char	*command;
{
 long	tsk;				/* rad50 task name */
 int	dsw;				/* directive status word */
 int	len;				/* length of command excl '\033' */

	ascr50(6,"MCR...",&tsk);
/*
printf("%lo\n",tsk);	one day the above will be a constant (sigh)
*/

/*
 * we do a really VILE thing here: we want to end the command with '\033'
 * so we replace the caller's string terminator '\0' with '\033',
 * spawn the command, then (we could) change it back to '\0'.
 * this will fail if string is in ROM or otherwise sensitive.
 * this is a bug not a feature - beware!
 */
	len = strlen(command);
	command[len] = 033;	/* escape */
	dsw = spwn(&tsk,0,EFN,0,0,command,len+1);
	if	(dsw!=IS_SUC)	error("\7chain dsw=%oo",dsw);
	exit();
/*	command[len] = 0; */	/* EOS */
}

/*	and now for the testing ...	*/

#undef	testing	!	/* define to assemble a main() to test */
			/* undefine to just use as a library module */

/*
	to build:

	>#C Chain
	>#T chain

*/

#ifdef testing

main()
{
	char		cmd[100];	/* the command to the spawned task */

	printf("command to attempt :");
	gets(cmd);
	printf("command = \"%s\"\n",cmd);

	printf("chain returned %oo\n",chain(cmd));
}

#endif


/* end: chain.c */
