2 * $Id: zoomsh.c,v 1.21 2003-07-09 23:00:21 mike 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>
28 static int next_token (const char **cpp, const char **t_start)
31 const char *cp = *cpp;
38 while (*cp && *cp != '"')
49 while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
58 return len; /* return -1 if no token was read .. */
61 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
64 int len = next_token (cpp, &start);
72 memcpy (buf_out, start, len);
77 static int is_command (const char *cmd_str, const char *this_str, int this_len)
79 int cmd_len = strlen(cmd_str);
80 if (cmd_len != this_len)
82 if (memcmp (cmd_str, this_str, cmd_len))
87 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
91 char key[40], val[80];
93 if (next_token_copy (args, key, sizeof(key)) < 0)
95 printf ("missing argument for set\n");
98 if (next_token_copy (args, val, sizeof(val)) < 0)
99 ZOOM_options_set(options, key, 0);
101 ZOOM_options_set(options, key, val);
104 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
105 ZOOM_options options,
109 if (next_token_copy (args, key, sizeof(key)) < 0)
111 printf ("missing argument for get\n");
115 const char *val = ZOOM_options_get(options, key);
116 printf ("%s = %s\n", key, val ? val : "<null>");
120 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
121 ZOOM_options options,
126 next_token_copy (args, host, sizeof(host));
127 for (i = 0; i<MAX_CON; i++)
132 if ((h = ZOOM_connection_option_get(c[i], "host"))
133 && !strcmp (h, host))
135 ZOOM_connection_destroy (c[i]);
138 else if (*host == '\0')
140 ZOOM_connection_destroy (c[i]);
146 static const char *oid_name_to_dotstring(const char *name) {
149 static char oidbuf[100]; /* ### bad interface */
152 /* Translate syntax to oid_val */
153 oid_value value = oid_getvalbyname(name);
155 /* Build it into an oident */
156 ent.proto = PROTO_Z3950;
157 ent.oclass = CLASS_RECSYN;
160 /* Translate to an array of int */
161 (void) oid_ent_to_oid(&ent, oid);
163 /* Write the array of int into a dotted string (phew!) */
165 for (i = 0; oid[i] != -1; i++) {
167 sprintf(tmpbuf, "%d", oid[i]);
168 if (i > 0) strcat(oidbuf, ".");
169 strcat(oidbuf, tmpbuf);
175 static void display_records (ZOOM_connection c,
177 int start, int count)
180 for (i = 0; i<count; i++)
183 ZOOM_record rec = ZOOM_resultset_record (r, pos);
184 const char *db = ZOOM_record_get (rec, "database", 0);
186 const char *render = ZOOM_record_get (rec, "render", &len);
187 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
188 /* if rec is non-null, we got a record for display */
191 const char *syntax_oid = oid_name_to_dotstring(syntax);
192 printf ("%d %s %s (%s)\n",
193 pos+1, (db ? db : "unknown"), syntax, syntax_oid);
195 fwrite (render, 1, len, stdout);
201 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
202 ZOOM_options options,
206 char start_str[10], count_str[10];
208 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
209 ZOOM_options_set (options, "start", start_str);
211 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
212 ZOOM_options_set (options, "count", count_str);
214 for (i = 0; i<MAX_CON; i++)
215 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
216 while (ZOOM_event (MAX_CON, c))
219 for (i = 0; i<MAX_CON; i++)
222 const char *errmsg, *addinfo, *dset;
223 /* display errors if any */
226 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
227 printf ("%s error: %s (%s:%d) %s\n",
228 ZOOM_connection_option_get(c[i], "host"), errmsg,
229 dset, error, addinfo);
232 /* OK, no major errors. Display records... */
233 int start = ZOOM_options_get_int (options, "start", 0);
234 int count = ZOOM_options_get_int (options, "count", 0);
235 display_records (c[i], r[i], start, count);
238 ZOOM_options_set (options, "count", "0");
239 ZOOM_options_set (options, "start", "0");
242 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
243 ZOOM_options options,
246 ZOOM_package p[MAX_CON];
250 for (i = 0; i<MAX_CON; i++)
254 p[i] = ZOOM_connection_package (c[i], 0);
255 ZOOM_package_send(p[i], "itemorder");
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);
279 ZOOM_package_destroy (p[i]);
283 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
284 ZOOM_options options,
287 yaz_log_init_level(LOG_ALL);
290 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
291 ZOOM_options options,
295 const char *query_str = *args;
298 s = ZOOM_query_create ();
299 while (*query_str == ' ')
301 if (memcmp(query_str, "cql:", 4) == 0)
303 ZOOM_query_cql (s, query_str + 4);
305 else if (ZOOM_query_prefix (s, query_str))
307 printf ("Bad PQF: %s\n", query_str);
310 for (i = 0; i<MAX_CON; i++)
314 ZOOM_resultset_destroy (r[i]);
318 r[i] = ZOOM_connection_search (c[i], s);
321 while (ZOOM_event (MAX_CON, c))
324 for (i = 0; i<MAX_CON; i++)
327 const char *errmsg, *addinfo, *dset;
328 /* display errors if any */
331 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
332 printf ("%s error: %s (%s:%d) %s\n",
333 ZOOM_connection_option_get(c[i], "host"), errmsg,
334 dset, error, addinfo);
337 /* OK, no major errors. Look at the result count */
338 int start = ZOOM_options_get_int (options, "start", 0);
339 int count = ZOOM_options_get_int (options, "count", 0);
341 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
342 ZOOM_resultset_size(r[i]));
344 display_records (c[i], r[i], start, count);
347 ZOOM_query_destroy (s);
350 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
351 ZOOM_options options,
354 const char *start_term = *args;
356 ZOOM_scanset s[MAX_CON];
358 while (*start_term == ' ')
361 for (i = 0; i<MAX_CON; i++)
364 s[i] = ZOOM_connection_scan(c[i], start_term);
368 while (ZOOM_event(MAX_CON, c))
370 for (i = 0; i<MAX_CON; i++)
373 size_t p, sz = ZOOM_scanset_size(s[i]);
374 for (p = 0; p < sz; p++)
378 const char *term = ZOOM_scanset_term(s[i], p, &occ, &len);
379 fwrite(term, 1, len, stdout);
380 printf (" %d\n", occ);
382 ZOOM_scanset_destroy(s[i]);
387 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
388 ZOOM_options options,
391 printf ("connect <zurl>\n");
392 printf ("search <pqf>\n");
393 printf ("show [<start> [<count>]\n");
394 printf ("scan <term>\n");
396 printf ("close <zurl>\n");
397 printf ("set <option> [<value>]\n");
398 printf ("get <option>\n");
400 printf ("options:\n");
403 printf (" databaseName\n");
404 printf (" preferredRecordSyntax\n");
406 printf (" elementSetName\n");
407 printf (" maximumRecordSize\n");
408 printf (" preferredRecordSize\n");
410 printf (" piggyback\n");
414 printf (" implementationName\n");
415 printf (" charset\n");
419 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
420 ZOOM_options options,
424 const char *errmsg, *addinfo, *dset;
427 if (next_token_copy (args, host, sizeof(host)) < 0)
429 printf ("missing host after connect\n");
432 for (j = -1, i = 0; i<MAX_CON; i++)
435 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
438 ZOOM_connection_destroy (c[i]);
441 else if (c[i] == 0 && j == -1)
444 if (i == MAX_CON) /* no match .. */
448 printf ("no more connection available\n");
451 i = j; /* OK, use this one is available */
453 c[i] = ZOOM_connection_create (options);
454 ZOOM_connection_connect (c[i], host, 0);
456 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
457 printf ("%s error: %s (%s:%d) %s\n",
458 ZOOM_connection_option_get(c[i], "host"), errmsg,
459 dset, error, addinfo);
462 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
463 ZOOM_options options,
469 cmd_len = next_token (buf, &cmd_str);
472 if (is_command ("quit", cmd_str, cmd_len))
474 else if (is_command ("set", cmd_str, cmd_len))
475 cmd_set (c, r, options, buf);
476 else if (is_command ("get", cmd_str, cmd_len))
477 cmd_get (c, r, options, buf);
478 else if (is_command ("connect", cmd_str, cmd_len))
479 cmd_connect (c, r, options, buf);
480 else if (is_command ("open", cmd_str, cmd_len))
481 cmd_connect (c, r, options, buf);
482 else if (is_command ("search", cmd_str, cmd_len))
483 cmd_search (c, r, options, buf);
484 else if (is_command ("find", cmd_str, cmd_len))
485 cmd_search (c, r, options, buf);
486 else if (is_command ("show", cmd_str, cmd_len))
487 cmd_show (c, r, options, buf);
488 else if (is_command ("close", cmd_str, cmd_len))
489 cmd_close (c, r, options, buf);
490 else if (is_command ("help", cmd_str, cmd_len))
491 cmd_help(c, r, options, buf);
492 else if (is_command ("ext", cmd_str, cmd_len))
493 cmd_ext(c, r, options, buf);
494 else if (is_command ("debug", cmd_str, cmd_len))
495 cmd_debug(c, r, options, buf);
496 else if (is_command ("scan", cmd_str, cmd_len))
497 cmd_scan(c, r, options, buf);
499 printf ("unknown command %.*s\n", cmd_len, cmd_str);
503 void shell(ZOOM_connection *c, ZOOM_resultset *r,
504 ZOOM_options options)
510 const char *bp = buf;
511 #if HAVE_READLINE_READLINE_H
513 line_in=readline("ZOOM>");
516 #if HAVE_READLINE_HISTORY_H
518 add_history(line_in);
520 if(strlen(line_in) > 999) {
521 printf("Input line too long\n");
527 printf ("ZOOM>"); fflush (stdout);
528 if (!fgets (buf, 999, stdin))
531 if ((cp = strchr(buf, '\n')))
533 if (!cmd_parse (c, r, options, &bp))
538 int main (int argc, char **argv)
540 ZOOM_options options = ZOOM_options_create();
542 ZOOM_connection z39_con[MAX_CON];
543 ZOOM_resultset z39_res[MAX_CON];
546 for (i = 0; i<MAX_CON; i++)
552 for (i = 0; i<MAX_CON; i++)
556 for (i = 1; i<argc; i++)
558 const char *bp = argv[i];
559 res = cmd_parse(z39_con, z39_res, options, &bp);
560 if (res == 0) /* received quit */
563 if (res) /* do cmdline shell only if not quitting */
564 shell(z39_con, z39_res, options);
565 ZOOM_options_destroy(options);
567 for (i = 0; i<MAX_CON; i++)
569 ZOOM_connection_destroy(z39_con[i]);
570 ZOOM_resultset_destroy(z39_res[i]);