2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.50 2007-09-11 15:16:20 adam Exp $
9 \brief ZOOM C command line tool (shell)
17 #include <yaz/comstack.h>
19 #if HAVE_READLINE_READLINE_H
20 #include <readline/readline.h>
22 #if HAVE_READLINE_HISTORY_H
23 #include <readline/history.h>
26 #include <yaz/xmalloc.h>
34 static int next_token(const char **cpp, const char **t_start)
37 const char *cp = *cpp;
44 while (*cp && *cp != '"')
55 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
64 return len; /* return -1 if no token was read .. */
67 static int next_token_copy(const char **cpp, char *buf_out, int buf_max)
70 int len = next_token(cpp, &start);
78 memcpy(buf_out, start, len);
83 static int is_command(const char *cmd_str, const char *this_str, int this_len)
85 int cmd_len = strlen(cmd_str);
86 if (cmd_len != this_len)
88 if (memcmp(cmd_str, this_str, cmd_len))
93 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
97 char key[40], val[80];
99 if (next_token_copy(args, key, sizeof(key)) < 0)
101 printf("missing argument for set\n");
104 if (next_token_copy(args, val, sizeof(val)) < 0)
105 ZOOM_options_set(options, key, 0);
107 ZOOM_options_set(options, key, val);
110 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
111 ZOOM_options options,
115 if (next_token_copy(args, key, sizeof(key)) < 0)
117 printf("missing argument for get\n");
121 const char *val = ZOOM_options_get(options, key);
122 printf("%s = %s\n", key, val ? val : "<null>");
126 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
127 ZOOM_options options,
131 if (next_token_copy(args, key, sizeof(key)) < 0)
133 printf("missing argument for get\n");
138 for (i = 0; i<MAX_CON; i++)
144 val = ZOOM_resultset_option_get(r[i], key);
145 printf("%s = %s\n", key, val ? val : "<null>");
150 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
151 ZOOM_options options,
156 next_token_copy(args, host, sizeof(host));
157 for (i = 0; i<MAX_CON; i++)
162 if ((h = ZOOM_connection_option_get(c[i], "host"))
165 ZOOM_connection_destroy(c[i]);
168 else if (*host == '\0')
170 ZOOM_connection_destroy(c[i]);
176 static void display_records(ZOOM_connection c,
178 int start, int count)
181 for (i = 0; i<count; i++)
184 ZOOM_record rec = ZOOM_resultset_record(r, pos);
185 const char *db = ZOOM_record_get(rec, "database", 0);
187 if (ZOOM_record_error(rec, 0, 0, 0))
192 int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
194 printf("%d %s: %s (%s:%d) %s\n", pos, (db ? db : "unknown"),
195 msg, diagset, error, addinfo ? addinfo : "none");
200 const char *render = ZOOM_record_get(rec, "render", &len);
201 const char *opac_render = ZOOM_record_get(rec, "opac", &opac_len);
202 const char *syntax = ZOOM_record_get(rec, "syntax", 0);
203 const char *schema = ZOOM_record_get(rec, "schema", 0);
204 /* if rec is non-null, we got a record for display */
207 printf("%d database=%s syntax=%s schema=%s\n",
208 pos, (db ? db : "unknown"), syntax,
209 schema ? schema : "unknown");
211 fwrite(render, 1, len, stdout);
214 fwrite(opac_render, 1, opac_len, stdout);
220 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
221 ZOOM_options options,
225 char start_str[10], count_str[10];
227 if (next_token_copy(args, start_str, sizeof(start_str)) >= 0)
228 ZOOM_options_set(options, "start", start_str);
230 if (next_token_copy(args, count_str, sizeof(count_str)) >= 0)
231 ZOOM_options_set(options, "count", count_str);
233 for (i = 0; i<MAX_CON; i++)
234 ZOOM_resultset_records(r[i], 0, atoi(start_str), atoi(count_str));
235 while (ZOOM_event(MAX_CON, c))
238 for (i = 0; i<MAX_CON; i++)
241 const char *errmsg, *addinfo, *dset;
242 /* display errors if any */
245 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
246 printf("%s error: %s (%s:%d) %s\n",
247 ZOOM_connection_option_get(c[i], "host"), errmsg,
248 dset, error, addinfo);
251 /* OK, no major errors. Display records... */
252 int start = ZOOM_options_get_int(options, "start", 0);
253 int count = ZOOM_options_get_int(options, "count", 0);
254 display_records(c[i], r[i], start, count);
257 ZOOM_options_set(options, "count", "0");
258 ZOOM_options_set(options, "start", "0");
261 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
262 ZOOM_options options,
265 ZOOM_package p[MAX_CON];
266 char ext_type_str[10];
270 if (next_token_copy(args, ext_type_str, sizeof(ext_type_str)) < 0)
273 for (i = 0; i<MAX_CON; i++)
277 p[i] = ZOOM_connection_package(c[i], 0);
278 ZOOM_package_send(p[i], ext_type_str);
284 while (ZOOM_event(MAX_CON, c))
287 for (i = 0; i<MAX_CON; i++)
290 const char *errmsg, *addinfo, *dset;
291 /* display errors if any */
294 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
295 printf("%s error: %s (%s:%d) %s\n",
296 ZOOM_connection_option_get(c[i], "host"), errmsg,
297 dset, error, addinfo);
302 v = ZOOM_package_option_get(p[i], "targetReference");
304 printf("targetReference: %s\n", v);
305 v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
307 printf("xmlUpdateDoc: %s\n", v);
309 ZOOM_package_destroy(p[i]);
313 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
314 ZOOM_options options,
317 yaz_log_init_level(YLOG_ALL);
320 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
321 ZOOM_options options,
325 const char *query_str = *args;
328 s = ZOOM_query_create();
329 while (*query_str == ' ')
331 if (memcmp(query_str, "cql:", 4) == 0)
333 ZOOM_query_cql(s, query_str + 4);
335 else if (ZOOM_query_prefix(s, query_str))
337 printf("Bad PQF: %s\n", query_str);
340 for (i = 0; i<MAX_CON; i++)
345 ZOOM_resultset_destroy(r[i]);
349 r[i] = ZOOM_connection_search(c[i], s);
351 ZOOM_query_destroy(s);
353 while (ZOOM_event(MAX_CON, c))
356 for (i = 0; i<MAX_CON; i++)
359 const char *errmsg, *addinfo, *dset;
360 /* display errors if any */
363 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
364 printf("%s error: %s (%s:%d) %s\n",
365 ZOOM_connection_option_get(c[i], "host"), errmsg,
366 dset, error, addinfo);
369 /* OK, no major errors. Look at the result count */
370 int start = ZOOM_options_get_int(options, "start", 0);
371 int count = ZOOM_options_get_int(options, "count", 0);
373 printf("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
374 (long) ZOOM_resultset_size(r[i]));
376 display_records(c[i], r[i], start, count);
381 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
382 ZOOM_options options,
385 const char *query_str = *args;
386 ZOOM_query query = ZOOM_query_create();
388 ZOOM_scanset s[MAX_CON];
390 while (*query_str == ' ')
393 if (memcmp(query_str, "cql:", 4) == 0)
395 ZOOM_query_cql(query, query_str + 4);
397 else if (ZOOM_query_prefix(query, query_str))
399 printf("Bad PQF: %s\n", query_str);
403 for (i = 0; i<MAX_CON; i++)
406 s[i] = ZOOM_connection_scan1(c[i], query);
410 ZOOM_query_destroy(query);
412 while (ZOOM_event(MAX_CON, c))
414 for (i = 0; i<MAX_CON; i++)
417 const char *errmsg, *addinfo, *dset;
418 /* display errors if any */
421 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
422 printf("%s error: %s (%s:%d) %s\n",
423 ZOOM_connection_option_get(c[i], "host"), errmsg,
424 dset, error, addinfo);
427 size_t p, sz = ZOOM_scanset_size(s[i]);
428 for (p = 0; p < sz; p++)
432 const char *term = ZOOM_scanset_display_term(s[i], p,
434 fwrite(term, 1, len, stdout);
435 printf(" %d\n", occ);
437 ZOOM_scanset_destroy(s[i]);
442 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
443 ZOOM_options options,
446 const char *sort_spec = *args;
449 while (*sort_spec == ' ')
452 for (i = 0; i<MAX_CON; i++)
455 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
457 while (ZOOM_event(MAX_CON, c))
461 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
462 ZOOM_options options,
465 printf("connect <zurl>\n");
466 printf("search <pqf>\n");
467 printf("show [<start> [<count>]\n");
468 printf("scan <term>\n");
470 printf("close <zurl>\n");
471 printf("ext <type>\n");
472 printf("set <option> [<value>]\n");
473 printf("get <option>\n");
475 printf("options:\n");
478 printf(" databaseName\n");
479 printf(" preferredRecordSyntax\n");
481 printf(" elementSetName\n");
482 printf(" maximumRecordSize\n");
483 printf(" preferredRecordSize\n");
485 printf(" piggyback\n");
488 printf(" password\n");
489 printf(" implementationName\n");
490 printf(" charset\n");
494 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
495 ZOOM_options options,
499 const char *errmsg, *addinfo, *dset;
502 if (next_token_copy(args, host, sizeof(host)) < 0)
504 printf("missing host after connect\n");
507 for (j = -1, i = 0; i<MAX_CON; i++)
510 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
513 ZOOM_connection_destroy(c[i]);
516 else if (c[i] == 0 && j == -1)
519 if (i == MAX_CON) /* no match .. */
523 printf("no more connection available\n");
526 i = j; /* OK, use this one is available */
528 c[i] = ZOOM_connection_create(options);
529 ZOOM_connection_connect(c[i], host, 0);
531 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
532 printf("%s error: %s (%s:%d) %s\n",
533 ZOOM_connection_option_get(c[i], "host"), errmsg,
534 dset, error, addinfo);
537 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
538 ZOOM_options options,
544 cmd_len = next_token(buf, &cmd_str);
547 if (is_command("quit", cmd_str, cmd_len))
549 else if (is_command("set", cmd_str, cmd_len))
550 cmd_set(c, r, options, buf);
551 else if (is_command("get", cmd_str, cmd_len))
552 cmd_get(c, r, options, buf);
553 else if (is_command("rget", cmd_str, cmd_len))
554 cmd_rget(c, r, options, buf);
555 else if (is_command("connect", cmd_str, cmd_len))
556 cmd_connect(c, r, options, buf);
557 else if (is_command("open", cmd_str, cmd_len))
558 cmd_connect(c, r, options, buf);
559 else if (is_command("search", cmd_str, cmd_len))
560 cmd_search(c, r, options, buf);
561 else if (is_command("find", cmd_str, cmd_len))
562 cmd_search(c, r, options, buf);
563 else if (is_command("show", cmd_str, cmd_len))
564 cmd_show(c, r, options, buf);
565 else if (is_command("close", cmd_str, cmd_len))
566 cmd_close(c, r, options, buf);
567 else if (is_command("help", cmd_str, cmd_len))
568 cmd_help(c, r, options, buf);
569 else if (is_command("ext", cmd_str, cmd_len))
570 cmd_ext(c, r, options, buf);
571 else if (is_command("debug", cmd_str, cmd_len))
572 cmd_debug(c, r, options, buf);
573 else if (is_command("scan", cmd_str, cmd_len))
574 cmd_scan(c, r, options, buf);
575 else if (is_command("sort", cmd_str, cmd_len))
576 cmd_sort(c, r, options, buf);
578 printf("unknown command %.*s\n", cmd_len, cmd_str);
582 void shell(ZOOM_connection *c, ZOOM_resultset *r,
583 ZOOM_options options)
589 const char *bp = buf;
590 #if HAVE_READLINE_READLINE_H
592 line_in=readline("ZOOM>");
595 #if HAVE_READLINE_HISTORY_H
597 add_history(line_in);
599 if(strlen(line_in) > 999) {
600 printf("Input line too long\n");
606 printf("ZOOM>"); fflush(stdout);
607 if (!fgets(buf, 999, stdin))
610 if ((cp = strchr(buf, '\n')))
612 if (!cmd_parse(c, r, options, &bp))
617 static void zoomsh(int argc, char **argv)
619 ZOOM_options options = ZOOM_options_create();
621 ZOOM_connection z39_con[MAX_CON];
622 ZOOM_resultset z39_res[MAX_CON];
624 for (i = 0; i<MAX_CON; i++)
630 for (i = 0; i<MAX_CON; i++)
634 for (i = 1; i<argc; i++)
636 const char *bp = argv[i];
637 res = cmd_parse(z39_con, z39_res, options, &bp);
638 if (res == 0) /* received quit */
641 if (res) /* do cmdline shell only if not quitting */
642 shell(z39_con, z39_res, options);
643 ZOOM_options_destroy(options);
645 for (i = 0; i<MAX_CON; i++)
647 ZOOM_connection_destroy(z39_con[i]);
648 ZOOM_resultset_destroy(z39_res[i]);
652 int main(int argc, char **argv)
654 const char *maskstr = 0;
655 if (argc > 2 && !strcmp(argv[1], "-v"))
661 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
669 int mask = yaz_log_mask_str(maskstr);
670 yaz_log_init_level(mask);
678 * indent-tabs-mode: nil
680 * vim: shiftwidth=4 tabstop=8 expandtab