+// SPDX-License-Identifier: GPL-2.0-or-later
+
/***************************************************************************
* Copyright (C) 2005 by Dominic Rath *
* Dominic.Rath@gmx.de *
* *
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
{
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.");
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;
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);
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--;
}
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,
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");
/* 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;
+ }
}
}
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) {
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) {