shell.c
7.77 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (C) 2009, Freie Universitaet Berlin (FUB).
* Copyright (C) 2013, INRIA.
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_shell
* @{
*
* @file
* @brief Implementation of a very simple command interpreter.
* For each command (i.e. "echo"), a handler can be specified.
* If the first word of a user-entered command line matches the
* name of a handler, the handler will be called with the whole
* command line as parameter.
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author René Kijewski <rene.kijewski@fu-berlin.de>
*
* @}
*/
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "shell.h"
#include "shell_commands.h"
#if !defined(SHELL_NO_ECHO) || !defined(SHELL_NO_PROMPT)
#ifdef MODULE_NEWLIB
/* use local copy of putchar, as it seems to be inlined,
* enlarging code by 50% */
static void _putchar(int c) {
putchar(c);
}
#else
#define _putchar putchar
#endif
#endif
static shell_command_handler_t find_handler(const shell_command_t *command_list, char *command)
{
const shell_command_t *command_lists[] = {
command_list,
#ifdef MODULE_SHELL_COMMANDS
_shell_command_list,
#endif
};
const shell_command_t *entry;
/* iterating over command_lists */
for (unsigned int i = 0; i < sizeof(command_lists) / sizeof(entry); i++) {
if ((entry = command_lists[i])) {
/* iterating over commands in command_lists entry */
while (entry->name != NULL) {
if (strcmp(entry->name, command) == 0) {
return entry->handler;
}
else {
entry++;
}
}
}
}
return NULL;
}
static void print_help(const shell_command_t *command_list)
{
printf("%-20s %s\n", "Command", "Description");
puts("---------------------------------------");
const shell_command_t *command_lists[] = {
command_list,
#ifdef MODULE_SHELL_COMMANDS
_shell_command_list,
#endif
};
const shell_command_t *entry;
/* iterating over command_lists */
for (unsigned int i = 0; i < sizeof(command_lists) / sizeof(entry); i++) {
if ((entry = command_lists[i])) {
/* iterating over commands in command_lists entry */
while (entry->name != NULL) {
printf("%-20s %s\n", entry->name, entry->desc);
entry++;
}
}
}
}
static void handle_input_line(const shell_command_t *command_list, char *line)
{
static const char *INCORRECT_QUOTING = "shell: incorrect quoting";
/* first we need to calculate the number of arguments */
unsigned argc = 0;
char *pos = line;
int contains_esc_seq = 0;
while (1) {
if ((unsigned char) *pos > ' ') {
/* found an argument */
if (*pos == '"' || *pos == '\'') {
/* it's a quoted argument */
const char quote_char = *pos;
do {
++pos;
if (!*pos) {
puts(INCORRECT_QUOTING);
return;
}
else if (*pos == '\\') {
/* skip over the next character */
++contains_esc_seq;
++pos;
if (!*pos) {
puts(INCORRECT_QUOTING);
return;
}
continue;
}
} while (*pos != quote_char);
if ((unsigned char) pos[1] > ' ') {
puts(INCORRECT_QUOTING);
return;
}
}
else {
/* it's an unquoted argument */
do {
if (*pos == '\\') {
/* skip over the next character */
++contains_esc_seq;
++pos;
if (!*pos) {
puts(INCORRECT_QUOTING);
return;
}
}
++pos;
if (*pos == '"') {
puts(INCORRECT_QUOTING);
return;
}
} while ((unsigned char) *pos > ' ');
}
/* count the number of arguments we got */
++argc;
}
/* zero out the current position (space or quotation mark) and advance */
if (*pos > 0) {
*pos = 0;
++pos;
}
else {
break;
}
}
if (!argc) {
return;
}
/* then we fill the argv array */
char *argv[argc + 1];
argv[argc] = NULL;
pos = line;
for (unsigned i = 0; i < argc; ++i) {
while (!*pos) {
++pos;
}
if (*pos == '"' || *pos == '\'') {
++pos;
}
argv[i] = pos;
while (*pos) {
++pos;
}
}
for (char **arg = argv; contains_esc_seq && *arg; ++arg) {
for (char *c = *arg; *c; ++c) {
if (*c != '\\') {
continue;
}
for (char *d = c; *d; ++d) {
*d = d[1];
}
if (--contains_esc_seq == 0) {
break;
}
}
}
/* then we call the appropriate handler */
shell_command_handler_t handler = find_handler(command_list, argv[0]);
if (handler != NULL) {
handler(argc, argv);
}
else {
if (strcmp("help", argv[0]) == 0) {
print_help(command_list);
}
else {
printf("shell: command not found: %s\n", argv[0]);
}
}
}
static int readline(char *buf, size_t size)
{
char *line_buf_ptr = buf;
while (1) {
if ((line_buf_ptr - buf) >= ((int) size) - 1) {
return -1;
}
int c = getchar();
if (c < 0) {
return 1;
}
/* We allow Unix linebreaks (\n), DOS linebreaks (\r\n), and Mac linebreaks (\r). */
/* QEMU transmits only a single '\r' == 13 on hitting enter ("-serial stdio"). */
/* DOS newlines are handled like hitting enter twice, but empty lines are ignored. */
if (c == '\r' || c == '\n') {
*line_buf_ptr = '\0';
#ifndef SHELL_NO_ECHO
_putchar('\r');
_putchar('\n');
#endif
/* return 1 if line is empty, 0 otherwise */
return line_buf_ptr == buf;
}
/* QEMU uses 0x7f (DEL) as backspace, while 0x08 (BS) is for most terminals */
else if (c == 0x08 || c == 0x7f) {
if (line_buf_ptr == buf) {
/* The line is empty. */
continue;
}
*--line_buf_ptr = '\0';
/* white-tape the character */
#ifndef SHELL_NO_ECHO
_putchar('\b');
_putchar(' ');
_putchar('\b');
#endif
}
else {
*line_buf_ptr++ = c;
#ifndef SHELL_NO_ECHO
_putchar(c);
#endif
}
}
}
static inline void print_prompt(void)
{
#ifndef SHELL_NO_PROMPT
_putchar('>');
_putchar(' ');
#endif
#ifdef MODULE_NEWLIB
fflush(stdout);
#endif
}
void shell_run(const shell_command_t *shell_commands, char *line_buf, int len)
{
print_prompt();
while (1) {
int res = readline(line_buf, len);
if (!res) {
handle_input_line(shell_commands, line_buf);
}
print_prompt();
}
}