2 * $Id: zoomsh.c,v 1.17 2003-02-24 13:14:49 adam 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>
26 static int next_token (const char **cpp, const char **t_start)
29 const char *cp = *cpp;
33 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
42 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
45 int len = next_token (cpp, &start);
53 memcpy (buf_out, start, len);
58 static int is_command (const char *cmd_str, const char *this_str, int this_len)
60 int cmd_len = strlen(cmd_str);
61 if (cmd_len != this_len)
63 if (memcmp (cmd_str, this_str, cmd_len))
68 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
72 char key[40], val[80];
74 if (!next_token_copy (args, key, sizeof(key)))
76 printf ("missing argument for set\n");
79 if (!next_token_copy (args, val, sizeof(val)))
80 ZOOM_options_set(options, key, 0);
82 ZOOM_options_set(options, key, val);
85 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
90 if (!next_token_copy (args, key, sizeof(key)))
92 printf ("missing argument for get\n");
96 const char *val = ZOOM_options_get(options, key);
97 printf ("%s = %s\n", key, val ? val : "<null>");
101 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
102 ZOOM_options options,
107 next_token_copy (args, host, sizeof(host));
108 for (i = 0; i<MAX_CON; i++)
113 if ((h = ZOOM_connection_option_get(c[i], "host"))
114 && !strcmp (h, host))
116 ZOOM_connection_destroy (c[i]);
119 else if (*host == '\0')
121 ZOOM_connection_destroy (c[i]);
127 static void display_records (ZOOM_connection c,
129 int start, int count)
132 for (i = 0; i<count; i++)
135 ZOOM_record rec = ZOOM_resultset_record (r, pos);
136 const char *db = ZOOM_record_get (rec, "database", 0);
138 const char *render = ZOOM_record_get (rec, "render", &len);
139 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
140 /* if rec is non-null, we got a record for display */
143 printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
145 fwrite (render, 1, len, stdout);
151 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
152 ZOOM_options options,
156 char start_str[10], count_str[10];
158 if (next_token_copy (args, start_str, sizeof(start_str)))
159 ZOOM_options_set (options, "start", start_str);
161 if (next_token_copy (args, count_str, sizeof(count_str)))
162 ZOOM_options_set (options, "count", count_str);
164 for (i = 0; i<MAX_CON; i++)
165 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
166 while (ZOOM_event (MAX_CON, c))
169 for (i = 0; i<MAX_CON; i++)
172 const char *errmsg, *addinfo, *dset;
173 /* display errors if any */
176 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
177 printf ("%s error: %s (%s:%d) %s\n",
178 ZOOM_connection_option_get(c[i], "host"), errmsg,
179 dset, error, addinfo);
182 /* OK, no major errors. Display records... */
183 int start = ZOOM_options_get_int (options, "start", 0);
184 int count = ZOOM_options_get_int (options, "count", 0);
185 display_records (c[i], r[i], start, count);
188 ZOOM_options_set (options, "count", "0");
189 ZOOM_options_set (options, "start", "0");
192 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
193 ZOOM_options options,
196 ZOOM_package p[MAX_CON];
200 for (i = 0; i<MAX_CON; i++)
204 p[i] = ZOOM_connection_package (c[i], 0);
205 ZOOM_package_send(p[i], "itemorder");
211 while (ZOOM_event (MAX_CON, c))
214 for (i = 0; i<MAX_CON; i++)
217 const char *errmsg, *addinfo, *dset;
218 /* display errors if any */
221 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
222 printf ("%s error: %s (%s:%d) %s\n",
223 ZOOM_connection_option_get(c[i], "host"), errmsg,
224 dset, error, addinfo);
229 ZOOM_package_destroy (p[i]);
233 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
234 ZOOM_options options,
237 yaz_log_init_level(LOG_ALL);
240 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
241 ZOOM_options options,
245 const char *query_str = *args;
248 s = ZOOM_query_create ();
249 while (*query_str == ' ')
251 if (memcmp(query_str, "cql:", 4) == 0)
253 ZOOM_query_cql (s, query_str + 4);
255 else if (ZOOM_query_prefix (s, query_str))
257 printf ("Bad PQF: %s\n", query_str);
260 for (i = 0; i<MAX_CON; i++)
264 ZOOM_resultset_destroy (r[i]);
268 r[i] = ZOOM_connection_search (c[i], s);
271 while (ZOOM_event (MAX_CON, c))
274 for (i = 0; i<MAX_CON; i++)
277 const char *errmsg, *addinfo, *dset;
278 /* display errors if any */
281 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
282 printf ("%s error: %s (%s:%d) %s\n",
283 ZOOM_connection_option_get(c[i], "host"), errmsg,
284 dset, error, addinfo);
287 /* OK, no major errors. Look at the result count */
288 int start = ZOOM_options_get_int (options, "start", 0);
289 int count = ZOOM_options_get_int (options, "count", 0);
291 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
292 ZOOM_resultset_size(r[i]));
294 display_records (c[i], r[i], start, count);
297 ZOOM_query_destroy (s);
300 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
301 ZOOM_options options,
304 const char *start_term = *args;
306 ZOOM_scanset s[MAX_CON];
308 while (*start_term == ' ')
311 for (i = 0; i<MAX_CON; i++)
314 s[i] = ZOOM_connection_scan(c[i], start_term);
318 while (ZOOM_event(MAX_CON, c))
320 for (i = 0; i<MAX_CON; i++)
323 size_t p, sz = ZOOM_scanset_size(s[i]);
324 for (p = 0; p < sz; p++)
328 const char *term = ZOOM_scanset_term(s[i], p, &occ, &len);
329 printf ("%.*s %d\n", len, term, occ);
331 ZOOM_scanset_destroy(s[i]);
336 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
337 ZOOM_options options,
340 printf ("connect <zurl>\n");
341 printf ("search <pqf>\n");
342 printf ("show [<start> [<count>]\n");
343 printf ("scan <term>\n");
345 printf ("close <zurl>\n");
346 printf ("set <option> [<value>]\n");
347 printf ("get <option>\n");
349 printf ("options:\n");
352 printf (" databaseName\n");
353 printf (" preferredRecordSyntax\n");
355 printf (" elementSetName\n");
356 printf (" maximumRecordSize\n");
357 printf (" preferredRecordSize\n");
359 printf (" piggyback\n");
363 printf (" implementationName\n");
364 printf (" charset\n");
368 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
369 ZOOM_options options,
373 const char *errmsg, *addinfo, *dset;
376 if (!next_token_copy (args, host, sizeof(host)))
378 printf ("missing host after connect\n");
381 for (j = -1, i = 0; i<MAX_CON; i++)
384 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
387 ZOOM_connection_destroy (c[i]);
390 else if (c[i] == 0 && j == -1)
393 if (i == MAX_CON) /* no match .. */
397 printf ("no more connection available\n");
400 i = j; /* OK, use this one is available */
402 c[i] = ZOOM_connection_create (options);
403 ZOOM_connection_connect (c[i], host, 0);
405 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
406 printf ("%s error: %s (%s:%d) %s\n",
407 ZOOM_connection_option_get(c[i], "host"), errmsg,
408 dset, error, addinfo);
411 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
412 ZOOM_options options,
418 cmd_len = next_token (buf, &cmd_str);
421 if (is_command ("quit", cmd_str, cmd_len))
423 else if (is_command ("set", cmd_str, cmd_len))
424 cmd_set (c, r, options, buf);
425 else if (is_command ("get", cmd_str, cmd_len))
426 cmd_get (c, r, options, buf);
427 else if (is_command ("connect", cmd_str, cmd_len))
428 cmd_connect (c, r, options, buf);
429 else if (is_command ("open", cmd_str, cmd_len))
430 cmd_connect (c, r, options, buf);
431 else if (is_command ("search", cmd_str, cmd_len))
432 cmd_search (c, r, options, buf);
433 else if (is_command ("find", cmd_str, cmd_len))
434 cmd_search (c, r, options, buf);
435 else if (is_command ("show", cmd_str, cmd_len))
436 cmd_show (c, r, options, buf);
437 else if (is_command ("close", cmd_str, cmd_len))
438 cmd_close (c, r, options, buf);
439 else if (is_command ("help", cmd_str, cmd_len))
440 cmd_help(c, r, options, buf);
441 else if (is_command ("ext", cmd_str, cmd_len))
442 cmd_ext(c, r, options, buf);
443 else if (is_command ("debug", cmd_str, cmd_len))
444 cmd_debug(c, r, options, buf);
445 else if (is_command ("scan", cmd_str, cmd_len))
446 cmd_scan(c, r, options, buf);
448 printf ("unknown command %.*s\n", cmd_len, cmd_str);
452 void shell(ZOOM_connection *c, ZOOM_resultset *r,
453 ZOOM_options options)
459 const char *bp = buf;
460 #if HAVE_READLINE_READLINE_H
462 line_in=readline("ZOOM>");
465 #if HAVE_READLINE_HISTORY_H
467 add_history(line_in);
469 if(strlen(line_in) > 999) {
470 printf("Input line too long\n");
476 printf ("ZOOM>"); fflush (stdout);
477 if (!fgets (buf, 999, stdin))
480 if ((cp = strchr(buf, '\n')))
482 if (!cmd_parse (c, r, options, &bp))
487 int main (int argc, char **argv)
489 ZOOM_options options = ZOOM_options_create();
491 ZOOM_connection z39_con[MAX_CON];
492 ZOOM_resultset z39_res[MAX_CON];
495 for (i = 0; i<MAX_CON; i++)
501 for (i = 0; i<MAX_CON; i++)
505 for (i = 1; i<argc; i++)
507 const char *bp = argv[i];
508 res = cmd_parse(z39_con, z39_res, options, &bp);
509 if (res == 0) /* received quit */
512 if (res) /* do cmdline shell only if not quitting */
513 shell(z39_con, z39_res, options);
514 ZOOM_options_destroy(options);
516 for (i = 0; i<MAX_CON; i++)
518 ZOOM_connection_destroy(z39_con[i]);
519 ZOOM_resultset_destroy(z39_res[i]);