1 /* $Id: command.c,v 1.5 2006-12-03 06:43:24 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));
48 static int cmd_load(struct command_session *s, char **argv, int argc)
51 command_puts(s, "Usage: load filename\n");
53 if (load_targets(s->psession, argv[1]) < 0)
54 command_puts(s, "Failed to open file\n");
59 static int cmd_search(struct command_session *s, char **argv, int argc)
63 command_puts(s, "Usage: search word\n");
66 search(s->psession, argv[1]);
70 static int cmd_hitsbytarget(struct command_session *s, char **argv, int argc)
75 struct hitsbytarget *ht = hitsbytarget(s->psession, &count);
76 for (i = 0; i < count; i++)
80 sprintf(buf, "%s: %d (%d records, diag=%d, state=%s conn=%d)\n", ht[i].id, ht[i].hits,
81 ht[i].records, ht[i].diagnostic, ht[i].state, ht[i].connected);
87 static int cmd_show(struct command_session *s, char **argv, int argc)
97 recs = show(s->psession, 0, &num, &merged, &total);
99 for (i = 0; i < num; i++)
102 struct record *cnode;
103 struct record *r = recs[i];
105 command_puts(s, r->merge_key);
106 for (rc = 1, cnode = r->next_cluster; cnode; cnode = cnode->next_cluster, rc++)
111 sprintf(buf, " (%d records)", rc);
112 command_puts(s, buf);
114 command_puts(s, "\n");
119 static int cmd_stat(struct command_session *s, char **argv, int argc)
122 struct statistics stat;
124 statistics(s->psession, &stat);
125 sprintf(buf, "Number of connections: %d\n", stat.num_clients);
126 command_puts(s, buf);
127 if (stat.num_no_connection)
129 sprintf(buf, "#No_connection: %d\n", stat.num_no_connection);
130 command_puts(s, buf);
132 if (stat.num_connecting)
134 sprintf(buf, "#Connecting: %d\n", stat.num_connecting);
135 command_puts(s, buf);
137 if (stat.num_initializing)
139 sprintf(buf, "#Initializing: %d\n", stat.num_initializing);
140 command_puts(s, buf);
142 if (stat.num_searching)
144 sprintf(buf, "#Searching: %d\n", stat.num_searching);
145 command_puts(s, buf);
147 if (stat.num_presenting)
149 sprintf(buf, "#Ppresenting: %d\n", stat.num_presenting);
150 command_puts(s, buf);
154 sprintf(buf, "#Idle: %d\n", stat.num_idle);
155 command_puts(s, buf);
159 sprintf(buf, "#Failed: %d\n", stat.num_failed);
160 command_puts(s, buf);
164 sprintf(buf, "#Error: %d\n", stat.num_error);
165 command_puts(s, buf);
172 int (*fun)(struct command_session *s, char *argv[], int argc);
178 {"find", cmd_search},
179 {"ht", cmd_hitsbytarget},
185 void command_command(struct command_session *s, char *command)
201 while (*p && !isspace(*p))
208 for (i = 0; cmd_array[i].cmd; i++)
210 if (!strcmp(cmd_array[i].cmd, argv[0])) {
211 res = (cmd_array[i].fun)(s, argv, argc);
217 command_puts(s, "Unknown command.\n");
230 static void command_io(IOCHAN i, int event)
234 struct command_session *s;
236 s = iochan_getdata(i);
242 res = read(iochan_getfd(i), buf, 1024);
245 yaz_log(YLOG_WARN|YLOG_ERRNO, "read command");
246 close(iochan_getfd(i));
251 if (!index(buf, '\n')) {
252 yaz_log(YLOG_WARN|YLOG_ERRNO, "Did not receive complete command");
253 close(iochan_getfd(i));
259 command_command(s, buf);
262 if (!s->outbuflen || s->outbufwrit < 0)
264 yaz_log(YLOG_WARN, "Called with outevent but no data");
265 iochan_clearflag(i, EVENT_OUTPUT);
269 res = write(iochan_getfd(i), s->outbuf + s->outbufwrit, s->outbuflen -
272 yaz_log(YLOG_WARN|YLOG_ERRNO, "write command");
273 close(iochan_getfd(i));
279 s->outbufwrit += res;
280 if (s->outbufwrit >= s->outbuflen)
282 s->outbuflen = s->outbufwrit = 0;
283 iochan_clearflag(i, EVENT_OUTPUT);
289 yaz_log(YLOG_WARN, "Bad voodoo on socket");
293 void command_puts(struct command_session *s, const char *buf)
295 int len = strlen(buf);
296 memcpy(s->outbuf + s->outbuflen, buf, len);
298 iochan_setflag(s->channel, EVENT_OUTPUT);
301 void command_prompt(struct command_session *s)
303 command_puts(s, "Pazpar2> ");
307 /* Accept a new command connection */
308 static void command_accept(IOCHAN i, int event)
310 struct sockaddr_in addr;
311 int fd = iochan_getfd(i);
315 struct command_session *ses;
319 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
321 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
324 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
325 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
326 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
327 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
329 yaz_log(YLOG_LOG, "New command connection");
330 c = iochan_create(s, command_io, EVENT_INPUT | EVENT_EXCEPT);
332 ses = xmalloc(sizeof(*ses));
333 ses->outbuf = xmalloc(50000);
337 ses->psession = new_session();
338 iochan_setdata(c, ses);
340 command_puts(ses, "Welcome to pazpar2\n\n");
343 c->next = channel_list;
347 void command_destroy(struct command_session *s) {
352 /* Create a command-channel listener */
353 void command_init(int port)
358 struct sockaddr_in myaddr;
361 yaz_log(YLOG_LOG, "Command port is %d", port);
362 if (!(p = getprotobyname("tcp"))) {
365 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
366 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
367 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
368 &one, sizeof(one)) < 0)
371 bzero(&myaddr, sizeof myaddr);
372 myaddr.sin_family = AF_INET;
373 myaddr.sin_addr.s_addr = INADDR_ANY;
374 myaddr.sin_port = htons(port);
375 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
376 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
377 if (listen(l, SOMAXCONN) < 0)
378 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
380 c = iochan_create(l, command_accept, EVENT_INPUT | EVENT_EXCEPT);
381 //iochan_setdata(c, &l);
382 c->next = channel_list;
389 * indent-tabs-mode: nil
391 * vim: shiftwidth=4 tabstop=8 expandtab