/*
 *  ChkTeX v1.4, operating system specific code for ChkTeX.
 *  Copyright (C) 1995-96 Jens T. Berger Thielemann
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Contact the author at:
 *		Jens Berger
 *		Spektrumvn. 4
 *		N-0666 Oslo
 *		Norway
 *		E-mail: <jensthi@ifi.uio.no>
 *
 *
 */


/*
 * Some functions which have to be made different from OS to OS,
 * unfortunately...:\
 *
 */

#ifdef AMIGA
#	define	__USE_SYSBASE
#	include <dos.h>
#	include <dos/dos.h>
#	include <dos/dosasl.h>
#	include <proto/dos.h>
#	include <proto/exec.h>
#	include <signal.h>
#endif

#include "ChkTeX.h"




/*  -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=-  */

#ifdef AMIGA
const char
	VersString [] = "$VER: ChkTeX 1.4 " __AMIGADATE__
	" Copyright (c) 1995-96 Jens T. Berger Thielemann <jensthi@ifi.uio.no>",
	__stdiowin [] = "CON:0/10/640/180/ChkTeX",
	__stdiov37 [] = "/AUTO/CLOSE/WAIT";

static
	struct AnchorPath	*AnchorPath = NULL;

static void		KillAnchorPath(void);
static STRPTR	InitAnchorPath(STRPTR String);
#endif


/*
 * This is the name of the global resource file.
 */

#ifndef DATADIR
#   ifdef AMIGA
#       define DATADIR  "S:"
#   elif defined(__unix__)
#       define DATADIR "/usr/local/lib/"
#   else
#       define DATADIR
#   endif
#endif
#define RCBASENAME              "chktexrc"
#define LOCALRCFILE             "." RCBASENAME

UBYTE   ConfigFile [BUFSIZ] = LOCALRCFILE;


/*  -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=-  */


/*
 * Modify this one to suit your needs. In any case, it should fill
 * the ConfigFile (sized BUFLEN) buffer above with full name & path
 * for the configuration file. The macro RCFILE will give you the
 * filename part of the file, if you need that.
 *
 * If you choose to do nothing, only the current directory will be
 * searched.
 *
 */


enum LookIn
{
    liCurDir,
    liEnvir,
    liUsrDir,
    liSysDir,
    liNFound,
    liMax
};


void SetupVars(void)
{
    STRPTR Env;
#ifdef __MSDOS__
    STRPTR Ptr;
#endif
    enum LookIn i;

    for(i = 0;
	i < liMax;
	i++)
    {
	switch(i)
	{
	case liCurDir:  /* Current directory */
	    strcpy(ConfigFile, LOCALRCFILE);
	    break;
	case liEnvir:  /* Environment defined */
	    if((Env = getenv("CHKTEXRC")))
	    {
		strcpy(ConfigFile, Env);
		tackon(ConfigFile, LOCALRCFILE);
	    }
	    else
		*ConfigFile = 0;
	    break;
	case liUsrDir: /* User dir for resource files */
#ifdef AMIGA
	    if(DOSBase->dl_lib.lib_Version < 36)
		strcpy(ConfigFile, "S:");
	    else
		strcpy(ConfigFile, "ENV:");

	    tackon(ConfigFile, LOCALRCFILE);
#elif defined(__unix__)
	    if((Env = getenv("HOME")))
	    {
		strcpy(ConfigFile, Env);
		tackon(ConfigFile, LOCALRCFILE);
	    }
	    else
		*ConfigFile = 0;
#elif defined(__MSDOS__)
            strcpy(ConfigFile, PrgName);
            if(Ptr = strrchr(ConfigFile, '\\'))
            {
                strcpy(++Ptr, RCBASENAME);
            }
            else
                *ConfigFile = 0;
#endif
	    break;
	case liSysDir: /* System dir for resource files */
#ifdef __unix__
	    if(!fexists(ConfigFile))
	    {
		strcpy(ConfigFile, DATADIR);
		tackon(ConfigFile, RCBASENAME);
	    }
	    else
		*ConfigFile = 0;
#endif
	    break;
	case liNFound:
	case liMax:
	    PrintPrgErr(pmNoRsrc);
	}

	if(*ConfigFile && fexists(ConfigFile))
	    break;
    }
}

/*  -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=-  */

#ifdef AMIGA

/*
 * This function should be called first with String being a file pattern.
 * It should then return the first filename matching that pattern.
 * Subsequent calls will be made with String = NULL; it should then return
 * any other filenames that matches the same pattern.
 * If the first call is made with NULL, you should return NULL.
 * It may be called more than once with String != NULL, however, you need
 * only to keep track of the last invocation.
 *
 * Don't assume that the String passed will live on until the next
 * invocation, though. strdup() it if you need it after the first time.
 */


STRPTR MatchFileName(STRPTR String)
{
    STRPTR	Retval = NULL;

    if(AnchorPath || String)	/* Is this the first invocation? */
    {
	if(DOSBase->dl_lib.lib_Version > 36)
	{
	    ifn(AnchorPath)
	    {
		if(AnchorPath = malloc(sizeof(struct AnchorPath) + FMSIZE))
		{
		    Retval = InitAnchorPath(String);
		    atexit(&KillAnchorPath);
		}
	    }
	    else
	    {
		if(String)
		{
		    MatchEnd(AnchorPath);
		    Retval = InitAnchorPath(String);
		}
		else
		{
		    ifn(MatchNext(AnchorPath))
			Retval = AnchorPath->ap_Buf;
		}
	    }
	}
	else		/* ARP support may be added in the future */
	    Retval = String;

	if(AnchorPath && AnchorPath->ap_FoundBreak)
	    raise(SIGINT);
    }

    if(!Retval && String)
	Retval = String;

    return(Retval);
}

static void KillAnchorPath(void)
{
    MatchEnd(AnchorPath);
}

static STRPTR InitAnchorPath(STRPTR String)
{
    STRPTR	Retval = NULL;

    AnchorPath->ap_BreakBits = SIGBREAKF_CTRL_C;
    AnchorPath->ap_Strlen = FMSIZE;
    AnchorPath->ap_FoundBreak = FALSE;
    AnchorPath->ap_Flags = 0L;

    ifn(MatchFirst(String, AnchorPath))
	Retval = AnchorPath->ap_Buf;
    else
	PrintPrgErr(pmNoFileMatch, String);

    AnchorPath->ap_Flags &= ~(APF_DODIR);
    return(Retval);
}

#else
STRPTR MatchFileName(STRPTR String)
{
    return(String);
}
#endif


