fix a few compilation problems.
[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", wlen, 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 int i;
88 tcl_connection_t *tclc;
89
90 tclc = malloc(sizeof(tcl_connection_t));
91 if (tclc == NULL)
92 return ERROR_CONNECTION_REJECTED;
93
94 memset(tclc, 0, sizeof(tcl_connection_t));
95 connection->priv = tclc;
96 return ERROR_OK;
97 }
98
99 static int tcl_input(connection_t *connection)
100 {
101 int retval;
102 int i;
103 ssize_t rlen;
104 const char *result;
105 int reslen;
106 tcl_connection_t *tclc;
107 char in[256];
108
109 rlen = read_socket(connection->fd, &in, sizeof(in));
110 if (rlen <= 0) {
111 if (rlen < 0)
112 LOG_ERROR("error during read: %s", strerror(errno));
113 return ERROR_SERVER_REMOTE_CLOSED;
114 }
115
116 tclc = connection->priv;
117 if (tclc == NULL)
118 return ERROR_CONNECTION_REJECTED;
119
120 /* push as much data into the line as possible */
121 for (i = 0; i < rlen; i++)
122 {
123 if (!isprint(in[i]) && !isspace(in[i]))
124 {
125 /* drop this line */
126 tclc->tc_linedrop = 1;
127 continue;
128 }
129
130 /* buffer the data */
131 tclc->tc_line[tclc->tc_lineoffset] = in[i];
132 if (tclc->tc_lineoffset < TCL_MAX_LINE)
133 tclc->tc_lineoffset++;
134 else
135 tclc->tc_linedrop = 1;
136
137 if (in[i] != '\n')
138 continue;
139
140 /* process the line */
141 if (tclc->tc_linedrop) {
142 #define ESTR "line too long\n"
143 retval = tcl_output(connection, ESTR, sizeof(ESTR));
144 if (retval != ERROR_OK)
145 return retval;
146 #undef ESTR
147 } else {
148 tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
149 retval = Jim_Eval(interp, tclc->tc_line);
150 result = Jim_GetString(Jim_GetResult(interp), &reslen);
151 retval = tcl_output(connection, result, reslen);
152 if (retval != ERROR_OK)
153 return retval;
154 if (memchr(result, '\n', reslen) == NULL)
155 tcl_output(connection, "\n", 1);
156 }
157 tclc->tc_lineoffset = 0;
158 tclc->tc_linedrop = 0;
159 }
160
161 return ERROR_OK;
162 }
163
164 static int tcl_closed(connection_t *connection)
165 {
166 /* cleanup connection context */
167 if (connection->priv) {
168 free(connection->priv);
169 connection->priv = NULL;
170 }
171 return ERROR_OK;
172 }
173
174 int tcl_init(void)
175 {
176 int retval;
177
178 if (tcl_port == 0)
179 {
180 LOG_WARNING("no tcl port specified, using default port 5555");
181 tcl_port = 5555;
182 }
183
184 retval = add_service("tcl", CONNECTION_TCL, tcl_port, 1, tcl_new_connection, tcl_input, tcl_closed, NULL);
185 return retval;
186 }
187
188
189 int tcl_register_commands(command_context_t *cmd_ctx)
190 {
191 register_command(cmd_ctx, NULL, "tcl_port", handle_tcl_port_command, COMMAND_CONFIG, "");
192 return ERROR_OK;
193 }
194
195 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
196 {
197 if (argc == 1) {
198 tcl_port = strtoul(args[0], NULL, 0);
199 }
200 return ERROR_OK;
201 }

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)