2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.43 2007-01-03 08:42:17 adam Exp $
9 \brief ZOOM C command line tool (shell)
17 #include <yaz/comstack.h>
19 #if HAVE_READLINE_READLINE_H
20 #include <readline/readline.h>
22 #if HAVE_READLINE_HISTORY_H
23 #include <readline/history.h>
26 #include <yaz/xmalloc.h>
35 static int next_token (const char **cpp, const char **t_start)
38 const char *cp = *cpp;
45 while (*cp && *cp != '"')
56 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
65 return len; /* return -1 if no token was read .. */
68 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
71 int len = next_token (cpp, &start);
79 memcpy (buf_out, start, len);
84 static int is_command (const char *cmd_str, const char *this_str, int this_len)
86 int cmd_len = strlen(cmd_str);
87 if (cmd_len != this_len)
89 if (memcmp (cmd_str, this_str, cmd_len))
94 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
98 char key[40], val[80];
100 if (next_token_copy (args, key, sizeof(key)) < 0)
102 printf ("missing argument for set\n");
105 if (next_token_copy (args, val, sizeof(val)) < 0)
106 ZOOM_options_set(options, key, 0);
108 ZOOM_options_set(options, key, val);
111 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
112 ZOOM_options options,
116 if (next_token_copy (args, key, sizeof(key)) < 0)
118 printf ("missing argument for get\n");
122 const char *val = ZOOM_options_get(options, key);
123 printf ("%s = %s\n", key, val ? val : "<null>");
127 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
128 ZOOM_options options,
132 if (next_token_copy (args, key, sizeof(key)) < 0)
134 printf ("missing argument for get\n");
139 for (i = 0; i<MAX_CON; i++)
145 val = ZOOM_resultset_option_get(r[i], key);
146 printf ("%s = %s\n", key, val ? val : "<null>");
151 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
152 ZOOM_options options,
157 next_token_copy (args, host, sizeof(host));
158 for (i = 0; i<MAX_CON; i++)
163 if ((h = ZOOM_connection_option_get(c[i], "host"))
164 && !strcmp (h, host))
166 ZOOM_connection_destroy (c[i]);
169 else if (*host == '\0')
171 ZOOM_connection_destroy (c[i]);
177 static void display_records (ZOOM_connection c,
179 int start, int count)
182 for (i = 0; i<count; i++)
185 ZOOM_record rec = ZOOM_resultset_record (r, pos);
186 const char *db = ZOOM_record_get (rec, "database", 0);
188 if (ZOOM_record_error(rec, 0, 0, 0))
193 int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
195 printf("%d %s: %s (%s:%d) %s\n", pos, (db ? db : "unknown"),
196 msg, diagset, error, addinfo);
201 const char *render = ZOOM_record_get (rec, "render", &len);
202 const char *opac_render = ZOOM_record_get (rec, "opac", &opac_len);
203 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
204 /* if rec is non-null, we got a record for display */
208 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
209 printf ("%d %s %s (%s)\n",
210 pos, (db ? db : "unknown"), syntax, oidbuf);
212 fwrite (render, 1, len, stdout);
215 fwrite (opac_render, 1, opac_len, stdout);
222 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
223 ZOOM_options options,
227 char start_str[10], count_str[10];
229 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
230 ZOOM_options_set (options, "start", start_str);
232 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
233 ZOOM_options_set (options, "count", count_str);
235 for (i = 0; i<MAX_CON; i++)
236 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
237 while (ZOOM_event (MAX_CON, c))
240 for (i = 0; i<MAX_CON; i++)
243 const char *errmsg, *addinfo, *dset;
244 /* display errors if any */
247 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
248 printf ("%s error: %s (%s:%d) %s\n",
249 ZOOM_connection_option_get(c[i], "host"), errmsg,
250 dset, error, addinfo);
253 /* OK, no major errors. Display records... */
254 int start = ZOOM_options_get_int (options, "start", 0);
255 int count = ZOOM_options_get_int (options, "count", 0);
256 display_records (c[i], r[i], start, count);
259 ZOOM_options_set (options, "count", "0");
260 ZOOM_options_set (options, "start", "0");
263 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
264 ZOOM_options options,
267 ZOOM_package p[MAX_CON];
268 char ext_type_str[10];
272 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
275 for (i = 0; i<MAX_CON; i++)
279 p[i] = ZOOM_connection_package (c[i], 0);
280 ZOOM_package_send(p[i], ext_type_str);
286 while (ZOOM_event (MAX_CON, c))
289 for (i = 0; i<MAX_CON; i++)
292 const char *errmsg, *addinfo, *dset;
293 /* display errors if any */
296 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
297 printf ("%s error: %s (%s:%d) %s\n",
298 ZOOM_connection_option_get(c[i], "host"), errmsg,
299 dset, error, addinfo);
304 v = ZOOM_package_option_get (p[i], "targetReference");
306 printf("targetReference: %s\n", v);
307 v = ZOOM_package_option_get (p[i], "xmlUpdateDoc");
309 printf("xmlUpdateDoc: %s\n", v);
311 ZOOM_package_destroy (p[i]);
315 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
316 ZOOM_options options,
319 yaz_log_init_level(YLOG_ALL);
322 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
323 ZOOM_options options,
327 const char *query_str = *args;
330 s = ZOOM_query_create ();
331 while (*query_str == ' ')
333 if (memcmp(query_str, "cql:", 4) == 0)
335 ZOOM_query_cql (s, query_str + 4);
337 else if (ZOOM_query_prefix (s, query_str))
339 printf ("Bad PQF: %s\n", query_str);
342 for (i = 0; i<MAX_CON; i++)
346 ZOOM_resultset_destroy (r[i]);
350 r[i] = ZOOM_connection_search (c[i], s);
353 while (ZOOM_event (MAX_CON, c))
356 for (i = 0; i<MAX_CON; i++)
359 const char *errmsg, *addinfo, *dset;
360 /* display errors if any */
363 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
364 printf ("%s error: %s (%s:%d) %s\n",
365 ZOOM_connection_option_get(c[i], "host"), errmsg,
366 dset, error, addinfo);
369 /* OK, no major errors. Look at the result count */
370 int start = ZOOM_options_get_int (options, "start", 0);
371 int count = ZOOM_options_get_int (options, "count", 0);
373 printf ("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
374 (long) ZOOM_resultset_size(r[i]));
376 display_records (c[i], r[i], start, count);
379 ZOOM_query_destroy (s);
382 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
383 ZOOM_options options,
386 const char *start_term = *args;
388 ZOOM_scanset s[MAX_CON];
390 while (*start_term == ' ')
393 for (i = 0; i<MAX_CON; i++)
396 s[i] = ZOOM_connection_scan(c[i], start_term);
400 while (ZOOM_event(MAX_CON, c))
402 for (i = 0; i<MAX_CON; i++)
405 size_t p, sz = ZOOM_scanset_size(s[i]);
406 for (p = 0; p < sz; p++)
410 const char *term = ZOOM_scanset_display_term(s[i], p,
412 fwrite(term, 1, len, stdout);
413 printf (" %d\n", occ);
415 ZOOM_scanset_destroy(s[i]);
420 static void cmd_sort (ZOOM_connection *c, ZOOM_resultset *r,
421 ZOOM_options options,
424 const char *sort_spec = *args;
427 while (*sort_spec == ' ')
430 for (i = 0; i<MAX_CON; i++)
433 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
435 while (ZOOM_event(MAX_CON, c))
439 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
440 ZOOM_options options,
443 printf ("connect <zurl>\n");
444 printf ("search <pqf>\n");
445 printf ("show [<start> [<count>]\n");
446 printf ("scan <term>\n");
448 printf ("close <zurl>\n");
449 printf ("ext <type>\n");
450 printf ("set <option> [<value>]\n");
451 printf ("get <option>\n");
453 printf ("options:\n");
456 printf (" databaseName\n");
457 printf (" preferredRecordSyntax\n");
459 printf (" elementSetName\n");
460 printf (" maximumRecordSize\n");
461 printf (" preferredRecordSize\n");
463 printf (" piggyback\n");
466 printf (" password\n");
467 printf (" implementationName\n");
468 printf (" charset\n");
472 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
473 ZOOM_options options,
477 const char *errmsg, *addinfo, *dset;
480 if (next_token_copy (args, host, sizeof(host)) < 0)
482 printf ("missing host after connect\n");
485 for (j = -1, i = 0; i<MAX_CON; i++)
488 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
491 ZOOM_connection_destroy (c[i]);
494 else if (c[i] == 0 && j == -1)
497 if (i == MAX_CON) /* no match .. */
501 printf ("no more connection available\n");
504 i = j; /* OK, use this one is available */
506 c[i] = ZOOM_connection_create (options);
507 ZOOM_connection_connect (c[i], host, 0);
509 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
510 printf ("%s error: %s (%s:%d) %s\n",
511 ZOOM_connection_option_get(c[i], "host"), errmsg,
512 dset, error, addinfo);
515 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
516 ZOOM_options options,
522 cmd_len = next_token (buf, &cmd_str);
525 if (is_command ("quit", cmd_str, cmd_len))
527 else if (is_command ("set", cmd_str, cmd_len))
528 cmd_set (c, r, options, buf);
529 else if (is_command ("get", cmd_str, cmd_len))
530 cmd_get (c, r, options, buf);
531 else if (is_command ("rget", cmd_str, cmd_len))
532 cmd_rget (c, r, options, buf);
533 else if (is_command ("connect", cmd_str, cmd_len))
534 cmd_connect (c, r, options, buf);
535 else if (is_command ("open", cmd_str, cmd_len))
536 cmd_connect (c, r, options, buf);
537 else if (is_command ("search", cmd_str, cmd_len))
538 cmd_search (c, r, options, buf);
539 else if (is_command ("find", cmd_str, cmd_len))
540 cmd_search (c, r, options, buf);
541 else if (is_command ("show", cmd_str, cmd_len))
542 cmd_show (c, r, options, buf);
543 else if (is_command ("close", cmd_str, cmd_len))
544 cmd_close (c, r, options, buf);
545 else if (is_command ("help", cmd_str, cmd_len))
546 cmd_help(c, r, options, buf);
547 else if (is_command ("ext", cmd_str, cmd_len))
548 cmd_ext(c, r, options, buf);
549 else if (is_command ("debug", cmd_str, cmd_len))
550 cmd_debug(c, r, options, buf);
551 else if (is_command ("scan", cmd_str, cmd_len))
552 cmd_scan(c, r, options, buf);
553 else if (is_command ("sort", cmd_str, cmd_len))
554 cmd_sort(c, r, options, buf);
556 printf ("unknown command %.*s\n", cmd_len, cmd_str);
560 void shell(ZOOM_connection *c, ZOOM_resultset *r,
561 ZOOM_options options)
567 const char *bp = buf;
568 #if HAVE_READLINE_READLINE_H
570 line_in=readline("ZOOM>");
573 #if HAVE_READLINE_HISTORY_H
575 add_history(line_in);
577 if(strlen(line_in) > 999) {
578 printf("Input line too long\n");
584 printf ("ZOOM>"); fflush (stdout);
585 if (!fgets (buf, 999, stdin))
588 if ((cp = strchr(buf, '\n')))
590 if (!cmd_parse (c, r, options, &bp))
595 static void zoomsh(int argc, char **argv)
597 ZOOM_options options = ZOOM_options_create();
599 ZOOM_connection z39_con[MAX_CON];
600 ZOOM_resultset z39_res[MAX_CON];
602 for (i = 0; i<MAX_CON; i++)
608 for (i = 0; i<MAX_CON; i++)
612 for (i = 1; i<argc; i++)
614 const char *bp = argv[i];
615 res = cmd_parse(z39_con, z39_res, options, &bp);
616 if (res == 0) /* received quit */
619 if (res) /* do cmdline shell only if not quitting */
620 shell(z39_con, z39_res, options);
621 ZOOM_options_destroy(options);
623 for (i = 0; i<MAX_CON; i++)
625 ZOOM_connection_destroy(z39_con[i]);
626 ZOOM_resultset_destroy(z39_res[i]);
630 int main(int argc, char **argv)
632 const char *maskstr = 0;
633 if (argc > 2 && !strcmp(argv[1], "-v"))
639 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
647 int mask = yaz_log_mask_str(maskstr);
648 yaz_log_init_level(mask);
658 * indent-tabs-mode: nil
660 * vim: shiftwidth=4 tabstop=8 expandtab