+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);
+}
+