Remove redundant declarations to allow building with -Wredundant-decls.
[fw/openocd] / 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 "log.h"
28 #include "command.h"
29
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #define TCL_SERVER_VERSION      "TCL Server 0.1"
37 #define TCL_MAX_LINE            (4096)
38
39 typedef struct tcl_connection_s {
40         int tc_linedrop;
41         int tc_lineoffset;
42         char tc_line[TCL_MAX_LINE];
43         int tc_outerror; /* flag an output error */
44 } tcl_connection_t;
45
46 static unsigned short tcl_port = 0;
47
48 /* commands */
49 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50
51 /* handlers */
52 static int tcl_new_connection(connection_t *connection);
53 static int tcl_input(connection_t *connection);
54 static int tcl_output(connection_t *connection, const void *buf, ssize_t len);
55 static int tcl_closed(connection_t *connection);
56
57 /* write data out to a socket.
58  *
59  * this is a blocking write, so the return value must equal the length, if
60  * that is not the case then flag the connection with an output error.
61  */
62 int tcl_output(connection_t *connection, const void *data, ssize_t len)
63 {
64         ssize_t wlen;
65         tcl_connection_t *tclc;
66
67         tclc = connection->priv;
68         if (tclc->tc_outerror)
69                 return ERROR_SERVER_REMOTE_CLOSED;
70
71         wlen = write_socket(connection->fd, data, len);
72         if (wlen == len)
73                 return ERROR_OK;
74
75         LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
76         tclc->tc_outerror = 1;
77         return ERROR_SERVER_REMOTE_CLOSED;
78 }
79
80 /* connections */
81 static int tcl_new_connection(connection_t *connection)
82 {
83         tcl_connection_t *tclc;
84
85         tclc = malloc(sizeof(tcl_connection_t));
86         if (tclc == NULL)
87                 return ERROR_CONNECTION_REJECTED;
88
89         memset(tclc, 0, sizeof(tcl_connection_t));
90         connection->priv = tclc;
91         return ERROR_OK;
92 }
93
94 static int tcl_input(connection_t *connection)
95 {
96         int retval;
97         int i;
98         ssize_t rlen;
99         const char *result;
100         int reslen;
101         tcl_connection_t *tclc;
102         char in[256];
103
104         rlen = read_socket(connection->fd, &in, sizeof(in));
105         if (rlen <= 0) {
106                 if (rlen < 0)
107                         LOG_ERROR("error during read: %s", strerror(errno));
108                 return ERROR_SERVER_REMOTE_CLOSED;
109         }
110
111         tclc = connection->priv;
112         if (tclc == NULL)
113                 return ERROR_CONNECTION_REJECTED;
114
115         /* push as much data into the line as possible */
116         for (i = 0; i < rlen; i++)
117         {
118                 if (!isprint(in[i]) && !isspace(in[i]))
119                 {
120                         /* drop this line */
121                         tclc->tc_linedrop = 1;
122                         continue;
123                 }
124
125                 /* buffer the data */
126                 tclc->tc_line[tclc->tc_lineoffset] = in[i];
127                 if (tclc->tc_lineoffset < TCL_MAX_LINE)
128                         tclc->tc_lineoffset++;
129                 else
130                         tclc->tc_linedrop = 1;
131
132                 if (in[i] != '\n')
133                         continue;
134
135                 /* process the line */
136                 if (tclc->tc_linedrop) {
137 #define ESTR "line too long\n"
138                         retval = tcl_output(connection, ESTR, sizeof(ESTR));
139                         if (retval != ERROR_OK)
140                                 return retval;
141 #undef ESTR
142                 }
143                 else {
144                         tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
145                         retval = Jim_Eval_Named(interp, tclc->tc_line, "remote:connection",1);
146                         result = Jim_GetString(Jim_GetResult(interp), &reslen);
147                         retval = tcl_output(connection, result, reslen);
148                         if (retval != ERROR_OK)
149                                 return retval;
150                         if (memchr(result, '\n', reslen) == NULL)
151                                 tcl_output(connection, "\n", 1);
152                 }
153                 
154                 tclc->tc_lineoffset = 0;
155                 tclc->tc_linedrop = 0;
156         }
157
158         return ERROR_OK;
159 }
160
161 static int tcl_closed(connection_t *connection)
162 {
163         /* cleanup connection context */
164         if (connection->priv) {
165                 free(connection->priv);
166                 connection->priv = NULL;
167         }
168         return ERROR_OK;
169 }
170
171 int tcl_init(void)
172 {
173         int retval;
174
175         if (tcl_port == 0)
176         {
177                 LOG_WARNING("no tcl port specified, using default port 6666");
178                 tcl_port = 6666;
179         }
180
181         retval = add_service("tcl", CONNECTION_TCP, tcl_port, 1, tcl_new_connection, tcl_input, tcl_closed, NULL);
182         return retval;
183 }
184
185 int tcl_register_commands(command_context_t *cmd_ctx)
186 {
187         register_command(cmd_ctx, NULL, "tcl_port", handle_tcl_port_command, COMMAND_CONFIG, "port on which to listen for incoming TCL syntax");
188         return ERROR_OK;
189 }
190
191 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
192 {
193         if (argc == 1) {
194                 tcl_port = strtoul(args[0], NULL, 0);
195         }
196         return ERROR_OK;
197 }