1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 \brief ZOOM C command line tool (shell)
14 #include <yaz/wrbuf.h>
16 #if HAVE_READLINE_READLINE_H
17 #include <readline/readline.h>
19 #if HAVE_READLINE_HISTORY_H
20 #include <readline/history.h>
28 static int next_token(const char **cpp, const char **t_start)
31 const char *cp = *cpp;
38 while (*cp && *cp != '"')
49 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
58 return len; /* return -1 if no token was read .. */
61 static WRBUF next_token_new_wrbuf(const char **cpp)
65 int len = next_token(cpp, &start);
70 wrbuf_write(w, start, len);
74 static int is_command(const char *cmd_str, const char *this_str, int this_len)
76 int cmd_len = strlen(cmd_str);
77 if (cmd_len != this_len)
79 if (memcmp(cmd_str, this_str, cmd_len))
84 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
90 if (!(key = next_token_new_wrbuf(args)))
92 printf("missing argument for set\n");
95 if ((val = next_token_new_wrbuf(args)))
97 ZOOM_options_set(options, wrbuf_cstr(key), wrbuf_cstr(val));
101 ZOOM_options_set(options, wrbuf_cstr(key), 0);
105 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
106 ZOOM_options options,
110 if (!(key = next_token_new_wrbuf(args)))
112 printf("missing argument for get\n");
116 const char *val = ZOOM_options_get(options, wrbuf_cstr(key));
117 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
122 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
123 ZOOM_options options,
127 if (!(key = next_token_new_wrbuf(args)))
129 printf("missing argument for get\n");
134 for (i = 0; i<MAX_CON; i++)
140 val = ZOOM_resultset_option_get(r[i], wrbuf_cstr(key));
141 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
147 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
148 ZOOM_options options,
153 host = next_token_new_wrbuf(args);
154 for (i = 0; i<MAX_CON; i++)
161 ZOOM_connection_destroy(c[i]);
164 else if ((h = ZOOM_connection_option_get(c[i], "host"))
165 && !strcmp(h, wrbuf_cstr(host)))
167 ZOOM_connection_destroy(c[i]);
175 static void display_records(ZOOM_connection c,
177 size_t start, size_t count, const char *type)
180 for (i = 0; i < count; i++)
182 size_t pos = i + start;
183 ZOOM_record rec = ZOOM_resultset_record(r, pos);
184 const char *db = ZOOM_record_get(rec, "database", 0);
186 if (ZOOM_record_error(rec, 0, 0, 0))
191 int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
193 printf("%lld %s: %s (%s:%d) %s\n", (long long) pos,
194 (db ? db : "unknown"),
195 msg, diagset, error, addinfo ? addinfo : "none");
200 const char *render = ZOOM_record_get(rec, type, &len);
201 const char *syntax = ZOOM_record_get(rec, "syntax", 0);
202 const char *schema = ZOOM_record_get(rec, "schema", 0);
203 /* if rec is non-null, we got a record for display */
206 printf("%lld database=%s syntax=%s schema=%s\n",
207 (long long) pos, (db ? db : "unknown"), syntax,
208 schema ? schema : "unknown");
211 if (fwrite(render, 1, len, stdout) != (size_t) len)
213 printf("write to stdout failed\n");
222 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
223 ZOOM_options options,
227 size_t start = 0, count = 1;
228 const char *type = "render";
229 WRBUF render_str = 0;
234 if ((tmp = next_token_new_wrbuf(args)))
236 start = atoi(wrbuf_cstr(tmp));
240 if ((tmp = next_token_new_wrbuf(args)))
242 count = atoi(wrbuf_cstr(tmp));
245 render_str = next_token_new_wrbuf(args);
248 type = wrbuf_cstr(render_str);
250 for (i = 0; i < MAX_CON; i++)
251 ZOOM_resultset_records(r[i], 0, start, count);
252 while (ZOOM_event(MAX_CON, c))
255 for (i = 0; i < MAX_CON; i++)
258 const char *errmsg, *addinfo, *dset;
259 /* display errors if any */
262 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
263 printf("%s error: %s (%s:%d) %s\n",
264 ZOOM_connection_option_get(c[i], "host"), errmsg,
265 dset, error, addinfo);
268 /* OK, no major errors. Display records... */
269 display_records(c[i], r[i], start, count, type);
273 wrbuf_destroy(render_str);
277 static void display_facets(ZOOM_facet_field *facets, int count) {
279 printf("Facets: \n");
280 for (index = 0; index < count; index++) {
282 const char *facet_name = ZOOM_facet_field_name(facets[index]);
283 printf(" %s: \n", facet_name);
284 for (term_index = 0; term_index < ZOOM_facet_field_term_count(facets[index]); term_index++) {
286 const char *term = ZOOM_facet_field_get_term(facets[index], term_index, &freq);
287 printf(" %s(%d) \n", term, freq);
292 static void cmd_facets(ZOOM_connection *c, ZOOM_resultset *r,
293 ZOOM_options options,
297 size_t start = 0, count = 1;
298 const char *type = "render";
299 WRBUF render_str = 0;
305 if ((tmp = next_token_new_wrbuf(args)))
307 start = atoi(wrbuf_cstr(tmp));
311 if ((tmp = next_token_new_wrbuf(args)))
313 count = atoi(wrbuf_cstr(tmp));
316 render_str = next_token_new_wrbuf(args);
319 type = wrbuf_cstr(render_str);
322 for (i = 0; i < MAX_CON; i++) {
323 int num_facets = ZOOM_resultset_facet_size(r[i]);
324 ZOOM_resultset_records(r[i], 0, start, count);
327 while (ZOOM_event(MAX_CON, c))
330 for (i = 0; i < MAX_CON; i++)
333 const char *errmsg, *addinfo, *dset;
334 /* display errors if any */
337 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
338 printf("%s error: %s (%s:%d) %s\n",
339 ZOOM_connection_option_get(c[i], "host"), errmsg,
340 dset, error, addinfo);
343 int num_facets = ZOOM_resultset_facets_size(r[i]);
345 ZOOM_facet_field *facets = ZOOM_resultset_facets(r[i]);
346 display_facets(facets, num_facets);
351 wrbuf_destroy(render_str);
355 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
356 ZOOM_options options,
359 ZOOM_package p[MAX_CON];
361 WRBUF ext_type_str = next_token_new_wrbuf(args);
363 for (i = 0; i<MAX_CON; i++)
367 p[i] = ZOOM_connection_package(c[i], 0);
368 ZOOM_package_send(p[i], ext_type_str ? wrbuf_cstr(ext_type_str):0);
374 while (ZOOM_event(MAX_CON, c))
377 for (i = 0; i<MAX_CON; i++)
380 const char *errmsg, *addinfo, *dset;
381 /* display errors if any */
384 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
385 printf("%s error: %s (%s:%d) %s\n",
386 ZOOM_connection_option_get(c[i], "host"), errmsg,
387 dset, error, addinfo);
392 v = ZOOM_package_option_get(p[i], "targetReference");
394 printf("targetReference: %s\n", v);
395 v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
397 printf("xmlUpdateDoc: %s\n", v);
399 ZOOM_package_destroy(p[i]);
402 wrbuf_destroy(ext_type_str);
405 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
406 ZOOM_options options,
409 yaz_log_init_level(YLOG_ALL);
412 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
413 ZOOM_options options,
417 const char *query_str = *args;
420 s = ZOOM_query_create();
421 while (*query_str == ' ')
423 if (memcmp(query_str, "cql:", 4) == 0)
425 ZOOM_query_cql(s, query_str + 4);
427 else if (ZOOM_query_prefix(s, query_str))
429 printf("Bad PQF: %s\n", query_str);
432 for (i = 0; i<MAX_CON; i++)
437 ZOOM_resultset_destroy(r[i]);
441 r[i] = ZOOM_connection_search(c[i], s);
443 ZOOM_query_destroy(s);
445 while (ZOOM_event(MAX_CON, c))
448 for (i = 0; i<MAX_CON; i++)
451 const char *errmsg, *addinfo, *dset;
452 /* display errors if any */
455 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
456 printf("%s error: %s (%s:%d) %s\n",
457 ZOOM_connection_option_get(c[i], "host"), errmsg,
458 dset, error, addinfo);
461 /* OK, no major errors. Look at the result count */
462 int start = ZOOM_options_get_int(options, "start", 0);
463 int count = ZOOM_options_get_int(options, "count", 0);
465 printf("%s: %lld hits\n", ZOOM_connection_option_get(c[i], "host"),
466 (long long int) ZOOM_resultset_size(r[i]));
468 display_records(c[i], r[i], start, count, "render");
473 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
474 ZOOM_options options,
477 const char *query_str = *args;
478 ZOOM_query query = ZOOM_query_create();
480 ZOOM_scanset s[MAX_CON];
482 while (*query_str == ' ')
485 if (memcmp(query_str, "cql:", 4) == 0)
487 ZOOM_query_cql(query, query_str + 4);
489 else if (ZOOM_query_prefix(query, query_str))
491 printf("Bad PQF: %s\n", query_str);
495 for (i = 0; i<MAX_CON; i++)
498 s[i] = ZOOM_connection_scan1(c[i], query);
502 ZOOM_query_destroy(query);
504 while (ZOOM_event(MAX_CON, c))
506 for (i = 0; i<MAX_CON; i++)
509 const char *errmsg, *addinfo, *dset;
510 /* display errors if any */
513 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
514 printf("%s error: %s (%s:%d) %s\n",
515 ZOOM_connection_option_get(c[i], "host"), errmsg,
516 dset, error, addinfo);
519 size_t p, sz = ZOOM_scanset_size(s[i]);
520 for (p = 0; p < sz; p++)
524 const char *term = ZOOM_scanset_display_term(s[i], p,
526 printf("%.*s %lld\n", (int) len, term, (long long int) occ);
528 ZOOM_scanset_destroy(s[i]);
533 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
534 ZOOM_options options,
537 const char *sort_spec = *args;
540 while (*sort_spec == ' ')
543 for (i = 0; i<MAX_CON; i++)
546 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
548 while (ZOOM_event(MAX_CON, c))
552 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
553 ZOOM_options options,
556 printf("connect <zurl>\n");
557 printf("search <pqf>\n");
558 printf("show [<start> [<count> [<type]]]\n");
560 printf("scan <term>\n");
562 printf("close <zurl>\n");
563 printf("ext <type>\n");
564 printf("set <option> [<value>]\n");
565 printf("get <option>\n");
567 printf("options:\n");
570 printf(" databaseName\n");
571 printf(" preferredRecordSyntax\n");
573 printf(" elementSetName\n");
574 printf(" maximumRecordSize\n");
575 printf(" preferredRecordSize\n");
577 printf(" piggyback\n");
580 printf(" password\n");
581 printf(" implementationName\n");
582 printf(" charset\n");
584 printf(" timeout\n");
588 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
589 ZOOM_options options,
593 const char *errmsg, *addinfo, *dset;
595 WRBUF host = next_token_new_wrbuf(args);
598 printf("missing host after connect\n");
601 for (j = -1, i = 0; i<MAX_CON; i++)
604 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
605 !strcmp(h, wrbuf_cstr(host)))
607 ZOOM_connection_destroy(c[i]);
610 else if (c[i] == 0 && j == -1)
613 if (i == MAX_CON) /* no match .. */
617 printf("no more connection available\n");
621 i = j; /* OK, use this one is available */
623 c[i] = ZOOM_connection_create(options);
624 ZOOM_connection_connect(c[i], wrbuf_cstr(host), 0);
626 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
627 printf("%s error: %s (%s:%d) %s\n",
628 ZOOM_connection_option_get(c[i], "host"), errmsg,
629 dset, error, addinfo);
633 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
634 ZOOM_options options,
640 cmd_len = next_token(buf, &cmd_str);
643 if (is_command("quit", cmd_str, cmd_len))
645 else if (is_command("set", cmd_str, cmd_len))
646 cmd_set(c, r, options, buf);
647 else if (is_command("get", cmd_str, cmd_len))
648 cmd_get(c, r, options, buf);
649 else if (is_command("rget", cmd_str, cmd_len))
650 cmd_rget(c, r, options, buf);
651 else if (is_command("connect", cmd_str, cmd_len))
652 cmd_connect(c, r, options, buf);
653 else if (is_command("open", cmd_str, cmd_len))
654 cmd_connect(c, r, options, buf);
655 else if (is_command("search", cmd_str, cmd_len))
656 cmd_search(c, r, options, buf);
657 else if (is_command("facets", cmd_str, cmd_len))
658 cmd_facets(c, r, options, buf);
659 else if (is_command("find", cmd_str, cmd_len))
660 cmd_search(c, r, options, buf);
661 else if (is_command("show", cmd_str, cmd_len))
662 cmd_show(c, r, options, buf);
663 else if (is_command("close", cmd_str, cmd_len))
664 cmd_close(c, r, options, buf);
665 else if (is_command("help", cmd_str, cmd_len))
666 cmd_help(c, r, options, buf);
667 else if (is_command("ext", cmd_str, cmd_len))
668 cmd_ext(c, r, options, buf);
669 else if (is_command("debug", cmd_str, cmd_len))
670 cmd_debug(c, r, options, buf);
671 else if (is_command("scan", cmd_str, cmd_len))
672 cmd_scan(c, r, options, buf);
673 else if (is_command("sort", cmd_str, cmd_len))
674 cmd_sort(c, r, options, buf);
676 printf("unknown command %.*s\n", cmd_len, cmd_str);
680 void shell(ZOOM_connection *c, ZOOM_resultset *r,
681 ZOOM_options options)
687 const char *bp = buf;
688 #if HAVE_READLINE_READLINE_H
690 line_in=readline("ZOOM>");
693 #if HAVE_READLINE_HISTORY_H
695 add_history(line_in);
697 if(strlen(line_in) > 999) {
698 printf("Input line too long\n");
704 printf("ZOOM>"); fflush(stdout);
705 if (!fgets(buf, 999, stdin))
708 if ((cp = strchr(buf, '\n')))
710 if (!cmd_parse(c, r, options, &bp))
715 static void zoomsh(int argc, char **argv)
717 ZOOM_options options = ZOOM_options_create();
719 ZOOM_connection z39_con[MAX_CON];
720 ZOOM_resultset z39_res[MAX_CON];
722 for (i = 0; i<MAX_CON; i++)
728 for (i = 0; i<MAX_CON; i++)
732 for (i = 1; i<argc; i++)
734 const char *bp = argv[i];
735 res = cmd_parse(z39_con, z39_res, options, &bp);
736 if (res == 0) /* received quit */
739 if (res) /* do cmdline shell only if not quitting */
740 shell(z39_con, z39_res, options);
741 ZOOM_options_destroy(options);
743 for (i = 0; i<MAX_CON; i++)
745 ZOOM_connection_destroy(z39_con[i]);
746 ZOOM_resultset_destroy(z39_res[i]);
750 int main(int argc, char **argv)
752 const char *maskstr = 0;
753 if (argc > 2 && !strcmp(argv[1], "-v"))
759 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
767 int mask = yaz_log_mask_str(maskstr);
768 yaz_log_init_level(mask);
776 * c-file-style: "Stroustrup"
777 * indent-tabs-mode: nil
779 * vim: shiftwidth=4 tabstop=8 expandtab