/*
 *	Command.c - Copyright © 1990 by S.R. & P.C.
 *
 *	Created:	01 Jul 1990
 *	Modified:	07 Jul 1990
 *
 *	Make>> make
 */

/*
#define DO_ARP_COPIES
#include <intuition/intuition.h>
#include <libraries/arpbase.h>
#include <functions.h>
*/

#include <dos_functions.h>
#include <arpdos_pragmas.h>

#define OK		1
#define CANCEL	2
#define STRING	3

/*****				global functions					*****/

void Command( void );

/*****				 local variables					*****/

static UBYTE Buffer[128];

static struct StringInfo Gadget3SInfo = {
	Buffer,	/* buffer where text will be edited */
	NULL,	/* optional undo buffer */
	0,	/* character position in buffer */
	128,	/* maximum number of characters to allow */
	0,	/* first displayed character buffer position */
	0,0,0,0,0,	/* Intuition initialized and maintained variables */
	0,	/* Rastport of gadget */
	0,	/* initial value for integer gadgets */
	NULL	/* alternate keymap (fill in if you set the flag) */
};

static SHORT BorderVectors1[] = {
	0,0,
	293,0,
	293,11,
	0,11,
	0,0
};

static struct Border Border1 = {
	-3,-2,	/* XY origin relative to container TopLeft */
	2,0,JAM1,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	BorderVectors1,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

static struct Gadget Gadget3 = {
	NULL,	/* next gadget */
	12,17,	/* origin XY of hit box relative to window TopLeft */
	288,10,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY,	/* activation flags */
	STRGADGET,	/* gadget type flags */
	(APTR)&Border1,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	NULL,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	(APTR)&Gadget3SInfo,	/* SpecialInfo structure */
	STRING,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

static SHORT BorderVectors2[] = {
	0,0,
	72,0,
	72,13,
	0,13,
	0,0
};

static struct Border Border2 = {
	-1,-1,	/* XY origin relative to container TopLeft */
	2,0,JAM1,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	BorderVectors2,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

static struct IntuiText IText1 = {
	1,0,JAM1,	/* front and back text pens, drawmode and fill byte */
	12,2,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"Cancel",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

static struct Gadget Gadget2 = {
	&Gadget3,	/* next gadget */
	229,36,	/* origin XY of hit box relative to window TopLeft */
	71,12,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY+ENDGADGET,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&Border2,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	&IText1,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	CANCEL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

static SHORT BorderVectors3[] = {
	0,0,
	55,0,
	55,13,
	0,13,
	0,0
};
static struct Border Border3 = {
	-1,-1,	/* XY origin relative to container TopLeft */
	2,0,JAM1,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	BorderVectors3,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

static struct IntuiText IText2 = {
	1,0,JAM1,	/* front and back text pens, drawmode and fill byte */
	17,2,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"Ok!",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

static struct Gadget Gadget1 = {
	&Gadget2,	/* next gadget */
	12,36,	/* origin XY of hit box relative to window TopLeft */
	54,12,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY+ENDGADGET,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&Border3,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	&IText2,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	OK,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

static struct NewWindow ReqNewWin = {
	0,11,	/* window XY origin relative to TopLeft of screen */
	312,54,	/* window width and height */
	0,1,	/* detail and block pens */
	GADGETUP+ACTIVEWINDOW,	/* IDCMP flags */
	WINDOWDRAG+WINDOWDEPTH+ACTIVATE+NOCAREREFRESH,	/* other window flags */
	&Gadget1,	/* first gadget in gadget list */
	NULL,	/* custom CHECKMARK imagery */
	(UBYTE *)"Enter command",	/* window title */
	NULL,	/* custom screen pointer */
	NULL,	/* custom bitmap */
	5,5,	/* minimum width and height */
	-1,-1,	/* maximum width and height */
	WBENCHSCREEN	/* destination screen type */
};


extern void Warn( const char * );

extern struct Window *Win;
extern char CmdWindow[];

static struct Window *ReqWin;

void Command(void)
{
	struct IntuiMessage *Message;
	ULONG Class;
	USHORT Code;
	struct Gadget *G;
	BPTR fh;
	BOOL End = FALSE;
	char readbuffer[2];

	ReqNewWin.BlockPen = Win->BlockPen;
	ReqNewWin.DetailPen = Win->DetailPen;
	if ( (fh = Open( CmdWindow , MODE_NEWFILE )) == NULL ) {
		Warn( "Can't open output window" );
		return;
	}
	if ( (ReqWin = OpenWindow(&ReqNewWin)) == NULL ) {
		Warn( "Can't open Requester" );
		Close( fh );
		return;
	}
	ActivateGadget( &Gadget3 , ReqWin , NULL );
	while( !End ) {
		Wait( 1L << ReqWin->UserPort->mp_SigBit );
		Message = (struct IntuiMessage *)GetMsg(ReqWin->UserPort);
		Class = Message->Class;
		Code = Message->Code;
		G = (struct Gadget *) Message->IAddress;
		ReplyMsg( (struct Message *)Message );
		switch(Class) {
		case GADGETUP :
			CloseWindowSafely( ReqWin , NULL );
			End = TRUE;
			switch( G->GadgetID ) {
			case OK :
			case STRING :
				Execute( (char *)Buffer , NULL , (struct FileHandle *)fh );
				Write( fh , "[33mHit return...[30m›0 p" , 27L );
				Read( fh , readbuffer , 1L );
				break;
			case CANCEL :
				break;
			}
			break;
		case ACTIVEWINDOW :
			ActivateGadget( &Gadget3 , ReqWin , NULL );
			break;
		default :
			; /* do nothing! */
		}
	}
	Close( fh );
}
