/*
 *	Copyright (C) 1986  SRI International
 *	Copyright (C) 1991  TGV, Incorporated
 *
 *	Suspend execution of a program:  If there is a parent
 *					 command interpreter, resume it.
 *					 If not, spawn a command interpreter
 *
 */
#include <jpidef.h>

suspend_program()
{
	struct {
		unsigned short int Size;
		unsigned short int Type;
		unsigned char *Buffer;
		unsigned int  *Resultant_Size;
		} JPI_Item_List[2];
	int Status;
	int Parent_PID;
	static int Sub_Process_PID;

	if (getenv("MULTINET_DISABLE_SPAWN")) return;

	if (!getenv("MULTINET_PUSH_DOES_SPAWN")) {
		/*
		 *	Under VAX-11 "C" we 1st try to attach any parent CLI
		 */
		Parent_PID = 0;
		JPI_Item_List[0].Size = sizeof(Parent_PID);
		JPI_Item_List[0].Type = JPI$_OWNER;
		JPI_Item_List[0].Buffer = (unsigned char *)&Parent_PID;
		JPI_Item_List[0].Resultant_Size = 0;
		JPI_Item_List[1].Size = 0;
		JPI_Item_List[1].Type = 0;
		Status = sys$getjpiw(0,0,0,JPI_Item_List,0,0,0);
		/*
		 *	If the Parent PID was not 0, attach it
		 */

		if (Parent_PID != 0) {
			Status = lib$attach(&Parent_PID);
			return;
		}
	}
	/*
	 *	No parent -- if we have a saved CLI, attach it
	 */
	if (Sub_Process_PID != 0) {
		Status = lib$attach(&Sub_Process_PID);
		if (Status & 1) return;
	}
	/*
	 *	Spawn a new CLI
	 */
	Sub_Process_PID = 0;
	Status = lib$spawn(0,0,0,0,0,&Sub_Process_PID,0,0,0,0,0,0);
	return;
}
