- fixed build issues with win32
[openocd.git] / src / server / tcl_server.c
1 /***************************************************************************
2 * Copyright (C) 2008 *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdarg.h>
25 #include "tcl_server.h"
26
27 #include "../jim.h"
28 #include "log.h"
29 #include "command.h"
30
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <ctype.h>
36
37 #define TCL_SERVER_VERSION "TCL Server 0.1"
38 #define TCL_MAX_LINE (4096)
39
40 typedef struct tcl_connection_s {
41 int tc_linedrop;
42 int tc_lineoffset;
43 char tc_line[TCL_MAX_LINE];
44
45 int tc_outerror; /* flag an output error */
46 } tcl_connection_t;
47
48 extern Jim_Interp *interp;
49 static unsigned short tcl_port = 0;
50
51 /* commands */
52 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53
54 /* handlers */
55 static int tcl_new_connection(connection_t *connection);
56 static int tcl_input(connection_t *connection);
57 static int tcl_output(connection_t *connection, const void *buf, ssize_t len);
58 static int tcl_closed(connection_t *connection);
59
60 /* write data out to a socket.
61 *
62 * this is a blocking write, so the return value must equal the length, if
63 * that is not the case then flag the connection with an output error.
64 */
65 int tcl_output(connection_t *connection, const void *data, ssize_t len)
66 {
67 ssize_t wlen;
68 tcl_connection_t *tclc;
69
70 tclc = connection->priv;
71 if (tclc->tc_outerror)
72 return ERROR_SERVER_REMOTE_CLOSED;
73
74 wlen = write_socket(connection->fd, data, len);
75 if (wlen == len)
76 return ERROR_OK;
77
78 LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
79 tclc->tc_outerror = 1;
80 return ERROR_SERVER_REMOTE_CLOSED;
81 }
82
83
84 /* connections */
85 static int tcl_new_connection(connection_t *connection)
86 {
87 tcl_connection_t *tclc;
88
89 tclc = malloc(sizeof(tcl_connection_t));
90 if (tclc == NULL)
91 return ERROR_CONNECTION_REJECTED;
92
93 memset(tclc, 0, sizeof(tcl_connection_t));
94 connection->priv = tclc;
95 return ERROR_OK;
96 }
97
98 static int tcl_input(connection_t *connection)
99 {
100 int retval;
101 int i;
102 ssize_t rlen;
103 const char *result;
104 int reslen;
105 tcl_connection_t *tclc;
106 char in[256];
107
108 rlen = read_socket(connection->fd, &in, sizeof(in));
109 if (rlen <= 0) {
110 if (rlen < 0)
111 LOG_ERROR("error during read: %s", strerror(errno));
112 return ERROR_SERVER_REMOTE_CLOSED;
113 }
114
115 tclc = connection->priv;
116 if (tclc == NULL)
117 return ERROR_CONNECTION_REJECTED;
118
119 /* push as much data into the line as possible */
120 for (i = 0; i < rlen; i++)
121 {
122 if (!isprint(in[i]) && !isspace(in[i]))
123 {
124 /* drop this line */
125 tclc->tc_linedrop = 1;
126 continue;
127 }
128
129 /* buffer the data */
130 tclc->tc_line[tclc->tc_lineoffset] = in[i];
131 if (tclc->tc_lineoffset < TCL_MAX_LINE)
132 tclc->tc_lineoffset++;
133 else
134 tclc->tc_linedrop = 1;
135
136 if (in[i] != '\n')
137 continue;
138
139 /* process the line */
140 if (tclc->tc_linedrop) {
141 #define ESTR "line too long\n"
142 retval = tcl_output(connection, ESTR, sizeof(ESTR));
143 if (retval != ERROR_OK)
144 return retval;
145 #undef ESTR
146 } else {
147 tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
148 retval = Jim_Eval(interp, tclc->tc_line);
149 result = Jim_GetString(Jim_GetResult(interp), &reslen);
150 retval = tcl_output(connection, result, reslen);
151 if (retval != ERROR_OK)
152 return retval;
153 if (memchr(result, '\n', reslen) == NULL)
154 tcl_output(connection, "\n", 1);
155 }
156 tclc->tc_lineoffset = 0;
157 tclc->tc_linedrop = 0;
158 }
159
160 return ERROR_OK;
161 }
162
163 static int tcl_closed(connection_t *connection)
164 {
165 /* cleanup connection context */
166 if (connection->priv) {
167 free(connection->priv);
168 connection->priv = NULL;
169 }
170 return ERROR_OK;
171 }
172
173 int tcl_init(void)
174 {
175 int retval;
176
177 if (tcl_port == 0)
178 {
179 LOG_WARNING("no tcl port specified, using default port 6666");
180 tcl_port = 6666;
181 }
182
183 retval = add_service("tcl", CONNECTION_TCL, tcl_port, 1, tcl_new_connection, tcl_input, tcl_closed, NULL);
184 return retval;
185 }
186
187
188 int tcl_register_commands(command_context_t *cmd_ctx)
189 {
190 register_command(cmd_ctx, NULL, "tcl_port", handle_tcl_port_command, COMMAND_CONFIG, "");
191 return ERROR_OK;
192 }
193
194 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
195 {
196 if (argc == 1) {
197 tcl_port = strtoul(args[0], NULL, 0);
198 }
199 return ERROR_OK;
200 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)