1 /* $Id: command.c,v 1.4 2006-11-27 19:50:25 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)
95 recs = show(s->psession, 0, &num, &merged, &total);
97 for (i = 0; i < num; i++)
100 struct record *cnode;
101 struct record *r = recs[i];
103 command_puts(s, r->merge_key);
104 for (rc = 1, cnode = r->next_cluster; cnode; cnode = cnode->next_cluster, rc++)
109 sprintf(buf, " (%d records)", rc);
110 command_puts(s, buf);
112 command_puts(s, "\n");
117 static int cmd_stat(struct command_session *s, char **argv, int argc)
120 struct statistics stat;
122 statistics(s->psession, &stat);
123 sprintf(buf, "Number of connections: %d\n", stat.num_connections);
124 command_puts(s, buf);
125 if (stat.num_no_connection)
127 sprintf(buf, "#No_connection: %d\n", stat.num_no_connection);
128 command_puts(s, buf);
130 if (stat.num_connecting)
132 sprintf(buf, "#Connecting: %d\n", stat.num_connecting);
133 command_puts(s, buf);
135 if (stat.num_initializing)
137 sprintf(buf, "#Initializing: %d\n", stat.num_initializing);
138 command_puts(s, buf);
140 if (stat.num_searching)
142 sprintf(buf, "#Searching: %d\n", stat.num_searching);
143 command_puts(s, buf);
145 if (stat.num_presenting)
147 sprintf(buf, "#Ppresenting: %d\n", stat.num_presenting);
148 command_puts(s, buf);
152 sprintf(buf, "#Idle: %d\n", stat.num_idle);
153 command_puts(s, buf);
157 sprintf(buf, "#Failed: %d\n", stat.num_failed);
158 command_puts(s, buf);
162 sprintf(buf, "#Error: %d\n", stat.num_error);
163 command_puts(s, buf);
170 int (*fun)(struct command_session *s, char *argv[], int argc);
174 {"find", cmd_search},
175 {"ht", cmd_hitsbytarget},
181 void command_command(struct command_session *s, char *command)
197 while (*p && !isspace(*p))
204 for (i = 0; cmd_array[i].cmd; i++)
206 if (!strcmp(cmd_array[i].cmd, argv[0])) {
207 res = (cmd_array[i].fun)(s, argv, argc);
213 command_puts(s, "Unknown command.\n");
226 static void command_io(IOCHAN i, int event)
230 struct command_session *s;
232 s = iochan_getdata(i);
238 res = read(iochan_getfd(i), buf, 1024);
241 yaz_log(YLOG_WARN|YLOG_ERRNO, "read command");
242 close(iochan_getfd(i));
247 if (!index(buf, '\n')) {
248 yaz_log(YLOG_WARN|YLOG_ERRNO, "Did not receive complete command");
249 close(iochan_getfd(i));
255 command_command(s, buf);
258 if (!s->outbuflen || s->outbufwrit < 0)
260 yaz_log(YLOG_WARN, "Called with outevent but no data");
261 iochan_clearflag(i, EVENT_OUTPUT);
265 res = write(iochan_getfd(i), s->outbuf + s->outbufwrit, s->outbuflen -
268 yaz_log(YLOG_WARN|YLOG_ERRNO, "write command");
269 close(iochan_getfd(i));
275 s->outbufwrit += res;
276 if (s->outbufwrit >= s->outbuflen)
278 s->outbuflen = s->outbufwrit = 0;
279 iochan_clearflag(i, EVENT_OUTPUT);
285 yaz_log(YLOG_WARN, "Bad voodoo on socket");
289 void command_puts(struct command_session *s, const char *buf)
291 int len = strlen(buf);
292 memcpy(s->outbuf + s->outbuflen, buf, len);
294 iochan_setflag(s->channel, EVENT_OUTPUT);
297 void command_prompt(struct command_session *s)
299 command_puts(s, "Pazpar2> ");
303 /* Accept a new command connection */
304 static void command_accept(IOCHAN i, int event)
306 struct sockaddr_in addr;
307 int fd = iochan_getfd(i);
311 struct command_session *ses;
315 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
317 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
320 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
321 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
322 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
323 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
325 yaz_log(YLOG_LOG, "New command connection");
326 c = iochan_create(s, command_io, EVENT_INPUT | EVENT_EXCEPT);
328 ses = xmalloc(sizeof(*ses));
329 ses->outbuf = xmalloc(50000);
333 ses->psession = new_session();
334 iochan_setdata(c, ses);
336 command_puts(ses, "Welcome to pazpar2\n\n");
339 c->next = channel_list;
343 void command_destroy(struct command_session *s) {
348 /* Create a command-channel listener */
349 void command_init(int port)
354 struct sockaddr_in myaddr;
357 yaz_log(YLOG_LOG, "Command port is %d", port);
358 if (!(p = getprotobyname("tcp"))) {
361 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
362 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
363 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
364 &one, sizeof(one)) < 0)
367 bzero(&myaddr, sizeof myaddr);
368 myaddr.sin_family = AF_INET;
369 myaddr.sin_addr.s_addr = INADDR_ANY;
370 myaddr.sin_port = htons(port);
371 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
372 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
373 if (listen(l, SOMAXCONN) < 0)
374 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
376 c = iochan_create(l, command_accept, EVENT_INPUT | EVENT_EXCEPT);
377 //iochan_setdata(c, &l);
378 c->next = channel_list;
385 * indent-tabs-mode: nil
387 * vim: shiftwidth=4 tabstop=8 expandtab