/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* |_o_o|\\ Copyright (c) 1986 The Software Distillery.  All Rights Reserved */
/* |. o.| || This program may not be distributed without the permission of   */
/* | .  | || the authors.                                                    */
/* | o  | ||    Dave Baker     Ed Burnette  Stan Chow    Jay Denebeim        */
/* |  . |//     Gordon Keener  Jack Rouse   John Toebes  Doug Walker         */
/* ======          BBS:(919)-471-6436      VOICE:(919)-469-4210              */ 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
 * VERY loosely based on the input.device example by Rob Peck, 12/1/85
 */

/* * * * * * * * * INCLUDE FILES * * * * * * * * * * * */
#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <exec/interrupts.h>
#include <exec/ports.h>
#include <exec/libraries.h>
#include <exec/io.h>
#include <exec/tasks.h>
#include <exec/execbase.h>
#include <exec/devices.h>
#include <devices/timer.h>
#include <devices/input.h>
#include <devices/inputevent.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <libraries/dos.h>
#include <graphics/gfxmacros.h>
#include <hardware/custom.h>
#include <hardware/dmabits.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <string.h>

/* too bad "#include <#?/#?.h>" doesn't work.... */



/* #defines and declarations for POPCLI */

/* * * * * * * * * * * CONSTANTS * * * * * * * * * * * * */
#define PORTNAME     "POPCLI4"
#define TIMEINTERVAL 1L      /* in seconds */
#define DEFTIME      300     /* five minute timeout */
#define MAXCMD       207     /* Might as well fill up a COMMAND_BLOCK */
#define DEFKEY       0x45    /* ESC */
#define DEFCMD       "NEWCLI"


/* * * * * * * * * * * GLOBAL VARIABLES * * * * * * * * * */

typedef struct COMMAND_BLOCK
   {
   short                   minmem;
   short                   minchip;
   unsigned char           cmdflags;
   char                    command[MAXCMD];
   } CommandBlock;

/* Two kludges to get around naming problems with #defines */
typedef struct GfxBase		*GBasePtr;
typedef struct IntuitionBase	*IBasePtr;

typedef struct
   {
   struct Task            *buddy;
   ULONG                   unblanksig;
   ULONG                   noevents;
   short                   blanksignum;
   struct MsgPort         *execport;
   struct Screen          *blankscreen;
   IBasePtr		   IBase;
   GBasePtr		   GBase;
   struct DosLibrary      *DosBase;
   struct Custom          *customptr;
   struct NewScreen        newscreen;
   CommandBlock           *keytab[256];
   BPTR                   stdout;
   } GLOBAL_DATA;

/* #defines for the global pointer. In _main, we #undef "global" */

#define global		(*gptr)
#define DOSBase		global.DosBase
#define IntuitionBase	global.IBase
#define GfxBase		global.GBase

/* KLUDGE!!! KLUDGE!!! KLUDGE!!! */
#define custom (*((struct Custom *)0xdff000))

struct OURMSG {
   struct Message          msgpart;
   char                    type;
   unsigned char           key;
   short                   interval;
   short                   minmem;
   short                   minchip;
   unsigned char           cmdflags;
   char                    command[MAXCMD];
};

#define OURMSGHDRSZ          6

void        _main(void);
int         popparse( GLOBAL_DATA *, char *, struct OURMSG * );
void        popbanner( GLOBAL_DATA * );
void        poptrailer( GLOBAL_DATA * );

void  __far copyproc( GLOBAL_DATA *, struct Process *, struct Process * );
void        termproc( GLOBAL_DATA *, struct Process * );

void        DeleteIOReq(GLOBAL_DATA *, struct IOStdReq *);
void        QueueTimer( GLOBAL_DATA *, struct timerequest *, ULONG);
struct IOStdReq *CreateIOReq(GLOBAL_DATA *, struct MsgPort *, int);
void        MyDeletePort( GLOBAL_DATA *, struct MsgPort * );
struct MsgPort *MyCreatePort( GLOBAL_DATA *, char * );
struct InputEvent *
__asm
myhandler(
  register __a0 struct InputEvent *ev,  /* pointer to a list of events */
  register __a1 GLOBAL_DATA *gptr);     /* Everything we need to know  */


struct CDIR {		/* Structure for process path chain */
	BPTR	next;	/* I hate BPTRs */
	BPTR	lock;	/* LOCK for this path component */
};

/*-------------------------------------------------------------------------*/
/* POPCLI internal constants                                               */

/*- Message types -*/

#define   MSG_NONE	0   /* Used only by parser */
#define   MSG_ADDKEY    1
#define   MSG_DELKEY    2
#define   MSG_EXECUTE   3
#define   MSG_SHUTDOWN  4
#define   MSG_BLANK     5
#define   MSG_UNBLANK   6
#define   MSG_SETTIME   7

/*- Flags in cmdflags (8 bits) -*/

#define   FLAG_NOWB2F   0x80   /* Don't WBenchToFront() before command   */

/*- Return codes from the parser -*/

#define   OK            0
#define   LEAVE         1
#define   SHUTDOWN      2

/*-------------------------------------------------------------------------*/
/* Structure for CBACK */

typedef struct {
   BPTR         Backstdout;     /* standard output when run in background */
   long         SyncSignal;	/* signal to terminate resident portion   */
   struct Task  *SyncProcess;   /* Task to terminate                      */
} CBACK_DATA;
