summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFreeArtMan <=>2016-05-19 23:49:22 +0100
committerFreeArtMan <=>2016-05-19 23:49:22 +0100
commit9945de67982050338b7319dfb525d028536007a4 (patch)
treeb6046addbfe8195ca9160f705661cd013a5fd000
downloadlibcmd-9945de67982050338b7319dfb525d028536007a4.tar.gz
libcmd-9945de67982050338b7319dfb525d028536007a4.zip
Initial commit
-rw-r--r--.gitignore6
-rw-r--r--Makefile43
-rw-r--r--cmd.c544
-rw-r--r--cmd.h85
-rw-r--r--cmd_parse.c311
-rw-r--r--cmd_parse.h8
-rw-r--r--cmd_parse.ragel86
-rw-r--r--debug.h69
-rw-r--r--queue.h533
9 files changed, 1685 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4f79d43
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+TODO
+*.so
+*.a
+*.o
+test/
+*.txt \ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..86f0ae6
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,43 @@
+PROJ=libcmd
+CC=gcc
+CFLAGS=-g3
+LDFLAGS=
+RAGEL=ragel
+DOT=dot
+
+SOURCE=cmd cmd_parse
+OBJECTS=$(SOURCE:=.o)
+SOURCES=$(SOURCE:=.c)
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c $<
+
+
+make: dynamic static object
+
+dynamic: CFLAGS+=-fPIC
+dynamic: clean $(OBJECTS)
+ $(CC) $(CFLAGS) $(OBJECTS) -shared -o $(PROJ).so
+
+static: clean $(OBJECTS)
+ ar rcs $(PROJ).a $(OBJECTS)
+
+object: clean $(OBJECTS)
+ ld -r $(OBJECTS) -o $(PROJ).o
+
+ragel:
+ $(RAGEL) cmd_parse.ragel
+
+cli:
+ $(CC) $(CFLAGS) -L./ -I./ -lcmd ./test/cmd_cli.c -o ./cmd_cli
+
+pdf:
+ $(RAGEL) -V cmd_parse.ragel -o cmd_parse.dot
+ $(DOT) -Tpdf cmd_parse.dot -o cmd_parse.pdf
+
+leak:
+ valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./cmd_cli
+
+clean:
+ rm -f cmd_cli
+ rm -rf *.so *.o *.dot *.pdf
diff --git a/cmd.c b/cmd.c
new file mode 100644
index 0000000..c1d43fc
--- /dev/null
+++ b/cmd.c
@@ -0,0 +1,544 @@
+#include "cmd.h"
+
+cmd_tok_t* cmd_tok_create( char *s, char *e, int sz, int type )
+{
+ cmd_tok_t *ret = NULL;
+
+ ret = malloc( sizeof(cmd_tok_t) );
+ if ( ret == NULL )
+ return ret;
+
+ memset( ret, 0, sizeof(cmd_tok_t) );
+
+ ret->s = s;
+ ret->e = e;
+ ret->sz = sz;
+ ret->type = type;
+ ret->next = NULL; //no need, but sec programming says we need it
+
+ return ret;
+}
+
+
+int cmd_tok_add( cmd_tok_t *tok, cmd_tok_t *next )
+{
+ int ret = 0;
+
+ if ( tok == NULL )
+ {
+ return -1;
+ }
+
+ if ( next == NULL )
+ {
+ return -1;
+ }
+
+ if ( tok->next != NULL )
+ {
+ printf("next token allready set\n");
+ return -1;
+ }
+
+ tok->next = next;
+
+ return ret;
+}
+
+
+int cmd_tok_print( cmd_tok_t *tok )
+{
+ int ret = 0;
+
+ printf("TOK %p\n",tok);
+ if (tok)
+ {
+ printf("S:%p E:%p SZ:%d \n", tok->s, tok->e, tok->sz);
+ printf("TYPE: ");
+ switch (tok->type)
+ {
+ case CMDT_NONE:
+ printf("NONE");
+ break;
+ case CMDT_INT:
+ printf("INT");
+ break;
+ case CMDT_HEX:
+ printf("HEX");
+ break;
+ case CMDT_BIN:
+ printf("BIN");
+ break;
+ case CMDT_STR:
+ printf("STR");
+ break;
+ case CMDT_WORD:
+ printf("WORD");
+ break;
+ case CMDT_SP:
+ printf("SP");
+ break;
+ default:
+ printf("UNKNOWN");
+ }
+ printf("\n");
+ printf("NEXT: %p\n", tok->next );
+ }
+
+ return ret;
+}
+
+
+void cmd_tok_destroy( cmd_tok_t *tok )
+{
+ cmd_tok_t *t=NULL, *n=NULL;
+
+ if (tok == NULL)
+ {
+ return;
+ }
+
+ if ( tok->next == NULL )
+ {
+ free( tok );
+ return;
+ }
+
+ t = tok;
+ n = t;
+ while ( n != NULL)
+ {
+ n = t->next;
+ t->next = NULL;
+ free(t);
+ t = n;
+ }
+}
+
+
+int cmd_tok_count( cmd_tok_t *tok )
+{
+ int ret = 0;
+ cmd_tok_t *iter = NULL;
+
+ if (tok == NULL)
+ return 0;
+
+ iter = tok;
+ while( iter != NULL )
+ {
+ ret += 1;
+ iter = iter->next;
+ }
+
+ return ret;
+}
+
+
+
+cmd_arg_t* cmd_arg_create( cmd_tok_t *tok )
+{
+ cmd_arg_t *ret = NULL;
+ int argc = -1;
+ cmd_tok_t *iter = NULL;
+ int i = 0;
+
+ ret = malloc( sizeof(cmd_arg_t) );
+ if ( ret == NULL )
+ {
+ return NULL;
+ }
+ memset( ret, 0, sizeof( cmd_arg_t ));
+
+ //get number of arguments in command line
+ argc = cmd_tok_count( tok );
+ ret->argc = argc;
+
+
+ //alloc mem for argument string values
+ ret->argv = malloc( sizeof(int*)*argc );
+ if ( ret->argv == NULL )
+ {
+ //printf("ERR:err_malloc_argv\n");
+ goto err_malloc_argv;
+ }
+ memset( ret->argv, 0, sizeof(int*)*argc );
+
+ //alloc mem for argument type values
+ ret->type = malloc( sizeof(int)*argc );
+ if ( ret->type == NULL )
+ {
+ //printf("ERR:err_malloc_type\n");
+ goto err_malloc_type;
+ }
+ memset( ret->type, 0, sizeof(int)*argc );
+
+ //create for each cmd token string and set type
+ iter = tok;
+ for (i=0; i<argc; i++,iter=iter->next)
+ {
+ ret->argv[i] = malloc( iter->sz+1 );
+ memcpy(ret->argv[i], iter->s, iter->sz );
+ ret->argv[i][iter->sz] = 0x0;
+ ret->type[i] = iter->type;
+ }
+
+ return ret;
+
+err_malloc_type:
+ free( ret->argv );
+err_malloc_argv:
+ free( ret );
+err_exit:
+ return NULL;
+}
+
+
+cmd_arg_t* cmd_arg_sub( cmd_arg_t *arg )
+{
+ cmd_arg_t *ret = NULL;
+
+ int i=0;
+
+ if ( arg == NULL )
+ return NULL;
+
+ if ( arg->argc == 1 )
+ {
+ ret = cmd_arg_sub_empty();
+ return ret;
+ }
+
+ ret = malloc( sizeof(cmd_arg_t) );
+
+ ret->argc = arg->argc-1;
+
+ ret->type = malloc(sizeof(int)*(arg->argc-1));
+ memset(ret->type, 0, sizeof(int)*(arg->argc-1) );
+
+ ret->argv = malloc( sizeof(char*)*(arg->argc-1) );
+ memset(ret->argv, 0, sizeof(char*)*(arg->argc-1) );
+
+ ret->__sub_cmd = 1;
+
+ for (i=0; i<ret->argc; i++)
+ {
+ ret->argv[i] = arg->argv[i+1];
+ ret->type[i] = arg->type[i+1];
+ }
+
+ return ret;
+}
+
+
+cmd_arg_t* cmd_arg_sub_empty()
+{
+ cmd_arg_t *ret = NULL;
+
+ ret = malloc( sizeof(cmd_arg_t) );
+ ret->argc = 0;
+ ret->argv = NULL;
+ ret->type = NULL;
+ ret->__sub_cmd = 1;
+
+ return ret;
+}
+
+void cmd_arg_destroy( cmd_arg_t *arg )
+{
+ int i;
+
+ if ( arg == NULL )
+ return;
+
+ for ( i=0; i<arg->argc; i++)
+ {
+ if ( arg->__sub_cmd == 0 )
+ free( arg->argv[i] );
+ }
+
+ free( arg->argv );
+
+ free( arg->type );
+
+ free( arg );
+}
+
+int cmd_exec( cmd_table_t *tbl, cmd_arg_t *arg )
+{
+ int ret = -1;
+ int fret = 0;
+ int i;
+
+ cmd_arg_t *sub_arg = NULL;
+
+ if ( arg->argc < 1 )
+ {
+ printf("Hm ... no arguments\n");
+ return -1;
+ }
+
+ i = 0;
+ while ( (tbl[i].cmd != NULL) && (tbl[i].clb != NULL) )
+ {
+ //printf("tbl.cmd %s\n", tbl[i].cmd );
+ if ((strlen(tbl[i].cmd) == strlen(arg->argv[0])) //if there is 0 args then here could be non-0 and we get troubles
+ && (strlen(tbl[i].cmd) != 0)) //combo if
+ if ( strncmp( tbl[i].cmd, arg->argv[0], strlen(arg->argv[0]) ) == 0 )
+ {
+ //never will exec becouse of statment in while
+ if ( tbl[i].clb == NULL )
+ {
+ printf("Empty callback %s\n", tbl[i].cmd);
+ ret = -1;
+ break;
+ }
+
+ sub_arg = cmd_arg_sub( arg );
+ fret = tbl[i].clb( sub_arg );
+
+ //if command whent wrong or not still clean mem
+ if ( sub_arg != NULL )
+ {
+ cmd_arg_destroy( sub_arg );
+ }
+
+ //command execution whent wrong lets go out
+ if ( fret != 0 )
+ {
+ printf("Command broken execution\n");
+ ret = -1;
+ break;
+ }
+ ret = 0; //succesfull execution
+ break;
+ }
+
+ i++;
+ }
+
+
+
+ return ret;
+}
+
+int cmd_exec_ev( cmd_table_t *tbl, cmd_arg_t *arg, int event )
+{
+ int ret = -1;
+ int fret = 0;
+ int i;
+
+ cmd_arg_t *sub_arg = NULL;
+
+ if ( arg->argc < 1 )
+ {
+ printf("Hm ... no arguments\n");
+ return -1;
+ }
+
+ i = 0;
+ while ( (tbl[i].cmd != NULL) && (tbl[i].clb != NULL) )
+ {
+ //printf("tbl.cmd %s\n", tbl[i].cmd );
+ if ((strlen(tbl[i].cmd) == strlen(arg->argv[0])) //if there is 0 args then here could be non-0 and we get troubles
+ && (strlen(tbl[i].cmd) != 0)) //combo if
+ if ( strncmp( tbl[i].cmd, arg->argv[0], strlen(arg->argv[0]) ) == 0 )
+ {
+ //never will exec becouse of statment in while
+ if ( tbl[i].clb == NULL )
+ {
+ printf("Empty callback %s\n", tbl[i].cmd);
+ ret = -1;
+ break;
+ }
+
+ sub_arg = cmd_arg_sub( arg );
+
+ if ( sub_arg == NULL )
+ sub_arg = cmd_arg_sub_empty();
+
+ fret = tbl[i].clb( sub_arg );
+
+ //if command whent wrong or not still clean mem
+ if ( sub_arg != NULL )
+ {
+ cmd_arg_destroy( sub_arg );
+ }
+
+ //command execution whent wrong lets go out
+ if ( fret != 0 )
+ {
+ printf("Command broken execution\n");
+ ret = -1;
+ break;
+ }
+ ret = 0;
+ break;
+ }
+
+ i++;
+ }
+
+
+ return ret;
+}
+
+/*
+For now support only first command autocomplete, and return first cmd
+RETURN: string to command, dont modify it
+*/
+char* cmd_ac( cmd_table_t *tbl, const char *s )
+{
+ char *ret = NULL;
+ int i = -1;
+ int idx = -1, match=-1;//best match
+ int ret_match;
+ int str_ac_sz = strlen(s);
+
+
+ i = 0;
+ while ( (tbl[i].cmd != NULL) && (tbl[i].clb != NULL) )
+ {
+ ret_match = strncmp_ac( s, tbl[i].cmd, str_ac_sz );
+ if (ret_match > 0)
+ {
+ if (ret_match == str_ac_sz+1)
+ {
+ idx = i;
+ break;
+ } else
+ {
+ if (ret_match > match)
+ {
+ idx = i;
+ match = ret_match;
+ }
+ }
+ }
+ i++;
+ }
+
+ if ( idx >= 0 )
+ {
+ ret = tbl[idx].cmd;
+ }
+
+ return ret;
+}
+
+struct cmd_acq_head_t* cmd_acq( cmd_table_t *tbl, const char *s )
+{
+ struct cmd_acq_head_t *ret = NULL;
+
+ int i = -1;
+ int ret_match;
+ int str_ac_sz = strlen(s);
+
+ struct cmd_acq_head_t *ac = malloc(sizeof(struct cmd_acq_t));
+
+ SLIST_INIT(ac);
+ i = 0;
+ while ( (tbl[i].cmd != NULL) && (tbl[i].clb != NULL) )
+ {
+ ret_match = strncmp_ac( s, tbl[i].cmd, str_ac_sz );
+ if (ret_match > 0)
+ {
+ //found 100% match
+ if (ret_match == str_ac_sz+1)
+ {
+ PRINT("Unknown state\n");
+ break;
+ //partial match
+ } else
+ {
+ struct cmd_acq_t *acq = malloc(sizeof(struct cmd_acq_t));
+ acq->suggestion = tbl[i].cmd;
+ SLIST_INSERT_HEAD(ac,acq,next_sugg);
+ }
+ }
+ i++;
+ }
+
+ ret = ac;
+
+ return ret;
+}
+
+void cmd_acq_free( struct cmd_acq_head_t *acq )
+{
+ struct cmd_acq_t *iter;
+ struct cmd_acq_t *prev;
+
+ prev = NULL;
+ iter = NULL;
+ SLIST_FOREACH(iter,acq,next_sugg)
+ {
+ if ( prev != NULL )
+ {
+ free( prev );
+ prev = NULL;
+ }
+ prev = iter;
+ }
+
+ return;
+}
+
+/*
+Clothest match function
+AA AB = (1) equile <100%
+AA AA = (3) equite 100%
+AA AAA = (2) equile 100% but there is more
+A B = (0) not equile at all
+*/
+int strncmp_ac(const char *s1, const char *s2, const int n)
+{
+ int i=0;
+
+ if (n<0)
+ {
+ printf("n<0 wrong argument value\n");
+ return 0;
+ }
+
+ /* not equile at all */
+ /* A B */
+ if ( s1[0] != s2[0] )
+ {
+ return 0;
+ }
+
+ i = 0;
+ while ( (s1[i] == s2[i]) && (i<n))
+ {
+ i++;
+ }
+
+ //printf("i");
+ /* AA AA */
+ if ( (s1[i]==0x00) && (s2[i]==0x0) && (i==n) )
+ {
+ //printf("1 ");
+ return i+1;
+ /* AA AAA */
+ } else if ( (s1[i]==0x0) && (s2[i]!=0x0) && (i==n) )
+ {
+ //printf("2 ");
+ return i;
+ /* AAA AA */
+ } else if ( (s1[i]!=0x0) && (s2[i]==0x0) && (i<n) )
+ {
+ //printf("3 ");
+ return -i;
+ /* AA AB */
+ } else if ( (s1[i]!=0x0) && (s2[i]!=0x0) && (s1[i]!=s2[i]) && (i<n) )
+ {
+ //printf("4 ");
+ return -i;
+ } else
+ {
+ printf("Unknown condition\n");
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/cmd.h b/cmd.h
new file mode 100644
index 0000000..736cb2b
--- /dev/null
+++ b/cmd.h
@@ -0,0 +1,85 @@
+#ifndef __CMD_H
+#define __CMD_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "debug.h"
+#include "queue.h"
+
+#define CMDT_NONE 0
+#define CMDT_INT 1
+#define CMDT_HEX 2
+#define CMDT_BIN 3
+#define CMDT_STR 4
+#define CMDT_WORD 5
+#define CMDT_SP 6
+
+#define CMDE_NONE 0 //do nothing
+#define CMDE_AUTOCOMPLETE 1 //try to auto complete first command
+#define CMDE_ALIAS 2 //command aliases
+
+typedef struct cmd_tok_t
+{
+ char *s,*e;
+ int sz;
+ int type;
+ struct cmd_tok_t *next;
+} cmd_tok_t;
+
+typedef struct cmd_arg_t
+{
+ int argc;
+ char **argv;
+ int *type;
+
+ /* bad practice stuff */
+ int __sub_cmd; // if 1 then dont free argv stuff
+} cmd_arg_t;
+
+
+typedef struct cmd_table_t
+{
+ char *cmd;
+ int (*clb)(cmd_arg_t*);
+} cmd_table_t;
+
+struct cmd_acq_t
+{
+ char *suggestion;
+ SLIST_ENTRY(cmd_acq_t) next_sugg;
+};
+SLIST_HEAD(cmd_acq_head_t,cmd_acq_t);
+
+cmd_tok_t* cmd_tok_create( char *s, char *e, int sz, int type );
+int cmd_tok_add( cmd_tok_t *tok, cmd_tok_t *next );
+int cmd_tok_print( cmd_tok_t *tok );
+void cmd_tok_destroy( cmd_tok_t *tok ); //clean token by ->next token
+int cmd_tok_count( cmd_tok_t *tok );
+
+cmd_arg_t* cmd_arg_create( cmd_tok_t *tok );
+cmd_arg_t* cmd_arg_sub( cmd_arg_t *arg ); //just return without first arg
+cmd_arg_t* cmd_arg_sub_empty();
+void cmd_arg_destroy( cmd_arg_t *arg );
+
+int cmd_exec( cmd_table_t *tbl, cmd_arg_t *arg );
+int cmd_exec_ev( cmd_table_t *tbl, cmd_arg_t *arg, int event ); //auto complete and all other
+char* cmd_ac( cmd_table_t *tbl, const char *s ); //autocomplete
+struct cmd_acq_head_t* cmd_acq( cmd_table_t *tbl, const char *s ); //autocomplete
+void cmd_acq_free( struct cmd_acq_head_t *acq );
+
+#define STR_AC_EQ(A,B) (strncmp_ac((A),(B),strlen(A))==(strlen(A)+1))
+#define STR_AC_PEQ(A,B) (strncmp_ac((A),(B),strlen(A))<(strlen(A)))
+
+/*
+Clothest match function
+AA AB = (1) equile <100%
+AA AA = (3) equite 100%
+AA AAA = (2) equile 100% but there is more
+A B = (0) not equile at all
+*/
+int strncmp_ac( const char *s1, const char *s2, const int n);
+
+#endif \ No newline at end of file
diff --git a/cmd_parse.c b/cmd_parse.c
new file mode 100644
index 0000000..0136782
--- /dev/null
+++ b/cmd_parse.c
@@ -0,0 +1,311 @@
+
+#line 1 "cmd_parse.ragel"
+#include "cmd_parse.h"
+
+//#define CTS(X) {if (!dm_current_tok)token_s = p; printf("%s ",#X);if (!dm_current_tok) dm_current_tok = TOK_##X;}
+//#define CTE() {token_e = p;}
+#define TADD(T,S,E,SZ) {cmd_tok_t *t=cmd_tok_create(S,E,SZ,T);cmd_tok_add(tl,t);tl=tl->next;}
+//#define TADD(T,S,E)
+
+//#define PR_TK_DBG(S) printf(S);fwrite(ts,1,te-ts,stdout);printf("\n");
+#define PR_TK_DBG(S) ;
+
+char *new_string( const char *start, const char *end )
+{
+ int str_s = end-start+1;
+ char *new_str=malloc( str_s+1 );
+ memcpy( new_str, start, str_s );
+ if ( new_str != NULL )
+ new_str[str_s]=0x0;
+ return new_str;
+}
+
+int print_token( char *s, char *e, int tok)
+{
+ char *p = new_string( s, e );
+ printf("t=%d,p=%s\n",tok,p);
+ free( p );
+ return 0;
+}
+
+
+
+#line 34 "cmd_parse.c"
+static const char _cmd_actions[] = {
+ 0, 1, 0, 1, 1, 1, 2, 1,
+ 3, 1, 4, 1, 5, 1, 6, 1,
+ 7, 1, 8, 1, 9, 1, 10
+};
+
+static const char _cmd_key_offsets[] = {
+ 0, 0, 8, 17, 19, 25, 39, 43,
+ 45, 47, 53
+};
+
+static const char _cmd_trans_keys[] = {
+ 32, 39, 48, 57, 65, 90, 97, 122,
+ 32, 34, 39, 48, 57, 65, 90, 97,
+ 122, 48, 49, 48, 57, 65, 70, 97,
+ 102, 32, 34, 39, 44, 48, 59, 9,
+ 10, 49, 57, 65, 90, 97, 122, 98,
+ 120, 48, 57, 48, 57, 48, 49, 48,
+ 57, 65, 70, 97, 102, 48, 57, 65,
+ 90, 97, 122, 0
+};
+
+static const char _cmd_single_lengths[] = {
+ 0, 2, 3, 0, 0, 6, 2, 0,
+ 0, 0, 0
+};
+
+static const char _cmd_range_lengths[] = {
+ 0, 3, 3, 1, 3, 4, 1, 1,
+ 1, 3, 3
+};
+
+static const char _cmd_index_offsets[] = {
+ 0, 0, 6, 13, 15, 19, 30, 34,
+ 36, 38, 42
+};
+
+static const char _cmd_indicies[] = {
+ 0, 0, 0, 0, 0, 1, 0, 2,
+ 0, 0, 0, 0, 1, 4, 3, 5,
+ 5, 5, 3, 6, 7, 6, 6, 8,
+ 10, 6, 9, 11, 11, 1, 13, 14,
+ 9, 12, 9, 12, 4, 15, 5, 5,
+ 5, 16, 11, 11, 11, 17, 0
+};
+
+static const char _cmd_trans_targs[] = {
+ 2, 0, 5, 5, 8, 9, 5, 1,
+ 6, 7, 5, 10, 5, 3, 4, 5,
+ 5, 5
+};
+
+static const char _cmd_trans_actions[] = {
+ 0, 0, 9, 21, 0, 0, 7, 0,
+ 5, 0, 11, 0, 15, 0, 0, 17,
+ 13, 19
+};
+
+static const char _cmd_to_state_actions[] = {
+ 0, 0, 0, 0, 0, 1, 0, 0,
+ 0, 0, 0
+};
+
+static const char _cmd_from_state_actions[] = {
+ 0, 0, 0, 0, 0, 3, 0, 0,
+ 0, 0, 0
+};
+
+static const char _cmd_eof_trans[] = {
+ 0, 0, 0, 4, 4, 0, 13, 13,
+ 16, 17, 18
+};
+
+static const int cmd_start = 5;
+static const int cmd_first_final = 5;
+static const int cmd_error = 0;
+
+static const int cmd_en_main = 5;
+
+
+#line 56 "cmd_parse.ragel"
+
+
+int parse_cmd( cmd_tok_t *tl, const char *str )
+{
+ static uint8_t cs;
+ const int stacksize = 10;
+ int res=0, *stack=NULL, act=0;
+ stack = malloc( sizeof(stack)*stacksize );
+ char *p=(char *)str, *pe = (char *)str + strlen( str ), *eof=NULL;
+ char *ts, *te = 0;
+
+ /*
+ variables used in state machine
+ */
+
+
+#line 132 "cmd_parse.c"
+ {
+ cs = cmd_start;
+ ts = 0;
+ te = 0;
+ act = 0;
+ }
+
+#line 72 "cmd_parse.ragel"
+
+#line 142 "cmd_parse.c"
+ {
+ int _klen;
+ unsigned int _trans;
+ const char *_acts;
+ unsigned int _nacts;
+ const char *_keys;
+
+ if ( p == pe )
+ goto _test_eof;
+ if ( cs == 0 )
+ goto _out;
+_resume:
+ _acts = _cmd_actions + _cmd_from_state_actions[cs];
+ _nacts = (unsigned int) *_acts++;
+ while ( _nacts-- > 0 ) {
+ switch ( *_acts++ ) {
+ case 1:
+#line 1 "NONE"
+ {ts = p;}
+ break;
+#line 163 "cmd_parse.c"
+ }
+ }
+
+ _keys = _cmd_trans_keys + _cmd_key_offsets[cs];
+ _trans = _cmd_index_offsets[cs];
+
+ _klen = _cmd_single_lengths[cs];
+ if ( _klen > 0 ) {
+ const char *_lower = _keys;
+ const char *_mid;
+ const char *_upper = _keys + _klen - 1;
+ while (1) {
+ if ( _upper < _lower )
+ break;
+
+ _mid = _lower + ((_upper-_lower) >> 1);
+ if ( (*p) < *_mid )
+ _upper = _mid - 1;
+ else if ( (*p) > *_mid )
+ _lower = _mid + 1;
+ else {
+ _trans += (unsigned int)(_mid - _keys);
+ goto _match;
+ }
+ }
+ _keys += _klen;
+ _trans += _klen;
+ }
+
+ _klen = _cmd_range_lengths[cs];
+ if ( _klen > 0 ) {
+ const char *_lower = _keys;
+ const char *_mid;
+ const char *_upper = _keys + (_klen<<1) - 2;
+ while (1) {
+ if ( _upper < _lower )
+ break;
+
+ _mid = _lower + (((_upper-_lower) >> 1) & ~1);
+ if ( (*p) < _mid[0] )
+ _upper = _mid - 2;
+ else if ( (*p) > _mid[1] )
+ _lower = _mid + 2;
+ else {
+ _trans += (unsigned int)((_mid - _keys)>>1);
+ goto _match;
+ }
+ }
+ _trans += _klen;
+ }
+
+_match:
+ _trans = _cmd_indicies[_trans];
+_eof_trans:
+ cs = _cmd_trans_targs[_trans];
+
+ if ( _cmd_trans_actions[_trans] == 0 )
+ goto _again;
+
+ _acts = _cmd_actions + _cmd_trans_actions[_trans];
+ _nacts = (unsigned int) *_acts++;
+ while ( _nacts-- > 0 )
+ {
+ switch ( *_acts++ )
+ {
+ case 2:
+#line 1 "NONE"
+ {te = p+1;}
+ break;
+ case 3:
+#line 45 "cmd_parse.ragel"
+ {te = p+1;{PR_TK_DBG("sp = ");}}
+ break;
+ case 4:
+#line 46 "cmd_parse.ragel"
+ {te = p+1;{PR_TK_DBG("str = ");TADD(CMDT_STR, ts,te,te-ts);}}
+ break;
+ case 5:
+#line 49 "cmd_parse.ragel"
+ {te = p+1;{PR_TK_DBG("sep = ");TADD(CMDT_SP, ts,te,te-ts);}}
+ break;
+ case 6:
+#line 44 "cmd_parse.ragel"
+ {te = p;p--;{PR_TK_DBG("hex = ");TADD(CMDT_HEX, ts,te,te-ts);}}
+ break;
+ case 7:
+#line 47 "cmd_parse.ragel"
+ {te = p;p--;{PR_TK_DBG("dec = ");TADD(CMDT_INT, ts,te,te-ts);}}
+ break;
+ case 8:
+#line 48 "cmd_parse.ragel"
+ {te = p;p--;{PR_TK_DBG("bin = ");TADD(CMDT_BIN, ts,te,te-ts);}}
+ break;
+ case 9:
+#line 50 "cmd_parse.ragel"
+ {te = p;p--;{PR_TK_DBG("wrd = ");TADD(CMDT_WORD,ts,te,te-ts);}}
+ break;
+ case 10:
+#line 47 "cmd_parse.ragel"
+ {{p = ((te))-1;}{PR_TK_DBG("dec = ");TADD(CMDT_INT, ts,te,te-ts);}}
+ break;
+#line 265 "cmd_parse.c"
+ }
+ }
+
+_again:
+ _acts = _cmd_actions + _cmd_to_state_actions[cs];
+ _nacts = (unsigned int) *_acts++;
+ while ( _nacts-- > 0 ) {
+ switch ( *_acts++ ) {
+ case 0:
+#line 1 "NONE"
+ {ts = 0;}
+ break;
+#line 278 "cmd_parse.c"
+ }
+ }
+
+ if ( cs == 0 )
+ goto _out;
+ if ( ++p != pe )
+ goto _resume;
+ _test_eof: {}
+ if ( p == eof )
+ {
+ if ( _cmd_eof_trans[cs] > 0 ) {
+ _trans = _cmd_eof_trans[cs] - 1;
+ goto _eof_trans;
+ }
+ }
+
+ _out: {}
+ }
+
+#line 73 "cmd_parse.ragel"
+
+ if ( cs == cmd_error )
+ {
+ printf("CLIPARSE ERR state [%d] pos[%d]:[%s]\n", res, p-str, p);
+ res = -1;
+ }
+
+ free( stack );
+
+ return res;
+}
+
+
+
diff --git a/cmd_parse.h b/cmd_parse.h
new file mode 100644
index 0000000..88a02e8
--- /dev/null
+++ b/cmd_parse.h
@@ -0,0 +1,8 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "cmd.h"
+
+int parse_cmd( cmd_tok_t *tl, const char *str ); \ No newline at end of file
diff --git a/cmd_parse.ragel b/cmd_parse.ragel
new file mode 100644
index 0000000..a80d7d1
--- /dev/null
+++ b/cmd_parse.ragel
@@ -0,0 +1,86 @@
+#include "cmd_parse.h"
+
+//#define CTS(X) {if (!dm_current_tok)token_s = p; printf("%s ",#X);if (!dm_current_tok) dm_current_tok = TOK_##X;}
+//#define CTE() {token_e = p;}
+#define TADD(T,S,E,SZ) {cmd_tok_t *t=cmd_tok_create(S,E,SZ,T);cmd_tok_add(tl,t);tl=tl->next;}
+//#define TADD(T,S,E)
+
+//#define PR_TK_DBG(S) printf(S);fwrite(ts,1,te-ts,stdout);printf("\n");
+#define PR_TK_DBG(S) ;
+
+char *new_string( const char *start, const char *end )
+{
+ int str_s = end-start+1;
+ char *new_str=malloc( str_s+1 );
+ memcpy( new_str, start, str_s );
+ if ( new_str != NULL )
+ new_str[str_s]=0x0;
+ return new_str;
+}
+
+int print_token( char *s, char *e, int tok)
+{
+ char *p = new_string( s, e );
+ printf("t=%d,p=%s\n",tok,p);
+ free( p );
+ return 0;
+}
+
+
+%%{
+ machine cmd;
+
+
+ hex = '0x' [a-fA-F0-9]+;
+ decimal = [0-9]+;
+ binary = '0b' [0-1]+;
+ string = ('"' ([a-zA-Z0-9' '])+ '"');
+ seperate = ';';
+ word = [a-zA-Z] ([a-zA-Z0-9])*;
+
+ sp = ([' ','\t','\n']);
+
+ main := |*
+ hex => {PR_TK_DBG("hex = ");TADD(CMDT_HEX, ts,te,te-ts);};
+ sp => {PR_TK_DBG("sp = ");};
+ string => {PR_TK_DBG("str = ");TADD(CMDT_STR, ts,te,te-ts);};
+ decimal => {PR_TK_DBG("dec = ");TADD(CMDT_INT, ts,te,te-ts);};
+ binary => {PR_TK_DBG("bin = ");TADD(CMDT_BIN, ts,te,te-ts);};
+ seperate => {PR_TK_DBG("sep = ");TADD(CMDT_SP, ts,te,te-ts);};
+ word => {PR_TK_DBG("wrd = ");TADD(CMDT_WORD,ts,te,te-ts);};
+ *|;
+
+ #main := ( ' ' @{fcall lang}; );
+
+ write data;
+}%%
+
+int parse_cmd( cmd_tok_t *tl, const char *str )
+{
+ static uint8_t cs;
+ const int stacksize = 10;
+ int res=0, *stack=NULL, act=0;
+ stack = malloc( sizeof(stack)*stacksize );
+ char *p=(char *)str, *pe = (char *)str + strlen( str ), *eof=NULL;
+ char *ts, *te = 0;
+
+ /*
+ variables used in state machine
+ */
+
+ %%write init;
+ %%write exec;
+
+ if ( cs == cmd_error )
+ {
+ printf("CLIPARSE ERR state [%d] pos[%d]:[%s]\n", res, p-str, p);
+ res = -1;
+ }
+
+ free( stack );
+
+ return res;
+}
+
+
+
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/queue.h b/queue.h
new file mode 100644
index 0000000..cc2b25e
--- /dev/null
+++ b/queue.h
@@ -0,0 +1,533 @@
+/* $OpenBSD: queue.h,v 1.43 2015/12/28 19:38:40 millert Exp $ */
+/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
+
+/*
+ * Copyright (c) 1991, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)queue.h 8.5 (Berkeley) 8/20/94
+ */
+
+#ifndef _SYS_QUEUE_H_
+#define _SYS_QUEUE_H_
+
+/*
+ * This file defines five types of data structures: singly-linked lists,
+ * lists, simple queues, tail queues and XOR simple queues.
+ *
+ *
+ * A singly-linked list is headed by a single forward pointer. The elements
+ * are singly linked for minimum space and pointer manipulation overhead at
+ * the expense of O(n) removal for arbitrary elements. New elements can be
+ * added to the list after an existing element or at the head of the list.
+ * Elements being removed from the head of the list should use the explicit
+ * macro for this purpose for optimum efficiency. A singly-linked list may
+ * only be traversed in the forward direction. Singly-linked lists are ideal
+ * for applications with large datasets and few or no removals or for
+ * implementing a LIFO queue.
+ *
+ * A list is headed by a single forward pointer (or an array of forward
+ * pointers for a hash table header). The elements are doubly linked
+ * so that an arbitrary element can be removed without a need to
+ * traverse the list. New elements can be added to the list before
+ * or after an existing element or at the head of the list. A list
+ * may only be traversed in the forward direction.
+ *
+ * A simple queue is headed by a pair of pointers, one to the head of the
+ * list and the other to the tail of the list. The elements are singly
+ * linked to save space, so elements can only be removed from the
+ * head of the list. New elements can be added to the list before or after
+ * an existing element, at the head of the list, or at the end of the
+ * list. A simple queue may only be traversed in the forward direction.
+ *
+ * A tail queue is headed by a pair of pointers, one to the head of the
+ * list and the other to the tail of the list. The elements are doubly
+ * linked so that an arbitrary element can be removed without a need to
+ * traverse the list. New elements can be added to the list before or
+ * after an existing element, at the head of the list, or at the end of
+ * the list. A tail queue may be traversed in either direction.
+ *
+ * An XOR simple queue is used in the same way as a regular simple queue.
+ * The difference is that the head structure also includes a "cookie" that
+ * is XOR'd with the queue pointer (first, last or next) to generate the
+ * real pointer value.
+ *