-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathci.cc
275 lines (235 loc) · 6.64 KB
/
ci.cc
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (C) 2004-2011 David Weber <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "ci.hh"
#include "debug.h"
#include "libtecla.h"
#include "cmd_table.h"
#define HISTORY_FILE "~/.lci.history"
//------------------------------------------------------------------------------
ci::ci(cmd_table_entry_t* cmd_table, uint32_t num_cmds, const char* prompt_string)
{
this->cmd_table = cmd_table;
this->num_cmds = num_cmds;
this->prompt_string = prompt_string;
this->get_line = new_GetLine(1024, 2048);
gl_load_history(this->get_line, HISTORY_FILE, "history file");
}
//------------------------------------------------------------------------------
ci::~ci()
{
if (this->get_line)
{
gl_save_history(this->get_line, HISTORY_FILE, "history file", 100);
del_GetLine(this->get_line);
}
}
//------------------------------------------------------------------------------
void ci::init()
{
}
//------------------------------------------------------------------------------
void ci::run()
{
ci_buf_t ci_buf;
bool done = false;
status_t status = SUCCESS;
while(!done)
{
if (feof(stdin))
{
done = true;
}
else
{
status = receive_cmd(ci_buf);
if(status == SUCCESS)
{
status = process_cmd(ci_buf);
process_status(ci_buf, status);
if(status == EXIT)
{
done = true;
}
}
else
{
done = true;
}
}
}
}
//------------------------------------------------------------------------------
status_t ci::handler(const char* cmd_line, arg_array_t cmd_args, uint32_t num_args)
{
status_t status = SUCCESS;
uint8_t i;
for(i = 0; i < num_cmds; i++)
{
if(strcasecmp(cmd_args[0], cmd_table[i].cmd) != 0)
{
status = CI_UNRECOGNIZED_COMMAND;
}
else if (((num_args < cmd_table[i].min_args) ||
(num_args > cmd_table[i].max_args)) &&
((cmd_table[i].min_args != 0) && (cmd_table[i].max_args != 0)))
{
status = CI_INVALID_NUMBER_OF_ARGUMENTS;
break;
}
else
{
status = cmd_table[i].cmd_handler(cmd_line, cmd_args, num_args);
break;
}
}
if (status == CI_UNRECOGNIZED_COMMAND)
{
if (system(cmd_line) != -1)
{
status = SUCCESS;
}
}
return status;
}
//------------------------------------------------------------------------------
status_t ci::receive_cmd(ci_buf_t buf)
{
status_t status = SUCCESS;
char* line = NULL;
if (this->get_line == NULL)
{
status = FAILURE;
}
else
{
line = gl_get_line(this->get_line, this->prompt_string, NULL, -1);
if (line == NULL)
{
status = FAILURE;
}
else
{
strcpy(buf, line);
}
}
return status;
}
//------------------------------------------------------------------------------
status_t ci::process_cmd(const char* cmd_line)
{
status_t status = SUCCESS;
const char* scanf_format_string = "%s %s %s %s %s %s %s %s";
arg_array_t cmd_args = {0,0,0,0,0,0,0,0};
uint16_t num_args = 0;
if ((strcmp("\n", cmd_line) != 0) && (cmd_line[0] != '#'))
{
num_args = sscanf(cmd_line, scanf_format_string,
cmd_args[0], cmd_args[1],
cmd_args[2], cmd_args[3],
cmd_args[4], cmd_args[5],
cmd_args[6], cmd_args[7]);
if(num_args > 0)
{
print_args(cmd_args, num_args);
status = handler(cmd_line, cmd_args, num_args);
}
}
return status;
}
//------------------------------------------------------------------------------
void ci::send_response()
{
}
//------------------------------------------------------------------------------
void ci::process_status(const char* cmd_line, status_t status)
{
switch(status)
{
case CI_INVALID_NUMBER_OF_ARGUMENTS:
fprintf(stderr, "Error - invalid number of arguments\n");
fprintf(stderr, "%s\n", cmd_line);
break;
case CI_INVALID_ARG:
fprintf(stderr, "Error - invalid argument\n");
fprintf(stderr, "%s\n", cmd_line);
break;
case CI_UNRECOGNIZED_COMMAND:
fprintf(stderr, "Error - unrecognized command\n");
fprintf(stderr, "%s\n", cmd_line);
break;
case CI_INVALID_HW_REG_OBJECT:
fprintf(stderr, "Error - invalid hw_reg object, run \"map\" command\n");
fprintf(stderr, "%s\n", cmd_line);
break;
case HW_INVALID_ADDRESS:
fprintf(stderr, "Error - invalid address\n");
fprintf(stderr, "%s\n", cmd_line);
break;
case HW_INVALID_MEM_MAP_OBJECT:
fprintf(stderr, "Error - invalid mem_map object, run \"map\" command\n");
fprintf(stderr, "%s\n", cmd_line);
break;
case HW_CMD_MAP_FAILURE:
fprintf(stderr, "Error - unable to map specified address\n");
fprintf(stderr, "%s\n", cmd_line);
break;
case SUCCESS:
break;
case EXIT:
fprintf(stderr, "\n");
break;
case FAILURE:
fprintf(stderr, "Error - operation failed\n");
fprintf(stderr, "%s\n", cmd_line);
break;
default:
fprintf(stderr, "Error - unrecognized error - %d\n", status);
fprintf(stderr, "%s\n", cmd_line);
break;
}
}
//------------------------------------------------------------------------------
void ci::print_args(arg_array_t args, uint32_t num_args)
{
uint8_t i = 0;
DPRINTF(DBG_LVL_CI, "num_args: %d\n", num_args);
for(i = 0; i < num_args; i++)
{
DPRINTF(DBG_LVL_CI, "arg[%d] = \"%s\"\n", i, args[i]);
}
}
//------------------------------------------------------------------------------
void ci::help()
{
int i;
printf("\n");
printf(" Command Summary\n");
printf("------------------------------------------------------------------------\n");
printf("\n");
for(i = 0; i < num_cmds; i++)
{
printf(" %s\n", cmd_table[i].desc);
}
printf("\n");
printf("------------------------------------------------------------------------\n");
}