;/* InsertKey - compiles with SAS/C 5.10a by executing this file
lc -cfis -v -b0 -iinclude:intui.sym -iinclude:pragmas.sym -M -O -j73 InsertKey
blink InsertKey.o to InsertKey
quit ;*/
/*
*	InsertKey
*
*	Inserts an input event corresponding to the (commodities-style)
*	description of a keystroke give on the command line.
*
*	So e.g. 'InsertKey lcommand esc' would give you a shell on many
*	systems.
*
*	Martin W. Scott, Sunday 01-Nov-92.
*/
#include <exec/types.h>
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <libraries/commodities.h>
#include <proto/exec.h>		/* use PRAGMAS */
#include <proto/dos.h>
#include <proto/commodities.h>
#include <clib/alib_protos.h>
void FreeIEvents(struct InputEvent *);

#define PROJECT	"InsertKey"	/* program name */
#define ARGSTR	"KEY/A/F"	/* argument template */
#define NARGS	1	/* number of args in template */
#define KEY	0

LONG	main(void);		/* prototypes */

const char version_str[] = "$VER: InsertKey 1.0";
struct DosLibrary *DOSBase;
struct Library *CxBase;

BOOL InsertKey(char *);

LONG main(void)		/* entry: InsertKey */
{
	struct RDArgs *readargs;
	LONG rargs[NARGS];
	LONG rc = 20;

	if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L))
	{
		if (readargs = ReadArgs(ARGSTR, rargs, NULL))
		{
			if (InsertKey((char *)rargs[KEY]))
				rc = 0;
			else PutStr(PROJECT": Invalid string\n");
		}
		else PrintFault(IoErr(), PROJECT);

		CloseLibrary(DOSBase);
	}

	return rc;

} /* main */

struct InputEvent ie;

BOOL
InsertKey(char *str)
{
	IX ix;
	BOOL success = FALSE;

	if (CxBase = OpenLibrary("commodities.library", 37L))
	{
		if (!ParseIX(str, &ix))
		{
			ie.ie_Class = ix.ix_Class;
			ie.ie_Code = ix.ix_Code;
			ie.ie_Qualifier = ix.ix_Qualifier;
			AddIEvents(&ie);
			success = TRUE;
		}
	}

	return success;
}
