2 * $Id: zoomsh.c,v 1.11 2002-06-05 21:09:20 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>
25 static int next_token (const char **cpp, const char **t_start)
28 const char *cp = *cpp;
32 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
41 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
44 int len = next_token (cpp, &start);
52 memcpy (buf_out, start, len);
57 static int is_command (const char *cmd_str, const char *this_str, int this_len)
59 int cmd_len = strlen(cmd_str);
60 if (cmd_len != this_len)
62 if (memcmp (cmd_str, this_str, cmd_len))
67 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
71 char key[40], val[80];
73 if (!next_token_copy (args, key, sizeof(key)))
75 printf ("missing argument for set\n");
78 if (!next_token_copy (args, val, sizeof(val)))
79 ZOOM_options_set(options, key, 0);
81 ZOOM_options_set(options, key, val);
84 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
89 if (!next_token_copy (args, key, sizeof(key)))
91 printf ("missing argument for get\n");
95 const char *val = ZOOM_options_get(options, key);
96 printf ("%s = %s\n", key, val ? val : "<null>");
100 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
101 ZOOM_options options,
106 next_token_copy (args, host, sizeof(host));
107 for (i = 0; i<MAX_CON; i++)
112 if ((h = ZOOM_connection_option_get(c[i], "host"))
113 && !strcmp (h, host))
115 ZOOM_connection_destroy (c[i]);
118 else if (*host == '\0')
120 ZOOM_connection_destroy (c[i]);
126 static void display_records (ZOOM_connection c,
128 int start, int count)
131 for (i = 0; i<count; i++)
134 ZOOM_record rec = ZOOM_resultset_record (r, pos);
135 const char *db = ZOOM_record_get (rec, "database", 0);
137 const char *render = ZOOM_record_get (rec, "render", &len);
138 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
139 /* if rec is non-null, we got a record for display */
142 printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
144 fwrite (render, 1, len, stdout);
150 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
151 ZOOM_options options,
155 char start_str[10], count_str[10];
157 if (next_token_copy (args, start_str, sizeof(start_str)))
158 ZOOM_options_set (options, "start", start_str);
160 if (next_token_copy (args, count_str, sizeof(count_str)))
161 ZOOM_options_set (options, "count", count_str);
163 for (i = 0; i<MAX_CON; i++)
164 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
165 while (ZOOM_event (MAX_CON, c))
168 for (i = 0; i<MAX_CON; i++)
171 const char *errmsg, *addinfo;
172 /* display errors if any */
175 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
176 fprintf (stderr, "%s error: %s (%d) %s\n",
177 ZOOM_connection_option_get(c[i], "host"), errmsg,
181 /* OK, no major errors. Display records... */
182 int start = ZOOM_options_get_int (options, "start", 0);
183 int count = ZOOM_options_get_int (options, "count", 0);
184 display_records (c[i], r[i], start, count);
189 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
190 ZOOM_options options,
193 ZOOM_package p[MAX_CON];
197 for (i = 0; i<MAX_CON; i++)
201 p[i] = ZOOM_connection_package (c[i], 0);
202 ZOOM_package_send(p[i], "itemorder");
208 while (ZOOM_event (MAX_CON, c))
211 for (i = 0; i<MAX_CON; i++)
214 const char *errmsg, *addinfo;
215 /* display errors if any */
218 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
219 fprintf (stderr, "%s error: %s (%d) %s\n",
220 ZOOM_connection_option_get(c[i], "host"), errmsg,
226 ZOOM_package_destroy (p[i]);
230 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
231 ZOOM_options options,
237 s = ZOOM_query_create ();
238 if (ZOOM_query_prefix (s, *args))
240 fprintf (stderr, "Bad PQF: %s\n", *args);
243 for (i = 0; i<MAX_CON; i++)
247 ZOOM_resultset_destroy (r[i]);
251 r[i] = ZOOM_connection_search (c[i], s);
254 while (ZOOM_event (MAX_CON, c))
257 for (i = 0; i<MAX_CON; i++)
260 const char *errmsg, *addinfo;
261 /* display errors if any */
264 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
265 fprintf (stderr, "%s error: %s (%d) %s\n",
266 ZOOM_connection_option_get(c[i], "host"), errmsg,
270 /* OK, no major errors. Look at the result count */
271 int start = ZOOM_options_get_int (options, "start", 0);
272 int count = ZOOM_options_get_int (options, "count", 0);
274 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
275 ZOOM_resultset_size(r[i]));
277 display_records (c[i], r[i], start, count);
280 ZOOM_query_destroy (s);
283 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
284 ZOOM_options options,
287 printf ("connect <zurl>\n");
288 printf ("search <pqf>\n");
289 printf ("show [<start> [<count>]\n");
291 printf ("close <zurl>\n");
292 printf ("set <option> [<value>]\n");
293 printf ("get <option>\n");
295 printf ("options:\n");
298 printf (" databaseName\n");
299 printf (" preferredRecordSyntax\n");
301 printf (" elementSetName\n");
302 printf (" maximumRecordSize\n");
303 printf (" preferredRecordSize\n");
305 printf (" piggyback\n");
309 printf (" implementationName\n");
310 printf (" charset\n");
314 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
315 ZOOM_options options,
319 const char *errmsg, *addinfo;
322 if (!next_token_copy (args, host, sizeof(host)))
324 printf ("missing host after connect\n");
327 for (j = -1, i = 0; i<MAX_CON; i++)
330 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
333 ZOOM_connection_destroy (c[i]);
336 else if (c[i] == 0 && j == -1)
339 if (i == MAX_CON) /* no match .. */
343 printf ("no more connection available\n");
346 i = j; /* OK, use this one is available */
348 c[i] = ZOOM_connection_create (options);
349 ZOOM_connection_connect (c[i], host, 0);
351 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
352 printf ("%s error: %s (%d) %s\n",
353 ZOOM_connection_option_get(c[i], "host"),
354 errmsg, error, addinfo);
358 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
359 ZOOM_options options,
365 cmd_len = next_token (buf, &cmd_str);
368 if (is_command ("quit", cmd_str, cmd_len))
370 else if (is_command ("set", cmd_str, cmd_len))
371 cmd_set (c, r, options, buf);
372 else if (is_command ("get", cmd_str, cmd_len))
373 cmd_get (c, r, options, buf);
374 else if (is_command ("connect", cmd_str, cmd_len))
375 cmd_connect (c, r, options, buf);
376 else if (is_command ("open", cmd_str, cmd_len))
377 cmd_connect (c, r, options, buf);
378 else if (is_command ("search", cmd_str, cmd_len))
379 cmd_search (c, r, options, buf);
380 else if (is_command ("find", cmd_str, cmd_len))
381 cmd_search (c, r, options, buf);
382 else if (is_command ("show", cmd_str, cmd_len))
383 cmd_show (c, r, options, buf);
384 else if (is_command ("close", cmd_str, cmd_len))
385 cmd_close (c, r, options, buf);
386 else if (is_command ("help", cmd_str, cmd_len))
387 cmd_help(c, r, options, buf);
388 else if (is_command ("ext", cmd_str, cmd_len))
389 cmd_ext(c, r, options, buf);
391 printf ("unknown command %.*s\n", cmd_len, cmd_str);
395 void shell(ZOOM_connection *c, ZOOM_resultset *r,
396 ZOOM_options options)
402 const char *bp = buf;
403 #if HAVE_READLINE_READLINE_H
405 line_in=readline("ZOOM>");
408 #if HAVE_READLINE_HISTORY_H
410 add_history(line_in);
412 if(strlen(line_in) > 999) {
413 fprintf(stderr,"Input line too long\n");
419 printf ("ZOOM>"); fflush (stdout);
420 if (!fgets (buf, 999, stdin))
423 if ((cp = strchr(buf, '\n')))
425 if (!cmd_parse (c, r, options, &bp))
430 int main (int argc, char **argv)
432 ZOOM_options options = ZOOM_options_create();
434 ZOOM_connection z39_con[MAX_CON];
435 ZOOM_resultset z39_res[MAX_CON];
436 for (i = 0; i<MAX_CON; i++)
442 for (i = 0; i<MAX_CON; i++)
446 for (i = 1; i<argc; i++)
448 const char *bp = argv[i];
449 res = cmd_parse(z39_con, z39_res, options, &bp);
450 if (res == 0) /* received quit */
453 if (res) /* do cmdline shell only if not quitting */
454 shell(z39_con, z39_res, options);
455 ZOOM_options_destroy(options);
457 for (i = 0; i<MAX_CON; i++)
459 ZOOM_connection_destroy(z39_con[i]);
460 ZOOM_resultset_destroy(z39_res[i]);