Loading...
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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | /*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <shell/shell.h>
#include "shell_utils.h"
#include "shell_ops.h"
#include "shell_vt100.h"
#define SHELL_MSG_CMD_NOT_SUPPORTED "Command not supported.\n"
#define SHELL_HELP_CLEAR "Clear screen."
#define SHELL_HELP_BACKSPACE_MODE "Toggle backspace key mode.\n" \
"Some terminals are not sending separate escape code for " \
"backspace and delete button. This command forces shell to interpret" \
" delete key as backspace."
#define SHELL_HELP_BACKSPACE_MODE_BACKSPACE "Set different escape" \
" code for backspace and delete key."
#define SHELL_HELP_BACKSPACE_MODE_DELETE "Set the same escape" \
" code for backspace and delete key."
#define SHELL_HELP_COLORS "Toggle colored syntax."
#define SHELL_HELP_COLORS_OFF "Disable colored syntax."
#define SHELL_HELP_COLORS_ON "Enable colored syntax."
#define SHELL_HELP_STATISTICS "Shell statistics."
#define SHELL_HELP_STATISTICS_SHOW \
"Get shell statistics for the Logger module."
#define SHELL_HELP_STATISTICS_RESET \
"Reset shell statistics for the Logger module."
#define SHELL_HELP_RESIZE \
"Console gets terminal screen size or assumes 80 in case" \
" the readout fails. It must be executed after each terminal" \
" width change to ensure correct text display."
#define SHELL_HELP_RESIZE_DEFAULT \
"Assume 80 chars screen width and send this setting " \
"to the terminal."
#define SHELL_HELP_HISTORY "Command history."
#define SHELL_HELP_ECHO "Toggle shell echo."
#define SHELL_HELP_ECHO_ON "Enable shell echo."
#define SHELL_HELP_ECHO_OFF \
"Disable shell echo. Editing keys and meta-keys are not handled"
#define SHELL_HELP_SHELL "Useful, not Unix-like shell commands."
#define SHELL_HELP_HELP "Prints help message."
#define SHELL_MSG_UNKNOWN_PARAMETER " unknown parameter: "
#define SHELL_MAX_TERMINAL_SIZE (250u)
/* 10 == {esc, [, 2, 5, 0, ;, 2, 5, 0, '\0'} */
#define SHELL_CURSOR_POSITION_BUFFER (10u)
/* Function reads cursor position from terminal. */
static int cursor_position_get(const struct shell *shell, u16_t *x, u16_t *y)
{
u16_t buff_idx = 0U;
size_t cnt;
char c = 0;
*x = 0U;
*y = 0U;
memset(shell->ctx->temp_buff, 0, sizeof(shell->ctx->temp_buff));
/* escape code asking terminal about its size */
static char const cmd_get_terminal_size[] = "\033[6n";
shell_raw_fprintf(shell->fprintf_ctx, cmd_get_terminal_size);
/* fprintf buffer needs to be flushed to start sending prepared
* escape code to the terminal.
*/
transport_buffer_flush(shell);
/* timeout for terminal response = ~1s */
for (u16_t i = 0; i < 1000; i++) {
do {
(void)shell->iface->api->read(shell->iface, &c,
sizeof(c), &cnt);
if (cnt == 0) {
k_busy_wait(1000);
break;
}
if ((c != SHELL_VT100_ASCII_ESC) &&
(shell->ctx->temp_buff[0] !=
SHELL_VT100_ASCII_ESC)) {
continue;
}
if (c == 'R') { /* End of response from the terminal. */
shell->ctx->temp_buff[buff_idx] = '\0';
if (shell->ctx->temp_buff[1] != '[') {
shell->ctx->temp_buff[0] = 0;
return -EIO;
}
/* Index start position in the buffer where 'y'
* is stored.
*/
buff_idx = 2U;
while (shell->ctx->temp_buff[buff_idx] != ';') {
*y = *y * 10U +
(shell->ctx->temp_buff[buff_idx++] -
'0');
if (buff_idx >=
CONFIG_SHELL_CMD_BUFF_SIZE) {
return -EMSGSIZE;
}
}
if (++buff_idx >= CONFIG_SHELL_CMD_BUFF_SIZE) {
return -EIO;
}
while (shell->ctx->temp_buff[buff_idx]
!= '\0') {
*x = *x * 10U +
(shell->ctx->temp_buff[buff_idx++] -
'0');
if (buff_idx >=
CONFIG_SHELL_CMD_BUFF_SIZE) {
return -EMSGSIZE;
}
}
/* horizontal cursor position */
if (*x > SHELL_MAX_TERMINAL_SIZE) {
*x = SHELL_MAX_TERMINAL_SIZE;
}
/* vertical cursor position */
if (*y > SHELL_MAX_TERMINAL_SIZE) {
*y = SHELL_MAX_TERMINAL_SIZE;
}
shell->ctx->temp_buff[0] = 0;
return 0;
}
shell->ctx->temp_buff[buff_idx] = c;
if (++buff_idx > SHELL_CURSOR_POSITION_BUFFER - 1) {
shell->ctx->temp_buff[0] = 0;
/* data_buf[SHELL_CURSOR_POSITION_BUFFER - 1]
* is reserved for '\0'
*/
return -ENOMEM;
}
} while (cnt > 0);
}
return -ETIMEDOUT;
}
/* Function gets terminal width and height. */
static int terminal_size_get(const struct shell *shell)
{
u16_t x; /* horizontal position */
u16_t y; /* vertical position */
int ret_val = 0;
cursor_save(shell);
/* Assumption: terminal width and height < 999. */
/* Move to last column. */
shell_op_cursor_vert_move(shell, -SHELL_MAX_TERMINAL_SIZE);
/* Move to last row. */
shell_op_cursor_horiz_move(shell, SHELL_MAX_TERMINAL_SIZE);
if (cursor_position_get(shell, &x, &y) == 0) {
shell->ctx->vt100_ctx.cons.terminal_wid = x;
shell->ctx->vt100_ctx.cons.terminal_hei = y;
} else {
ret_val = -ENOTSUP;
}
cursor_restore(shell);
return ret_val;
}
static int cmd_clear(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argv);
SHELL_VT100_CMD(shell, SHELL_VT100_CURSORHOME);
SHELL_VT100_CMD(shell, SHELL_VT100_CLEARSCREEN);
return 0;
}
static int cmd_bacskpace_mode_backspace(const struct shell *shell, size_t argc,
char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
flag_mode_delete_set(shell, false);
return 0;
}
static int cmd_bacskpace_mode_delete(const struct shell *shell, size_t argc,
char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
flag_mode_delete_set(shell, true);
return 0;
}
static int cmd_colors_off(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
flag_use_colors_set(shell, false);
return 0;
}
static int cmd_colors_on(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argv);
ARG_UNUSED(argv);
flag_use_colors_set(shell, true);
return 0;
}
static int cmd_echo_off(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
flag_echo_set(shell, false);
return 0;
}
static int cmd_echo_on(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
flag_echo_set(shell, true);
return 0;
}
static int cmd_echo(const struct shell *shell, size_t argc, char **argv)
{
if (argc == 2) {
shell_error(shell, "%s:%s%s", argv[0],
SHELL_MSG_UNKNOWN_PARAMETER, argv[1]);
return -EINVAL;
}
shell_print(shell, "Echo status: %s",
flag_echo_get(shell) ? "on" : "off");
return 0;
}
static int cmd_help(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_print(shell,
"Please press the <Tab> button to see all available commands.\n"
"You can also use the <Tab> button to prompt or auto-complete"
" all commands or its subcommands.\n"
"You can try to call commands with <-h> or <--help> parameter"
" for more information.");
#if CONFIG_SHELL_METAKEYS
shell_print(shell,
"Shell supports following meta-keys:\n"
"Ctrl+a, Ctrl+b, Ctrl+c, Ctrl+d, Ctrl+e, Ctrl+f, Ctrl+k,"
" Ctrl+l, Ctrl+u, Ctrl+w\nAlt+b, Alt+f.\nPlease refer to"
" shell documentation for more details.");
#endif
return 0;
}
static int cmd_history(const struct shell *shell, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
size_t i = 0;
u16_t len;
if (!IS_ENABLED(CONFIG_SHELL_HISTORY)) {
shell_error(shell, "Command not supported.");
return -ENOEXEC;
}
while (1) {
shell_history_get(shell->history, true,
shell->ctx->temp_buff, &len);
if (len) {
shell_print(shell, "[%3d] %s",
i++, shell->ctx->temp_buff);
} else {
break;
}
}
shell->ctx->temp_buff[0] = '\0';
return 0;
}
static int cmd_shell_stats_show(const struct shell *shell, size_t argc,
char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
if (!IS_ENABLED(CONFIG_SHELL_STATS)) {
shell_error(shell, "Command not supported.");
return -ENOEXEC;
}
shell_print(shell, "Lost logs: %u", shell->stats->log_lost_cnt);
return 0;
}
static int cmd_shell_stats_reset(const struct shell *shell,
size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
if (!IS_ENABLED(CONFIG_SHELL_STATS)) {
shell_error(shell, "Command not supported.");
return -ENOEXEC;
}
shell->stats->log_lost_cnt = 0;
return 0;
}
static int cmd_resize_default(const struct shell *shell,
size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
SHELL_VT100_CMD(shell, SHELL_VT100_SETCOL_80);
shell->ctx->vt100_ctx.cons.terminal_wid = SHELL_DEFAULT_TERMINAL_WIDTH;
shell->ctx->vt100_ctx.cons.terminal_hei = SHELL_DEFAULT_TERMINAL_HEIGHT;
return 0;
}
static int cmd_resize(const struct shell *shell, size_t argc, char **argv)
{
int err;
if (!IS_ENABLED(CONFIG_SHELL_CMDS_RESIZE)) {
shell_error(shell, "Command not supported.");
return -ENOEXEC;
}
if (argc != 1) {
shell_error(shell, "%s:%s%s", argv[0],
SHELL_MSG_UNKNOWN_PARAMETER, argv[1]);
return -EINVAL;
}
err = terminal_size_get(shell);
if (err != 0) {
shell->ctx->vt100_ctx.cons.terminal_wid =
SHELL_DEFAULT_TERMINAL_WIDTH;
shell->ctx->vt100_ctx.cons.terminal_hei =
SHELL_DEFAULT_TERMINAL_HEIGHT;
shell_warn(shell, "No response from the terminal, assumed 80x24"
" screen size");
return -ENOEXEC;
}
return 0;
}
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_colors,
SHELL_CMD_ARG(off, NULL, SHELL_HELP_COLORS_OFF, cmd_colors_off, 1, 0),
SHELL_CMD_ARG(on, NULL, SHELL_HELP_COLORS_ON, cmd_colors_on, 1, 0),
SHELL_SUBCMD_SET_END
);
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_echo,
SHELL_CMD_ARG(off, NULL, SHELL_HELP_ECHO_OFF, cmd_echo_off, 1, 0),
SHELL_CMD_ARG(on, NULL, SHELL_HELP_ECHO_ON, cmd_echo_on, 1, 0),
SHELL_SUBCMD_SET_END
);
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_shell_stats,
SHELL_CMD_ARG(reset, NULL, SHELL_HELP_STATISTICS_RESET,
cmd_shell_stats_reset, 1, 0),
SHELL_CMD_ARG(show, NULL, SHELL_HELP_STATISTICS_SHOW,
cmd_shell_stats_show, 1, 0),
SHELL_SUBCMD_SET_END
);
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_backspace_mode,
SHELL_CMD_ARG(backspace, NULL, SHELL_HELP_BACKSPACE_MODE_BACKSPACE,
cmd_bacskpace_mode_backspace, 1, 0),
SHELL_CMD_ARG(delete, NULL, SHELL_HELP_BACKSPACE_MODE_DELETE,
cmd_bacskpace_mode_delete, 1, 0),
SHELL_SUBCMD_SET_END
);
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_shell,
SHELL_CMD(backspace_mode, &m_sub_backspace_mode,
SHELL_HELP_BACKSPACE_MODE, NULL),
SHELL_CMD(colors, &m_sub_colors, SHELL_HELP_COLORS, NULL),
SHELL_CMD_ARG(echo, &m_sub_echo, SHELL_HELP_ECHO, cmd_echo, 1, 1),
SHELL_CMD(stats, &m_sub_shell_stats, SHELL_HELP_STATISTICS, NULL),
SHELL_SUBCMD_SET_END
);
SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_resize,
SHELL_CMD_ARG(default, NULL, SHELL_HELP_RESIZE_DEFAULT,
cmd_resize_default, 1, 0),
SHELL_SUBCMD_SET_END
);
SHELL_CMD_ARG_REGISTER(clear, NULL, SHELL_HELP_CLEAR, cmd_clear, 1, 0);
SHELL_CMD_REGISTER(shell, &m_sub_shell, SHELL_HELP_SHELL, NULL);
SHELL_CMD_ARG_REGISTER(help, NULL, SHELL_HELP_HELP, cmd_help, 1, 255);
SHELL_CMD_ARG_REGISTER(history, NULL, SHELL_HELP_HISTORY, cmd_history, 1, 0);
SHELL_CMD_ARG_REGISTER(resize, &m_sub_resize, SHELL_HELP_RESIZE, cmd_resize,
1, 1);
|