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

    MODULE
	SRunSX.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
	"SRunSX COMMAND commandstring" fails; U _MUST_ use
	"SRunSX commandstring"

    TODO
	?

    EXAMPLES
	> echo >  t:xxx "echo hallo"
	> protect t:xxx +s
	> t:xxx
	  hallo
	> "t:xxx" ; beware of the quotes!
	  EXECUTE: Can't open t:xxx
	  object not found
	> echo >"t:xxx " "echo error - this is the wrong script"
	> "t:xxx" ; beware of the quotes!
	  error - this is the wrong script
	> SRunSX "t:xxx"
	  hallo
	> SRunSX echo hallo
	  hallo

    SEE ALSO
	SRun

    HISTORY
	20-02-95 b_noll created SRun.*
	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
	10-04-95 b_noll copied 2 SRunSX.* Removed #ifdef PARSE added Examine

    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>
#include <string.h>
#include <exec/memory.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 "SRunSX 1.3 " __AMIGADATE__  + VERSIONOFFSET;
    long retval = RETURN_FAIL;
    struct ExecBase*SysBase = *ABSEXECBASE;
    struct Library* DOSBase;

    if (DOSBase = OpenLibrary (DOSNAME, 37)) {
	STRPTR template = FORMATPREFIX FORMAT;
	STRPTR a;
	BOOL sprot = NULL;

	a = GetArgStr();
	retval = RETURN_ERROR;

	do {
	    /* ---- Skip spaces */
	    //while ((*a == ' ') || (*a == '\t')) ++a;

	    /* ---- check for quoted script file */
	    //if (*a == '"')
	    {
		UBYTE buffer[MAXPATHLEN];
		BPTR lock;
		if (ReadItem (buffer, MAXPATHLEN, NULL) == ITEM_QUOTED)
		    if (lock = Lock(buffer, SHARED_LOCK)) {
			struct FileInfoBlock *fib;
			if (fib = AllocDosObject(DOS_FIB, NULL)) {
			    if (Examine (lock, fib))
				sprot = fib->fib_Protection & FIBF_SCRIPT;
			    FreeDosObject (DOS_FIB, fib);
			} /* if */
			UnLock(lock);
		    } /* if */
	    } /* if */

	    if (sprot) {
		STRPTR b;
		if (!(b = AllocVec(strlen(a) + 10, MEMF_CLEAR))) {
		    retval = 20;
		    break;
		} /* if */

		strcpy (b, "Execute ");
		strcpy (b + 8, a);
		a = b;
	    } /* if */

	    retval = SystemTagList (a, NULL);

	    if (sprot)
		FreeVec (a);
	} while (0);

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

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

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

/******************************************************************************
*****  END SRunSX.c
******************************************************************************/
