summaryrefslogtreecommitdiff
path: root/extlibs/arg.c
diff options
context:
space:
mode:
Diffstat (limited to 'extlibs/arg.c')
-rw-r--r--extlibs/arg.c659
1 files changed, 659 insertions, 0 deletions
diff --git a/extlibs/arg.c b/extlibs/arg.c
new file mode 100644
index 0000000..3d4475b
--- /dev/null
+++ b/extlibs/arg.c
@@ -0,0 +1,659 @@
+#include "arg.h"
+
+#include "debug.h"
+
+//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]
+*/
+#define CHK_ARG(ARG,NUM) if ((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
+ void *arg_def_val = NULL;
+
+ //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;
+ int num=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 %d\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;j<declared_params->num;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;j<declared_params->num;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;
+
+ 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->used = 1;
+ val->ptr = argv[i+1];
+
+ __add_arg( ret, ARGT_VAL, val );
+ //set that requied option allready where at cmd
+ argl[pos].used = 1;
+
+ //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->used = 1;
+ flg->flag = 1;
+
+ __add_arg( ret, ARGT_FLAG, flg );
+
+
+ argl[pos].used = 1;
+ used[i] = 1;
+
+ break;
+ }
+ default:
+ printf("Unknown arg %d \"%s\"\n", i, argv[i]);
+
+ }
+
+ i++;
+ }
+
+ //printf("arg ");
+ for (i=1;i<argc;i++)
+ {
+ if (used[i] == 0)
+ {
+ //printf("%d ",i);
+ }
+ }
+ //printf("\n");
+
+ free(used);
+
+
+ return ret;
+}
+
+
+void arg_free( arg_t *arg )
+{
+ int i;
+
+ for (i=0;i<arg->size;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;i<num;i++)
+ {
+ ptr_c[i] = vals[i];
+ }
+
+ ret->num = 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; i<arg->size;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;k<lst->num;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;
+}
+
+