1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 \brief ZOOM C command line tool (shell)
15 #include <yaz/comstack.h>
17 #if HAVE_READLINE_READLINE_H
18 #include <readline/readline.h>
20 #if HAVE_READLINE_HISTORY_H
21 #include <readline/history.h>
24 #include <yaz/xmalloc.h>
32 static int next_token(const char **cpp, const char **t_start)
35 const char *cp = *cpp;
42 while (*cp && *cp != '"')
53 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
62 return len; /* return -1 if no token was read .. */
65 static int next_token_copy(const char **cpp, char *buf_out, int buf_max)
68 int len = next_token(cpp, &start);
76 memcpy(buf_out, start, len);
81 static int is_command(const char *cmd_str, const char *this_str, int this_len)
83 int cmd_len = strlen(cmd_str);
84 if (cmd_len != this_len)
86 if (memcmp(cmd_str, this_str, cmd_len))
91 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
95 char key[40], val[80];
97 if (next_token_copy(args, key, sizeof(key)) < 0)
99 printf("missing argument for set\n");
102 if (next_token_copy(args, val, sizeof(val)) < 0)
103 ZOOM_options_set(options, key, 0);
105 ZOOM_options_set(options, key, val);
108 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
109 ZOOM_options options,
113 if (next_token_copy(args, key, sizeof(key)) < 0)
115 printf("missing argument for get\n");
119 const char *val = ZOOM_options_get(options, key);
120 printf("%s = %s\n", key, val ? val : "<null>");
124 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
125 ZOOM_options options,
129 if (next_token_copy(args, key, sizeof(key)) < 0)
131 printf("missing argument for get\n");
136 for (i = 0; i<MAX_CON; i++)
142 val = ZOOM_resultset_option_get(r[i], key);
143 printf("%s = %s\n", key, val ? val : "<null>");
148 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
149 ZOOM_options options,
154 next_token_copy(args, host, sizeof(host));
155 for (i = 0; i<MAX_CON; i++)
160 if ((h = ZOOM_connection_option_get(c[i], "host"))
163 ZOOM_connection_destroy(c[i]);
166 else if (*host == '\0')
168 ZOOM_connection_destroy(c[i]);
174 static void display_records(ZOOM_connection c,
176 int start, int count, const char *type)
179 for (i = 0; i<count; i++)
182 ZOOM_record rec = ZOOM_resultset_record(r, pos);
183 const char *db = ZOOM_record_get(rec, "database", 0);
185 if (ZOOM_record_error(rec, 0, 0, 0))
190 int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
192 printf("%d %s: %s (%s:%d) %s\n", pos, (db ? db : "unknown"),
193 msg, diagset, error, addinfo ? addinfo : "none");
198 const char *render = ZOOM_record_get(rec, type, &len);
199 const char *opac_render = ZOOM_record_get(rec, "opac", &opac_len);
200 const char *syntax = ZOOM_record_get(rec, "syntax", 0);
201 const char *schema = ZOOM_record_get(rec, "schema", 0);
202 /* if rec is non-null, we got a record for display */
205 printf("%d database=%s syntax=%s schema=%s\n",
206 pos, (db ? db : "unknown"), syntax,
207 schema ? schema : "unknown");
210 if (fwrite(render, 1, len, stdout) != (size_t) len)
212 printf("write to stdout failed\n");
218 if (fwrite(opac_render, 1, opac_len, stdout) != (size_t)
220 printf("write to stdout failed\n");
227 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
228 ZOOM_options options,
232 char start_str[10], count_str[10], render_str[60];
233 const char *type = "render";
235 if (next_token_copy(args, start_str, sizeof(start_str)) >= 0)
236 ZOOM_options_set(options, "start", start_str);
238 if (next_token_copy(args, count_str, sizeof(count_str)) >= 0)
239 ZOOM_options_set(options, "count", count_str);
241 if (next_token_copy(args, render_str, sizeof(render_str)) >= 0)
244 for (i = 0; i<MAX_CON; i++)
245 ZOOM_resultset_records(r[i], 0, atoi(start_str), atoi(count_str));
246 while (ZOOM_event(MAX_CON, c))
249 for (i = 0; i<MAX_CON; i++)
252 const char *errmsg, *addinfo, *dset;
253 /* display errors if any */
256 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
257 printf("%s error: %s (%s:%d) %s\n",
258 ZOOM_connection_option_get(c[i], "host"), errmsg,
259 dset, error, addinfo);
262 /* OK, no major errors. Display records... */
263 int start = ZOOM_options_get_int(options, "start", 0);
264 int count = ZOOM_options_get_int(options, "count", 0);
265 display_records(c[i], r[i], start, count, type);
268 ZOOM_options_set(options, "count", "0");
269 ZOOM_options_set(options, "start", "0");
272 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
273 ZOOM_options options,
276 ZOOM_package p[MAX_CON];
277 char ext_type_str[10];
281 if (next_token_copy(args, ext_type_str, sizeof(ext_type_str)) < 0)
284 for (i = 0; i<MAX_CON; i++)
288 p[i] = ZOOM_connection_package(c[i], 0);
289 ZOOM_package_send(p[i], ext_type_str);
295 while (ZOOM_event(MAX_CON, c))
298 for (i = 0; i<MAX_CON; i++)
301 const char *errmsg, *addinfo, *dset;
302 /* display errors if any */
305 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
306 printf("%s error: %s (%s:%d) %s\n",
307 ZOOM_connection_option_get(c[i], "host"), errmsg,
308 dset, error, addinfo);
313 v = ZOOM_package_option_get(p[i], "targetReference");
315 printf("targetReference: %s\n", v);
316 v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
318 printf("xmlUpdateDoc: %s\n", v);
320 ZOOM_package_destroy(p[i]);
324 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
325 ZOOM_options options,
328 yaz_log_init_level(YLOG_ALL);
331 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
332 ZOOM_options options,
336 const char *query_str = *args;
339 s = ZOOM_query_create();
340 while (*query_str == ' ')
342 if (memcmp(query_str, "cql:", 4) == 0)
344 ZOOM_query_cql(s, query_str + 4);
346 else if (ZOOM_query_prefix(s, query_str))
348 printf("Bad PQF: %s\n", query_str);
351 for (i = 0; i<MAX_CON; i++)
356 ZOOM_resultset_destroy(r[i]);
360 r[i] = ZOOM_connection_search(c[i], s);
362 ZOOM_query_destroy(s);
364 while (ZOOM_event(MAX_CON, c))
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);
380 /* OK, no major errors. Look at the result count */
381 int start = ZOOM_options_get_int(options, "start", 0);
382 int count = ZOOM_options_get_int(options, "count", 0);
384 printf("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
385 (long) ZOOM_resultset_size(r[i]));
387 display_records(c[i], r[i], start, count, "render");
392 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
393 ZOOM_options options,
396 const char *query_str = *args;
397 ZOOM_query query = ZOOM_query_create();
399 ZOOM_scanset s[MAX_CON];
401 while (*query_str == ' ')
404 if (memcmp(query_str, "cql:", 4) == 0)
406 ZOOM_query_cql(query, query_str + 4);
408 else if (ZOOM_query_prefix(query, query_str))
410 printf("Bad PQF: %s\n", query_str);
414 for (i = 0; i<MAX_CON; i++)
417 s[i] = ZOOM_connection_scan1(c[i], query);
421 ZOOM_query_destroy(query);
423 while (ZOOM_event(MAX_CON, c))
425 for (i = 0; i<MAX_CON; i++)
428 const char *errmsg, *addinfo, *dset;
429 /* display errors if any */
432 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
433 printf("%s error: %s (%s:%d) %s\n",
434 ZOOM_connection_option_get(c[i], "host"), errmsg,
435 dset, error, addinfo);
438 size_t p, sz = ZOOM_scanset_size(s[i]);
439 for (p = 0; p < sz; p++)
443 const char *term = ZOOM_scanset_display_term(s[i], p,
446 printf("%.*s %d\n", len, term, occ);
448 ZOOM_scanset_destroy(s[i]);
453 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
454 ZOOM_options options,
457 const char *sort_spec = *args;
460 while (*sort_spec == ' ')
463 for (i = 0; i<MAX_CON; i++)
466 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
468 while (ZOOM_event(MAX_CON, c))
472 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
473 ZOOM_options options,
476 printf("connect <zurl>\n");
477 printf("search <pqf>\n");
478 printf("show [<start> [<count>]\n");
479 printf("scan <term>\n");
481 printf("close <zurl>\n");
482 printf("ext <type>\n");
483 printf("set <option> [<value>]\n");
484 printf("get <option>\n");
486 printf("options:\n");
489 printf(" databaseName\n");
490 printf(" preferredRecordSyntax\n");
492 printf(" elementSetName\n");
493 printf(" maximumRecordSize\n");
494 printf(" preferredRecordSize\n");
496 printf(" piggyback\n");
499 printf(" password\n");
500 printf(" implementationName\n");
501 printf(" charset\n");
505 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
506 ZOOM_options options,
510 const char *errmsg, *addinfo, *dset;
513 if (next_token_copy(args, host, sizeof(host)) < 0)
515 printf("missing host after connect\n");
518 for (j = -1, i = 0; i<MAX_CON; i++)
521 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
524 ZOOM_connection_destroy(c[i]);
527 else if (c[i] == 0 && j == -1)
530 if (i == MAX_CON) /* no match .. */
534 printf("no more connection available\n");
537 i = j; /* OK, use this one is available */
539 c[i] = ZOOM_connection_create(options);
540 ZOOM_connection_connect(c[i], host, 0);
542 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
543 printf("%s error: %s (%s:%d) %s\n",
544 ZOOM_connection_option_get(c[i], "host"), errmsg,
545 dset, error, addinfo);
548 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
549 ZOOM_options options,
555 cmd_len = next_token(buf, &cmd_str);
558 if (is_command("quit", cmd_str, cmd_len))
560 else if (is_command("set", cmd_str, cmd_len))
561 cmd_set(c, r, options, buf);
562 else if (is_command("get", cmd_str, cmd_len))
563 cmd_get(c, r, options, buf);
564 else if (is_command("rget", cmd_str, cmd_len))
565 cmd_rget(c, r, options, buf);
566 else if (is_command("connect", cmd_str, cmd_len))
567 cmd_connect(c, r, options, buf);
568 else if (is_command("open", cmd_str, cmd_len))
569 cmd_connect(c, r, options, buf);
570 else if (is_command("search", cmd_str, cmd_len))
571 cmd_search(c, r, options, buf);
572 else if (is_command("find", cmd_str, cmd_len))
573 cmd_search(c, r, options, buf);
574 else if (is_command("show", cmd_str, cmd_len))
575 cmd_show(c, r, options, buf);
576 else if (is_command("close", cmd_str, cmd_len))
577 cmd_close(c, r, options, buf);
578 else if (is_command("help", cmd_str, cmd_len))
579 cmd_help(c, r, options, buf);
580 else if (is_command("ext", cmd_str, cmd_len))
581 cmd_ext(c, r, options, buf);
582 else if (is_command("debug", cmd_str, cmd_len))
583 cmd_debug(c, r, options, buf);
584 else if (is_command("scan", cmd_str, cmd_len))
585 cmd_scan(c, r, options, buf);
586 else if (is_command("sort", cmd_str, cmd_len))
587 cmd_sort(c, r, options, buf);
589 printf("unknown command %.*s\n", cmd_len, cmd_str);
593 void shell(ZOOM_connection *c, ZOOM_resultset *r,
594 ZOOM_options options)
600 const char *bp = buf;
601 #if HAVE_READLINE_READLINE_H
603 line_in=readline("ZOOM>");
606 #if HAVE_READLINE_HISTORY_H
608 add_history(line_in);
610 if(strlen(line_in) > 999) {
611 printf("Input line too long\n");
617 printf("ZOOM>"); fflush(stdout);
618 if (!fgets(buf, 999, stdin))
621 if ((cp = strchr(buf, '\n')))
623 if (!cmd_parse(c, r, options, &bp))
628 static void zoomsh(int argc, char **argv)
630 ZOOM_options options = ZOOM_options_create();
632 ZOOM_connection z39_con[MAX_CON];
633 ZOOM_resultset z39_res[MAX_CON];
635 for (i = 0; i<MAX_CON; i++)
641 for (i = 0; i<MAX_CON; i++)
645 for (i = 1; i<argc; i++)
647 const char *bp = argv[i];
648 res = cmd_parse(z39_con, z39_res, options, &bp);
649 if (res == 0) /* received quit */
652 if (res) /* do cmdline shell only if not quitting */
653 shell(z39_con, z39_res, options);
654 ZOOM_options_destroy(options);
656 for (i = 0; i<MAX_CON; i++)
658 ZOOM_connection_destroy(z39_con[i]);
659 ZOOM_resultset_destroy(z39_res[i]);
663 int main(int argc, char **argv)
665 const char *maskstr = 0;
666 if (argc > 2 && !strcmp(argv[1], "-v"))
672 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
680 int mask = yaz_log_mask_str(maskstr);
681 yaz_log_init_level(mask);
689 * c-file-style: "Stroustrup"
690 * indent-tabs-mode: nil
692 * vim: shiftwidth=4 tabstop=8 expandtab