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

    MODULE
	Tackon.c

    DESCRIPTION
	Get three strings on Commandline
	a path, a name and a suffix,
	build a filename from these three
	strings and write it to StdOut

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

    BUGS
	there is a strange behaviour if compiled w/ DICE;
	- I cannot say why, but If You do not specify
	each input-slot, You will get a Enforcer-Hits;
	for that reason, I'll distribute Chris' SAS-executable


    TODO

    EXAMPLES
	> tackon xx yy zz
	xx/yy.zz

	> tackon xx :yy -zz
	:yy-zz

    SEE ALSO
	Suffix, PathPart, FilePart

    INDEX

    HISTORY
	01-08-93 b_noll created
	20-02-95 b_noll restructured source, removed mstrcpy
	21-02-95 b_noll added version/format-prefix/offset
	27-02-95 b_noll removed an Enforcer Hit (huch!)

    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 <strings.h>

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

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

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

struct _arg {
/* ******************** USER FORMAT ******************** */
#define FORMAT "PATH,NAME/A,SUFFIX"

    STRPTR path;
    STRPTR name;
    STRPTR suffix;

#define isalnum(x) ((((x) <= '9') && ((x >= '0'))) || (((x) <= 'Z') && ((x >= 'A'))) || (((x) <= 'z') && ((x >= 'a'))))
#define BODY(x)      do{ x }while(0)
#define findlast(s)  BODY(char*td=(s);while(*td) ++td; (s)=td;)

/* ******************** 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 "Tackon 1.1 " __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 ******************** */

	    char* b2;
	    char* buffer;
	    if (buffer = AllocMem(MAXPATHLEN, 0)) {

		retval	= RETURN_OK;
		*buffer = 0;

		strcpy (buffer, argv.path);

		if (AddPart (buffer, argv.name, MAXPATHLEN-1)) {
		    b2 = buffer;
		    while (*b2) ++b2;
		    if (argv.suffix) {
			if (isalnum(*argv.suffix)) {
			    *b2   = '.';
			    ++b2;
			} /* if */
			strcpy (b2, argv.suffix);
		    } /* if */

		    PutStr (buffer);
		    PutStr ("\n");
		} /* addpart */
		FreeMem (buffer, MAXPATHLEN);
	    } /* if */

/* ******************** USER BODY ******************** */
	    FreeArgs (args);
	} /* if */
	CloseLibrary (DOSBase);
    } /* if */
    return (retval);
} /* _main */

/******************************************************************************
*****  END Tackon.c
******************************************************************************/

