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)
15 #include <yaz/wrbuf.h>
18 #if HAVE_READLINE_READLINE_H
19 #include <readline/readline.h>
21 #if HAVE_READLINE_HISTORY_H
22 #include <readline/history.h>
30 static void process_events(ZOOM_connection *c)
34 yaz_log(YLOG_DEBUG, "process_events");
35 while ((i = ZOOM_event(MAX_CON, c)) != 0)
37 int peek = ZOOM_connection_peek_event(c[i-1]);
38 int event = ZOOM_connection_last_event(c[i-1]);
39 yaz_log(YLOG_DEBUG, "no = %d peek = %d event = %d %s", i-1,
42 ZOOM_get_event_str(event));
46 static int next_token_chars(const char **cpp, const char **t_start,
47 const char *tok_chars)
50 const char *cp = *cpp;
57 while (*cp && *cp != '"')
68 while (*cp && !strchr(tok_chars, *cp))
77 return len; /* return -1 if no token was read .. */
80 static int next_token(const char **cpp, const char **t_start)
82 return next_token_chars(cpp, t_start, "\r\n ");
86 static WRBUF next_token_new_wrbuf(const char **cpp)
90 int len = next_token(cpp, &start);
95 wrbuf_write(w, start, len);
99 static int is_command(const char *cmd_str, const char *this_str, int this_len)
101 int cmd_len = strlen(cmd_str);
102 if (cmd_len != this_len)
104 if (memcmp(cmd_str, this_str, cmd_len))
109 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
110 ZOOM_options options,
117 if (!(key = next_token_new_wrbuf(args)))
119 printf("missing argument for set\n");
122 val_len = next_token_chars(args, &val_buf, "");
124 ZOOM_options_setl(options, wrbuf_cstr(key), val_buf, val_len);
126 ZOOM_options_set(options, wrbuf_cstr(key), 0);
130 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
131 ZOOM_options options,
135 if (!(key = next_token_new_wrbuf(args)))
137 printf("missing argument for get\n");
141 const char *val = ZOOM_options_get(options, wrbuf_cstr(key));
142 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
147 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
148 ZOOM_options options,
152 if (!(key = next_token_new_wrbuf(args)))
154 printf("missing argument for get\n");
159 for (i = 0; i<MAX_CON; i++)
165 val = ZOOM_resultset_option_get(r[i], wrbuf_cstr(key));
166 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
172 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
173 ZOOM_options options,
178 host = next_token_new_wrbuf(args);
179 for (i = 0; i<MAX_CON; i++)
186 ZOOM_connection_destroy(c[i]);
189 else if ((h = ZOOM_connection_option_get(c[i], "host"))
190 && !strcmp(h, wrbuf_cstr(host)))
192 ZOOM_connection_destroy(c[i]);
200 static void display_records(ZOOM_connection c,
202 size_t start, size_t count, const char *type)
205 for (i = 0; i < count; i++)
207 size_t pos = i + start;
208 ZOOM_record rec = ZOOM_resultset_record(r, pos);
209 const char *db = ZOOM_record_get(rec, "database", 0);
211 if (ZOOM_record_error(rec, 0, 0, 0))
216 int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
218 printf("%lld %s: %s (%s:%d) %s\n", (long long) pos,
219 (db ? db : "unknown"),
220 msg, diagset, error, addinfo ? addinfo : "none");
225 const char *render = ZOOM_record_get(rec, type, &len);
226 const char *syntax = ZOOM_record_get(rec, "syntax", 0);
227 const char *schema = ZOOM_record_get(rec, "schema", 0);
228 /* if rec is non-null, we got a record for display */
231 printf("%lld database=%s syntax=%s schema=%s\n",
232 (long long) pos, (db ? db : "unknown"), syntax,
233 schema ? schema : "unknown");
236 if (fwrite(render, 1, len, stdout) != (size_t) len)
238 printf("write to stdout failed\n");
247 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
248 ZOOM_options options,
252 size_t start = 0, count = 1;
253 const char *type = "render";
254 WRBUF render_str = 0;
259 if ((tmp = next_token_new_wrbuf(args)))
261 start = atoi(wrbuf_cstr(tmp));
265 if ((tmp = next_token_new_wrbuf(args)))
267 count = atoi(wrbuf_cstr(tmp));
270 render_str = next_token_new_wrbuf(args);
273 type = wrbuf_cstr(render_str);
275 for (i = 0; i < MAX_CON; i++)
276 ZOOM_resultset_records(r[i], 0, start, count);
279 for (i = 0; i < MAX_CON; i++)
282 const char *errmsg, *addinfo, *dset;
283 /* display errors if any */
286 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
287 printf("%s error: %s (%s:%d) %s\n",
288 ZOOM_connection_option_get(c[i], "host"), errmsg,
289 dset, error, addinfo);
292 /* OK, no major errors. Display records... */
293 display_records(c[i], r[i], start, count, type);
297 wrbuf_destroy(render_str);
301 static void display_facets(ZOOM_facet_field *facets, int count) {
303 printf("Facets: \n");
304 for (index = 0; index < count; index++) {
306 const char *facet_name = ZOOM_facet_field_name(facets[index]);
307 printf(" %s: \n", facet_name);
308 for (term_index = 0; term_index < ZOOM_facet_field_term_count(facets[index]); term_index++) {
310 const char *term = ZOOM_facet_field_get_term(facets[index], term_index, &freq);
311 printf(" %s(%d) \n", term, freq);
316 static void cmd_facets(ZOOM_connection *c, ZOOM_resultset *r,
317 ZOOM_options options,
324 for (i = 0; i < MAX_CON; i++)
327 const char *errmsg, *addinfo, *dset;
328 /* display errors if any */
331 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
332 printf("%s error: %s (%s:%d) %s\n",
333 ZOOM_connection_option_get(c[i], "host"), errmsg,
334 dset, error, addinfo);
337 int num_facets = ZOOM_resultset_facets_size(r[i]);
339 ZOOM_facet_field *facets = ZOOM_resultset_facets(r[i]);
340 display_facets(facets, num_facets);
346 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
347 ZOOM_options options,
350 ZOOM_package p[MAX_CON];
352 WRBUF ext_type_str = next_token_new_wrbuf(args);
354 for (i = 0; i<MAX_CON; i++)
358 p[i] = ZOOM_connection_package(c[i], 0);
359 ZOOM_package_send(p[i], ext_type_str ? wrbuf_cstr(ext_type_str):0);
367 for (i = 0; i<MAX_CON; i++)
370 const char *errmsg, *addinfo, *dset;
371 /* display errors if any */
374 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
375 printf("%s error: %s (%s:%d) %s\n",
376 ZOOM_connection_option_get(c[i], "host"), errmsg,
377 dset, error, addinfo);
382 v = ZOOM_package_option_get(p[i], "targetReference");
384 printf("targetReference: %s\n", v);
385 v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
387 printf("xmlUpdateDoc: %s\n", v);
389 ZOOM_package_destroy(p[i]);
392 wrbuf_destroy(ext_type_str);
395 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
396 ZOOM_options options,
399 yaz_log_init_level(YLOG_ALL);
402 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
403 ZOOM_options options,
407 const char *query_str = *args;
410 s = ZOOM_query_create();
411 while (*query_str == ' ')
413 if (memcmp(query_str, "cql:", 4) == 0)
415 ZOOM_query_cql(s, query_str + 4);
417 else if (ZOOM_query_prefix(s, query_str))
419 printf("Bad PQF: %s\n", query_str);
422 for (i = 0; i<MAX_CON; i++)
427 ZOOM_resultset_destroy(r[i]);
431 r[i] = ZOOM_connection_search(c[i], s);
433 ZOOM_query_destroy(s);
437 for (i = 0; i<MAX_CON; i++)
440 const char *errmsg, *addinfo, *dset;
441 /* display errors if any */
444 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
445 printf("%s error: %s (%s:%d) %s\n",
446 ZOOM_connection_option_get(c[i], "host"), errmsg,
447 dset, error, addinfo);
450 /* OK, no major errors. Look at the result count */
451 int start = ZOOM_options_get_int(options, "start", 0);
452 int count = ZOOM_options_get_int(options, "count", 0);
455 printf("%s: %lld hits\n", ZOOM_connection_option_get(c[i], "host"),
456 (long long int) ZOOM_resultset_size(r[i]));
458 facet_num = ZOOM_resultset_facets_size(r[i]);
461 ZOOM_facet_field *facets = ZOOM_resultset_facets(r[i]);
463 for (facet_idx = 0; facet_idx < facet_num; facet_idx++)
465 const char *name = ZOOM_facet_field_name(facets[facet_idx]);
467 size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
468 printf("facet: %s\n", name);
469 for (term_idx = 0; term_idx < term_num; term_idx++ )
473 ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
474 printf("term: %s %d\n", term, freq);
479 display_records(c[i], r[i], start, count, "render");
484 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
485 ZOOM_options options,
488 const char *query_str = *args;
489 ZOOM_query query = ZOOM_query_create();
491 ZOOM_scanset s[MAX_CON];
493 while (*query_str == ' ')
496 if (memcmp(query_str, "cql:", 4) == 0)
498 ZOOM_query_cql(query, query_str + 4);
500 else if (ZOOM_query_prefix(query, query_str))
502 printf("Bad PQF: %s\n", query_str);
506 for (i = 0; i<MAX_CON; i++)
509 s[i] = ZOOM_connection_scan1(c[i], query);
513 ZOOM_query_destroy(query);
517 for (i = 0; i<MAX_CON; i++)
520 const char *errmsg, *addinfo, *dset;
521 /* display errors if any */
524 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
525 printf("%s error: %s (%s:%d) %s\n",
526 ZOOM_connection_option_get(c[i], "host"), errmsg,
527 dset, error, addinfo);
530 size_t p, sz = ZOOM_scanset_size(s[i]);
531 for (p = 0; p < sz; p++)
535 const char *term = ZOOM_scanset_display_term(s[i], p,
537 printf("%.*s %lld\n", (int) len, term, (long long int) occ);
539 ZOOM_scanset_destroy(s[i]);
544 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
545 ZOOM_options options,
548 const char *sort_spec = *args;
551 while (*sort_spec == ' ')
554 for (i = 0; i<MAX_CON; i++)
557 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
562 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
563 ZOOM_options options,
566 printf("connect <zurl>\n");
567 printf("search <pqf>\n");
568 printf("show [<start> [<count> [<type]]]\n");
570 printf("scan <term>\n");
572 printf("close <zurl>\n");
573 printf("ext <type>\n");
574 printf("set <option> [<value>]\n");
575 printf("get <option>\n");
577 printf("options:\n");
580 printf(" databaseName\n");
581 printf(" preferredRecordSyntax\n");
583 printf(" elementSetName\n");
584 printf(" maximumRecordSize\n");
585 printf(" preferredRecordSize\n");
587 printf(" piggyback\n");
590 printf(" password\n");
591 printf(" implementationName\n");
592 printf(" charset\n");
594 printf(" timeout\n");
598 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
599 ZOOM_options options,
603 const char *errmsg, *addinfo, *dset;
605 WRBUF host = next_token_new_wrbuf(args);
608 printf("missing host after connect\n");
611 for (j = -1, i = 0; i<MAX_CON; i++)
614 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
615 !strcmp(h, wrbuf_cstr(host)))
617 ZOOM_connection_destroy(c[i]);
620 else if (c[i] == 0 && j == -1)
623 if (i == MAX_CON) /* no match .. */
627 printf("no more connection available\n");
631 i = j; /* OK, use this one is available */
633 c[i] = ZOOM_connection_create(options);
634 ZOOM_connection_connect(c[i], wrbuf_cstr(host), 0);
636 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
637 printf("%s error: %s (%s:%d) %s\n",
638 ZOOM_connection_option_get(c[i], "host"), errmsg,
639 dset, error, addinfo);
643 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
644 ZOOM_options options,
650 cmd_len = next_token(buf, &cmd_str);
653 if (is_command("quit", cmd_str, cmd_len))
655 else if (is_command("set", cmd_str, cmd_len))
656 cmd_set(c, r, options, buf);
657 else if (is_command("get", cmd_str, cmd_len))
658 cmd_get(c, r, options, buf);
659 else if (is_command("rget", cmd_str, cmd_len))
660 cmd_rget(c, r, options, buf);
661 else if (is_command("connect", cmd_str, cmd_len))
662 cmd_connect(c, r, options, buf);
663 else if (is_command("open", cmd_str, cmd_len))
664 cmd_connect(c, r, options, buf);
665 else if (is_command("search", cmd_str, cmd_len))
666 cmd_search(c, r, options, buf);
667 else if (is_command("facets", cmd_str, cmd_len))
668 cmd_facets(c, r, options, buf);
669 else if (is_command("find", cmd_str, cmd_len))
670 cmd_search(c, r, options, buf);
671 else if (is_command("show", cmd_str, cmd_len))
672 cmd_show(c, r, options, buf);
673 else if (is_command("close", cmd_str, cmd_len))
674 cmd_close(c, r, options, buf);
675 else if (is_command("help", cmd_str, cmd_len))
676 cmd_help(c, r, options, buf);
677 else if (is_command("ext", cmd_str, cmd_len))
678 cmd_ext(c, r, options, buf);
679 else if (is_command("debug", cmd_str, cmd_len))
680 cmd_debug(c, r, options, buf);
681 else if (is_command("scan", cmd_str, cmd_len))
682 cmd_scan(c, r, options, buf);
683 else if (is_command("sort", cmd_str, cmd_len))
684 cmd_sort(c, r, options, buf);
686 printf("unknown command %.*s\n", cmd_len, cmd_str);
690 void shell(ZOOM_connection *c, ZOOM_resultset *r,
691 ZOOM_options options)
697 const char *bp = buf;
698 #if HAVE_READLINE_READLINE_H
700 line_in=readline("ZOOM>");
703 #if HAVE_READLINE_HISTORY_H
705 add_history(line_in);
707 if(strlen(line_in) > 999) {
708 printf("Input line too long\n");
714 printf("ZOOM>"); fflush(stdout);
715 if (!fgets(buf, 999, stdin))
718 if ((cp = strchr(buf, '\n')))
720 if (!cmd_parse(c, r, options, &bp))
725 static void zoomsh(int argc, char **argv)
727 ZOOM_options options = ZOOM_options_create();
729 ZOOM_connection z39_con[MAX_CON];
730 ZOOM_resultset z39_res[MAX_CON];
732 for (i = 0; i<MAX_CON; i++)
738 for (i = 0; i<MAX_CON; i++)
742 for (i = 1; i<argc; i++)
744 const char *bp = argv[i];
745 res = cmd_parse(z39_con, z39_res, options, &bp);
746 if (res == 0) /* received quit */
749 if (res) /* do cmdline shell only if not quitting */
750 shell(z39_con, z39_res, options);
751 ZOOM_options_destroy(options);
753 for (i = 0; i<MAX_CON; i++)
755 ZOOM_connection_destroy(z39_con[i]);
756 ZOOM_resultset_destroy(z39_res[i]);
760 int main(int argc, char **argv)
762 const char *maskstr = 0;
763 if (argc > 2 && !strcmp(argv[1], "-v"))
769 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
777 int mask = yaz_log_mask_str(maskstr);
778 yaz_log_init_level(mask);
786 * c-file-style: "Stroustrup"
787 * indent-tabs-mode: nil
789 * vim: shiftwidth=4 tabstop=8 expandtab