2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.39 2005-11-02 21:41:27 adam Exp $
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>
33 static int next_token (const char **cpp, const char **t_start)
36 const char *cp = *cpp;
43 while (*cp && *cp != '"')
54 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
63 return len; /* return -1 if no token was read .. */
66 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
69 int len = next_token (cpp, &start);
77 memcpy (buf_out, start, len);
82 static int is_command (const char *cmd_str, const char *this_str, int this_len)
84 int cmd_len = strlen(cmd_str);
85 if (cmd_len != this_len)
87 if (memcmp (cmd_str, this_str, cmd_len))
92 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
96 char key[40], val[80];
98 if (next_token_copy (args, key, sizeof(key)) < 0)
100 printf ("missing argument for set\n");
103 if (next_token_copy (args, val, sizeof(val)) < 0)
104 ZOOM_options_set(options, key, 0);
106 ZOOM_options_set(options, key, val);
109 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
110 ZOOM_options options,
114 if (next_token_copy (args, key, sizeof(key)) < 0)
116 printf ("missing argument for get\n");
120 const char *val = ZOOM_options_get(options, key);
121 printf ("%s = %s\n", key, val ? val : "<null>");
125 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
126 ZOOM_options options,
130 if (next_token_copy (args, key, sizeof(key)) < 0)
132 printf ("missing argument for get\n");
137 for (i = 0; i<MAX_CON; i++)
143 val = ZOOM_resultset_option_get(r[i], key);
144 printf ("%s = %s\n", key, val ? val : "<null>");
149 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
150 ZOOM_options options,
155 next_token_copy (args, host, sizeof(host));
156 for (i = 0; i<MAX_CON; i++)
161 if ((h = ZOOM_connection_option_get(c[i], "host"))
162 && !strcmp (h, host))
164 ZOOM_connection_destroy (c[i]);
167 else if (*host == '\0')
169 ZOOM_connection_destroy (c[i]);
175 static void display_records (ZOOM_connection c,
177 int start, int count)
180 for (i = 0; i<count; i++)
183 ZOOM_record rec = ZOOM_resultset_record (r, pos);
184 const char *db = ZOOM_record_get (rec, "database", 0);
186 const char *render = ZOOM_record_get (rec, "render", &len);
187 const char *opac_render = ZOOM_record_get (rec, "opac", &opac_len);
188 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
189 /* if rec is non-null, we got a record for display */
193 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
194 printf ("%d %s %s (%s)\n",
195 pos+1, (db ? db : "unknown"), syntax, oidbuf);
197 fwrite (render, 1, len, stdout);
200 fwrite (opac_render, 1, opac_len, stdout);
206 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
207 ZOOM_options options,
211 char start_str[10], count_str[10];
213 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
214 ZOOM_options_set (options, "start", start_str);
216 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
217 ZOOM_options_set (options, "count", count_str);
219 for (i = 0; i<MAX_CON; i++)
220 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
221 while (ZOOM_event (MAX_CON, c))
224 for (i = 0; i<MAX_CON; i++)
227 const char *errmsg, *addinfo, *dset;
228 /* display errors if any */
231 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
232 printf ("%s error: %s (%s:%d) %s\n",
233 ZOOM_connection_option_get(c[i], "host"), errmsg,
234 dset, error, addinfo);
237 /* OK, no major errors. Display records... */
238 int start = ZOOM_options_get_int (options, "start", 0);
239 int count = ZOOM_options_get_int (options, "count", 0);
240 display_records (c[i], r[i], start, count);
243 ZOOM_options_set (options, "count", "0");
244 ZOOM_options_set (options, "start", "0");
247 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
248 ZOOM_options options,
251 ZOOM_package p[MAX_CON];
252 char ext_type_str[10];
256 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
259 for (i = 0; i<MAX_CON; i++)
263 p[i] = ZOOM_connection_package (c[i], 0);
264 ZOOM_package_send(p[i], ext_type_str);
270 while (ZOOM_event (MAX_CON, c))
273 for (i = 0; i<MAX_CON; i++)
276 const char *errmsg, *addinfo, *dset;
277 /* display errors if any */
280 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
281 printf ("%s error: %s (%s:%d) %s\n",
282 ZOOM_connection_option_get(c[i], "host"), errmsg,
283 dset, error, addinfo);
288 v = ZOOM_package_option_get (p[i], "targetReference");
290 printf("targetReference: %s\n", v);
291 v = ZOOM_package_option_get (p[i], "xmlUpdateDoc");
293 printf("xmlUpdateDoc: %s\n", v);
295 ZOOM_package_destroy (p[i]);
299 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
300 ZOOM_options options,
303 yaz_log_init_level(YLOG_ALL);
306 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
307 ZOOM_options options,
311 const char *query_str = *args;
314 s = ZOOM_query_create ();
315 while (*query_str == ' ')
317 if (memcmp(query_str, "cql:", 4) == 0)
319 ZOOM_query_cql (s, query_str + 4);
321 else if (ZOOM_query_prefix (s, query_str))
323 printf ("Bad PQF: %s\n", query_str);
326 for (i = 0; i<MAX_CON; i++)
330 ZOOM_resultset_destroy (r[i]);
334 r[i] = ZOOM_connection_search (c[i], s);
337 while (ZOOM_event (MAX_CON, c))
340 for (i = 0; i<MAX_CON; i++)
343 const char *errmsg, *addinfo, *dset;
344 /* display errors if any */
347 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
348 printf ("%s error: %s (%s:%d) %s\n",
349 ZOOM_connection_option_get(c[i], "host"), errmsg,
350 dset, error, addinfo);
353 /* OK, no major errors. Look at the result count */
354 int start = ZOOM_options_get_int (options, "start", 0);
355 int count = ZOOM_options_get_int (options, "count", 0);
357 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
358 ZOOM_resultset_size(r[i]));
360 display_records (c[i], r[i], start, count);
363 ZOOM_query_destroy (s);
366 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
367 ZOOM_options options,
370 const char *start_term = *args;
372 ZOOM_scanset s[MAX_CON];
374 while (*start_term == ' ')
377 for (i = 0; i<MAX_CON; i++)
380 s[i] = ZOOM_connection_scan(c[i], start_term);
384 while (ZOOM_event(MAX_CON, c))
386 for (i = 0; i<MAX_CON; i++)
389 size_t p, sz = ZOOM_scanset_size(s[i]);
390 for (p = 0; p < sz; p++)
394 const char *term = ZOOM_scanset_display_term(s[i], p,
396 fwrite(term, 1, len, stdout);
397 printf (" %d\n", occ);
399 ZOOM_scanset_destroy(s[i]);
404 static void cmd_sort (ZOOM_connection *c, ZOOM_resultset *r,
405 ZOOM_options options,
408 const char *sort_spec = *args;
411 while (*sort_spec == ' ')
414 for (i = 0; i<MAX_CON; i++)
417 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
419 while (ZOOM_event(MAX_CON, c))
423 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
424 ZOOM_options options,
427 printf ("connect <zurl>\n");
428 printf ("search <pqf>\n");
429 printf ("show [<start> [<count>]\n");
430 printf ("scan <term>\n");
432 printf ("close <zurl>\n");
433 printf ("ext <type>\n");
434 printf ("set <option> [<value>]\n");
435 printf ("get <option>\n");
437 printf ("options:\n");
440 printf (" databaseName\n");
441 printf (" preferredRecordSyntax\n");
443 printf (" elementSetName\n");
444 printf (" maximumRecordSize\n");
445 printf (" preferredRecordSize\n");
447 printf (" piggyback\n");
450 printf (" password\n");
451 printf (" implementationName\n");
452 printf (" charset\n");
456 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
457 ZOOM_options options,
461 const char *errmsg, *addinfo, *dset;
464 if (next_token_copy (args, host, sizeof(host)) < 0)
466 printf ("missing host after connect\n");
469 for (j = -1, i = 0; i<MAX_CON; i++)
472 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
475 ZOOM_connection_destroy (c[i]);
478 else if (c[i] == 0 && j == -1)
481 if (i == MAX_CON) /* no match .. */
485 printf ("no more connection available\n");
488 i = j; /* OK, use this one is available */
490 c[i] = ZOOM_connection_create (options);
491 ZOOM_connection_connect (c[i], host, 0);
493 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
494 printf ("%s error: %s (%s:%d) %s\n",
495 ZOOM_connection_option_get(c[i], "host"), errmsg,
496 dset, error, addinfo);
499 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
500 ZOOM_options options,
506 cmd_len = next_token (buf, &cmd_str);
509 if (is_command ("quit", cmd_str, cmd_len))
511 else if (is_command ("set", cmd_str, cmd_len))
512 cmd_set (c, r, options, buf);
513 else if (is_command ("get", cmd_str, cmd_len))
514 cmd_get (c, r, options, buf);
515 else if (is_command ("rget", cmd_str, cmd_len))
516 cmd_rget (c, r, options, buf);
517 else if (is_command ("connect", cmd_str, cmd_len))
518 cmd_connect (c, r, options, buf);
519 else if (is_command ("open", cmd_str, cmd_len))
520 cmd_connect (c, r, options, buf);
521 else if (is_command ("search", cmd_str, cmd_len))
522 cmd_search (c, r, options, buf);
523 else if (is_command ("find", cmd_str, cmd_len))
524 cmd_search (c, r, options, buf);
525 else if (is_command ("show", cmd_str, cmd_len))
526 cmd_show (c, r, options, buf);
527 else if (is_command ("close", cmd_str, cmd_len))
528 cmd_close (c, r, options, buf);
529 else if (is_command ("help", cmd_str, cmd_len))
530 cmd_help(c, r, options, buf);
531 else if (is_command ("ext", cmd_str, cmd_len))
532 cmd_ext(c, r, options, buf);
533 else if (is_command ("debug", cmd_str, cmd_len))
534 cmd_debug(c, r, options, buf);
535 else if (is_command ("scan", cmd_str, cmd_len))
536 cmd_scan(c, r, options, buf);
537 else if (is_command ("sort", cmd_str, cmd_len))
538 cmd_sort(c, r, options, buf);
540 printf ("unknown command %.*s\n", cmd_len, cmd_str);
544 void shell(ZOOM_connection *c, ZOOM_resultset *r,
545 ZOOM_options options)
551 const char *bp = buf;
552 #if HAVE_READLINE_READLINE_H
554 line_in=readline("ZOOM>");
557 #if HAVE_READLINE_HISTORY_H
559 add_history(line_in);
561 if(strlen(line_in) > 999) {
562 printf("Input line too long\n");
568 printf ("ZOOM>"); fflush (stdout);
569 if (!fgets (buf, 999, stdin))
572 if ((cp = strchr(buf, '\n')))
574 if (!cmd_parse (c, r, options, &bp))
579 static void zoomsh(int argc, char **argv)
581 ZOOM_options options = ZOOM_options_create();
583 ZOOM_connection z39_con[MAX_CON];
584 ZOOM_resultset z39_res[MAX_CON];
586 for (i = 0; i<MAX_CON; i++)
592 for (i = 0; i<MAX_CON; i++)
596 for (i = 1; i<argc; i++)
598 const char *bp = argv[i];
599 res = cmd_parse(z39_con, z39_res, options, &bp);
600 if (res == 0) /* received quit */
603 if (res) /* do cmdline shell only if not quitting */
604 shell(z39_con, z39_res, options);
605 ZOOM_options_destroy(options);
607 for (i = 0; i<MAX_CON; i++)
609 ZOOM_connection_destroy(z39_con[i]);
610 ZOOM_resultset_destroy(z39_res[i]);
614 int main(int argc, char **argv)
616 const char *maskstr = 0;
617 if (argc > 2 && !strcmp(argv[1], "-v"))
623 else if (argc > 1 && !strncmp(argv[1], "-v", 2))
631 int mask = yaz_log_mask_str(maskstr);
632 yaz_log_init_level(mask);
642 * indent-tabs-mode: nil
644 * vim: shiftwidth=4 tabstop=8 expandtab