Fix a bunch of typos.
[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
28 #define TCL_SERVER_VERSION "TCL Server 0.1"
29 #define TCL_MAX_LINE (4096)
30
31 struct tcl_connection {
32 int tc_linedrop;
33 int tc_lineoffset;
34 char tc_line[TCL_MAX_LINE];
35 int tc_outerror; /* flag an output error */
36 };
37
38 static const char *tcl_port;
39
40 /* handlers */
41 static int tcl_new_connection(struct connection *connection);
42 static int tcl_input(struct connection *connection);
43 static int tcl_output(struct connection *connection, const void *buf, ssize_t len);
44 static int tcl_closed(struct connection *connection);
45
46 /* write data out to a socket.
47 *
48 * this is a blocking write, so the return value must equal the length, if
49 * that is not the case then flag the connection with an output error.
50 */
51 int tcl_output(struct connection *connection, const void *data, ssize_t len)
52 {
53 ssize_t wlen;
54 struct tcl_connection *tclc;
55
56 tclc = connection->priv;
57 if (tclc->tc_outerror)
58 return ERROR_SERVER_REMOTE_CLOSED;
59
60 wlen = connection_write(connection, data, len);
61
62 if (wlen == len)
63 return ERROR_OK;
64
65 LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
66 tclc->tc_outerror = 1;
67 return ERROR_SERVER_REMOTE_CLOSED;
68 }
69
70 /* connections */
71 static int tcl_new_connection(struct connection *connection)
72 {
73 struct tcl_connection *tclc;
74
75 tclc = malloc(sizeof(struct tcl_connection));
76 if (tclc == NULL)
77 return ERROR_CONNECTION_REJECTED;
78
79 memset(tclc, 0, sizeof(struct tcl_connection));
80 connection->priv = tclc;
81 return ERROR_OK;
82 }
83
84 static int tcl_input(struct connection *connection)
85 {
86 Jim_Interp *interp = (Jim_Interp *)connection->cmd_ctx->interp;
87 int retval;
88 int i;
89 ssize_t rlen;
90 const char *result;
91 int reslen;
92 struct tcl_connection *tclc;
93 unsigned char in[256];
94
95 rlen = connection_read(connection, &in, sizeof(in));
96 if (rlen <= 0) {
97 if (rlen < 0)
98 LOG_ERROR("error during read: %s", strerror(errno));
99 return ERROR_SERVER_REMOTE_CLOSED;
100 }
101
102 tclc = connection->priv;
103 if (tclc == NULL)
104 return ERROR_CONNECTION_REJECTED;
105
106 /* push as much data into the line as possible */
107 for (i = 0; i < rlen; i++)
108 {
109 /* buffer the data */
110 tclc->tc_line[tclc->tc_lineoffset] = in[i];
111 if (tclc->tc_lineoffset < TCL_MAX_LINE)
112 tclc->tc_lineoffset++;
113 else
114 tclc->tc_linedrop = 1;
115
116 /* ctrl-z is end of command. When testing from telnet, just
117 * press ctrl-z a couple of times first to put telnet into the
118 * mode where it will send 0x1a in response to pressing ctrl-z
119 */
120 if (in[i] != '\x1a')
121 continue;
122
123 /* process the line */
124 if (tclc->tc_linedrop) {
125 #define ESTR "line too long\n"
126 retval = tcl_output(connection, ESTR, sizeof(ESTR));
127 if (retval != ERROR_OK)
128 return retval;
129 #undef ESTR
130 }
131 else {
132 tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
133 LOG_DEBUG("Executing script:\n %s", tclc->tc_line);
134 retval = Jim_Eval_Named(interp, tclc->tc_line, "remote:connection",1);
135 LOG_DEBUG("Result: %d\n %s", retval, Jim_GetString(Jim_GetResult(interp), &reslen));
136 result = Jim_GetString(Jim_GetResult(interp), &reslen);
137 retval = tcl_output(connection, result, reslen);
138 if (retval != ERROR_OK)
139 return retval;
140 /* Always output ctrl-d as end of line to allow multiline results */
141 tcl_output(connection, "\x1a", 1);
142 }
143
144 tclc->tc_lineoffset = 0;
145 tclc->tc_linedrop = 0;
146 }
147
148 return ERROR_OK;
149 }
150
151 static int tcl_closed(struct connection *connection)
152 {
153 /* cleanup connection context */
154 if (connection->priv) {
155 free(connection->priv);
156 connection->priv = NULL;
157 }
158 return ERROR_OK;
159 }
160
161 int tcl_init(void)
162 {
163 if (strcmp(tcl_port, "disabled") == 0)
164 {
165 LOG_INFO("tcl server disabled");
166 return ERROR_OK;
167 }
168
169 return add_service("tcl", tcl_port, 1,
170 &tcl_new_connection, &tcl_input,
171 &tcl_closed, NULL);
172 }
173
174 COMMAND_HANDLER(handle_tcl_port_command)
175 {
176 return CALL_COMMAND_HANDLER(server_pipe_command, &tcl_port);
177 }
178
179 static const struct command_registration tcl_command_handlers[] = {
180 {
181 .name = "tcl_port",
182 .handler = handle_tcl_port_command,
183 .mode = COMMAND_CONFIG,
184 .help = "Specify port on which to listen "
185 "for incoming Tcl syntax. "
186 "Read help on 'gdb_port'.",
187 .usage = "[port_num]",
188 },
189 COMMAND_REGISTRATION_DONE
190 };
191
192 int tcl_register_commands(struct command_context *cmd_ctx)
193 {
194 tcl_port = strdup("6666");
195 return register_commands(cmd_ctx, NULL, tcl_command_handlers);
196 }