2 * $Id: zoomsh.c,v 1.15 2003-02-17 21:23:31 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);
190 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
191 ZOOM_options options,
194 ZOOM_package p[MAX_CON];
198 for (i = 0; i<MAX_CON; i++)
202 p[i] = ZOOM_connection_package (c[i], 0);
203 ZOOM_package_send(p[i], "itemorder");
209 while (ZOOM_event (MAX_CON, c))
212 for (i = 0; i<MAX_CON; i++)
215 const char *errmsg, *addinfo, *dset;
216 /* display errors if any */
219 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
220 printf ("%s error: %s (%s:%d) %s\n",
221 ZOOM_connection_option_get(c[i], "host"), errmsg,
222 dset, error, addinfo);
227 ZOOM_package_destroy (p[i]);
231 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
232 ZOOM_options options,
235 yaz_log_init_level(LOG_ALL);
238 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
239 ZOOM_options options,
243 const char *query_str = *args;
246 s = ZOOM_query_create ();
247 while (*query_str == ' ')
249 if (memcmp(query_str, "cql:", 4) == 0)
251 ZOOM_query_cql (s, query_str + 4);
253 else if (ZOOM_query_prefix (s, query_str))
255 printf ("Bad PQF: %s\n", query_str);
258 for (i = 0; i<MAX_CON; i++)
262 ZOOM_resultset_destroy (r[i]);
266 r[i] = ZOOM_connection_search (c[i], s);
269 while (ZOOM_event (MAX_CON, c))
272 for (i = 0; i<MAX_CON; i++)
275 const char *errmsg, *addinfo, *dset;
276 /* display errors if any */
279 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
280 printf ("%s error: %s (%s:%d) %s\n",
281 ZOOM_connection_option_get(c[i], "host"), errmsg,
282 dset, error, addinfo);
285 /* OK, no major errors. Look at the result count */
286 int start = ZOOM_options_get_int (options, "start", 0);
287 int count = ZOOM_options_get_int (options, "count", 0);
289 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
290 ZOOM_resultset_size(r[i]));
292 display_records (c[i], r[i], start, count);
295 ZOOM_query_destroy (s);
298 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
299 ZOOM_options options,
302 printf ("connect <zurl>\n");
303 printf ("search <pqf>\n");
304 printf ("show [<start> [<count>]\n");
306 printf ("close <zurl>\n");
307 printf ("set <option> [<value>]\n");
308 printf ("get <option>\n");
310 printf ("options:\n");
313 printf (" databaseName\n");
314 printf (" preferredRecordSyntax\n");
316 printf (" elementSetName\n");
317 printf (" maximumRecordSize\n");
318 printf (" preferredRecordSize\n");
320 printf (" piggyback\n");
324 printf (" implementationName\n");
325 printf (" charset\n");
329 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
330 ZOOM_options options,
334 const char *errmsg, *addinfo, *dset;
337 if (!next_token_copy (args, host, sizeof(host)))
339 printf ("missing host after connect\n");
342 for (j = -1, i = 0; i<MAX_CON; i++)
345 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
348 ZOOM_connection_destroy (c[i]);
351 else if (c[i] == 0 && j == -1)
354 if (i == MAX_CON) /* no match .. */
358 printf ("no more connection available\n");
361 i = j; /* OK, use this one is available */
363 c[i] = ZOOM_connection_create (options);
364 ZOOM_connection_connect (c[i], host, 0);
366 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
367 printf ("%s error: %s (%s:%d) %s\n",
368 ZOOM_connection_option_get(c[i], "host"), errmsg,
369 dset, error, addinfo);
372 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
373 ZOOM_options options,
379 cmd_len = next_token (buf, &cmd_str);
382 if (is_command ("quit", cmd_str, cmd_len))
384 else if (is_command ("set", cmd_str, cmd_len))
385 cmd_set (c, r, options, buf);
386 else if (is_command ("get", cmd_str, cmd_len))
387 cmd_get (c, r, options, buf);
388 else if (is_command ("connect", cmd_str, cmd_len))
389 cmd_connect (c, r, options, buf);
390 else if (is_command ("open", cmd_str, cmd_len))
391 cmd_connect (c, r, options, buf);
392 else if (is_command ("search", cmd_str, cmd_len))
393 cmd_search (c, r, options, buf);
394 else if (is_command ("find", cmd_str, cmd_len))
395 cmd_search (c, r, options, buf);
396 else if (is_command ("show", cmd_str, cmd_len))
397 cmd_show (c, r, options, buf);
398 else if (is_command ("close", cmd_str, cmd_len))
399 cmd_close (c, r, options, buf);
400 else if (is_command ("help", cmd_str, cmd_len))
401 cmd_help(c, r, options, buf);
402 else if (is_command ("ext", cmd_str, cmd_len))
403 cmd_ext(c, r, options, buf);
404 else if (is_command ("debug", cmd_str, cmd_len))
405 cmd_debug(c, r, options, buf);
407 printf ("unknown command %.*s\n", cmd_len, cmd_str);
411 void shell(ZOOM_connection *c, ZOOM_resultset *r,
412 ZOOM_options options)
418 const char *bp = buf;
419 #if HAVE_READLINE_READLINE_H
421 line_in=readline("ZOOM>");
424 #if HAVE_READLINE_HISTORY_H
426 add_history(line_in);
428 if(strlen(line_in) > 999) {
429 printf("Input line too long\n");
435 printf ("ZOOM>"); fflush (stdout);
436 if (!fgets (buf, 999, stdin))
439 if ((cp = strchr(buf, '\n')))
441 if (!cmd_parse (c, r, options, &bp))
446 int main (int argc, char **argv)
448 ZOOM_options options = ZOOM_options_create();
450 ZOOM_connection z39_con[MAX_CON];
451 ZOOM_resultset z39_res[MAX_CON];
454 for (i = 0; i<MAX_CON; i++)
460 for (i = 0; i<MAX_CON; i++)
464 for (i = 1; i<argc; i++)
466 const char *bp = argv[i];
467 res = cmd_parse(z39_con, z39_res, options, &bp);
468 if (res == 0) /* received quit */
471 if (res) /* do cmdline shell only if not quitting */
472 shell(z39_con, z39_res, options);
473 ZOOM_options_destroy(options);
475 for (i = 0; i<MAX_CON; i++)
477 ZOOM_connection_destroy(z39_con[i]);
478 ZOOM_resultset_destroy(z39_res[i]);