/*
**	$RCSfile: Request.c,v $
**	$Filename: Request.c $
**	$Revision: 0.1 $
**	$Date: 1995/04/15 17:33:51 $
**
**	sysmon.library support command Request (version 0.1)
**	
**	(C) Copyright 1995 by Etienne Vogt
*/

#include <exec/alerts.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <workbench/startup.h>
#define __USE_SYSBASE
#include <proto/exec.h>
#include <proto/dos.h>
#include <clib/alib_protos.h>
#include <string.h>

struct ExecBase *SysBase;
struct DosLibrary *DOSBase;
static struct WBStartup *wbmsg;
static struct RDArgs *myrda;

static UBYTE version[] = "$VER: Request 0.1 (6.8.95)";
static UBYTE template[] = "OFF/S,ON/S,WB/S";

#define	OPT_OFF	0
#define	OPT_ON	1
#define OPT_WB	2
#define OPTMAX	3

ULONG __saveds main(void);
static void cleanexit(ULONG rc);

ULONG __saveds main(void)	/* No startup code */
{
  struct Process *myproc;
  LONG opts[OPTMAX];

  SysBase = *(struct ExecBase **)4;
  DOSBase = NULL;
  wbmsg = NULL;
  myrda = NULL;

  myproc = (struct Process *)FindTask(NULL);
  if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",36)) == NULL)
  { Alert(AT_Recovery|AG_OpenLib|AO_DOSLib);
    return 100;
  }

  if (!(myproc->pr_CLI))		/* If started from WB, exit cleanly */
  { WaitPort(&(myproc->pr_MsgPort));
    wbmsg = (struct WBStartup *)GetMsg(&(myproc->pr_MsgPort));
    cleanexit(20);
  }
  else
  { memset((char *)opts, 0, sizeof(opts));
    if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
    { PrintFault(IoErr(),"Request failed");
      cleanexit(20);
    }
    if (opts[OPT_OFF]) myproc->pr_WindowPtr = (APTR)-1L;
    if (opts[OPT_WB]) myproc->pr_WindowPtr = NULL;
    if (opts[OPT_ON])
    { struct MsgPort *conport;
      struct InfoData *myinfo;
      APTR cliwin = NULL;

      if (myinfo = AllocVec(sizeof(struct InfoData), MEMF_PUBLIC | MEMF_CLEAR))
      { if (conport = GetConsoleTask())
	{ if (DoPkt1(conport, ACTION_DISK_INFO, MKBADDR(myinfo)) == DOSTRUE) cliwin = (APTR)myinfo->id_VolumeNode;
	}
	FreeVec(myinfo);
      }
      myproc->pr_WindowPtr = cliwin;
    }
    PutStr("System Requesters are ");
    switch ((LONG)myproc->pr_WindowPtr)
    { case 0:
	PutStr("On Default Public Screen.\n");
	break;
      case -1:
	PutStr("Disabled.\n");
	break;
      default:
	PutStr("On Window Screen.\n");
	break;
    }
  }

  cleanexit(0);
}

static void cleanexit(ULONG rc)
{
  if (myrda) FreeArgs(myrda);
  if (DOSBase) CloseLibrary((struct Library *)DOSBase);
  if (wbmsg)
  { Forbid();
    ReplyMsg((struct Message *)wbmsg);
  }
  Exit(rc);
}
