/*
**  CxKiller.c : Gael Marziou
**
**  A simple program to kill Commodties safely.
**  Without any argument it kills all commodities.
**  Arguments must be a list of Commodities.names
**  Names are case sensitive.
**
**  29 Nov. 93
**  09 Jan. 94 -- Mike Cuddy (fensende!mcuddy@apple.com)
**		added 'DISABLE/ENABLE' argument (as first arg), to send
**		CXCMD_DISABLE / CXCMD_ENABLE instead of CXCMD_KILL
*/

#include <exec/libraries.h>
#include <exec/memory.h>
#include <libraries/commodities.h>
#include <dos/dos.h>
#include <string.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/commodities_protos.h>

UBYTE *version = "$VER: CxKiller 1.2";

#ifdef __SASC
int CXBRK(void) { return(0); }  /* Disable SAS_C CTRL/C handling */
int chkabort(void) { return(0); } 
#include <pragmas/commodities_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#endif

/*** private functions of "commodities.library" ***/

#pragma libcall CxBase FindBroker 6c 801
#pragma libcall CxBase CopyBrokerList ba 801
#pragma libcall CxBase FreeBrokerList c0 801
#pragma libcall CxBase BrokerCommand c6 802

CxObj *FindBroker(char *);
LONG CopyBrokerList(struct List *);
LONG FreeBrokerList(struct List *);
LONG BrokerCommand(char *, LONG id);

struct Library *CxBase;

/*** private structures & defines ***/

struct BrokerCopy	{
	struct Node	bc_Node;
	char	bc_Name[CBD_NAMELEN];
	char	bc_Title[CBD_TITLELEN];
	char	bc_Descr[CBD_DESCRLEN];
	LONG	bc_Task;
	LONG	bc_Dummy1;
	LONG	bc_Dummy2;
	UWORD	bc_Flags;
};

#define COF_ACTIVE 2


__inline void 
SendToAllBrokers (LONG Id)
{
	struct List *l;
	struct Node *n, *nn;

    if (l = AllocVec(sizeof(struct List),MEMF_PUBLIC)) {
		NewList(l);
		CopyBrokerList(l);
		for (n = l->lh_Head; n && (nn = n->ln_Succ); n = nn)
			BrokerCommand((char *)((struct BrokerCopy *)n)->bc_Name,Id);
		FreeBrokerList(l);
		FreeVec(l);
    }
}

void 
main(int argc, char *argv[])
{
	unsigned char i;
	LONG Id = CXCMD_KILL;

    /* Before bothering with anything else, open the library */
    if (CxBase = OpenLibrary("commodities.library", 37L))
    {
		if (argv[1]) {
		    if (stricmp(argv[1],"DISABLE")==0) {
			Id = CXCMD_DISABLE;
			argv++; argc--;
		    } else if (stricmp(argv[1],"ENABLE")==0) {
			Id = CXCMD_ENABLE;
			argv++; argc--;
		    }
		}
		if (argc < 2) 
		{	/* no argument given, send to all brokers. */
			SendToAllBrokers(Id);
		}
		else
		{
			for (i=1; i<argc ; i++)
			{
			    BrokerCommand((char *)argv[i], Id);
			}
		}
	}
	CloseLibrary(CxBase);
}



