30d55cbc348304e94ae03358f3af676d3d177153
[openocd.git] / src / server / tcl_server.c
1 /***************************************************************************
2 * Copyright (C) 2010 √ėyvind Harboe *
3 * oyvind.harboe@zylin.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "tcl_server.h"
26
27 #define TCL_SERVER_VERSION "TCL Server 0.1"
28 #define TCL_MAX_LINE (4096)
29
30 struct tcl_connection {
31 int tc_linedrop;
32 int tc_lineoffset;
33 char tc_line[TCL_MAX_LINE];
34 int tc_outerror;/* flag an output error */
35 };
36
37 static const char *tcl_port;
38
39 /* handlers */
40 static int tcl_new_connection(struct connection *connection);
41 static int tcl_input(struct connection *connection);
42 static int tcl_output(struct connection *connection, const void *buf, ssize_t len);
43 static int tcl_closed(struct connection *connection);
44
45 /* write data out to a socket.
46 *
47 * this is a blocking write, so the return value must equal the length, if
48 * that is not the case then flag the connection with an output error.
49 */
50 int tcl_output(struct connection *connection, const void *data, ssize_t len)
51 {
52 ssize_t wlen;
53 struct tcl_connection *tclc;
54
55 tclc = connection->priv;
56 if (tclc->tc_outerror)
57 return ERROR_SERVER_REMOTE_CLOSED;
58
59 wlen = connection_write(connection, data, len);
60
61 if (wlen == len)
62 return ERROR_OK;
63
64 LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
65 tclc->tc_outerror = 1;
66 return ERROR_SERVER_REMOTE_CLOSED;
67 }
68
69 /* connections */
70 static int tcl_new_connection(struct connection *connection)
71 {
72 struct tcl_connection *tclc;
73
74 tclc = malloc(sizeof(struct tcl_connection));
75 if (tclc == NULL)
76 return ERROR_CONNECTION_REJECTED;
77
78 memset(tclc, 0, sizeof(struct tcl_connection));
79 connection->priv = tclc;
80 return ERROR_OK;
81 }
82
83 static int tcl_input(struct connection *connection)
84 {
85 Jim_Interp *interp = (Jim_Interp *)connection->cmd_ctx->interp;
86 int retval;
87 int i;
88 ssize_t rlen;
89 const char *result;
90 int reslen;
91 struct tcl_connection *tclc;
92 unsigned char in[256];
93
94 rlen = connection_read(connection, &in, sizeof(in));
95 if (rlen <= 0) {
96 if (rlen < 0)
97 LOG_ERROR("error during read: %s", strerror(errno));
98 return ERROR_SERVER_REMOTE_CLOSED;
99 }
100
101 tclc = connection->priv;
102 if (tclc == NULL)
103 return ERROR_CONNECTION_REJECTED;
104
105 /* push as much data into the line as possible */
106 for (i = 0; i < rlen; i++) {
107 /* buffer the data */
108 tclc->tc_line[tclc->tc_lineoffset] = in[i];
109 if (tclc->tc_lineoffset < TCL_MAX_LINE)
110 tclc->tc_lineoffset++;
111 else
112 tclc->tc_linedrop = 1;
113
114 /* ctrl-z is end of command. When testing from telnet, just
115 * press ctrl-z a couple of times first to put telnet into the
116 * mode where it will send 0x1a in response to pressing ctrl-z
117 */
118 if (in[i] != '\x1a')
119 continue;
120
121 /* process the line */
122 if (tclc->tc_linedrop) {
123 #define ESTR "line too long\n"
124 retval = tcl_output(connection, ESTR, sizeof(ESTR));
125 if (retval != ERROR_OK)
126 return retval;
127 #undef ESTR
128 } else {
129 tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
130 LOG_DEBUG("Executing script:\n %s", tclc->tc_line);
131 retval = Jim_Eval_Named(interp, tclc->tc_line, "remote:connection", 1);
132 LOG_DEBUG("Result: %d\n %s", retval,
133 Jim_GetString(Jim_GetResult(interp), &reslen));
134 result = Jim_GetString(Jim_GetResult(interp), &reslen);
135 retval = tcl_output(connection, result, reslen);
136 if (retval != ERROR_OK)
137 return retval;
138 /* Always output ctrl-d as end of line to allow multiline results */
139 tcl_output(connection, "\x1a", 1);
140 }
141
142 tclc->tc_lineoffset = 0;
143 tclc->tc_linedrop = 0;
144 }
145
146 return ERROR_OK;
147 }
148
149 static int tcl_closed(struct connection *connection)
150 {
151 /* cleanup connection context */
152 if (connection->priv) {
153 free(connection->priv);
154 connection->priv = NULL;
155 }
156 return ERROR_OK;
157 }
158
159 int tcl_init(void)
160 {
161 if (strcmp(tcl_port, "disabled") == 0) {
162 LOG_INFO("tcl server disabled");
163 return ERROR_OK;
164 }
165
166 return add_service("tcl", tcl_port, 1,
167 &tcl_new_connection, &tcl_input,
168 &tcl_closed, NULL);
169 }
170
171 COMMAND_HANDLER(handle_tcl_port_command)
172 {
173 return CALL_COMMAND_HANDLER(server_pipe_command, &tcl_port);
174 }
175
176 static const struct command_registration tcl_command_handlers[] = {
177 {
178 .name = "tcl_port",
179 .handler = handle_tcl_port_command,
180 .mode = COMMAND_CONFIG,
181 .help = "Specify port on which to listen "
182 "for incoming Tcl syntax. "
183 "Read help on 'gdb_port'.",
184 .usage = "[port_num]",
185 },
186 COMMAND_REGISTRATION_DONE
187 };
188
189 int tcl_register_commands(struct command_context *cmd_ctx)
190 {
191 tcl_port = strdup("6666");
192 return register_commands(cmd_ctx, NULL, tcl_command_handlers);
193 }

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)