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(const char **cpp, const char **t_start)
49 const char *cp = *cpp;
56 while (*cp && *cp != '"')
67 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
76 return len; /* return -1 if no token was read .. */
79 static WRBUF next_token_new_wrbuf(const char **cpp)
83 int len = next_token(cpp, &start);
88 wrbuf_write(w, start, len);
92 static int is_command(const char *cmd_str, const char *this_str, int this_len)
94 int cmd_len = strlen(cmd_str);
95 if (cmd_len != this_len)
97 if (memcmp(cmd_str, this_str, cmd_len))
102 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
103 ZOOM_options options,
108 if (!(key = next_token_new_wrbuf(args)))
110 printf("missing argument for set\n");
113 if ((val = next_token_new_wrbuf(args)))
115 ZOOM_options_set(options, wrbuf_cstr(key), wrbuf_cstr(val));
119 ZOOM_options_set(options, wrbuf_cstr(key), 0);
123 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
124 ZOOM_options options,
128 if (!(key = next_token_new_wrbuf(args)))
130 printf("missing argument for get\n");
134 const char *val = ZOOM_options_get(options, wrbuf_cstr(key));
135 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
140 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
141 ZOOM_options options,
145 if (!(key = next_token_new_wrbuf(args)))
147 printf("missing argument for get\n");
152 for (i = 0; i<MAX_CON; i++)
158 val = ZOOM_resultset_option_get(r[i], wrbuf_cstr(key));
159 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
165 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
166 ZOOM_options options,
171 host = next_token_new_wrbuf(args);
172 for (i = 0; i<MAX_CON; i++)
179 ZOOM_connection_destroy(c[i]);
182 else if ((h = ZOOM_connection_option_get(c[i], "host"))
183 && !strcmp(h, wrbuf_cstr(host)))
185 ZOOM_connection_destroy(c[i]);
193 static void display_records(ZOOM_connection c,
195 size_t start, size_t count, const char *type)
198 for (i = 0; i < count; i++)
200 size_t pos = i + start;
201 ZOOM_record rec = ZOOM_resultset_record(r, pos);
202 const char *db = ZOOM_record_get(rec, "database", 0);
204 if (ZOOM_record_error(rec, 0, 0, 0))
209 int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
211 printf("%lld %s: %s (%s:%d) %s\n", (long long) pos,
212 (db ? db : "unknown"),
213 msg, diagset, error, addinfo ? addinfo : "none");
218 const char *render = ZOOM_record_get(rec, type, &len);
219 const char *syntax = ZOOM_record_get(rec, "syntax", 0);
220 const char *schema = ZOOM_record_get(rec, "schema", 0);
221 /* if rec is non-null, we got a record for display */
224 printf("%lld database=%s syntax=%s schema=%s\n",
225 (long long) pos, (db ? db : "unknown"), syntax,
226 schema ? schema : "unknown");
229 if (fwrite(render, 1, len, stdout) != (size_t) len)
231 printf("write to stdout failed\n");
240 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
241 ZOOM_options options,
245 size_t start = 0, count = 1;
246 const char *type = "render";
247 WRBUF render_str = 0;
252 if ((tmp = next_token_new_wrbuf(args)))
254 start = atoi(wrbuf_cstr(tmp));
258 if ((tmp = next_token_new_wrbuf(args)))
260 count = atoi(wrbuf_cstr(tmp));
263 render_str = next_token_new_wrbuf(args);
266 type = wrbuf_cstr(render_str);
268 for (i = 0; i < MAX_CON; i++)
269 ZOOM_resultset_records(r[i], 0, start, count);
272 for (i = 0; i < MAX_CON; i++)
275 const char *errmsg, *addinfo, *dset;
276 /* display errors if any */
279 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
280 printf("%s error: %s (%s:%d) %s\n",
281 ZOOM_connection_option_get(c[i], "host"), errmsg,
282 dset, error, addinfo);
285 /* OK, no major errors. Display records... */
286 display_records(c[i], r[i], start, count, type);
290 wrbuf_destroy(render_str);
294 static void display_facets(ZOOM_facet_field *facets, int count) {
296 printf("Facets: \n");
297 for (index = 0; index < count; index++) {
299 const char *facet_name = ZOOM_facet_field_name(facets[index]);
300 printf(" %s: \n", facet_name);
301 for (term_index = 0; term_index < ZOOM_facet_field_term_count(facets[index]); term_index++) {
303 const char *term = ZOOM_facet_field_get_term(facets[index], term_index, &freq);
304 printf(" %s(%d) \n", term, freq);
309 static void cmd_facets(ZOOM_connection *c, ZOOM_resultset *r,
310 ZOOM_options options,
317 for (i = 0; i < MAX_CON; i++)
320 const char *errmsg, *addinfo, *dset;
321 /* display errors if any */
324 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
325 printf("%s error: %s (%s:%d) %s\n",
326 ZOOM_connection_option_get(c[i], "host"), errmsg,
327 dset, error, addinfo);
330 int num_facets = ZOOM_resultset_facets_size(r[i]);
332 ZOOM_facet_field *facets = ZOOM_resultset_facets(r[i]);
333 display_facets(facets, num_facets);
339 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
340 ZOOM_options options,
343 ZOOM_package p[MAX_CON];
345 WRBUF ext_type_str = next_token_new_wrbuf(args);
347 for (i = 0; i<MAX_CON; i++)
351 p[i] = ZOOM_connection_package(c[i], 0);
352 ZOOM_package_send(p[i], ext_type_str ? wrbuf_cstr(ext_type_str):0);
360 for (i = 0; i<MAX_CON; i++)
363 const char *errmsg, *addinfo, *dset;
364 /* display errors if any */
367 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
368 printf("%s error: %s (%s:%d) %s\n",
369 ZOOM_connection_option_get(c[i], "host"), errmsg,
370 dset, error, addinfo);
375 v = ZOOM_package_option_get(p[i], "targetReference");
377 printf("targetReference: %s\n", v);
378 v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
380 printf("xmlUpdateDoc: %s\n", v);
382 ZOOM_package_destroy(p[i]);
385 wrbuf_destroy(ext_type_str);
388 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
389 ZOOM_options options,
392 yaz_log_init_level(YLOG_ALL);
395 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
396 ZOOM_options options,
400 const char *query_str = *args;
403 s = ZOOM_query_create();
404 while (*query_str == ' ')
406 if (memcmp(query_str, "cql:", 4) == 0)
408 ZOOM_query_cql(s, query_str + 4);
410 else if (ZOOM_query_prefix(s, query_str))
412 printf("Bad PQF: %s\n", query_str);
415 for (i = 0; i<MAX_CON; i++)
420 ZOOM_resultset_destroy(r[i]);
424 r[i] = ZOOM_connection_search(c[i], s);
426 ZOOM_query_destroy(s);
430 for (i = 0; i<MAX_CON; i++)
433 const char *errmsg, *addinfo, *dset;
434 /* display errors if any */
437 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
438 printf("%s error: %s (%s:%d) %s\n",
439 ZOOM_connection_option_get(c[i], "host"), errmsg,
440 dset, error, addinfo);
443 /* OK, no major errors. Look at the result count */
444 int start = ZOOM_options_get_int(options, "start", 0);
445 int count = ZOOM_options_get_int(options, "count", 0);
448 printf("%s: %lld hits\n", ZOOM_connection_option_get(c[i], "host"),
449 (long long int) ZOOM_resultset_size(r[i]));
451 facet_num = ZOOM_resultset_facets_size(r[i]);
454 ZOOM_facet_field *facets = ZOOM_resultset_facets(r[i]);
456 for (facet_idx = 0; facet_idx < facet_num; facet_idx++)
458 const char *name = ZOOM_facet_field_name(facets[facet_idx]);
460 size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
461 printf("facet: %s\n", name);
462 for (term_idx = 0; term_idx < term_num; term_idx++ )
466 ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
467 printf("term: %s %d\n", term, freq);
472 display_records(c[i], r[i], start, count, "render");
477 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
478 ZOOM_options options,
481 const char *query_str = *args;
482 ZOOM_query query = ZOOM_query_create();
484 ZOOM_scanset s[MAX_CON];
486 while (*query_str == ' ')
489 if (memcmp(query_str, "cql:", 4) == 0)
491 ZOOM_query_cql(query, query_str + 4);
493 else if (ZOOM_query_prefix(query, query_str))
495 printf("Bad PQF: %s\n", query_str);
499 for (i = 0; i<MAX_CON; i++)
502 s[i] = ZOOM_connection_scan1(c[i], query);
506 ZOOM_query_destroy(query);
510 for (i = 0; i<MAX_CON; i++)
513 const char *errmsg, *addinfo, *dset;
514 /* display errors if any */
517 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
518 printf("%s error: %s (%s:%d) %s\n",
519 ZOOM_connection_option_get(c[i], "host"), errmsg,
520 dset, error, addinfo);
523 size_t p, sz = ZOOM_scanset_size(s[i]);
524 for (p = 0; p < sz; p++)
528 const char *term = ZOOM_scanset_display_term(s[i], p,
530 printf("%.*s %lld\n", (int) len, term, (long long int) occ);
532 ZOOM_scanset_destroy(s[i]);
537 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
538 ZOOM_options options,
541 const char *sort_spec = *args;
544 while (*sort_spec == ' ')
547 for (i = 0; i<MAX_CON; i++)
550 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
555 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
556 ZOOM_options options,
559 printf("connect <zurl>\n");
560 printf("search <pqf>\n");
561 printf("show [<start> [<count> [<type]]]\n");
563 printf("scan <term>\n");
565 printf("close <zurl>\n");
566 printf("ext <type>\n");
567 printf("set <option> [<value>]\n");
568 printf("get <option>\n");
570 printf("options:\n");
573 printf(" databaseName\n");
574 printf(" preferredRecordSyntax\n");
576 printf(" elementSetName\n");
577 printf(" maximumRecordSize\n");
578 printf(" preferredRecordSize\n");
580 printf(" piggyback\n");
583 printf(" password\n");
584 printf(" implementationName\n");
585 printf(" charset\n");
587 printf(" timeout\n");
591 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
592 ZOOM_options options,
596 const char *errmsg, *addinfo, *dset;
598 WRBUF host = next_token_new_wrbuf(args);
601 printf("missing host after connect\n");
604 for (j = -1, i = 0; i<MAX_CON; i++)
607 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
608 !strcmp(h, wrbuf_cstr(host)))
610 ZOOM_connection_destroy(c[i]);
613 else if (c[i] == 0 && j == -1)
616 if (i == MAX_CON) /* no match .. */
620 printf("no more connection available\n");
624 i = j; /* OK, use this one is available */
626 c[i] = ZOOM_connection_create(options);
627 ZOOM_connection_connect(c[i], wrbuf_cstr(host), 0);
629 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
630 printf("%s error: %s (%s:%d) %s\n",
631 ZOOM_connection_option_get(c[i], "host"), errmsg,
632 dset, error, addinfo);
636 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
637 ZOOM_options options,
643 cmd_len = next_token(buf, &cmd_str);
646 if (is_command("quit", cmd_str, cmd_len))
648 else if (is_command("set", cmd_str, cmd_len))
649 cmd_set(c, r, options, buf);
650 else if (is_command("get", cmd_str, cmd_len))
651 cmd_get(c, r, options, buf);
652 else if (is_command("rget", cmd_str, cmd_len))
653 cmd_rget(c, r, options, buf);
654 else if (is_command("connect", cmd_str, cmd_len))
655 cmd_connect(c, r, options, buf);
656 else if (is_command("open", cmd_str, cmd_len))
657 cmd_connect(c, r, options, buf);
658 else if (is_command("search", cmd_str, cmd_len))
659 cmd_search(c, r, options, buf);
660 else if (is_command("facets", cmd_str, cmd_len))
661 cmd_facets(c, r, options, buf);
662 else if (is_command("find", cmd_str, cmd_len))
663 cmd_search(c, r, options, buf);
664 else if (is_command("show", cmd_str, cmd_len))
665 cmd_show(c, r, options, buf);
666 else if (is_command("close", cmd_str, cmd_len))
667 cmd_close(c, r, options, buf);
668 else if (is_command("help", cmd_str, cmd_len))
669 cmd_help(c, r, options, buf);
670 else if (is_command("ext", cmd_str, cmd_len))
671 cmd_ext(c, r, options, buf);
672 else if (is_command("debug", cmd_str, cmd_len))
673 cmd_debug(c, r, options, buf);
674 else if (is_command("scan", cmd_str, cmd_len))
675 cmd_scan(c, r, options, buf);
676 else if (is_command("sort", cmd_str, cmd_len))
677 cmd_sort(c, r, options, buf);
679 printf("unknown command %.*s\n", cmd_len, cmd_str);
683 void shell(ZOOM_connection *c, ZOOM_resultset *r,
684 ZOOM_options options)
690 const char *bp = buf;
691 #if HAVE_READLINE_READLINE_H
693 line_in=readline("ZOOM>");
696 #if HAVE_READLINE_HISTORY_H
698 add_history(line_in);
700 if(strlen(line_in) > 999) {
701 printf("Input line too long\n");
707 printf("ZOOM>"); fflush(stdout);
708 if (!fgets(buf, 999, stdin))
711 if ((cp = strchr(buf, '\n')))
713 if (!cmd_parse(c, r, options, &bp))
718 static void zoomsh(int argc, char **argv)
720 ZOOM_options options = ZOOM_options_create();
722 ZOOM_connection z39_con[MAX_CON];
723 ZOOM_resultset z39_res[MAX_CON];
725 for (i = 0; i<MAX_CON; i++)
731 for (i = 0; i<MAX_CON; i++)
735 for (i = 1; i<argc; i++)
737 const char *bp = argv[i];
738 res = cmd_parse(z39_con, z39_res, options, &bp);
739 if (res == 0) /* received quit */
742 if (res) /* do cmdline shell only if not quitting */
743 shell(z39_con, z39_res, options);
744 ZOOM_options_destroy(options);
746 for (i = 0; i<MAX_CON; i++)
748 ZOOM_connection_destroy(z39_con[i]);
749 ZOOM_resultset_destroy(z39_res[i]);
753 int main(int argc, char **argv)
755 const char *maskstr = 0;
756 if (argc > 2 && !strcmp(argv[1], "-v"))
762 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
770 int mask = yaz_log_mask_str(maskstr);
771 yaz_log_init_level(mask);
779 * c-file-style: "Stroustrup"
780 * indent-tabs-mode: nil
782 * vim: shiftwidth=4 tabstop=8 expandtab