1 /* $Id: command.c,v 1.2 2006-11-18 05:00:38 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_show(struct command_session *s, char **argv, int argc)
94 recs = show(s->psession, 0, &num);
96 for (i = 0; i < num; i++)
100 struct record *r = recs[i];
102 command_puts(s, r->merge_key);
103 for (rc = 1, cnode = r->next_cluster; cnode; cnode = cnode->next_cluster, rc++)
108 sprintf(buf, " (%d records)", rc);
109 command_puts(s, buf);
111 command_puts(s, "\n");
116 static int cmd_stat(struct command_session *s, char **argv, int argc)
119 struct statistics stat;
121 statistics(s->psession, &stat);
122 sprintf(buf, "Number of connections: %d\n", stat.num_connections);
123 command_puts(s, buf);
124 if (stat.num_no_connection)
126 sprintf(buf, "#No_connection: %d\n", stat.num_no_connection);
127 command_puts(s, buf);
129 if (stat.num_connecting)
131 sprintf(buf, "#Connecting: %d\n", stat.num_connecting);
132 command_puts(s, buf);
134 if (stat.num_initializing)
136 sprintf(buf, "#Initializing: %d\n", stat.num_initializing);
137 command_puts(s, buf);
139 if (stat.num_searching)
141 sprintf(buf, "#Searching: %d\n", stat.num_searching);
142 command_puts(s, buf);
144 if (stat.num_presenting)
146 sprintf(buf, "#Ppresenting: %d\n", stat.num_presenting);
147 command_puts(s, buf);
151 sprintf(buf, "#Idle: %d\n", stat.num_idle);
152 command_puts(s, buf);
156 sprintf(buf, "#Failed: %d\n", stat.num_failed);
157 command_puts(s, buf);
161 sprintf(buf, "#Error: %d\n", stat.num_error);
162 command_puts(s, buf);
169 int (*fun)(struct command_session *s, char *argv[], int argc);
173 {"find", cmd_search},
174 {"ht", cmd_hitsbytarget},
180 void command_command(struct command_session *s, char *command)
196 while (*p && !isspace(*p))
203 for (i = 0; cmd_array[i].cmd; i++)
205 if (!strcmp(cmd_array[i].cmd, argv[0])) {
206 res = (cmd_array[i].fun)(s, argv, argc);
212 command_puts(s, "Unknown command.\n");
225 static void command_io(IOCHAN i, int event)
229 struct command_session *s;
231 s = iochan_getdata(i);
237 res = read(iochan_getfd(i), buf, 1024);
240 yaz_log(YLOG_WARN|YLOG_ERRNO, "read command");
241 close(iochan_getfd(i));
246 if (!index(buf, '\n')) {
247 yaz_log(YLOG_WARN|YLOG_ERRNO, "Did not receive complete command");
248 close(iochan_getfd(i));
254 command_command(s, buf);
257 if (!s->outbuflen || s->outbufwrit < 0)
259 yaz_log(YLOG_WARN, "Called with outevent but no data");
260 iochan_clearflag(i, EVENT_OUTPUT);
264 res = write(iochan_getfd(i), s->outbuf + s->outbufwrit, s->outbuflen -
267 yaz_log(YLOG_WARN|YLOG_ERRNO, "write command");
268 close(iochan_getfd(i));
274 s->outbufwrit += res;
275 if (s->outbufwrit >= s->outbuflen)
277 s->outbuflen = s->outbufwrit = 0;
278 iochan_clearflag(i, EVENT_OUTPUT);
284 yaz_log(YLOG_WARN, "Bad voodoo on socket");
288 void command_puts(struct command_session *s, const char *buf)
290 int len = strlen(buf);
291 memcpy(s->outbuf + s->outbuflen, buf, len);
293 iochan_setflag(s->channel, EVENT_OUTPUT);
296 void command_prompt(struct command_session *s)
298 command_puts(s, "Pazpar2> ");
302 /* Accept a new command connection */
303 static void command_accept(IOCHAN i, int event)
305 struct sockaddr_in addr;
306 int fd = iochan_getfd(i);
310 struct command_session *ses;
314 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
316 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
319 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
320 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
321 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
322 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
324 yaz_log(YLOG_LOG, "New command connection");
325 c = iochan_create(s, command_io, EVENT_INPUT | EVENT_EXCEPT);
327 ses = xmalloc(sizeof(*ses));
328 ses->outbuf = xmalloc(50000);
332 ses->psession = new_session();
333 iochan_setdata(c, ses);
335 command_puts(ses, "Welcome to pazpar2\n\n");
338 c->next = channel_list;
342 void command_destroy(struct command_session *s) {
347 /* Create a command-channel listener */
348 void command_init(int port)
353 struct sockaddr_in myaddr;
355 yaz_log(YLOG_LOG, "Command port is %d", port);
356 if (!(p = getprotobyname("tcp"))) {
359 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
360 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
361 bzero(&myaddr, sizeof myaddr);
362 myaddr.sin_family = AF_INET;
363 myaddr.sin_addr.s_addr = INADDR_ANY;
364 myaddr.sin_port = port;
365 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
366 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
367 if (listen(l, 5) < 0)
368 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
370 c = iochan_create(l, command_accept, EVENT_INPUT | EVENT_EXCEPT);
371 //iochan_setdata(c, &l);
372 c->next = channel_list;
379 * indent-tabs-mode: nil
381 * vim: shiftwidth=4 tabstop=8 expandtab