|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | +#include <lib/fnmatch/fnmatch.h> |
| 9 | +#include "shell_wildcard.h" |
| 10 | +#include "shell_utils.h" |
| 11 | + |
| 12 | +static void subcmd_get(const struct shell_cmd_entry *cmd, |
| 13 | + size_t idx, const struct shell_static_entry **entry, |
| 14 | + struct shell_static_entry *d_entry) { |
| 15 | + assert(entry != NULL); |
| 16 | + assert(st_entry != NULL); |
| 17 | + |
| 18 | + if (cmd == NULL) { |
| 19 | + *entry = NULL; |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + if (cmd->is_dynamic) { |
| 24 | + cmd->u.dynamic_get(idx, d_entry); |
| 25 | + *entry = (d_entry->syntax != NULL) ? d_entry : NULL; |
| 26 | + } else { |
| 27 | + *entry = (cmd->u.entry[idx].syntax != NULL) ? |
| 28 | + &cmd->u.entry[idx] : NULL; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +static enum shell_wildcard_status command_add(char *buff, u16_t *buff_len, |
| 33 | + char const *cmd, |
| 34 | + char const *pattern) |
| 35 | +{ |
| 36 | + u16_t cmd_len = shell_strlen(cmd); |
| 37 | + char *completion_addr; |
| 38 | + u16_t shift; |
| 39 | + |
| 40 | + /* +1 for space */ |
| 41 | + if ((*buff_len + cmd_len + 1) > CONFIG_SHELL_CMD_BUFF_SIZE) { |
| 42 | + return SHELL_WILDCARD_CMD_MISSING_SPACE; |
| 43 | + } |
| 44 | + |
| 45 | + completion_addr = strstr(buff, pattern); |
| 46 | + |
| 47 | + if (!completion_addr) { |
| 48 | + return SHELL_WILDCARD_CMD_NO_MATCH_FOUND; |
| 49 | + } |
| 50 | + |
| 51 | + shift = shell_strlen(completion_addr); |
| 52 | + |
| 53 | + /* make place for new command: + 1 for space + 1 for EOS */ |
| 54 | + memmove(completion_addr + cmd_len + 1, completion_addr, shift + 1); |
| 55 | + memcpy(completion_addr, cmd, cmd_len); |
| 56 | + /* adding space to not brake next command in the buffer */ |
| 57 | + completion_addr[cmd_len] = ' '; |
| 58 | + |
| 59 | + *buff_len += cmd_len + 1; // + 1 for space |
| 60 | + |
| 61 | + return SHELL_WILDCARD_CMD_ADDED; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @internal @brief Function for searching and adding commands to the temporary |
| 66 | + * shell buffer matching to wildcard pattern. |
| 67 | + * |
| 68 | + * This function is internal to shell module and shall be not called directly. |
| 69 | + * |
| 70 | + * @param[in/out] shell Pointer to the CLI instance. |
| 71 | + * @param[in] cmd Pointer to command which will be processed |
| 72 | + * @param[in] pattern Pointer to wildcard pattern. |
| 73 | + * |
| 74 | + * @retval WILDCARD_CMD_ADDED All matching commands added to the buffer. |
| 75 | + * @retval WILDCARD_CMD_ADDED_MISSING_SPACE Not all matching commands added |
| 76 | + * because CONFIG_SHELL_CMD_BUFF_SIZE |
| 77 | + * is too small. |
| 78 | + * @retval WILDCARD_CMD_NO_MATCH_FOUND No matching command found. |
| 79 | + */ |
| 80 | +static enum shell_wildcard_status commands_expand(const struct shell *shell, |
| 81 | + const struct shell_cmd_entry *cmd, |
| 82 | + const char *pattern) |
| 83 | +{ |
| 84 | + enum shell_wildcard_status ret_val = SHELL_WILDCARD_CMD_NO_MATCH_FOUND; |
| 85 | + struct shell_static_entry const * p_static_entry = NULL; |
| 86 | + struct shell_static_entry static_entry; |
| 87 | + size_t cmd_idx = 0; |
| 88 | + size_t cnt = 0; |
| 89 | + |
| 90 | + do { |
| 91 | + subcmd_get(cmd, cmd_idx++, &p_static_entry, &static_entry); |
| 92 | + |
| 93 | + if (!p_static_entry) { |
| 94 | + break; |
| 95 | + } |
| 96 | + |
| 97 | + if (0 == fnmatch(pattern, p_static_entry->syntax, 0)) { |
| 98 | + ret_val = command_add(shell->ctx->temp_buff, |
| 99 | + &shell->ctx->cmd_tmp_buff_len, |
| 100 | + p_static_entry->syntax, pattern); |
| 101 | + if (ret_val == SHELL_WILDCARD_CMD_MISSING_SPACE) { |
| 102 | + shell_fprintf(shell, |
| 103 | + SHELL_WARNING, |
| 104 | + "Command buffer is too short to" |
| 105 | + "expand all commands matching " |
| 106 | + "wildcard pattern: %s\r\n", |
| 107 | + pattern); |
| 108 | + break; |
| 109 | + } else if (ret_val != SHELL_WILDCARD_CMD_ADDED) |
| 110 | + { |
| 111 | + break; |
| 112 | + } |
| 113 | + cnt++; |
| 114 | + } |
| 115 | + } while(cmd_idx); |
| 116 | + |
| 117 | + if (cnt > 0) |
| 118 | + { |
| 119 | + shell_pattern_remove(shell->ctx->temp_buff, |
| 120 | + &shell->ctx->cmd_tmp_buff_len, pattern); |
| 121 | + } |
| 122 | + |
| 123 | + return ret_val; |
| 124 | +} |
| 125 | + |
| 126 | +bool shell_wildcard_character_exist(const char *str) |
| 127 | +{ |
| 128 | + size_t str_len = shell_strlen(str); |
| 129 | + |
| 130 | + for (size_t i = 0; i < str_len; i++) { |
| 131 | + if ((str[i] == '?') || (str[i] == '*')) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return false; |
| 137 | +} |
| 138 | + |
| 139 | +void shell_wildcard_prepare(const struct shell *shell) |
| 140 | +{ |
| 141 | + /* Wildcard can be correctly handled under following conditions: |
| 142 | + * - wildcard command does not have a handler |
| 143 | + * - wildcard command is on the deepest commands level |
| 144 | + * - other commands on the same level as wildcard command shall also not |
| 145 | + * have a handler |
| 146 | + * |
| 147 | + * Algorithm: |
| 148 | + * 1. Command buffer is copied to Temp buffer. |
| 149 | + * 2. Algorithm goes through Command buffer to find handlers and |
| 150 | + * subcommands. |
| 151 | + * 3. If algorithm will find a wildcard character it switches to Temp |
| 152 | + * buffer. |
| 153 | + * 4. In the Temp buffer command with found wildcard character is |
| 154 | + * changed into matching command(s). |
| 155 | + * 5. Algorithm switch back to Command buffer and analyzes next command. |
| 156 | + * 6. When all arguments are analyzed from Command buffer, Temp buffer |
| 157 | + * is copied to Command buffer. |
| 158 | + * 7. Last found handler is executed with all arguments in the Command |
| 159 | + * buffer. |
| 160 | + */ |
| 161 | + |
| 162 | + memset(shell->ctx->temp_buff, 0, sizeof(shell->ctx->temp_buff)); |
| 163 | + memcpy(shell->ctx->temp_buff, |
| 164 | + shell->ctx->cmd_buff, |
| 165 | + shell->ctx->cmd_buff_len); |
| 166 | + |
| 167 | + /* Function shell_spaces_trim must be used instead of shell_make_argv. |
| 168 | + * At this point it is important to keep temp_buff as a one string. |
| 169 | + * It will allow to find wildcard commands easily with strstr function. |
| 170 | + */ |
| 171 | + shell_spaces_trim(shell->ctx->temp_buff); |
| 172 | + |
| 173 | + /* +1 for EOS*/ |
| 174 | + shell->ctx->cmd_tmp_buff_len = shell_strlen(shell->ctx->temp_buff) + 1; |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +enum shell_wildcard_status shell_wildcard_process(const struct shell *shell, |
| 179 | + const struct shell_cmd_entry *cmd, |
| 180 | + const char *pattern) |
| 181 | +{ |
| 182 | + enum shell_wildcard_status ret_val = SHELL_WILDCARD_NOT_FOUND; |
| 183 | + |
| 184 | + if (cmd == NULL) { |
| 185 | + return ret_val; |
| 186 | + } |
| 187 | + |
| 188 | + if (!shell_wildcard_character_exist(pattern)) { |
| 189 | + return ret_val; |
| 190 | + } |
| 191 | + |
| 192 | + /* Function will search commands tree for commands matching wildcard |
| 193 | + * pattern stored in argv[cmd_lvl]. If match is found wildcard pattern |
| 194 | + * will be replaced by matching commands in temp_buffer. If there is no |
| 195 | + * space to add all matching commands function will add as many as |
| 196 | + * possible. Next it will continue to search for next wildcard pattern |
| 197 | + * and it will try to add matching commands. |
| 198 | + */ |
| 199 | + ret_val = commands_expand(shell, cmd, pattern); |
| 200 | + |
| 201 | + return ret_val; |
| 202 | +} |
| 203 | + |
| 204 | +void shell_wildcard_finalize(const struct shell *shell) |
| 205 | +{ |
| 206 | + memcpy(shell->ctx->cmd_buff, |
| 207 | + shell->ctx->temp_buff, |
| 208 | + shell->ctx->cmd_tmp_buff_len); |
| 209 | + shell->ctx->cmd_buff_len = shell->ctx->cmd_tmp_buff_len; |
| 210 | +} |
0 commit comments