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