2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.46 2007-04-12 13:52:58 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"))
163 && !strcmp (h, 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);
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 /* if rec is non-null, we got a record for display */
206 printf ("%d %s %s\n",
207 pos, (db ? db : "unknown"), syntax);
209 fwrite (render, 1, len, stdout);
212 fwrite (opac_render, 1, opac_len, stdout);
219 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
220 ZOOM_options options,
224 char start_str[10], count_str[10];
226 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
227 ZOOM_options_set (options, "start", start_str);
229 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
230 ZOOM_options_set (options, "count", count_str);
232 for (i = 0; i<MAX_CON; i++)
233 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
234 while (ZOOM_event (MAX_CON, c))
237 for (i = 0; i<MAX_CON; i++)
240 const char *errmsg, *addinfo, *dset;
241 /* display errors if any */
244 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
245 printf ("%s error: %s (%s:%d) %s\n",
246 ZOOM_connection_option_get(c[i], "host"), errmsg,
247 dset, error, addinfo);
250 /* OK, no major errors. Display records... */
251 int start = ZOOM_options_get_int (options, "start", 0);
252 int count = ZOOM_options_get_int (options, "count", 0);
253 display_records (c[i], r[i], start, count);
256 ZOOM_options_set (options, "count", "0");
257 ZOOM_options_set (options, "start", "0");
260 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
261 ZOOM_options options,
264 ZOOM_package p[MAX_CON];
265 char ext_type_str[10];
269 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
272 for (i = 0; i<MAX_CON; i++)
276 p[i] = ZOOM_connection_package (c[i], 0);
277 ZOOM_package_send(p[i], ext_type_str);
283 while (ZOOM_event (MAX_CON, c))
286 for (i = 0; i<MAX_CON; i++)
289 const char *errmsg, *addinfo, *dset;
290 /* display errors if any */
293 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
294 printf ("%s error: %s (%s:%d) %s\n",
295 ZOOM_connection_option_get(c[i], "host"), errmsg,
296 dset, error, addinfo);
301 v = ZOOM_package_option_get (p[i], "targetReference");
303 printf("targetReference: %s\n", v);
304 v = ZOOM_package_option_get (p[i], "xmlUpdateDoc");
306 printf("xmlUpdateDoc: %s\n", v);
308 ZOOM_package_destroy (p[i]);
312 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
313 ZOOM_options options,
316 yaz_log_init_level(YLOG_ALL);
319 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
320 ZOOM_options options,
324 const char *query_str = *args;
327 s = ZOOM_query_create ();
328 while (*query_str == ' ')
330 if (memcmp(query_str, "cql:", 4) == 0)
332 ZOOM_query_cql (s, query_str + 4);
334 else if (ZOOM_query_prefix (s, query_str))
336 printf ("Bad PQF: %s\n", query_str);
339 for (i = 0; i<MAX_CON; i++)
343 ZOOM_resultset_destroy (r[i]);
347 r[i] = ZOOM_connection_search (c[i], s);
350 while (ZOOM_event (MAX_CON, c))
353 for (i = 0; i<MAX_CON; i++)
356 const char *errmsg, *addinfo, *dset;
357 /* display errors if any */
360 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
361 printf ("%s error: %s (%s:%d) %s\n",
362 ZOOM_connection_option_get(c[i], "host"), errmsg,
363 dset, error, addinfo);
366 /* OK, no major errors. Look at the result count */
367 int start = ZOOM_options_get_int (options, "start", 0);
368 int count = ZOOM_options_get_int (options, "count", 0);
370 printf ("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
371 (long) ZOOM_resultset_size(r[i]));
373 display_records (c[i], r[i], start, count);
376 ZOOM_query_destroy (s);
379 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
380 ZOOM_options options,
383 const char *start_term = *args;
385 ZOOM_scanset s[MAX_CON];
387 while (*start_term == ' ')
390 for (i = 0; i<MAX_CON; i++)
393 s[i] = ZOOM_connection_scan(c[i], start_term);
397 while (ZOOM_event(MAX_CON, c))
399 for (i = 0; i<MAX_CON; i++)
402 size_t p, sz = ZOOM_scanset_size(s[i]);
403 for (p = 0; p < sz; p++)
407 const char *term = ZOOM_scanset_display_term(s[i], p,
409 fwrite(term, 1, len, stdout);
410 printf (" %d\n", occ);
412 ZOOM_scanset_destroy(s[i]);
417 static void cmd_sort (ZOOM_connection *c, ZOOM_resultset *r,
418 ZOOM_options options,
421 const char *sort_spec = *args;
424 while (*sort_spec == ' ')
427 for (i = 0; i<MAX_CON; i++)
430 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
432 while (ZOOM_event(MAX_CON, c))
436 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
437 ZOOM_options options,
440 printf ("connect <zurl>\n");
441 printf ("search <pqf>\n");
442 printf ("show [<start> [<count>]\n");
443 printf ("scan <term>\n");
445 printf ("close <zurl>\n");
446 printf ("ext <type>\n");
447 printf ("set <option> [<value>]\n");
448 printf ("get <option>\n");
450 printf ("options:\n");
453 printf (" databaseName\n");
454 printf (" preferredRecordSyntax\n");
456 printf (" elementSetName\n");
457 printf (" maximumRecordSize\n");
458 printf (" preferredRecordSize\n");
460 printf (" piggyback\n");
463 printf (" password\n");
464 printf (" implementationName\n");
465 printf (" charset\n");
469 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
470 ZOOM_options options,
474 const char *errmsg, *addinfo, *dset;
477 if (next_token_copy (args, host, sizeof(host)) < 0)
479 printf ("missing host after connect\n");
482 for (j = -1, i = 0; i<MAX_CON; i++)
485 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
488 ZOOM_connection_destroy (c[i]);
491 else if (c[i] == 0 && j == -1)
494 if (i == MAX_CON) /* no match .. */
498 printf ("no more connection available\n");
501 i = j; /* OK, use this one is available */
503 c[i] = ZOOM_connection_create (options);
504 ZOOM_connection_connect (c[i], host, 0);
506 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
507 printf ("%s error: %s (%s:%d) %s\n",
508 ZOOM_connection_option_get(c[i], "host"), errmsg,
509 dset, error, addinfo);
512 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
513 ZOOM_options options,
519 cmd_len = next_token (buf, &cmd_str);
522 if (is_command ("quit", cmd_str, cmd_len))
524 else if (is_command ("set", cmd_str, cmd_len))
525 cmd_set (c, r, options, buf);
526 else if (is_command ("get", cmd_str, cmd_len))
527 cmd_get (c, r, options, buf);
528 else if (is_command ("rget", cmd_str, cmd_len))
529 cmd_rget (c, r, options, buf);
530 else if (is_command ("connect", cmd_str, cmd_len))
531 cmd_connect (c, r, options, buf);
532 else if (is_command ("open", cmd_str, cmd_len))
533 cmd_connect (c, r, options, buf);
534 else if (is_command ("search", cmd_str, cmd_len))
535 cmd_search (c, r, options, buf);
536 else if (is_command ("find", cmd_str, cmd_len))
537 cmd_search (c, r, options, buf);
538 else if (is_command ("show", cmd_str, cmd_len))
539 cmd_show (c, r, options, buf);
540 else if (is_command ("close", cmd_str, cmd_len))
541 cmd_close (c, r, options, buf);
542 else if (is_command ("help", cmd_str, cmd_len))
543 cmd_help(c, r, options, buf);
544 else if (is_command ("ext", cmd_str, cmd_len))
545 cmd_ext(c, r, options, buf);
546 else if (is_command ("debug", cmd_str, cmd_len))
547 cmd_debug(c, r, options, buf);
548 else if (is_command ("scan", cmd_str, cmd_len))
549 cmd_scan(c, r, options, buf);
550 else if (is_command ("sort", cmd_str, cmd_len))
551 cmd_sort(c, r, options, buf);
553 printf ("unknown command %.*s\n", cmd_len, cmd_str);
557 void shell(ZOOM_connection *c, ZOOM_resultset *r,
558 ZOOM_options options)
564 const char *bp = buf;
565 #if HAVE_READLINE_READLINE_H
567 line_in=readline("ZOOM>");
570 #if HAVE_READLINE_HISTORY_H
572 add_history(line_in);
574 if(strlen(line_in) > 999) {
575 printf("Input line too long\n");
581 printf ("ZOOM>"); fflush (stdout);
582 if (!fgets (buf, 999, stdin))
585 if ((cp = strchr(buf, '\n')))
587 if (!cmd_parse (c, r, options, &bp))
592 static void zoomsh(int argc, char **argv)
594 ZOOM_options options = ZOOM_options_create();
596 ZOOM_connection z39_con[MAX_CON];
597 ZOOM_resultset z39_res[MAX_CON];
599 for (i = 0; i<MAX_CON; i++)
605 for (i = 0; i<MAX_CON; i++)
609 for (i = 1; i<argc; i++)
611 const char *bp = argv[i];
612 res = cmd_parse(z39_con, z39_res, options, &bp);
613 if (res == 0) /* received quit */
616 if (res) /* do cmdline shell only if not quitting */
617 shell(z39_con, z39_res, options);
618 ZOOM_options_destroy(options);
620 for (i = 0; i<MAX_CON; i++)
622 ZOOM_connection_destroy(z39_con[i]);
623 ZOOM_resultset_destroy(z39_res[i]);
627 int main(int argc, char **argv)
629 const char *maskstr = 0;
630 if (argc > 2 && !strcmp(argv[1], "-v"))
636 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
644 int mask = yaz_log_mask_str(maskstr);
645 yaz_log_init_level(mask);
655 * indent-tabs-mode: nil
657 * vim: shiftwidth=4 tabstop=8 expandtab