
static __inline long _start(void) { extern long _main(void); return _main(); }

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

    MODULE
	$(NAME).c

    DESCRIPTION
	FILTER
$(DESCRIPTION)

    NOTES
	Kickstart 2.0+ required
	compiles w/ Dice 2.07R - inline-pragmas required
	compiles w/ SAS/C v6.51

    BUGS
	none known
$(@IF@?BUGS@$(BUGS))

    TODO

    EXAMPLES
$(@IF@?EXAMPLES@$(EXAMPLES))

    SEE ALSO

    INDEX

    HISTORY
$(@IF@?HISTORY@$(HISTORY))

    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   EXEC_MEMORY_H
# include <exec/memory.h>
#endif /* EXEC_MEMORY_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>

$(@IF@?LIBRARIES@$(@FOREACH@LIBRARY@
#include <proto/$(@TOLOWER@$(NAME)).h>))

/**************************************
	    Structures
**************************************/

struct _gvars {
    struct DosLibrary *DOSBase;
    struct ExecBase   *SysBase;
    struct {
/* *************************** BEGIN USER FORMAT *************************** */
#define $(NAME)Fmt "$(@IF@?FORMAT@$(FORMAT))"                  /* CLI FORMAT */
							/* ****** STRUCTURES */
$(@IF@?ARGS@$(ARGS))

/* **************************** END USER FORMAT **************************** */
	STRPTR	to;
	STRPTR *file;
    } _argv;
    //struct ExecBase	*SysBase;
    //struct DosLibrary *DOSBase;
    //struct Library	*UtilityBase;
    //struct Library	*IconBase;
$(@IF@?LIBRARIES@$(@FOREACH@LIBRARY@
    struct Library *$(NAME)Base;))
    BPTR    output;
    BPTR    input;
    int     infnum;
    STRPTR  inname;
    APTR    userdata; /* this structure is reserved for "userdata" */
}; /* struct _gvars */


#define SysBase     gvars->SysBase
#define DOSBase     gvars->DOSBase
//#define UtilityBase gvars->UtilityBase
//#define IconBase    gvars->IconBase
$(@IF@?LIBRARIES@$(@FOREACH@LIBRARY@
#define $(NAME)Base gvars->$(NAME)Base))
#define outfile     gvars->output
#define infile	    gvars->input

#define argv	    (&gvars->_argv)

#define MAXPATHLEN  256
#define MAXLINELEN  256
#define EOF	    -1

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

/* static long _$(NAME) (struct _gvars *gvars); */
/* static BPTR _nextfile (struct _gvars *gvars) */

$(@IF@?APPEND@$(APPEND))

static __inline long $(NAME)Init (struct _gvars *gvars) {
    long retval = 0;
/* **************************** BEGIN USER INIT **************************** */

$(@IF@?INIT@$(INIT))

/* ***************************** END USER INIT ***************************** */
    return retval;
} /* $(NAME)Init */


static __inline long $(NAME)Fnc (struct _gvars *gvars) {
    long retval = 0;
/* **************************** BEGIN USER BODY **************************** */

$(@IF@?BODY@$(BODY))

/* ***************************** END USER BODY ***************************** */
    return retval;
} /* $(NAME)Fnc */

static __inline void $(NAME)Exit (struct _gvars *gvars) {
    //long retval = 0;
/* **************************** BEGIN USER EXIT **************************** */

$(@IF@?EXIT@$(EXIT))

/* ***************************** END USER EXIT ***************************** */
    //return retval;
} /* $(NAME)Exit */


static __inline BPTR _nextfile (struct _gvars *gvars) {
    if (!argv->file) {
	if (!infile)
	    infile = Input();
	else
	    return FALSE;
	gvars->inname = "STDIN";
    } else {
	if (infile)
	    Close(infile);

#ifndef PATTERNMATCHING

	do {
	    STRPTR fn;
	    fn = gvars->inname = argv->file[gvars->infnum++];
	    if (!fn)
		return FALSE;

	    infile = Open(fn, MODE_OLDFILE);
	    PrintFault(IoErr(), fn);
	} while (!infile);
#else
#error "Muss noch angepasst werden!"
    for (error = MatchFirst(pat, ap); error == 0; error = MatchNext(ap)) {
	...
    } /* for */
    MatchEnd(ap);
    if (error != ERROR_NO_MORE_ENTRIES) { /* abnormal error */
	...
    } /* if */

#endif
    } /* if */
    return infile;
} /* _nextfile */


long _main (void)
{
    const char* version = "$VER: $(NAME) 1.0 " __AMIGADATE__ ;
    long retval = 20;

#ifdef STACK_VARS
    struct _gvars _gvars  = { 0 };
# ifdef FIX_SASPP
    struct _gvars *gvars = (&_gvars);
# else
#   define gvars (&_gvars)
# endif

    SysBase = *((struct ExecBase**)4L);
#else
    //struct _gvars *gvars = (struct _gvars *)(-(LONG)sizeof(argv));
    struct _gvars *gvars = NULL;

# ifdef FIX_SASOPT /* might this be fixed w/ NoConstLibBase? */
    if ((ULONG)&SysBase == 4)
# endif
    if (gvars = AllocVec(sizeof (*gvars), MEMF_CLEAR)) {
	SysBase = *((struct ExecBase**)4L);
#endif

	if (DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 37)) {
	    struct RDArgs *args;
$(@IF@?LIBRARIES@$(@FOREACH@LIBRARIES@
	    if (!($(NAME)Base = OpenLibrary("$(@TOLOWER@$(NAME)).library, 37)))
		goto closelibs;)

	    retval   = 10;
	    if (args = ReadArgs( $(NAME)Fmt "TO/K,FILE/M", (LONG*)gvars, NULL)) {

		outfile = argv->to ? Open(argv->to, MODE_NEWFILE) : Output();

		if (outfile) {
		    if (!(retval = $(NAME)Init(gvars))) {
			while (_nextfile(gvars))
			    if (retval = $(NAME)Fnc (gvars))
				break;
			$(NAME)Exit(gvars);
		    } /* if */

		    if (argv->to)
			Close(outfile);
		} /* if */

		FreeArgs (args);
	    } /* if */
	    if (retval)
		PrintFault(IoErr(), "$(NAME)");

closelibs:

$(@IF@?LIBRARIES@$(@FOREACH@LIBRARIES@
	    if ($(NAME)Base)
		CloseLibrary($(NAME)Base);))
	    CloseLibrary ((struct Library *)DOSBase);
	} /* if */
#ifndef STACK_VARS
	FreeVec (gvars);
    } /* if */
#endif
    return retval;
} /* _main */

/******************************************************************************
*****  END $(NAME).c
******************************************************************************/

#ifdef TPLTER

LIBRARIES = {
};

#endif
