/*
.index main.c
 */
/************************************************************************
 *  vt100 terminal emulator with xmodem transfer capability
 *  written by Michael Mounier
 *  new version by Dave Wecker
 * Tek 4010 emulation added, and several bug fixes T.Whelan
 * this version has trouble at 19200 bps in tek mode but not in vt100
 * sugestions would be appreciated. I have managed for plain text ( no scroll)
 * data rates of about 15000 bps, but scrolling makes this much worse. This
 * version lets AmigaDOS do the X-On/X-Off as it wants to. Amiga Dos 1.2
 * will let the user control this.
 
 * T.Whelan
 * Dept of Physics & Astronomy
 * University of Iowa
 * Iowa City
 * IA 52244
 * 
 * SPAN: IOWA::WHELAN
 * bitnet - too unreliable.
 *
 *           860823 DBW - Integrated and rewrote lots of code
 *      v2.0 860809 DBW - Major rewrite
 *      v1.1 860720 DBW - Switches, 80 cols, colors, bug fixes
 *      v1.0 860712 DBW - First version released
 *
 *
 ************************************************************************/

/*  all includes defines and globals */
#define MODULE_MAIN
#include "vt100.h"
/******************************************************/
/*                   Main Program                     */
/*                                                    */
/*      This is the main body of the program.         */
/******************************************************/
FILE * tranr = NULL;
FILE * trans = NULL;
int send;

/*
.page.index main
 */
