/*
Failat 1
SC MAKEGST dialup.gst NOLINK dialup.h
QUIT $RC
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <exec/devices.h>
#include <dos/dos.h>
#include <devices/serial.h>
#include <devices/timer.h>
#include <devices/sana2.h>
#include <exec/errors.h>
#include <intuition/intuition.h>
/* #include <usesysbase.h> */
/* delete this line or put ' */
#define __USE_SYSBASE
/* ' in this file, it's neccessary if you do a GST */
/* MGST ignores any #defines and declarations in the .c file since it is precompiling your HEADER FILES. */
/* Library Reference Manual, Chapter 4, Page 30 */
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>

struct SP
{
	UBYTE	serDevName[100];
	ULONG	serUnit;
	ULONG	serBaudRate;
	BOOL	listen2CD;
	BOOL	serHWHS;
};

struct argspec
{
	const UBYTE		*name;
	LONG			deflt;
};

enum argnames
{
	A_CMD,
	A_PN,
	A_RDD,
	A_MCT,
	A_RGS,
	A_WBW,
	A_WUS,
	A_LIP,
	A_LTO,
	A_LIN,
	A_PWP,
	A_PW,
	A_SLIP,
	A_SLIN,
	A_SPWP,
	A_SPW,
	A_LOK,
	A_LOT,
	A_VERBOSE,
	A_TEST,
	A_NSI,
	A_NDI,
	A_RPF,
	A_QUIET,
	A_STE,
	A_AA,
	A_NRC,
	A_SAD,
	A_SAU,
	A_SCF,
	A_CS,
	A_MSC,
	A_MSH,
	A_MQC,
	A_MRC,
	A_MHC,
	A_MOK,
	A_MNC,
	A_MLB,
	A_MLD,
	A_MRG,
	A_MAC,
	A_MAAC,
	A_MOC,
	A_MLC,
	A_MTC,
	A_MTO,
	A_MOH,
	A_ALL,
	A_END
};

#define WS(string) (string), sizeof(string)
#define ABBREV(arg, key)                ( !strnicmp( ( arg ), ( key ), strlen( key ) ) )
#define CUP(msgstr, res)		{msg(msgstr);	                result = (res); goto cleanup;}
#define CUP1(msgstr, arg1, res)		{msg((msgstr), (arg1));	        result = (res); goto cleanup;}
#define CUP2(msgstr, arg1, arg2, res)	{msg((msgstr), (arg1), (arg2)); result = (res); goto cleanup;}
#define MAXMATCHSTRING 10
#define VARBUFSIZE 3000
#define TEMPLATESIZE 1000
#define RXBUFSIZE 10000
#define SERPORTNAME "TheOneAndOnlySLIPPort"
#define BREAKSTR "\n*BREAK*\n"
#define VERIFY(fr) { if ( !(fr) ) return(FALSE); }
#define _OK_ {return(TRUE);}
#define _FAIL_ {return(FALSE);}
#define loop for(;;)

BOOL	query(UBYTE *varbuf);
BOOL	hangup(UBYTE *varbuf);
BOOL	connect(UBYTE *varbuf);
BOOL	take(UBYTE *varbuf);
BOOL	commandmode(UBYTE *varbuf);
BOOL	dropline(UBYTE *varbuf);
BOOL	carrier(void);
BOOL	checkconnect(UBYTE *buf);
BOOL	unique(struct MsgPort *serport);
void    initDefaultsAndTemplate(UBYTE *);
BOOL    initDefaultsTemplate(UBYTE *, LONG argd[]);
void	showdefaults(LONG argd[]);
BOOL	initSerialLine(struct SP *serpara);
void	buildMatchStringArray( UBYTE *pattern, UBYTE *buf, UBYTE *msa[] );
UBYTE * matchString( UBYTE *string, UBYTE *msa[]);
UBYTE *	sStrMatch(UBYTE *string, UBYTE *pattern, UBYTE *buf);
BOOL	sendLine( UBYTE *send );
BOOL	sendToSer( UBYTE *send );
BOOL    wuSend( UBYTE *send );   /* taylor made for the WAKEUPSTRING -GH- */
BOOL    sendWUSSer( UBYTE *send ); /* more taylormade stuffs -GH- */
BOOL    kickport( UBYTE *send ); /* more of them -GH- */
UBYTE *	expectFromSer( UBYTE *expect, USHORT timeout );
UBYTE *	exSend(	UBYTE *expect, USHORT timeout, UBYTE  *send );
UBYTE *	sendEx(	UBYTE *send,   UBYTE  *expect, USHORT timeout );
void	adjustdefs(void);

BOOL	ReadConfig(STRPTR sanacfgfile, USHORT sanaUnit, struct SP *sp);
VOID	msg(UBYTE *msgstr, ...);
VOID	kprintf(UBYTE *msgstr, ...);
