2 * $Id: zoomsh.c,v 1.14 2003-02-17 14:35:42 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, *dset;
172 /* display errors if any */
175 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
176 printf ("%s error: %s (%s:%d) %s\n",
177 ZOOM_connection_option_get(c[i], "host"), errmsg,
178 dset, error, addinfo);
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, *dset;
215 /* display errors if any */
218 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
219 printf ("%s error: %s (%s:%d) %s\n",
220 ZOOM_connection_option_get(c[i], "host"), errmsg,
221 dset, error, addinfo);
226 ZOOM_package_destroy (p[i]);
230 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
231 ZOOM_options options,
235 const char *query_str = *args;
238 s = ZOOM_query_create ();
239 while (*query_str == ' ')
241 if (memcmp(query_str, "cql:", 4) == 0)
243 ZOOM_query_cql (s, query_str + 4);
245 else if (ZOOM_query_prefix (s, query_str))
247 printf ("Bad PQF: %s\n", query_str);
250 for (i = 0; i<MAX_CON; i++)
254 ZOOM_resultset_destroy (r[i]);
258 r[i] = ZOOM_connection_search (c[i], s);
261 while (ZOOM_event (MAX_CON, c))
264 for (i = 0; i<MAX_CON; i++)
267 const char *errmsg, *addinfo, *dset;
268 /* display errors if any */
271 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
272 printf ("%s error: %s (%s:%d) %s\n",
273 ZOOM_connection_option_get(c[i], "host"), errmsg,
274 dset, error, addinfo);
277 /* OK, no major errors. Look at the result count */
278 int start = ZOOM_options_get_int (options, "start", 0);
279 int count = ZOOM_options_get_int (options, "count", 0);
281 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
282 ZOOM_resultset_size(r[i]));
284 display_records (c[i], r[i], start, count);
287 ZOOM_query_destroy (s);
290 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
291 ZOOM_options options,
294 printf ("connect <zurl>\n");
295 printf ("search <pqf>\n");
296 printf ("show [<start> [<count>]\n");
298 printf ("close <zurl>\n");
299 printf ("set <option> [<value>]\n");
300 printf ("get <option>\n");
302 printf ("options:\n");
305 printf (" databaseName\n");
306 printf (" preferredRecordSyntax\n");
308 printf (" elementSetName\n");
309 printf (" maximumRecordSize\n");
310 printf (" preferredRecordSize\n");
312 printf (" piggyback\n");
316 printf (" implementationName\n");
317 printf (" charset\n");
321 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
322 ZOOM_options options,
326 const char *errmsg, *addinfo, *dset;
329 if (!next_token_copy (args, host, sizeof(host)))
331 printf ("missing host after connect\n");
334 for (j = -1, i = 0; i<MAX_CON; i++)
337 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
340 ZOOM_connection_destroy (c[i]);
343 else if (c[i] == 0 && j == -1)
346 if (i == MAX_CON) /* no match .. */
350 printf ("no more connection available\n");
353 i = j; /* OK, use this one is available */
355 c[i] = ZOOM_connection_create (options);
356 ZOOM_connection_connect (c[i], host, 0);
358 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
359 printf ("%s error: %s (%s:%d) %s\n",
360 ZOOM_connection_option_get(c[i], "host"), errmsg,
361 dset, error, addinfo);
364 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
365 ZOOM_options options,
371 cmd_len = next_token (buf, &cmd_str);
374 if (is_command ("quit", cmd_str, cmd_len))
376 else if (is_command ("set", cmd_str, cmd_len))
377 cmd_set (c, r, options, buf);
378 else if (is_command ("get", cmd_str, cmd_len))
379 cmd_get (c, r, options, buf);
380 else if (is_command ("connect", cmd_str, cmd_len))
381 cmd_connect (c, r, options, buf);
382 else if (is_command ("open", cmd_str, cmd_len))
383 cmd_connect (c, r, options, buf);
384 else if (is_command ("search", cmd_str, cmd_len))
385 cmd_search (c, r, options, buf);
386 else if (is_command ("find", cmd_str, cmd_len))
387 cmd_search (c, r, options, buf);
388 else if (is_command ("show", cmd_str, cmd_len))
389 cmd_show (c, r, options, buf);
390 else if (is_command ("close", cmd_str, cmd_len))
391 cmd_close (c, r, options, buf);
392 else if (is_command ("help", cmd_str, cmd_len))
393 cmd_help(c, r, options, buf);
394 else if (is_command ("ext", cmd_str, cmd_len))
395 cmd_ext(c, r, options, buf);
397 printf ("unknown command %.*s\n", cmd_len, cmd_str);
401 void shell(ZOOM_connection *c, ZOOM_resultset *r,
402 ZOOM_options options)
408 const char *bp = buf;
409 #if HAVE_READLINE_READLINE_H
411 line_in=readline("ZOOM>");
414 #if HAVE_READLINE_HISTORY_H
416 add_history(line_in);
418 if(strlen(line_in) > 999) {
419 printf("Input line too long\n");
425 printf ("ZOOM>"); fflush (stdout);
426 if (!fgets (buf, 999, stdin))
429 if ((cp = strchr(buf, '\n')))
431 if (!cmd_parse (c, r, options, &bp))
436 int main (int argc, char **argv)
438 ZOOM_options options = ZOOM_options_create();
440 ZOOM_connection z39_con[MAX_CON];
441 ZOOM_resultset z39_res[MAX_CON];
444 yaz_log_init_level(65535);
446 for (i = 0; i<MAX_CON; i++)
452 for (i = 0; i<MAX_CON; i++)
456 for (i = 1; i<argc; i++)
458 const char *bp = argv[i];
459 res = cmd_parse(z39_con, z39_res, options, &bp);
460 if (res == 0) /* received quit */
463 if (res) /* do cmdline shell only if not quitting */
464 shell(z39_con, z39_res, options);
465 ZOOM_options_destroy(options);
467 for (i = 0; i<MAX_CON; i++)
469 ZOOM_connection_destroy(z39_con[i]);
470 ZOOM_resultset_destroy(z39_res[i]);