#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "minigetopt.h"



char *optarg = NULL;
int optind = 0;
int opterr = 0;
int curarg = 1;
/*
 * minigetopt - commandline parser
 *
 * works similar to GNU getopt, but with much 
 * stripped-out functionality
 * 
 * will look for options on optstring, returning
 * -1 if any error (including end of option processing)
 * occurs.
 * 
 * minigetopt is called continuously, returning
 * the option it found on each call.  If an option uses
 * 
 */
int minigetopt( int argc, char *argv[], char *optstring )
{
	char *loc = NULL;
	
	if ( curarg < argc ) {
		/* before looking into specifics, is this even 
		 * of the form -X, where X is the arg         */
		if ( strlen( argv[ curarg ] ) != 2 ) {
				opterr = ERRORARGLEN;
				return opterr;
				
		}
		/* test to ensure argument begins with - or / */	
		switch( argv[ curarg ][0] ) {
			case '-':  
			case '/':
				break;  
			default:
				opterr = ERRORFORMAT;
				return opterr;
		}
		/* test to ensure argument is valid according to 
		 * what's contained in opstring                */
		loc = strchr( optstring , argv[ curarg ][1] );
		if ( loc == NULL ) {
			opterr = ERRORINVALID;
			return opterr;
		}
		/* look one index past the option in opstring: it
		 * will be one of three things: a ':', another option,
		 * or '\0'.  if it is ':' set optarg to the param   */
		if ( loc[1] == ':' ) {
			if ( curarg+1 == argc ) {
				opterr = ERRORPARAM;
				return opterr;
			}
			optarg = argv[curarg+1];
			curarg+=2; /* consumed option AND parameter */
			return *loc;
		}
		else {
			curarg++; /* consumed the option */
			return *loc;
		}
			
	}
	/* got here because curarg >= argc now...processing done */
	opterr = 0; 
	return ARGSUCCESS; /* returns -1 when complete */
}













/******************************* FOR DEBUGGING ******************************
void err( void ){
		
	printf("doing error analysis...\n");
	switch( opterr ) {
			case ERRORDONE:
					printf("exited due to lack of more options\n");
					break;
			case ERRORFORMAT:
					printf("exited due to options format (need - or /)\n");
					break;
			case ERRORINVALID:
					printf("exited due to invalid option\n");
					break;
			case ERRORARGLEN:
					printf("exited due to arglen (no -xxxx allowed!)\n");
					break;
			default:
					printf("unknown error!\n");
					break;
	}
}

int main(int argc, char* argv[])
{
	//extern char *optarg;
	//extern int optind;
	//extern int opterr;
	//extern int curarg;
	int res = 1;
	while( (res = minigetopt( argc, argv, "ab:c" )) != -1 ) {
		
		switch( res ) {
		case 'a':
			printf("found a\n");
			break;
		case 'b':
			printf("found b (opt is %s)\n",optarg);
			break;
		case 'c':
			printf("found c\n");
			break;
		default:
			printf("unk %d\n",opterr);
			err( );
			exit( EXIT_FAILURE );
			break;
		}
		
	}
	return 0;
}
**************************************************************************/
