1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
6 \brief ZOOM C command line tool (shell)
16 #include <yaz/wrbuf.h>
19 #if HAVE_READLINE_READLINE_H
20 #include <readline/readline.h>
22 #if HAVE_READLINE_HISTORY_H
23 #include <readline/history.h>
31 static void process_events(ZOOM_connection *c)
35 yaz_log(YLOG_DEBUG, "process_events");
36 while ((i = ZOOM_event(MAX_CON, c)) != 0)
38 int peek = ZOOM_connection_peek_event(c[i-1]);
39 int event = ZOOM_connection_last_event(c[i-1]);
40 yaz_log(YLOG_DEBUG, "no = %d peek = %d event = %d %s", i-1,
43 ZOOM_get_event_str(event));
47 static int next_token(const char **cpp, const char **t_start)
50 const char *cp = *cpp;
57 while (*cp && *cp != '"')
68 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
77 return len; /* return -1 if no token was read .. */
80 static WRBUF next_token_new_wrbuf(const char **cpp)
84 int len = next_token(cpp, &start);
89 wrbuf_write(w, start, len);
93 static int is_command(const char *cmd_str, const char *this_str, int this_len)
95 int cmd_len = strlen(cmd_str);
96 if (cmd_len != this_len)
98 if (memcmp(cmd_str, this_str, cmd_len))
103 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
104 ZOOM_options options,
109 if (!(key = next_token_new_wrbuf(args)))
111 printf("missing argument for set\n");
114 if ((val = next_token_new_wrbuf(args)))
116 ZOOM_options_set(options, wrbuf_cstr(key), wrbuf_cstr(val));
120 ZOOM_options_set(options, wrbuf_cstr(key), 0);
124 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
125 ZOOM_options options,
129 if (!(key = next_token_new_wrbuf(args)))
131 printf("missing argument for get\n");
135 const char *val = ZOOM_options_get(options, wrbuf_cstr(key));
136 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
141 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
142 ZOOM_options options,
146 if (!(key = next_token_new_wrbuf(args)))
148 printf("missing argument for get\n");
153 for (i = 0; i<MAX_CON; i++)
159 val = ZOOM_resultset_option_get(r[i], wrbuf_cstr(key));
160 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
166 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
167 ZOOM_options options,
172 host = next_token_new_wrbuf(args);
173 for (i = 0; i<MAX_CON; i++)
180 ZOOM_connection_destroy(c[i]);
183 else if ((h = ZOOM_connection_option_get(c[i], "host"))
184 && !strcmp(h, wrbuf_cstr(host)))
186 ZOOM_connection_destroy(c[i]);
194 static void display_records(ZOOM_connection c,
196 size_t start, size_t count, const char *type)
199 for (i = 0; i < count; i++)
201 size_t pos = i + start;
202 ZOOM_record rec = ZOOM_resultset_record(r, pos);
203 const char *db = ZOOM_record_get(rec, "database", 0);
205 if (ZOOM_record_error(rec, 0, 0, 0))
210 int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
212 printf("%lld %s: %s (%s:%d) %s\n", (long long) pos,
213 (db ? db : "unknown"),
214 msg, diagset, error, addinfo ? addinfo : "none");
219 const char *render = ZOOM_record_get(rec, type, &len);
220 const char *syntax = ZOOM_record_get(rec, "syntax", 0);
221 const char *schema = ZOOM_record_get(rec, "schema", 0);
222 /* if rec is non-null, we got a record for display */
225 printf("%lld database=%s syntax=%s schema=%s\n",
226 (long long) pos, (db ? db : "unknown"), syntax,
227 schema ? schema : "unknown");
230 if (fwrite(render, 1, len, stdout) != (size_t) len)
232 printf("write to stdout failed\n");
241 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
242 ZOOM_options options,
246 size_t start = 0, count = 1;
247 const char *type = "render";
248 WRBUF render_str = 0;
253 if ((tmp = next_token_new_wrbuf(args)))
255 start = atoi(wrbuf_cstr(tmp));
259 if ((tmp = next_token_new_wrbuf(args)))
261 count = atoi(wrbuf_cstr(tmp));
264 render_str = next_token_new_wrbuf(args);
267 type = wrbuf_cstr(render_str);
269 for (i = 0; i < MAX_CON; i++)
270 ZOOM_resultset_records(r[i], 0, start, count);
273 for (i = 0; i < MAX_CON; i++)
276 const char *errmsg, *addinfo, *dset;
277 /* display errors if any */
280 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
281 printf("%s error: %s (%s:%d) %s\n",
282 ZOOM_connection_option_get(c[i], "host"), errmsg,
283 dset, error, addinfo);
286 /* OK, no major errors. Display records... */
287 display_records(c[i], r[i], start, count, type);
291 wrbuf_destroy(render_str);
295 static void display_facets(ZOOM_facet_field *facets, int count) {
297 printf("Facets: \n");
298 for (index = 0; index < count; index++) {
300 const char *facet_name = ZOOM_facet_field_name(facets[index]);
301 printf(" %s: \n", facet_name);
302 for (term_index = 0; term_index < ZOOM_facet_field_term_count(facets[index]); term_index++) {
304 const char *term = ZOOM_facet_field_get_term(facets[index], term_index, &freq);
305 printf(" %s(%d) \n", term, freq);
310 static void cmd_facets(ZOOM_connection *c, ZOOM_resultset *r,
311 ZOOM_options options,
315 size_t start = 0, count = 1;
316 const char *type = "render";
317 WRBUF render_str = 0;
323 if ((tmp = next_token_new_wrbuf(args)))
325 start = atoi(wrbuf_cstr(tmp));
329 if ((tmp = next_token_new_wrbuf(args)))
331 count = atoi(wrbuf_cstr(tmp));
334 render_str = next_token_new_wrbuf(args);
337 type = wrbuf_cstr(render_str);
341 for (i = 0; i < MAX_CON; i++)
344 const char *errmsg, *addinfo, *dset;
345 /* display errors if any */
348 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
349 printf("%s error: %s (%s:%d) %s\n",
350 ZOOM_connection_option_get(c[i], "host"), errmsg,
351 dset, error, addinfo);
354 int num_facets = ZOOM_resultset_facets_size(r[i]);
356 ZOOM_facet_field *facets = ZOOM_resultset_facets(r[i]);
357 display_facets(facets, num_facets);
362 wrbuf_destroy(render_str);
366 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
367 ZOOM_options options,
370 ZOOM_package p[MAX_CON];
372 WRBUF ext_type_str = next_token_new_wrbuf(args);
374 for (i = 0; i<MAX_CON; i++)
378 p[i] = ZOOM_connection_package(c[i], 0);
379 ZOOM_package_send(p[i], ext_type_str ? wrbuf_cstr(ext_type_str):0);
387 for (i = 0; i<MAX_CON; i++)
390 const char *errmsg, *addinfo, *dset;
391 /* display errors if any */
394 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
395 printf("%s error: %s (%s:%d) %s\n",
396 ZOOM_connection_option_get(c[i], "host"), errmsg,
397 dset, error, addinfo);
402 v = ZOOM_package_option_get(p[i], "targetReference");
404 printf("targetReference: %s\n", v);
405 v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
407 printf("xmlUpdateDoc: %s\n", v);
409 ZOOM_package_destroy(p[i]);
412 wrbuf_destroy(ext_type_str);
415 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
416 ZOOM_options options,
419 yaz_log_init_level(YLOG_ALL);
422 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
423 ZOOM_options options,
427 const char *query_str = *args;
430 s = ZOOM_query_create();
431 while (*query_str == ' ')
433 if (memcmp(query_str, "cql:", 4) == 0)
435 ZOOM_query_cql(s, query_str + 4);
437 else if (ZOOM_query_prefix(s, query_str))
439 printf("Bad PQF: %s\n", query_str);
442 for (i = 0; i<MAX_CON; i++)
447 ZOOM_resultset_destroy(r[i]);
451 r[i] = ZOOM_connection_search(c[i], s);
453 ZOOM_query_destroy(s);
457 for (i = 0; i<MAX_CON; i++)
460 const char *errmsg, *addinfo, *dset;
461 /* display errors if any */
464 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
465 printf("%s error: %s (%s:%d) %s\n",
466 ZOOM_connection_option_get(c[i], "host"), errmsg,
467 dset, error, addinfo);
470 /* OK, no major errors. Look at the result count */
471 int start = ZOOM_options_get_int(options, "start", 0);
472 int count = ZOOM_options_get_int(options, "count", 0);
474 printf("%s: %lld hits\n", ZOOM_connection_option_get(c[i], "host"),
475 (long long int) ZOOM_resultset_size(r[i]));
477 display_records(c[i], r[i], start, count, "render");
482 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
483 ZOOM_options options,
486 const char *query_str = *args;
487 ZOOM_query query = ZOOM_query_create();
489 ZOOM_scanset s[MAX_CON];
491 while (*query_str == ' ')
494 if (memcmp(query_str, "cql:", 4) == 0)
496 ZOOM_query_cql(query, query_str + 4);
498 else if (ZOOM_query_prefix(query, query_str))
500 printf("Bad PQF: %s\n", query_str);
504 for (i = 0; i<MAX_CON; i++)
507 s[i] = ZOOM_connection_scan1(c[i], query);
511 ZOOM_query_destroy(query);
515 for (i = 0; i<MAX_CON; i++)
518 const char *errmsg, *addinfo, *dset;
519 /* display errors if any */
522 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
523 printf("%s error: %s (%s:%d) %s\n",
524 ZOOM_connection_option_get(c[i], "host"), errmsg,
525 dset, error, addinfo);
528 size_t p, sz = ZOOM_scanset_size(s[i]);
529 for (p = 0; p < sz; p++)
533 const char *term = ZOOM_scanset_display_term(s[i], p,
535 printf("%.*s %lld\n", (int) len, term, (long long int) occ);
537 ZOOM_scanset_destroy(s[i]);
542 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
543 ZOOM_options options,
546 const char *sort_spec = *args;
549 while (*sort_spec == ' ')
552 for (i = 0; i<MAX_CON; i++)
555 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
560 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
561 ZOOM_options options,
564 printf("connect <zurl>\n");
565 printf("search <pqf>\n");
566 printf("show [<start> [<count> [<type]]]\n");
568 printf("scan <term>\n");
570 printf("close <zurl>\n");
571 printf("ext <type>\n");
572 printf("set <option> [<value>]\n");
573 printf("get <option>\n");
575 printf("options:\n");
578 printf(" databaseName\n");
579 printf(" preferredRecordSyntax\n");
581 printf(" elementSetName\n");
582 printf(" maximumRecordSize\n");
583 printf(" preferredRecordSize\n");
585 printf(" piggyback\n");
588 printf(" password\n");
589 printf(" implementationName\n");
590 printf(" charset\n");
592 printf(" timeout\n");
596 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
597 ZOOM_options options,
601 const char *errmsg, *addinfo, *dset;
603 WRBUF host = next_token_new_wrbuf(args);
606 printf("missing host after connect\n");
609 for (j = -1, i = 0; i<MAX_CON; i++)
612 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
613 !strcmp(h, wrbuf_cstr(host)))
615 ZOOM_connection_destroy(c[i]);
618 else if (c[i] == 0 && j == -1)
621 if (i == MAX_CON) /* no match .. */
625 printf("no more connection available\n");
629 i = j; /* OK, use this one is available */
631 c[i] = ZOOM_connection_create(options);
632 ZOOM_connection_connect(c[i], wrbuf_cstr(host), 0);
634 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
635 printf("%s error: %s (%s:%d) %s\n",
636 ZOOM_connection_option_get(c[i], "host"), errmsg,
637 dset, error, addinfo);
641 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
642 ZOOM_options options,
648 cmd_len = next_token(buf, &cmd_str);
651 if (is_command("quit", cmd_str, cmd_len))
653 else if (is_command("set", cmd_str, cmd_len))
654 cmd_set(c, r, options, buf);
655 else if (is_command("get", cmd_str, cmd_len))
656 cmd_get(c, r, options, buf);
657 else if (is_command("rget", cmd_str, cmd_len))
658 cmd_rget(c, r, options, buf);
659 else if (is_command("connect", cmd_str, cmd_len))
660 cmd_connect(c, r, options, buf);
661 else if (is_command("open", cmd_str, cmd_len))
662 cmd_connect(c, r, options, buf);
663 else if (is_command("search", cmd_str, cmd_len))
664 cmd_search(c, r, options, buf);
665 else if (is_command("facets", cmd_str, cmd_len))
666 cmd_facets(c, r, options, buf);
667 else if (is_command("find", cmd_str, cmd_len))
668 cmd_search(c, r, options, buf);
669 else if (is_command("show", cmd_str, cmd_len))
670 cmd_show(c, r, options, buf);
671 else if (is_command("close", cmd_str, cmd_len))
672 cmd_close(c, r, options, buf);
673 else if (is_command("help", cmd_str, cmd_len))
674 cmd_help(c, r, options, buf);
675 else if (is_command("ext", cmd_str, cmd_len))
676 cmd_ext(c, r, options, buf);
677 else if (is_command("debug", cmd_str, cmd_len))
678 cmd_debug(c, r, options, buf);
679 else if (is_command("scan", cmd_str, cmd_len))
680 cmd_scan(c, r, options, buf);
681 else if (is_command("sort", cmd_str, cmd_len))
682 cmd_sort(c, r, options, buf);
684 printf("unknown command %.*s\n", cmd_len, cmd_str);
688 void shell(ZOOM_connection *c, ZOOM_resultset *r,
689 ZOOM_options options)
695 const char *bp = buf;
696 #if HAVE_READLINE_READLINE_H
698 line_in=readline("ZOOM>");
701 #if HAVE_READLINE_HISTORY_H
703 add_history(line_in);
705 if(strlen(line_in) > 999) {
706 printf("Input line too long\n");
712 printf("ZOOM>"); fflush(stdout);
713 if (!fgets(buf, 999, stdin))
716 if ((cp = strchr(buf, '\n')))
718 if (!cmd_parse(c, r, options, &bp))
723 static void zoomsh(int argc, char **argv)
725 ZOOM_options options = ZOOM_options_create();
727 ZOOM_connection z39_con[MAX_CON];
728 ZOOM_resultset z39_res[MAX_CON];
730 for (i = 0; i<MAX_CON; i++)
736 for (i = 0; i<MAX_CON; i++)
740 for (i = 1; i<argc; i++)
742 const char *bp = argv[i];
743 res = cmd_parse(z39_con, z39_res, options, &bp);
744 if (res == 0) /* received quit */
747 if (res) /* do cmdline shell only if not quitting */
748 shell(z39_con, z39_res, options);
749 ZOOM_options_destroy(options);
751 for (i = 0; i<MAX_CON; i++)
753 ZOOM_connection_destroy(z39_con[i]);
754 ZOOM_resultset_destroy(z39_res[i]);
758 int main(int argc, char **argv)
760 const char *maskstr = 0;
761 if (argc > 2 && !strcmp(argv[1], "-v"))
767 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
775 int mask = yaz_log_mask_str(maskstr);
776 yaz_log_init_level(mask);
784 * c-file-style: "Stroustrup"
785 * indent-tabs-mode: nil
787 * vim: shiftwidth=4 tabstop=8 expandtab