2 * Copyright (c) 2002-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: zoomsh.c,v 1.26 2004-01-16 10:04:55 adam Exp $
15 #if HAVE_READLINE_READLINE_H
16 #include <readline/readline.h>
18 #if HAVE_READLINE_HISTORY_H
19 #include <readline/history.h>
22 #include <yaz/xmalloc.h>
31 static int next_token (const char **cpp, const char **t_start)
34 const char *cp = *cpp;
41 while (*cp && *cp != '"')
52 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
61 return len; /* return -1 if no token was read .. */
64 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
67 int len = next_token (cpp, &start);
75 memcpy (buf_out, start, len);
80 static int is_command (const char *cmd_str, const char *this_str, int this_len)
82 int cmd_len = strlen(cmd_str);
83 if (cmd_len != this_len)
85 if (memcmp (cmd_str, this_str, cmd_len))
90 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
94 char key[40], val[80];
96 if (next_token_copy (args, key, sizeof(key)) < 0)
98 printf ("missing argument for set\n");
101 if (next_token_copy (args, val, sizeof(val)) < 0)
102 ZOOM_options_set(options, key, 0);
104 ZOOM_options_set(options, key, val);
107 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
108 ZOOM_options options,
112 if (next_token_copy (args, key, sizeof(key)) < 0)
114 printf ("missing argument for get\n");
118 const char *val = ZOOM_options_get(options, key);
119 printf ("%s = %s\n", key, val ? val : "<null>");
123 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
124 ZOOM_options options,
129 next_token_copy (args, host, sizeof(host));
130 for (i = 0; i<MAX_CON; i++)
135 if ((h = ZOOM_connection_option_get(c[i], "host"))
136 && !strcmp (h, host))
138 ZOOM_connection_destroy (c[i]);
141 else if (*host == '\0')
143 ZOOM_connection_destroy (c[i]);
149 static void display_records (ZOOM_connection c,
151 int start, int count)
154 for (i = 0; i<count; i++)
157 ZOOM_record rec = ZOOM_resultset_record (r, pos);
158 const char *db = ZOOM_record_get (rec, "database", 0);
160 const char *render = ZOOM_record_get (rec, "render", &len);
161 const char *opac_render = ZOOM_record_get (rec, "opac", &opac_len);
162 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
163 /* if rec is non-null, we got a record for display */
167 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
168 printf ("%d %s %s (%s)\n",
169 pos+1, (db ? db : "unknown"), syntax, oidbuf);
171 fwrite (render, 1, len, stdout);
174 fwrite (opac_render, 1, opac_len, stdout);
180 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
181 ZOOM_options options,
185 char start_str[10], count_str[10];
187 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
188 ZOOM_options_set (options, "start", start_str);
190 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
191 ZOOM_options_set (options, "count", count_str);
193 for (i = 0; i<MAX_CON; i++)
194 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
195 while (ZOOM_event (MAX_CON, c))
198 for (i = 0; i<MAX_CON; i++)
201 const char *errmsg, *addinfo, *dset;
202 /* display errors if any */
205 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
206 printf ("%s error: %s (%s:%d) %s\n",
207 ZOOM_connection_option_get(c[i], "host"), errmsg,
208 dset, error, addinfo);
211 /* OK, no major errors. Display records... */
212 int start = ZOOM_options_get_int (options, "start", 0);
213 int count = ZOOM_options_get_int (options, "count", 0);
214 display_records (c[i], r[i], start, count);
217 ZOOM_options_set (options, "count", "0");
218 ZOOM_options_set (options, "start", "0");
221 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
222 ZOOM_options options,
225 ZOOM_package p[MAX_CON];
226 char ext_type_str[10];
230 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
233 for (i = 0; i<MAX_CON; i++)
237 p[i] = ZOOM_connection_package (c[i], 0);
238 ZOOM_package_send(p[i], ext_type_str);
244 while (ZOOM_event (MAX_CON, c))
247 for (i = 0; i<MAX_CON; i++)
250 const char *errmsg, *addinfo, *dset;
251 /* display errors if any */
254 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
255 printf ("%s error: %s (%s:%d) %s\n",
256 ZOOM_connection_option_get(c[i], "host"), errmsg,
257 dset, error, addinfo);
262 ZOOM_package_destroy (p[i]);
266 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
267 ZOOM_options options,
270 yaz_log_init_level(LOG_ALL);
273 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
274 ZOOM_options options,
278 const char *query_str = *args;
281 s = ZOOM_query_create ();
282 while (*query_str == ' ')
284 if (memcmp(query_str, "cql:", 4) == 0)
286 ZOOM_query_cql (s, query_str + 4);
288 else if (ZOOM_query_prefix (s, query_str))
290 printf ("Bad PQF: %s\n", query_str);
293 for (i = 0; i<MAX_CON; i++)
297 ZOOM_resultset_destroy (r[i]);
301 r[i] = ZOOM_connection_search (c[i], s);
304 while (ZOOM_event (MAX_CON, c))
307 for (i = 0; i<MAX_CON; i++)
310 const char *errmsg, *addinfo, *dset;
311 /* display errors if any */
314 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
315 printf ("%s error: %s (%s:%d) %s\n",
316 ZOOM_connection_option_get(c[i], "host"), errmsg,
317 dset, error, addinfo);
320 /* OK, no major errors. Look at the result count */
321 int start = ZOOM_options_get_int (options, "start", 0);
322 int count = ZOOM_options_get_int (options, "count", 0);
324 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
325 ZOOM_resultset_size(r[i]));
327 display_records (c[i], r[i], start, count);
330 ZOOM_query_destroy (s);
333 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
334 ZOOM_options options,
337 const char *start_term = *args;
339 ZOOM_scanset s[MAX_CON];
341 while (*start_term == ' ')
344 for (i = 0; i<MAX_CON; i++)
347 s[i] = ZOOM_connection_scan(c[i], start_term);
351 while (ZOOM_event(MAX_CON, c))
353 for (i = 0; i<MAX_CON; i++)
356 size_t p, sz = ZOOM_scanset_size(s[i]);
357 for (p = 0; p < sz; p++)
361 const char *term = ZOOM_scanset_display_term(s[i], p,
363 fwrite(term, 1, len, stdout);
364 printf (" %d\n", occ);
366 ZOOM_scanset_destroy(s[i]);
371 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
372 ZOOM_options options,
375 printf ("connect <zurl>\n");
376 printf ("search <pqf>\n");
377 printf ("show [<start> [<count>]\n");
378 printf ("scan <term>\n");
380 printf ("close <zurl>\n");
381 printf ("set <option> [<value>]\n");
382 printf ("get <option>\n");
384 printf ("options:\n");
387 printf (" databaseName\n");
388 printf (" preferredRecordSyntax\n");
390 printf (" elementSetName\n");
391 printf (" maximumRecordSize\n");
392 printf (" preferredRecordSize\n");
394 printf (" piggyback\n");
397 printf (" password\n");
398 printf (" implementationName\n");
399 printf (" charset\n");
403 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
404 ZOOM_options options,
408 const char *errmsg, *addinfo, *dset;
411 if (next_token_copy (args, host, sizeof(host)) < 0)
413 printf ("missing host after connect\n");
416 for (j = -1, i = 0; i<MAX_CON; i++)
419 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
422 ZOOM_connection_destroy (c[i]);
425 else if (c[i] == 0 && j == -1)
428 if (i == MAX_CON) /* no match .. */
432 printf ("no more connection available\n");
435 i = j; /* OK, use this one is available */
437 c[i] = ZOOM_connection_create (options);
438 ZOOM_connection_connect (c[i], host, 0);
440 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
441 printf ("%s error: %s (%s:%d) %s\n",
442 ZOOM_connection_option_get(c[i], "host"), errmsg,
443 dset, error, addinfo);
446 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
447 ZOOM_options options,
453 cmd_len = next_token (buf, &cmd_str);
456 if (is_command ("quit", cmd_str, cmd_len))
458 else if (is_command ("set", cmd_str, cmd_len))
459 cmd_set (c, r, options, buf);
460 else if (is_command ("get", cmd_str, cmd_len))
461 cmd_get (c, r, options, buf);
462 else if (is_command ("connect", cmd_str, cmd_len))
463 cmd_connect (c, r, options, buf);
464 else if (is_command ("open", cmd_str, cmd_len))
465 cmd_connect (c, r, options, buf);
466 else if (is_command ("search", cmd_str, cmd_len))
467 cmd_search (c, r, options, buf);
468 else if (is_command ("find", cmd_str, cmd_len))
469 cmd_search (c, r, options, buf);
470 else if (is_command ("show", cmd_str, cmd_len))
471 cmd_show (c, r, options, buf);
472 else if (is_command ("close", cmd_str, cmd_len))
473 cmd_close (c, r, options, buf);
474 else if (is_command ("help", cmd_str, cmd_len))
475 cmd_help(c, r, options, buf);
476 else if (is_command ("ext", cmd_str, cmd_len))
477 cmd_ext(c, r, options, buf);
478 else if (is_command ("debug", cmd_str, cmd_len))
479 cmd_debug(c, r, options, buf);
480 else if (is_command ("scan", cmd_str, cmd_len))
481 cmd_scan(c, r, options, buf);
483 printf ("unknown command %.*s\n", cmd_len, cmd_str);
487 void shell(ZOOM_connection *c, ZOOM_resultset *r,
488 ZOOM_options options)
494 const char *bp = buf;
495 #if HAVE_READLINE_READLINE_H
497 line_in=readline("ZOOM>");
500 #if HAVE_READLINE_HISTORY_H
502 add_history(line_in);
504 if(strlen(line_in) > 999) {
505 printf("Input line too long\n");
511 printf ("ZOOM>"); fflush (stdout);
512 if (!fgets (buf, 999, stdin))
515 if ((cp = strchr(buf, '\n')))
517 if (!cmd_parse (c, r, options, &bp))
522 int main (int argc, char **argv)
524 ZOOM_options options = ZOOM_options_create();
526 ZOOM_connection z39_con[MAX_CON];
527 ZOOM_resultset z39_res[MAX_CON];
530 for (i = 0; i<MAX_CON; i++)
536 for (i = 0; i<MAX_CON; i++)
540 for (i = 1; i<argc; i++)
542 const char *bp = argv[i];
543 res = cmd_parse(z39_con, z39_res, options, &bp);
544 if (res == 0) /* received quit */
547 if (res) /* do cmdline shell only if not quitting */
548 shell(z39_con, z39_res, options);
549 ZOOM_options_destroy(options);
551 for (i = 0; i<MAX_CON; i++)
553 ZOOM_connection_destroy(z39_con[i]);
554 ZOOM_resultset_destroy(z39_res[i]);