head 1.3; access; symbols start:1.1.1.1 blursoft:1.1.1; locks; strict; comment @// @; 1.3 date 2002.10.29.18.05.14; author steven; state Exp; branches; next 1.2; 1.2 date 2002.10.24.00.19.46; author steven; state Exp; branches; next 1.1; 1.1 date 2002.10.21.14.58.28; author steven; state Exp; branches 1.1.1.1; next ; 1.1.1.1 date 2002.10.21.14.58.28; author steven; state Exp; branches; next ; desc @@ 1.3 log @Added winamputil files to abstract some of the ugliness of SendMessage away. Added a -? option to print the usage and program name. Also made sure that if the user uses some incorrect options the show_usage() is called. Also made various cleanups. @ text @#include #include #include #include "minigetopt.h" char *optarg = NULL; int optind = 0; int opterr = 0; int curarg = 1; /* * minigetopt - commandline parser * Steven Romej [http://romej.com] ~March 2002 * 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. * * */ 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; } **************************************************************************/ @ 1.2 log @Changed how we handle the commandline. Also added *some* error checking. Paved the way for the client to connect to a specified server (instead of having it hardcoded). Also made an update_interval to specify when updates should be done to the winamp position. @ text @d1 142 a142 142 #include #include #include #include "minigetopt.h" char *optarg = NULL; int optind = 0; int opterr = 0; int curarg = 1; /* * minigetopt - commandline parser * Steven Romej [http://romej.com] ~March 2002 * 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; } **************************************************************************/ @ 1.1 log @Initial revision @ text @d14 1 a14 1 * d24 1 @ 1.1.1.1 log @Blimp Project @ text @@