diff --git a/third_party/gopt/gopt.c b/third_party/gopt/gopt.c index 3f9e3bb24c5be2b1ff33f5c5f3a4ad8ea234ae46..ff16a79de3a5266b6413d28ecbaaf5620642935d 100644 --- a/third_party/gopt/gopt.c +++ b/third_party/gopt/gopt.c @@ -75,7 +75,7 @@ void *gopt_sort( int *argc, const char **argv, const void *opt_specs ){ if( ! opts ){ perror( argv[0] ); exit( EX_OSERR ); - } + } for( ; *arg_p; ++arg_p ) if( '-' == (*arg_p)[0] && (*arg_p)[1] ) if( '-' == (*arg_p)[1] ) @@ -114,11 +114,11 @@ void *gopt_sort( int *argc, const char **argv, const void *opt_specs ){ if( ! next_option-> key ){ fprintf( stderr, "%s: --%.*s: unknown option\n", argv[0], (int)strcspn( (*arg_p) + 2, "=" ), (*arg_p) + 2 ); free( opts ); - exit( EX_USAGE ); + exit( EX_USAGE ); } for( opt_spec_p= opt_specs; opt_spec_p-> key != next_option-> key; ++opt_spec_p ); found_long: - + if( !( opt_spec_p-> flags & GOPT_REPEAT )){ const opt_t *opt_p= opts; for( ; opt_p != next_option; ++opt_p ) @@ -160,7 +160,7 @@ void *gopt_sort( int *argc, const char **argv, const void *opt_specs ){ const char *short_opt= (*arg_p) + 1; for( ;*short_opt; ++short_opt ){ const opt_spec_t *opt_spec_p= opt_specs; - + for( ; opt_spec_p-> key; ++opt_spec_p ) if( strchr( opt_spec_p-> shorts, *short_opt )){ if( !( opt_spec_p-> flags & GOPT_REPEAT )){ @@ -177,7 +177,7 @@ void *gopt_sort( int *argc, const char **argv, const void *opt_specs ){ if( opt_spec_p-> flags & GOPT_ARG ){ if( short_opt[1] ) next_option-> arg= short_opt + 1; - + else { ++arg_p; if( !*arg_p || '-' == (*arg_p)[0] && (*arg_p)[1] ){ @@ -223,7 +223,7 @@ size_t gopt( const void *vptr_opts, int key ){ size_t gopt_arg( const void *vptr_opts, int key, const char **arg ){ const opt_t *opts= vptr_opts; size_t count= 0; - + for( ; opts-> key; ++opts ) if( opts-> key == key ){ if( ! count ) @@ -235,11 +235,11 @@ size_t gopt_arg( const void *vptr_opts, int key, const char **arg ){ const char *gopt_arg_i( const void *vptr_opts, int key, size_t i ){ const opt_t *opts= vptr_opts; - + for( ; opts-> key; ++opts ) if( opts-> key == key ){ if( ! i ) - return opts-> arg; + return opts-> arg; --i; } return NULL; @@ -254,7 +254,7 @@ size_t gopt_args( const void *vptr_opts, int key, const char **args, size_t args if( opts-> key == key ){ if( args_stop == args_ptr ) return args_len + gopt( opts, key ); - + *args_ptr++= opts-> arg; } if( args_stop != args_ptr ) diff --git a/third_party/gopt/gopt.h b/third_party/gopt/gopt.h index 441e8b14713c5fa7ab80dd6c2aa5dafffd181616..c2a7f7b51b0bf83251d7eaa6096950a5d4f74f7f 100644 --- a/third_party/gopt/gopt.h +++ b/third_party/gopt/gopt.h @@ -31,34 +31,40 @@ read http://www.purposeful.co.uk/tfl/ #define gopt_longs( ... ) (const char**)(const char*[]){ __VA_ARGS__, NULL } +/** Returns a pointer for use in the following calls. Prints to + * stderr and call exit() on error. + */ void *gopt_sort( int *argc, const char **argv, const void *opt_specs ); -/* returns a pointer for use in the following calls - * prints to stderr and call exit() on error + +/** Returns the number of times the option was specified which + * will be 0 or 1 unless GOPT_REPEAT was used. */ size_t gopt( const void *opts, int key ); -/* returns the number of times the option was specified - * which will be 0 or 1 unless GOPT_REPEAT was used + +/** Returns the number of times the option was specified writes + * a pointer to the option argument from the first (or only) + * occurrence to *arg. */ size_t gopt_arg( const void *opts, int key, const char **arg ); -/* returns the number of times the option was specified - * writes a pointer to the option argument from the first (or only) occurance to *arg + +/** Returns a pointer to the ith (starting at zero) occurrence of + * the option, or NULL if it was not specified that many times. */ const char *gopt_arg_i( const void *opts, int key, size_t i ); -/* returns a pointer to the ith (starting at zero) occurance - * of the option, or NULL if it was not specified that many times + +/** Returns the number of times the option was specified writes + * pointers to the option arguments in the order of occurrence to + * args[]. Writes at most args_len pointers if the return value is + * less than args_len, also writes a null pointer. */ size_t gopt_args( const void *opts, int key, const char **args, size_t args_len ); -/* returns the number of times the option was specified - * writes pointers to the option arguments in the order of occurance to args[]. - * writes at most args_len pointers - * if the return value is less than args_len, also writes a null pointer + +/** Releases memory allocated in the corresponding call to + * gopt_sort(); opts can no longer be used. */ void gopt_free( void *opts ); -/* releases memory allocated in the corresponding call to gopt_sort() - * opts can no longer be used - */ +/** Prints descriptions for all options. */ void gopt_help(const void *opt_def); -/* prints options description */ #endif /* GOPT_H_INCLUDED */