ARM ADI-V5: cleanup CID/PID addressing
[openocd/dnglaze.git] / src / server / tcl_server.c
blob7c8e130ba702a8f2b660725143a08d27d938e236
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 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "tcl_server.h"
27 #define TCL_SERVER_VERSION "TCL Server 0.1"
28 #define TCL_MAX_LINE (4096)
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 */
37 static unsigned short tcl_port = 6666;
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);
45 /* write data out to a socket.
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.
50 int tcl_output(struct connection *connection, const void *data, ssize_t len)
52 ssize_t wlen;
53 struct tcl_connection *tclc;
55 tclc = connection->priv;
56 if (tclc->tc_outerror)
57 return ERROR_SERVER_REMOTE_CLOSED;
59 wlen = write_socket(connection->fd, data, len);
60 if (wlen == len)
61 return ERROR_OK;
63 LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
64 tclc->tc_outerror = 1;
65 return ERROR_SERVER_REMOTE_CLOSED;
68 /* connections */
69 static int tcl_new_connection(struct connection *connection)
71 struct tcl_connection *tclc;
73 tclc = malloc(sizeof(struct tcl_connection));
74 if (tclc == NULL)
75 return ERROR_CONNECTION_REJECTED;
77 memset(tclc, 0, sizeof(struct tcl_connection));
78 connection->priv = tclc;
79 return ERROR_OK;
82 static int tcl_input(struct connection *connection)
84 Jim_Interp *interp = (Jim_Interp *)connection->cmd_ctx->interp;
85 int retval;
86 int i;
87 ssize_t rlen;
88 const char *result;
89 int reslen;
90 struct tcl_connection *tclc;
91 unsigned char in[256];
93 rlen = read_socket(connection->fd, &in, sizeof(in));
94 if (rlen <= 0) {
95 if (rlen < 0)
96 LOG_ERROR("error during read: %s", strerror(errno));
97 return ERROR_SERVER_REMOTE_CLOSED;
100 tclc = connection->priv;
101 if (tclc == NULL)
102 return ERROR_CONNECTION_REJECTED;
104 /* push as much data into the line as possible */
105 for (i = 0; i < rlen; i++)
107 if (!isprint(in[i]) && !isspace(in[i]))
109 /* drop this line */
110 tclc->tc_linedrop = 1;
111 continue;
114 /* buffer the data */
115 tclc->tc_line[tclc->tc_lineoffset] = in[i];
116 if (tclc->tc_lineoffset < TCL_MAX_LINE)
117 tclc->tc_lineoffset++;
118 else
119 tclc->tc_linedrop = 1;
121 if (in[i] != '\n')
122 continue;
124 /* process the line */
125 if (tclc->tc_linedrop) {
126 #define ESTR "line too long\n"
127 retval = tcl_output(connection, ESTR, sizeof(ESTR));
128 if (retval != ERROR_OK)
129 return retval;
130 #undef ESTR
132 else {
133 tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
134 retval = Jim_Eval_Named(interp, tclc->tc_line, "remote:connection",1);
135 result = Jim_GetString(Jim_GetResult(interp), &reslen);
136 retval = tcl_output(connection, result, reslen);
137 if (retval != ERROR_OK)
138 return retval;
139 if (memchr(result, '\n', reslen) == NULL)
140 tcl_output(connection, "\n", 1);
143 tclc->tc_lineoffset = 0;
144 tclc->tc_linedrop = 0;
147 return ERROR_OK;
150 static int tcl_closed(struct connection *connection)
152 /* cleanup connection context */
153 if (connection->priv) {
154 free(connection->priv);
155 connection->priv = NULL;
157 return ERROR_OK;
160 int tcl_init(void)
162 int retval;
164 if (tcl_port == 0)
166 LOG_INFO("tcl port disabled");
167 return ERROR_OK;
170 retval = add_service("tcl", CONNECTION_TCP, tcl_port, 1,
171 &tcl_new_connection, &tcl_input,
172 &tcl_closed, NULL);
173 return retval;
176 COMMAND_HANDLER(handle_tcl_port_command)
178 return CALL_COMMAND_HANDLER(server_port_command, &tcl_port);
181 static const struct command_registration tcl_command_handlers[] = {
183 .name = "tcl_port",
184 .handler = handle_tcl_port_command,
185 .mode = COMMAND_CONFIG,
186 .help = "Specify port on which to listen "
187 "for incoming Tcl syntax. "
188 "No arguments reports Tcl port; zero disables.",
189 .usage = "[port_num]",
191 COMMAND_REGISTRATION_DONE
194 int tcl_register_commands(struct command_context *cmd_ctx)
196 return register_commands(cmd_ctx, NULL, tcl_command_handlers);