/*
**  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
*/


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

#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


void KillAllBrokers (void)
{
	struct List *l;
	ULONG nr;
	struct Node *n, *nn;

	if (l = AllocMem(sizeof(struct List), MEMF_PUBLIC))
	{
		/*** generate an empty list ***/
		NewList(l);

		/*** private function to generate the system broker list ***/
		nr = CopyBrokerList(l);
		
		/*** start at head of list ***/
		n = l->lh_Head;
		while (n && (nn = n->ln_Succ))
		{
			/*** kill current broker ***/
			BrokerCommand((char *)&((struct BrokerCopy *)n)->bc_Name, CXCMD_KILL);

			/*** next node ***/
			n = nn;
		}
		/*** free list and allocated mem ***/
		FreeBrokerList(l);
		FreeMem(l, sizeof(struct List));
	}
}

void KillOneBroker (char *BrokerName)
{
	BrokerCommand(BrokerName, CXCMD_KILL);
}



void main(int argc, char **argv)
{
	unsigned char i;

    /* Before bothering with anything else, open the library */
    if (CxBase = OpenLibrary("commodities.library", 37L))
    {
		if (argc < 2) 
		{	/* no argument given */
			KillAllBrokers();
		}
		else
		{
			for (i=1; i<argc ; i++)
			{
				KillOneBroker((char *)argv[i]);
			}
		}
	}
	CloseLibrary(CxBase);
}



