2 * $Id: zoomsh.c,v 1.10 2002-06-02 21:27:17 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,
88 char key[40], val[80];
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,
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;
216 /* display errors if any */
219 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
220 fprintf (stderr, "%s error: %s (%d) %s\n",
221 ZOOM_connection_option_get(c[i], "host"), errmsg,
227 ZOOM_package_destroy (p[i]);
231 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
232 ZOOM_options options,
238 s = ZOOM_query_create ();
239 if (ZOOM_query_prefix (s, *args))
241 fprintf (stderr, "Bad PQF: %s\n", *args);
244 for (i = 0; i<MAX_CON; i++)
248 ZOOM_resultset_destroy (r[i]);
252 r[i] = ZOOM_connection_search (c[i], s);
255 while (ZOOM_event (MAX_CON, c))
258 for (i = 0; i<MAX_CON; i++)
261 const char *errmsg, *addinfo;
262 /* display errors if any */
265 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
266 fprintf (stderr, "%s error: %s (%d) %s\n",
267 ZOOM_connection_option_get(c[i], "host"), errmsg,
271 /* OK, no major errors. Look at the result count */
272 int start = ZOOM_options_get_int (options, "start", 0);
273 int count = ZOOM_options_get_int (options, "count", 0);
275 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
276 ZOOM_resultset_size(r[i]));
278 display_records (c[i], r[i], start, count);
281 ZOOM_query_destroy (s);
284 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
285 ZOOM_options options,
288 printf ("connect <zurl>\n");
289 printf ("search <pqf>\n");
290 printf ("show [<start> [<count>]\n");
292 printf ("close <zurl>\n");
293 printf ("set <option> [<value>]\n");
294 printf ("get <option>\n");
296 printf ("options:\n");
299 printf (" databaseName\n");
300 printf (" preferredRecordSyntax\n");
302 printf (" elementSetName\n");
303 printf (" maximumRecordSize\n");
304 printf (" preferredRecordSize\n");
306 printf (" piggyback\n");
310 printf (" implementationName\n");
311 printf (" charset\n");
315 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
316 ZOOM_options options,
320 const char *errmsg, *addinfo;
323 if (!next_token_copy (args, host, sizeof(host)))
325 printf ("missing host after connect\n");
328 for (j = -1, i = 0; i<MAX_CON; i++)
331 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
334 ZOOM_connection_destroy (c[i]);
337 else if (c[i] == 0 && j == -1)
340 if (i == MAX_CON) /* no match .. */
344 printf ("no more connection available\n");
347 i = j; /* OK, use this one is available */
349 c[i] = ZOOM_connection_create (options);
350 ZOOM_connection_connect (c[i], host, 0);
352 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
353 printf ("%s error: %s (%d) %s\n",
354 ZOOM_connection_option_get(c[i], "host"),
355 errmsg, error, addinfo);
359 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
360 ZOOM_options options,
366 cmd_len = next_token (buf, &cmd_str);
369 if (is_command ("quit", cmd_str, cmd_len))
371 else if (is_command ("set", cmd_str, cmd_len))
372 cmd_set (c, r, options, buf);
373 else if (is_command ("get", cmd_str, cmd_len))
374 cmd_get (c, r, options, buf);
375 else if (is_command ("connect", cmd_str, cmd_len))
376 cmd_connect (c, r, options, buf);
377 else if (is_command ("open", cmd_str, cmd_len))
378 cmd_connect (c, r, options, buf);
379 else if (is_command ("search", cmd_str, cmd_len))
380 cmd_search (c, r, options, buf);
381 else if (is_command ("find", cmd_str, cmd_len))
382 cmd_search (c, r, options, buf);
383 else if (is_command ("show", cmd_str, cmd_len))
384 cmd_show (c, r, options, buf);
385 else if (is_command ("close", cmd_str, cmd_len))
386 cmd_close (c, r, options, buf);
387 else if (is_command ("help", cmd_str, cmd_len))
388 cmd_help(c, r, options, buf);
389 else if (is_command ("ext", cmd_str, cmd_len))
390 cmd_ext(c, r, options, buf);
392 printf ("unknown command %.*s\n", cmd_len, cmd_str);
396 void shell(ZOOM_connection *c, ZOOM_resultset *r,
397 ZOOM_options options)
403 const char *bp = buf;
404 #if HAVE_READLINE_READLINE_H
406 line_in=readline("ZOOM>");
409 #if HAVE_READLINE_HISTORY_H
411 add_history(line_in);
413 if(strlen(line_in) > 999) {
414 fprintf(stderr,"Input line too long\n");
420 printf ("ZOOM>"); fflush (stdout);
421 if (!fgets (buf, 999, stdin))
424 if ((cp = strchr(buf, '\n')))
426 if (!cmd_parse (c, r, options, &bp))
431 int main (int argc, char **argv)
433 ZOOM_options options = ZOOM_options_create();
435 ZOOM_connection z39_con[MAX_CON];
436 ZOOM_resultset z39_res[MAX_CON];
437 for (i = 0; i<MAX_CON; i++)
443 for (i = 0; i<MAX_CON; i++)
447 for (i = 1; i<argc; i++)
449 const char *bp = argv[i];
450 res = cmd_parse(z39_con, z39_res, options, &bp);
451 if (res == 0) /* received quit */
454 if (res) /* do cmdline shell only if not quitting */
455 shell(z39_con, z39_res, options);
456 ZOOM_options_destroy(options);
458 for (i = 0; i<MAX_CON; i++)
460 ZOOM_connection_destroy(z39_con[i]);
461 ZOOM_resultset_destroy(z39_res[i]);