1 /* $Id: command.c,v 1.1 2006-11-14 20:44:37 quinn Exp $ */
4 #include <sys/socket.h>
13 #include <yaz/yaz-util.h>
14 #include <yaz/comstack.h>
22 extern IOCHAN channel_list;
24 struct command_session {
31 struct session *psession;
34 void command_destroy(struct command_session *s);
35 void command_prompt(struct command_session *s);
36 void command_puts(struct command_session *s, const char *buf);
38 static int cmd_quit(struct command_session *s, char **argv, int argc)
40 IOCHAN i = s->channel;
41 close(iochan_getfd(i));
47 static int cmd_load(struct command_session *s, char **argv, int argc)
50 command_puts(s, "Usage: load filename\n");
52 if (load_targets(s->psession, argv[1]) < 0)
53 command_puts(s, "Failed to open file\n");
57 static int cmd_search(struct command_session *s, char **argv, int argc)
61 command_puts(s, "Usage: search word\n");
64 search(s->psession, argv[1]);
68 static int cmd_hitsbytarget(struct command_session *s, char **argv, int argc)
73 struct hitsbytarget *ht = hitsbytarget(s->psession, &count);
74 for (i = 0; i < count; i++)
78 sprintf(buf, "%s: %d (%d records, diag=%d, state=%s)\n", ht[i].id, ht[i].hits,
79 ht[i].records, ht[i].diagnostic, ht[i].state);
85 static int cmd_stat(struct command_session *s, char **argv, int argc)
88 struct statistics stat;
90 statistics(s->psession, &stat);
91 sprintf(buf, "Number of connections: %d\n", stat.num_connections);
93 if (stat.num_no_connection)
95 sprintf(buf, "#No_connection: %d\n", stat.num_no_connection);
98 if (stat.num_connecting)
100 sprintf(buf, "#Connecting: %d\n", stat.num_connecting);
101 command_puts(s, buf);
103 if (stat.num_initializing)
105 sprintf(buf, "#Initializing: %d\n", stat.num_initializing);
106 command_puts(s, buf);
108 if (stat.num_searching)
110 sprintf(buf, "#Searching: %d\n", stat.num_searching);
111 command_puts(s, buf);
113 if (stat.num_presenting)
115 sprintf(buf, "#Ppresenting: %d\n", stat.num_presenting);
116 command_puts(s, buf);
120 sprintf(buf, "#Idle: %d\n", stat.num_idle);
121 command_puts(s, buf);
125 sprintf(buf, "#Failed: %d\n", stat.num_failed);
126 command_puts(s, buf);
130 sprintf(buf, "#Error: %d\n", stat.num_error);
131 command_puts(s, buf);
138 int (*fun)(struct command_session *s, char *argv[], int argc);
142 {"search", cmd_search},
143 {"ht", cmd_hitsbytarget},
148 void command_command(struct command_session *s, char *command)
164 while (*p && !isspace(*p))
171 for (i = 0; cmd_array[i].cmd; i++)
173 if (!strcmp(cmd_array[i].cmd, argv[0])) {
174 res = (cmd_array[i].fun)(s, argv, argc);
180 command_puts(s, "Unknown command.\n");
193 static void command_io(IOCHAN i, int event)
197 struct command_session *s;
199 s = iochan_getdata(i);
205 res = read(iochan_getfd(i), buf, 1024);
208 yaz_log(YLOG_WARN|YLOG_ERRNO, "read command");
209 close(iochan_getfd(i));
214 if (!index(buf, '\n')) {
215 yaz_log(YLOG_WARN|YLOG_ERRNO, "Did not receive complete command");
216 close(iochan_getfd(i));
222 command_command(s, buf);
225 if (!s->outbuflen || s->outbufwrit < 0)
227 yaz_log(YLOG_WARN, "Called with outevent but no data");
228 iochan_clearflag(i, EVENT_OUTPUT);
232 res = write(iochan_getfd(i), s->outbuf + s->outbufwrit, s->outbuflen -
235 yaz_log(YLOG_WARN|YLOG_ERRNO, "write command");
236 close(iochan_getfd(i));
242 s->outbufwrit += res;
243 if (s->outbufwrit >= s->outbuflen)
245 s->outbuflen = s->outbufwrit = 0;
246 iochan_clearflag(i, EVENT_OUTPUT);
252 yaz_log(YLOG_WARN, "Bad voodoo on socket");
256 void command_puts(struct command_session *s, const char *buf)
258 int len = strlen(buf);
259 memcpy(s->outbuf + s->outbuflen, buf, len);
261 iochan_setflag(s->channel, EVENT_OUTPUT);
264 void command_prompt(struct command_session *s)
266 command_puts(s, "Pazpar2> ");
270 /* Accept a new command connection */
271 static void command_accept(IOCHAN i, int event)
273 struct sockaddr_in addr;
274 int fd = iochan_getfd(i);
278 struct command_session *ses;
282 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
284 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
287 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
288 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
289 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
290 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
292 yaz_log(YLOG_LOG, "New command connection");
293 c = iochan_create(s, command_io, EVENT_INPUT | EVENT_EXCEPT);
295 ses = xmalloc(sizeof(*ses));
296 ses->outbuf = xmalloc(50000);
300 ses->psession = new_session();
301 iochan_setdata(c, ses);
303 command_puts(ses, "Welcome to pazpar2\n\n");
306 c->next = channel_list;
310 void command_destroy(struct command_session *s) {
315 /* Create a command-channel listener */
316 void command_init(int port)
321 struct sockaddr_in myaddr;
323 yaz_log(YLOG_LOG, "Command port is %d", port);
324 if (!(p = getprotobyname("tcp"))) {
327 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
328 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
329 bzero(&myaddr, sizeof myaddr);
330 myaddr.sin_family = AF_INET;
331 myaddr.sin_addr.s_addr = INADDR_ANY;
332 myaddr.sin_port = port;
333 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
334 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
335 if (listen(l, 5) < 0)
336 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
338 c = iochan_create(l, command_accept, EVENT_INPUT | EVENT_EXCEPT);
339 //iochan_setdata(c, &l);
340 c->next = channel_list;
347 * indent-tabs-mode: nil
349 * vim: shiftwidth=4 tabstop=8 expandtab