/* MCM  -  MultiUser Communications Manager

   Copyright (C) 1991,2 Michael Sawyer

   This code may be reproduced without charge provided that this
   notice remains intact on all copies distributed.

   Any part of this code may be used in other programs provided that
   some citation of this work is given.
*/

#include "mcm.h"

/* This is the split-screen support module.  It is actually two */
/* modules in one; a Curses based routine, and a seperate Termcap */
/* based routine.  They really shouldn't be as seperate as they are, */
/* but I haven't had the chance to merge them yet... */

#ifdef CURSES
/* The curses code is GONE! */
#endif /* CURSES */

#ifdef TERMCAP

/* Strip padding from the capabilities */
char *depad (textptr)
char *textptr;
{
  int i;

  while ((isdigit(textptr[0]))||(textptr[0]=='*'))
    textptr++;
  return (textptr);
}


extern struct JOB job[NUMJOBS];

void splinit(id,termtype)
int id;
char *termtype;
{
  char glob[1024];
  char message[256];
  char *textptr;
  char storage[1024];
  char *area;
  int i;

  for (i=0;i<1024;i++)
    storage[i]=0;
  if (!testjob(id))
    return;
  /* Set char-at-a-time mode */
  write (job[id].fd,"\377\373\001\377\373\003",6);
  if (tgetent(glob,termtype)!=1)
  {
    strcpy (message,"Could not open TERMCAP entry!");
    goto OPENBAIL;
  }
  area=storage;
  if ((textptr=tgetstr("al",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].al,textptr,32);
  }
  else
    job[id].al[0]=0;
  if ((textptr=tgetstr("bc",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].bc,textptr,32);
  }
  else
    strcpy (job[id].bc,"\007");
  if ((textptr=tgetstr("cd",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].cd,textptr,32);
  }
  else
    job[id].cd[0]=0;
  if ((textptr=tgetstr("rs",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].rs,textptr,32);
  }
  else
    strcpy (job[id].rs," --- RESET TERMINAL NOW ---\r\n");
  if ((textptr=tgetstr("ce",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].ce,textptr,32);
  }
  else
  {
    strcpy (message,"Your terminal isn't smart enough to run split-screen!");
    goto OPENBAIL;
  }
  if ((textptr=tgetstr("do",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].xdo,textptr,32);
  }
  else
  {
    strcpy (message,"Your terminal isn't smart enough to run split-screen!");
    goto OPENBAIL;
  }
  if ((textptr=tgetstr("sr",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].sr,textptr,32);
  }
  else
    job[id].sr[0]=0;
  if ((textptr=tgetstr("cs",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].cs,textptr,32);
  }
  else
    job[id].cs[0]=0;
  if ((textptr=tgetstr("cl",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].cl,textptr,32);
  }
  else
  {
    strcpy (message,"Your terminal isn't smart enough to run split-screen!");
    goto OPENBAIL;
  }
  if ((textptr=tgetstr("cm",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy (job[id].cm,textptr,32);
  }
  else
  {
    strcpy (message,"Your terminal isn't smart enough to run split-screen!");
    goto OPENBAIL;	
  }
  if ((textptr=tgetstr("dc",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].dc,textptr,32);
  }
  else
    job[id].dc[0]=0;
  if ((textptr=tgetstr("dl",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].dl,textptr,32);
  }
  else
    job[id].dl[0]=0;
  if ((textptr=tgetstr("so",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].so,textptr,32);
  }
  else
    job[id].so[0]=0;
  if ((textptr=tgetstr("se",&area))!=NULL)
  {
    textptr=depad(textptr);
    strncpy(job[id].se,textptr,32);
  }
  else
    job[id].se[0]=0;
  if ((job[id].cs[0]==0)&&((job[id].dl[0]==0)||(job[id].al[0]==0)))
  {
    strcpy (message,"Your terminal isn't smart enough to run split-screen!");
    goto OPENBAIL;	
  }
  job[id].co=tgetnum("co");
  job[id].li=tgetnum("li");
  strncpy(job[id].termtype,termtype,16);
  textptr=tgoto(job[id].cm,0,job[id].li-4);
  sprintf (message, "%s%s%s MCM Split-Screen mode (TERMCAP)  --- /T alone to return to normal mode %s\n\r",
	   job[id].cl,textptr,job[id].so,job[id].se);
  write (job[id].fd,message,strlen(message));
  strcpy (message,tgoto(job[id].cm,0,job[id].li-3));
  write (job[id].fd,message,strlen(message));
  return;

OPENBAIL:
  write (job[id].fd,"\377\374\001\377\374\003",6);
  job[id].termtype[0]=0;
  tell (-1,id,"(sys)",message);
  return;
}

void splend(id)
int id;
{
  char *textptr;

  if (job[id].cs[0]!=0)
  {
    textptr=tgoto (job[id].cs,127,0);
    write (job[id].fd,textptr,strlen(textptr));
  }
  if (job[id].cl[0]!=0)
  {
    write (job[id].fd,job[id].cl,strlen(job[id].cl));
  }
  write (job[id].fd,job[id].rs,strlen(job[id].rs));
  write (job[id].fd,"\027\377\374\001\377\374\003",7);
  job[id].termtype[0]=0;
}

void spltxout(id,text)
int id;
char *text;
{
  char *textptr;

  if (job[id].cs[0]!=0)
  {
    textptr=tgoto (job[id].cs,job[id].li-5,1);
    write (job[id].fd,textptr,strlen(textptr));
    textptr=tgoto (job[id].cm,0,job[id].li-5);
    write (job[id].fd,textptr,strlen(textptr));
    write (job[id].fd,text,strlen(text));
    return;
  }

  /* First, check to see if we can go to the top, and remove a line */
  if ((job[id].dl[0]!=NULL)&&(job[id].al[0]!=0))
  {
    /* Now, we can do it the easy way! */
    textptr=tgoto(job[id].cm,0,0);
    write (job[id].fd,textptr,strlen(textptr));
    write (job[id].fd,job[id].dl,strlen(job[id].dl));
    textptr=tgoto(job[id].cm,0,job[id].li-5);
    write (job[id].fd,textptr,strlen(textptr));
    write (job[id].fd,job[id].al,strlen(job[id].al));
    write (job[id].fd,text,strlen(text));
    return;
  }
  /* Now we have to do it the hard way */
  /* We will add this later; before release!!! */
}

void spltext(msgfrom,id,system,text,privmsg)
int msgfrom, id, privmsg;
char *system, *text;
{
  char tempstring[64];
  char outbuf[512];
  int x, y;
  char *textptr;

  if (!validjob(id))
    return;
  if (job[id].termtype[0]==0)
    return;

  if (msgfrom>=0)
  {
    outbuf[0]=0;
    if (msgcount>99)
      msgcount=0;
    if ((job[id].showcount)&&(!privmsg))
      sprintf (outbuf,"[%02d]",msgcount);
    strcat (outbuf,system);
    if (job[id].showjob)
    {
      sprintf (tempstring,"%d-",msgfrom);
      strcat (outbuf,tempstring);
    }
    if (!job[id].padname)
      strcat (outbuf,job[msgfrom].name);
    else
    {
      sprintf (tempstring,"%-10s",job[msgfrom].name);
      strcat (outbuf,tempstring);
    }
    strcat (outbuf,"> ");
    strcat (outbuf,text);
  }
  else
    sprintf (outbuf,"%s %s",system,text);
  wwrap (outbuf);
  spltxout (id,outbuf);
  x=strlen(job[id].buf);
  y=job[id].li-3;
  while (x>=80)
  {
    x-=80;
    y++;
  }
  textptr=tgoto(job[id].cm,x,y);
  write (job[id].fd,textptr,strlen(textptr));
}

void splredraw(id)
int id;
{
  char *textptr, message[256];

  textptr=tgoto(job[id].cm,0,job[id].li-4);
  sprintf (message, "%s%s%s MCM Split-Screen mode (TERMCAP)  --- /T alone to return to normal mode %s\n\r",
	   job[id].cl,textptr,job[id].so,job[id].se);
  write (job[id].fd,message,strlen(message));
  strcpy (message,tgoto(job[id].cm,0,job[id].li-3));
  write (job[id].fd,message,strlen(message));
  splnewinp(id);
  write (job[id].fd,job[id].buf,strlen(job[id].buf));
  return;
}

/* For some reason, ANSI won't let the old K&R stuff work here... */
#ifdef NOANSI
void splchar(id,text)
int id;
char text;
#else
void splchar(int id, char text)
#endif
{
  write (job[id].fd,&text,1);
}

void splbackup(id)
int id;
{
  write (job[id].fd,"\b \b",3);
}

void splnewinp(id)
int id;
{
  char outbuf[256];
  int i;

  if (!validjob(id))
    return;

  if (job[id].termtype[0]==0)
    return;

  for (i=job[id].li;i>=job[id].li-3;i--)
  {
    sprintf (outbuf,"%s%s",tgoto(job[id].cm,0,i),job[id].ce);
    write (job[id].fd,outbuf,strlen(outbuf));
  }
}

void set_lines (id,lines)
int id, lines;
{
  if ((lines<8)||(lines>99))
    return;
  if (!validjob(id))
    return;
  if (job[id].termtype[0]==0)
    return;

  job[id].li=lines;
  splredraw(id);
  tell (-1,id,"(sys)","Lines per page changed.");
}

#endif /* TERMCAP */
