1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#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_QSTR 4
#define CMDT_STR 5
#define CMDT_WORD 6
#define CMDT_SP 7
#define CMDT_FLOAT 8
#define CMDE_NONE 0 //do nothing
#define CMDE_AUTOCOMPLETE 1 //try to auto complete first command
#define CMDE_ALIAS 2 //command aliases
#define CMD_EOK 0 //all ok
#define CMD_EEXE 1 //command execution error
#define CMD_ECMD 2 //unknown command
#define CMD_HISTORY_SIZE 16 //fixed size history
/*
Arch
[CLI]------->[PARSE]
[FILE]----/
[PARSE]-------->[[EXEC]->[HISTORY] other extra features]
[CMDLIST]----/
*/
/*
Token list that comes from parser
*/
typedef struct cmd_tok_t
{
char *s,*e;
int sz;
int type;
struct cmd_tok_t *next;
} cmd_tok_t;
/*
Argument list passed to callback cmd functions
*/
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;
/*
Table of defined commands
*/
typedef struct cmd_table_t
{
char *cmd;
int (*clb)(cmd_arg_t*); //callback to command
int (*hlp)(cmd_arg_t*); //help command
int (*pre)(cmd_arg_t*); //preconditions for execution
int (*autocpl)(cmd_arg_t*); //autocomplete according to context
} cmd_table_t;
/*
autocomplete suggestions from command list. Used for autcomplet "<TAB>"
*/
struct cmd_acq_t
{
char *suggestion;
SLIST_ENTRY(cmd_acq_t) next_sugg;
};
SLIST_HEAD(cmd_acq_head_t,cmd_acq_t);
/*
Contain all non cli parsing execution things.
Manage history and logging
Manage command loading from file
Manage autocomplete
*/
typedef struct cmd_mng_t
{
int flag_history;
/*simple que*/
int history_size;
char *history[CMD_HISTORY_SIZE];
//not yet ready
//int flag_logging;
// flag turn on/off autocomplete
int flag_autocomplete;
cmd_table_t *table;
} cmd_mng_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 );
/*
If history is on.
PARAM:
cmng - cli manager
index - index value in history
RETURN:
return non-NULL value if there something
*/
char* cmd_mng_history( cmd_mng_t *cmng, int index );
/*
PARAM:
cmng
cmd - non complete command, not to execute, seach for suggestions
RETURN:
[FREE] return string list of autocomplete commands
*/
char* cmd_mng_autocomplete( cmd_mng_t *cmng, char *cmd, size_t cmd_sz );
/*
PARAM:
cmng
cmd - command to execute expect NULL at the end
RETURN:
return result of execution
*/
int cmd_mng_exec( cmd_mng_t *cmng, const char *cmd, size_t sz_cmd );
/*
Free resources that where givent to cmd manager, like history
*/
int cmd_mng_free( cmd_mng_t *cmng);
#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
|