main(argc,argv)
int     argc;
char    **argv;
    {
    ULONG class;
    unsigned int code;
    int la, dola, actual;
    char c, *base, *this, *exitloop;

    base = NULL; 			/* avoid a Lattice warning message */
    InitDefaults (argc,argv);
    InitDevs ();
    InitTek (mywindow->UserPort);
    InitMenu ();
    
    KeepGoing =     TRUE;
    send      =     FALSE;
    maxcol    =     MAXX / 8;
    x         =     MINX ; 
    y         =     MINY; 
    curmode   =     0;
    script_on =     FALSE;
    script_wait=    TRUE;
    SetAPen (mywindow->RPort,1);
    cursoron ();
    cursoroff ();    
    emit (12);
    BeginIO (Read_Request);
    while( KeepGoing )
            {
            /* wait for window message or serial port message */
            cursoron ();
            if (script_wait)    /* if script ready dont wait here */
                Wait(
                 (1L << Read_Request->IOSer.io_Message.mn_ReplyPort->mp_SigBit) |
                 (1L << mywindow->UserPort->mp_SigBit) |
                 (1L << Script_Timer_Port->mp_SigBit));
            cursoroff ();
#if 0
            /* do ascii file send */
            if (send)
                {
                if ((c = getc (trans)) != EOF) {
                    if (c == '\n') c = '\r';
                    sendchar (c);
                    }
                else {
                    fclose (trans);
                    emits ("\nFile Sent\n");
                    send = FALSE;
                    }
                }
#endif
            /* see if there are any characters from the host */
            if (CheckIO (Read_Request)) 
		{
                WaitIO (Read_Request);
                doremote (rs_in[0]);
#if 0
                if (script_on) chk_script (rs_in[0]);
#endif
                Read_Request->IOSer.io_Command = SDCMD_QUERY;
                BeginIO (Read_Request);
                WaitIO (Read_Request);
                Read_Request->IOSer.io_Command = CMD_READ;
                actual = (int) Read_Request->IOSer.io_Actual;
                if (actual > 0) 
		    {
                    Read_Request->IOSer.io_Length =
                                               Read_Request->IOSer.io_Actual;
                    BeginIO (Read_Request);
	            WaitIO (Read_Request);
                    Read_Request->IOSer.io_Length = 1;

		    dola = 0;
		    la = 0;
		    this = &rs_in[0];
		    exitloop = this + actual;
                    for (; this < exitloop; this++) 
			{
#if 0
                        if (script_on) chk_script (&this);
#endif
                        if (dola != 0) 
			    {
                            if (isprint (*this)) 
				la++;
                            else 
				{
				/* ok, got a non printing char, let's dump
				 * whatever has come heretofore.
				 */
                                if (la > 0)
                                    emitbatch (la,base);
                                doremote (*this);
				/* reset state variables */
				la = 0;
                                dola = 0;
                                } /* end handling of non-printing chars */
                            }
                        else /* dola is == 0 */
			    {
                            doremote (*this);
			    /* doremote () has set these control flags: */
                            if (inesc < 0 && inctrl  <  0 && a[alt]  == 0) 
				{
                                dola = 1;
			        base = this + 1;
			        la = 0;
			        }
                            } /* endifthenelse (dola!=0) */
                        } /* end for loop(; this < exitloop; this++) */

                    /* dump anything left in the lookahead buffer */
                    if (la > 0) 
                        emitbatch (la, base);

                    } /* endif (actual > 0) */
                BeginIO (Read_Request);

                } /* endif (CheckIO (Read_Request)) */
/*
.page
 */
	    /* ok, you handled whatever i/o came from the serial port, now
	     * handle whatever the operator has for you from the menu or
	     * from the keyboard.
	     */
            while (NewMessage = (struct IntuiMessage *)
		GetMsg (mywindow->UserPort) )
                {
                class = NewMessage->Class;
                code = NewMessage->Code;
                ReplyMsg (NewMessage);
                switch( class )
                    {
                    case RAWKEY:
			c = toasc(code,0);
                    break;
                    case MENUPICK:
                        if ( code != MENUNULL ) 
			    menupick(code);
                    }/* end switch */

                }/* endwhile ( newmessage ) */

            if (!script_wait || 
                (CheckIO(&Script_Timer) && script_wait == WAIT_TIMER)) 
                do_script_cmd(NEXTCOMMAND);

            }  /* end while ( keepgoing ) */
                
    /*   It must be time to quit, so we have to clean
     *   up and exit.
     */
    CloseDevice (&Timer);
    DeletePort (Timer_Port);
    CloseDevice (&Script_Timer);
    DeletePort (Script_Timer_Port);
    CloseDevice (Read_Request);
    DeletePort (Read_Request->IOSer.io_Message.mn_ReplyPort);
    FreeMem (Read_Request,(long)sizeof(*Read_Request));
    CloseDevice (Write_Request);
    DeletePort (Write_Request->IOSer.io_Message.mn_ReplyPort);
    FreeMem (Write_Request,(long)sizeof(*Write_Request));
    ClearMenuStrip (mywindow);
    CloseWindow (mywindow);
    CloseTek ();
    exit (FALSE);
    } /* end main */
/*
.page.index do_capture
 */
do_capture (file)
char * file;
{
    static int capture;

    if (capture == TRUE)
        {
        capture = FALSE;
        fclose (tranr);
        emits ("\nEnd File Capture\n");
        }
    else
        {
        if (file == NULL) 
	    {
            emits ("\nAscii Capture:");
            filename (name);
            } 
        else 
	    strcpy(name, file);
        if ((tranr = fopen (name,"w")) == 0)
            {
            capture = FALSE;
            emits ("\nError Opening File\n");
            return (FALSE);
            }
        capture = TRUE;
        }
}/* end do_capture */
/*
.index do_send
 */
do_send (file)
char *file;
{
    if (send == TRUE)
        { 
        send = FALSE;
        fclose (trans);
        emits ("\nFile Send Cancelled\n");
        }
    else
        {
        if (file == NULL) 
	    {
            emits ("\nAscii Send:");
            filename (name);
            }
        else 
            strcpy(name, file);
        if ((trans = fopen (name,"r")) == 0)
            {
            send = FALSE;
            emits ("\nError Opening File\n");
            return (FALSE);
            }
        send = TRUE;
        }
}/* end do_send */
