1 /***************************************************************************
\r
2 * Copyright (C) 2008 *
\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
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
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
20 #ifdef HAVE_CONFIG_H
\r
25 #include "tcl_server.h"
\r
29 #include "command.h"
\r
37 #define TCL_SERVER_VERSION "TCL Server 0.1"
\r
38 #define TCL_MAX_LINE (4096)
\r
40 typedef struct tcl_connection_s {
\r
43 char tc_line[TCL_MAX_LINE];
\r
45 int tc_outerror; /* flag an output error */
\r
48 extern Jim_Interp *interp;
\r
49 static unsigned short tcl_port = 0;
\r
52 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\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
60 /* write data out to a socket.
\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
65 int tcl_output(connection_t *connection, const void *data, ssize_t len)
\r
68 tcl_connection_t *tclc;
\r
70 tclc = connection->priv;
\r
71 if (tclc->tc_outerror)
\r
72 return ERROR_SERVER_REMOTE_CLOSED;
\r
74 wlen = write_socket(connection->fd, data, len);
\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
85 static int tcl_new_connection(connection_t *connection)
\r
88 tcl_connection_t *tclc;
\r
90 tclc = malloc(sizeof(tcl_connection_t));
\r
92 return ERROR_CONNECTION_REJECTED;
\r
94 memset(tclc, 0, sizeof(tcl_connection_t));
\r
95 connection->priv = tclc;
\r
99 static int tcl_input(connection_t *connection)
\r
104 const char *result;
\r
106 tcl_connection_t *tclc;
\r
109 rlen = read_socket(connection->fd, &in, sizeof(in));
\r
112 LOG_ERROR("error during read: %s", strerror(errno));
\r
113 return ERROR_SERVER_REMOTE_CLOSED;
\r
116 tclc = connection->priv;
\r
118 return ERROR_CONNECTION_REJECTED;
\r
120 /* push as much data into the line as possible */
\r
121 for (i = 0; i < rlen; i++)
\r
123 if (!isprint(in[i]) && !isspace(in[i]))
\r
125 /* drop this line */
\r
126 tclc->tc_linedrop = 1;
\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
135 tclc->tc_linedrop = 1;
\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
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
154 if (memchr(result, '\n', reslen) == NULL)
\r
155 tcl_output(connection, "\n", 1);
\r
157 tclc->tc_lineoffset = 0;
\r
158 tclc->tc_linedrop = 0;
\r
164 static int tcl_closed(connection_t *connection)
\r
166 /* cleanup connection context */
\r
167 if (connection->priv) {
\r
168 free(connection->priv);
\r
169 connection->priv = NULL;
\r
180 LOG_WARNING("no tcl port specified, using default port 5555");
\r
184 retval = add_service("tcl", CONNECTION_TCL, tcl_port, 1, tcl_new_connection, tcl_input, tcl_closed, NULL);
\r
189 int tcl_register_commands(command_context_t *cmd_ctx)
\r
191 register_command(cmd_ctx, NULL, "tcl_port", handle_tcl_port_command, COMMAND_CONFIG, "");
\r
195 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
198 tcl_port = strtoul(args[0], NULL, 0);
\r