
/******************************************************************************

    MODULE
	IconR.c

    DESCRIPTION
	IconX replacement (should be compatible 2 IconX)

    NOTES
	Kickstart 2.0+ required
	compiles w/ SAS/C v6.51

    BUGS
	none known

    TODO

    EXAMPLES

    SEE ALSO

    INDEX

    HISTORY
	20-02-95 b_noll created
	21-02-95 b_noll added version/format-prefix/offset
	20-03-95 b_noll added args diagnostics

    AUTHOR
	Bernd Noll, Brunnenstrasse 55, D-67661 Kaiserslautern
	b_noll@informatik.uni-kl.de

******************************************************************************/

/**************************************
	    Includes
**************************************/

#ifndef   EXEC_LIBRARIES_H
# include <exec/libraries.h>
#endif /* EXEC_LIBRARIES_H */

#ifndef   CLIB_EXEC_PROTOS_H
# include <clib/exec_protos.h>
#endif /* CLIB_EXEC_PROTOS_H */

#ifndef   DOS_DOS_H
# include <dos/dos.h>
#endif /* DOS_DOS_H */

#ifndef   CLIB_DOS_PROTOS_H
# include <clib/dos_protos.h>
#endif /* CLIB_DOS_PROTOS_H */

#include <proto/dos.h>
#include <proto/exec.h>

/* ******************** USER INCLUDES ******************** */

#include <dos/dostags.h>

/* ******************** USER INCLUDES ******************** */

/**************************************
	 Defines & Structures
**************************************/

#ifndef ABSEXECBASE
#define ABSEXECBASE ((struct ExecBase **)4L)
#endif

struct _arg {
/* ******************** USER FORMAT ******************** */

#define FORMAT "COMMAND/F"

    STRPTR command;

/* ******************** USER FORMAT ******************** */
}; /* struct _argv */

#define MAXPATHLEN 256
#define MAXLINELEN 256

#define VERSIONPREFIX	"\0$VER: "
#define VERSIONOFFSET	0
#define FORMATPREFIX	"\0$ARG: "
#define FORMATOFFSET	7

/**************************************
	    Implementation
**************************************/

long _main (void)
{
    const char* version = VERSIONPREFIX "IconR 1.0 " __AMIGADATE__  + VERSIONOFFSET;
    long retval = RETURN_FAIL;
    struct ExecBase*SysBase = *ABSEXECBASE;
    struct Library* DOSBase = NULL, *IconBase = NULL;
    struct WBStartup *wbs;
    struct Process *pr = FindTask(NULL);

    /* ---- No CLI-Start allowed! */
    if (pr->pr_CLI)
	return 20;

    /* ---- Get WBStartup argument */
    WaitPort (&pr->pr_MsgPort);
    while (!(wbs = (struct WBStartup *)GetMsg(pr->pr_MsgPort)));
    //Remove (&wbs->sm_Message.mn_Node);

    do {
	/* ---- Open libraries */
	if (!(DOSBase = OpenLibrary (DOSNAME, 37)))
	    break;
	if (!(IconBase = OpenLibrary("icon.library", 37)))
	    break;

	do {
	    /* /K */
	    if (!(winname = FindToolType (..., "WINDOW")))
		winname = StrDup(owinname = "CON:0/50//80/IconR/AUTO/CLOSE/WAIT");

	    /* /K/N */
	    if (!(stacksizestr = FindToolType(..., "STACK")) ||
		!(stacksize = atol(stacksizestr)))
		    stacksize = 4000;

	    /* /K/T */
	    if (!(usershell = FindToolType(..., "USERSHELL")))
		;

	    /* /K/T */
	    if ((owinname) && (!(dowait = FindToolType(...,"WAIT")) || !MatchToolValue(dowait, "YES")))
		StrDel(winname,...,...);

	    /* /K/T */
	    if ((owinname) && (!(dowait = FindToolType(...,"DELAY")) || !MatchToolValue(dowait, "YES")))
		StrDel(winname,...,...);

	    /* new suggested tooltypes:
		CLIARGS     added to clicommand before file arguments /* /K/M */
		CLICOMMAND  replaces filename			      /* /K/M */
		EXECUTE=YES use or don't use Execute                  /* /K/T */
		FAILAT=xx   instead of 100			      /* /K/N */
		REXX	    do sendrexx instead of System()           /* /K/T */
	    */
	    /*
		current template string is
		"FailAt 100
		Execute \"%s\""
	    */

	    if (!(iostream = Open (winname, MODE_READWRITE))) {

	    } /* if */

	    createcommandstring();


	    retval = SystemTags (command,
		SYS_Input,  iostream,
		SYS_Output, NULL,
		//SYS_Asynch, TRUE,
		SYS_UserShell, ...,
		TAG_END);

	} while (0);

	/* ---- Free Temp Data */
	if (iostream)
	    Close (iostream);
	if (winname)
	    Free(winname);
	if (command)
	    Free(command);

    } while (0);

    /* ---- close libraries */
    if (IconBase)
	CloseLibrary (IconBase);
    if (DOSBase)
	CloseLibrary (DOSBase);

    /* ---- Reply WBStartup Msg (Forbid() prevents WB from UnLoadSeg()'ing us before rts is called) */
    Forbid();
    ReplyMsg (&wbs->sm_Message);
    return 0;
} /* _main */

/******************************************************************************
*****  END IconR.c
******************************************************************************/
