/*
**	$Filename: RawINTest.c $
**	$Release: 1.00 $
**	$Date: June 13, 1992 $
**	$Author: Sam Yee (sam@sfu.ca) $
**
**	Test program for RawIN.lib
*/

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <RawIN.h>		/* use #include <RawIN.h> if file in include: */

int CXBRK(void) {return(0);}	/* disable Lattice C ^C checking */

/* required for pragmas */
extern struct SysBase	*SysBase;
extern struct DOSBase	*DOSBase;

/*******************************************************************/
main(int argc, char *argv[])
{
    struct StandardPacket	packet;
    struct MsgPort		*port;	/* message port to receive message when
					   user types a character */
    char			InputChar,	/* when user types a character, this is
						   where it's put */
				quit = 0,	/* if true exit program */
				EOL,		/* true if user presses CR */
				killed = 0,	/* true if user pressed ^C */
				prompt[50],
				tempbuf[10];
    struct Line			*line;
    long			mask,		/* signal masks */
				returnmask,
				readcount,	/* used in RawGets() */
				i;
    struct History		*history;

    printf("\nRawIN test by Sam Yee.  May 8, 1992\n\n");
    sprintf(prompt,"%s> ",argv[0]);	/* make prompt */

    if (port = (struct MsgPort *)CreatePort(NULL,NULL))	/* create a port */
    {
		mask = 1L<<0xc | 1L<<port->mp_SigBit;	/* mask the signals */
        printf("AllocLine(100,10) -- 100 character buffer, 10 history lines.\n");
		if (line = AllocLine(100,10))	/* 100 characters for a line, 10 history lines */
		{
		    printf("\nLogging into the test program...\n\n\n\n");
		    printf("login: ");
		    RawGets(tempbuf,8,0,0,0,0);

		    printf("\nPassword:");
		    RawGets(tempbuf,8,0,0,0,1);
		    printf("\n\nPassword okay.  :-)\n\nWelcome to the test program...\n\n\n\n");
		    printf("MakeHistory(line,\"This\",5)\n");
		    MakeHistory(line,"This",5);
		    printf("MakeHistory(line,\"is\",3)\n");
		    MakeHistory(line,"is",3);
		    printf("MakeHistory(line,\"a\",2)\n");
		    MakeHistory(line,"a",2);
		    printf("MakeHistory(line,\"test.\",6)\n");
		    MakeHistory(line,"test.",6);
		    printf("ShowHistory(Output(),line,1L,-1L,0) -- ascending order.\n");
		    ShowHistory(Output(),line,1L,-1L,0);
		    printf("ShowHistory(Output(),line,-1L,-1L,1) -- descending order.\n");
		    ShowHistory(Output(),line,-1L,-1L,1);
		    printf("GetHistory(line,2L)...\n");

		    history = GetHistory(line,2L);
		    if (history)
				printf("%s\n",history->buf);

		    printf("DeleteHistory(line,1) -- delete first history line.\n");
		    DeleteHistory(line,1);
		    printf("DeleteHistory(line,-1L) --- delete last history line.\n");
		    DeleteHistory(line,-1L);
		    printf("ShowHistory(Output(),line,1L,-1L,0)...\n");
		    ShowHistory(Output(),line,1L,-1L,0);

		    RawMode(-1L);	/* go raw mode */
		    printf("RawMode(-1L) -- Go in raw mode.\n");
		    StartAsyncRead(&packet,port,&InputChar);	/* start the first read */
		    printf("StartAsyncRead() done.\n");

		    printf("Testing HOT Keys..\n\n");
		    for (i = 0; i < 49; ++i)
		    {
				printf("*** Smash any key to abort...\n");
				if (GetMsg(port))
				{
				    printf("You've pressed %c\n",InputChar);
				    StartAsyncRead(&packet,port,&InputChar);	/* read more */
				    break;
				}
		    }
		    printf("\n\nFor history list type history.  Press ^C to exit.\n\n");
		    printf(prompt);

		    while (!quit)
		    {
				if (GetMsg(port))	/* did user type anything? what about abort? */
				{
				    if (ReadDone(&packet))
						quit = 1;
				    else if (EOL = BuildLine(Output(),line,InputChar,-1L))	/* did user press CR? */
				    {
						printf("\nYou typed \"%s\".\n",line->buf);

						if (!strcmp(line->buf,"history"))
						{
						    printf("\nShowHistory()...\n\n");
				   			ShowHistory(Output(),line,1L,-1L,0);
						}
						ResetLine(line);	/* initialize line */
						printf("\nResetLine() done.\n\n");
						printf(prompt);
				    }
				    if (!killed && !quit)	/* did user press ^C? */
						StartAsyncRead(&packet,port,&InputChar);
				    else
						quit = 1;
				}
				else if (!quit)
				{
				    returnmask = Wait(mask);	/* sleep until something happens */
				    if (1L<<0xc & returnmask)	/* did user press ^C? */
				    {
						AbortRead(port);
						killed = 1;
						quit = 1;
				    }
				}
		    }
		    if (killed)
				printf("\n***Break\n");
		    printf("\nRawGets() with default option.\n\n");
		    printf("Do you like RawIN.lib? => ");
		    strcpy(tempbuf,"YES!");
		    readcount = RawGets(tempbuf,5,strlen(tempbuf),0,0,0);
		    if (readcount > 0)	/* if user had indeed typed something */
		 		printf("\nYou response was \"%s\".\n",tempbuf);

		    printf("\n\nRawGets() with hotkeys, CAPS, and no echo...\n\n");
		    printf("Would you consider using RawIN.lib in your programs?\n[Y/n] => ");
		    readcount = RawGets(tempbuf,2,0,1,1,1);
		    if (!readcount)
				printf("Why, of course! ;)\n");
		    else if (readcount > 0)
		    {
				if (tempbuf[0] == 'Y')
				    printf("Most definitely.\n");
				else
				    printf("No thanks, it's utterly useless for me.\n");
		    }
		    RawMode(0L);
		    FreeLine(line);
		    printf("\nFreeLine() done.\n\n");
		}
		DeletePort((struct MsgPort *)port);
    }
}

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