
/*
 *  MBTERM.C - 10/19/87 - Terminal emulators and things.
 */

#include "mb.h"
#ifdef MCH_AMIGA
extern char hostport;
extern char tmpstr[];
#endif
char *talkm1, *talkm2, *talkm3, *talkm4;

/*
 *  User wants to talk to the console.
 */

utalk()
{
  register PORTS *p;
  register short i;

  p = port;
  prtx(talkm2);

  ioport(cport);
  prtx(talkm4);
#ifdef MCH_AMIGA
   initbeep();
#endif
  for (i = 0; i < 10; i++)
  {
    wait(2);
#ifndef MCH_AMIGA
    if (s_param & s_page) outchar(ctl_g);
#else
   beep();
#endif
    wait(1);
#ifndef MCH_AMIGA
    if (s_param & s_page) outchar(ctl_g);
#else
   beep();
#endif

    if (instat())
    {
      cport->fl = NULL;
      if (p->tmode)
      {
   ioport(p);
   cmdtnc(); convtnc();
      }
#ifdef MCH_AMIGA
      cursor_on();
#endif
      term(p);
#ifdef MCH_AMIGA
      cursor_off();
#endif
      ioport(p);
      if (p->tmode)
      {
   cmdtnc();
   trantnc();
      }
#ifdef MCH_AMIGA
   donebeep();
#endif
      return;
    }

    ioport(p);
#ifndef MCH_AMIGA
    if (instat()) return;
#else
   if(instat()) {
      donebeep();
      return;
   }
#endif
    ioport(cport);
  }
  ioport(p); port->msg = talkm3;
}

/*
 *  Console used as terminal to tnc.
 */

lterm()
{
  register PORTS *m, *s;

  m = cport;

  if (m->flds is 2)
  if ((m->fl = fopen(m->fld[1], "r")) isnt NULL)
  { fclose(m->fl); m->msg = mexst; return; }

  if ((s = findport(m->opt2)) is NULL) { m->msg = mnport; return; }
  if (s is m)                          { m->msg = mcant;  return; }

  if (!(s->flags & p_term))
  {
    if (!(s->mode & idle)) { m->msg = minuse; return; }
    ioport(s);

    switch(s->dev)
    {
#ifdef MCH_AMIGA
      case p_nulmdm:
#else
      case p_serial:
#endif
   prtx("*** CONNECTED to $O\n");
   break;

      default: ;
    }

    s->flags setbit p_term;
  }

  m->fl = NULL;
  if (m->flds is 2) m->fl = fopen(m->fld[1], "w");
#ifdef MCH_AMIGA
   if(hostport != 'A') sprintf(tmpstr,"talk.on.%c",hostport);
   else strcpy(tmpstr,"talk.on");
/* send some commands to TNC before talking to it */
   if(s->dev == p_tnc)inittnc(tmpstr,0);
   cursor_on();
#endif
  term(s);
#ifdef MCH_AMIGA
   cursor_off();
   if(hostport != 'A') sprintf(tmpstr,"talk.off.%c",hostport);
   else strcpy(tmpstr,"talk.off");
/* send some commands to TNC after talking to it */
   if(s->dev == p_tnc)inittnc(tmpstr,0);
#endif
  ioport(m);
  if (m->fl isnt NULL) fclose(m->fl);
}

/*
 *  General "do terminal" things.
 */

#ifndef MCH_AMIGA
term(s)
PORTS *s;
{
  register char ch;
  register PORTS *m;
  byte ec;

  m = cport;
  m->flags clrbit p_give;
  ec = s->ec;
  s->ec = true;

  while(true)
  {
    ioport(m);
    if (instat())
    {
      if ((ch = inchar()) is '\r') ch = '\n';
      if (ch is rchar)
      {
   m->flags setbit p_give;
   s->ec = ec;
   return;
      }
      ioport(s); outchar(ch);
      if (m->fl isnt NULL) if (ch isnt cpmeof) putc(ch, m->fl);
    }

    ioport(s);
    if (instat())
    {
      if ((ch = inchar()) is '\r') ch = '\n';
      if (m->fl isnt NULL) if (ch isnt cpmeof) putc(ch, m->fl);
    }
  }
}
#endif

