Browse code

- new command line tool which communicates with the ctl module (using the new binary protocol) - supports various connection methods: udp, tcp, unix sockets (datagram and stream) - can do basic reply formatting (see -f ) - supports both command line and interactive modes - if compiled with libreadline supports command completion in the interactive mode - depends only on libreadline/libedit if compiled with command completion support Example: sercmd -s unixd:/tmp/unix_dgram -f 'pid:%v desc:"%v"\n' core.ps sercmd # enters interactive mode, uses default unix:/tmp/ser_ctl socket sercmd ps sercmd who (see also sercmd -h and utils/sercmd/EXAMPLES)

Andrei Pelinescu-Onciul authored on 23/02/2006 20:07:45
Showing 8 changed files
... ...
@@ -10,6 +10,14 @@ new archs:
10 10
 
11 11
 
12 12
 modules:
13
+ - ctl        - new  fifo/unixsocket/xmlrpc like module, using a space 
14
+                efficient binary encoding for the requests.
15
+                It supports multiple clients on tcp, udp, unix stream or
16
+                unix datagram modes. By default (no modparams) it opens
17
+                one unix stream control socket in /tmp/ser_ctl.
18
+                It also includes extended fifo support: multiple fifos,
19
+                fifo over tcp, udp and unix sockets (see ctl/ctl.cfg).
20
+                Use utils/sercmd/sercmd to send commands to it.
13 21
  - dispatcher - added hashing after request uri and to uri
14 22
               - added a new flag parameter which can be used (for now) to
15 23
                 select only the username or the username, host and port when
... ...
@@ -41,6 +49,24 @@ modules:
41 41
               Vias a.s.o) and not on the original message
42 42
  
43 43
 core:
