2 * $Id: zoomsh.c,v 1.16 2003-02-23 15:24:27 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_help (ZOOM_connection *c, ZOOM_resultset *r,
301 ZOOM_options options,
304 printf ("connect <zurl>\n");
305 printf ("search <pqf>\n");
306 printf ("show [<start> [<count>]\n");
308 printf ("close <zurl>\n");
309 printf ("set <option> [<value>]\n");
310 printf ("get <option>\n");
312 printf ("options:\n");
315 printf (" databaseName\n");
316 printf (" preferredRecordSyntax\n");
318 printf (" elementSetName\n");
319 printf (" maximumRecordSize\n");
320 printf (" preferredRecordSize\n");
322 printf (" piggyback\n");
326 printf (" implementationName\n");
327 printf (" charset\n");
331 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
332 ZOOM_options options,
336 const char *errmsg, *addinfo, *dset;
339 if (!next_token_copy (args, host, sizeof(host)))
341 printf ("missing host after connect\n");
344 for (j = -1, i = 0; i<MAX_CON; i++)
347 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
350 ZOOM_connection_destroy (c[i]);
353 else if (c[i] == 0 && j == -1)
356 if (i == MAX_CON) /* no match .. */
360 printf ("no more connection available\n");
363 i = j; /* OK, use this one is available */
365 c[i] = ZOOM_connection_create (options);
366 ZOOM_connection_connect (c[i], host, 0);
368 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
369 printf ("%s error: %s (%s:%d) %s\n",
370 ZOOM_connection_option_get(c[i], "host"), errmsg,
371 dset, error, addinfo);
374 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
375 ZOOM_options options,
381 cmd_len = next_token (buf, &cmd_str);
384 if (is_command ("quit", cmd_str, cmd_len))
386 else if (is_command ("set", cmd_str, cmd_len))
387 cmd_set (c, r, options, buf);
388 else if (is_command ("get", cmd_str, cmd_len))
389 cmd_get (c, r, options, buf);
390 else if (is_command ("connect", cmd_str, cmd_len))
391 cmd_connect (c, r, options, buf);
392 else if (is_command ("open", cmd_str, cmd_len))
393 cmd_connect (c, r, options, buf);
394 else if (is_command ("search", cmd_str, cmd_len))
395 cmd_search (c, r, options, buf);
396 else if (is_command ("find", cmd_str, cmd_len))
397 cmd_search (c, r, options, buf);
398 else if (is_command ("show", cmd_str, cmd_len))
399 cmd_show (c, r, options, buf);
400 else if (is_command ("close", cmd_str, cmd_len))
401 cmd_close (c, r, options, buf);
402 else if (is_command ("help", cmd_str, cmd_len))
403 cmd_help(c, r, options, buf);
404 else if (is_command ("ext", cmd_str, cmd_len))
405 cmd_ext(c, r, options, buf);
406 else if (is_command ("debug", cmd_str, cmd_len))
407 cmd_debug(c, r, options, buf);
409 printf ("unknown command %.*s\n", cmd_len, cmd_str);
413 void shell(ZOOM_connection *c, ZOOM_resultset *r,
414 ZOOM_options options)
420 const char *bp = buf;
421 #if HAVE_READLINE_READLINE_H
423 line_in=readline("ZOOM>");
426 #if HAVE_READLINE_HISTORY_H
428 add_history(line_in);
430 if(strlen(line_in) > 999) {
431 printf("Input line too long\n");
437 printf ("ZOOM>"); fflush (stdout);
438 if (!fgets (buf, 999, stdin))
441 if ((cp = strchr(buf, '\n')))
443 if (!cmd_parse (c, r, options, &bp))
448 int main (int argc, char **argv)
450 ZOOM_options options = ZOOM_options_create();
452 ZOOM_connection z39_con[MAX_CON];
453 ZOOM_resultset z39_res[MAX_CON];
456 for (i = 0; i<MAX_CON; i++)
462 for (i = 0; i<MAX_CON; i++)
466 for (i = 1; i<argc; i++)
468 const char *bp = argv[i];
469 res = cmd_parse(z39_con, z39_res, options, &bp);
470 if (res == 0) /* received quit */
473 if (res) /* do cmdline shell only if not quitting */
474 shell(z39_con, z39_res, options);
475 ZOOM_options_destroy(options);
477 for (i = 0; i<MAX_CON; i++)
479 ZOOM_connection_destroy(z39_con[i]);
480 ZOOM_resultset_destroy(z39_res[i]);