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

    MODULE
	ScriptName.c

    DESCRIPTION
	Tries to recognize the name of the currently
	active scriptfile.

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

    BUGS
	The result is most likely something like
	'T:Command-??-T??' with ? being numbers,
	if the system 'decided' to create a tempfile;
	Up to now we can not get another result,
	for if no tempfile is created, we get an
	empty string =8-(.

    TODO

    EXAMPLES

    SEE ALSO

    INDEX

    HISTORY
	16-02-95 b_noll created
	20-02-95 b_noll restructured source
	21-02-95 b_noll added version/format-prefix/offset
	27-02-95 b_noll replace BSTR extraction by FPrintf %b
	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>

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

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

struct _arg {
/* ******************** USER FORMAT ******************** */
#define FORMAT ","

    int dummy;

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

    if (DOSBase = OpenLibrary (DOSNAME, 37)) {
	struct _arg argv = { 0 };
	APTR   args;
	retval	 = RETURN_ERROR;
	if (args = (void*)ReadArgs(FORMATPREFIX FORMAT + FORMATOFFSET, (LONG*)&argv, NULL)) {
/* ******************** USER BODY ******************** */

	    //struct Process		  *pr;
	    struct CommandLineInterface *cli;
	    //pr = (struct Process *)FindTask(NULL);

	    if (cli = Cli()) {

		if (cli->cli_Interactive) {
		    retval = RETURN_ERROR;
		    SetIoErr(ERROR_OBJECT_WRONG_TYPE);
		    //PutStr ("* Not in a Script *\n");
		} else if (cli->cli_CommandFile) {
		    //from = BADDR(cli->cli_CommandFile);

/* Bah! this is in fact a _PSEUDO_ BSTR: 'len' is totally undefined */

		    //// this would be: get BSTR
		    //while (len--)
		    //	  if (!(buffer[len] = from[len]))
		    //	      buffer[len] = '\0';

		    retval = RETURN_OK;

		    FPrintf(Output(), "%b\n", cli->cli_CommandFile);
		} else {
		    retval = RETURN_WARN;
		    //PutStr ("* No ScriptName *\n");
		    SetIoErr(ERROR_OBJECT_NOT_FOUND);
		} /* if */
	    } else {
		SetIoErr(ERROR_OBJECT_WRONG_TYPE);
	    } /* if */

/* ******************** USER BODY ******************** */
	    FreeArgs (args);
	} /* if */

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

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

/******************************************************************************
*****  END ScriptName.c
******************************************************************************/

