From 47a9b0bb556784757f36e15f5a00d05dabc9756f Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Tue, 7 Feb 2017 20:54:38 +0000 Subject: rearranged structures --- arg.c | 40 +++++++++------- arg.h | 148 +++++++++++++++++++++++++++++++++++------------------------ test/test1.c | 1 + 3 files changed, 113 insertions(+), 76 deletions(-) diff --git a/arg.c b/arg.c index 82f635a..cae18d7 100644 --- a/arg.c +++ b/arg.c @@ -2,10 +2,15 @@ //for local usage only? int __search_arg( char *in, def_arg *arg ); +/*from s_arg_[TYPE] create type arg_[TYPE]*/ void* __create_arg( int type, void *val ); -int __add_arg( arg_t *arg, int type,void *val ); +/*add to argument list new parsed/recognized argument*/ +int __add_arg( arg_t *arg, int type, void *val ); +/* +check if command line argument ARG[NUM=argc) {\ printf("not enought arguments for %s ",argv[ARG]);break;} @@ -25,6 +30,10 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl ) used = malloc(sizeof(uint8_t)*argc); memset( used, 0, sizeof(uint8_t)*argc ); + /*************************************************************************** + Go troght all define arguments and try to parse and match all found arguments. + Prepare list of all recognized arguments + ***************************************************************************/ i = 1; //we can ignore first as first arg is name of programm while ( i < argc ) { @@ -89,9 +98,9 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl ) arg_val *val = __create_arg( argl[pos].type, argl[pos].def ); - if (((s_arg_val*)argl[pos].def)->ptr != NULL) - val->def = 0; - val->used = 1; + //if (((s_arg_val*)argl[pos].def)->ptr != NULL) + // val->def = 0; + //val->used = 1; val->ptr = argv[i+1]; __add_arg( ret, ARGT_VAL, val ); @@ -121,7 +130,7 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl ) flg = __create_arg( argl[pos].type, argl[pos].def ); - flg->used = 1; + //flg->used = 1; flg->flag = 1; __add_arg( ret, ARGT_FLAG, flg ); @@ -198,8 +207,8 @@ arg_ip* arg_c_ip( uint32_t ip, uint16_t port ) ret = malloc( sizeof(arg_ip) ); memset(ret,0,sizeof(arg_ip)); - ret->default_ip = ip; - ret->default_port = port; + ret->ip = ip; + ret->port = port; return ret; } @@ -212,9 +221,9 @@ arg_range* arg_c_range( uint32_t start, uint32_t end, uint32_t step ) ret = malloc( sizeof(arg_range) ); memset( ret, 0, sizeof(arg_range) ); - ret->default_start = start; - ret->default_end = end; - ret->default_step = step; + ret->start = start; + ret->end = end; + ret->step = step; return ret; } @@ -227,13 +236,13 @@ arg_float* arg_c_float( float val ) ret = malloc( sizeof(arg_float) ); memset( ret, 0, sizeof(arg_float) ); - ret->default_val = val; + ret->val = val; return ret; } -arg_list* arg_c_list( uint32_t num, void **vals, void *def ) +arg_list* arg_c_list( uint32_t num, void **vals) { arg_list *ret = NULL; @@ -242,7 +251,6 @@ arg_list* arg_c_list( uint32_t num, void **vals, void *def ) ret->num = num; ret->vals = vals; - ret->default_val = def; return ret; } @@ -255,7 +263,7 @@ arg_file* arg_c_file( char *name ) ret = malloc( sizeof(arg_file) ); memset( ret, 0, sizeof(arg_file) ); - ret->default_name = name; + ret->name = name; return ret; } @@ -268,7 +276,7 @@ arg_val* arg_c_val( char *ptr ) ret = malloc( sizeof(arg_val) ); memset( ret, 0, sizeof(arg_val) ); - ret->default_ptr = ptr; + ret->ptr = ptr; return ret; } @@ -412,7 +420,7 @@ void* __create_arg( int type, void *val ) arg_list *list = NULL; s_arg_list *s_list = NULL; - list = arg_c_list( s_list->num, s_list->vals, s_list->default_val ); + list = arg_c_list( s_list->num, s_list->vals ); ret = list; break; diff --git a/arg.h b/arg.h index 7dff3de..7fc3f93 100644 --- a/arg.h +++ b/arg.h @@ -17,12 +17,15 @@ #define ARG_ENTRY(P,T,V) {.param=P,.type=ARGT_##T,.def=V,.used=0} +/* +used to define table with predefine arguments +*/ typedef struct def_arg { - char *param; //parametr that comes from shell - uint32_t type; //type of argument - void *def; //define default values, NULL if no default values - uint8_t used; + char *param; //parametr that comes from shell + uint32_t type; //type of argument + void *def; //define default values, NULL if no default values + uint8_t used; } def_arg; typedef struct argv_t @@ -34,12 +37,17 @@ typedef struct argv_t typedef struct arg_t { - int size; - argv_t **arg; + int size; //number of arguments + argv_t **arg; //list of pointers to arguments } arg_t; - -typedef struct arg_ip +//ip argument default behaviour structure +/* +Supported ip addresses: +...1 = 127.0.0.1:[default port] +:80 = [default ip]:80 +*/ +typedef struct s_arg_ip { int used; //value is in cmd int def; //says if default should be used if no value in cmd @@ -47,109 +55,129 @@ typedef struct arg_ip uint32_t default_ip; uint16_t port; uint32_t default_port; -} arg_ip; - -typedef struct s_arg_ip -{ - uint32_t ip; - uint16_t port; } s_arg_ip; +//resulting structure with ip/port values +typedef struct arg_ip +{ + uint32_t ip; //IP4 ip addrers + uint16_t port;//port number +} arg_ip; -typedef struct arg_range +//configure range argument behaviour +/* +number format decimal ones +1k = 1000 +1m = 1000k = 1000000 +1g = 1000m = 1000000k = 1000000000 +10:100 - from 10 till 100 step [default step] +100: - from 100 till [default end] [default step] +:200 - from [default start] till +100:200:1 - from 100 till 200 with step 1 +*/ +typedef struct s_arg_range { int used; - int def; + int def; //says if default should be used if no value in cmd uint32_t start; - uint32_t default_start; - uint32_t end; - uint32_t default_end; - uint32_t step; - uint32_t default_step; -} arg_range; + uint32_t default_start; //default start value + uint32_t end; // + uint32_t default_end; //default end value + uint32_t step; // + uint32_t default_step; //default step value +} s_arg_range; -typedef struct s_arg_range +//configuration structure for range argument +typedef struct arg_range { uint32_t start; uint32_t end; uint32_t step; -} s_arg_range; - +} arg_range; -typedef struct arg_float +//configuration structure for float argument +typedef struct s_arg_float { int used; - int def; + int def; //says if default should be used if no value in cmd float val; float default_val; -} arg_float; +} s_arg_float; -typedef struct s_arg_float +typedef struct arg_float { float val; -} s_arg_float; +} arg_float; +//configuration structure for list argument +/* +list arguments by name -typedef struct arg_list +supports +PARAM1|PARAM2|PARAM3 +*/ +typedef struct s_arg_list { int used; - int def; + int def; //says if default should be used if no value in cmd uint32_t num; - void **vals; - char *default_val; -} arg_list; + void **vals; //list of supported arguments + char *default_val; //points to default arguments in da list +} s_arg_list; -typedef struct s_arg_list +typedef struct arg_list { uint32_t num; - void *default_val; void **vals; -} s_arg_list; - +} arg_list; -typedef struct arg_file +/* +configuration structure to default file argument +*/ +typedef struct s_arg_file { int used; - int def; - char *name; - char *default_name; - char *abspath; //dire where to search stuff? - char *default_abspath; -} arg_file; + int def; //says if default should be used if no value in cmd + char *name; //current filename + char *default_name; //default filename + char *abspath; //dire where to search stuff? + char *default_abspath; //default absoulute file path +} s_arg_file; -typedef struct s_arg_file +/* +resulting structure for file argument +*/ +typedef struct arg_file { char *name; char *abspath; -} s_arg_file; +} arg_file; -typedef struct arg_val +typedef struct s_arg_val { int used; - int def; + int def; //says if default should be used if no value in cmd char *ptr; char *default_ptr; -} arg_val; +} s_arg_val; -typedef struct s_arg_val +typedef struct arg_val { char *ptr; -} s_arg_val; +} arg_val; -typedef struct arg_flag +typedef struct s_arg_flag { int used; int flag; -} arg_flag; +} s_arg_flag; //no need as there is no default values -/* -typedef struct s_arg_flag +typedef struct arg_flag { int flag; -} s_arg_flag; -*/ +} arg_flag; arg_t* arg_load( int argc, char **argv, def_arg *argl ); void arg_free( arg_t *arg ); @@ -158,7 +186,7 @@ argv_t* arg_get( arg_t *arg, int num ); arg_ip* arg_c_ip( uint32_t ip, uint16_t port ); arg_range* arg_c_range( uint32_t start, uint32_t end, uint32_t step ); arg_float* arg_c_float( float val ); -arg_list* arg_c_list( uint32_t num, void **vals, void *def ); +arg_list* arg_c_list( uint32_t num, void **vals); arg_file* arg_c_file( char *name ); arg_val* arg_c_val( char *ptr ); arg_flag* arg_c_flag(); diff --git a/test/test1.c b/test/test1.c index dacae2e..eea5759 100644 --- a/test/test1.c +++ b/test/test1.c @@ -33,6 +33,7 @@ s_arg_val number_val = .ptr = NULL }; +//cant be const def_arg cmd_arg[] = { ARG_ENTRY("-a",IP,&src_ip), -- cgit v1.2.3