#include "intercmd.h"



/*****************************************
    7.0 ok
    determine which command was entered
*****************************************/

int getCommand(char ** commands, char ** in) {

  char * cursor;
  char * sep = " \t";
  int  i; 

 while( (**in == ' ') || (**in == '\t')) (*in)++;  // skip white space 
 if (**in == '\0') return 0;               // 0 is EMPTY
 cursor = strsep(in, sep); 
 for( i = 0; ((strcmp(cursor, commands[i]) !=0 ) && (*commands[i] != '\0')) ; i++); 
 return i;
 
}




/*****************************************
    determine which command was entered
*****************************************/

int getCommand2(CLOSet_t * this, char ** in) {

  char * cursor;
  char * sep = " \t";
  int  i; 

  while( (**in == ' ') || (**in == '\t')) (*in)++;  // skip white space 
  if (**in == '\0') return 0;               // 0 is EMPTY
  cursor = strsep(in, sep);  
  for( i = 0; ((strcmp(cursor, this->CmdOptions[i].CallName) !=0 ) && (*this->CmdOptions[i].CallName != '\0')) ; i++);         
  return (*this->CmdOptions[i].handler)(*in); // run the handler 
         
}



   
/**************************************
   tear off any trailing linefeeds
**************************************/

void chomp(char * string) {
  char * target;
  
  target = strchr(string,'\n');
  if (target != NULL) { *target = '\0'; }
  
}     

/**************************************
  Forward pointer thru whitespace
***************************************/
void FF(char ** string) {
  while((**string == ' ') || (**string == '\t')) (*string)++;
}
         
     
/**************************************
  Get a number in decimal, octal, hex, or binary
  s is your string in, l is a pointer to the remainder
***************************************/    
long getNumber(char * s, char ** l) {
  char * temp;
  temp = s;
  FF(&temp);
  if (strncmp(temp, "0b", 2)==0) { // pick off binary
    return strtol(temp+2, l, 2);
  } else {
    return strtol(temp, l, 0);    
  }         
}         
         
         
