2 * Copyright (c) 2002-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.31 2004-12-13 14:21:58 heikki 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_close (ZOOM_connection *c, ZOOM_resultset *r,
126 ZOOM_options options,
131 next_token_copy (args, host, sizeof(host));
132 for (i = 0; i<MAX_CON; i++)
137 if ((h = ZOOM_connection_option_get(c[i], "host"))
138 && !strcmp (h, host))
140 ZOOM_connection_destroy (c[i]);
143 else if (*host == '\0')
145 ZOOM_connection_destroy (c[i]);
151 static void display_records (ZOOM_connection c,
153 int start, int count)
156 for (i = 0; i<count; i++)
159 ZOOM_record rec = ZOOM_resultset_record (r, pos);
160 const char *db = ZOOM_record_get (rec, "database", 0);
162 const char *render = ZOOM_record_get (rec, "render", &len);
163 const char *opac_render = ZOOM_record_get (rec, "opac", &opac_len);
164 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
165 /* if rec is non-null, we got a record for display */
169 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
170 printf ("%d %s %s (%s)\n",
171 pos+1, (db ? db : "unknown"), syntax, oidbuf);
173 fwrite (render, 1, len, stdout);
176 fwrite (opac_render, 1, opac_len, stdout);
182 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
183 ZOOM_options options,
187 char start_str[10], count_str[10];
189 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
190 ZOOM_options_set (options, "start", start_str);
192 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
193 ZOOM_options_set (options, "count", count_str);
195 for (i = 0; i<MAX_CON; i++)
196 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
197 while (ZOOM_event (MAX_CON, c))
200 for (i = 0; i<MAX_CON; i++)
203 const char *errmsg, *addinfo, *dset;
204 /* display errors if any */
207 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
208 printf ("%s error: %s (%s:%d) %s\n",
209 ZOOM_connection_option_get(c[i], "host"), errmsg,
210 dset, error, addinfo);
213 /* OK, no major errors. Display records... */
214 int start = ZOOM_options_get_int (options, "start", 0);
215 int count = ZOOM_options_get_int (options, "count", 0);
216 display_records (c[i], r[i], start, count);
219 ZOOM_options_set (options, "count", "0");
220 ZOOM_options_set (options, "start", "0");
223 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
224 ZOOM_options options,
227 ZOOM_package p[MAX_CON];
228 char ext_type_str[10];
232 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
235 for (i = 0; i<MAX_CON; i++)
239 p[i] = ZOOM_connection_package (c[i], 0);
240 ZOOM_package_send(p[i], ext_type_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);
264 ZOOM_package_destroy (p[i]);
268 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
269 ZOOM_options options,
272 yaz_log_init_level(YLOG_ALL);
275 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
276 ZOOM_options options,
280 const char *query_str = *args;
283 s = ZOOM_query_create ();
284 while (*query_str == ' ')
286 if (memcmp(query_str, "cql:", 4) == 0)
288 ZOOM_query_cql (s, query_str + 4);
290 else if (ZOOM_query_prefix (s, query_str))
292 printf ("Bad PQF: %s\n", query_str);
295 for (i = 0; i<MAX_CON; i++)
299 ZOOM_resultset_destroy (r[i]);
303 r[i] = ZOOM_connection_search (c[i], s);
306 while (ZOOM_event (MAX_CON, c))
309 for (i = 0; i<MAX_CON; i++)
312 const char *errmsg, *addinfo, *dset;
313 /* display errors if any */
316 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
317 printf ("%s error: %s (%s:%d) %s\n",
318 ZOOM_connection_option_get(c[i], "host"), errmsg,
319 dset, error, addinfo);
322 /* OK, no major errors. Look at the result count */
323 int start = ZOOM_options_get_int (options, "start", 0);
324 int count = ZOOM_options_get_int (options, "count", 0);
326 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
327 ZOOM_resultset_size(r[i]));
329 display_records (c[i], r[i], start, count);
332 ZOOM_query_destroy (s);
335 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
336 ZOOM_options options,
339 const char *start_term = *args;
341 ZOOM_scanset s[MAX_CON];
343 while (*start_term == ' ')
346 for (i = 0; i<MAX_CON; i++)
349 s[i] = ZOOM_connection_scan(c[i], start_term);
353 while (ZOOM_event(MAX_CON, c))
355 for (i = 0; i<MAX_CON; i++)
358 size_t p, sz = ZOOM_scanset_size(s[i]);
359 for (p = 0; p < sz; p++)
363 const char *term = ZOOM_scanset_display_term(s[i], p,
365 fwrite(term, 1, len, stdout);
366 printf (" %d\n", occ);
368 ZOOM_scanset_destroy(s[i]);
373 static void cmd_sort (ZOOM_connection *c, ZOOM_resultset *r,
374 ZOOM_options options,
377 const char *sort_spec = *args;
380 while (*sort_spec == ' ')
383 for (i = 0; i<MAX_CON; i++)
386 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
388 while (ZOOM_event(MAX_CON, c))
392 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
393 ZOOM_options options,
396 printf ("connect <zurl>\n");
397 printf ("search <pqf>\n");
398 printf ("show [<start> [<count>]\n");
399 printf ("scan <term>\n");
401 printf ("close <zurl>\n");
402 printf ("set <option> [<value>]\n");
403 printf ("get <option>\n");
405 printf ("options:\n");
408 printf (" databaseName\n");
409 printf (" preferredRecordSyntax\n");
411 printf (" elementSetName\n");
412 printf (" maximumRecordSize\n");
413 printf (" preferredRecordSize\n");
415 printf (" piggyback\n");
418 printf (" password\n");
419 printf (" implementationName\n");
420 printf (" charset\n");
424 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
425 ZOOM_options options,
429 const char *errmsg, *addinfo, *dset;
432 if (next_token_copy (args, host, sizeof(host)) < 0)
434 printf ("missing host after connect\n");
437 for (j = -1, i = 0; i<MAX_CON; i++)
440 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
443 ZOOM_connection_destroy (c[i]);
446 else if (c[i] == 0 && j == -1)
449 if (i == MAX_CON) /* no match .. */
453 printf ("no more connection available\n");
456 i = j; /* OK, use this one is available */
458 c[i] = ZOOM_connection_create (options);
459 ZOOM_connection_connect (c[i], host, 0);
461 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
462 printf ("%s error: %s (%s:%d) %s\n",
463 ZOOM_connection_option_get(c[i], "host"), errmsg,
464 dset, error, addinfo);
467 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
468 ZOOM_options options,
474 cmd_len = next_token (buf, &cmd_str);
477 if (is_command ("quit", cmd_str, cmd_len))
479 else if (is_command ("set", cmd_str, cmd_len))
480 cmd_set (c, r, options, buf);
481 else if (is_command ("get", cmd_str, cmd_len))
482 cmd_get (c, r, options, buf);
483 else if (is_command ("connect", cmd_str, cmd_len))
484 cmd_connect (c, r, options, buf);
485 else if (is_command ("open", cmd_str, cmd_len))
486 cmd_connect (c, r, options, buf);
487 else if (is_command ("search", cmd_str, cmd_len))
488 cmd_search (c, r, options, buf);
489 else if (is_command ("find", cmd_str, cmd_len))
490 cmd_search (c, r, options, buf);
491 else if (is_command ("show", cmd_str, cmd_len))
492 cmd_show (c, r, options, buf);
493 else if (is_command ("close", cmd_str, cmd_len))
494 cmd_close (c, r, options, buf);
495 else if (is_command ("help", cmd_str, cmd_len))
496 cmd_help(c, r, options, buf);
497 else if (is_command ("ext", cmd_str, cmd_len))
498 cmd_ext(c, r, options, buf);
499 else if (is_command ("debug", cmd_str, cmd_len))
500 cmd_debug(c, r, options, buf);
501 else if (is_command ("scan", cmd_str, cmd_len))
502 cmd_scan(c, r, options, buf);
503 else if (is_command ("sort", cmd_str, cmd_len))
504 cmd_sort(c, r, options, buf);
506 printf ("unknown command %.*s\n", cmd_len, cmd_str);
510 void shell(ZOOM_connection *c, ZOOM_resultset *r,
511 ZOOM_options options)
517 const char *bp = buf;
518 #if HAVE_READLINE_READLINE_H
520 line_in=readline("ZOOM>");
523 #if HAVE_READLINE_HISTORY_H
525 add_history(line_in);
527 if(strlen(line_in) > 999) {
528 printf("Input line too long\n");
534 printf ("ZOOM>"); fflush (stdout);
535 if (!fgets (buf, 999, stdin))
538 if ((cp = strchr(buf, '\n')))
540 if (!cmd_parse (c, r, options, &bp))
545 int main (int argc, char **argv)
547 ZOOM_options options = ZOOM_options_create();
549 ZOOM_connection z39_con[MAX_CON];
550 ZOOM_resultset z39_res[MAX_CON];
553 for (i = 0; i<MAX_CON; i++)
559 for (i = 0; i<MAX_CON; i++)
563 for (i = 1; i<argc; i++)
565 const char *bp = argv[i];
566 res = cmd_parse(z39_con, z39_res, options, &bp);
567 if (res == 0) /* received quit */
570 if (res) /* do cmdline shell only if not quitting */
571 shell(z39_con, z39_res, options);
572 ZOOM_options_destroy(options);
574 for (i = 0; i<MAX_CON; i++)
576 ZOOM_connection_destroy(z39_con[i]);
577 ZOOM_resultset_destroy(z39_res[i]);