1 /*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #ifndef SHELL_UTILS_H__
7 #define SHELL_UTILS_H__
8
9 #include <zephyr.h>
10 #include <shell/shell.h>
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 #define SHELL_MSG_SPECIFY_SUBCOMMAND "Please specify a subcommand.\n"
17
18 int32_t z_row_span_with_buffer_offsets_get(struct shell_multiline_cons *cons,
19 uint16_t offset1,
20 uint16_t offset2);
21
22 int32_t z_column_span_with_buffer_offsets_get(struct shell_multiline_cons *cons,
23 uint16_t offset1,
24 uint16_t offset2);
25
26 void z_shell_multiline_data_calc(struct shell_multiline_cons *cons,
27 uint16_t buff_pos, uint16_t buff_len);
28
z_shell_strlen(const char * str)29 static inline uint16_t z_shell_strlen(const char *str)
30 {
31 return str == NULL ? 0U : (uint16_t)strlen(str);
32 }
33
34 char z_shell_make_argv(size_t *argc, const char **argv,
35 char *cmd, uint8_t max_argc);
36
37 /** @brief Removes pattern and following space
38 *
39 */
40 void z_shell_pattern_remove(char *buff, uint16_t *buff_len,
41 const char *pattern);
42
43 /** @brief Get subcommand with given index from the root.
44 *
45 * @param parent Parent entry. Null to get root command from index.
46 * @param idx Command index.
47 * @param dloc Location used to write dynamic entry.
48 *
49 * @return Fetched command or null if command with that index does not exist.
50 */
51 const struct shell_static_entry *z_shell_cmd_get(
52 const struct shell_static_entry *parent,
53 size_t idx,
54 struct shell_static_entry *dloc);
55
56 const struct shell_static_entry *z_shell_find_cmd(
57 const struct shell_static_entry *parent,
58 const char *cmd_str,
59 struct shell_static_entry *dloc);
60
61 /* @internal @brief Function returns pointer to a shell's subcommands array
62 * for a level given by argc and matching command patter provided in argv.
63 *
64 * @param shell Entry. NULL for root entry.
65 * @param argc Number of arguments.
66 * @param argv Pointer to an array with arguments.
67 * @param match_arg Subcommand level of last matching argument.
68 * @param d_entry Shell static command descriptor.
69 * @param only_static If true search only for static commands.
70 *
71 * @return Pointer to found command.
72 */
73 const struct shell_static_entry *z_shell_get_last_command(
74 const struct shell_static_entry *entry,
75 size_t argc,
76 const char *argv[],
77 size_t *match_arg,
78 struct shell_static_entry *dloc,
79 bool only_static);
80
81 void z_shell_spaces_trim(char *str);
82 void z_shell_cmd_trim(const struct shell *shell);
83
84 const struct shell_static_entry *root_cmd_find(const char *syntax);
85
z_transport_buffer_flush(const struct shell * shell)86 static inline void z_transport_buffer_flush(const struct shell *shell)
87 {
88 z_shell_fprintf_buffer_flush(shell->fprintf_ctx);
89 }
90
z_shell_in_select_mode(const struct shell * shell)91 static inline bool z_shell_in_select_mode(const struct shell *shell)
92 {
93 return shell->ctx->selected_cmd == NULL ? false : true;
94 }
95
96 #ifdef __cplusplus
97 }
98 #endif
99
100 #endif /* SHELL_UTILS_H__ */
101