From 587dd24b5970aa3605cc2bc2826f7e35344b4317 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sun, 3 Jun 2018 00:04:50 +0100 Subject: Initial commit --- Makefile | 26 ++ arg.c | 932 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ arg.h | 420 +++++++++++++++++++++++++ debug.h | 69 +++++ test/.gitignore | 2 + test/Makefile | 9 + test/exp1.c | 21 ++ test/test1.c | 172 +++++++++++ 8 files changed, 1651 insertions(+) create mode 100644 Makefile create mode 100644 arg.c create mode 100644 arg.h create mode 100644 debug.h create mode 100644 test/.gitignore create mode 100644 test/Makefile create mode 100644 test/exp1.c create mode 100644 test/test1.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ab57711 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +PROJ=libarg +CC=g++ +CFLAGS=-Wall -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables -Wno-comment +LDFLAGS= + +SOURCE=arg +OBJECTS=$(SOURCE:=.o) +SOURCES=$(SOURCE:=.c) + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +make: dynamic static + +dynamic: CFLAGS+=-fPIC +dynamic: clean $(OBJECTS) + $(CC) $(CFLAGS) $(OBJECTS) -shared -o $(PROJ).so + +static: clean $(OBJECTS) + ar rcs $(PROJ).a $(OBJECTS) + + +clean: + rm -rf *.o + rm -rf *.a + rm -rf *.so \ No newline at end of file diff --git a/arg.c b/arg.c new file mode 100644 index 0000000..c02465f --- /dev/null +++ b/arg.c @@ -0,0 +1,932 @@ +#include "arg.h" + +#include "debug.h" + +//extern "C" void* emulate_cc_new(unsigned len) { \ +// void *p = malloc(len); +// if (p == 0) { +// /* Don't use stdio (e.g. fputs), because that may want to allocate more +// * memory. +// */ +// (void)!write(2, "out of memory\n", 14); +// abort(); +// } +// return p; +//} +//extern "C" void emulate_cc_delete(void* p) { +// if (p != 0) +// free(p); +//} +//void* operator new (unsigned len) __attribute__((alias("emulate_cc_new"))); +//void* operator new[](unsigned len) __attribute__((alias("emulate_cc_new"))); +//void operator delete (void* p) __attribute__((alias("emulate_cc_delete"))); +//void operator delete[](void* p) __attribute__((alias("emulate_cc_delete"))); +//void* __cxa_pure_virtual = 0; + + +//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 ); +/*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;} // + +//arg_t* arg_load( int argc, char **argv, def_arg *argl ) +//{ +// arg_t *ret = NULL; +// uint8_t *used; //count unused arguments, there should be no unused +// //elements at the end +// int i=0; +// int pos = -1; //position of argv inside def_arg table// + +// //prepare return value +// ret = malloc( sizeof(arg_t) ); +// memset( ret, 0, sizeof(arg_t) );// + +// 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 ) +// { +// //printf("%s ",argv[i]); +// pos = __search_arg(argv[i], argl); +// //printf("TAB:%d\n", pos );// + +// switch (argl[pos].type) +// { +// case ARGT_NONE: +// { +// //printf("No arg\n"); +// break; +// } +// case ARGT_IP: +// { +// //printf("%d is ip\n",i); +// /* +// arg_ip *ip = __create_arg( argl[i].type, argl[i].def );// + +// //if ip is 0.0.0.0 then probably no default value for ip +// if ( ((s_arg_ip*)argl[i].def)->ip == 0 ) +// ip->def = 0; +// //ip->ptr = argv[i+1]; +// ip->used = 1; +// i+=1; +// */ +// break; +// } +// case ARGT_RANGE: +// { +// printf("%d is range\n",i); +// /* +// CHK_ARG(i,1);// + +// if (argl[pos].used == 1) +// { +// printf("Allready used %s\n",argv[i]); +// break; +// }// + +// arg_range *range = __create_arg(argl[pos].type, argl[pos].def);// +// +// + +// i += 1;// + +// */// + +// break; +// } +// case ARGT_FLOAT: +// { +// //printf("%d is float\n",i); +// break; +// } +// case ARGT_LIST: +// { +// int j=0; +// char **parse_params = NULL; +// int param_num = 0; +// s_arg_list *declared_params = argl[pos].def; +// char *iter=NULL, *st=NULL, *s=NULL;// + +// //printf("%d is list\n",i); +// CHK_ARG(i,1);// +// + +// if (argl[pos].used == 1) +// { +// printf("Allready used %s\n", argv[i]); +// break; +// } +// +// //super algo for parsing +// iter = argv[i+1]; +// st = iter; +// while ((*iter)!='\0') +// { +// if ((*iter)=='|') +// { +// if ((iter)>st) +// { +// s = malloc(iter-st+1); +// memcpy(s, st, iter-st); +// s[iter-st]=0;// + +// //TODO double flags will be passed in =P +// for (j=0;jnum;j++) +// { +// int n1 = strlen(s); +// int n2 = strlen(declared_params->vals[j]); +// int n = n1 > n2 ? n1 : n2; +// if (strncmp(s,declared_params->vals[j],n)==0) +// { +// //PNL(); +// param_num += 1; +// char **realoc_ptr = realloc(parse_params, sizeof(char *)*param_num); +// if (realoc_ptr) +// { +// parse_params = (char **)realoc_ptr; +// parse_params[param_num-1] = s; +// } else +// { +// param_num -= 1; +// } +// } +// } +// st = iter+1; +// } else +// { +// st = iter+1; +// } +// } +// iter++; +// if (*iter=='\0') +// { +// if ((iter)>st) +// { +// s = malloc(iter-st+1); +// memcpy(s, st, iter-st); +// s[iter-st]=0; +// +// //some hash table could be nice to use here =p +// //TODO double flags will be passed in =P +// for (j=0;jnum;j++) +// { +// int n1 = strlen(s); +// int n2 = strlen(declared_params->vals[j]); +// int n = n1 > n2 ? n1 : n2; +// if (strncmp(s,declared_params->vals[j],n)==0) +// { +// param_num += 1; +// char **realoc_ptr = realloc(parse_params, sizeof(char *)*param_num); +// if (realoc_ptr) +// { +// parse_params = (char **)realoc_ptr; +// parse_params[param_num-1] = s; +// } else +// { +// param_num -= 1; +// } +// } +// }// + +// st = iter+1; +// } +// } +// }// + +// arg_list *list=NULL; +// { +// //PNL(); +// //hack depends on __create_arg logic +// s_arg_list arg; +// arg.num = param_num; +// arg.vals = parse_params; +// list = __create_arg(argl[pos].type, &arg); +// }// + +// //check param values// + +// if (-1 == __add_arg(ret, ARGT_LIST, list)) +// { +// ENL(); +// } +// // +// + +// argl[pos].used = 1; +// ((s_arg_list *)argl[pos].def)->result = list;// + +// used[i] = 1; +// used[i+1] = 1;// + +// i+=1; +// break;// + +// } +// case ARGT_FILE: +// { +// //printf("%d is file\n",i);// + +// break; +// } +// // we need one argument after flag +// case ARGT_VAL: +// { +// //printf("%d is !val\n",i); +// CHK_ARG(i,1);// + +// if (argl[pos].used == 1) +// { +// printf("Allready used %s\n", argv[i]); +// break; +// }// + +// arg_val *val = __create_arg( argl[pos].type, argl[pos].def );// + +// //if (((s_arg_val*)argl[pos].def)->ptr != NULL) +// // val->def = 0; +// val->ptr = argv[i+1];// + +// __add_arg( ret, ARGT_VAL, val ); +// //set that requied option allready where at cmd +// argl[pos].used = 1; +// ((s_arg_val *)argl[pos].def)->result = val;// + +// //set used cmd arg +// used[i ] = 1; +// used[i+1] = 1;// + +// i += 1;// + +// break; +// } +// // enought that this is triggered +// case ARGT_FLAG: +// { +// arg_flag *flg = NULL;// + +// //printf("%d is flag\n", i);// + +// if ( argl[pos].used == 1 ) +// { +// printf("Allready used %s\n", argv[i]); +// break; +// }// + +// flg = __create_arg( argl[pos].type, argl[pos].def );// + +// flg->flag = 1;// + +// __add_arg( ret, ARGT_FLAG, flg );// +// +// + +// argl[pos].used = 1; +// ((s_arg_flag *)argl[pos].def)->result = flg; +// +// printf("result = ?\n");// + +// used[i] = 1;// + +// break; +// } +// default: +// printf("Unknown arg %d \"%s\"\n", i, argv[i]);// + +// }// + +// i++; +// }// + +// /*if some arguments wherent used then add default value*/ +// i = 0; +// while(argl[i].param != NULL) +// { +// if (argl[i].used == 0) +// { +// switch(argl[i].type) +// { +// case ARGT_IP: +// { +// //printf("Not yet\n"); +// break; +// } +// case ARGT_RANGE: +// { +// //printf("Not yet\n"); +// break; +// } +// case ARGT_FLOAT: +// { +// //printf("Not yet\n"); +// break; +// } +// case ARGT_LIST: +// { +// //printf("Not yet\n"); +// break; +// } +// case ARGT_FILE: +// { +// //printf("Not yet\n"); +// break; +// } +// case ARGT_VAL: +// { +// printf("VAL default\n"); +// arg_val *val= __create_arg( argl[i].type, argl[i].def ); +// val->ptr = ((s_arg_val *)argl[i].def)->default_ptr; +// ((s_arg_val *)argl[i].def)->result = val; +// break; +// } +// case ARGT_FLAG: +// { +// printf("FLAG default\n"); +// arg_flag *flag= __create_arg( argl[i].type, argl[i].def ); +// //flag->flag = ((s_arg_flag *)argl[i].def)->flag; +// flag->flag = 0; +// ((s_arg_flag *)argl[i].def)->result = flag; +// break; +// } +// default: +// { +// printf("Unknown type\n"); +// } +// } +// }// + +// i++; +// }// + +// free(used);// +// + +// return ret; +//} + +/* +void arg_free( arg_t *arg ) +{ + int i; + + for (i=0;isize;i++) + { + free( arg->arg[i]->val ); + free( arg->arg[i] ); + } + free( arg->arg ); + + free( arg ); +} + +int arg_type( arg_t *arg, int num ) +{ + int ret = ARGT_NONE; + + + + return ret; +} + + +argv_t* arg_get( arg_t *arg, int num ) +{ + argv_t *ret = NULL; + + + + return ret; +} + + +arg_ip* arg_c_ip( uint32_t ip, uint16_t port ) +{ + arg_ip *ret = NULL; + + ret = malloc( sizeof(arg_ip) ); + memset(ret,0,sizeof(arg_ip)); + + ret->ip = ip; + ret->port = port; + + return ret; +} + + +arg_range* arg_c_range( uint32_t start, uint32_t end, uint32_t step ) +{ + arg_range* ret = NULL; + + ret = malloc( sizeof(arg_range) ); + memset( ret, 0, sizeof(arg_range) ); + + ret->start = start; + ret->end = end; + ret->step = step; + + return ret; +} + + +arg_float* arg_c_float( float val ) +{ + arg_float* ret = NULL; + + ret = malloc( sizeof(arg_float) ); + memset( ret, 0, sizeof(arg_float) ); + + ret->val = val; + + return ret; +} + + +arg_list* arg_c_list( uint32_t num, char **vals) +{ + int i; + arg_list *ret = NULL; + char **ptr_c=NULL; + + ret = malloc( sizeof(arg_list) ); + memset( ret, 0, sizeof(arg_list) ); + + ptr_c = malloc(sizeof(char*)*num); + for (i=0;inum = num; + ret->vals = ptr_c; + + return ret; +} + + +arg_file* arg_c_file( char *name ) +{ + arg_file *ret = NULL; + + ret = malloc( sizeof(arg_file) ); + memset( ret, 0, sizeof(arg_file) ); + + ret->name = name; + + return ret; +} + + +arg_val* arg_c_val( char *ptr ) +{ + arg_val *ret = NULL; + + ret = malloc( sizeof(arg_val) ); + memset( ret, 0, sizeof(arg_val) ); + + ret->ptr = ptr; + + return ret; +} + + +arg_flag* arg_c_flag() +{ + arg_flag *ret = NULL; + + ret = malloc( sizeof(arg_flag) ); + memset( ret, 0, sizeof(arg_flag) ); + + ret->flag = 0; + + return ret; +} +*/ + +/* +void arg_print( arg_t *arg ) +{ + int i = 0; + argv_t *v = NULL; + + if ( arg == NULL ) + { + printf("Nothing to print\n"); + return; + } + + printf("| NUM |\n"); + for ( i=0; isize;i++ ) + { + printf("| %02d |\n", i); + v = arg->arg[i]; + if (v == NULL) + continue; + printf(" ->| %02d | ", v->type); + switch( v->type ) + { + case ARGT_NONE: + { + printf("None\n"); + break; + } + case ARGT_IP: + { + printf("IP\n"); + break; + } + case ARGT_LIST: + { + int k; + arg_list *lst = (arg_list *)v->val; + printf("Lst | "); + for (k=0;knum;k++) + { + printf("%s ", lst->vals[k]); + } + printf("\n"); + break; + } + case ARGT_VAL: + { + arg_val *val = (void *)v->val; + printf("Val | "); + + printf("%s\n",val->ptr); + + break; + } + case ARGT_FLAG: + { + arg_flag *flag = (void *)v->val; + printf("FLg | "); + + printf("%d\n",flag->flag); + + break; + } + default: + printf("Unknown\n"); + } + + } + +} +*/ + +/* +int __search_arg( char *in, def_arg *arg) +{ + int ret = -1; + int i = 0; + + i = 0; + while ( arg[i].param != NULL ) + { + if (strncmp(arg[i].param,in,strlen(in)) == 0) + return i; + i++; + } + + + return ret; +} +*/ + +/* +void* __create_arg( int type, void *val ) +{ + void *ret = NULL; + + switch ( type ) + { + case ARGT_NONE: + { + break; + } + + case ARGT_IP: + { + arg_ip *ip = NULL; + s_arg_ip *s_ip= val; + + ip = arg_c_ip( s_ip->ip, s_ip->port ); + + ret = ip; + break; + } + + case ARGT_RANGE: + { + arg_range *range = NULL; + s_arg_range *s_range = val; + + range = arg_c_range( s_range->start, s_range->end, s_range->end ); + + ret = range; + break; + } + + case ARGT_FLOAT: + { + arg_float *fl = NULL; + s_arg_float *s_fl = val; + + fl = arg_c_float( s_fl->val ); + + ret = fl; + break; + } + + case ARGT_LIST: + { + arg_list *list = NULL; + s_arg_list *s_list = val; + + //so NULL where given as we use *->* then be carefull + if (s_list != NULL) + { + list = arg_c_list( s_list->num, s_list->vals ); + } + + ret = list; + break; + } + + case ARGT_FILE: + { + arg_file *f = NULL; + s_arg_file *s_f = val; + + f = arg_c_file( s_f->name ); + + ret = f; + break; + } + + case ARGT_VAL: + { + arg_val *v = NULL; + s_arg_val *s_v = val; + + v = arg_c_val( s_v->ptr ); + + ret = v; + break; + } + case ARGT_FLAG: + { + arg_flag *flag = NULL; + + flag = arg_c_flag(); + + ret = flag; + break; + } + + default: + printf("Unknown type\n"); + } + + return ret; +} +*/ + +/* +int __add_arg( arg_t *arg, int type, void *val ) +{ + argv_t *argv = NULL; + + void *new_ptr = NULL; + + new_ptr = realloc( arg->arg, sizeof(void*)*(arg->size+1) ); + if (new_ptr == NULL) + { + ENL(); + return -1; + } + arg->arg = new_ptr; + arg->size += 1; + + argv = malloc(sizeof(argv_t)); + if (argv == NULL) + { + //do resize back reallocated arg->arg + ENL(); + return -1; + } + memset(argv, 0, sizeof(argv_t)); + argv->type = type; + argv->val = val; + + arg->arg[arg->size-1] = argv; + + return 0; +} +*/ + + + + +/* +CmdArgVar::CmdArgVar() +{ + this->_type = 0; + this->val = NULL; +} + +uint8_t* CmdArgVar::get() +{ + return this->val; +} + +int CmdArgVar::type() +{ + return this->_type; +} +*/ +/* +CmdArg::CmdArg() +{ + this->used = 0; + this->_size = 0; + this->arg = NULL; +} + +int CmdArg::isUsed() +{ + return this->used; +} + +int CmdArg::size() +{ + return this->_size; +} +*/ + +CmdArgFloat::CmdArgFloat() +{ + this->used = 0; + this->val = 0.0f; +} + +int CmdArgFloat::isUsed() +{ + return this->used; +} + +float CmdArgFloat::get() +{ + return this->val; +} + +CmdArgFloatConf::CmdArgFloatConf() +{ + this->used = 0; + this->def = 0; + this->val = 0.0f; + this->default_val = 0.0f; + this->result = NULL; +} + +void CmdArgFloatConf::link(CmdArgFloat *floatArg) +{ + this->result = floatArg; +} + +int CmdArgFloatConf::isUsed() +{ + return this->used; +} + +CmdArgFlag::CmdArgFlag() +{ + this->used = 0; + this->flag = 0; +} + +int CmdArgFlag::isUsed() +{ + return this->used; +} + +int CmdArgFlag::get() +{ + return this->flag; +} + +CmdArgFlagConf::CmdArgFlagConf() +{ + this->used = 0; + this->flag = 0; + this->result = NULL; +} + +CmdArgFlagConf::CmdArgFlagConf(int flag) +{ + this->used = 0; + this->flag = flag; + this->result = NULL; +} + +int CmdArgFlagConf::isUsed() +{ + return this->used; +} + +void CmdArgFlagConf::link(CmdArgFlag *flagArg) +{ + this->result = flagArg; +} + +CmdArgDefault::CmdArgDefault() +{ + this->param = NULL; + this->type = 0; + this->def = NULL; + this->used = 0; + this->help = NULL; + this->result = NULL; +} + +/* +CmdArgDefault::CmdArgDefault(char *param, uint32_t type, void *def, char *help, void *result) +{ + this->param = param; + this->type = type; + this->def = def; + this->used = 0; + this->help = help; + this->result = result; +} +*/ + +CmdArgDefault::CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result) +{ + printf("Construct CmdArgDefault::CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result)\n"); + this->param = param; + this->type = ARGT_FLAG; + this->def = (void *)def; + this->used = 0; + this->help = help; + this->result = (void *)result; +} + + +void CmdArgDefault::setParam(char *param) +{ + this->param = param; +} + +void CmdArgDefault::setType(uint32_t type) +{ + this->type = type; +} + +void CmdArgDefault::setDefValue(void *def) +{ + this->def = def; +} + +int CmdArgDefault::isUsed() +{ + return this->used; +} + +void CmdArgDefault::setHelp(char *help) +{ + this->help = help; +} + + +void CmdArgDefault::link(void *result) +{ + this->result = result; +} + +CmdArg::CmdArg() +{ + +} + +void CmdArg::load(int argc, char **argv) +{ + +} + +void CmdArg::add(CmdArgDefault *param) +{ + +} \ No newline at end of file diff --git a/arg.h b/arg.h new file mode 100644 index 0000000..84bb5f2 --- /dev/null +++ b/arg.h @@ -0,0 +1,420 @@ +#ifndef __ARG_H +#define __ARG_H + +#include +#include +#include +#include +#include + +#define ARGT_NONE 0 //nothing +#define ARGT_IP 1 //ip addr +#define ARGT_RANGE 2 //range of values +#define ARGT_FLOAT 3 //could be float +#define ARGT_LIST 4 //list of values +#define ARGT_FILE 5 //path to file +#define ARGT_VAL 6 //command line direct val +#define ARGT_FLAG 7 //flag value on/off + +#define ARG_ENTRY(P,T,V,H) {.param=P,.type=ARGT_##T,.def=V,.used=0,.help=H} + + + +/* +typedef struct argv_t +{ + int type; + uint8_t *val; // points to arg_{ip|range|..} +} argv_t; +*/ +/* +class CmdArgVar +{ + int _type; + uint8_t *val; // points to arg_{ip|range|..} +public: + CmdArgVar(); + uint8_t *get(); + int type(); +}; +*/ + +/* +typedef struct arg_t +{ + int used; + int size; //number of arguments + argv_t **arg; //list of pointers to arguments +} arg_t; +*/ +/* +class CmdArg +{ + int used; + int _size; //number of arguments + CmdArgVar **arg; //list of pointers to arguments +public: + CmdArg(); + int isUsed(); + int size(); +}; +*/ + +//ip argument default behaviour structure + +//resulting structure with ip/port values +/* +typedef struct arg_ip +{ + int used; + uint32_t ip; //IP4 ip addrers + uint16_t port;//port number +} arg_ip; +*/ + +class CmdArgIp { + int used; + uint32_t ip; //IP4 ip addrers + uint16_t port;//port number +}; +/* +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 + uint32_t ip; + uint32_t default_ip; + uint16_t port; + uint32_t default_port; + arg_ip *result; +} s_arg_ip; +*/ + +class CmdArgIpConf { + int used; //value is in cmd + int def; //says if default should be used if no value in cmd + uint32_t ip; + uint32_t default_ip; + uint16_t port; + uint32_t default_port; + CmdArgIp *result; +}; + + +//configure range argument behaviour +//configuration structure for range argument +/* +typedef struct arg_range +{ + int used; + uint32_t start; + uint32_t end; + uint32_t step; +} arg_range; +*/ +class CmdArgRange { + int used; + uint32_t start; + uint32_t end; + uint32_t step; +}; +/* +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; //says if default should be used if no value in cmd + uint32_t start; + 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 + arg_range *result; +} s_arg_range; +*/ +class CmdArgRangeConf { + int used; + int def; //says if default should be used if no value in cmd + uint32_t start; + 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 + CmdArgRange *result; +}; + + + +//configuration structure for float argument +/* +typedef struct arg_float +{ + int used; + float val; +} arg_float; +*/ + +class CmdArgFloat +{ + int used; + float val; + +public: + CmdArgFloat(); + int isUsed(); + float get(); +}; + +/* +typedef struct s_arg_float +{ + int used; + int def; //says if default should be used if no value in cmd + float val; + float default_val; + arg_float *result; +} s_arg_float; +*/ +class CmdArgFloatConf { + int used; + int def; //says if default should be used if no value in cmd + float val; + float default_val; + CmdArgFloat *result; + +public: + CmdArgFloatConf(); + void link(CmdArgFloat *floatArg); + int isUsed(); +}; + + +//configuration structure for list argument +/* +typedef struct arg_list +{ + int used; + uint32_t num; + char **vals; +} arg_list; +*/ + +class CmdArgList { + int used; + uint32_t num; + char **vals; +}; + +/* +list arguments by name + +supports +PARAM1|PARAM2|PARAM3 +*/ +/* +typedef struct s_arg_list +{ + int used; + int def; //says if default should be used if no value in cmd + uint32_t num; + char **vals; //list of supported arguments + char *default_val; //points to default arguments in da list + arg_list *result; +} s_arg_list; +*/ + +class CmdArgListConf { + int used; + int def; //says if default should be used if no value in cmd + uint32_t num; + char **vals; //list of supported arguments + char *default_val; //points to default arguments in da list + CmdArgList *result; +}; + + +/* +configuration structure to default file argument +*/ +/* +resulting structure for file argument +*/ +/* +typedef struct arg_file +{ + int used; + char *name; + char *abspath; +} arg_file; +*/ + +class CmdArgFile { + int used; + char *name; + char *abspath; +}; + +/* +typedef struct s_arg_file +{ + int used; + 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 + arg_file *result; +} s_arg_file; +*/ + +class CmdArgFileConf { + int used; + 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 + CmdArgFile *result; +}; + +/* +typedef struct arg_val +{ + int used; + char *ptr; +} arg_val; + +typedef struct s_arg_val +{ + int used; + int def; //says if default should be used if no value in cmd + char *ptr; + char *default_ptr; + arg_val *result; +} s_arg_val; +*/ + +class CmdArgVal { + int used; + char *ptr; +}; + +class CmdArgValConf { + int used; + int def; //says if default should be used if no value in cmd + char *ptr; + char *default_ptr; + CmdArgVal *result; +}; + +/* +//no need as there is no default values +typedef struct arg_flag +{ + int used; + int flag; +} arg_flag; + +typedef struct s_arg_flag +{ + int used; + int flag; //do we need it? + arg_flag *result; +} s_arg_flag; +*/ + +class CmdArgFlag { + int used; + int flag; +public: + CmdArgFlag(); + int isUsed(); + int get(); +}; + +class CmdArgFlagConf { + int used; + int flag; //do we need it? + CmdArgFlag *result; +public: + CmdArgFlagConf(); + CmdArgFlagConf(int flag); + int isUsed(); + void link(CmdArgFlag *flagArg); +}; + +/* +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 *help; //command description + void *result; //result +} def_arg; +*/ + +class CmdArgDefault +{ + char *param; //parametr that comes from shell + uint32_t type; //type of argument + void *def; //define default values, NULL if no default values + int32_t used; + char *help; //command description + void *result; //result +public: + CmdArgDefault(); + //CmdArgDefault(char *param, uint32_t type, void *def, char *help, void *result); + CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result); + void setParam(char *param); + void setType(uint32_t type); + void setDefValue(void *def); + int isUsed(); + void setHelp(char *help); + void link(void *result); +}; + +class CmdArg { +public: + CmdArg(); + void load(int argc, char **argv); + void add(CmdArgDefault *param); +}; + +/* +arg_t* arg_load( int argc, char **argv, def_arg *argl ); +void arg_free( arg_t *arg ); +int arg_type( arg_t *arg, int num ); +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, char **vals); +arg_file* arg_c_file( char *name ); +arg_val* arg_c_val( char *ptr ); +arg_flag* arg_c_flag(); +void arg_print( arg_t *arg ); +*/ + + +#endif \ No newline at end of file diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..a8bf211 --- /dev/null +++ b/debug.h @@ -0,0 +1,69 @@ +#ifndef __RB_DEBUG_UTILS_H +#define __RB_DEBUG_UTILS_H + +//what about kprintf? + +//config options +#define PRINTF printf +#define COLORIZE +#define PRINT_LINENUM +#define PRINT_FILENAME +#define PRINT_DEBUG + + +//use color +#ifdef COLORIZE + #define D_COLOR "1;32m" + #define D_COLOR_S "\033[" D_COLOR + #define D_COLOR_E "\033[0m" + #define E_COLOR "1;31m" + #define E_COLOR_S "\033[" E_COLOR + #define E_COLOR_E "\033[0m" +#else + #define D_COLOR + #define D_COLOR_S + #define D_COLOR_E + #define E_COLOR + #define E_COLOR_S + #define E_COLOR_E +#endif + +//print debug line +#ifdef PRINT_LINENUM + #define PRINT_LINE_F "LINE:%d " + #define PRINT_LINE_D __LINE__ +#else + #define PRINT_LINE_F "" + #define PRINT_LINE_D "" +#endif + +//print +#ifdef PRINT_FILENAME + #define PRINT_FILE_F "FILE:%s " + #define PRINT_FILE_D __FILE__ +#else + #define PRINT_FILE_F "" + #define PRINT_FILE_D "" +#endif + +//print debug string +#ifdef PRINT_DEBUG + #define PRINT_DEBUG_F "Debug: " +#else + #define PRINT_DEBUG_F "" +#endif + +#define PRINT( format, args ... ) PRINTF( D_COLOR_S PRINT_DEBUG_F \ + PRINT_FILE_F PRINT_LINE_F format D_COLOR_E, PRINT_FILE_D, \ + PRINT_LINE_D, ##args); + +#define ERROR( format, args ... ) PRINTF( E_COLOR_S PRINT_DEBUG_F \ + PRINT_FILE_F PRINT_LINE_F format E_COLOR_E, PRINT_FILE_D, \ + PRINT_LINE_D, ##args); + +#define PNL() PRINT("\n"); + +#define ENL() ERROR("\n"); + + +#endif diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..92746d2 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +log.txt +test1 diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..f1e1388 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,9 @@ +CC=g++ +CFLAGS=-g3 -fno-rtti -fno-exceptions -fno-unwind-tables + +make: + g++ -c test1.c + gcc $(CFLAGS) test1.o ../arg.o -o test1 + +leak: + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test1 -d "ads|asd|asd" \ No newline at end of file diff --git a/test/exp1.c b/test/exp1.c new file mode 100644 index 0000000..8488e9b --- /dev/null +++ b/test/exp1.c @@ -0,0 +1,21 @@ +#include +#include + +typedef struct s +{ + int num; + char **val; +} s; + +char *list1[] = {"asd","asd"}; + +s def1[] = +{ + {1,list1} +}; + +int main() +{ + + return 0; +} \ No newline at end of file diff --git a/test/test1.c b/test/test1.c new file mode 100644 index 0000000..d87443f --- /dev/null +++ b/test/test1.c @@ -0,0 +1,172 @@ +#include +#include + + +#include "../arg.h" + +//define ip4 127.0.0.1:8080 +/* +s_arg_ip src_ip = +{ + port:8080, + .ip = 0x7f000001, + .result = NULL +}; +*/ + + +/* +//? +//frequency range from 88:108 +s_arg_range freq_range = +{ + .start = 88, + .end = 108, + .step = 100e3, + .result = NULL +}; +*/ + +/* +s_arg_float ratio_float = +{ + .val = 0.8, + .result = NULL +}; +*/ + + + +/* +char *color_list[] = {"RED","GREEN","BLUE"}; + +s_arg_list color_name_list = +{ + .num = 3, + .default_val = "RED", + .vals = color_list, + .result = NULL +}; +*/ + +/* +s_arg_val number_val = +{ + .ptr = NULL, + .default_ptr = "None", + .result = NULL +}; +*/ + +/* +s_arg_flag flag_help = +{ + .used = 0, + .result = NULL +}; +*/ + +/* +//cant be const +def_arg cmd_arg[] = +{ + ARG_ENTRY("-a",IP, &src_ip,"test ip address argument"), + ARG_ENTRY("-b",RANGE,&freq_range,"test integer range argument"), + ARG_ENTRY("-c",FLOAT,&ratio_float,"test float number"), + ARG_ENTRY("-d",LIST, &color_name_list,"check list of values"), + ARG_ENTRY("-e",VAL, &number_val,"string number values"), + ARG_ENTRY("-f",FILE, NULL,"check file argument"), + ARG_ENTRY("-g",FLAG, &flag_help,"check flag argument"), + {NULL,0,NULL} +}; +*/ + +extern "C" void* emulate_cc_new(unsigned len) { \ + printf("New allocated\n"); + void *p = malloc(len); + if (p == 0) { + /* Don't use stdio (e.g. fputs), because that may want to allocate more + * memory. + */ + (void)!write(2, "out of memory\n", 14); + abort(); + } + return p; +} +extern "C" void emulate_cc_delete(void* p) { + if (p != 0) + free(p); +} +void* operator new(long unsigned int len) __attribute__((alias("emulate_cc_new"))); +void* operator new[](long unsigned int len) __attribute__((alias("emulate_cc_new"))); +void operator delete(void* p, unsigned long len) __attribute__((alias("emulate_cc_delete"))); +void operator delete[](void* p, unsigned long len) __attribute__((alias("emulate_cc_delete"))); +void* __cxa_pure_virtual = 0; +__gxx_personality_v0 + +#include "../arg.h" + +int main( int argc, char **argv ) +{ + + /* + int i=0; + arg_t *cfg = NULL; + + i = 0; + while ( cmd_arg[i].param != NULL ) + { + printf("%s - %s\n",cmd_arg[i].param, cmd_arg[i].help); + i++; + } + + cfg = arg_load( argc, argv, cmd_arg ); + + if (cfg->size == 0) + { + printf("No arguments found\n"); + //goto exit_error; + } + + for (i=0;isize;i++) + { + printf("TYPE:%d\n", cfg->arg[i]->type); + } + + //arg_print( cfg ); + + if (flag_help.result != NULL) + { + printf("Help flag %d\n", flag_help.result->flag); + } + + if (number_val.result != NULL) + { + printf("Number val :%s\n", number_val.result->ptr); + } + + //arg_free( cfg ); + + return 0; + +exit_error: + arg_free( cfg ); + + return -1; + */ + + CmdArgFlag *enable = new CmdArgFlag(); + CmdArgFlagConf enableConf = CmdArgFlagConf(); + //CmdArgDefault enableDefault = CmdArgDefault((char *)"-a", &enableConf, (char *)"enable A", &enable); + + CmdArgFlag enableText = CmdArgFlag(); + CmdArgFlagConf enableTextConf = CmdArgFlagConf(); + CmdArgDefault enableTextDefault = CmdArgDefault((char *)"-t", &enableTextConf, (char *)"enable text", &enableText); + + CmdArg arguments = CmdArg(); + + //enableConf(); + +} + + -- cgit v1.2.3