+/* check buffer size leaving one spare character for string null termination */
+static inline bool telnet_can_insert(struct connection *connection, size_t len)
+{
+ struct telnet_connection *t_con = connection->priv;
+
+ return t_con->line_size + len < TELNET_LINE_MAX_SIZE;
+}
+
+/* write to telnet console, and update the telnet_connection members
+ * this function is capable of inserting in the middle of a line
+ * please ensure that data does not contain special characters (\n, \r, \t, \b ...)
+ *
+ * returns false when it fails to insert the requested data
+ */
+static bool telnet_insert(struct connection *connection, const void *data, size_t len)
+{
+ struct telnet_connection *t_con = connection->priv;
+
+ if (!telnet_can_insert(connection, len)) {
+ telnet_bell(connection);
+ return false;
+ }
+
+ if (t_con->line_cursor < t_con->line_size) {
+ /* we have some content after the cursor */
+ memmove(t_con->line + t_con->line_cursor + len,
+ t_con->line + t_con->line_cursor,
+ t_con->line_size - t_con->line_cursor);
+ }
+
+ strncpy(t_con->line + t_con->line_cursor, data, len);
+
+ telnet_write(connection,
+ t_con->line + t_con->line_cursor,
+ t_con->line_size + len - t_con->line_cursor);
+
+ t_con->line_size += len;
+ t_con->line_cursor += len;
+
+ for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
+ telnet_write(connection, "\b", 1);
+
+ return true;
+}
+
+static void telnet_delete_character(struct connection *connection)
+{
+ struct telnet_connection *t_con = connection->priv;
+
+ if (t_con->line_cursor == 0)
+ return;
+
+ if (t_con->line_cursor != t_con->line_size) {
+ size_t i;
+ telnet_write(connection, "\b", 1);
+ t_con->line_cursor--;
+ t_con->line_size--;
+ memmove(t_con->line + t_con->line_cursor,
+ t_con->line + t_con->line_cursor + 1,
+ t_con->line_size -
+ t_con->line_cursor);
+
+ telnet_write(connection,
+ t_con->line + t_con->line_cursor,
+ t_con->line_size -
+ t_con->line_cursor);
+ telnet_write(connection, " \b", 2);
+ for (i = t_con->line_cursor; i < t_con->line_size; i++)
+ telnet_write(connection, "\b", 1);
+ } else {
+ t_con->line_size--;
+ t_con->line_cursor--;
+ /* back space: move the 'printer' head one char
+ * back, overwrite with space, move back again */
+ telnet_write(connection, "\b \b", 3);
+ }
+}
+
+static void telnet_remove_character(struct connection *connection)
+{
+ struct telnet_connection *t_con = connection->priv;
+
+ if (t_con->line_cursor < t_con->line_size) {
+ size_t i;
+ t_con->line_size--;
+ /* remove char from line buffer */
+ memmove(t_con->line + t_con->line_cursor,
+ t_con->line + t_con->line_cursor + 1,
+ t_con->line_size - t_con->line_cursor);
+
+ /* print remainder of buffer */
+ telnet_write(connection, t_con->line + t_con->line_cursor,
+ t_con->line_size - t_con->line_cursor);
+ /* overwrite last char with whitespace */
+ telnet_write(connection, " \b", 2);
+
+ /* move back to cursor position*/
+ for (i = t_con->line_cursor; i < t_con->line_size; i++)
+ telnet_write(connection, "\b", 1);
+ }
+}
+
+static int telnet_exec_line(struct connection *connection)
+{
+ struct telnet_connection *t_con = connection->priv;
+ struct command_context *command_context = connection->cmd_ctx;
+ int retval;
+
+ telnet_write(connection, "\r\n\x00", 3);
+
+ if (strcmp(t_con->line, "history") == 0) {
+ retval = telnet_history_print(connection);
+
+ if (retval != ERROR_OK)
+ return retval;
+
+ return ERROR_OK;
+ }
+
+ telnet_history_add(connection);
+
+ t_con->line_size = 0;
+
+ /* to suppress prompt in log callback during command execution */
+ t_con->prompt_visible = false;
+
+ if (strcmp(t_con->line, "shutdown") == 0)
+ telnet_save_history(t_con);
+
+ retval = command_run_line(command_context, t_con->line);
+
+ t_con->line_cursor = 0;
+ t_con->prompt_visible = true;
+
+ if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
+ return ERROR_SERVER_REMOTE_CLOSED;
+
+ /* the prompt is always placed at the line beginning */
+ telnet_write(connection, "\r", 1);
+
+ retval = telnet_prompt(connection);
+ if (retval == ERROR_SERVER_REMOTE_CLOSED)
+ return ERROR_SERVER_REMOTE_CLOSED;
+
+ return ERROR_OK;
+}
+
+static void telnet_cut_line_to_end(struct connection *connection)
+{
+ struct telnet_connection *t_con = connection->priv;
+
+ /* FIXME: currently this function does not save to clipboard */
+
+ if (t_con->line_cursor < t_con->line_size) {
+ /* overwrite with space, until end of line, move back */
+ for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
+ telnet_write(connection, " ", 1);
+ for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
+ telnet_write(connection, "\b", 1);
+ t_con->line[t_con->line_cursor] = '\0';
+ t_con->line_size = t_con->line_cursor;
+ }
+}
+
+static void telnet_interrupt(struct connection *connection)
+{
+ struct telnet_connection *t_con = connection->priv;
+
+ /* print '^C' at line end, and display a new command prompt */
+ telnet_move_cursor(connection, t_con->line_size);
+ telnet_write(connection, "^C\n\r", 4);
+ t_con->line_cursor = 0;
+ t_con->line_size = 0;
+ telnet_prompt(connection);
+}
+
+static void telnet_auto_complete(struct connection *connection)
+{
+ struct telnet_connection *t_con = connection->priv;
+ struct command_context *command_context = connection->cmd_ctx;
+
+ struct cmd_match {
+ char *cmd;
+ struct list_head lh;
+ };
+
+ LIST_HEAD(matches);
+
+ /* - 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 (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--;
+ }
+
+ /* user command position in the line, ignore leading spaces */
+ size_t usr_cmd_pos = seq_start;
+ while ((usr_cmd_pos < t_con->line_cursor) && isspace(t_con->line[usr_cmd_pos]))
+ usr_cmd_pos++;
+
+ /* 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,
+ * because info commands does not tolerate multiple spaces */
+ size_t optimized_spaces = 0;
+ char query[usr_cmd_len + 1];
+ for (size_t i = 0; i < usr_cmd_len; i++) {
+ if ((i < usr_cmd_len - 1) && isspace(t_con->line[usr_cmd_pos + i])
+ && isspace(t_con->line[usr_cmd_pos + i + 1])) {
+ optimized_spaces++;
+ continue;
+ }
+
+ query[i - optimized_spaces] = t_con->line[usr_cmd_pos + i];
+ }
+
+ usr_cmd_len -= optimized_spaces;
+ query[usr_cmd_len] = '\0';
+
+ /* filter commands */
+ 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");
+ return;
+ }
+
+ int retval = Jim_EvalSource(command_context->interp, __FILE__, __LINE__, query_cmd);
+ free(query_cmd);
+ if (retval != JIM_OK)
+ return;
+
+ Jim_Obj *list = Jim_GetResult(command_context->interp);
+ Jim_IncrRefCount(list);
+
+ /* common prefix length of the matched commands */
+ size_t common_len = 0;
+ char *first_match = NULL; /* used to compute the common prefix length */
+
+ int len = Jim_ListLength(command_context->interp, list);
+ for (int i = 0; i < len; i++) {
+ Jim_Obj *elem = Jim_ListGetIndex(command_context->interp, list, i);
+ Jim_IncrRefCount(elem);
+
+ char *name = (char *)Jim_GetString(elem, NULL);
+
+ /* validate the command */
+ bool ignore_cmd = false;
+ if (!is_variable_auto_completion) {
+ 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 (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;
+ }
+ }
+ }
+
+ /* save the command in the prediction list */
+ if (!ignore_cmd) {
+ struct cmd_match *match = calloc(1, sizeof(struct cmd_match));
+ if (!match) {
+ LOG_ERROR("Out of memory");
+ Jim_DecrRefCount(command_context->interp, elem);
+ break; /* break the for loop */
+ }
+
+ if (list_empty(&matches)) {
+ common_len = strlen(name);
+ first_match = name;
+ } else {
+ size_t new_common_len = usr_cmd_len; /* save some loops */
+
+ while (new_common_len < common_len && first_match[new_common_len] == name[new_common_len])
+ new_common_len++;
+
+ common_len = new_common_len;
+ }
+
+ match->cmd = name;
+ list_add_tail(&match->lh, &matches);
+ }
+
+ Jim_DecrRefCount(command_context->interp, elem);
+ }
+ /* end of command filtering */
+
+ /* proceed with auto-completion */
+ if (list_empty(&matches))
+ telnet_bell(connection);
+ else if (common_len == usr_cmd_len && list_is_singular(&matches) && t_con->line_cursor == t_con->line_size)
+ telnet_insert(connection, " ", 1);
+ else if (common_len > usr_cmd_len) {
+ int completion_size = common_len - usr_cmd_len;
+ if (telnet_insert(connection, first_match + usr_cmd_len, completion_size)) {
+ /* in bash this extra space is only added when the cursor in at the end of line */
+ if (list_is_singular(&matches) && t_con->line_cursor == t_con->line_size)
+ telnet_insert(connection, " ", 1);
+ }
+ } else if (!list_is_singular(&matches)) {
+ telnet_write(connection, "\n\r", 2);
+
+ struct cmd_match *match;
+ list_for_each_entry(match, &matches, lh) {
+ telnet_write(connection, match->cmd, strlen(match->cmd));
+ telnet_write(connection, "\n\r", 2);
+ }
+
+ telnet_prompt(connection);
+ telnet_write(connection, t_con->line, t_con->line_size);
+
+ /* restore the terminal visible cursor location */
+ for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
+ telnet_write(connection, "\b", 1);
+ }
+
+ /* destroy the command_list */
+ struct cmd_match *tmp, *match;
+ list_for_each_entry_safe(match, tmp, &matches, lh)
+ free(match);
+
+ Jim_DecrRefCount(command_context->interp, list);
+}
+