/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by Mike Parker.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include <ctype.h>
#include "fsm.h"
#define _TMPIFSM_get ((*(_TMPIFSM_inp->string))?(*++(_TMPIFSM_inp->string)):'\0')
#define _TMPIFSM_char (0x7f&(int)*(_TMPIFSM_inp->string))

static char *_TMPIFSM_lastfail;
static FSMarg _TMPIFSM_mainarg = { 0, 0 };


_TMPIFSM_(s)
char *s;
{
 int i;
#ifdef vax
 char *cp;
#endif

 _TMPIFSM_mainarg.string = s;
#ifdef vax
 cp = (char *) &s;
 if (cp[-4] >= 2) /* VAX SPECIFIC!!!! */
  { _TMPIFSM_mainarg.flags = ((int *)cp)[1];
  }
 else
#endif
  { _TMPIFSM_mainarg.flags = 0;
  }
 _TMPIFSM_lastfail = 0;
 i = __TMPIFSM_(&_TMPIFSM_mainarg,1);
 if (!i && _TMPIFSM_lastfail)
  { _TMPIFSM_mainarg.string = _TMPIFSM_lastfail;
  }
 return(i);
}

FSMarg *_TMPIFSM_getarg()
{
 return(&_TMPIFSM_mainarg);
}

char *_TMPIFSM_rest()
{
 return(_TMPIFSM_mainarg.string);
}

_TMPIFSM_cmp(in,str)
FSMarg *in;
char *str;
{
 register int i;
 register char *cp = in->string;

 for (i=0;str[i];i++)
  { if (cp[i]!=str[i])
     { return(0);
     }
  }
 in->string = cp + i;
 return(1);
}

__TMPIFSM_rec(_TMPIFSM_inp,_TMPIFSM_state)
FSMarg *_TMPIFSM_inp;
int _TMPIFSM_state;
{
 char *cp = _TMPIFSM_inp->string;

 if (__TMPIFSM_(_TMPIFSM_inp,_TMPIFSM_state))
  { return(1);
  }
 if (_TMPIFSM_lastfail == 0)
  { _TMPIFSM_lastfail = _TMPIFSM_inp->string;
  }
 _TMPIFSM_inp->string = cp;
 return(0);
}

_TMPIFSM_xval(c)
char c;
{
 if (isdigit(c))
  { return(c-'0');
  }
 else if (isupper(c))
  { return((c-'A')+10);
  }
 else
  { return((c-'a')+10);
  }
}

__TMPIFSM_(_TMPIFSM_inp,_TMPIFSM_state)
FSMarg *_TMPIFSM_inp;
int _TMPIFSM_state;
{
 while(1)
  { if (!(_TMPIFSM_inp->flags&FSM_FLAG_PARSE_BLANKS))
     { while (isspace(_TMPIFSM_char))
	{ _TMPIFSM_get;
	}
     }
    _TMPIFSM_lastfail = 0;
    switch(_TMPIFSM_state){
/*
 *
 *
 *
 * Machine begins here
 *
 *
 *
 */
				case 1: /* int */
if(1){
					/* $lambda */
					_TMPI_init(0)     ;{
_TMPIFSM_state=2;break;}}
					/* ... -> [next state] */
return(0);break;
				case 2:
if(_TMPIFSM_char=='-'){
					/* '-' */
					_TMPI_negative(_TMPIFSM_char)     ;{
_TMPIFSM_get;
_TMPIFSM_state=3;break;}}
					/* ... -> [next state] */
if(_TMPIFSM_char=='+'){
					/* '+' */
{
_TMPIFSM_get;
_TMPIFSM_state=3;break;}}
					/* ... -> [next state] */
if(1){
					/* $lambda */
{
_TMPIFSM_state=3;break;}}
					/* ... -> [next state] */
return(0);break;
				case 3:
if(_TMPIFSM_cmp(_TMPIFSM_inp,"0x")){
					/* "0x" */
{
_TMPIFSM_state=5;break;}}
					/* ... -> hex */
if(_TMPIFSM_char=='0'){
					/* '0' */
{
_TMPIFSM_get;
_TMPIFSM_state=4;break;}}
					/* ... -> octal */
if(isdigit(_TMPIFSM_char)){
					/* $decimal */
int _TMPIFSM_i=_TMPIFSM_char-'0';
while(isdigit(_TMPIFSM_get))
_TMPIFSM_i=(_TMPIFSM_i*10)+_TMPIFSM_char-'0';
					_TMPI_value(_TMPIFSM_i)     ;{
_TMPIFSM_state=6;break;}}
					/* ... -> done */
return(0);break;
				case 4: /* octal */
if((_TMPIFSM_char<='7')&&(_TMPIFSM_char>='0')){
					/* $octal */
int _TMPIFSM_i=_TMPIFSM_char-'0';
while(isdigit(_TMPIFSM_get)&&(_TMPIFSM_char<='7'))
_TMPIFSM_i=(_TMPIFSM_i<<3)+_TMPIFSM_char-'0';
					_TMPI_value(_TMPIFSM_i)     ;{
_TMPIFSM_state=6;break;}}
					/* ... -> done */
if(_TMPIFSM_char=='\0'){
					/* $eos */
					_TMPI_value(0)     ;{
return(1);}}
					/* ... -> $exit */
return(0);break;
				case 5: /* hex */
if(isxdigit(_TMPIFSM_char)){
					/* $hex */
int _TMPIFSM_i=_TMPIFSM_xval(_TMPIFSM_char);
while(isxdigit(_TMPIFSM_get))
_TMPIFSM_i=(_TMPIFSM_i<<4)+_TMPIFSM_xval(_TMPIFSM_char);
					_TMPI_value(_TMPIFSM_i)     ;{
_TMPIFSM_state=6;break;}}
					/* ... -> done */
return(0);break;
				case 6: /* done */
if(_TMPIFSM_char=='\0'){
					/* $eos */
{
return(1);}}
					/* ... -> $exit */
return(0);break;
}}}
