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

    MODULE
	SRun.c

    DESCRIPTION
	Run a command synchroneously (usable e.g. to redirect a script)

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

    BUGS
	"SRun COMMAND commandstring" fails; U _MUST_ use
	"SRun commandstring"

    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
	09-04-95 b_noll shortened the file: arg Parsing dropped, removed NP_tags

    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 "SRun 1.2 " __AMIGADATE__  + VERSIONOFFSET;
    long retval = RETURN_FAIL;
    struct ExecBase*SysBase = *ABSEXECBASE;
    struct Library* DOSBase;

    if (DOSBase = OpenLibrary (DOSNAME, 37)) {
#ifdef PARSE
	struct _arg argv = { 0 };
	APTR   args;

	retval	 = RETURN_ERROR;
	if (args = (void*)ReadArgs(FORMATPREFIX FORMAT + FORMATOFFSET, (LONG*)&argv, NULL)) {

	    retval = RETURN_OK;
/* ******************** USER BODY ******************** */

#if 1

	    /* ---- this way does work */

	    retval = SystemTags (
		argv.command,
		//NP_Input, Input(),
		//NP_CloseInput, FALSE,
		//NP_Output, Output(),
		//NP_CloseOutput, FALSE,
		TAG_END);

	    if (retval == -1) {
		retval = RETURN_ERROR;
		//PrintFault(IoErr(), argv.command);
	    } /* if */

#else

	    /* ---- this way does _NOT_ work. Because the */
	    /*	    process waits for EOF on Input()?     */

	    if (!Execute (argv.command, Input(), Output())) {
		retval = RETURN_ERROR;
		//PrintFault (IoErr(), argv.command);
	    } /* if */

#endif

/* ******************** USER BODY ******************** */
	    FreeArgs (args);
	} /* if */
#else
	STRPTR template = FORMATPREFIX FORMAT;

	retval = SystemTagList (GetArgStr(), NULL);
	    //TAG_IGNORE,     template,
	    //TAG_END);

	if (retval == -1) {
	    retval = RETURN_ERROR;
	} /* if */

#endif

	if (retval > RETURN_WARN)
	    PrintFault(IoErr(), "SRun");

	CloseLibrary (DOSBase);
    } /* if */
    return (retval);
} /* _main */

/******************************************************************************
*****  END SRun.c
******************************************************************************/
