#include <stdio.h>
#include <string.h>

char	ParmArg[2048];

/*
   A Set of functions to make using command line options easier and
   standard.
   Known limitations: Cannot pass filenames with a '-' as its first
   char.
*/

int checkPassFlag(char *valid, char *flag);

/*
   Gets parms from a switch that takes 'em
*/

/*
   Checks to see if a flag was passed on the CLI
*/
char *GetParmFlag( char *flag, int argc, char *argv[] )
{
int     processed=0;
int     strings=0;

while ( processed < argc-1 )
   {
   if ( argv[processed+1][0] == '-' )
      {
      if ( checkPassFlag(flag,argv[processed+1]) == 0 )
         {
         return argv[processed+2];
         }
      }
   else
      {
      strings++;
      }
   processed++;
   }

return "";
}

int TestFlag( char *flag, int argc, char *argv[] )
{
int     processed=0;
int     strings=0;

while ( processed < argc-1 )
   {  
   if ( argv[processed+1][0] == '-' )
      {
      if ( checkPassFlag(flag,argv[processed+1]) == 0 )
         {
         return 0;
         }
      }
   else
      {
      strings++;
      }
   processed++;
   }

return -1;
}

/* 
   CheckCMD ---
   Checks to make sure that the program was passed valid options 

   - validflags is a specially formatted string containing the valid
     flags a program will accept.  Example:
         "-q|--quiet|--psyco-fork" each option is sperated by a '|'

   - parmflags are flags that take an additional string as apart of the
     flag.  Example:
         "./myprogram --delete file"
     Note that the parmflags aren't counted as strings, and a parmflag
     will only "claim ownership" on exactly 1 string directly after it.

   - min/maxstrings are the number of non-flag strings to accept.

   Returns 0 if it looks good, -1 if there is a problem.

*/

int CheckCMD( char *validflags, char *parmflags, int minstrings, int maxstrings, int argc, char *argv[] )
{
int	processed=0;
int	flags=0;
int	strings=0;
int	stringflag=0;

while ( processed < argc-1 )
   {
   if ( argv[processed+1][0] == '-' )
      {
      if ( checkPassFlag(validflags,argv[processed+1]) == 0 )
         {
         //printf("------%s\n",argv[processed+1]);
         /* It is a valid flag, check to see if it takes a string */
         flags++;
         if ( checkPassFlag(parmflags,argv[processed+1]) == 0 )
            {
            /* skip the next arg, and up the stringflag count */
            processed++;
            stringflag++;
            }
         }
      else
         {
         /* Unrecognized flag */
         printf(" Unknown flag\n");
         return -1;
         }
      }
   else
      {
      strings++;
      }
   processed++;
   }

/* make sure it meets the minimum string count */
if ( minstrings > strings )
   {
   //printf(" Min string fail %i %i\n",minstrings, strings);
   return -1;
   }

/* check to make sure there aren't too many strings */
if ( maxstrings < strings )
   {
   //printf(" %i < %i\n",maxstrings,strings);
   return -1;
   }

return 0;
}



/* Non External functions */

/* 
   Bascially an advanced version of strstr tailored for '|' as
   end of segments and\or string
*/
int checkPassFlag(char *valid, char *flag)
{
int	i=0;
int	k=0;
char	ValidFlags[2048];
char	TstFlag[2048];

strcpy(ValidFlags,valid);
strcpy(TstFlag,flag);

while ( ValidFlags[i] != 0 )
   {
   while ( ValidFlags[i] == TstFlag[k] ) /* The first bytes line up */
      {
      i++;
      k++;
      /* the bytes matched until the end of segment or string */
      if (( ValidFlags[i] == '|' ) || ( ValidFlags[i] == 0 ))
         return 0;
      }
   k=0;
   i++;
   }
return -1;
}
