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 cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
278 ZOOM_options options,
281 ZOOM_package p[MAX_CON];
283 WRBUF ext_type_str = next_token_new_wrbuf(args);
285 for (i = 0; i<MAX_CON; i++)
289 p[i] = ZOOM_connection_package(c[i], 0);
290 ZOOM_package_send(p[i], ext_type_str ? wrbuf_cstr(ext_type_str):0);
296 while (ZOOM_event(MAX_CON, c))
299 for (i = 0; i<MAX_CON; i++)
302 const char *errmsg, *addinfo, *dset;
303 /* display errors if any */
306 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
307 printf("%s error: %s (%s:%d) %s\n",
308 ZOOM_connection_option_get(c[i], "host"), errmsg,
309 dset, error, addinfo);
314 v = ZOOM_package_option_get(p[i], "targetReference");
316 printf("targetReference: %s\n", v);
317 v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
319 printf("xmlUpdateDoc: %s\n", v);
321 ZOOM_package_destroy(p[i]);
324 wrbuf_destroy(ext_type_str);
327 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
328 ZOOM_options options,
331 yaz_log_init_level(YLOG_ALL);
334 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
335 ZOOM_options options,
339 const char *query_str = *args;
342 s = ZOOM_query_create();
343 while (*query_str == ' ')
345 if (memcmp(query_str, "cql:", 4) == 0)
347 ZOOM_query_cql(s, query_str + 4);
349 else if (ZOOM_query_prefix(s, query_str))
351 printf("Bad PQF: %s\n", query_str);
354 for (i = 0; i<MAX_CON; i++)
359 ZOOM_resultset_destroy(r[i]);
363 r[i] = ZOOM_connection_search(c[i], s);
365 ZOOM_query_destroy(s);
367 while (ZOOM_event(MAX_CON, c))
370 for (i = 0; i<MAX_CON; i++)
373 const char *errmsg, *addinfo, *dset;
374 /* display errors if any */
377 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
378 printf("%s error: %s (%s:%d) %s\n",
379 ZOOM_connection_option_get(c[i], "host"), errmsg,
380 dset, error, addinfo);
383 /* OK, no major errors. Look at the result count */
384 int start = ZOOM_options_get_int(options, "start", 0);
385 int count = ZOOM_options_get_int(options, "count", 0);
387 printf("%s: %lld hits\n", ZOOM_connection_option_get(c[i], "host"),
388 (long long int) ZOOM_resultset_size(r[i]));
390 display_records(c[i], r[i], start, count, "render");
395 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
396 ZOOM_options options,
399 const char *query_str = *args;
400 ZOOM_query query = ZOOM_query_create();
402 ZOOM_scanset s[MAX_CON];
404 while (*query_str == ' ')
407 if (memcmp(query_str, "cql:", 4) == 0)
409 ZOOM_query_cql(query, query_str + 4);
411 else if (ZOOM_query_prefix(query, query_str))
413 printf("Bad PQF: %s\n", query_str);
417 for (i = 0; i<MAX_CON; i++)
420 s[i] = ZOOM_connection_scan1(c[i], query);
424 ZOOM_query_destroy(query);
426 while (ZOOM_event(MAX_CON, c))
428 for (i = 0; i<MAX_CON; i++)
431 const char *errmsg, *addinfo, *dset;
432 /* display errors if any */
435 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
436 printf("%s error: %s (%s:%d) %s\n",
437 ZOOM_connection_option_get(c[i], "host"), errmsg,
438 dset, error, addinfo);
441 size_t p, sz = ZOOM_scanset_size(s[i]);
442 for (p = 0; p < sz; p++)
446 const char *term = ZOOM_scanset_display_term(s[i], p,
448 printf("%.*s %lld\n", (int) len, term, (long long int) occ);
450 ZOOM_scanset_destroy(s[i]);
455 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
456 ZOOM_options options,
459 const char *sort_spec = *args;
462 while (*sort_spec == ' ')
465 for (i = 0; i<MAX_CON; i++)
468 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
470 while (ZOOM_event(MAX_CON, c))
474 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
475 ZOOM_options options,
478 printf("connect <zurl>\n");
479 printf("search <pqf>\n");
480 printf("show [<start> [<count> [<type]]]\n");
481 printf("scan <term>\n");
483 printf("close <zurl>\n");
484 printf("ext <type>\n");
485 printf("set <option> [<value>]\n");
486 printf("get <option>\n");
488 printf("options:\n");
491 printf(" databaseName\n");
492 printf(" preferredRecordSyntax\n");
494 printf(" elementSetName\n");
495 printf(" maximumRecordSize\n");
496 printf(" preferredRecordSize\n");
498 printf(" piggyback\n");
501 printf(" password\n");
502 printf(" implementationName\n");
503 printf(" charset\n");
505 printf(" timeout\n");
508 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
509 ZOOM_options options,
513 const char *errmsg, *addinfo, *dset;
515 WRBUF host = next_token_new_wrbuf(args);
518 printf("missing host after connect\n");
521 for (j = -1, i = 0; i<MAX_CON; i++)
524 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
525 !strcmp(h, wrbuf_cstr(host)))
527 ZOOM_connection_destroy(c[i]);
530 else if (c[i] == 0 && j == -1)
533 if (i == MAX_CON) /* no match .. */
537 printf("no more connection available\n");
541 i = j; /* OK, use this one is available */
543 c[i] = ZOOM_connection_create(options);
544 ZOOM_connection_connect(c[i], wrbuf_cstr(host), 0);
546 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
547 printf("%s error: %s (%s:%d) %s\n",
548 ZOOM_connection_option_get(c[i], "host"), errmsg,
549 dset, error, addinfo);
553 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
554 ZOOM_options options,
560 cmd_len = next_token(buf, &cmd_str);
563 if (is_command("quit", cmd_str, cmd_len))
565 else if (is_command("set", cmd_str, cmd_len))
566 cmd_set(c, r, options, buf);
567 else if (is_command("get", cmd_str, cmd_len))
568 cmd_get(c, r, options, buf);
569 else if (is_command("rget", cmd_str, cmd_len))
570 cmd_rget(c, r, options, buf);
571 else if (is_command("connect", cmd_str, cmd_len))
572 cmd_connect(c, r, options, buf);
573 else if (is_command("open", cmd_str, cmd_len))
574 cmd_connect(c, r, options, buf);
575 else if (is_command("search", cmd_str, cmd_len))
576 cmd_search(c, r, options, buf);
577 else if (is_command("find", cmd_str, cmd_len))
578 cmd_search(c, r, options, buf);
579 else if (is_command("show", cmd_str, cmd_len))
580 cmd_show(c, r, options, buf);
581 else if (is_command("close", cmd_str, cmd_len))
582 cmd_close(c, r, options, buf);
583 else if (is_command("help", cmd_str, cmd_len))
584 cmd_help(c, r, options, buf);
585 else if (is_command("ext", cmd_str, cmd_len))
586 cmd_ext(c, r, options, buf);
587 else if (is_command("debug", cmd_str, cmd_len))
588 cmd_debug(c, r, options, buf);
589 else if (is_command("scan", cmd_str, cmd_len))
590 cmd_scan(c, r, options, buf);
591 else if (is_command("sort", cmd_str, cmd_len))
592 cmd_sort(c, r, options, buf);
594 printf("unknown command %.*s\n", cmd_len, cmd_str);
598 void shell(ZOOM_connection *c, ZOOM_resultset *r,
599 ZOOM_options options)
605 const char *bp = buf;
606 #if HAVE_READLINE_READLINE_H
608 line_in=readline("ZOOM>");
611 #if HAVE_READLINE_HISTORY_H
613 add_history(line_in);
615 if(strlen(line_in) > 999) {
616 printf("Input line too long\n");
622 printf("ZOOM>"); fflush(stdout);
623 if (!fgets(buf, 999, stdin))
626 if ((cp = strchr(buf, '\n')))
628 if (!cmd_parse(c, r, options, &bp))
633 static void zoomsh(int argc, char **argv)
635 ZOOM_options options = ZOOM_options_create();
637 ZOOM_connection z39_con[MAX_CON];
638 ZOOM_resultset z39_res[MAX_CON];
640 for (i = 0; i<MAX_CON; i++)
646 for (i = 0; i<MAX_CON; i++)
650 for (i = 1; i<argc; i++)
652 const char *bp = argv[i];
653 res = cmd_parse(z39_con, z39_res, options, &bp);
654 if (res == 0) /* received quit */
657 if (res) /* do cmdline shell only if not quitting */
658 shell(z39_con, z39_res, options);
659 ZOOM_options_destroy(options);
661 for (i = 0; i<MAX_CON; i++)
663 ZOOM_connection_destroy(z39_con[i]);
664 ZOOM_resultset_destroy(z39_res[i]);
668 int main(int argc, char **argv)
670 const char *maskstr = 0;
671 if (argc > 2 && !strcmp(argv[1], "-v"))
677 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
685 int mask = yaz_log_mask_str(maskstr);
686 yaz_log_init_level(mask);
694 * c-file-style: "Stroustrup"
695 * indent-tabs-mode: nil
697 * vim: shiftwidth=4 tabstop=8 expandtab