2 * Copyright (c) 2002-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.28 2004-04-28 22:45:00 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>
40 static int next_token (const char **cpp, const char **t_start)
43 const char *cp = *cpp;
50 while (*cp && *cp != '"')
61 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
70 return len; /* return -1 if no token was read .. */
73 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
76 int len = next_token (cpp, &start);
84 memcpy (buf_out, start, len);
89 static int is_command (const char *cmd_str, const char *this_str, int this_len)
91 int cmd_len = strlen(cmd_str);
92 if (cmd_len != this_len)
94 if (memcmp (cmd_str, this_str, cmd_len))
99 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
100 ZOOM_options options,
103 char key[40], val[80];
105 if (next_token_copy (args, key, sizeof(key)) < 0)
107 printf ("missing argument for set\n");
110 if (next_token_copy (args, val, sizeof(val)) < 0)
111 ZOOM_options_set(options, key, 0);
113 ZOOM_options_set(options, key, val);
116 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
117 ZOOM_options options,
121 if (next_token_copy (args, key, sizeof(key)) < 0)
123 printf ("missing argument for get\n");
127 const char *val = ZOOM_options_get(options, key);
128 printf ("%s = %s\n", key, val ? val : "<null>");
132 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
133 ZOOM_options options,
138 next_token_copy (args, host, sizeof(host));
139 for (i = 0; i<MAX_CON; i++)
144 if ((h = ZOOM_connection_option_get(c[i], "host"))
145 && !strcmp (h, host))
147 ZOOM_connection_destroy (c[i]);
150 else if (*host == '\0')
152 ZOOM_connection_destroy (c[i]);
158 static void display_records (ZOOM_connection c,
160 int start, int count)
163 for (i = 0; i<count; i++)
166 ZOOM_record rec = ZOOM_resultset_record (r, pos);
167 const char *db = ZOOM_record_get (rec, "database", 0);
169 const char *render = ZOOM_record_get (rec, "render", &len);
170 const char *opac_render = ZOOM_record_get (rec, "opac", &opac_len);
171 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
172 /* if rec is non-null, we got a record for display */
176 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
177 printf ("%d %s %s (%s)\n",
178 pos+1, (db ? db : "unknown"), syntax, oidbuf);
180 fwrite (render, 1, len, stdout);
183 fwrite (opac_render, 1, opac_len, stdout);
189 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
190 ZOOM_options options,
194 char start_str[10], count_str[10];
196 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
197 ZOOM_options_set (options, "start", start_str);
199 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
200 ZOOM_options_set (options, "count", count_str);
202 for (i = 0; i<MAX_CON; i++)
203 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
204 while (ZOOM_event (MAX_CON, c))
207 for (i = 0; i<MAX_CON; i++)
210 const char *errmsg, *addinfo, *dset;
211 /* display errors if any */
214 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
215 printf ("%s error: %s (%s:%d) %s\n",
216 ZOOM_connection_option_get(c[i], "host"), errmsg,
217 dset, error, addinfo);
220 /* OK, no major errors. Display records... */
221 int start = ZOOM_options_get_int (options, "start", 0);
222 int count = ZOOM_options_get_int (options, "count", 0);
223 display_records (c[i], r[i], start, count);
226 ZOOM_options_set (options, "count", "0");
227 ZOOM_options_set (options, "start", "0");
230 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
231 ZOOM_options options,
234 ZOOM_package p[MAX_CON];
235 char ext_type_str[10];
239 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
242 for (i = 0; i<MAX_CON; i++)
246 p[i] = ZOOM_connection_package (c[i], 0);
247 ZOOM_package_send(p[i], ext_type_str);
253 while (ZOOM_event (MAX_CON, c))
256 for (i = 0; i<MAX_CON; i++)
259 const char *errmsg, *addinfo, *dset;
260 /* display errors if any */
263 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
264 printf ("%s error: %s (%s:%d) %s\n",
265 ZOOM_connection_option_get(c[i], "host"), errmsg,
266 dset, error, addinfo);
271 ZOOM_package_destroy (p[i]);
275 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
276 ZOOM_options options,
279 yaz_log_init_level(LOG_ALL);
282 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
283 ZOOM_options options,
287 const char *query_str = *args;
290 s = ZOOM_query_create ();
291 while (*query_str == ' ')
293 if (memcmp(query_str, "cql:", 4) == 0)
295 ZOOM_query_cql (s, query_str + 4);
297 else if (ZOOM_query_prefix (s, query_str))
299 printf ("Bad PQF: %s\n", query_str);
302 for (i = 0; i<MAX_CON; i++)
306 ZOOM_resultset_destroy (r[i]);
310 r[i] = ZOOM_connection_search (c[i], s);
313 while (ZOOM_event (MAX_CON, c))
316 for (i = 0; i<MAX_CON; i++)
319 const char *errmsg, *addinfo, *dset;
320 /* display errors if any */
323 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
324 printf ("%s error: %s (%s:%d) %s\n",
325 ZOOM_connection_option_get(c[i], "host"), errmsg,
326 dset, error, addinfo);
329 /* OK, no major errors. Look at the result count */
330 int start = ZOOM_options_get_int (options, "start", 0);
331 int count = ZOOM_options_get_int (options, "count", 0);
333 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
334 ZOOM_resultset_size(r[i]));
336 display_records (c[i], r[i], start, count);
339 ZOOM_query_destroy (s);
342 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
343 ZOOM_options options,
346 const char *start_term = *args;
348 ZOOM_scanset s[MAX_CON];
350 while (*start_term == ' ')
353 for (i = 0; i<MAX_CON; i++)
356 s[i] = ZOOM_connection_scan(c[i], start_term);
360 while (ZOOM_event(MAX_CON, c))
362 for (i = 0; i<MAX_CON; i++)
365 size_t p, sz = ZOOM_scanset_size(s[i]);
366 for (p = 0; p < sz; p++)
370 const char *term = ZOOM_scanset_display_term(s[i], p,
372 fwrite(term, 1, len, stdout);
373 printf (" %d\n", occ);
375 ZOOM_scanset_destroy(s[i]);
380 static void cmd_sort (ZOOM_connection *c, ZOOM_resultset *r,
381 ZOOM_options options,
384 const char *sort_spec = *args;
387 while (*sort_spec == ' ')
390 for (i = 0; i<MAX_CON; i++)
393 ZOOM_resultset_sort(r[i], "yaz", sort_spec);
395 while (ZOOM_event(MAX_CON, c))
399 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
400 ZOOM_options options,
403 printf ("connect <zurl>\n");
404 printf ("search <pqf>\n");
405 printf ("show [<start> [<count>]\n");
406 printf ("scan <term>\n");
408 printf ("close <zurl>\n");
409 printf ("set <option> [<value>]\n");
410 printf ("get <option>\n");
412 printf ("options:\n");
415 printf (" databaseName\n");
416 printf (" preferredRecordSyntax\n");
418 printf (" elementSetName\n");
419 printf (" maximumRecordSize\n");
420 printf (" preferredRecordSize\n");
422 printf (" piggyback\n");
425 printf (" password\n");
426 printf (" implementationName\n");
427 printf (" charset\n");
431 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
432 ZOOM_options options,
436 const char *errmsg, *addinfo, *dset;
439 if (next_token_copy (args, host, sizeof(host)) < 0)
441 printf ("missing host after connect\n");
444 for (j = -1, i = 0; i<MAX_CON; i++)
447 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
450 ZOOM_connection_destroy (c[i]);
453 else if (c[i] == 0 && j == -1)
456 if (i == MAX_CON) /* no match .. */
460 printf ("no more connection available\n");
463 i = j; /* OK, use this one is available */
465 c[i] = ZOOM_connection_create (options);
466 ZOOM_connection_connect (c[i], host, 0);
468 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
469 printf ("%s error: %s (%s:%d) %s\n",
470 ZOOM_connection_option_get(c[i], "host"), errmsg,
471 dset, error, addinfo);
474 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
475 ZOOM_options options,
481 cmd_len = next_token (buf, &cmd_str);
484 if (is_command ("quit", cmd_str, cmd_len))
486 else if (is_command ("set", cmd_str, cmd_len))
487 cmd_set (c, r, options, buf);
488 else if (is_command ("get", cmd_str, cmd_len))
489 cmd_get (c, r, options, buf);
490 else if (is_command ("connect", cmd_str, cmd_len))
491 cmd_connect (c, r, options, buf);
492 else if (is_command ("open", cmd_str, cmd_len))
493 cmd_connect (c, r, options, buf);
494 else if (is_command ("search", cmd_str, cmd_len))
495 cmd_search (c, r, options, buf);
496 else if (is_command ("find", cmd_str, cmd_len))
497 cmd_search (c, r, options, buf);
498 else if (is_command ("show", cmd_str, cmd_len))
499 cmd_show (c, r, options, buf);
500 else if (is_command ("close", cmd_str, cmd_len))
501 cmd_close (c, r, options, buf);
502 else if (is_command ("help", cmd_str, cmd_len))
503 cmd_help(c, r, options, buf);
504 else if (is_command ("ext", cmd_str, cmd_len))
505 cmd_ext(c, r, options, buf);
506 else if (is_command ("debug", cmd_str, cmd_len))
507 cmd_debug(c, r, options, buf);
508 else if (is_command ("scan", cmd_str, cmd_len))
509 cmd_scan(c, r, options, buf);
510 else if (is_command ("sort", cmd_str, cmd_len))
511 cmd_sort(c, r, options, buf);
513 printf ("unknown command %.*s\n", cmd_len, cmd_str);
517 void shell(ZOOM_connection *c, ZOOM_resultset *r,
518 ZOOM_options options)
524 const char *bp = buf;
525 #if HAVE_READLINE_READLINE_H
527 line_in=readline("ZOOM>");
530 #if HAVE_READLINE_HISTORY_H
532 add_history(line_in);
534 if(strlen(line_in) > 999) {
535 printf("Input line too long\n");
541 printf ("ZOOM>"); fflush (stdout);
542 if (!fgets (buf, 999, stdin))
545 if ((cp = strchr(buf, '\n')))
547 if (!cmd_parse (c, r, options, &bp))
552 int main (int argc, char **argv)
554 ZOOM_options options = ZOOM_options_create();
556 ZOOM_connection z39_con[MAX_CON];
557 ZOOM_resultset z39_res[MAX_CON];
563 cs_create_host(0, 0, 0);
566 for (i = 0; i<MAX_CON; i++)
572 for (i = 0; i<MAX_CON; i++)
576 for (i = 1; i<argc; i++)
578 const char *bp = argv[i];
579 res = cmd_parse(z39_con, z39_res, options, &bp);
580 if (res == 0) /* received quit */
583 if (res) /* do cmdline shell only if not quitting */
584 shell(z39_con, z39_res, options);
585 ZOOM_options_destroy(options);
587 for (i = 0; i<MAX_CON; i++)
589 ZOOM_connection_destroy(z39_con[i]);
590 ZOOM_resultset_destroy(z39_res[i]);