2 * $Id: zoomsh.c,v 1.22 2003-07-10 11:50:32 mike Exp $
12 #if HAVE_READLINE_READLINE_H
13 #include <readline/readline.h>
15 #if HAVE_READLINE_HISTORY_H
16 #include <readline/history.h>
19 #include <yaz/xmalloc.h>
28 static int next_token (const char **cpp, const char **t_start)
31 const char *cp = *cpp;
38 while (*cp && *cp != '"')
49 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
58 return len; /* return -1 if no token was read .. */
61 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
64 int len = next_token (cpp, &start);
72 memcpy (buf_out, start, len);
77 static int is_command (const char *cmd_str, const char *this_str, int this_len)
79 int cmd_len = strlen(cmd_str);
80 if (cmd_len != this_len)
82 if (memcmp (cmd_str, this_str, cmd_len))
87 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
91 char key[40], val[80];
93 if (next_token_copy (args, key, sizeof(key)) < 0)
95 printf ("missing argument for set\n");
98 if (next_token_copy (args, val, sizeof(val)) < 0)
99 ZOOM_options_set(options, key, 0);
101 ZOOM_options_set(options, key, val);
104 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
105 ZOOM_options options,
109 if (next_token_copy (args, key, sizeof(key)) < 0)
111 printf ("missing argument for get\n");
115 const char *val = ZOOM_options_get(options, key);
116 printf ("%s = %s\n", key, val ? val : "<null>");
120 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
121 ZOOM_options options,
126 next_token_copy (args, host, sizeof(host));
127 for (i = 0; i<MAX_CON; i++)
132 if ((h = ZOOM_connection_option_get(c[i], "host"))
133 && !strcmp (h, host))
135 ZOOM_connection_destroy (c[i]);
138 else if (*host == '\0')
140 ZOOM_connection_destroy (c[i]);
146 static void display_records (ZOOM_connection c,
148 int start, int count)
151 for (i = 0; i<count; i++)
154 ZOOM_record rec = ZOOM_resultset_record (r, pos);
155 const char *db = ZOOM_record_get (rec, "database", 0);
157 const char *render = ZOOM_record_get (rec, "render", &len);
158 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
159 /* if rec is non-null, we got a record for display */
163 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
164 printf ("%d %s %s (%s)\n",
165 pos+1, (db ? db : "unknown"), syntax, oidbuf);
167 fwrite (render, 1, len, stdout);
173 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
174 ZOOM_options options,
178 char start_str[10], count_str[10];
180 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
181 ZOOM_options_set (options, "start", start_str);
183 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
184 ZOOM_options_set (options, "count", count_str);
186 for (i = 0; i<MAX_CON; i++)
187 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
188 while (ZOOM_event (MAX_CON, c))
191 for (i = 0; i<MAX_CON; i++)
194 const char *errmsg, *addinfo, *dset;
195 /* display errors if any */
198 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
199 printf ("%s error: %s (%s:%d) %s\n",
200 ZOOM_connection_option_get(c[i], "host"), errmsg,
201 dset, error, addinfo);
204 /* OK, no major errors. Display records... */
205 int start = ZOOM_options_get_int (options, "start", 0);
206 int count = ZOOM_options_get_int (options, "count", 0);
207 display_records (c[i], r[i], start, count);
210 ZOOM_options_set (options, "count", "0");
211 ZOOM_options_set (options, "start", "0");
214 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
215 ZOOM_options options,
218 ZOOM_package p[MAX_CON];
222 for (i = 0; i<MAX_CON; i++)
226 p[i] = ZOOM_connection_package (c[i], 0);
227 ZOOM_package_send(p[i], "itemorder");
233 while (ZOOM_event (MAX_CON, c))
236 for (i = 0; i<MAX_CON; i++)
239 const char *errmsg, *addinfo, *dset;
240 /* display errors if any */
243 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
244 printf ("%s error: %s (%s:%d) %s\n",
245 ZOOM_connection_option_get(c[i], "host"), errmsg,
246 dset, error, addinfo);
251 ZOOM_package_destroy (p[i]);
255 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
256 ZOOM_options options,
259 yaz_log_init_level(LOG_ALL);
262 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
263 ZOOM_options options,
267 const char *query_str = *args;
270 s = ZOOM_query_create ();
271 while (*query_str == ' ')
273 if (memcmp(query_str, "cql:", 4) == 0)
275 ZOOM_query_cql (s, query_str + 4);
277 else if (ZOOM_query_prefix (s, query_str))
279 printf ("Bad PQF: %s\n", query_str);
282 for (i = 0; i<MAX_CON; i++)
286 ZOOM_resultset_destroy (r[i]);
290 r[i] = ZOOM_connection_search (c[i], s);
293 while (ZOOM_event (MAX_CON, c))
296 for (i = 0; i<MAX_CON; i++)
299 const char *errmsg, *addinfo, *dset;
300 /* display errors if any */
303 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
304 printf ("%s error: %s (%s:%d) %s\n",
305 ZOOM_connection_option_get(c[i], "host"), errmsg,
306 dset, error, addinfo);
309 /* OK, no major errors. Look at the result count */
310 int start = ZOOM_options_get_int (options, "start", 0);
311 int count = ZOOM_options_get_int (options, "count", 0);
313 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
314 ZOOM_resultset_size(r[i]));
316 display_records (c[i], r[i], start, count);
319 ZOOM_query_destroy (s);
322 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
323 ZOOM_options options,
326 const char *start_term = *args;
328 ZOOM_scanset s[MAX_CON];
330 while (*start_term == ' ')
333 for (i = 0; i<MAX_CON; i++)
336 s[i] = ZOOM_connection_scan(c[i], start_term);
340 while (ZOOM_event(MAX_CON, c))
342 for (i = 0; i<MAX_CON; i++)
345 size_t p, sz = ZOOM_scanset_size(s[i]);
346 for (p = 0; p < sz; p++)
350 const char *term = ZOOM_scanset_term(s[i], p, &occ, &len);
351 fwrite(term, 1, len, stdout);
352 printf (" %d\n", occ);
354 ZOOM_scanset_destroy(s[i]);
359 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
360 ZOOM_options options,
363 printf ("connect <zurl>\n");
364 printf ("search <pqf>\n");
365 printf ("show [<start> [<count>]\n");
366 printf ("scan <term>\n");
368 printf ("close <zurl>\n");
369 printf ("set <option> [<value>]\n");
370 printf ("get <option>\n");
372 printf ("options:\n");
375 printf (" databaseName\n");
376 printf (" preferredRecordSyntax\n");
378 printf (" elementSetName\n");
379 printf (" maximumRecordSize\n");
380 printf (" preferredRecordSize\n");
382 printf (" piggyback\n");
386 printf (" implementationName\n");
387 printf (" charset\n");
391 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
392 ZOOM_options options,
396 const char *errmsg, *addinfo, *dset;
399 if (next_token_copy (args, host, sizeof(host)) < 0)
401 printf ("missing host after connect\n");
404 for (j = -1, i = 0; i<MAX_CON; i++)
407 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
410 ZOOM_connection_destroy (c[i]);
413 else if (c[i] == 0 && j == -1)
416 if (i == MAX_CON) /* no match .. */
420 printf ("no more connection available\n");
423 i = j; /* OK, use this one is available */
425 c[i] = ZOOM_connection_create (options);
426 ZOOM_connection_connect (c[i], host, 0);
428 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
429 printf ("%s error: %s (%s:%d) %s\n",
430 ZOOM_connection_option_get(c[i], "host"), errmsg,
431 dset, error, addinfo);
434 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
435 ZOOM_options options,
441 cmd_len = next_token (buf, &cmd_str);
444 if (is_command ("quit", cmd_str, cmd_len))
446 else if (is_command ("set", cmd_str, cmd_len))
447 cmd_set (c, r, options, buf);
448 else if (is_command ("get", cmd_str, cmd_len))
449 cmd_get (c, r, options, buf);
450 else if (is_command ("connect", cmd_str, cmd_len))
451 cmd_connect (c, r, options, buf);
452 else if (is_command ("open", cmd_str, cmd_len))
453 cmd_connect (c, r, options, buf);
454 else if (is_command ("search", cmd_str, cmd_len))
455 cmd_search (c, r, options, buf);
456 else if (is_command ("find", cmd_str, cmd_len))
457 cmd_search (c, r, options, buf);
458 else if (is_command ("show", cmd_str, cmd_len))
459 cmd_show (c, r, options, buf);
460 else if (is_command ("close", cmd_str, cmd_len))
461 cmd_close (c, r, options, buf);
462 else if (is_command ("help", cmd_str, cmd_len))
463 cmd_help(c, r, options, buf);
464 else if (is_command ("ext", cmd_str, cmd_len))
465 cmd_ext(c, r, options, buf);
466 else if (is_command ("debug", cmd_str, cmd_len))
467 cmd_debug(c, r, options, buf);
468 else if (is_command ("scan", cmd_str, cmd_len))
469 cmd_scan(c, r, options, buf);
471 printf ("unknown command %.*s\n", cmd_len, cmd_str);
475 void shell(ZOOM_connection *c, ZOOM_resultset *r,
476 ZOOM_options options)
482 const char *bp = buf;
483 #if HAVE_READLINE_READLINE_H
485 line_in=readline("ZOOM>");
488 #if HAVE_READLINE_HISTORY_H
490 add_history(line_in);
492 if(strlen(line_in) > 999) {
493 printf("Input line too long\n");
499 printf ("ZOOM>"); fflush (stdout);
500 if (!fgets (buf, 999, stdin))
503 if ((cp = strchr(buf, '\n')))
505 if (!cmd_parse (c, r, options, &bp))
510 int main (int argc, char **argv)
512 ZOOM_options options = ZOOM_options_create();
514 ZOOM_connection z39_con[MAX_CON];
515 ZOOM_resultset z39_res[MAX_CON];
518 for (i = 0; i<MAX_CON; i++)
524 for (i = 0; i<MAX_CON; i++)
528 for (i = 1; i<argc; i++)
530 const char *bp = argv[i];
531 res = cmd_parse(z39_con, z39_res, options, &bp);
532 if (res == 0) /* received quit */
535 if (res) /* do cmdline shell only if not quitting */
536 shell(z39_con, z39_res, options);
537 ZOOM_options_destroy(options);
539 for (i = 0; i<MAX_CON; i++)
541 ZOOM_connection_destroy(z39_con[i]);
542 ZOOM_resultset_destroy(z39_res[i]);