server/telnet: simplify telnet_input function
[fw/openocd] / src / server / telnet_server.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007-2010 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2008 by Spencer Oliver                                  *
9  *   spen@spen-soft.co.uk                                                  *
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  *   This program is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
23  ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "telnet_server.h"
30 #include <target/target_request.h>
31 #include <helper/configuration.h>
32 #include <helper/list.h>
33
34 static char *telnet_port;
35
36 static char *negotiate =
37         "\xFF\xFB\x03"                  /* IAC WILL Suppress Go Ahead */
38         "\xFF\xFB\x01"                  /* IAC WILL Echo */
39         "\xFF\xFD\x03"                  /* IAC DO Suppress Go Ahead */
40         "\xFF\xFE\x01";                 /* IAC DON'T Echo */
41
42 #define CTRL(c) (c - '@')
43 #define TELNET_HISTORY  ".openocd_history"
44
45 /* The only way we can detect that the socket is closed is the first time
46  * we write to it, we will fail. Subsequent write operations will
47  * succeed. Shudder!
48  */
49 static int telnet_write(struct connection *connection, const void *data,
50         int len)
51 {
52         struct telnet_connection *t_con = connection->priv;
53         if (t_con->closed)
54                 return ERROR_SERVER_REMOTE_CLOSED;
55
56         if (connection_write(connection, data, len) == len)
57                 return ERROR_OK;
58         t_con->closed = true;
59         return ERROR_SERVER_REMOTE_CLOSED;
60 }
61
62 /* output an audible bell */
63 static int telnet_bell(struct connection *connection)
64 {
65         /* ("\a" does not work, at least on windows) */
66         return telnet_write(connection, "\x07", 1);
67 }
68
69 static int telnet_prompt(struct connection *connection)
70 {
71         struct telnet_connection *t_con = connection->priv;
72
73         return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
74 }
75
76 static int telnet_outputline(struct connection *connection, const char *line)
77 {
78         int len;
79
80         /* process lines in buffer */
81         while (*line) {
82                 char *line_end = strchr(line, '\n');
83
84                 if (line_end)
85                         len = line_end-line;
86                 else
87                         len = strlen(line);
88
89                 telnet_write(connection, line, len);
90                 if (line_end) {
91                         telnet_write(connection, "\r\n", 2);
92                         line += len + 1;
93                 } else
94                         line += len;
95         }
96
97         return ERROR_OK;
98 }
99
100 static int telnet_output(struct command_context *cmd_ctx, const char *line)
101 {
102         struct connection *connection = cmd_ctx->output_handler_priv;
103
104         return telnet_outputline(connection, line);
105 }
106
107 static void telnet_log_callback(void *priv, const char *file, unsigned line,
108         const char *function, const char *string)
109 {
110         struct connection *connection = priv;
111         struct telnet_connection *t_con = connection->priv;
112         size_t i;
113         size_t tmp;
114
115         /* If the prompt is not visible, simply output the message. */
116         if (!t_con->prompt_visible) {
117                 telnet_outputline(connection, string);
118                 return;
119         }
120
121         /* Clear the command line. */
122         tmp = strlen(t_con->prompt) + t_con->line_size;
123
124         for (i = 0; i < tmp; i += 16)
125                 telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
126                         MIN(tmp - i, 16));
127
128         for (i = 0; i < tmp; i += 16)
129                 telnet_write(connection, "                ", MIN(tmp - i, 16));
130
131         for (i = 0; i < tmp; i += 16)
132                 telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
133                         MIN(tmp - i, 16));
134
135         telnet_outputline(connection, string);
136
137         /* Put the command line to its previous state. */
138         telnet_prompt(connection);
139         telnet_write(connection, t_con->line, t_con->line_size);
140
141         for (i = t_con->line_cursor; i < t_con->line_size; i++)
142                 telnet_write(connection, "\b", 1);
143 }
144
145 static void telnet_load_history(struct telnet_connection *t_con)
146 {
147         FILE *histfp;
148         char buffer[TELNET_BUFFER_SIZE];
149         int i = 0;
150
151         char *history = get_home_dir(TELNET_HISTORY);
152
153         if (!history) {
154                 LOG_INFO("unable to get user home directory, telnet history will be disabled");
155                 return;
156         }
157
158         histfp = fopen(history, "rb");
159
160         if (histfp) {
161
162                 while (fgets(buffer, sizeof(buffer), histfp)) {
163
164                         char *p = strchr(buffer, '\n');
165                         if (p)
166                                 *p = '\0';
167                         if (buffer[0] && i < TELNET_LINE_HISTORY_SIZE)
168                                 t_con->history[i++] = strdup(buffer);
169                 }
170
171                 t_con->next_history = i;
172                 t_con->next_history %= TELNET_LINE_HISTORY_SIZE;
173                 /* try to set to last entry - 1, that way we skip over any exit/shutdown cmds */
174                 t_con->current_history = t_con->next_history > 0 ? i - 1 : 0;
175                 fclose(histfp);
176         }
177
178         free(history);
179 }
180
181 static void telnet_save_history(struct telnet_connection *t_con)
182 {
183         FILE *histfp;
184         int i;
185         int num;
186
187         char *history = get_home_dir(TELNET_HISTORY);
188
189         if (!history) {
190                 LOG_INFO("unable to get user home directory, telnet history will be disabled");
191                 return;
192         }
193
194         histfp = fopen(history, "wb");
195
196         if (histfp) {
197
198                 num = TELNET_LINE_HISTORY_SIZE;
199                 i = t_con->current_history + 1;
200                 i %= TELNET_LINE_HISTORY_SIZE;
201
202                 while (!t_con->history[i] && num > 0) {
203                         i++;
204                         i %= TELNET_LINE_HISTORY_SIZE;
205                         num--;
206                 }
207
208                 if (num > 0) {
209                         for (; num > 0; num--) {
210                                 fprintf(histfp, "%s\n", t_con->history[i]);
211                                 i++;
212                                 i %= TELNET_LINE_HISTORY_SIZE;
213                         }
214                 }
215                 fclose(histfp);
216         }
217
218         free(history);
219 }
220
221 static int telnet_new_connection(struct connection *connection)
222 {
223         struct telnet_connection *telnet_connection;
224         struct telnet_service *telnet_service = connection->service->priv;
225         int i;
226
227         telnet_connection = malloc(sizeof(struct telnet_connection));
228
229         if (!telnet_connection) {
230                 LOG_ERROR("Failed to allocate telnet connection.");
231                 return ERROR_FAIL;
232         }
233
234         connection->priv = telnet_connection;
235
236         /* initialize telnet connection information */
237         telnet_connection->closed = false;
238         telnet_connection->line_size = 0;
239         telnet_connection->line_cursor = 0;
240         telnet_connection->prompt = strdup("> ");
241         telnet_connection->prompt_visible = true;
242         telnet_connection->state = TELNET_STATE_DATA;
243
244         /* output goes through telnet connection */
245         command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
246
247         /* negotiate telnet options */
248         telnet_write(connection, negotiate, strlen(negotiate));
249
250         /* print connection banner */
251         if (telnet_service->banner) {
252                 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
253                 telnet_write(connection, "\r\n", 2);
254         }
255
256         /* the prompt is always placed at the line beginning */
257         telnet_write(connection, "\r", 1);
258         telnet_prompt(connection);
259
260         /* initialize history */
261         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
262                 telnet_connection->history[i] = NULL;
263         telnet_connection->next_history = 0;
264         telnet_connection->current_history = 0;
265         telnet_load_history(telnet_connection);
266
267         log_add_callback(telnet_log_callback, connection);
268
269         return ERROR_OK;
270 }
271
272 static void telnet_clear_line(struct connection *connection,
273         struct telnet_connection *t_con)
274 {
275         /* move to end of line */
276         if (t_con->line_cursor < t_con->line_size)
277                 telnet_write(connection,
278                         t_con->line + t_con->line_cursor,
279                         t_con->line_size - t_con->line_cursor);
280
281         /* backspace, overwrite with space, backspace */
282         while (t_con->line_size > 0) {
283                 telnet_write(connection, "\b \b", 3);
284                 t_con->line_size--;
285         }
286         t_con->line_cursor = 0;
287 }
288
289 static void telnet_history_go(struct connection *connection, int idx)
290 {
291         struct telnet_connection *t_con = connection->priv;
292
293         if (t_con->history[idx]) {
294                 telnet_clear_line(connection, t_con);
295                 t_con->line_size = strlen(t_con->history[idx]);
296                 t_con->line_cursor = t_con->line_size;
297                 memcpy(t_con->line, t_con->history[idx], t_con->line_size);
298                 telnet_write(connection, t_con->line, t_con->line_size);
299                 t_con->current_history = idx;
300         }
301         t_con->state = TELNET_STATE_DATA;
302 }
303
304 static void telnet_history_up(struct connection *connection)
305 {
306         struct telnet_connection *t_con = connection->priv;
307
308         size_t last_history = (t_con->current_history > 0) ?
309                                 t_con->current_history - 1 :
310                                 TELNET_LINE_HISTORY_SIZE-1;
311         telnet_history_go(connection, last_history);
312 }
313
314 static void telnet_history_down(struct connection *connection)
315 {
316         struct telnet_connection *t_con = connection->priv;
317         size_t next_history;
318
319         next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
320         telnet_history_go(connection, next_history);
321 }
322
323 static void telnet_history_add(struct connection *connection)
324 {
325         struct telnet_connection *t_con = connection->priv;
326
327         /* save only non-blank not repeating lines in the history */
328         char *prev_line = t_con->history[(t_con->current_history > 0) ?
329                         t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
330
331         if (*t_con->line && (!prev_line || strcmp(t_con->line, prev_line))) {
332                 /* if the history slot is already taken, free it */
333                 free(t_con->history[t_con->next_history]);
334
335                 /* add line to history */
336                 t_con->history[t_con->next_history] = strdup(t_con->line);
337
338                 /* wrap history at TELNET_LINE_HISTORY_SIZE */
339                 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
340
341                 /* current history line starts at the new entry */
342                 t_con->current_history = t_con->next_history;
343
344                 free(t_con->history[t_con->current_history]);
345                 t_con->history[t_con->current_history] = strdup("");
346         }
347 }
348
349 static int telnet_history_print(struct connection *connection)
350 {
351         struct telnet_connection *tc;
352
353         tc = connection->priv;
354
355         for (size_t i = 1; i < TELNET_LINE_HISTORY_SIZE; i++) {
356                 char *line;
357
358                 /*
359                  * The tc->next_history line contains empty string (unless NULL), thus
360                  * it is not printed.
361                  */
362                 line = tc->history[(tc->next_history + i) % TELNET_LINE_HISTORY_SIZE];
363
364                 if (line) {
365                         telnet_write(connection, line, strlen(line));
366                         telnet_write(connection, "\r\n\x00", 3);
367                 }
368         }
369
370         tc->line_size = 0;
371         tc->line_cursor = 0;
372
373         /* The prompt is always placed at the line beginning. */
374         telnet_write(connection, "\r", 1);
375
376         return telnet_prompt(connection);
377 }
378
379 static void telnet_move_cursor(struct connection *connection, size_t pos)
380 {
381         struct telnet_connection *tc = connection->priv;
382         size_t tmp;
383
384         if (pos == tc->line_cursor) /* nothing to do */
385                 return;
386
387         if (pos > tc->line_size) /* out of bounds */
388                 return;
389
390         if (pos < tc->line_cursor) {
391                 tmp = tc->line_cursor - pos;
392
393                 for (size_t i = 0; i < tmp; i += 16)
394                         telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
395                                 MIN(tmp - i, 16));
396         } else {
397                 tmp = pos - tc->line_cursor;
398
399                 for (size_t i = 0; i < tmp; i += 16)
400                         telnet_write(connection, tc->line + tc->line_cursor + i,
401                                 MIN(tmp - i, 16));
402         }
403
404         tc->line_cursor = pos;
405 }
406
407 /* check buffer size leaving one spare character for string null termination */
408 static inline bool telnet_can_insert(struct connection *connection, size_t len)
409 {
410         struct telnet_connection *t_con = connection->priv;
411
412         return t_con->line_size + len < TELNET_LINE_MAX_SIZE;
413 }
414
415 /* write to telnet console, and update the telnet_connection members
416  * this function is capable of inserting in the middle of a line
417  * please ensure that data does not contain special characters (\n, \r, \t, \b ...)
418  *
419  * returns false when it fails to insert the requested data
420  */
421 static bool telnet_insert(struct connection *connection, const void *data, size_t len)
422 {
423         struct telnet_connection *t_con = connection->priv;
424
425         if (!telnet_can_insert(connection, len)) {
426                 telnet_bell(connection);
427                 return false;
428         }
429
430         if (t_con->line_cursor < t_con->line_size) {
431                 /* we have some content after the cursor */
432                 memmove(t_con->line + t_con->line_cursor + len,
433                                 t_con->line + t_con->line_cursor,
434                                 t_con->line_size - t_con->line_cursor);
435         }
436
437         strncpy(t_con->line + t_con->line_cursor, data, len);
438
439         telnet_write(connection,
440                         t_con->line + t_con->line_cursor,
441                         t_con->line_size + len - t_con->line_cursor);
442
443         t_con->line_size += len;
444         t_con->line_cursor += len;
445
446         for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
447                 telnet_write(connection, "\b", 1);
448
449         return true;
450 }
451
452 static void telnet_delete_character(struct connection *connection)
453 {
454         struct telnet_connection *t_con = connection->priv;
455
456         if (t_con->line_cursor == 0)
457                 return;
458
459         if (t_con->line_cursor != t_con->line_size) {
460                 size_t i;
461                 telnet_write(connection, "\b", 1);
462                 t_con->line_cursor--;
463                 t_con->line_size--;
464                 memmove(t_con->line + t_con->line_cursor,
465                                 t_con->line + t_con->line_cursor + 1,
466                                 t_con->line_size -
467                                 t_con->line_cursor);
468
469                 telnet_write(connection,
470                                 t_con->line + t_con->line_cursor,
471                                 t_con->line_size -
472                                 t_con->line_cursor);
473                 telnet_write(connection, " \b", 2);
474                 for (i = t_con->line_cursor; i < t_con->line_size; i++)
475                         telnet_write(connection, "\b", 1);
476         } else {
477                 t_con->line_size--;
478                 t_con->line_cursor--;
479                 /* back space: move the 'printer' head one char
480                  * back, overwrite with space, move back again */
481                 telnet_write(connection, "\b \b", 3);
482         }
483 }
484
485 static void telnet_remove_character(struct connection *connection)
486 {
487         struct telnet_connection *t_con = connection->priv;
488
489         if (t_con->line_cursor < t_con->line_size) {
490                 size_t i;
491                 t_con->line_size--;
492                 /* remove char from line buffer */
493                 memmove(t_con->line + t_con->line_cursor,
494                                 t_con->line + t_con->line_cursor + 1,
495                                 t_con->line_size - t_con->line_cursor);
496
497                 /* print remainder of buffer */
498                 telnet_write(connection, t_con->line + t_con->line_cursor,
499                                 t_con->line_size - t_con->line_cursor);
500                 /* overwrite last char with whitespace */
501                 telnet_write(connection, " \b", 2);
502
503                 /* move back to cursor position*/
504                 for (i = t_con->line_cursor; i < t_con->line_size; i++)
505                         telnet_write(connection, "\b", 1);
506         }
507 }
508
509 static int telnet_exec_line(struct connection *connection)
510 {
511         struct telnet_connection *t_con = connection->priv;
512         struct command_context *command_context = connection->cmd_ctx;
513         int retval;
514
515         telnet_write(connection, "\r\n\x00", 3);
516
517         if (strcmp(t_con->line, "history") == 0) {
518                 retval = telnet_history_print(connection);
519
520                 if (retval != ERROR_OK)
521                         return retval;
522
523                 return ERROR_OK;
524         }
525
526         telnet_history_add(connection);
527
528         t_con->line_size = 0;
529
530         /* to suppress prompt in log callback during command execution */
531         t_con->prompt_visible = false;
532
533         if (strcmp(t_con->line, "shutdown") == 0)
534                 telnet_save_history(t_con);
535
536         retval = command_run_line(command_context, t_con->line);
537
538         t_con->line_cursor = 0;
539         t_con->prompt_visible = true;
540
541         if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
542                 return ERROR_SERVER_REMOTE_CLOSED;
543
544         /* the prompt is always placed at the line beginning */
545         telnet_write(connection, "\r", 1);
546
547         retval = telnet_prompt(connection);
548         if (retval == ERROR_SERVER_REMOTE_CLOSED)
549                 return ERROR_SERVER_REMOTE_CLOSED;
550
551         return ERROR_OK;
552 }
553
554 static void telnet_cut_line_to_end(struct connection *connection)
555 {
556         struct telnet_connection *t_con = connection->priv;
557
558         /* FIXME: currently this function does not save to clipboard */
559
560         if (t_con->line_cursor < t_con->line_size) {
561                 /* overwrite with space, until end of line, move back */
562                 for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
563                         telnet_write(connection, " ", 1);
564                 for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
565                         telnet_write(connection, "\b", 1);
566                 t_con->line[t_con->line_cursor] = '\0';
567                 t_con->line_size = t_con->line_cursor;
568         }
569 }
570
571 static void telnet_interrupt(struct connection *connection)
572 {
573         struct telnet_connection *t_con = connection->priv;
574
575         /* print '^C' at line end, and display a new command prompt */
576         telnet_move_cursor(connection, t_con->line_size);
577         telnet_write(connection, "^C\n\r", 4);
578         t_con->line_cursor = 0;
579         t_con->line_size = 0;
580         telnet_prompt(connection);
581 }
582
583 static void telnet_auto_complete(struct connection *connection)
584 {
585         struct telnet_connection *t_con = connection->priv;
586         struct command_context *command_context = connection->cmd_ctx;
587
588         struct cmd_match {
589                 char *cmd;
590                 struct list_head lh;
591         };
592
593         LIST_HEAD(matches);
594
595         /* user command sequence, either at line beginning
596          * or we start over after these characters ';', '[', '{' */
597         size_t seq_start = (t_con->line_cursor == 0) ? 0 : (t_con->line_cursor - 1);
598         while (seq_start > 0) {
599                 char c = t_con->line[seq_start];
600                 if (c == ';' || c == '[' || c == '{') {
601                         seq_start++;
602                         break;
603                 }
604
605                 seq_start--;
606         }
607
608         /* user command position in the line, ignore leading spaces */
609         size_t usr_cmd_pos = seq_start;
610         while ((usr_cmd_pos < t_con->line_cursor) && isspace(t_con->line[usr_cmd_pos]))
611                 usr_cmd_pos++;
612
613         /* user command length */
614         size_t usr_cmd_len = t_con->line_cursor - usr_cmd_pos;
615
616         /* optimize multiple spaces in the user command,
617          * because info commands does not tolerate multiple spaces */
618         size_t optimized_spaces = 0;
619         char query[usr_cmd_len + 1];
620         for (size_t i = 0; i < usr_cmd_len; i++) {
621                 if ((i < usr_cmd_len - 1) && isspace(t_con->line[usr_cmd_pos + i])
622                                 && isspace(t_con->line[usr_cmd_pos + i + 1])) {
623                         optimized_spaces++;
624                         continue;
625                 }
626
627                 query[i - optimized_spaces] = t_con->line[usr_cmd_pos + i];
628         }
629
630         usr_cmd_len -= optimized_spaces;
631         query[usr_cmd_len] = '\0';
632
633         /* filter commands */
634         char *query_cmd = alloc_printf("_telnet_autocomplete_helper {%s*}", query);
635
636         if (!query_cmd) {
637                 LOG_ERROR("Out of memory");
638                 return;
639         }
640
641         int retval = Jim_EvalSource(command_context->interp, __FILE__, __LINE__, query_cmd);
642         free(query_cmd);
643         if (retval != JIM_OK)
644                 return;
645
646         Jim_Obj *list = Jim_GetResult(command_context->interp);
647         Jim_IncrRefCount(list);
648
649         /* common prefix length of the matched commands */
650         size_t common_len = 0;
651         char *first_match = NULL; /* used to compute the common prefix length */
652
653         int len = Jim_ListLength(command_context->interp, list);
654         for (int i = 0; i < len; i++) {
655                 Jim_Obj *elem = Jim_ListGetIndex(command_context->interp, list, i);
656                 Jim_IncrRefCount(elem);
657
658                 char *name = (char *)Jim_GetString(elem, NULL);
659
660                 /* validate the command */
661                 bool ignore_cmd = false;
662                 Jim_Cmd *jim_cmd = Jim_GetCommand(command_context->interp, elem, JIM_NONE);
663
664                 if (!jim_cmd) {
665                         /* Why we are here? Let's ignore it! */
666                         ignore_cmd = true;
667                 } else if (jimcmd_is_oocd_command(jim_cmd)) {
668                         struct command *cmd = jimcmd_privdata(jim_cmd);
669
670                         if (cmd && !cmd->handler && !cmd->jim_handler) {
671                                 /* Initial part of a multi-word command. Ignore it! */
672                                 ignore_cmd = true;
673                         } else if (cmd && cmd->mode == COMMAND_CONFIG) {
674                                 /* Not executable after config phase. Ignore it! */
675                                 ignore_cmd = true;
676                         }
677                 }
678
679                 /* save the command in the prediction list */
680                 if (!ignore_cmd) {
681                         struct cmd_match *match = calloc(1, sizeof(struct cmd_match));
682                         if (!match) {
683                                 LOG_ERROR("Out of memory");
684                                 Jim_DecrRefCount(command_context->interp, elem);
685                                 break; /* break the for loop */
686                         }
687
688                         if (list_empty(&matches)) {
689                                 common_len = strlen(name);
690                                 first_match = name;
691                         } else {
692                                 size_t new_common_len = usr_cmd_len; /* save some loops */
693
694                                 while (new_common_len < common_len && first_match[new_common_len] == name[new_common_len])
695                                         new_common_len++;
696
697                                 common_len = new_common_len;
698                         }
699
700                         match->cmd = name;
701                         list_add_tail(&match->lh, &matches);
702                 }
703
704                 Jim_DecrRefCount(command_context->interp, elem);
705         }
706         /* end of command filtering */
707
708         /* proceed with auto-completion */
709         if (list_empty(&matches))
710                 telnet_bell(connection);
711         else if (common_len == usr_cmd_len && list_is_singular(&matches) && t_con->line_cursor == t_con->line_size)
712                 telnet_insert(connection, " ", 1);
713         else if (common_len > usr_cmd_len) {
714                 int completion_size = common_len - usr_cmd_len;
715                 if (telnet_insert(connection, first_match + usr_cmd_len, completion_size)) {
716                         /* in bash this extra space is only added when the cursor in at the end of line */
717                         if (list_is_singular(&matches) && t_con->line_cursor == t_con->line_size)
718                                 telnet_insert(connection, " ", 1);
719                 }
720         } else if (!list_is_singular(&matches)) {
721                 telnet_write(connection, "\n\r", 2);
722
723                 struct cmd_match *match;
724                 list_for_each_entry(match, &matches, lh) {
725                         telnet_write(connection, match->cmd, strlen(match->cmd));
726                         telnet_write(connection, "\n\r", 2);
727                 }
728
729                 telnet_prompt(connection);
730                 telnet_write(connection, t_con->line, t_con->line_size);
731
732                 /* restore the terminal visible cursor location */
733                 for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
734                         telnet_write(connection, "\b", 1);
735         }
736
737         /* destroy the command_list */
738         struct cmd_match *tmp, *match;
739         list_for_each_entry_safe(match, tmp, &matches, lh)
740                 free(match);
741
742         Jim_DecrRefCount(command_context->interp, list);
743 }
744
745 static int telnet_input(struct connection *connection)
746 {
747         int bytes_read;
748         unsigned char buffer[TELNET_BUFFER_SIZE];
749         unsigned char *buf_p;
750         struct telnet_connection *t_con = connection->priv;
751
752         bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
753
754         if (bytes_read == 0)
755                 return ERROR_SERVER_REMOTE_CLOSED;
756         else if (bytes_read == -1) {
757                 LOG_ERROR("error during read: %s", strerror(errno));
758                 return ERROR_SERVER_REMOTE_CLOSED;
759         }
760
761         buf_p = buffer;
762         while (bytes_read) {
763                 switch (t_con->state) {
764                         case TELNET_STATE_DATA:
765                                 if (*buf_p == 0xff) {
766                                         t_con->state = TELNET_STATE_IAC;
767                                 } else {
768                                         if (isprint(*buf_p)) {  /* printable character */
769                                                 telnet_insert(connection, buf_p, 1);
770                                         } else { /* non-printable */
771                                                 if (*buf_p == 0x1b) {   /* escape */
772                                                         t_con->state = TELNET_STATE_ESCAPE;
773                                                         t_con->last_escape = '\x00';
774                                                 } else if ((*buf_p == 0xd) || (*buf_p == 0xa)) { /* CR/LF */
775                                                         int retval;
776
777                                                         /* skip over combinations with CR/LF and NUL characters */
778                                                         if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) ||
779                                                                         (*(buf_p + 1) == 0xd))) {
780                                                                 buf_p++;
781                                                                 bytes_read--;
782                                                         }
783                                                         if ((bytes_read > 1) && (*(buf_p + 1) == 0)) {
784                                                                 buf_p++;
785                                                                 bytes_read--;
786                                                         }
787                                                         t_con->line[t_con->line_size] = 0;
788
789                                                         retval = telnet_exec_line(connection);
790                                                         if (retval != ERROR_OK)
791                                                                 return retval;
792                                                 } else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) {       /* delete character */
793                                                         telnet_delete_character(connection);
794                                                 } else if (*buf_p == 0x15) {    /* clear line */
795                                                         telnet_clear_line(connection, t_con);
796                                                 } else if (*buf_p == CTRL('B')) {       /* cursor left */
797                                                         telnet_move_cursor(connection, t_con->line_cursor - 1);
798                                                         t_con->state = TELNET_STATE_DATA;
799                                                 } else if (*buf_p == CTRL('C')) {       /* interrupt */
800                                                         telnet_interrupt(connection);
801                                                 } else if (*buf_p == CTRL('F')) {       /* cursor right */
802                                                         telnet_move_cursor(connection, t_con->line_cursor + 1);
803                                                         t_con->state = TELNET_STATE_DATA;
804                                                 } else if (*buf_p == CTRL('P')) {       /* cursor up */
805                                                         telnet_history_up(connection);
806                                                 } else if (*buf_p == CTRL('N')) {       /* cursor down */
807                                                         telnet_history_down(connection);
808                                                 } else if (*buf_p == CTRL('A')) {       /* move the cursor to the beginning of the line */
809                                                         telnet_move_cursor(connection, 0);
810                                                 } else if (*buf_p == CTRL('E')) {       /* move the cursor to the end of the line */
811                                                         telnet_move_cursor(connection, t_con->line_size);
812                                                 } else if (*buf_p == CTRL('K')) {       /* kill line to end */
813                                                         telnet_cut_line_to_end(connection);
814                                                 } else if (*buf_p == '\t') {
815                                                         telnet_auto_complete(connection);
816                                                 } else {
817                                                         LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
818                                                 }
819                                         }
820                                 }
821                                 break;
822                         case TELNET_STATE_IAC:
823                                 switch (*buf_p) {
824                                 case 0xfe:
825                                         t_con->state = TELNET_STATE_DONT;
826                                         break;
827                                 case 0xfd:
828                                         t_con->state = TELNET_STATE_DO;
829                                         break;
830                                 case 0xfc:
831                                         t_con->state = TELNET_STATE_WONT;
832                                         break;
833                                 case 0xfb:
834                                         t_con->state = TELNET_STATE_WILL;
835                                         break;
836                                 }
837                                 break;
838                         case TELNET_STATE_SB:
839                                 break;
840                         case TELNET_STATE_SE:
841                                 break;
842                         case TELNET_STATE_WILL:
843                         case TELNET_STATE_WONT:
844                         case TELNET_STATE_DO:
845                         case TELNET_STATE_DONT:
846                                 t_con->state = TELNET_STATE_DATA;
847                                 break;
848                         case TELNET_STATE_ESCAPE:
849                                 if (t_con->last_escape == '[') {
850                                         if (*buf_p == 'D') {    /* cursor left */
851                                                 telnet_move_cursor(connection, t_con->line_cursor - 1);
852                                                 t_con->state = TELNET_STATE_DATA;
853                                         } else if (*buf_p == 'C') {     /* cursor right */
854                                                 telnet_move_cursor(connection, t_con->line_cursor + 1);
855                                                 t_con->state = TELNET_STATE_DATA;
856                                         } else if (*buf_p == 'A') {     /* cursor up */
857                                                 telnet_history_up(connection);
858                                         } else if (*buf_p == 'B') {     /* cursor down */
859                                                 telnet_history_down(connection);
860                                         } else if (*buf_p == 'F') { /* end key */
861                                                 telnet_move_cursor(connection, t_con->line_size);
862                                                 t_con->state = TELNET_STATE_DATA;
863                                         } else if (*buf_p == 'H') { /* home key */
864                                                 telnet_move_cursor(connection, 0);
865                                                 t_con->state = TELNET_STATE_DATA;
866                                         } else if (*buf_p == '3') {
867                                                 t_con->last_escape = *buf_p;
868                                         } else {
869                                                 t_con->state = TELNET_STATE_DATA;
870                                         }
871                                 } else if (t_con->last_escape == '3') {
872                                         /* Remove character */
873                                         if (*buf_p == '~') {
874                                                 telnet_remove_character(connection);
875                                                 t_con->state = TELNET_STATE_DATA;
876                                         } else
877                                                 t_con->state = TELNET_STATE_DATA;
878                                 } else if (t_con->last_escape == '\x00') {
879                                         if (*buf_p == '[')
880                                                 t_con->last_escape = *buf_p;
881                                         else
882                                                 t_con->state = TELNET_STATE_DATA;
883                                 } else {
884                                         LOG_ERROR("BUG: unexpected value in t_con->last_escape");
885                                         t_con->state = TELNET_STATE_DATA;
886                                 }
887
888                                 break;
889                         default:
890                                 LOG_ERROR("unknown telnet state");
891                                 return ERROR_FAIL;
892                 }
893
894                 bytes_read--;
895                 buf_p++;
896         }
897
898         return ERROR_OK;
899 }
900
901 static int telnet_connection_closed(struct connection *connection)
902 {
903         struct telnet_connection *t_con = connection->priv;
904         int i;
905
906         log_remove_callback(telnet_log_callback, connection);
907
908         free(t_con->prompt);
909         t_con->prompt = NULL;
910
911         /* save telnet history */
912         telnet_save_history(t_con);
913
914         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++) {
915                 free(t_con->history[i]);
916                 t_con->history[i] = NULL;
917         }
918
919         /* if this connection registered a debug-message receiver delete it */
920         delete_debug_msg_receiver(connection->cmd_ctx, NULL);
921
922         free(connection->priv);
923         connection->priv = NULL;
924
925         return ERROR_OK;
926 }
927
928 int telnet_init(char *banner)
929 {
930         if (strcmp(telnet_port, "disabled") == 0) {
931                 LOG_INFO("telnet server disabled");
932                 return ERROR_OK;
933         }
934
935         struct telnet_service *telnet_service =
936                 malloc(sizeof(struct telnet_service));
937
938         if (!telnet_service) {
939                 LOG_ERROR("Failed to allocate telnet service.");
940                 return ERROR_FAIL;
941         }
942
943         telnet_service->banner = banner;
944
945         int ret = add_service("telnet", telnet_port, CONNECTION_LIMIT_UNLIMITED,
946                 telnet_new_connection, telnet_input, telnet_connection_closed,
947                 telnet_service);
948
949         if (ret != ERROR_OK) {
950                 free(telnet_service);
951                 return ret;
952         }
953
954         return ERROR_OK;
955 }
956
957 /* daemon configuration command telnet_port */
958 COMMAND_HANDLER(handle_telnet_port_command)
959 {
960         return CALL_COMMAND_HANDLER(server_pipe_command, &telnet_port);
961 }
962
963 COMMAND_HANDLER(handle_exit_command)
964 {
965         return ERROR_COMMAND_CLOSE_CONNECTION;
966 }
967
968 static const struct command_registration telnet_command_handlers[] = {
969         {
970                 .name = "exit",
971                 .handler = handle_exit_command,
972                 .mode = COMMAND_EXEC,
973                 .usage = "",
974                 .help = "exit telnet session",
975         },
976         {
977                 .name = "telnet_port",
978                 .handler = handle_telnet_port_command,
979                 .mode = COMMAND_CONFIG,
980                 .help = "Specify port on which to listen "
981                         "for incoming telnet connections.  "
982                         "Read help on 'gdb_port'.",
983                 .usage = "[port_num]",
984         },
985         COMMAND_REGISTRATION_DONE
986 };
987
988 int telnet_register_commands(struct command_context *cmd_ctx)
989 {
990         telnet_port = strdup("4444");
991         return register_commands(cmd_ctx, NULL, telnet_command_handlers);
992 }
993
994 void telnet_service_free(void)
995 {
996         free(telnet_port);
997 }