2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.40 2006-04-21 10:28:07 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>
35 static int next_token (const char **cpp, const char **t_start)
38 const char *cp = *cpp;
45 while (*cp && *cp != '"')
56 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
65 return len; /* return -1 if no token was read .. */
68 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
71 int len = next_token (cpp, &start);
79 memcpy (buf_out, start, len);
84 static int is_command (const char *cmd_str, const char *this_str, int this_len)
86 int cmd_len = strlen(cmd_str);
87 if (cmd_len != this_len)
89 if (memcmp (cmd_str, this_str, cmd_len))
94 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
98 char key[40], val[80];
100 if (next_token_copy (args, key, sizeof(key)) < 0)
102 printf ("missing argument for set\n");
105 if (next_token_copy (args, val, sizeof(val)) < 0)
106 ZOOM_options_set(options, key, 0);
108 ZOOM_options_set(options, key, val);
111 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
112 ZOOM_options options,
116 if (next_token_copy (args, key, sizeof(key)) < 0)
118 printf ("missing argument for get\n");
122 const char *val = ZOOM_options_get(options, key);
123 printf ("%s = %s\n", key, val ? val : "<null>");
127 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
128 ZOOM_options options,
132 if (next_token_copy (args, key, sizeof(key)) < 0)
134 printf ("missing argument for get\n");
139 for (i = 0; i<MAX_CON; i++)
145 val = ZOOM_resultset_option_get(r[i], key);
146 printf ("%s = %s\n", key, val ? val : "<null>");
151 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
152 ZOOM_options options,
157 next_token_copy (args, host, sizeof(host));
158 for (i = 0; i<MAX_CON; i++)
163 if ((h = ZOOM_connection_option_get(c[i], "host"))
164 && !strcmp (h, host))
166 ZOOM_connection_destroy (c[i]);
169 else if (*host == '\0')
171 ZOOM_connection_destroy (c[i]);
177 static void display_records (ZOOM_connection c,
179 int start, int count)
182 for (i = 0; i<count; i++)
185 ZOOM_record rec = ZOOM_resultset_record (r, pos);
186 const char *db = ZOOM_record_get (rec, "database", 0);
188 const char *render = ZOOM_record_get (rec, "render", &len);
189 const char *opac_render = ZOOM_record_get (rec, "opac", &opac_len);
190 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
191 /* if rec is non-null, we got a record for display */
195 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
196 printf ("%d %s %s (%s)\n",
197 pos+1, (db ? db : "unknown"), syntax, oidbuf);
199 fwrite (render, 1, len, stdout);
202 fwrite (opac_render, 1, opac_len, stdout);
208 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
209 ZOOM_options options,
213 char start_str[10], count_str[10];
215 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
216 ZOOM_options_set (options, "start", start_str);
218 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
219 ZOOM_options_set (options, "count", count_str);
221 for (i = 0; i<MAX_CON; i++)
222 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
223 while (ZOOM_event (MAX_CON, c))
226 for (i = 0; i<MAX_CON; i++)
229 const char *errmsg, *addinfo, *dset;
230 /* display errors if any */
233 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
234 printf ("%s error: %s (%s:%d) %s\n",
235 ZOOM_connection_option_get(c[i], "host"), errmsg,
236 dset, error, addinfo);
239 /* OK, no major errors. Display records... */
240 int start = ZOOM_options_get_int (options, "start", 0);
241 int count = ZOOM_options_get_int (options, "count", 0);
242 display_records (c[i], r[i], start, count);
245 ZOOM_options_set (options, "count", "0");
246 ZOOM_options_set (options, "start", "0");
249 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
250 ZOOM_options options,
253 ZOOM_package p[MAX_CON];
254 char ext_type_str[10];
258 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
261 for (i = 0; i<MAX_CON; i++)
265 p[i] = ZOOM_connection_package (c[i], 0);
266 ZOOM_package_send(p[i], ext_type_str);
272 while (ZOOM_event (MAX_CON, c))
275 for (i = 0; i<MAX_CON; i++)
278 const char *errmsg, *addinfo, *dset;
279 /* display errors if any */
282 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
283 printf ("%s error: %s (%s:%d) %s\n",
284 ZOOM_connection_option_get(c[i], "host"), errmsg,
285 dset, error, addinfo);
290 v = ZOOM_package_option_get (p[i], "targetReference");
292 printf("targetReference: %s\n", v);
293 v = ZOOM_package_option_get (p[i], "xmlUpdateDoc");
295 printf("xmlUpdateDoc: %s\n", v);
297 ZOOM_package_destroy (p[i]);
301 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
302 ZOOM_options options,
305 yaz_log_init_level(YLOG_ALL);
308 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
309 ZOOM_options options,
313 const char *query_str = *args;
316 s = ZOOM_query_create ();
317 while (*query_str == ' ')
319 if (memcmp(query_str, "cql:", 4) == 0)
321 ZOOM_query_cql (s, query_str + 4);
323 else if (ZOOM_query_prefix (s, query_str))
325 printf ("Bad PQF: %s\n", query_str);
328 for (i = 0; i<MAX_CON; i++)
332 ZOOM_resultset_destroy (r[i]);
336 r[i] = ZOOM_connection_search (c[i], s);
339 while (ZOOM_event (MAX_CON, c))
342 for (i = 0; i<MAX_CON; i++)
345 const char *errmsg, *addinfo, *dset;
346 /* display errors if any */
349 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
350 printf ("%s error: %s (%s:%d) %s\n",
351 ZOOM_connection_option_get(c[i], "host"), errmsg,
352 dset, error, addinfo);
355 /* OK, no major errors. Look at the result count */
356 int start = ZOOM_options_get_int (options, "start", 0);
357 int count = ZOOM_options_get_int (options, "count", 0);
359 printf ("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
360 ZOOM_resultset_size(r[i]));
362 display_records (c[i], r[i], start, count);
365 ZOOM_query_destroy (s);
368 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
369 ZOOM_options options,
372 const char *start_term = *args;
374 ZOOM_scanset s[MAX_CON];
376 while (*start_term == ' ')
379 for (i = 0; i<MAX_CON; i++)
382 s[i] = ZOOM_connection_scan(c[i], start_term);
386 while (ZOOM_event(MAX_CON, c))
388 for (i = 0; i<MAX_CON; i++)
391 size_t p, sz = ZOOM_scanset_size(s[i]);
392 for (p = 0; p < sz; p++)
396 const char *term = ZOOM_scanset_display_term(s[i], p,
398 fwrite(term, 1, len, stdout);
399 printf (" %d\n", occ);
401 ZOOM_scanset_destroy(s[i]);
406 static void cmd_sort (ZOOM_connection *c, ZOOM_resultset *r,
407 ZOOM_options options,
410 const char *sort_spec = *args;
413 while (*sort_spec == ' ')
416 for (i = 0; i<MAX_CON; i++)
419 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
421 while (ZOOM_event(MAX_CON, c))
425 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
426 ZOOM_options options,
429 printf ("connect <zurl>\n");
430 printf ("search <pqf>\n");
431 printf ("show [<start> [<count>]\n");
432 printf ("scan <term>\n");
434 printf ("close <zurl>\n");
435 printf ("ext <type>\n");
436 printf ("set <option> [<value>]\n");
437 printf ("get <option>\n");
439 printf ("options:\n");
442 printf (" databaseName\n");
443 printf (" preferredRecordSyntax\n");
445 printf (" elementSetName\n");
446 printf (" maximumRecordSize\n");
447 printf (" preferredRecordSize\n");
449 printf (" piggyback\n");
452 printf (" password\n");
453 printf (" implementationName\n");
454 printf (" charset\n");
458 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
459 ZOOM_options options,
463 const char *errmsg, *addinfo, *dset;
466 if (next_token_copy (args, host, sizeof(host)) < 0)
468 printf ("missing host after connect\n");
471 for (j = -1, i = 0; i<MAX_CON; i++)
474 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
477 ZOOM_connection_destroy (c[i]);
480 else if (c[i] == 0 && j == -1)
483 if (i == MAX_CON) /* no match .. */
487 printf ("no more connection available\n");
490 i = j; /* OK, use this one is available */
492 c[i] = ZOOM_connection_create (options);
493 ZOOM_connection_connect (c[i], host, 0);
495 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
496 printf ("%s error: %s (%s:%d) %s\n",
497 ZOOM_connection_option_get(c[i], "host"), errmsg,
498 dset, error, addinfo);
501 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
502 ZOOM_options options,
508 cmd_len = next_token (buf, &cmd_str);
511 if (is_command ("quit", cmd_str, cmd_len))
513 else if (is_command ("set", cmd_str, cmd_len))
514 cmd_set (c, r, options, buf);
515 else if (is_command ("get", cmd_str, cmd_len))
516 cmd_get (c, r, options, buf);
517 else if (is_command ("rget", cmd_str, cmd_len))
518 cmd_rget (c, r, options, buf);
519 else if (is_command ("connect", cmd_str, cmd_len))
520 cmd_connect (c, r, options, buf);
521 else if (is_command ("open", cmd_str, cmd_len))
522 cmd_connect (c, r, options, buf);
523 else if (is_command ("search", cmd_str, cmd_len))
524 cmd_search (c, r, options, buf);
525 else if (is_command ("find", cmd_str, cmd_len))
526 cmd_search (c, r, options, buf);
527 else if (is_command ("show", cmd_str, cmd_len))
528 cmd_show (c, r, options, buf);
529 else if (is_command ("close", cmd_str, cmd_len))
530 cmd_close (c, r, options, buf);
531 else if (is_command ("help", cmd_str, cmd_len))
532 cmd_help(c, r, options, buf);
533 else if (is_command ("ext", cmd_str, cmd_len))
534 cmd_ext(c, r, options, buf);
535 else if (is_command ("debug", cmd_str, cmd_len))
536 cmd_debug(c, r, options, buf);
537 else if (is_command ("scan", cmd_str, cmd_len))
538 cmd_scan(c, r, options, buf);
539 else if (is_command ("sort", cmd_str, cmd_len))
540 cmd_sort(c, r, options, buf);
542 printf ("unknown command %.*s\n", cmd_len, cmd_str);
546 void shell(ZOOM_connection *c, ZOOM_resultset *r,
547 ZOOM_options options)
553 const char *bp = buf;
554 #if HAVE_READLINE_READLINE_H
556 line_in=readline("ZOOM>");
559 #if HAVE_READLINE_HISTORY_H
561 add_history(line_in);
563 if(strlen(line_in) > 999) {
564 printf("Input line too long\n");
570 printf ("ZOOM>"); fflush (stdout);
571 if (!fgets (buf, 999, stdin))
574 if ((cp = strchr(buf, '\n')))
576 if (!cmd_parse (c, r, options, &bp))
581 static void zoomsh(int argc, char **argv)
583 ZOOM_options options = ZOOM_options_create();
585 ZOOM_connection z39_con[MAX_CON];
586 ZOOM_resultset z39_res[MAX_CON];
588 for (i = 0; i<MAX_CON; i++)
594 for (i = 0; i<MAX_CON; i++)
598 for (i = 1; i<argc; i++)
600 const char *bp = argv[i];
601 res = cmd_parse(z39_con, z39_res, options, &bp);
602 if (res == 0) /* received quit */
605 if (res) /* do cmdline shell only if not quitting */
606 shell(z39_con, z39_res, options);
607 ZOOM_options_destroy(options);
609 for (i = 0; i<MAX_CON; i++)
611 ZOOM_connection_destroy(z39_con[i]);
612 ZOOM_resultset_destroy(z39_res[i]);
616 int main(int argc, char **argv)
618 const char *maskstr = 0;
619 if (argc > 2 && !strcmp(argv[1], "-v"))
625 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
633 int mask = yaz_log_mask_str(maskstr);
634 yaz_log_init_level(mask);
644 * indent-tabs-mode: nil
646 * vim: shiftwidth=4 tabstop=8 expandtab