2 * $Id: zoomsh.c,v 1.20 2003-05-26 11:35:46 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>
27 static int next_token (const char **cpp, const char **t_start)
30 const char *cp = *cpp;
37 while (*cp && *cp != '"')
48 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
57 return len; /* return -1 if no token was read .. */
60 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
63 int len = next_token (cpp, &start);
71 memcpy (buf_out, start, len);
76 static int is_command (const char *cmd_str, const char *this_str, int this_len)
78 int cmd_len = strlen(cmd_str);
79 if (cmd_len != this_len)
81 if (memcmp (cmd_str, this_str, cmd_len))
86 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
90 char key[40], val[80];
92 if (next_token_copy (args, key, sizeof(key)) < 0)
94 printf ("missing argument for set\n");
97 if (next_token_copy (args, val, sizeof(val)) < 0)
98 ZOOM_options_set(options, key, 0);
100 ZOOM_options_set(options, key, val);
103 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
104 ZOOM_options options,
108 if (next_token_copy (args, key, sizeof(key)) < 0)
110 printf ("missing argument for get\n");
114 const char *val = ZOOM_options_get(options, key);
115 printf ("%s = %s\n", key, val ? val : "<null>");
119 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
120 ZOOM_options options,
125 next_token_copy (args, host, sizeof(host));
126 for (i = 0; i<MAX_CON; i++)
131 if ((h = ZOOM_connection_option_get(c[i], "host"))
132 && !strcmp (h, host))
134 ZOOM_connection_destroy (c[i]);
137 else if (*host == '\0')
139 ZOOM_connection_destroy (c[i]);
145 static void display_records (ZOOM_connection c,
147 int start, int count)
150 for (i = 0; i<count; i++)
153 ZOOM_record rec = ZOOM_resultset_record (r, pos);
154 const char *db = ZOOM_record_get (rec, "database", 0);
156 const char *render = ZOOM_record_get (rec, "render", &len);
157 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
158 /* if rec is non-null, we got a record for display */
161 printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
163 fwrite (render, 1, len, stdout);
169 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
170 ZOOM_options options,
174 char start_str[10], count_str[10];
176 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
177 ZOOM_options_set (options, "start", start_str);
179 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
180 ZOOM_options_set (options, "count", count_str);
182 for (i = 0; i<MAX_CON; i++)
183 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
184 while (ZOOM_event (MAX_CON, c))
187 for (i = 0; i<MAX_CON; i++)
190 const char *errmsg, *addinfo, *dset;
191 /* display errors if any */
194 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
195 printf ("%s error: %s (%s:%d) %s\n",
196 ZOOM_connection_option_get(c[i], "host"), errmsg,
197 dset, error, addinfo);
200 /* OK, no major errors. Display records... */
201 int start = ZOOM_options_get_int (options, "start", 0);
202 int count = ZOOM_options_get_int (options, "count", 0);
203 display_records (c[i], r[i], start, count);
206 ZOOM_options_set (options, "count", "0");
207 ZOOM_options_set (options, "start", "0");
210 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
211 ZOOM_options options,
214 ZOOM_package p[MAX_CON];
218 for (i = 0; i<MAX_CON; i++)
222 p[i] = ZOOM_connection_package (c[i], 0);
223 ZOOM_package_send(p[i], "itemorder");
229 while (ZOOM_event (MAX_CON, c))
232 for (i = 0; i<MAX_CON; i++)
235 const char *errmsg, *addinfo, *dset;
236 /* display errors if any */
239 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
240 printf ("%s error: %s (%s:%d) %s\n",
241 ZOOM_connection_option_get(c[i], "host"), errmsg,
242 dset, error, addinfo);
247 ZOOM_package_destroy (p[i]);
251 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
252 ZOOM_options options,
255 yaz_log_init_level(LOG_ALL);
258 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
259 ZOOM_options options,
263 const char *query_str = *args;
266 s = ZOOM_query_create ();
267 while (*query_str == ' ')
269 if (memcmp(query_str, "cql:", 4) == 0)
271 ZOOM_query_cql (s, query_str + 4);
273 else if (ZOOM_query_prefix (s, query_str))
275 printf ("Bad PQF: %s\n", query_str);
278 for (i = 0; i<MAX_CON; i++)
282 ZOOM_resultset_destroy (r[i]);
286 r[i] = ZOOM_connection_search (c[i], s);
289 while (ZOOM_event (MAX_CON, c))
292 for (i = 0; i<MAX_CON; i++)
295 const char *errmsg, *addinfo, *dset;
296 /* display errors if any */
299 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
300 printf ("%s error: %s (%s:%d) %s\n",
301 ZOOM_connection_option_get(c[i], "host"), errmsg,
302 dset, error, addinfo);
305 /* OK, no major errors. Look at the result count */
306 int start = ZOOM_options_get_int (options, "start", 0);
307 int count = ZOOM_options_get_int (options, "count", 0);
309 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
310 ZOOM_resultset_size(r[i]));
312 display_records (c[i], r[i], start, count);
315 ZOOM_query_destroy (s);
318 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
319 ZOOM_options options,
322 const char *start_term = *args;
324 ZOOM_scanset s[MAX_CON];
326 while (*start_term == ' ')
329 for (i = 0; i<MAX_CON; i++)
332 s[i] = ZOOM_connection_scan(c[i], start_term);
336 while (ZOOM_event(MAX_CON, c))
338 for (i = 0; i<MAX_CON; i++)
341 size_t p, sz = ZOOM_scanset_size(s[i]);
342 for (p = 0; p < sz; p++)
346 const char *term = ZOOM_scanset_term(s[i], p, &occ, &len);
347 fwrite(term, 1, len, stdout);
348 printf (" %d\n", occ);
350 ZOOM_scanset_destroy(s[i]);
355 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
356 ZOOM_options options,
359 printf ("connect <zurl>\n");
360 printf ("search <pqf>\n");
361 printf ("show [<start> [<count>]\n");
362 printf ("scan <term>\n");
364 printf ("close <zurl>\n");
365 printf ("set <option> [<value>]\n");
366 printf ("get <option>\n");
368 printf ("options:\n");
371 printf (" databaseName\n");
372 printf (" preferredRecordSyntax\n");
374 printf (" elementSetName\n");
375 printf (" maximumRecordSize\n");
376 printf (" preferredRecordSize\n");
378 printf (" piggyback\n");
382 printf (" implementationName\n");
383 printf (" charset\n");
387 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
388 ZOOM_options options,
392 const char *errmsg, *addinfo, *dset;
395 if (next_token_copy (args, host, sizeof(host)) < 0)
397 printf ("missing host after connect\n");
400 for (j = -1, i = 0; i<MAX_CON; i++)
403 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
406 ZOOM_connection_destroy (c[i]);
409 else if (c[i] == 0 && j == -1)
412 if (i == MAX_CON) /* no match .. */
416 printf ("no more connection available\n");
419 i = j; /* OK, use this one is available */
421 c[i] = ZOOM_connection_create (options);
422 ZOOM_connection_connect (c[i], host, 0);
424 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
425 printf ("%s error: %s (%s:%d) %s\n",
426 ZOOM_connection_option_get(c[i], "host"), errmsg,
427 dset, error, addinfo);
430 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
431 ZOOM_options options,
437 cmd_len = next_token (buf, &cmd_str);
440 if (is_command ("quit", cmd_str, cmd_len))
442 else if (is_command ("set", cmd_str, cmd_len))
443 cmd_set (c, r, options, buf);
444 else if (is_command ("get", cmd_str, cmd_len))
445 cmd_get (c, r, options, buf);
446 else if (is_command ("connect", cmd_str, cmd_len))
447 cmd_connect (c, r, options, buf);
448 else if (is_command ("open", cmd_str, cmd_len))
449 cmd_connect (c, r, options, buf);
450 else if (is_command ("search", cmd_str, cmd_len))
451 cmd_search (c, r, options, buf);
452 else if (is_command ("find", cmd_str, cmd_len))
453 cmd_search (c, r, options, buf);
454 else if (is_command ("show", cmd_str, cmd_len))
455 cmd_show (c, r, options, buf);
456 else if (is_command ("close", cmd_str, cmd_len))
457 cmd_close (c, r, options, buf);
458 else if (is_command ("help", cmd_str, cmd_len))
459 cmd_help(c, r, options, buf);
460 else if (is_command ("ext", cmd_str, cmd_len))
461 cmd_ext(c, r, options, buf);
462 else if (is_command ("debug", cmd_str, cmd_len))
463 cmd_debug(c, r, options, buf);
464 else if (is_command ("scan", cmd_str, cmd_len))
465 cmd_scan(c, r, options, buf);
467 printf ("unknown command %.*s\n", cmd_len, cmd_str);
471 void shell(ZOOM_connection *c, ZOOM_resultset *r,
472 ZOOM_options options)
478 const char *bp = buf;
479 #if HAVE_READLINE_READLINE_H
481 line_in=readline("ZOOM>");
484 #if HAVE_READLINE_HISTORY_H
486 add_history(line_in);
488 if(strlen(line_in) > 999) {
489 printf("Input line too long\n");
495 printf ("ZOOM>"); fflush (stdout);
496 if (!fgets (buf, 999, stdin))
499 if ((cp = strchr(buf, '\n')))
501 if (!cmd_parse (c, r, options, &bp))
506 int main (int argc, char **argv)
508 ZOOM_options options = ZOOM_options_create();
510 ZOOM_connection z39_con[MAX_CON];
511 ZOOM_resultset z39_res[MAX_CON];
514 for (i = 0; i<MAX_CON; i++)
520 for (i = 0; i<MAX_CON; i++)
524 for (i = 1; i<argc; i++)
526 const char *bp = argv[i];
527 res = cmd_parse(z39_con, z39_res, options, &bp);
528 if (res == 0) /* received quit */
531 if (res) /* do cmdline shell only if not quitting */
532 shell(z39_con, z39_res, options);
533 ZOOM_options_destroy(options);
535 for (i = 0; i<MAX_CON; i++)
537 ZOOM_connection_destroy(z39_con[i]);
538 ZOOM_resultset_destroy(z39_res[i]);