2 * $Id: zoomsh.c,v 1.4 2001-11-15 08:58:29 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 (Z3950_connection *c, Z3950_resultset *r,
68 Z3950_options options,
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)))
80 const char *val = Z3950_options_get(options, key);
81 printf ("%s = %s\n", key, val ? val : "<null>");
84 Z3950_options_set(options, key, val);
87 static void cmd_close (Z3950_connection *c, Z3950_resultset *r,
88 Z3950_options options,
93 next_token_copy (args, host, sizeof(host));
94 for (i = 0; i<MAX_CON; i++)
99 if ((h = Z3950_connection_host(c[i])) && !strcmp (h, host))
101 Z3950_connection_destroy (c[i]);
104 else if (*host == '\0')
106 Z3950_connection_destroy (c[i]);
112 static void display_records (Z3950_connection c,
114 int start, int count)
117 for (i = 0; i<count; i++)
120 Z3950_record rec = Z3950_resultset_record (r, pos);
121 const char *db = Z3950_record_get (rec, "database", 0);
123 const char *render = Z3950_record_get (rec, "render", &len);
124 const char *syntax = Z3950_record_get (rec, "syntax", 0);
125 /* if rec is non-null, we got a record for display */
128 printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
130 fwrite (render, 1, len, stdout);
136 static void cmd_show (Z3950_connection *c, Z3950_resultset *r,
137 Z3950_options options,
141 char start_str[10], count_str[10];
143 if (next_token_copy (args, start_str, sizeof(start_str)))
144 Z3950_options_set (options, "start", start_str);
146 if (next_token_copy (args, count_str, sizeof(count_str)))
147 Z3950_options_set (options, "count", count_str);
149 for (i = 0; i<MAX_CON; i++)
150 Z3950_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
151 while (Z3950_event (MAX_CON, c))
154 for (i = 0; i<MAX_CON; i++)
157 const char *errmsg, *addinfo;
158 /* display errors if any */
161 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
162 fprintf (stderr, "%s error: %s (%d) %s\n",
163 Z3950_connection_host(c[i]), errmsg,
167 /* OK, no major errors. Display records... */
168 int start = Z3950_options_get_int (options, "start", 0);
169 int count = Z3950_options_get_int (options, "count", 0);
170 display_records (c[i], r[i], start, count);
175 static void cmd_search (Z3950_connection *c, Z3950_resultset *r,
176 Z3950_options options,
182 s = Z3950_query_create ();
183 if (Z3950_query_prefix (s, *args))
185 fprintf (stderr, "Bad PQF: %s\n", *args);
188 for (i = 0; i<MAX_CON; i++)
192 Z3950_resultset_destroy (r[i]);
196 r[i] = Z3950_connection_search (c[i], s);
199 while (Z3950_event (MAX_CON, c))
202 for (i = 0; i<MAX_CON; i++)
205 const char *errmsg, *addinfo;
206 /* display errors if any */
209 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
210 fprintf (stderr, "%s error: %s (%d) %s\n",
211 Z3950_connection_host(c[i]), errmsg,
215 /* OK, no major errors. Look at the result count */
216 int start = Z3950_options_get_int (options, "start", 0);
217 int count = Z3950_options_get_int (options, "count", 0);
219 printf ("%s: %d hits\n", Z3950_connection_host(c[i]),
220 Z3950_resultset_size(r[i]));
222 display_records (c[i], r[i], start, count);
225 Z3950_query_destroy (s);
228 static void cmd_help (Z3950_connection *c, Z3950_resultset *r,
229 Z3950_options options,
232 printf ("connect <zurl>\n");
233 printf ("search <pqf>\n");
234 printf ("show [<start> [<count>]\n");
236 printf ("close <zurl>\n");
237 printf ("set <option> [<value>]]\n");
239 printf ("options:\n");
242 printf (" databaseName\n");
243 printf (" preferredRecordSyntax\n");
245 printf (" elementSetName\n");
246 printf (" maximumRecordSize\n");
247 printf (" preferredRecordSize\n");
249 printf (" piggyback\n");
253 printf (" implementationName\n");
256 static void cmd_connect (Z3950_connection *c, Z3950_resultset *r,
257 Z3950_options options,
261 const char *errmsg, *addinfo;
264 if (!next_token_copy (args, host, sizeof(host)))
266 printf ("missing host after connect\n");
269 for (j = -1, i = 0; i<MAX_CON; i++)
272 if (c[i] && (h = Z3950_connection_host(c[i])) &&
275 Z3950_connection_destroy (c[i]);
278 else if (c[i] == 0 && j == -1)
281 if (i == MAX_CON) /* no match .. */
285 printf ("no more connection available\n");
288 i = j; /* OK, use this one is available */
290 c[i] = Z3950_connection_create (options);
291 Z3950_connection_connect (c[i], host, 0);
293 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
294 printf ("%s error: %s (%d) %s\n", Z3950_connection_host(c[i]),
295 errmsg, error, addinfo);
299 static int cmd_parse (Z3950_connection *c, Z3950_resultset *r,
300 Z3950_options options,
306 cmd_len = next_token (buf, &cmd_str);
309 if (is_command ("quit", cmd_str, cmd_len))
311 else if (is_command ("set", cmd_str, cmd_len))
312 cmd_set (c, r, options, buf);
313 else if (is_command ("connect", cmd_str, cmd_len))
314 cmd_connect (c, r, options, buf);
315 else if (is_command ("search", cmd_str, cmd_len))
316 cmd_search (c, r, options, buf);
317 else if (is_command ("show", cmd_str, cmd_len))
318 cmd_show (c, r, options, buf);
319 else if (is_command ("close", cmd_str, cmd_len))
320 cmd_close (c, r, options, buf);
321 else if (is_command ("help", cmd_str, cmd_len))
322 cmd_help(c, r, options, buf);
324 printf ("unknown command %.*s\n", cmd_len, cmd_str);
328 void shell(Z3950_connection *c, Z3950_resultset *r, Z3950_options options)
333 const char *bp = buf;
334 #if HAVE_READLINE_READLINE_H
336 line_in=readline("ZOOM>");
339 #if HAVE_READLINE_HISTORY_H
341 add_history(line_in);
343 if(strlen(line_in) > 999) {
344 fprintf(stderr,"Input line too long\n");
350 printf ("ZOOM>"); fflush (stdout);
351 if (!fgets (buf, 999, stdin))
354 if (!cmd_parse (c, r, options, &bp))
359 int main (int argc, char **argv)
361 Z3950_options options = Z3950_options_create();
363 Z3950_connection z39_con[MAX_CON];
364 Z3950_resultset z39_res[MAX_CON];
365 for (i = 0; i<MAX_CON; i++)
371 for (i = 0; i<MAX_CON; i++)
375 for (i = 1; i<argc; i++)
377 const char *bp = argv[i];
378 res = cmd_parse(z39_con, z39_res, options, &bp);
379 if (res == 0) /* received quit */
382 if (res) /* do cmdline shell only if not quitting */
383 shell(z39_con, z39_res, options);
384 Z3950_options_destroy(options);
386 for (i = 0; i<MAX_CON; i++)
388 Z3950_connection_destroy(z39_con[i]);
389 Z3950_resultset_destroy(z39_res[i]);