44
+ - added named routes: names can be used instead of numbers in all the
45
+   route commads or route declarations. route(number) is equivalent to
46
+   route("number").
47
+   Example:
48
+     route("test");
49
+      route["test"]{
50
+           ...
51
+     }
52
+ - added named flags, declared at the beginning of the config file with:
53
+     flags  flag1_name[:position],  flag2_name ...
54
+   Example:
55
+       flags test, a:1, b:2 ;
56
+       route{
57
+              setflag(test);
58
+              if (isflagset(a)){ # equiv. to isflagset(1)
59
+                ....
60
+              }
61
+              resetflag(b);  # equiv. to resetflag(2) 
44 62
  - added return [val] which returns from a route. if no value is specified, or
45 63
    a route reaches its end without executing a return statement, it returns 1.
46 64
    If return is used in the top level route is equivalent with exit [val].
... ...
@@ -123,6 +149,14 @@ new config variables:
123 123
    tcp_max_connections = no. - maximum number of tcp connections (if the number
124 124
       is exceeded no new tcp connections will be accepted). Default: 2048.
125 125
 
126
+tools:
127
+  utils/sercmd - command line serctl like tool for interrogating ser ctl
128
+                 module (uses the binrpc encoding). Supports various
129
+                 connection methods (udp, tcp, unix stream & datagram sockets),
130
+                 reply formating (see -f, e.g. sercmd -f "pid:%v %v\n" core.ps)
131
+                 , interactive mode, command line completion (if compiled with 
132
+                 libreadline) a.s.o.
133
+
126 134
 
127 135
 0.9.4 fixes/improvements (0.9.4 is a bug fix release for 0.9.3)
128 136
  
129 137
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+# $id$
1
+
2
+sercmd usage examples
3
+
4
+
5
+help:
6
+	sercmd  unixd:/tmp/unix_dgram -h
7
+
8
+use an udp ser control socket:
9
+ ser config:
10
+  loadmodule "modules/ctl/ctl.so"
11
+  modparam("ctl", "binrpc", "udp:localhost:2046")
12
+  modparam("ctl", "binrpc", "tcp:localhost:2046")
13
+  modparam("ctl", "binrpc", "unixs:/tmp/unix_stream")
14
+  modparam("ctl", "binrpc", "unixd:/tmp/unix_dgram")
15
+
16
+ sercmd:
17
+	sercmd -s udp:localhost:2046 core.version
18
+
19
+use a tcp socket:
20
+	sercmd -s tcp:localhost:2046 core.version
21
+
22
+use a stream unix socket:
23
+	sercmd -s unixs:/tmp/unix_stream core.version
24
+
25
+use a datagram unix socket:
26
+	sercmd -s unixd:/tmp/unix_dgram core.version
27
+
28
+
29
+
30
+list available commands on ser side:
31
+	sercmd -s unixd:/tmp/unix_drgam ls
32
+
33
+list all available commands (including sercmd builtins or aliases):
34
+	sercmd -s unixd:/tmp/unix_dgram ?
35
+or
36
+	sercmd -s unixd:/tmp/unix_dgram help
37
+
38
+get help on one command:
39
+	sercmd -s unixd:/tmp/unix_dgram help core.ps
40
+
41
+list ser processes:
42
+	sercmd -s unixd:/tmp/unix_dgram ps
43
+
44
+send an rpc command to ser:
45
+	sercmd -s unixd:/tmp/unix_dgram core.shmmem
46
+
47
+format the output:
48
+	sercmd -s unixd:/tmp/unix_dgram -f 'pid:%v desc:"%v"\n' core.ps
49
+	(note: you could use just ps instead of core.ps)
50
+	
51
+format the output as csv:
52
+	sercmd -s unixd:/tmp/unix_dgram -f '%v,' core.ps
53
+
54
+enter interactive mode:
55
+	sercmd -s unixd:/tmp/unix_dgram
56
+	(note: type help,or ? to see the command list, tab expansion should also
57
+	 work)
58
+
0 59
new file mode 100644
... ...
@@ -0,0 +1,22 @@
0
+# $Id$
1
+
2
+include ../../Makefile.defs
3
+
4
+auto_gen=
5
+NAME=sercmd
6
+RELEASE=0.1
7
+use_readline=1
8
+DEFS:= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' \
9
+		$(filter -D%HAVE -DARCH% -DOS% -D__CPU% -D__OS%, $(DEFS))
10
+LIBS:=$(filter-out -lfl  -ldl -lpthread -lssl -lcrypto, $(LIBS))
11
+
12
+ifeq ($(use_readline),1)
13
+	DEFS+=-DUSE_READLINE
14
+	LIBS+=-lreadline
15
+endif
16
+
17
+include ../../Makefile.sources
18
+include ../../Makefile.rules
19
+
20
+
21
+modules:
0 22
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+
1
+- help for internal commands & aliases
2
+- send timeout
3
+- reconnect?
0 4
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * sercmd GPL license, standard disclaimer and copyright
4
+ */
5
+
6
+#ifndef __license_h_
7
+#define __license_h_
8
+
9
+#define COPYRIGHT  "Copyright 2006 iptelorg GmbH"
10
+#define DISCLAIMER \
11
+"This is free software with ABSOLUTELY NO WARRANTY.\n\
12
+For details type `warranty'."
13
+
14
+
15
+#define LICENSE \
16
+"    This program is free software; you can redistribute it and/or modify\n\
17
+    it under the terms of the GNU General Public License as published by\n\
18
+    the Free Software Foundation; either version 2 of the License , or\n\
19
+    (at your option) any later version.\n\
20
+\n\
21
+    This program is distributed in the hope that it will be useful,\n\
22
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
23
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
24
+    GNU General Public License for more details.\n\
25
+\n\
26
+    You should have received a copy of the GNU General Public License\n\
27
+    along with this program. If not, write to\n\
28
+\n\
29
+       The Free Software Foundation, Inc.\n\
30
+       51 Franklin Street, Fifth Floor,\n\
31
+       Boston, MA 02110-1301, USA."
32
+
33
+#endif
0 34
new file mode 100644
... ...
@@ -0,0 +1,267 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2006 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+/* History:
27
+ * --------
28
+ *  2006-02-20  created by andrei
29
+ */
30
+
31
+
32
+#include <stdio.h>
33
+#include <stdlib.h> /* malloc */
34
+#include <string.h>
35
+#include <netdb.h> /* getservbyname*/
36
+
37
+#include "parse_listen_id.h"
38
+
39
+/* ser source compat. defines*/
40
+#define pkg_malloc malloc
41
+#define pkg_free   free
42
+
43
+#ifdef __SUNPRO_C
44
+#define DBG(...)
45
+#define LOG(lev, ...) fprintf(stderr,  __VA_ARGS__)
46
+#else
47
+#define DBG(fmt, args...)
48
+#define LOG(lev, fmt, args...) fprintf(stderr, fmt, ## args)
49
+#endif
50
+
51
+
52
+
53
+
54
+
55
+/* converts a str to an u. short, returns the u. short and sets *err on
56
+ * error and if err!=null
57
+  */
58
+static inline unsigned short str2s(const char* s, unsigned int len,
59
+									int *err)
60
+{
61
+	unsigned short ret;
62
+	int i;
63
+	unsigned char *limit;
64
+	unsigned char *init;
65
+	unsigned char* str;
66
+
67
+	/*init*/
68
+	str=(unsigned char*)s;
69
+	ret=i=0;
70
+	limit=str+len;
71
+	init=str;
72
+
73
+	for(;str<limit ;str++){
74
+		if ( (*str <= '9' ) && (*str >= '0') ){
75
+				ret=ret*10+*str-'0';
76
+				i++;
77
+				if (i>5) goto error_digits;
78
+		}else{
79
+				/* error unknown char */
80
+				goto error_char;
81
+		}
82
+	}
83
+	if (err) *err=0;
84
+	return ret;
85
+
86
+error_digits:
87
+	DBG("str2s: ERROR: too many letters in [%.*s]\n", (int)len, init);
88
+	if (err) *err=1;
89
+	return 0;
90
+error_char:
91
+	DBG("str2s: ERROR: unexpected char %c in %.*s\n", *str, (int)len, init);
92
+	if (err) *err=1;
93
+	return 0;
94
+}
95
+
96
+
97
+
98
+/* parse proto:address:port   or proto:address */
99
+/* returns struct id_list on success (pkg_malloc'ed), 0 on error
100
+ * WARNING: it will add \0 in the string*/
101
+/* parses:
102
+ *     tcp|udp|unix:host_name:port
103
+ *     tcp|udp|unix:host_name
104
+ *     host_name:port
105
+ *     host_name
106
+ * 
107
+ *
108
+ *     where host_name=string, ipv4 address, [ipv6 address],
109
+ *         unix socket path (starts with '/')
110
+ */
111
+struct id_list* parse_listen_id(char* l, int len, enum socket_protos def)
112
+{
113
+	char* p;
114
+	enum socket_protos proto;
115
+	char* name;
116
+	char* port_str;
117
+	int port;
118
+	int err;
119
+	struct servent* se;
120
+	char* s;
121
+	struct id_list* id;
122
+	
123
+	s=pkg_malloc((len+1)*sizeof(char));
124
+	if (s==0){
125
+		LOG(L_ERR, "ERROR:parse_listen_id: out of memory\n");
126
+		goto error;
127
+	}
128
+	memcpy(s, l, len);
129
+	s[len]=0; /* null terminate */
130
+	
131
+	/* duplicate */
132
+	proto=UNKNOWN_SOCK;
133
+	port=0;
134
+	name=0;
135
+	port_str=0;
136
+	p=s;
137
+	
138
+	if ((*p)=='[') goto ipv6;
139
+	/* find proto or name */
140
+	for (; *p; p++){
141
+		if (*p==':'){
142
+			*p=0;
143
+			if (strcasecmp("tcp", s)==0){
144
+				proto=TCP_SOCK;
145
+				goto find_host;
146
+			}else if (strcasecmp("udp", s)==0){
147
+				proto=UDP_SOCK;
148
+				goto find_host;
149
+			}else if ((strcasecmp("unix", s)==0)||(strcasecmp("unixd", s)==0)){
150
+				proto=UNIXS_SOCK;
151
+				goto find_host;
152
+			}else if (strcasecmp("unixs", s)==0){
153
+				proto=UNIXS_SOCK;
154
+				goto find_host;
155
+#ifdef USE_FIFO
156
+			}else if (strcasecmp("fifo", s)==0){
157
+				proto=FIFO_SOCK;
158
+				goto find_host;
159
+#endif
160
+			}else{
161
+				proto=UNKNOWN_SOCK;
162
+				/* this might be the host */
163
+				name=s;
164
+				goto find_port;
165
+			}
166
+		}
167
+	}
168
+	name=s;
169
+	goto end; /* only name found */
170
+find_host:
171
+	p++;
172
+	if (*p=='[') goto ipv6;
173
+	name=p;
174
+	for (; *p; p++){
175
+		if ((*p)==':'){
176
+			*p=0;
177
+			goto find_port;
178
+		}
179
+	}
180
+	goto end; /* nothing after name */
181
+ipv6:
182
+	name=p;
183
+	p++;
184
+	for(;*p;p++){
185
+		if(*p==']'){
186
+			if(*(p+1)==':'){
187
+				p++; *p=0;
188
+				goto find_port;
189
+			}else if (*(p+1)==0) goto end;
190
+		}else{
191
+			goto error;
192
+		}
193
+	}
194
+	
195
+find_port:
196
+	p++;
197
+	port_str=(*p)?p:0;
198
+	
199
+end:
200
+	/* fix all the stuff */
201
+	if (name==0) goto error;
202
+	if (proto==UNKNOWN_SOCK){
203
+		/* try to guess */
204
+		if (port_str){
205
+			switch(def){
206
+				case TCP_SOCK:
207
+				case UDP_SOCK:
208
+					proto=def;
209
+					break;
210
+				default:
211
+					proto=TCP_SOCK;
212
+					DBG("guess:%s is a tcp socket\n", name);
213
+			}
214
+		}else if (name && strchr(name, '/')){
215
+			switch(def){
216
+				case TCP_SOCK:
217
+				case UDP_SOCK:
218
+					DBG("guess:%s is a unix socket\n", name);
219
+					proto=UNIXD_SOCK;
220
+					break;
221
+				default:
222
+					/* def is filename based => use default */
223
+					proto=def;
224
+			}
225
+		}else{
226
+			/* using default */
227
+			proto=def;
228
+		}
229
+	}
230
+	if (port_str){
231
+		port=str2s(port_str, strlen(port_str), &err);
232
+		if (err){
233
+			/* try getservbyname */
234
+			se=getservbyname(port_str, 
235
+					(proto==TCP_SOCK)?"tcp":(proto==UDP_SOCK)?"udp":0);
236
+			if (se) port=ntohs(se->s_port);
237
+			else goto error;
238
+		}
239
+	}else{
240
+		/* no port, check if the hostname is a port 
241
+		 * (e.g. tcp:3012 == tcp:*:3012 */
242
+		if (proto==TCP_SOCK|| proto==UDP_SOCK){
243
+			port=str2s(name, strlen(name), &err);
244
+			if (err){
245
+				port=0;
246
+			}else{
247
+				name="*"; /* inaddr any  */
248
+			}
249
+		}
250
+	}
251
+	id=pkg_malloc(sizeof(struct id_list));
252
+	if (id==0){
253
+		LOG(L_ERR, "ERROR:parse_listen_id: out of memory\n");
254
+		goto error;
255
+	}
256
+	id->name=name;
257
+	id->proto=proto;
258
+	id->data_proto=P_BINRPC;
259
+	id->port=port;
260
+	id->buf=s;
261
+	id->next=0;
262
+	return id;
263
+error:
264
+	if (s) pkg_free(s);
265
+	return 0;
266
+}
0 267
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2006 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+/* History:
27
+ * --------
28
+ *  2006-02-20  created by andrei
29
+ */
30
+
31
+
32
+#ifndef parse_listen_id_h
33
+#define parse_listen_id_h
34
+
35
+enum payload_proto	{ P_BINRPC , P_FIFO };
36
+
37
+enum socket_protos	{	UNKNOWN_SOCK=0, UDP_SOCK, TCP_SOCK, 
38
+						UNIXS_SOCK, UNIXD_SOCK
39
+#ifdef USE_FIFO
40
+							, FIFO_SOCK
41
+#endif
42
+};
43
+
44
+
45
+
46
+struct id_list{
47
+	char* name;
48
+	enum socket_protos proto;
49
+	enum payload_proto data_proto;
50
+	int port;
51
+	char* buf; /* name points somewhere here */
52
+	struct id_list* next;
53
+};
54
+
55
+
56
+struct id_list* parse_listen_id(char* l, int len, enum socket_protos def);
57
+
58
+#endif
0 59
new file mode 100644
... ...
@@ -0,0 +1,1467 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2006 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+/*
27
+ * send commands using binrpc
28
+ *
29
+ * History:
30
+ * --------
31
+ *  2006-02-14  created by andrei
32
+ */
33
+
34
+
35
+#include <stdlib.h> /* exit, abort */
36
+#include <stdio.h>
37
+#include <string.h>
38
+#include <unistd.h>
39
+#include <errno.h>
40
+#include <ctype.h> /* isprint */
41
+#include <sys/socket.h>
42
+#include <sys/un.h> /* unix sock*/
43
+#include <netinet/in.h> /* udp sock */
44
+#include <sys/uio.h> /* writev */
45
+#include <netdb.h> /* gethostbyname */
46
+#include <time.h> /* time */
47
+
48
+#ifdef USE_READLINE
49
+#include <readline/readline.h>
50
+#include <readline/history.h>
51
+#endif
52
+
53
+#include "parse_listen_id.h"
54
+#include "license.h"
55
+
56
+#include "../../modules/ctl/ctl_defaults.h" /* default socket & port */
57
+#include "../../modules/ctl/binrpc.h"
58
+#include "../../modules/ctl/binrpc.c" /* ugly hack */
59
+
60
+
61
+#ifndef NAME
62
+#define NAME    "sercmd"
63
+#endif
64
+#ifndef VERSION
65
+#define VERSION "0.1"
66
+#endif
67
+
68
+#define IOVEC_CNT 20
69
+#define BUF_SIZE  65535
70
+#define MAX_LINE_SIZE 1024 /* for non readline mode */
71
+#define MAX_REPLY_SIZE  4096
72
+#define MAX_BODY_SIZE   4096
73
+#define MAX_BINRPC_ARGS  128
74
+
75
+
76
+#ifndef UNIX_PATH_MAX
77
+#define UNIX_PATH_MAX 108
78
+#endif
79
+
80
+static char id[]="$Id$";
81
+static char version[]= NAME " " VERSION;
82
+static char compiled[]= __TIME__ " " __DATE__;
83
+static char help_msg[]="\
84
+Usage: " NAME " [options][-s address] [ cmd ]\n\
85
+Options:\n\
86
+    -s address  unix socket name or host name to send the commands on\n\
87
+    -R name     force reply socket name, for the unix datagram socket mode\n\
88
+    -D dir      create the reply socket in the directory <dir> if no reply \n\
89
+                socket is forced (-R) and a unix datagram socket is selected\n\
90
+                as the transport\n\
91
+    -f format   print the result using format. Format is a string containing\n\
92
+                %v at the places where values read from the reply should be\n\
93
+                substituted. To print '%v', escape it using '%': %%v.\n\
94
+    -v          Verbose       \n\
95
+    -V          Version number\n\
96
+    -h          This help message\n\
97
+address:\n\
98
+    [proto:]name[:port]   where proto is one of tcp, udp, unixs or unixd\n\
99
+                          e.g.:  tcp:localhost:2048 , unixs:/tmp/ser_ctl\n\
100
+cmd:\n\
101
+    method  [arg1 [arg2...]]\n\
102
+arg:\n\
103
+     string or number; to force a number to be interpreted as string \n\
104
+     prefix it by \"s:\", e.g. s:1\n\
105
+Example:\n\
106
+        " NAME " -s unixs:/tmp/ser_unix system.listMethods\n\
107
+        " NAME " -f \"pid: %v  desc: %v\\n\" -s udp:localhost:2047 core.ps \n\
108
+        " NAME " ps  # uses default ctl socket \n\
109
+        " NAME "     # enters interactive mode on the default socket \n\
110
+        " NAME " -s tcp:localhost # interactive mode, default port \n\
111
+";
112
+
113
+
114
+int verbose=0;
115
+char* reply_socket=0; /* unix datagram reply socket name */
116
+char* sock_dir=0;     /* same as above, but only the directory */
117
+char* unix_socket=0;
118
+struct sockaddr_un mysun;
119
+int quit; /* used only in interactive mode */
120
+
121
+struct binrpc_val* rpc_array;
122
+int rpc_no=0;
123
+
124
+
125
+
126
+
127
+#define IOV_SET(vect, str) \
128
+	do{\
129
+		(vect).iov_base=(str); \
130
+		(vect).iov_len=strlen((str)); \
131
+	}while(0)
132
+
133
+
134
+#define INT2STR_MAX_LEN  (19+1+1) /* 2^64~= 16*10^18 => 19+1 digits + \0 */
135
+
136
+/* returns a pointer to a static buffer containing l in asciiz & sets len */
137
+static inline char* int2str(unsigned int l, int* len)
138
+{
139
+	static char r[INT2STR_MAX_LEN];
140
+	int i;
141
+	
142
+	i=INT2STR_MAX_LEN-2;
143
+	r[INT2STR_MAX_LEN-1]=0; /* null terminate */
144
+	do{
145
+		r[i]=l%10+'0';
146
+		i--;
147
+		l/=10;
148
+	}while(l && (i>=0));
149
+	if (l && (i<0)){
150
+		fprintf(stderr, "BUG: int2str: overflow\n");
151
+	}
152
+	if (len) *len=(INT2STR_MAX_LEN-2)-i;
153
+	return &r[i+1];
154
+}
155
+
156
+
157
+
158
+static char* trim_ws(char* l)
159
+{
160
+	char* ret;
161
+	
162
+	for(;*l && ((*l==' ')||(*l=='\t')||(*l=='\n')||(*l=='\r')); l++);
163
+	ret=l;
164
+	if (*ret==0) return ret;
165
+	for(l=l+strlen(l)-1; (l>ret) && 
166
+			((*l==' ')||(*l=='\t')||(*l=='\n')||(*l=='\r')); l--);
167
+	*(l+1)=0;
168
+	return ret;
169
+}
170
+
171
+
172
+
173
+int gen_cookie()
174
+{
175
+	return rand();
176
+}
177
+
178
+
179
+
180
+struct binrpc_cmd{
181
+	char* method;
182
+	int argc;
183
+	struct binrpc_val argv[MAX_BINRPC_ARGS];
184
+};
185
+
186
+
187
+struct cmd_alias{
188
+	char* name;
189
+	char* method;
190
+	char* format; /* reply print format */
191
+};
192
+
193
+
194
+struct sercmd_builtin{
195
+	char* name;
196
+	int (*f)(int, struct binrpc_cmd*);
197
+	char* doc;
198
+};
199
+
200
+
201
+static int sercmd_help(int s, struct binrpc_cmd* cmd);
202
+static int sercmd_ver(int s, struct binrpc_cmd* cmd);
203
+static int sercmd_quit(int s, struct binrpc_cmd* cmd);
204
+static int sercmd_warranty(int s, struct binrpc_cmd* cmd);
205
+
206
+
207
+static struct cmd_alias cmd_aliases[]={
208
+	{	"ps",			"core.ps",				"%v\t%v\n"	},
209
+	{	"list",			"system.listMethods",	0			},
210
+	{	"ls",			"system.listMethods",	0			},
211
+	{	"server",		"core.version",			0			},
212
+	{	"serversion",	"core.version",			0			},
213
+	{	"who",			"ctl.who",				"[%v] %v: %v %v -> %v %v\n"},
214
+	{	"listen",		"ctl.listen",			"[%v] %v: %v %v\n"},
215
+	{0,0,0}
216
+};
217
+
218
+
219
+static struct sercmd_builtin builtins[]={
220
+	{	"?",		sercmd_help, "help"},
221
+	{	"help",		sercmd_help, "displays help for a command"},
222
+	{	"version",	sercmd_ver,  "displays " NAME "version"},
223
+	{	"quit",		sercmd_quit, "exits " NAME },
224
+	{	"exit",		sercmd_quit, "exits " NAME },
225
+	{	"warranty",		sercmd_warranty, "displays " NAME "'s warranty info"},
226
+	{	"license",		sercmd_warranty, "displays " NAME "'s license"},
227
+	{0,0}
228
+};
229
+
230
+
231
+
232
+#ifdef USE_READLINE
233
+/* commands for which we complete the params to other command names */
234
+char* complete_params[]={
235
+	"?",
236
+	"h",
237
+	"help",
238
+	"system.methodSignature",
239
+	"system.methodHelp",
240
+	0
241
+};
242
+#endif
243
+
244
+
245
+
246
+static int parse_arg(struct binrpc_val* v, char* arg)
247
+{
248
+	int i;
249
+	double f;
250
+	char* tmp;
251
+	int len;
252
+	
253
+	i=strtol(arg, &tmp, 10);
254
+	if ((tmp==0) || (*tmp)){
255
+		f=strtod(arg, &tmp);
256
+		if ((tmp==0) || (*tmp)){
257
+			/* not an int or a float => string */
258
+			len=strlen(arg);
259
+			if ((len>=2) && (arg[0]=='s') && (arg[1]==':')){
260
+				tmp=&arg[2];
261
+				len-=2;
262
+			}else{
263
+				tmp=arg;
264
+			}
265
+			v->type=BINRPC_T_STR;
266
+			v->u.strval.s=tmp;
267
+			v->u.strval.len=len;
268
+		}else{ /* float */
269
+			v->type=BINRPC_T_DOUBLE;
270
+			v->u.fval=f;
271
+		}
272
+	}else{ /* int */
273
+		v->type=BINRPC_T_INT;
274
+		v->u.intval=i;
275
+	}
276
+	return 0;
277
+}
278
+
279
+
280
+
281
+static int parse_cmd(struct binrpc_cmd* cmd, char** argv, int count)
282
+{
283
+	int r;
284
+	
285
+	cmd->method=argv[0];
286
+	if ((count-1)>MAX_BINRPC_ARGS){
287
+		fprintf(stderr,  "ERROR: too many args %d, only %d allowed\n",
288
+					count-1, MAX_BINRPC_ARGS);
289
+		return -1;
290
+	}
291
+	for (r=1; r<count; r++){
292
+		if (parse_arg(&cmd->argv[r-1], argv[r])<0)
293
+			return -1;
294
+	}
295
+	cmd->argc=r-1;
296
+	return 0;
297
+}
298
+
299
+
300
+void print_binrpc_val(struct binrpc_val* v, int ident)
301
+{
302
+	int r;
303
+
304
+	if ((v->type==BINRPC_T_STRUCT) && !v->u.end)
305
+		ident--; /* fix to have strut beg. idented differently */
306
+	for (r=0; r<ident; r++) putchar('	');
307
+	if (v->name.s){
308
+		printf("%.*s: ", v->name.len, v->name.s);
309
+	}
310
+	switch(v->type){
311
+		case BINRPC_T_INT:
312
+			printf("%d", v->u.intval);
313
+			break;
314
+		case BINRPC_T_STR:
315
+		case BINRPC_T_BYTES:
316
+			printf("%.*s", v->u.strval.len, v->u.strval.s);
317
+			break;
318
+		case BINRPC_T_ARRAY:
319
+			printf("%c", (v->u.end)?']':'[');
320
+			break;
321
+		case BINRPC_T_STRUCT:
322
+			printf("%c", (v->u.end)?'}':'{');
323
+			break;
324
+		default:
325
+			printf("ERROR: unknown type %d\n", v->type);
326
+	};
327
+}
328
+
329
+
330
+
331
+/* opens,  and  connects on a STREAM unix socket
332
+ * returns socket fd or -1 on error */
333
+int connect_unix_sock(char* name, int type)
334
+{
335
+	struct sockaddr_un ifsun;
336
+	int s;
337
+	int len;
338
+	int ret;
339
+	int retries;
340
+	
341
+	retries=0;
342
+	s=-1;
343
+	memset(&ifsun, 0, sizeof (struct sockaddr_un));
344
+	len=strlen(name);
345
+	if (len>UNIX_PATH_MAX){
346
+		fprintf(stderr, "ERROR: connect_unix_sock: name too long "
347
+				"(%d > %d): %s\n", len, UNIX_PATH_MAX, name);
348
+		goto error;
349
+	}
350
+	ifsun.sun_family=AF_UNIX;
351
+	memcpy(ifsun.sun_path, name, len);
352
+#ifdef HAVE_SOCKADDR_SA_LEN
353
+	ifsun.sun_len=len;
354
+#endif
355
+	s=socket(PF_UNIX, type, 0);
356
+	if (s==-1){
357
+		fprintf(stderr, "ERROR: connect_unix_sock: cannot create unix socket"
358
+				" %s: %s [%d]\n", name, strerror(errno), errno);
359
+		goto error;
360
+	}
361
+	if (type==SOCK_DGRAM){
362
+		/* we must bind so that we can receive replies */
363
+		if (reply_socket==0){
364
+			if (sock_dir==0)
365
+				sock_dir="/tmp";
366
+retry:
367
+			ret=snprintf(mysun.sun_path, UNIX_PATH_MAX, "%s/" NAME "_%d",
368
+							sock_dir, rand()); 
369
+			if ((ret<0) ||(ret>=UNIX_PATH_MAX)){
370
+				fprintf(stderr, "ERROR: buffer overflow while trying to"
371
+							"generate unix datagram socket name");
372
+				goto error;
373
+			}
374
+		}else{
375
+			if (strlen(reply_socket)>UNIX_PATH_MAX){
376
+				fprintf(stderr, "ERROR: buffer overflow while trying to"
377
+							"use the provided unix datagram socket name (%s)",
378
+							reply_socket);
379
+				goto error;
380
+			}
381
+			strcpy(mysun.sun_path, reply_socket);
382
+		}
383
+		mysun.sun_family=AF_UNIX;
384
+		if (bind(s, (struct sockaddr*)&mysun, sizeof(mysun))==-1){
385
+			if (errno==EADDRINUSE && (reply_socket==0) && (retries < 10)){
386
+				retries++;
387
+				/* try another one */
388
+				goto retry;
389
+			}
390
+			fprintf(stderr, "ERROR: could not bind the unix socket to"
391
+					" %s: %s (%d)\n",
392
+					mysun.sun_path, strerror(errno), errno);
393
+			goto error;
394
+		}
395
+		unix_socket=mysun.sun_path;
396
+	}
397
+	if (connect(s, (struct sockaddr *)&ifsun, sizeof(ifsun))==-1){
398
+		fprintf(stderr, "ERROR: connect_unix_sock: connect(%s): %s [%d]\n",
399
+				name, strerror(errno), errno);
400
+		goto error;
401
+	}
402
+	return s;
403
+error:
404
+	if (s!=-1) close(s);
405
+	return -1;
406
+}
407
+
408
+
409
+
410
+int connect_tcpudp_socket(char* address, int port, int type)
411
+{
412
+	struct sockaddr_in addr;
413
+	struct hostent* he;
414
+	int sock;
415
+	
416
+	sock=-1;
417
+	/* resolve destination */
418
+	he=gethostbyname(address);
419
+	if (he==0){
420
+		fprintf(stderr, "ERROR: could not resolve %s\n", address);
421
+		goto error;
422
+	}
423
+	/* open socket*/
424
+	addr.sin_family=he->h_addrtype;
425
+	addr.sin_port=htons(port);
426
+	memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
427
+	
428
+	sock = socket(he->h_addrtype, type, 0);
429
+	if (sock==-1){
430
+		fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
431
+		goto error;
432
+	}
433
+	if (connect(sock, (struct sockaddr*) &addr, sizeof(struct sockaddr))!=0){
434
+		fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
435
+		goto error;
436
+	}
437
+	return sock;
438
+error:
439
+	if (sock!=-1) close(sock);
440
+	return -1;
441
+}
442
+
443
+
444
+
445
+static void hexdump(unsigned char* buf, int len, int ascii)
446
+{
447
+	int r, i;
448
+	
449
+	/* dump it in hex */
450
+	for (r=0; r<len; r++){
451
+		if ((r) && ((r%16)==0)){
452
+			if (ascii){
453
+				putchar(' ');
454
+				for (i=r-16; i<r; i++){
455
+					if (isprint(buf[i]))
456
+						putchar(buf[i]);
457
+					else
458
+						putchar('.');
459
+				}
460
+			}
461
+			putchar('\n');
462
+		}
463
+		printf("%02x ", buf[r]);
464
+	};
465
+	if (ascii){
466
+		for (i=r;i%16; i++)
467
+			printf("   ");
468
+		putchar(' ');
469
+		for (i=16*(r/16); i<r; i++){
470
+			if (isprint(buf[i]))
471
+				putchar(buf[i]);
472
+			else
473
+				putchar('.');
474
+		}
475
+	}
476
+	putchar('\n');
477
+}
478
+
479
+
480
+
481
+/* returns: -1 on error, number of bytes written on success */
482
+static int send_binrpc_cmd(int s, struct binrpc_cmd* cmd, int cookie)
483
+{
484
+	struct iovec v[IOVEC_CNT];
485
+	int r;
486
+	unsigned char msg_body[MAX_BODY_SIZE];
487
+	unsigned char msg_hdr[BINRPC_MAX_HDR_SIZE];
488
+	struct binrpc_pkt body;
489
+	int ret;
490
+	int n;
491
+	
492
+	ret=binrpc_init_pkt(&body, msg_body, MAX_BODY_SIZE);
493
+	if (ret<0) goto binrpc_err;
494
+	ret=binrpc_addstr(&body, cmd->method, strlen(cmd->method));
495
+	if (ret<0) goto binrpc_err;
496
+	for (r=0; r<cmd->argc; r++){
497
+		switch(cmd->argv[r].type){
498
+			case BINRPC_T_STR:
499
+				ret=binrpc_addstr(&body, cmd->argv[r].u.strval.s,
500
+										cmd->argv[r].u.strval.len);
501
+				break;
502
+			case BINRPC_T_INT:
503
+				ret=binrpc_addint(&body, cmd->argv[r].u.intval);
504
+				break;
505
+			case BINRPC_T_DOUBLE:
506
+				ret=binrpc_adddouble(&body, cmd->argv[r].u.fval);
507
+				break;
508
+			default:
509
+				fprintf(stderr, "ERROR: unsupported type %d\n",
510
+								cmd->argv[r].type);
511
+		}
512
+		if (ret<0) goto binrpc_err;
513
+	}
514
+	ret=binrpc_build_hdr(BINRPC_REQ, binrpc_pkt_len(&body), cookie, msg_hdr,
515
+							BINRPC_MAX_HDR_SIZE);
516
+	if (ret<0) goto binrpc_err;
517
+	v[0].iov_base=msg_hdr;
518
+	v[0].iov_len=ret;
519
+	v[1].iov_base=msg_body;
520
+	v[1].iov_len=binrpc_pkt_len(&body);
521
+write_again:
522
+	if ((n=writev(s, v, 2))<0){
523
+		if (errno==EINTR)
524
+			goto write_again;
525
+		goto error_send;
526
+	}
527
+	
528
+	return n;
529
+error_send:
530
+	return -1;
531
+binrpc_err:
532
+	return -2;
533
+}
534
+
535
+
536
+
537
+/* reads the whole reply
538
+ * returns < 0 on error, reply size on success + initializes in_pkt */
539
+static int get_reply(int s, unsigned char* reply_buf, int max_reply_size,
540
+						int cookie, struct binrpc_parse_ctx* in_pkt,
541
+						unsigned char** body)
542
+{
543
+	unsigned char* crt;
544
+	unsigned char* hdr_end;
545
+	unsigned char* msg_end;
546
+	int n;
547
+	int ret;
548
+	
549
+	
550
+	hdr_end=crt=reply_buf;
551
+	msg_end=reply_buf+max_reply_size;
552
+	do{
553
+		n=read(s, crt, (int)(msg_end-crt));
554
+		if (n<0){
555
+			if (errno==EINTR)
556
+				continue;
557
+			goto error_read;
558
+		}
559
+		if (verbose >= 3){
560
+			/* dump it in hex */
561
+			printf("received %d bytes in reply (@offset %d):\n",
562
+					n, (int)(crt-reply_buf));
563
+			hexdump(crt, n, 1);
564
+		}
565
+		crt+=n;
566
+		/* parse header if not parsed yet */
567
+		if (hdr_end==reply_buf){
568
+			hdr_end=binrpc_parse_init(in_pkt, reply_buf, n, &ret);
569
+			if (ret<0){
570
+				if (ret==E_BINRPC_MORE_DATA)
571
+					continue;
572
+				goto error_parse;
573
+			}
574
+			if (verbose>1){
575
+				printf("new packet: type %02x, len %d, cookie %02x\n",
576
+						in_pkt->type, in_pkt->tlen, in_pkt->cookie);
577
+			}
578
+			if (in_pkt->cookie!=cookie){
579
+				fprintf(stderr, "bad reply, cookie doesn't match: sent %02x "
580
+						"and received  %02x\n",
581
+						cookie, in_pkt->cookie);
582
+				goto error;
583
+			}
584
+			msg_end=hdr_end+in_pkt->tlen;
585
+			if ((int)(msg_end-reply_buf)>max_reply_size)
586
+				goto error_toolong;
587
+		}
588
+	}while(crt<msg_end);
589
+	
590
+	*body=hdr_end;
591
+	return (int)(msg_end-reply_buf);
592
+error_read:
593
+	return -1;
594
+error_parse:
595
+	return -2;
596
+error:
597
+	return -3;
598
+error_toolong:
599
+	return -4;
600
+}
601
+
602
+
603
+
604
+/* returns a malloced copy of str, with all the escapes ('\') resolved */
605
+static char* str_escape(char* str)
606
+{
607
+	char* n;
608
+	char* ret;
609
+	
610
+	ret=n=malloc(strlen(str)+1);
611
+	if (n==0)
612
+		goto end;
613
+	
614
+	for(;*str;str++){
615
+		*n=*str;
616
+		if (*str=='\\'){
617
+			switch(*(str+1)){
618
+				case 'n':
619
+					*n='\n';
620
+					str++;
621
+					break;
622
+				case 'r':
623
+					*n='\r';
624
+					str++;
625
+					break;
626
+				case 't':
627
+					*n='\t';
628
+					str++;
629
+					break;
630
+				case '\\':
631
+					str++;
632
+					break;
633
+			}
634
+		}
635
+		n++;
636
+	}
637
+	*n=*str; /* terminating 0 */
638
+end:
639
+	return ret;
640
+}
641
+
642
+
643
+
644
+/* parses strings like "bla bla %v 10%% %v\n test=%v",
645
+ * and stops at each %v,  returning  a pointer after the %v, setting *size
646
+ * to the string length (not including %v) and *type to the corresponding
647
+ * BINRPC type (for now only BINRPC_T_ALL).
648
+ * To escape a '%', use "%%", and check for type==-1 (which means skip an call
649
+ *  again parse_fmt).
650
+ * Usage:
651
+ *        n="test: %v,%v,%v\n";
652
+ *        while(*n){
653
+ *          s=n;
654
+ *          n=parse_fmt(n, &type, &size);
655
+ *          printf("%.*s", size, s);
656
+ *          if (type==-1)
657
+ *            continue;
658
+ *          else 
659
+ *             printf("now we should get & print an object of type %d\n", type)
660
+ *        }
661
+ */
662
+static char* parse_fmt(char* fmt, int* type, int* size)
663
+{
664
+	char* s;
665
+
666
+	s=fmt;
667
+	do{
668
+		for(;*fmt && *fmt!='%'; fmt++);
669
+		if (*fmt=='%'){
670
+			switch(*(fmt+1)){
671
+				case 'v':
672
+					*type=BINRPC_T_ALL;
673
+					*size=(int)(fmt-s);
674
+					return (fmt+2);
675
+					break;
676
+				case '%':
677
+					/* escaped % */
678
+					*size=(int)(fmt-s)+1;
679
+					*type=-1; /* skip */
680
+					return (fmt+2);
681
+					break;
682
+			}
683
+		}
684
+	}while(*fmt);
685
+	*type=-1; /* no value */
686
+	*size=(fmt-s);
687
+	return fmt;
688
+}
689
+
690
+
691
+
692
+static int print_body(struct binrpc_parse_ctx* in_pkt, 
693
+						unsigned char* body, int size, char* fmt)
694
+{
695
+	
696
+	unsigned char* p;
697
+	unsigned char* end;
698
+	struct binrpc_val val;
699
+	int ret;
700
+	int rec;
701
+	char *f;
702
+	char* s;
703
+	int f_size;
704
+	int fmt_has_values;
705
+	
706
+	p=body;
707
+	end=p+size;
708
+	rec=0;
709
+	f=fmt;
710
+	fmt_has_values=0;
711
+	/* read body */
712
+	while(p<end){
713
+		if (f){
714
+			
715
+			do{
716
+				if (*f==0)
717
+					f=fmt; /* reset */
718
+				s=f;
719
+				f=parse_fmt(f, &val.type, &f_size);
720
+				printf("%.*s", f_size, s);
721
+				if (val.type!=-1){
722
+					fmt_has_values=1;
723
+					goto read_value;
724
+				}
725
+			}while(*f || fmt_has_values);
726
+			val.type=BINRPC_T_ALL;
727
+		}else{
728
+			val.type=BINRPC_T_ALL;
729
+		}
730
+read_value:
731
+		val.name.s=0;
732
+		val.name.len=0;
733
+		p=binrpc_read_record(in_pkt, p, end, &val, &ret);
734
+		if (ret<0){
735
+			if (fmt)
736
+				putchar('\n');
737
+			/*if (ret==E_BINRPC_MORE_DATA)
738
+				goto error_read_again;*/
739
+			if (ret==E_BINRPC_EOP){
740
+				printf("end of message detected\n");
741
+				break;
742
+			}
743
+			fprintf(stderr, "ERROR while parsing the record %d,"
744
+					" @%d: %02x : %s\n", rec,
745
+					in_pkt->offset, *p, binrpc_error(ret));
746
+			goto error;
747
+		}
748
+		rec++;
749
+		if (fmt){
750
+			print_binrpc_val(&val, 0);
751
+		}else{
752
+			print_binrpc_val(&val, in_pkt->in_struct+in_pkt->in_array);
753
+			putchar('\n');
754
+		}
755
+	}
756
+	if (fmt && *f){
757
+		/* print the rest, with empty values */
758
+		while(*f){
759
+			s=f;
760
+			f=parse_fmt(f, &val.type, &f_size);
761
+			printf("%.*s", f_size, s);
762
+		}
763
+	}
764
+	return 0;
765
+error:
766
+	return -1;
767
+/*error_read_again:
768
+	fprintf(stderr, "ERROR: more data needed\n");
769
+	return -2;
770
+	*/
771
+}
772
+
773
+
774
+
775
+static int print_fault(struct binrpc_parse_ctx* in_pkt, 
776
+						unsigned char* body, int size)
777
+{
778
+	printf("error: ");
779
+	return print_body(in_pkt, body, size, "%v - %v\n");
780
+}
781
+
782
+
783
+
784
+static int run_binrpc_cmd(int s, struct binrpc_cmd * cmd, char* fmt)
785
+{
786
+	int cookie;
787
+	unsigned char reply_buf[MAX_REPLY_SIZE];
788
+	unsigned char* msg_body;
789
+	struct binrpc_parse_ctx in_pkt;
790
+	int ret;
791
+	
792
+	cookie=gen_cookie();
793
+	if ((ret=send_binrpc_cmd(s, cmd, cookie))<0){
794
+		if (ret==-1) goto error_send;
795
+		else goto binrpc_err;
796
+	}
797
+	/* read reply */
798
+	memset(&in_pkt, 0, sizeof(in_pkt));
799
+	if ((ret=get_reply(s, reply_buf, MAX_REPLY_SIZE, cookie, &in_pkt,
800
+					&msg_body))<0){
801
+		switch(ret){
802
+			case -1:
803
+				goto error_read;
804
+			case -2:
805
+			case -3:
806
+			case -4:
807
+				goto error_parse;
808
+				goto error_parse;
809
+				goto error_parse;
810
+		}
811
+		goto error;
812
+	}
813
+	switch(in_pkt.type){
814
+		case BINRPC_FAULT:
815
+			if (print_fault(&in_pkt, msg_body, in_pkt.tlen)<0){
816
+				goto error;
817
+			}
818
+			break;
819
+		case BINRPC_REPL:
820
+			if (print_body(&in_pkt, msg_body, in_pkt.tlen, fmt)<0){
821
+				goto error;
822
+			}
823
+			break;
824
+		default:
825
+			fprintf(stderr, "ERROR: not a reply\n");
826
+			goto error;
827
+	}
828
+	if (verbose) printf(".\n");
829
+	/* normal exit */
830
+	return 0;
831
+binrpc_err:
832
+	fprintf(stderr, "ERROR while building the packet: %s\n", 
833
+				binrpc_error(ret));
834
+	goto error;
835
+error_parse:
836
+	fprintf(stderr, "ERROR while parsing the reply: %s\n", 
837
+				binrpc_error(ret));
838
+	goto error;
839
+error_send:
840
+	fprintf(stderr, "ERROR: send packet failed: %s (%d)\n",
841
+			strerror(errno), errno);
842
+	goto error;
843
+error_read:
844
+	fprintf(stderr, "ERROR: read reply failed: %s (%d)\n",
845
+			strerror(errno), errno);
846
+	goto error;
847
+error:
848
+	return -1;
849
+}
850
+
851
+
852
+
853
+static int parse_line(struct binrpc_cmd* cmd, char* line)
854
+{
855
+	char* p;
856
+	int count;
857
+	
858
+	cmd->method=strtok(line, " \t");
859
+	if (cmd->method==0)
860
+		goto error_no_method;
861
+	count=0;
862
+	for(p=strtok(0, " \t"); p; p=strtok(0, " \t")){
863
+		if (count>=MAX_BINRPC_ARGS)
864
+			goto error_too_many;
865
+		if (parse_arg(&cmd->argv[count], p)<0){
866
+			goto error_arg;
867
+		}
868
+		count++;
869
+	}
870
+	cmd->argc=count;
871
+	return 0;
872
+error_no_method:
873
+	printf( "ERROR: no method name\n");
874
+	return -1;
875
+error_too_many:
876
+	printf("ERROR: too many arguments (%d), no more than %d allowed\n",
877
+			count, MAX_BINRPC_ARGS);
878
+	return -1;
879
+error_arg:
880
+	printf("ERROR: bad argument %d: %s\n", count+1, p);
881
+	return -1;
882
+}
883
+
884
+
885
+
886
+/* resolves builtin aliases */
887
+static void fix_cmd(struct binrpc_cmd* cmd, char** format)
888
+{
889
+	int r;
890
+	
891
+	for (r=0; cmd_aliases[r].name; r++){
892
+		if (strcmp(cmd_aliases[r].name, cmd->method)==0){
893
+			cmd->method=cmd_aliases[r].method;
894
+			if (*format==0)
895
+				*format=cmd_aliases[r].format;
896
+			break;
897
+		}
898
+	}
899
+}
900
+
901
+
902
+
903
+/* intercept builtin commands, returns 1 if intercepted, 0 if not, <0 on error
904
+ */
905
+static int run_builtins(int s, struct binrpc_cmd* cmd)
906
+{
907
+	int r;