/*  Comm phone library routines */

#define  PHONE 1

#include "globals.h"

extern void emits_tx(), emits_rx();
extern UBYTE *strncpy();
extern UBYTE *index();
extern struct MenuItem PhoneItem[];

#define strchr(s,c)  index(s,c)
#define PLEN   32
#define SLEN   32
#define HLEN   32
#define ELEN   32
#define ILEN   32

static UBYTE prefix[ PLEN ] = "ATDT";
static UBYTE suffix[ SLEN ] = "\\r";
static UBYTE hangup[ HLEN ] = "\\w\\w+++\\w\\w\\w\\wATH\\r";

UBYTE doinit[ ILEN ] = "";
UBYTE doexit[ ELEN ] = "";

/*  determine which library to use
    if no name given on command line, use default library PHONELIB
    otherwise, use the one on the command line
*/

void Init_phone_lib()
{
    *alt_serv = NULL;
}

/*  open phone library.  Return 0 if not found, else return # of numbers
     in the library.
    read library data into structure pdir.
*/
int load_plib(fname)
UBYTE *fname;
{
   int i,indx;
   FILE *lib;
   UBYTE *nl,temp[40],*file;

   static UBYTE pbuff[ MAXLINE ],
                *name    = &pbuff[ 0 ],
                *number  = &pbuff[ NAMESIZE ],
                *baud    = &pbuff[ NAMESIZE+NBRSIZE ],
                *comment = &pbuff[ NAMESIZE+NBRSIZE+BAUDSIZE ];

    strcpy(temp,install.DefDir);
    strcat(temp,fname);

    file = fname;
    if((lib = fopen(fname,"r")) == NULL) /* look in default dir */
    {
         file = temp;                /* nope: try default */
         if((lib = fopen(temp,"r")) == NULL)
           return 0;                 /* not in default either, return */
    }

   strncpy(phonedir,file,MAXFNAME-1);  phonedir[MAXFNAME-1] = NULL;

   for(i = 0; i < MAXPHONE; i++)
         pdir[i].number[0] = pdir[i].name[0] = NULL;

   i = 0;
   while (i < MAXPHONE)
   {
      if(fgets(pbuff, MAXLINE, lib) == NULL)  /* read line from library */
         break;
      if( nl = strchr(pbuff,'\n'))            /* remove \n from line */
         *nl = NULL;
      indx = strlen(pbuff);

      while( indx < MAXLINE )                /* zero out unused fields */
         pbuff[indx++] = NULL;

      if(pbuff[0] == '<')                    /* a new modem prefix ? */
      {
         switch( toupper(pbuff[1]) )
         {
             case 'D':
             case 'P':   pbuff[ 2+PLEN ] = NULL;
                         strcpy(prefix,&pbuff[2]); break;
             case 'T':
             case 'S':   pbuff[ 2+SLEN ] = NULL;
                         strcpy(suffix,&pbuff[2]); break;
             case 'H':   pbuff[ 2+HLEN ] = NULL;
                         strcpy(hangup,&pbuff[2]); break;
             case 'E':   pbuff[ 2+ELEN ] = NULL;
                         strcpy(doexit,&pbuff[2]); break;
             case 'I':   pbuff[ 2+ILEN ] = NULL;
                         strcpy(doinit,&pbuff[2]); break;
         }
         continue;      /* OK, go get another line */
      }
      strncpy(pdir[i].name,   name   ,NAMESIZE);
      strncpy(pdir[i].number, number ,NBRSIZE);
      strncpy(pdir[i].baud,   baud   ,BAUDSIZE);
      strncpy(pdir[i].comment,comment,COMTSIZE);

      if( nl == NULL)     /* if NULL, line extended beyond MAXLINE */
        fgets(pbuff, MAXLINE, lib);       /* discard  rest of line */

      if(pdir[i].name[0] == '|' || pdir[i].name[0] == NULL)
         continue;        /* ignore empty lines and our comment lines */
      if(pdir[i].name[0] == '>')     /* check for alternate service */
      {
         strcpy(alt_serv, pdir[i].number);
         continue;                /* overwrite entry */
      }
      i++;
   }
   fclose(lib);
   if(i != 0)
   return (i);
}

/*  list the contents of the phone library entry to the screen
*/

void list_plib(item)
unsigned int item;
{
     sprintf(sbuff,"\n%s%s%s%s\n", pdir[item].name,
          pdir[item].number, pdir[item].baud, pdir[item].comment );
     emits_rx(sbuff);
}

/*  Dial number.  Passed a pointer to a phone number string.
     Append prefix needed to get modem to dial, then send the string
      to the modem.
*/

void dial(num)
char *num;
{
   int i;
   unsigned char c;

   if(*num)
   {
      i = 0;
      sendout(prefix);             /* start of dial string */
      i = 0;
      if(altservflg)               /* send alt. service # if required */
        sendout(alt_serv);
      while(c = *num++)            /* send phone number */
        if(c == ' ' || c == '-') continue;
          else         sendchar(c);
      sendout(suffix);             /* terminate dial string */
   }
   altservflg = FALSE;
}

/*  Hang up modem.
       send hangup command string.
*/

hang_up()
{
   sendout(hangup);
}

