2 * $Id: zoomsh.c,v 1.24 2003-11-25 23:19:59 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>
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 void display_records (ZOOM_connection c,
148 int start, int count)
151 for (i = 0; i<count; i++)
154 ZOOM_record rec = ZOOM_resultset_record (r, pos);
155 const char *db = ZOOM_record_get (rec, "database", 0);
157 const char *render = ZOOM_record_get (rec, "render", &len);
158 const char *opac_render = ZOOM_record_get (rec, "opac", &opac_len);
159 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
160 /* if rec is non-null, we got a record for display */
164 (void) oid_name_to_dotstring(CLASS_RECSYN, syntax, oidbuf);
165 printf ("%d %s %s (%s)\n",
166 pos+1, (db ? db : "unknown"), syntax, oidbuf);
168 fwrite (render, 1, len, stdout);
171 fwrite (opac_render, 1, opac_len, stdout);
177 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
178 ZOOM_options options,
182 char start_str[10], count_str[10];
184 if (next_token_copy (args, start_str, sizeof(start_str)) >= 0)
185 ZOOM_options_set (options, "start", start_str);
187 if (next_token_copy (args, count_str, sizeof(count_str)) >= 0)
188 ZOOM_options_set (options, "count", count_str);
190 for (i = 0; i<MAX_CON; i++)
191 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
192 while (ZOOM_event (MAX_CON, c))
195 for (i = 0; i<MAX_CON; i++)
198 const char *errmsg, *addinfo, *dset;
199 /* display errors if any */
202 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
203 printf ("%s error: %s (%s:%d) %s\n",
204 ZOOM_connection_option_get(c[i], "host"), errmsg,
205 dset, error, addinfo);
208 /* OK, no major errors. Display records... */
209 int start = ZOOM_options_get_int (options, "start", 0);
210 int count = ZOOM_options_get_int (options, "count", 0);
211 display_records (c[i], r[i], start, count);
214 ZOOM_options_set (options, "count", "0");
215 ZOOM_options_set (options, "start", "0");
218 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
219 ZOOM_options options,
222 ZOOM_package p[MAX_CON];
223 char ext_type_str[10];
227 if (next_token_copy (args, ext_type_str, sizeof(ext_type_str)) < 0)
230 for (i = 0; i<MAX_CON; i++)
234 p[i] = ZOOM_connection_package (c[i], 0);
235 ZOOM_package_send(p[i], ext_type_str);
241 while (ZOOM_event (MAX_CON, c))
244 for (i = 0; i<MAX_CON; i++)
247 const char *errmsg, *addinfo, *dset;
248 /* display errors if any */
251 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
252 printf ("%s error: %s (%s:%d) %s\n",
253 ZOOM_connection_option_get(c[i], "host"), errmsg,
254 dset, error, addinfo);
259 ZOOM_package_destroy (p[i]);
263 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
264 ZOOM_options options,
267 yaz_log_init_level(LOG_ALL);
270 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
271 ZOOM_options options,
275 const char *query_str = *args;
278 s = ZOOM_query_create ();
279 while (*query_str == ' ')
281 if (memcmp(query_str, "cql:", 4) == 0)
283 ZOOM_query_cql (s, query_str + 4);
285 else if (ZOOM_query_prefix (s, query_str))
287 printf ("Bad PQF: %s\n", query_str);
290 for (i = 0; i<MAX_CON; i++)
294 ZOOM_resultset_destroy (r[i]);
298 r[i] = ZOOM_connection_search (c[i], s);
301 while (ZOOM_event (MAX_CON, c))
304 for (i = 0; i<MAX_CON; i++)
307 const char *errmsg, *addinfo, *dset;
308 /* display errors if any */
311 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
312 printf ("%s error: %s (%s:%d) %s\n",
313 ZOOM_connection_option_get(c[i], "host"), errmsg,
314 dset, error, addinfo);
317 /* OK, no major errors. Look at the result count */
318 int start = ZOOM_options_get_int (options, "start", 0);
319 int count = ZOOM_options_get_int (options, "count", 0);
321 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
322 ZOOM_resultset_size(r[i]));
324 display_records (c[i], r[i], start, count);
327 ZOOM_query_destroy (s);
330 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
331 ZOOM_options options,
334 const char *start_term = *args;
336 ZOOM_scanset s[MAX_CON];
338 while (*start_term == ' ')
341 for (i = 0; i<MAX_CON; i++)
344 s[i] = ZOOM_connection_scan(c[i], start_term);
348 while (ZOOM_event(MAX_CON, c))
350 for (i = 0; i<MAX_CON; i++)
353 size_t p, sz = ZOOM_scanset_size(s[i]);
354 for (p = 0; p < sz; p++)
358 const char *term = ZOOM_scanset_term(s[i], p, &occ, &len);
359 fwrite(term, 1, len, stdout);
360 printf (" %d\n", occ);
362 ZOOM_scanset_destroy(s[i]);
367 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
368 ZOOM_options options,
371 printf ("connect <zurl>\n");
372 printf ("search <pqf>\n");
373 printf ("show [<start> [<count>]\n");
374 printf ("scan <term>\n");
376 printf ("close <zurl>\n");
377 printf ("set <option> [<value>]\n");
378 printf ("get <option>\n");
380 printf ("options:\n");
383 printf (" databaseName\n");
384 printf (" preferredRecordSyntax\n");
386 printf (" elementSetName\n");
387 printf (" maximumRecordSize\n");
388 printf (" preferredRecordSize\n");
390 printf (" piggyback\n");
394 printf (" implementationName\n");
395 printf (" charset\n");
399 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
400 ZOOM_options options,
404 const char *errmsg, *addinfo, *dset;
407 if (next_token_copy (args, host, sizeof(host)) < 0)
409 printf ("missing host after connect\n");
412 for (j = -1, i = 0; i<MAX_CON; i++)
415 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
418 ZOOM_connection_destroy (c[i]);
421 else if (c[i] == 0 && j == -1)
424 if (i == MAX_CON) /* no match .. */
428 printf ("no more connection available\n");
431 i = j; /* OK, use this one is available */
433 c[i] = ZOOM_connection_create (options);
434 ZOOM_connection_connect (c[i], host, 0);
436 if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
437 printf ("%s error: %s (%s:%d) %s\n",
438 ZOOM_connection_option_get(c[i], "host"), errmsg,
439 dset, error, addinfo);
442 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
443 ZOOM_options options,
449 cmd_len = next_token (buf, &cmd_str);
452 if (is_command ("quit", cmd_str, cmd_len))
454 else if (is_command ("set", cmd_str, cmd_len))
455 cmd_set (c, r, options, buf);
456 else if (is_command ("get", cmd_str, cmd_len))
457 cmd_get (c, r, options, buf);
458 else if (is_command ("connect", cmd_str, cmd_len))
459 cmd_connect (c, r, options, buf);
460 else if (is_command ("open", cmd_str, cmd_len))
461 cmd_connect (c, r, options, buf);
462 else if (is_command ("search", cmd_str, cmd_len))
463 cmd_search (c, r, options, buf);
464 else if (is_command ("find", cmd_str, cmd_len))
465 cmd_search (c, r, options, buf);
466 else if (is_command ("show", cmd_str, cmd_len))
467 cmd_show (c, r, options, buf);
468 else if (is_command ("close", cmd_str, cmd_len))
469 cmd_close (c, r, options, buf);
470 else if (is_command ("help", cmd_str, cmd_len))
471 cmd_help(c, r, options, buf);
472 else if (is_command ("ext", cmd_str, cmd_len))
473 cmd_ext(c, r, options, buf);
474 else if (is_command ("debug", cmd_str, cmd_len))
475 cmd_debug(c, r, options, buf);
476 else if (is_command ("scan", cmd_str, cmd_len))
477 cmd_scan(c, r, options, buf);
479 printf ("unknown command %.*s\n", cmd_len, cmd_str);
483 void shell(ZOOM_connection *c, ZOOM_resultset *r,
484 ZOOM_options options)
490 const char *bp = buf;
491 #if HAVE_READLINE_READLINE_H
493 line_in=readline("ZOOM>");
496 #if HAVE_READLINE_HISTORY_H
498 add_history(line_in);
500 if(strlen(line_in) > 999) {
501 printf("Input line too long\n");
507 printf ("ZOOM>"); fflush (stdout);
508 if (!fgets (buf, 999, stdin))
511 if ((cp = strchr(buf, '\n')))
513 if (!cmd_parse (c, r, options, &bp))
518 int main (int argc, char **argv)
520 ZOOM_options options = ZOOM_options_create();
522 ZOOM_connection z39_con[MAX_CON];
523 ZOOM_resultset z39_res[MAX_CON];
526 for (i = 0; i<MAX_CON; i++)
532 for (i = 0; i<MAX_CON; i++)
536 for (i = 1; i<argc; i++)
538 const char *bp = argv[i];
539 res = cmd_parse(z39_con, z39_res, options, &bp);
540 if (res == 0) /* received quit */
543 if (res) /* do cmdline shell only if not quitting */
544 shell(z39_con, z39_res, options);
545 ZOOM_options_destroy(options);
547 for (i = 0; i<MAX_CON; i++)
549 ZOOM_connection_destroy(z39_con[i]);
550 ZOOM_resultset_destroy(z39_res[i]);