Lines Matching full:sh

44 static inline void receive_state_change(const struct shell *sh,  in receive_state_change()  argument
47 sh->ctx->receive_state = state; in receive_state_change()
50 static void cmd_buffer_clear(const struct shell *sh) in cmd_buffer_clear() argument
52 sh->ctx->cmd_buff[0] = '\0'; /* clear command buffer */ in cmd_buffer_clear()
53 sh->ctx->cmd_buff_pos = 0; in cmd_buffer_clear()
54 sh->ctx->cmd_buff_len = 0; in cmd_buffer_clear()
57 static void shell_internal_help_print(const struct shell *sh) in shell_internal_help_print() argument
63 z_shell_help_cmd_print(sh, &sh->ctx->active_cmd); in shell_internal_help_print()
64 z_shell_help_subcmd_print(sh, &sh->ctx->active_cmd, in shell_internal_help_print()
78 static int cmd_precheck(const struct shell *sh, in cmd_precheck() argument
82 z_shell_fprintf(sh, SHELL_ERROR, in cmd_precheck()
84 sh->ctx->active_cmd.syntax); in cmd_precheck()
87 shell_internal_help_print(sh); in cmd_precheck()
96 static inline void state_set(const struct shell *sh, enum shell_state state) in state_set() argument
98 sh->ctx->state = state; in state_set()
100 if (state == SHELL_STATE_ACTIVE && !sh->ctx->bypass) { in state_set()
101 cmd_buffer_clear(sh); in state_set()
102 if (z_flag_print_noinit_get(sh)) { in state_set()
103 z_shell_fprintf(sh, SHELL_WARNING, "%s", in state_set()
105 z_flag_print_noinit_set(sh, false); in state_set()
107 z_shell_print_prompt_and_cmd(sh); in state_set()
111 static inline enum shell_state state_get(const struct shell *sh) in state_get() argument
113 return sh->ctx->state; in state_get()
117 selected_cmd_get(const struct shell *sh) in selected_cmd_get() argument
121 return sh->ctx->selected_cmd; in selected_cmd_get()
127 static void tab_item_print(const struct shell *sh, const char *option, in tab_item_print() argument
136 sh->ctx->vt100_ctx.printed_cmd = 0; in tab_item_print()
142 columns = (sh->ctx->vt100_ctx.cons.terminal_wid in tab_item_print()
147 if (sh->ctx->vt100_ctx.printed_cmd++ % columns == 0U) { in tab_item_print()
148 z_shell_fprintf(sh, SHELL_OPTION, "\n%s%s", tab, option); in tab_item_print()
150 z_shell_fprintf(sh, SHELL_OPTION, "%s", option); in tab_item_print()
153 z_shell_op_cursor_horiz_move(sh, diff); in tab_item_print()
156 static void history_init(const struct shell *sh) in history_init() argument
162 z_shell_history_init(sh->history); in history_init()
165 static void history_purge(const struct shell *sh) in history_purge() argument
171 z_shell_history_purge(sh->history); in history_purge()
174 static void history_mode_exit(const struct shell *sh) in history_mode_exit() argument
180 z_flag_history_exit_set(sh, false); in history_mode_exit()
181 z_shell_history_mode_exit(sh->history); in history_mode_exit()
184 static void history_put(const struct shell *sh, uint8_t *line, size_t length) in history_put() argument
190 z_shell_history_put(sh->history, line, length); in history_put()
193 static void history_handle(const struct shell *sh, bool up) in history_handle() argument
204 if (z_flag_history_exit_get(sh)) { in history_handle()
205 z_flag_history_exit_set(sh, false); in history_handle()
206 z_shell_history_mode_exit(sh->history); in history_handle()
210 if (!z_shell_history_active(sh->history)) { in history_handle()
212 uint16_t cmd_len = z_shell_strlen(sh->ctx->cmd_buff); in history_handle()
215 strcpy(sh->ctx->temp_buff, in history_handle()
216 sh->ctx->cmd_buff); in history_handle()
218 sh->ctx->temp_buff[0] = '\0'; in history_handle()
227 history_mode = z_shell_history_get(sh->history, up, in history_handle()
228 sh->ctx->cmd_buff, &len); in history_handle()
232 strcpy(sh->ctx->cmd_buff, sh->ctx->temp_buff); in history_handle()
233 len = z_shell_strlen(sh->ctx->cmd_buff); in history_handle()
236 z_shell_op_cursor_home_move(sh); in history_handle()
237 z_clear_eos(sh); in history_handle()
238 z_shell_print_cmd(sh); in history_handle()
239 sh->ctx->cmd_buff_pos = len; in history_handle()
240 sh->ctx->cmd_buff_len = len; in history_handle()
241 z_shell_op_cond_next_line(sh); in history_handle()
244 static inline uint16_t completion_space_get(const struct shell *sh) in completion_space_get() argument
247 sh->ctx->cmd_buff_len; in completion_space_get()
252 static bool tab_prepare(const struct shell *sh, in tab_prepare() argument
258 uint16_t compl_space = completion_space_get(sh); in tab_prepare()
266 memcpy(sh->ctx->temp_buff, sh->ctx->cmd_buff, in tab_prepare()
267 sh->ctx->cmd_buff_pos); in tab_prepare()
268 sh->ctx->temp_buff[sh->ctx->cmd_buff_pos] = '\0'; in tab_prepare()
271 (void)z_shell_make_argv(argc, *argv, sh->ctx->temp_buff, in tab_prepare()
284 !z_shell_in_select_mode(sh)) { in tab_prepare()
292 int space = (sh->ctx->cmd_buff_pos > 0) ? in tab_prepare()
293 isspace((int)sh->ctx->cmd_buff[sh->ctx->cmd_buff_pos - 1]) : 0; in tab_prepare()
298 *cmd = selected_cmd_get(sh); in tab_prepare()
304 *cmd = z_shell_get_last_command(selected_cmd_get(sh), search_argc, in tab_prepare()
324 static void find_completion_candidates(const struct shell *sh, in find_completion_candidates() argument
355 static void autocomplete(const struct shell *sh, in autocomplete() argument
364 /* sh->ctx->active_cmd can be safely used outside of command context in autocomplete()
367 match = z_shell_cmd_get(cmd, subcmd_idx, &sh->ctx->active_cmd); in autocomplete()
376 z_shell_op_char_insert(sh, ' '); in autocomplete()
383 z_shell_op_completion_insert(sh, in autocomplete()
389 if (isspace((int) sh->ctx->cmd_buff[ in autocomplete()
390 sh->ctx->cmd_buff_pos]) == 0) { in autocomplete()
391 if (z_flag_insert_mode_get(sh)) { in autocomplete()
392 z_flag_insert_mode_set(sh, false); in autocomplete()
393 z_shell_op_char_insert(sh, ' '); in autocomplete()
394 z_flag_insert_mode_set(sh, true); in autocomplete()
396 z_shell_op_char_insert(sh, ' '); in autocomplete()
403 z_shell_op_cursor_move(sh, 1); in autocomplete()
424 static void tab_options_print(const struct shell *sh, in tab_options_print() argument
434 tab_item_print(sh, SHELL_INIT_OPTION_PRINTER, longest); in tab_options_print()
437 /* sh->ctx->active_cmd can be safely used outside of command in tab_options_print()
440 match = z_shell_cmd_get(cmd, idx, &sh->ctx->active_cmd); in tab_options_print()
448 tab_item_print(sh, match->syntax, longest); in tab_options_print()
452 z_cursor_next_line_move(sh); in tab_options_print()
453 z_shell_print_prompt_and_cmd(sh); in tab_options_print()
456 static uint16_t common_beginning_find(const struct shell *sh, in common_beginning_find() argument
470 strncpy(sh->ctx->temp_buff, match->syntax, in common_beginning_find()
471 sizeof(sh->ctx->temp_buff) - 1); in common_beginning_find()
485 curr_common = str_common(sh->ctx->temp_buff, match2->syntax, in common_beginning_find()
496 static void partial_autocomplete(const struct shell *sh, in partial_autocomplete() argument
503 uint16_t common = common_beginning_find(sh, cmd, &completion, first, in partial_autocomplete()
511 z_shell_op_completion_insert(sh, &completion[arg_len], in partial_autocomplete()
516 static int exec_cmd(const struct shell *sh, size_t argc, const char **argv, in exec_cmd() argument
521 if (sh->ctx->active_cmd.handler == NULL) { in exec_cmd()
526 if (help_entry->help != sh->ctx->active_cmd.help) { in exec_cmd()
527 sh->ctx->active_cmd = *help_entry; in exec_cmd()
529 shell_internal_help_print(sh); in exec_cmd()
533 z_shell_fprintf(sh, SHELL_ERROR, in exec_cmd()
540 if (sh->ctx->active_cmd.args.mandatory) { in exec_cmd()
541 uint32_t mand = sh->ctx->active_cmd.args.mandatory; in exec_cmd()
542 uint8_t opt8 = sh->ctx->active_cmd.args.optional; in exec_cmd()
548 ret_val = cmd_precheck(sh, in_range); in exec_cmd()
556 z_flag_cmd_ctx_set(sh, true); in exec_cmd()
560 k_mutex_unlock(&sh->ctx->wr_mtx); in exec_cmd()
561 ret_val = sh->ctx->active_cmd.handler(sh, argc, in exec_cmd()
564 k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER); in exec_cmd()
565 z_flag_cmd_ctx_set(sh, false); in exec_cmd()
594 static bool wildcard_check_report(const struct shell *sh, bool found, in wildcard_check_report() argument
601 z_shell_op_cursor_end_move(sh); in wildcard_check_report()
602 z_shell_op_cond_next_line(sh); in wildcard_check_report()
604 z_shell_fprintf(sh, SHELL_ERROR, in wildcard_check_report()
627 static int execute(const struct shell *sh) in execute() argument
631 const struct shell_static_entry *parent = selected_cmd_get(sh); in execute()
640 char *cmd_buf = sh->ctx->cmd_buff; in execute()
643 z_shell_op_cursor_end_move(sh); in execute()
644 if (!z_shell_cursor_in_empty_line(sh)) { in execute()
645 z_cursor_next_line_move(sh); in execute()
648 memset(&sh->ctx->active_cmd, 0, sizeof(sh->ctx->active_cmd)); in execute()
651 z_shell_cmd_trim(sh); in execute()
652 history_put(sh, sh->ctx->cmd_buff, in execute()
653 sh->ctx->cmd_buff_len); in execute()
657 z_shell_wildcard_prepare(sh); in execute()
665 active_cmd_prepare(parent, &sh->ctx->active_cmd, &help_entry, in execute()
682 z_shell_fprintf(sh, SHELL_ERROR, in execute()
693 sh->ctx->active_cmd = help_entry; in execute()
694 shell_internal_help_print(sh); in execute()
699 z_shell_fprintf(sh, SHELL_ERROR, in execute()
708 status = z_shell_wildcard_process(sh, entry, in execute()
734 if (wildcard_check_report(sh, wildcard_found, entry) in execute()
739 active_cmd_prepare(entry, &sh->ctx->active_cmd, in execute()
746 (!z_shell_in_select_mode(sh) || in execute()
747 sh->ctx->selected_cmd->handler == NULL)) { in execute()
748 z_shell_fprintf(sh, SHELL_ERROR, in execute()
770 z_shell_fprintf(sh, SHELL_ERROR, "%s\n", in execute()
776 z_shell_wildcard_finalize(sh); in execute()
782 &argv[selected_cmd_get(sh) ? 1 : 0], in execute()
783 sh->ctx->cmd_buff, in execute()
786 if (selected_cmd_get(sh)) { in execute()
806 return exec_cmd(sh, cmd_lvl - cmd_with_handler_lvl, in execute()
810 static void tab_handle(const struct shell *sh) in tab_handle() argument
823 bool tab_possible = tab_prepare(sh, &cmd, &argv, &argc, &arg_idx, in tab_handle()
830 find_completion_candidates(sh, cmd, argv[arg_idx], &first, &cnt, in tab_handle()
835 autocomplete(sh, cmd, argv[arg_idx], first); in tab_handle()
837 tab_options_print(sh, cmd, argv[arg_idx], first, cnt, in tab_handle()
839 partial_autocomplete(sh, cmd, argv[arg_idx], first, cnt); in tab_handle()
843 static void alt_metakeys_handle(const struct shell *sh, char data) in alt_metakeys_handle() argument
850 z_shell_op_cursor_word_move(sh, -1); in alt_metakeys_handle()
852 z_shell_op_cursor_word_move(sh, 1); in alt_metakeys_handle()
855 if (selected_cmd_get(sh) != NULL) { in alt_metakeys_handle()
856 z_shell_cmd_line_erase(sh); in alt_metakeys_handle()
857 z_shell_fprintf(sh, SHELL_WARNING, in alt_metakeys_handle()
860 sh->ctx->selected_cmd = root_cmd_find(CONFIG_SHELL_CMD_ROOT); in alt_metakeys_handle()
862 sh->ctx->selected_cmd = NULL; in alt_metakeys_handle()
864 z_shell_print_prompt_and_cmd(sh); in alt_metakeys_handle()
869 static void ctrl_metakeys_handle(const struct shell *sh, char data) in ctrl_metakeys_handle() argument
878 z_shell_op_cursor_home_move(sh); in ctrl_metakeys_handle()
882 z_shell_op_left_arrow(sh); in ctrl_metakeys_handle()
886 z_shell_op_cursor_end_move(sh); in ctrl_metakeys_handle()
887 if (!z_shell_cursor_in_empty_line(sh)) { in ctrl_metakeys_handle()
888 z_cursor_next_line_move(sh); in ctrl_metakeys_handle()
890 z_flag_history_exit_set(sh, true); in ctrl_metakeys_handle()
891 state_set(sh, SHELL_STATE_ACTIVE); in ctrl_metakeys_handle()
895 z_shell_op_char_delete(sh); in ctrl_metakeys_handle()
899 z_shell_op_cursor_end_move(sh); in ctrl_metakeys_handle()
903 z_shell_op_right_arrow(sh); in ctrl_metakeys_handle()
907 z_shell_op_delete_from_cursor(sh); in ctrl_metakeys_handle()
911 Z_SHELL_VT100_CMD(sh, SHELL_VT100_CURSORHOME); in ctrl_metakeys_handle()
912 Z_SHELL_VT100_CMD(sh, SHELL_VT100_CLEARSCREEN); in ctrl_metakeys_handle()
913 z_shell_print_prompt_and_cmd(sh); in ctrl_metakeys_handle()
917 history_handle(sh, false); in ctrl_metakeys_handle()
921 history_handle(sh, true); in ctrl_metakeys_handle()
925 z_shell_op_cursor_home_move(sh); in ctrl_metakeys_handle()
926 cmd_buffer_clear(sh); in ctrl_metakeys_handle()
927 z_flag_history_exit_set(sh, true); in ctrl_metakeys_handle()
928 z_clear_eos(sh); in ctrl_metakeys_handle()
932 z_shell_op_word_remove(sh); in ctrl_metakeys_handle()
933 z_flag_history_exit_set(sh, true); in ctrl_metakeys_handle()
942 static bool process_nl(const struct shell *sh, uint8_t data) in process_nl() argument
945 z_flag_last_nl_set(sh, 0); in process_nl()
949 if ((z_flag_last_nl_get(sh) == 0U) || in process_nl()
950 (data == z_flag_last_nl_get(sh))) { in process_nl()
951 z_flag_last_nl_set(sh, data); in process_nl()
968 static void state_collect(const struct shell *sh) in state_collect() argument
974 shell_bypass_cb_t bypass = sh->ctx->bypass; in state_collect()
979 (void)sh->iface->api->read(sh->iface, buf, in state_collect()
982 z_flag_cmd_ctx_set(sh, true); in state_collect()
983 bypass(sh, buf, count); in state_collect()
984 z_flag_cmd_ctx_set(sh, false); in state_collect()
986 if (!(volatile shell_bypass_cb_t *)sh->ctx->bypass) { in state_collect()
987 state_set(sh, SHELL_STATE_ACTIVE); in state_collect()
996 (void)sh->iface->api->read(sh->iface, &data, in state_collect()
1006 switch (sh->ctx->receive_state) { in state_collect()
1008 if (process_nl(sh, data)) { in state_collect()
1009 if (!sh->ctx->cmd_buff_len) { in state_collect()
1010 history_mode_exit(sh); in state_collect()
1011 z_cursor_next_line_move(sh); in state_collect()
1014 sh->ctx->ret_val = execute(sh); in state_collect()
1019 state_set(sh, SHELL_STATE_ACTIVE); in state_collect()
1025 receive_state_change(sh, SHELL_RECEIVE_ESC); in state_collect()
1032 if (z_flag_echo_get(sh) && in state_collect()
1039 z_flag_history_exit_set(sh, true); in state_collect()
1040 tab_handle(sh); in state_collect()
1045 if (z_flag_echo_get(sh)) { in state_collect()
1046 z_flag_history_exit_set(sh, true); in state_collect()
1047 z_shell_op_char_backspace(sh); in state_collect()
1052 if (z_flag_echo_get(sh)) { in state_collect()
1053 z_flag_history_exit_set(sh, true); in state_collect()
1054 if (z_flag_mode_delete_get(sh)) { in state_collect()
1055 z_shell_op_char_backspace(sh); in state_collect()
1058 z_shell_op_char_delete(sh); in state_collect()
1065 z_flag_history_exit_set(sh, true); in state_collect()
1066 z_shell_op_char_insert(sh, data); in state_collect()
1067 } else if (z_flag_echo_get(sh)) { in state_collect()
1068 ctrl_metakeys_handle(sh, data); in state_collect()
1076 receive_state_change(sh, in state_collect()
1079 } else if (z_flag_echo_get(sh)) { in state_collect()
1080 alt_metakeys_handle(sh, data); in state_collect()
1082 receive_state_change(sh, SHELL_RECEIVE_DEFAULT); in state_collect()
1086 receive_state_change(sh, SHELL_RECEIVE_DEFAULT); in state_collect()
1088 if (!z_flag_echo_get(sh)) { in state_collect()
1094 history_handle(sh, true); in state_collect()
1098 history_handle(sh, false); in state_collect()
1102 z_shell_op_right_arrow(sh); in state_collect()
1106 z_shell_op_left_arrow(sh); in state_collect()
1110 receive_state_change(sh, in state_collect()
1114 z_shell_op_cursor_end_move(sh); in state_collect()
1118 receive_state_change(sh, in state_collect()
1122 z_shell_op_cursor_home_move(sh); in state_collect()
1126 receive_state_change(sh, in state_collect()
1130 bool status = z_flag_insert_mode_get(sh); in state_collect()
1132 z_flag_insert_mode_set(sh, !status); in state_collect()
1137 receive_state_change(sh, in state_collect()
1139 if (z_flag_echo_get(sh)) { in state_collect()
1140 z_shell_op_char_delete(sh); in state_collect()
1150 receive_state_change(sh, SHELL_RECEIVE_DEFAULT); in state_collect()
1154 receive_state_change(sh, SHELL_RECEIVE_DEFAULT); in state_collect()
1159 z_transport_buffer_flush(sh); in state_collect()
1164 struct shell *sh = (struct shell *)ctx; in transport_evt_handler() local
1168 &sh->ctx->signals[SHELL_SIGNAL_RXRDY] : in transport_evt_handler()
1169 &sh->ctx->signals[SHELL_SIGNAL_TXDONE]; in transport_evt_handler()
1173 static void shell_log_process(const struct shell *sh) in shell_log_process() argument
1181 z_shell_cmd_line_erase(sh); in shell_log_process()
1184 sh->log_backend); in shell_log_process()
1188 &sh->ctx->signals[SHELL_SIGNAL_RXRDY]; in shell_log_process()
1190 z_shell_print_prompt_and_cmd(sh); in shell_log_process()
1195 if (sh->ctx->cmd_buff_len) { in shell_log_process()
1204 static int instance_init(const struct shell *sh, in instance_init() argument
1208 __ASSERT_NO_MSG((sh->shell_flag == SHELL_FLAG_CRLF_DEFAULT) || in instance_init()
1209 (sh->shell_flag == SHELL_FLAG_OLF_CRLF)); in instance_init()
1211 memset(sh->ctx, 0, sizeof(*sh->ctx)); in instance_init()
1213 sh->ctx->selected_cmd = root_cmd_find(CONFIG_SHELL_CMD_ROOT); in instance_init()
1216 history_init(sh); in instance_init()
1218 k_mutex_init(&sh->ctx->wr_mtx); in instance_init()
1221 k_poll_signal_init(&sh->ctx->signals[i]); in instance_init()
1222 k_poll_event_init(&sh->ctx->events[i], in instance_init()
1225 &sh->ctx->signals[i]); in instance_init()
1229 sh->stats->log_lost_cnt = 0; in instance_init()
1232 z_flag_tx_rdy_set(sh, true); in instance_init()
1234 sh->ctx->vt100_ctx.cons.terminal_wid = in instance_init()
1236 sh->ctx->vt100_ctx.cons.terminal_hei = in instance_init()
1240 shell_prompt_change(sh, sh->default_prompt); in instance_init()
1242 sh->ctx->prompt = sh->default_prompt; in instance_init()
1243 sh->ctx->vt100_ctx.cons.name_len = z_shell_strlen(sh->ctx->prompt); in instance_init()
1254 sh->ctx->cfg.flags = cfg_flags; in instance_init()
1256 int ret = sh->iface->api->init(sh->iface, transport_config, in instance_init()
1258 (void *)sh); in instance_init()
1260 state_set(sh, SHELL_STATE_INITIALIZED); in instance_init()
1266 static int instance_uninit(const struct shell *sh) in instance_uninit() argument
1268 __ASSERT_NO_MSG(sh); in instance_uninit()
1269 __ASSERT_NO_MSG(sh->ctx && sh->iface); in instance_uninit()
1273 if (z_flag_processing_get(sh)) { in instance_uninit()
1279 z_shell_log_backend_disable(sh->log_backend); in instance_uninit()
1282 err = sh->iface->api->uninit(sh->iface); in instance_uninit()
1287 history_purge(sh); in instance_uninit()
1288 state_set(sh, SHELL_STATE_UNINITIALIZED); in instance_uninit()
1293 typedef void (*shell_signal_handler_t)(const struct shell *sh);
1295 static void shell_signal_handle(const struct shell *sh, in shell_signal_handle() argument
1299 struct k_poll_signal *sig = &sh->ctx->signals[sig_idx]; in shell_signal_handle()
1307 handler(sh); in shell_signal_handle()
1311 static void kill_handler(const struct shell *sh) in kill_handler() argument
1313 int err = instance_uninit(sh); in kill_handler()
1315 if (sh->ctx->uninit_cb) { in kill_handler()
1316 sh->ctx->uninit_cb(sh, err); in kill_handler()
1319 sh->ctx->tid = NULL; in kill_handler()
1328 struct shell *sh = shell_handle; in shell_thread() local
1331 z_flag_handle_log_set(sh, (bool)arg_log_backend); in shell_thread()
1332 sh->ctx->log_level = POINTER_TO_UINT(arg_log_level); in shell_thread()
1334 err = sh->iface->api->enable(sh->iface, false); in shell_thread()
1341 err = shell_start(sh); in shell_thread()
1349 err = k_poll(sh->ctx->events, SHELL_SIGNAL_TXDONE, in shell_thread()
1353 k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER); in shell_thread()
1354 z_shell_fprintf(sh, SHELL_ERROR, in shell_thread()
1356 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_thread()
1360 k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER); in shell_thread()
1362 shell_signal_handle(sh, SHELL_SIGNAL_KILL, kill_handler); in shell_thread()
1363 shell_signal_handle(sh, SHELL_SIGNAL_RXRDY, shell_process); in shell_thread()
1365 shell_signal_handle(sh, SHELL_SIGNAL_LOG_MSG, in shell_thread()
1369 if (sh->iface->api->update) { in shell_thread()
1370 sh->iface->api->update(sh->iface); in shell_thread()
1373 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_thread()
1377 int shell_init(const struct shell *sh, const void *transport_config, in shell_init() argument
1381 __ASSERT_NO_MSG(sh); in shell_init()
1382 __ASSERT_NO_MSG(sh->ctx && sh->iface && sh->default_prompt); in shell_init()
1384 if (sh->ctx->tid) { in shell_init()
1388 int err = instance_init(sh, transport_config, cfg_flags); in shell_init()
1394 k_tid_t tid = k_thread_create(sh->thread, in shell_init()
1395 sh->stack, CONFIG_SHELL_STACK_SIZE, in shell_init()
1396 shell_thread, (void *)sh, (void *)log_backend, in shell_init()
1400 sh->ctx->tid = tid; in shell_init()
1401 k_thread_name_set(tid, sh->name); in shell_init()
1406 void shell_uninit(const struct shell *sh, shell_uninit_cb_t cb) in shell_uninit() argument
1408 __ASSERT_NO_MSG(sh); in shell_uninit()
1412 &sh->ctx->signals[SHELL_SIGNAL_KILL]; in shell_uninit()
1414 sh->ctx->uninit_cb = cb; in shell_uninit()
1421 int err = instance_uninit(sh); in shell_uninit()
1424 cb(sh, err); in shell_uninit()
1430 int shell_start(const struct shell *sh) in shell_start() argument
1432 __ASSERT_NO_MSG(sh); in shell_start()
1433 __ASSERT_NO_MSG(sh->ctx && sh->iface && sh->default_prompt); in shell_start()
1435 if (state_get(sh) != SHELL_STATE_INITIALIZED) { in shell_start()
1439 if (IS_ENABLED(CONFIG_SHELL_LOG_BACKEND) && z_flag_handle_log_get(sh) in shell_start()
1440 && !z_flag_obscure_get(sh)) { in shell_start()
1441 z_shell_log_backend_enable(sh->log_backend, (void *)sh, sh->ctx->log_level); in shell_start()
1444 k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER); in shell_start()
1447 z_shell_vt100_color_set(sh, SHELL_NORMAL); in shell_start()
1453 z_cursor_next_line_move(sh); in shell_start()
1454 state_set(sh, SHELL_STATE_ACTIVE); in shell_start()
1462 z_shell_backend_rx_buffer_flush(sh); in shell_start()
1464 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_start()
1469 int shell_stop(const struct shell *sh) in shell_stop() argument
1471 __ASSERT_NO_MSG(sh); in shell_stop()
1472 __ASSERT_NO_MSG(sh->ctx); in shell_stop()
1474 enum shell_state state = state_get(sh); in shell_stop()
1481 state_set(sh, SHELL_STATE_INITIALIZED); in shell_stop()
1484 z_shell_log_backend_disable(sh->log_backend); in shell_stop()
1490 void shell_process(const struct shell *sh) in shell_process() argument
1492 __ASSERT_NO_MSG(sh); in shell_process()
1493 __ASSERT_NO_MSG(sh->ctx); in shell_process()
1496 z_flag_processing_set(sh, true); in shell_process()
1498 switch (sh->ctx->state) { in shell_process()
1505 state_collect(sh); in shell_process()
1512 z_flag_processing_set(sh, false); in shell_process()
1529 void shell_vfprintf(const struct shell *sh, enum shell_vt100_color color, in shell_vfprintf() argument
1532 __ASSERT_NO_MSG(sh); in shell_vfprintf()
1534 __ASSERT_NO_MSG(sh->ctx); in shell_vfprintf()
1535 __ASSERT_NO_MSG(z_flag_cmd_ctx_get(sh) || in shell_vfprintf()
1536 (k_current_get() != sh->ctx->tid)); in shell_vfprintf()
1537 __ASSERT_NO_MSG(sh->fprintf_ctx); in shell_vfprintf()
1541 if (state_get(sh) != SHELL_STATE_ACTIVE) { in shell_vfprintf()
1542 z_flag_print_noinit_set(sh, true); in shell_vfprintf()
1546 k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER); in shell_vfprintf()
1547 if (!z_flag_cmd_ctx_get(sh) && !sh->ctx->bypass && z_flag_use_vt100_get(sh)) { in shell_vfprintf()
1548 z_shell_cmd_line_erase(sh); in shell_vfprintf()
1550 z_shell_vfprintf(sh, color, fmt, args); in shell_vfprintf()
1551 if (!z_flag_cmd_ctx_get(sh) && !sh->ctx->bypass && z_flag_use_vt100_get(sh)) { in shell_vfprintf()
1552 z_shell_print_prompt_and_cmd(sh); in shell_vfprintf()
1554 z_transport_buffer_flush(sh); in shell_vfprintf()
1555 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_vfprintf()
1566 void shell_fprintf_impl(const struct shell *sh, enum shell_vt100_color color, in shell_fprintf_impl() argument
1572 shell_vfprintf(sh, color, fmt, args); in shell_fprintf_impl()
1576 void shell_fprintf_info(const struct shell *sh, const char *fmt, ...) in shell_fprintf_info() argument
1581 shell_vfprintf(sh, SHELL_INFO, fmt, args); in shell_fprintf_info()
1585 void shell_fprintf_normal(const struct shell *sh, const char *fmt, ...) in shell_fprintf_normal() argument
1590 shell_vfprintf(sh, SHELL_NORMAL, fmt, args); in shell_fprintf_normal()
1594 void shell_fprintf_warn(const struct shell *sh, const char *fmt, ...) in shell_fprintf_warn() argument
1599 shell_vfprintf(sh, SHELL_WARNING, fmt, args); in shell_fprintf_warn()
1603 void shell_fprintf_error(const struct shell *sh, const char *fmt, ...) in shell_fprintf_error() argument
1608 shell_vfprintf(sh, SHELL_ERROR, fmt, args); in shell_fprintf_error()
1612 void shell_hexdump_line(const struct shell *sh, unsigned int offset, in shell_hexdump_line() argument
1615 __ASSERT_NO_MSG(sh); in shell_hexdump_line()
1619 shell_fprintf_normal(sh, "%08X: ", offset); in shell_hexdump_line()
1623 shell_fprintf_normal(sh, " "); in shell_hexdump_line()
1627 shell_fprintf_normal(sh, "%02x ", in shell_hexdump_line()
1630 shell_fprintf_normal(sh, " "); in shell_hexdump_line()
1634 shell_fprintf_normal(sh, "|"); in shell_hexdump_line()
1638 shell_fprintf_normal(sh, " "); in shell_hexdump_line()
1644 shell_fprintf_normal(sh, "%c", in shell_hexdump_line()
1647 shell_fprintf_normal(sh, " "); in shell_hexdump_line()
1651 shell_print(sh, "|"); in shell_hexdump_line()
1654 void shell_hexdump(const struct shell *sh, const uint8_t *data, size_t len) in shell_hexdump() argument
1656 __ASSERT_NO_MSG(sh); in shell_hexdump()
1664 shell_hexdump_line(sh, p - data, p, line_len); in shell_hexdump()
1671 int shell_prompt_change(const struct shell *sh, const char *prompt) in shell_prompt_change() argument
1674 __ASSERT_NO_MSG(sh); in shell_prompt_change()
1683 if (k_mutex_lock(&sh->ctx->wr_mtx, K_MSEC(mtx_timeout_ms))) { in shell_prompt_change()
1688 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_prompt_change()
1692 strcpy(sh->ctx->prompt, prompt); in shell_prompt_change()
1694 sh->ctx->vt100_ctx.cons.name_len = prompt_length; in shell_prompt_change()
1696 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_prompt_change()
1704 void shell_help(const struct shell *sh) in shell_help() argument
1706 k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER); in shell_help()
1707 shell_internal_help_print(sh); in shell_help()
1708 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_help()
1711 int shell_execute_cmd(const struct shell *sh, const char *cmd) in shell_execute_cmd() argument
1724 if (sh == NULL) { in shell_execute_cmd()
1726 sh = shell_backend_dummy_get_ptr(); in shell_execute_cmd()
1732 __ASSERT(!z_flag_cmd_ctx_get(sh), "Function cannot be called" in shell_execute_cmd()
1735 strcpy(sh->ctx->cmd_buff, cmd); in shell_execute_cmd()
1736 sh->ctx->cmd_buff_len = cmd_len; in shell_execute_cmd()
1737 sh->ctx->cmd_buff_pos = cmd_len; in shell_execute_cmd()
1739 k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER); in shell_execute_cmd()
1740 ret_val = execute(sh); in shell_execute_cmd()
1741 k_mutex_unlock(&sh->ctx->wr_mtx); in shell_execute_cmd()
1743 cmd_buffer_clear(sh); in shell_execute_cmd()
1748 int shell_insert_mode_set(const struct shell *sh, bool val) in shell_insert_mode_set() argument
1750 if (sh == NULL) { in shell_insert_mode_set()
1754 return (int)z_flag_insert_mode_set(sh, val); in shell_insert_mode_set()
1757 int shell_use_colors_set(const struct shell *sh, bool val) in shell_use_colors_set() argument
1759 if (sh == NULL) { in shell_use_colors_set()
1763 return (int)z_flag_use_colors_set(sh, val); in shell_use_colors_set()
1766 int shell_use_vt100_set(const struct shell *sh, bool val) in shell_use_vt100_set() argument
1768 if (sh == NULL) { in shell_use_vt100_set()
1772 return (int)z_flag_use_vt100_set(sh, val); in shell_use_vt100_set()
1775 int shell_get_return_value(const struct shell *sh) in shell_get_return_value() argument
1777 if (sh == NULL) { in shell_get_return_value()
1781 return z_shell_get_return_value(sh); in shell_get_return_value()
1784 int shell_echo_set(const struct shell *sh, bool val) in shell_echo_set() argument
1786 if (sh == NULL) { in shell_echo_set()
1790 return (int)z_flag_echo_set(sh, val); in shell_echo_set()
1793 int shell_obscure_set(const struct shell *sh, bool val) in shell_obscure_set() argument
1795 if (sh == NULL) { in shell_obscure_set()
1799 return (int)z_flag_obscure_set(sh, val); in shell_obscure_set()
1802 int shell_mode_delete_set(const struct shell *sh, bool val) in shell_mode_delete_set() argument
1804 if (sh == NULL) { in shell_mode_delete_set()
1808 return (int)z_flag_mode_delete_set(sh, val); in shell_mode_delete_set()
1811 void shell_set_bypass(const struct shell *sh, shell_bypass_cb_t bypass) in shell_set_bypass() argument
1813 __ASSERT_NO_MSG(sh); in shell_set_bypass()
1815 sh->ctx->bypass = bypass; in shell_set_bypass()
1818 cmd_buffer_clear(sh); in shell_set_bypass()
1822 bool shell_ready(const struct shell *sh) in shell_ready() argument
1824 __ASSERT_NO_MSG(sh); in shell_ready()
1826 return state_get(sh) == SHELL_STATE_ACTIVE; in shell_ready()
1829 static int cmd_help(const struct shell *sh, size_t argc, char **argv) in cmd_help() argument
1835 shell_print(sh, "Please press the <Tab> button to see all available " in cmd_help()
1840 shell_print(sh, in cmd_help()
1846 shell_print(sh, in cmd_help()
1852 shell_print(sh, in cmd_help()
1861 z_shell_help_subcmd_print(sh, NULL, in cmd_help()
1867 shell_print(sh, "\nAvailable commands:"); in cmd_help()
1869 shell_print(sh, " %s", entry->syntax); in cmd_help()