830cf6410caa953b0316aa1b344a3f546cee6770
[fw/openocd] / src / server / telnet_server.c
1 /***************************************************************************\r
2  *   Copyright (C) 2005 by Dominic Rath                                    *\r
3  *   Dominic.Rath@gmx.de                                                   *\r
4  *                                                                         *\r
5  *   This program is free software; you can redistribute it and/or modify  *\r
6  *   it under the terms of the GNU General Public License as published by  *\r
7  *   the Free Software Foundation; either version 2 of the License, or     *\r
8  *   (at your option) any later version.                                   *\r
9  *                                                                         *\r
10  *   This program is distributed in the hope that it will be useful,       *\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\r
13  *   GNU General Public License for more details.                          *\r
14  *                                                                         *\r
15  *   You should have received a copy of the GNU General Public License     *\r
16  *   along with this program; if not, write to the                         *\r
17  *   Free Software Foundation, Inc.,                                       *\r
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *\r
19  ***************************************************************************/\r
20 #ifdef HAVE_CONFIG_H\r
21 #include "config.h"\r
22 #endif\r
23 \r
24 #include "replacements.h"\r
25 \r
26 #include "telnet_server.h"\r
27 \r
28 #include "server.h"\r
29 #include "log.h"\r
30 #include "command.h"\r
31 #include "target.h"\r
32 #include "target_request.h"\r
33 \r
34 #include <stdlib.h>\r
35 #include <unistd.h>\r
36 #include <errno.h>\r
37 #include <string.h>\r
38 #include <ctype.h>\r
39 \r
40 static unsigned short telnet_port = 0;\r
41 \r
42 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
43 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
44 \r
45 static char *negotiate =\r
46                 "\xFF\xFB\x03"          /* IAC WILL Suppress Go Ahead */\r
47                 "\xFF\xFB\x01"          /* IAC WILL Echo */\r
48                 "\xFF\xFD\x03"          /* IAC DO Suppress Go Ahead */\r
49                 "\xFF\xFE\x01";         /* IAC DON'T Echo */\r
50                 \r
51 #define CTRL(c) (c - '@')\r
52         \r
53 /* The only way we can detect that the socket is closed is the first time\r
54  * we write to it, we will fail. Subsequent write operations will\r
55  * succeed. Shudder!\r
56  */\r
57 int telnet_write(connection_t *connection, void *data, int len)\r
58 {\r
59         telnet_connection_t *t_con = connection->priv;\r
60         if (t_con->closed)\r
61                 return ERROR_SERVER_REMOTE_CLOSED;\r
62 \r
63         if (write_socket(connection->fd, data, len) == len)\r
64         {\r
65                 return ERROR_OK;\r
66         }\r
67         t_con->closed = 1;\r
68         return ERROR_SERVER_REMOTE_CLOSED;\r
69 }\r
70 \r
71 int telnet_prompt(connection_t *connection)\r
72 {\r
73         telnet_connection_t *t_con = connection->priv;\r
74 \r
75         return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));\r
76 }\r
77 \r
78 int telnet_outputline(connection_t *connection, char* line)\r
79 {\r
80         telnet_write(connection, line, strlen(line));\r
81         return telnet_write(connection, "\r\n\0", 3);\r
82 }\r
83 \r
84 int telnet_output(struct command_context_s *cmd_ctx, char* line)\r
85 {\r
86         connection_t *connection = cmd_ctx->output_handler_priv;\r
87         \r
88         return telnet_outputline(connection, line);\r
89 }\r
90 \r
91 void telnet_log_callback(void *priv, const char *file, int line, \r
92                 const char *function, const char *format, va_list args)\r
93 {\r
94         connection_t *connection = priv;\r
95         char *t = alloc_printf(format, args);\r
96         char *t2;\r
97         if (t == NULL)\r
98                 return;\r
99         t2=t;\r
100         char *endline;\r
101         do \r
102         {\r
103                 if ((endline=strchr(t2, '\n'))!=NULL)\r
104                 {\r
105                         *endline=0;\r
106                 }\r
107                 telnet_outputline(connection, t2);\r
108                 t2=endline+1;\r
109         } while (endline);\r
110         \r
111         free(t);\r
112 }\r
113 \r
114 int telnet_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)\r
115 {\r
116         struct command_context_s *cmd_ctx = priv;\r
117         connection_t *connection = cmd_ctx->output_handler_priv;\r
118         telnet_connection_t *t_con = connection->priv;\r
119         \r
120         switch (event)\r
121         {\r
122                 case TARGET_EVENT_HALTED:\r
123                         target_arch_state(target);\r
124                         if (!t_con->suppress_prompt)\r
125                                 telnet_prompt(connection);\r
126                         break;\r
127                 case TARGET_EVENT_RESUMED:\r
128                         if (!t_con->suppress_prompt)\r
129                                 telnet_prompt(connection);\r
130                         break;\r
131                 default:\r
132                         break;\r
133         }\r
134 \r
135         return ERROR_OK;\r
136 }\r
137 \r
138 int telnet_new_connection(connection_t *connection)\r
139 {\r
140         telnet_connection_t *telnet_connection = malloc(sizeof(telnet_connection_t));\r
141         telnet_service_t *telnet_service = connection->service->priv;\r
142         int i;\r
143         \r
144         connection->priv = telnet_connection;\r
145         \r
146         /* initialize telnet connection information */\r
147         telnet_connection->closed = 0;\r
148         telnet_connection->line_size = 0;\r
149         telnet_connection->line_cursor = 0;\r
150         telnet_connection->option_size = 0;\r
151         telnet_connection->prompt = strdup("> ");\r
152         telnet_connection->suppress_prompt = 0;\r
153         telnet_connection->state = TELNET_STATE_DATA;\r
154         \r
155         /* output goes through telnet connection */\r
156         command_set_output_handler(connection->cmd_ctx, telnet_output, connection);\r
157         \r
158         /* negotiate telnet options */\r
159         telnet_write(connection, negotiate, strlen(negotiate));\r
160         \r
161         /* print connection banner */\r
162         if (telnet_service->banner)\r
163         {\r
164                 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));\r
165                 telnet_write(connection, "\r\n\0", 3);\r
166         }\r
167         \r
168         telnet_prompt(connection);\r
169         \r
170         /* initialize history */\r
171         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)\r
172         {\r
173                 telnet_connection->history[i] = NULL;\r
174         }\r
175         telnet_connection->next_history = 0;\r
176         telnet_connection->current_history = 0;\r
177 \r
178         target_register_event_callback(telnet_target_callback_event_handler, connection->cmd_ctx);\r
179         \r
180         return ERROR_OK;\r
181 }\r
182 \r
183 void telnet_clear_line(connection_t *connection, telnet_connection_t *t_con)\r
184 {\r
185         /* move to end of line */\r
186         if (t_con->line_cursor < t_con->line_size)\r
187         {\r
188                 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);\r
189         }\r
190                                                         \r
191         /* backspace, overwrite with space, backspace */\r
192         while (t_con->line_size > 0)\r
193         {\r
194                 telnet_write(connection, "\b \b", 3);\r
195                 t_con->line_size--;\r
196         }\r
197         t_con->line_cursor = 0;\r
198 }\r
199 \r
200 int telnet_input(connection_t *connection)\r
201 {\r
202         int bytes_read;\r
203         char buffer[TELNET_BUFFER_SIZE];\r
204         char *buf_p;\r
205         telnet_connection_t *t_con = connection->priv;\r
206         command_context_t *command_context = connection->cmd_ctx;\r
207         \r
208         bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE);\r
209         \r
210         if (bytes_read == 0)\r
211                 return ERROR_SERVER_REMOTE_CLOSED;\r
212         else if (bytes_read == -1)\r
213         {\r
214                 ERROR("error during read: %s", strerror(errno));\r
215                 return ERROR_SERVER_REMOTE_CLOSED;\r
216         }\r
217         \r
218         buf_p = buffer;\r
219         while (bytes_read)\r
220         {\r
221                 switch (t_con->state)\r
222                 {\r
223                         case TELNET_STATE_DATA:\r
224                                 if (*buf_p == '\xff')\r
225                                 {\r
226                                         t_con->state = TELNET_STATE_IAC;\r
227                                 }\r
228                                 else\r
229                                 {\r
230                                         if (isprint(*buf_p)) /* printable character */\r
231                                         {\r
232                                                 telnet_write(connection, buf_p, 1);\r
233                                                 if (t_con->line_cursor == t_con->line_size)\r
234                                                 {\r
235                                                         t_con->line[t_con->line_size++] = *buf_p;\r
236                                                         t_con->line_cursor++;\r
237                                                 }\r
238                                                 else\r
239                                                 {\r
240                                                         int i;\r
241                                                         memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);\r
242                                                         t_con->line[t_con->line_cursor++] = *buf_p;\r
243                                                         t_con->line_size++;\r
244                                                         telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);\r
245                                                         for (i = t_con->line_cursor; i < t_con->line_size; i++)\r
246                                                         {\r
247                                                                 telnet_write(connection, "\b", 1);\r
248                                                         }\r
249                                                 }\r
250                                         }\r
251                                         else /* non-printable */\r
252                                         {\r
253                                                 if (*buf_p == 0x1b) /* escape */\r
254                                                 {\r
255                                                         t_con->state = TELNET_STATE_ESCAPE;\r
256                                                         t_con->last_escape = '\x00';\r
257                                                 }\r
258                                                 else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */\r
259                                                 {\r
260                                                         int retval;\r
261                                                         \r
262                                                         /* skip over combinations with CR/LF + NUL */\r
263                                                         if (((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)) && (bytes_read > 1))\r
264                                                         {\r
265                                                                 buf_p++;\r
266                                                                 bytes_read--;\r
267                                                         }\r
268                                                         if ((*(buf_p + 1) == 0) && (bytes_read > 1))\r
269                                                         {\r
270                                                                 buf_p++;\r
271                                                                 bytes_read--;\r
272                                                         }\r
273                                                         t_con->line[t_con->line_size] = 0;\r
274                                                         \r
275                                                         telnet_write(connection, "\r\n\x00", 3);\r
276                                                         \r
277                                                         if (strcmp(t_con->line, "history") == 0)\r
278                                                         {\r
279                                                                 int i;\r
280                                                                 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)\r
281                                                                 {\r
282                                                                         if (t_con->history[i])\r
283                                                                         {\r
284                                                                                 telnet_write(connection, t_con->history[i], strlen(t_con->history[i]));\r
285                                                                                 telnet_write(connection, "\r\n\x00", 3);\r
286                                                                         }\r
287                                                                 }\r
288                                                                 telnet_prompt(connection);\r
289                                                                 t_con->line_size = 0;\r
290                                                                 t_con->line_cursor = 0;\r
291                                                                 continue;\r
292                                                         }\r
293                                                         \r
294                                                         log_add_callback(telnet_log_callback, connection);\r
295                                                         t_con->suppress_prompt = 1;\r
296 \r
297                                                         retval = command_run_line(command_context, t_con->line);\r
298                                                         \r
299                                                         log_remove_callback(telnet_log_callback, connection);\r
300                                                         t_con->suppress_prompt = 0;\r
301 \r
302                                                         if (retval == ERROR_COMMAND_CLOSE_CONNECTION)\r
303                                                         {\r
304                                                                 return ERROR_SERVER_REMOTE_CLOSED;\r
305                                                         }\r
306                                                         \r
307                                                         /* Save only non-blank lines in the history */\r
308                                                         if (t_con->line_size > 0)\r
309                                                         {\r
310                                                                 /* if the history slot is already taken, free it */\r
311                                                                 if (t_con->history[t_con->next_history])\r
312                                                                 {\r
313                                                                         free(t_con->history[t_con->next_history]);\r
314                                                                 }\r
315                 \r
316                                                                 /* add line to history */\r
317                                                                 t_con->history[t_con->next_history] = strdup(t_con->line);\r
318 \r
319                                                                 /* wrap history at TELNET_LINE_HISTORY_SIZE */\r
320                                                                 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;\r
321                                                         \r
322                                                                 /* current history line starts at the new entry */\r
323                                                                 t_con->current_history = t_con->next_history;\r
324                                                         \r
325                                                                 if (t_con->history[t_con->current_history])\r
326                                                                 {\r
327                                                                         free(t_con->history[t_con->current_history]);\r
328                                                                 }\r
329                                                                 t_con->history[t_con->current_history] = strdup("");\r
330                                                         }\r
331                                                         \r
332                                                         int t = telnet_prompt(connection);\r
333                                                         if (t == ERROR_SERVER_REMOTE_CLOSED)\r
334                                                                 return t;\r
335                                                         \r
336                                                         t_con->line_size = 0;\r
337                                                         t_con->line_cursor = 0;\r
338                                                 }\r
339                                                 else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */\r
340                                                 {\r
341                                                         if (t_con->line_cursor > 0)\r
342                                                         {\r
343                                                                 if (t_con->line_cursor != t_con->line_size)\r
344                                                                 {\r
345                                                                         int i;\r
346                                                                         telnet_write(connection, "\b", 1);\r
347                                                                         t_con->line_cursor--;\r
348                                                                         t_con->line_size--;\r
349                                                                         memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);\r
350                                                                         \r
351                                                                         telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);\r
352                                                                         telnet_write(connection, " \b", 2);\r
353                                                                         for (i = t_con->line_cursor; i < t_con->line_size; i++)\r
354                                                                         {\r
355                                                                                 telnet_write(connection, "\b", 1);\r
356                                                                         }\r
357                                                                 }\r
358                                                                 else\r
359                                                                 {\r
360                                                                         t_con->line_size--;\r
361                                                                         t_con->line_cursor--;\r
362                                                                         /* back space: move the 'printer' head one char back, overwrite with space, move back again */\r
363                                                                         telnet_write(connection, "\b \b", 3);\r
364                                                                 }\r
365                                                         }\r
366                                                 }\r
367                                                 else if (*buf_p == 0x15) /* clear line */\r
368                                                 {\r
369                                                         telnet_clear_line(connection, t_con);\r
370                                                 }\r
371                                                 else if (*buf_p == CTRL('B')) /* cursor left */\r
372                                                 {\r
373                                                         if (t_con->line_cursor > 0)\r
374                                                         {\r
375                                                                 telnet_write(connection, "\b", 1);\r
376                                                                 t_con->line_cursor--;\r
377                                                         }\r
378                                                         t_con->state = TELNET_STATE_DATA;\r
379                                                 }\r
380                                                 else if (*buf_p == CTRL('F')) /* cursor right */\r
381                                                 {\r
382                                                         if (t_con->line_cursor < t_con->line_size)\r
383                                                         {\r
384                                                                 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);\r
385                                                         }\r
386                                                         t_con->state = TELNET_STATE_DATA;\r
387                                                 }\r
388                                                 else\r
389                                                 {\r
390                                                         DEBUG("unhandled nonprintable: %2.2x", *buf_p);\r
391                                                 }\r
392                                         }\r
393                                 }\r
394                                 break;\r
395                         case TELNET_STATE_IAC:\r
396                                 switch (*buf_p)\r
397                                 {\r
398                                         case '\xfe':\r
399                                                 t_con->state = TELNET_STATE_DONT;\r
400                                                 break;\r
401                                         case '\xfd':\r
402                                                 t_con->state = TELNET_STATE_DO;\r
403                                                 break;\r
404                                         case '\xfc':\r
405                                                 t_con->state = TELNET_STATE_WONT;\r
406                                                 break;\r
407                                         case '\xfb':\r
408                                                 t_con->state = TELNET_STATE_WILL;\r
409                                                 break;\r
410                                 }\r
411                                 break;\r
412                         case TELNET_STATE_SB:\r
413                                 break;\r
414                         case TELNET_STATE_SE:\r
415                                 break;\r
416                         case TELNET_STATE_WILL:\r
417                         case TELNET_STATE_WONT:\r
418                         case TELNET_STATE_DO:\r
419                         case TELNET_STATE_DONT:\r
420                                 t_con->state = TELNET_STATE_DATA;\r
421                                 break;\r
422                         case TELNET_STATE_ESCAPE:\r
423                                 if (t_con->last_escape == '[')\r
424                                 {\r
425                                         if (*buf_p == 'D') /* cursor left */\r
426                                         {\r
427                                                 if (t_con->line_cursor > 0)\r
428                                                 {\r
429                                                         telnet_write(connection, "\b", 1);\r
430                                                         t_con->line_cursor--;\r
431                                                 }\r
432                                                 t_con->state = TELNET_STATE_DATA;\r
433                                         }\r
434                                         else if (*buf_p == 'C') /* cursor right */\r
435                                         {\r
436                                                 if (t_con->line_cursor < t_con->line_size)\r
437                                                 {\r
438                                                         telnet_write(connection, t_con->line + t_con->line_cursor++, 1);\r
439                                                 }\r
440                                                 t_con->state = TELNET_STATE_DATA;\r
441                                         }\r
442                                         else if (*buf_p == 'A') /* cursor up */\r
443                                         {\r
444                                                 int last_history = (t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1;\r
445                                                 if (t_con->history[last_history])\r
446                                                 {\r
447                                                         telnet_clear_line(connection, t_con);\r
448                                                         t_con->line_size = strlen(t_con->history[last_history]);\r
449                                                         t_con->line_cursor = t_con->line_size;\r
450                                                         memcpy(t_con->line, t_con->history[last_history], t_con->line_size + 1);\r
451                                                         telnet_write(connection, t_con->line, t_con->line_size);\r
452                                                         t_con->current_history = last_history;\r
453                                                 }\r
454                                                 t_con->state = TELNET_STATE_DATA;\r
455                                         }\r
456                                         else if (*buf_p == 'B') /* cursor down */\r
457                                         {\r
458                                                 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;\r
459                                                 if (t_con->history[next_history])\r
460                                                 {\r
461                                                         telnet_clear_line(connection, t_con);\r
462                                                         t_con->line_size = strlen(t_con->history[next_history]);\r
463                                                         t_con->line_cursor = t_con->line_size;\r
464                                                         memcpy(t_con->line, t_con->history[next_history], t_con->line_size + 1);\r
465                                                         telnet_write(connection, t_con->line, t_con->line_size);\r
466                                                         t_con->current_history = next_history;\r
467                                                 }\r
468                                                 t_con->state = TELNET_STATE_DATA;\r
469                                         }\r
470                                         else if (*buf_p == '3')\r
471                                         {\r
472                                                 t_con->last_escape = *buf_p;\r
473                                         }\r
474                                         else\r
475                                         {\r
476                                                 t_con->state = TELNET_STATE_DATA;\r
477                                         }\r
478                                 }\r
479                                 else if (t_con->last_escape == '3')\r
480                                 {\r
481                                         /* Remove character */\r
482                                         if (*buf_p == '~')\r
483                                         {\r
484                                                 if (t_con->line_cursor < t_con->line_size)\r
485                                                 {\r
486                                                         int i;\r
487                                                         t_con->line_size--;\r
488                                                         /* remove char from line buffer */\r
489                                                         memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);\r
490                                                         \r
491                                                         /* print remainder of buffer */\r
492                                                         telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);\r
493                                                         /* overwrite last char with whitespace */\r
494                                                         telnet_write(connection, " \b", 2);\r
495                                                         \r
496                                                         /* move back to cursor position*/\r
497                                                         for (i = t_con->line_cursor; i < t_con->line_size; i++)\r
498                                                         {\r
499                                                                 telnet_write(connection, "\b", 1);\r
500                                                         }\r
501                                                 }\r
502                                                         \r
503                                                 t_con->state = TELNET_STATE_DATA;\r
504                                         }\r
505                                         else\r
506                                         {\r
507                                                 t_con->state = TELNET_STATE_DATA;\r
508                                         }\r
509                                 }\r
510                                 else if (t_con->last_escape == '\x00')\r
511                                 {\r
512                                         if (*buf_p == '[')\r
513                                         {\r
514                                                 t_con->last_escape = *buf_p;\r
515                                         }\r
516                                         else\r
517                                         {\r
518                                                 t_con->state = TELNET_STATE_DATA;\r
519                                         }\r
520                                 }\r
521                                 else\r
522                                 {\r
523                                         ERROR("BUG: unexpected value in t_con->last_escape");\r
524                                         t_con->state = TELNET_STATE_DATA;\r
525                                 }\r
526                                 \r
527                                 break;\r
528                         default:\r
529                                 ERROR("unknown telnet state");\r
530                                 exit(-1);\r
531                 }\r
532 \r
533                 bytes_read--;\r
534                 buf_p++;\r
535         }\r
536         \r
537         return ERROR_OK;\r
538 }\r
539 \r
540 int telnet_connection_closed(connection_t *connection)\r
541 {\r
542         telnet_connection_t *t_con = connection->priv;\r
543         int i;\r
544         \r
545         if (t_con->prompt)\r
546         {\r
547                 free(t_con->prompt);\r
548                 t_con->prompt = NULL;\r
549         }\r
550         \r
551         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)\r
552         {\r
553                 if (t_con->history[i])\r
554                 {\r
555                         free(t_con->history[i]);\r
556                         t_con->history[i] = NULL;\r
557                 }\r
558         }\r
559         \r
560         /* if this connection registered a debug-message receiver delete it */\r
561         delete_debug_msg_receiver(connection->cmd_ctx, NULL);\r
562         \r
563         if (connection->priv)\r
564         {\r
565                 free(connection->priv);\r
566                 connection->priv = NULL;\r
567         }\r
568         else\r
569         {\r
570                 ERROR("BUG: connection->priv == NULL");\r
571         }\r
572         \r
573         target_unregister_event_callback(telnet_target_callback_event_handler, connection->cmd_ctx);\r
574 \r
575         return ERROR_OK;\r
576 }\r
577 \r
578 int telnet_set_prompt(connection_t *connection, char *prompt)\r
579 {\r
580         telnet_connection_t *t_con = connection->priv;\r
581 \r
582         t_con->prompt = strdup(prompt);\r
583         \r
584         return ERROR_OK;\r
585 }\r
586 \r
587 int telnet_init(char *banner)\r
588 {\r
589         telnet_service_t *telnet_service = malloc(sizeof(telnet_service_t));\r
590         \r
591         if (telnet_port == 0)\r
592         {\r
593                 WARNING("no telnet port specified, using default port 4444");\r
594                 telnet_port = 4444;\r
595         }\r
596         \r
597         telnet_service->banner = banner;\r
598         \r
599         add_service("telnet", CONNECTION_TELNET, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);\r
600         \r
601         return ERROR_OK;\r
602 }\r
603 \r
604 int telnet_register_commands(command_context_t *command_context)\r
605 {\r
606         register_command(command_context, NULL, "exit", handle_exit_command,\r
607                                          COMMAND_EXEC, "exit telnet session");\r
608         \r
609         register_command(command_context, NULL, "telnet_port", handle_telnet_port_command,\r
610                                          COMMAND_CONFIG, "");\r
611         \r
612         return ERROR_OK;\r
613 }\r
614 \r
615 /* daemon configuration command telnet_port */\r
616 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
617 {\r
618         if (argc == 0)\r
619                 return ERROR_OK;\r
620 \r
621         /* only if the port wasn't overwritten by cmdline */\r
622         if (telnet_port == 0)\r
623                 telnet_port = strtoul(args[0], NULL, 0);\r
624 \r
625         return ERROR_OK;\r
626 }\r
627 \r
628 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
629 {\r
630         return ERROR_COMMAND_CLOSE_CONNECTION;\r
631 }\r