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
|
/*
ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @file shell.c
* @brief Simple CLI shell code.
*
* @addtogroup SHELL
* @{
*/
#include <string.h>
#include "ch.h"
#include "hal.h"
#include "shell.h"
#include "shell_cmd.h"
#include "chprintf.h"
/*===========================================================================*/
/* Module local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Module exported variables. */
/*===========================================================================*/
/**
* @brief Shell termination event source.
*/
event_source_t shell_terminated;
/*===========================================================================*/
/* Module local types. */
/*===========================================================================*/
/*===========================================================================*/
/* Module local variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Module local functions. */
/*===========================================================================*/
static char *parse_arguments(char *str, char **saveptr) {
char *p;
if (str != NULL)
*saveptr = str;
p = *saveptr;
if (!p) {
return NULL;
}
/* Skipping white space.*/
p += strspn(p, " \t");
if (*p == '"') {
/* If an argument starts with a double quote then its delimiter is another
quote.*/
p++;
*saveptr = strpbrk(p, "\"");
}
else {
/* The delimiter is white space.*/
*saveptr = strpbrk(p, " \t");
}
/* Replacing the delimiter with a zero.*/
if (*saveptr != NULL) {
*(*saveptr)++ = '\0';
}
return *p != '\0' ? p : NULL;
}
static void usage(BaseSequentialStream *chp, char *p) {
chprintf(chp, "Usage: %s\r\n", p);
}
static void list_commands(BaseSequentialStream *chp, const ShellCommand *scp) {
while (scp->sc_name != NULL) {
chprintf(chp, "%s ", scp->sc_name);
scp++;
}
}
static bool cmdexec(const ShellCommand *scp, BaseSequentialStream *chp,
char *name, int argc, char *argv[]) {
while (scp->sc_name != NULL) {
if (strcmp(scp->sc_name, name) == 0) {
scp->sc_function(chp, argc, argv);
return false;
}
scp++;
}
return true;
}
/*===========================================================================*/
/* Module exported functions. */
/*===========================================================================*/
/**
* @brief Shell thread function.
*
* @param[in] p pointer to a @p BaseSequentialStream object
*/
THD_FUNCTION(shellThread, p) {
int n;
BaseSequentialStream *chp = ((ShellConfig *)p)->sc_channel;
const ShellCommand *scp = ((ShellConfig *)p)->sc_commands;
char *lp, *cmd, *tokp, line[SHELL_MAX_LINE_LENGTH];
char *args[SHELL_MAX_ARGUMENTS + 1];
chprintf(chp, "\r\nChibiOS/RT Shell\r\n");
while (true) {
chprintf(chp, "ch> ");
if (shellGetLine(chp, line, sizeof(line))) {
#if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
chprintf(chp, "\r\nlogout");
break;
#else
/* Putting a delay in order to avoid an endless loop trying to read
an unavailable stream.*/
osalThreadSleepMilliseconds(100);
#endif
}
lp = parse_arguments(line, &tokp);
cmd = lp;
n = 0;
while ((lp = parse_arguments(NULL, &tokp)) != NULL) {
if (n >= SHELL_MAX_ARGUMENTS) {
chprintf(chp, "too many arguments\r\n");
cmd = NULL;
break;
}
args[n++] = lp;
}
args[n] = NULL;
if (cmd != NULL) {
if (strcmp(cmd, "exit") == 0) {
if (n > 0) {
usage(chp, "exit");
continue;
}
break;
}
else if (strcmp(cmd, "help") == 0) {
if (n > 0) {
usage(chp, "help");
continue;
}
chprintf(chp, "Commands: help exit ");
list_commands(chp, shell_local_commands);
if (scp != NULL)
list_commands(chp, scp);
chprintf(chp, "\r\n");
}
else if (cmdexec(shell_local_commands, chp, cmd, n, args) &&
((scp == NULL) || cmdexec(scp, chp, cmd, n, args))) {
chprintf(chp, "%s", cmd);
chprintf(chp, " ?\r\n");
}
}
}
shellExit(MSG_OK);
}
/**
* @brief Shell manager initialization.
*
* @api
*/
void shellInit(void) {
chEvtObjectInit(&shell_terminated);
}
#if !defined(_CHIBIOS_NIL_) || defined(__DOXYGEN__)
/**
* @brief Terminates the shell.
* @note Must be invoked from the command handlers.
* @note Does not return.
*
* @param[in] msg shell exit code
*
* @api
*/
void shellExit(msg_t msg) {
/* Atomically broadcasting the event source and terminating the thread,
there is not a chSysUnlock() because the thread terminates upon return.*/
chSysLock();
chEvtBroadcastI(&shell_terminated);
chThdExitS(msg);
}
#endif
/**
* @brief Reads a whole line from the input channel.
* @note Input chars are echoed on the same stream object with the
* following exceptions:
* - DEL and BS are echoed as BS-SPACE-BS.
* - CR is echoed as CR-LF.
* - 0x4 is echoed as "^D".
* - Other values below 0x20 are not echoed.
* .
*
* @param[in] chp pointer to a @p BaseSequentialStream object
* @param[in] line pointer to the line buffer
* @param[in] size buffer maximum length
* @return The operation status.
* @retval true the channel was reset or CTRL-D pressed.
* @retval false operation successful.
*
* @api
*/
bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size) {
char *p = line;
while (true) {
char c;
if (streamRead(chp, (uint8_t *)&c, 1) == 0)
return true;
#if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
if (c == 4) {
chprintf(chp, "^D");
return true;
}
#endif
if ((c == 8) || (c == 127)) {
if (p != line) {
streamPut(chp, c);
streamPut(chp, 0x20);
streamPut(chp, c);
p--;
}
continue;
}
if (c == '\r') {
chprintf(chp, "\r\n");
*p = 0;
return false;
}
if (c < 0x20)
continue;
if (p < line + size - 1) {
streamPut(chp, c);
*p++ = (char)c;
}
}
}
/** @} */
|