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 | /*
* 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) {
z_shell_op_cursor_horiz_move(shell, terminal_offset);
}
/* Skipping whitespace. */
while (isspace((int) *(str + offset))) {
++offset;
}
while (true) {
size_t idx = 0;
length = z_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') {
z_transport_buffer_flush(shell);
z_shell_write(shell, str + offset, idx);
offset += idx + 1;
z_cursor_next_line_move(shell);
z_shell_op_cursor_horiz_move(shell,
terminal_offset);
break;
}
}
/* String will fit in one line. */
z_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.
*/
z_transport_buffer_flush(shell);
z_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;
}
z_cursor_next_line_move(shell);
z_shell_op_cursor_horiz_move(shell, terminal_offset);
}
z_cursor_next_line_move(shell);
}
static void help_item_print(const struct shell *shell, const char *item_name,
uint16_t item_name_width, const char *item_help)
{
static const uint8_t tabulator[] = " ";
const uint16_t offset = 2 * strlen(tabulator) + item_name_width + 1;
if ((item_name == NULL) || (item_name[0] == '\0')) {
return;
}
if (!IS_ENABLED(CONFIG_NEWLIB_LIBC) &&
!IS_ENABLED(CONFIG_ARCH_POSIX)) {
/* print option name */
z_shell_fprintf(shell, SHELL_NORMAL, "%s%-*s%s:", tabulator,
item_name_width, item_name, tabulator);
} else {
uint16_t tmp = item_name_width - strlen(item_name);
char space = ' ';
z_shell_fprintf(shell, SHELL_NORMAL, "%s%s", tabulator,
item_name);
for (uint16_t i = 0; i < tmp; i++) {
z_shell_write(shell, &space, 1);
}
z_shell_fprintf(shell, SHELL_NORMAL, "%s:", tabulator);
}
if (item_help == NULL) {
z_cursor_next_line_move(shell);
return;
}
/* print option help */
formatted_text_print(shell, item_help, offset, false);
}
/* Function prints all subcommands of the parent command together with their
* help string
*/
void z_shell_help_subcmd_print(const struct shell *shell,
const struct shell_static_entry *parent,
const char *description)
{
const struct shell_static_entry *entry = NULL;
struct shell_static_entry dloc;
uint16_t longest = 0U;
size_t idx = 0;
/* Searching for the longest subcommand to print. */
while ((entry = z_shell_cmd_get(parent, idx++, &dloc)) != NULL) {
longest = Z_MAX(longest, z_shell_strlen(entry->syntax));
}
/* No help to print */
if (longest == 0) {
return;
}
if (description != NULL) {
z_shell_fprintf(shell, SHELL_NORMAL, description);
}
/* Printing subcommands and help string (if exists). */
idx = 0;
while ((entry = z_shell_cmd_get(parent, idx++, &dloc)) != NULL) {
help_item_print(shell, entry->syntax, longest, entry->help);
}
}
void z_shell_help_cmd_print(const struct shell *shell,
const struct shell_static_entry *cmd)
{
static const char cmd_sep[] = " - "; /* commands separator */
uint16_t field_width;
field_width = z_shell_strlen(cmd->syntax) + z_shell_strlen(cmd_sep);
z_shell_fprintf(shell, SHELL_NORMAL, "%s%s", cmd->syntax, cmd_sep);
formatted_text_print(shell, cmd->help, field_width, false);
}
bool z_shell_help_request(const char *str)
{
if (!IS_ENABLED(CONFIG_SHELL_HELP_OPT_PARSE)) {
return false;
}
if (!strcmp(str, "-h") || !strcmp(str, "--help")) {
return true;
}
return false;
}
|