#ifndef __CMD_H #define __CMD_H #include #include #include #include #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