/*->c.faxfix */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>

#include "h.os"
#include "h.wimp"
#include "h.bbc"
#include "h.flex"

#include "h.Drawlevel0"


#include "h.def"
#include "h.wos"
#include "h.strdef"
#include "h.timex"
#include "h.xext"
#include "h.main"
#include "h.mym"
#include "h.sched"

#include "h.faxfix"



#define STRLEN 64



char * commandname[COMMANDS]=
{
 "ArcFax$FaxTo",
 "ArcFax$FaxAfter",
 "ArcFax$FaxName",
 "ArcFax$FaxBand",
 "ArcFax$FaxPrefix",
 "ArcFax$FaxPriority",
 "ArcFax$FaxMerge",
};


static char * targets[COMMANDS]=
{
 "{FAXTO:",
 "{FAXAFTER:",
 "{FAXNAME:",
 "{FAXBAND:",
 "{FAXPREFIX:",
 "{FAXPRIORITY:",
 "{FAXMERGE:",
};


static int targetlen[COMMANDS]=
{
 7,
 10,
 9,
 9,
 11,
 13,
 10,
};


static int targetmatch[COMMANDS];



static char curr[STRLEN];
static int  index;
static int  target;
static int  state;

void faxfixmachine(int c)
{
 char string[128];
 int  i;
 int  matchi;

 if(c<=0) 
 {
  state=index=0;
  memset(targetmatch,0,sizeof(targetmatch));
 }
 else
 if(state==0)    /* looking for a hit */
 {
  matchi=-1;

  for(i=0;i<COMMANDS;i++)
  {
   if(index>=targetmatch[i])
   {
    if(targets[i][index]==toupper(c))
    {
     targetmatch[i]++;
     matchi=i;
    }
   }
  }

  if(matchi<0)  /* no match at this index */
  {
   state=index=0;
   memset(targetmatch,0,sizeof(targetmatch));
  }
  else
  {
   if(index==(targetlen[matchi]-1))
   {
    state=1;
    index=0;
    target=matchi;
   }
   else
   {
    index++;
   }
  }
 }
 else
 if(state==1)   /* accumulating stuff */
 {
  if(c=='}')
  {
   sprintf(string,"Set %s %s",commandname[target],curr);
   oscli(string);
   dprintf(0,"set %s",string); 
   state=index=0;
  }
  else
  if(index<(STRLEN-1))
  {
   curr[index]=c;
   curr[index+1]=0;
   index++;
  }
  else
  {
   state=index=0;
   memset(targetmatch,0,sizeof(targetmatch));
  }
 }
}


