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