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 | /*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ctype.h>
#include "shell_ops.h"
#include "shell_help.h"
#include "shell_utils.h"
/* Function prints a string on terminal screen with requested margin.
* It takes care to not divide words.
* shell Pointer to shell instance.
* p_str Pointer to string to be printed.
* terminal_offset Requested left margin.
* offset_first_line Add margin to the first printed line.
*/
static void formatted_text_print(const struct shell *shell, const char *str,
size_t terminal_offset, bool offset_first_line)
{
size_t offset = 0;
size_t length;
if (str == NULL) {
return;
}
if (offset_first_line) {
shell_op_cursor_horiz_move(shell, terminal_offset);
}
/* Skipping whitespace. */
while (isspace((int) *(str + offset))) {
++offset;
}
while (true) {
size_t idx = 0;
length = shell_strlen(str) - offset;
if (length <=
shell->ctx->vt100_ctx.cons.terminal_wid - terminal_offset) {
for (idx = 0; idx < length; idx++) {
if (*(str + offset + idx) == '\n') {
transport_buffer_flush(shell);
shell_write(shell, str + offset, idx);
offset += idx + 1;
cursor_next_line_move(shell);
shell_op_cursor_horiz_move(shell,
terminal_offset);
break;
}
}
/* String will fit in one line. */
shell_raw_fprintf(shell->fprintf_ctx, str + offset);
break;
}
/* String is longer than terminal line so text needs to
* divide in the way to not divide words.
*/
length = shell->ctx->vt100_ctx.cons.terminal_wid
- terminal_offset;
while (true) {
/* Determining line break. */
if (isspace((int) (*(str + offset + idx)))) {
length = idx;
if (*(str + offset + idx) == '\n') {
break;
}
}
if ((idx + terminal_offset) >=
shell->ctx->vt100_ctx.cons.terminal_wid) {
/* End of line reached. */
break;
}
++idx;
}
/* Writing one line, fprintf IO buffer must be flushed
* before calling shell_write.
*/
transport_buffer_flush(shell);
shell_write(shell, str + offset, length);
offset += length;
/* Calculating text offset to ensure that next line will
* not begin with a space.
*/
while (isspace((int) (*(str + offset)))) {
++offset;
}
cursor_next_line_move(shell);
shell_op_cursor_horiz_move(shell, terminal_offset);
}
cursor_next_line_move(shell);
}
static void help_item_print(const struct shell *shell, const char *item_name,
u16_t item_name_width, const char *item_help)
{
static const u8_t tabulator[] = " ";
const u16_t offset = 2 * strlen(tabulator) + item_name_width + 1;
if (item_name == NULL) {
return;
}
if (!IS_ENABLED(CONFIG_NEWLIB_LIBC) && !IS_ENABLED(CONFIG_ARCH_POSIX)) {
/* print option name */
shell_internal_fprintf(shell, SHELL_NORMAL, "%s%-*s%s:",
tabulator,
item_name_width, item_name,
tabulator);
} else {
u16_t tmp = item_name_width - strlen(item_name);
char space = ' ';
shell_internal_fprintf(shell, SHELL_NORMAL, "%s%s", tabulator,
item_name);
for (u16_t i = 0; i < tmp; i++) {
shell_write(shell, &space, 1);
}
shell_internal_fprintf(shell, SHELL_NORMAL, "%s:", tabulator);
}
if (item_help == NULL) {
cursor_next_line_move(shell);
return;
}
/* print option help */
formatted_text_print(shell, item_help, offset, false);
}
/* Function is printing command help, its subcommands name and subcommands
* help string.
*/
void shell_help_subcmd_print(const struct shell *shell)
{
const struct shell_static_entry *entry = NULL;
struct shell_static_entry static_entry;
u16_t longest_syntax = 0U;
size_t cmd_idx = 0;
/* Checking if there are any subcommands available. */
if (!shell->ctx->active_cmd.subcmd) {
return;
}
/* Searching for the longest subcommand to print. */
do {
shell_cmd_get(shell, shell->ctx->active_cmd.subcmd,
!SHELL_CMD_ROOT_LVL,
cmd_idx++, &entry, &static_entry);
if (!entry) {
break;
}
u16_t len = shell_strlen(entry->syntax);
longest_syntax = longest_syntax > len ? longest_syntax : len;
} while (cmd_idx != 0); /* too many commands */
if (cmd_idx == 1) {
return;
}
shell_internal_fprintf(shell, SHELL_NORMAL, "Subcommands:\n");
/* Printing subcommands and help string (if exists). */
cmd_idx = 0;
while (true) {
shell_cmd_get(shell, shell->ctx->active_cmd.subcmd,
!SHELL_CMD_ROOT_LVL,
cmd_idx++, &entry, &static_entry);
if (entry == NULL) {
break;
}
help_item_print(shell, entry->syntax, longest_syntax,
entry->help);
}
}
void shell_help_cmd_print(const struct shell *shell)
{
static const char cmd_sep[] = " - "; /* commands separator */
u16_t field_width = shell_strlen(shell->ctx->active_cmd.syntax) +
shell_strlen(cmd_sep);
shell_internal_fprintf(shell, SHELL_NORMAL, "%s%s",
shell->ctx->active_cmd.syntax, cmd_sep);
formatted_text_print(shell, shell->ctx->active_cmd.help,
field_width, false);
}
|