X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fserver%2Ftelnet_server.c;h=a864f5fd293ea5d520b42f89052c95aab60a821a;hp=13fbc3fda61fe0d42acc5b6873cc72085233ac01;hb=HEAD;hpb=259e400276e939f7c8503068312e940dc25bdf46 diff --git a/src/server/telnet_server.c b/src/server/telnet_server.c index 13fbc3fda6..938bc5b062 100644 --- a/src/server/telnet_server.c +++ b/src/server/telnet_server.c @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + /*************************************************************************** * Copyright (C) 2005 by Dominic Rath * * Dominic.Rath@gmx.de * @@ -7,19 +9,6 @@ * * * Copyright (C) 2008 by Spencer Oliver * * spen@spen-soft.co.uk * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * ***************************************************************************/ #ifdef HAVE_CONFIG_H @@ -222,9 +211,8 @@ static int telnet_new_connection(struct connection *connection) { struct telnet_connection *telnet_connection; struct telnet_service *telnet_service = connection->service->priv; - int i; - telnet_connection = malloc(sizeof(struct telnet_connection)); + telnet_connection = calloc(1, sizeof(struct telnet_connection)); if (!telnet_connection) { LOG_ERROR("Failed to allocate telnet connection."); @@ -234,9 +222,6 @@ static int telnet_new_connection(struct connection *connection) connection->priv = telnet_connection; /* initialize telnet connection information */ - telnet_connection->closed = false; - telnet_connection->line_size = 0; - telnet_connection->line_cursor = 0; telnet_connection->prompt = strdup("> "); telnet_connection->prompt_visible = true; telnet_connection->state = TELNET_STATE_DATA; @@ -257,11 +242,6 @@ static int telnet_new_connection(struct connection *connection) telnet_write(connection, "\r", 1); telnet_prompt(connection); - /* initialize history */ - for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++) - telnet_connection->history[i] = NULL; - telnet_connection->next_history = 0; - telnet_connection->current_history = 0; telnet_load_history(telnet_connection); log_add_callback(telnet_log_callback, connection); @@ -592,16 +572,30 @@ static void telnet_auto_complete(struct connection *connection) LIST_HEAD(matches); - /* user command sequence, either at line beginning - * or we start over after these characters ';', '[', '{' */ + /* - user command sequence, either at line beginning + * or we start over after these characters ';', '[', '{' + * - user variable sequence, start after the character '$' + * and do not contain white spaces */ + bool is_variable_auto_completion = false; + bool have_spaces = false; size_t seq_start = (t_con->line_cursor == 0) ? 0 : (t_con->line_cursor - 1); - while (seq_start > 0) { + while (1) { char c = t_con->line[seq_start]; + if (c == ';' || c == '[' || c == '{') { seq_start++; break; + } else if (c == ' ') { + have_spaces = true; + } else if (c == '$' && !have_spaces) { + is_variable_auto_completion = true; + seq_start++; + break; } + if (seq_start == 0) + break; + seq_start--; } @@ -610,7 +604,11 @@ static void telnet_auto_complete(struct connection *connection) while ((usr_cmd_pos < t_con->line_cursor) && isspace(t_con->line[usr_cmd_pos])) usr_cmd_pos++; - /* user command length */ + /* check user command length */ + if (t_con->line_cursor < usr_cmd_pos) { + telnet_bell(connection); + return; + } size_t usr_cmd_len = t_con->line_cursor - usr_cmd_pos; /* optimize multiple spaces in the user command, @@ -631,7 +629,12 @@ static void telnet_auto_complete(struct connection *connection) query[usr_cmd_len] = '\0'; /* filter commands */ - char *query_cmd = alloc_printf("_telnet_autocomplete_helper {%s*}", query); + char *query_cmd; + + if (is_variable_auto_completion) + query_cmd = alloc_printf("lsort [info vars {%s*}]", query); + else + query_cmd = alloc_printf("_telnet_autocomplete_helper {%s*}", query); if (!query_cmd) { LOG_ERROR("Out of memory"); @@ -659,20 +662,22 @@ static void telnet_auto_complete(struct connection *connection) /* validate the command */ bool ignore_cmd = false; - Jim_Cmd *jim_cmd = Jim_GetCommand(command_context->interp, elem, JIM_NONE); - - if (!jim_cmd) { - /* Why we are here? Let's ignore it! */ - ignore_cmd = true; - } else if (jimcmd_is_oocd_command(jim_cmd)) { - struct command *cmd = jimcmd_privdata(jim_cmd); + if (!is_variable_auto_completion) { + Jim_Cmd *jim_cmd = Jim_GetCommand(command_context->interp, elem, JIM_NONE); - if (cmd && !cmd->handler && !cmd->jim_handler) { - /* Initial part of a multi-word command. Ignore it! */ - ignore_cmd = true; - } else if (cmd && cmd->mode == COMMAND_CONFIG) { - /* Not executable after config phase. Ignore it! */ + if (!jim_cmd) { + /* Why we are here? Let's ignore it! */ ignore_cmd = true; + } else if (jimcmd_is_oocd_command(jim_cmd)) { + struct command *cmd = jimcmd_privdata(jim_cmd); + + if (cmd && !cmd->handler && !cmd->jim_handler) { + /* Initial part of a multi-word command. Ignore it! */ + ignore_cmd = true; + } else if (cmd && cmd->mode == COMMAND_CONFIG) { + /* Not executable after config phase. Ignore it! */ + ignore_cmd = true; + } } } @@ -925,6 +930,15 @@ static int telnet_connection_closed(struct connection *connection) return ERROR_OK; } +static const struct service_driver telnet_service_driver = { + .name = "telnet", + .new_connection_during_keep_alive_handler = NULL, + .new_connection_handler = telnet_new_connection, + .input_handler = telnet_input, + .connection_closed_handler = telnet_connection_closed, + .keep_client_alive_handler = NULL, +}; + int telnet_init(char *banner) { if (strcmp(telnet_port, "disabled") == 0) { @@ -942,8 +956,7 @@ int telnet_init(char *banner) telnet_service->banner = banner; - int ret = add_service("telnet", telnet_port, CONNECTION_LIMIT_UNLIMITED, - telnet_new_connection, telnet_input, telnet_connection_closed, + int ret = add_service(&telnet_service_driver, telnet_port, CONNECTION_LIMIT_UNLIMITED, telnet_service); if (ret != ERROR_OK) {