2 * $Id: zoomsh.c,v 1.1 2001-10-23 21:00:20 adam Exp $
12 #define HAVE_READLINE_READLINE_H 1
13 #define HAVE_READLINE_HISTORY_H 1
15 #if HAVE_READLINE_READLINE_H
16 #include <readline/readline.h>
18 #if HAVE_READLINE_HISTORY_H
19 #include <readline/history.h>
22 #include <yaz/xmalloc.h>
28 static int next_token (const char **cpp, const char **t_start)
31 const char *cp = *cpp;
35 while (*cp && *cp != ' ')
44 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
47 int len = next_token (cpp, &start);
55 memcpy (buf_out, start, len);
60 static int is_command (const char *cmd_str, const char *this_str, int this_len)
62 int cmd_len = strlen(cmd_str);
63 if (cmd_len != this_len)
65 if (memcmp (cmd_str, this_str, cmd_len))
70 static void cmd_set (Z3950_connection *c, Z3950_resultset *r,
71 Z3950_options options,
74 char key[40], val[80];
76 if (!next_token_copy (args, key, sizeof(key)))
78 printf ("missing argument for set\n");
81 if (!next_token_copy (args, val, sizeof(val)))
83 const char *val = Z3950_options_get(options, key);
84 printf ("%s = %s\n", key, val ? val : "<null>");
87 Z3950_options_set(options, key, val);
90 static void cmd_close (Z3950_connection *c, Z3950_resultset *r,
91 Z3950_options options,
96 next_token_copy (args, host, sizeof(host));
97 for (i = 0; i<MAX_CON; i++)
102 if ((h = Z3950_connection_host(c[i])) && !strcmp (h, host))
104 Z3950_connection_destroy (c[i]);
107 else if (*host == '\0')
109 Z3950_connection_destroy (c[i]);
115 static void display_records (Z3950_connection c,
117 int start, int count)
120 for (i = 0; i<count; i++)
123 const char *db = Z3950_resultset_get (r, pos, "database", 0);
125 const char *rec = Z3950_resultset_get (r, pos, "render", &len);
126 const char *syntax = Z3950_resultset_get (r, pos, "syntax", 0);
127 /* if rec is non-null, we got a record for display */
130 printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
132 fwrite (rec, 1, len, stdout);
138 static void cmd_show (Z3950_connection *c, Z3950_resultset *r,
139 Z3950_options options,
143 char start_str[10], count_str[10];
145 if (next_token_copy (args, start_str, sizeof(start_str)))
146 Z3950_options_set (options, "start", start_str);
148 if (next_token_copy (args, count_str, sizeof(count_str)))
149 Z3950_options_set (options, "count", count_str);
151 for (i = 0; i<MAX_CON; i++)
152 Z3950_resultset_records (r[i], 0, 0);
153 while (Z3950_event (MAX_CON, c))
156 for (i = 0; i<MAX_CON; i++)
159 const char *errmsg, *addinfo;
160 /* display errors if any */
163 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
164 fprintf (stderr, "%s error: %s (%d) %s\n",
165 Z3950_connection_host(c[i]), errmsg,
169 /* OK, no major errors. Display records... */
170 int start = Z3950_options_get_int (options, "start", 0);
171 int count = Z3950_options_get_int (options, "count", 0);
172 display_records (c[i], r[i], start, count);
177 static void cmd_search (Z3950_connection *c, Z3950_resultset *r,
178 Z3950_options options,
184 s = Z3950_search_create ();
185 if (Z3950_search_prefix (s, *args))
187 fprintf (stderr, "Bad PQF: %s\n", *args);
190 for (i = 0; i<MAX_CON; i++)
194 Z3950_resultset_destroy (r[i]);
198 r[i] = Z3950_connection_search (c[i], s);
201 while (Z3950_event (MAX_CON, c))
204 for (i = 0; i<MAX_CON; i++)
207 const char *errmsg, *addinfo;
208 /* display errors if any */
211 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
212 fprintf (stderr, "%s error: %s (%d) %s\n",
213 Z3950_connection_host(c[i]), errmsg,
217 /* OK, no major errors. Look at the result count */
218 int start = Z3950_options_get_int (options, "start", 0);
219 int count = Z3950_options_get_int (options, "count", 0);
221 printf ("%s: %d hits\n", Z3950_connection_host(c[i]),
222 Z3950_resultset_size(r[i]));
224 display_records (c[i], r[i], start, count);
227 Z3950_search_destroy (s);
230 static void cmd_help (Z3950_connection *c, Z3950_resultset *r,
231 Z3950_options options,
234 printf ("connect <zurl>\n");
235 printf ("search <pqf>\n");
236 printf ("show [<start> [<count>]\n");
238 printf ("close <zurl>\n");
239 printf ("set <option> [<value>]]\n");
241 printf ("options:\n");
244 printf (" databaseName\n");
245 printf (" preferredRecordSyntax\n");
247 printf (" elementSetName\n");
248 printf (" maximumRecordSize\n");
249 printf (" preferredRecordSize\n");
251 printf (" piggyback\n");
255 printf (" implementationName\n");
258 static void cmd_connect (Z3950_connection *c, Z3950_resultset *r,
259 Z3950_options options,
263 const char *errmsg, *addinfo;
266 if (!next_token_copy (args, host, sizeof(host)))
268 printf ("missing host after connect\n");
271 for (j = -1, i = 0; i<MAX_CON; i++)
274 if (c[i] && (h = Z3950_connection_host(c[i])) &&
277 Z3950_connection_destroy (c[i]);
280 else if (c[i] == 0 && j == -1)
283 if (i == MAX_CON) /* no match .. */
287 printf ("no more connection available\n");
290 i = j; /* OK, use this one is available */
292 c[i] = Z3950_connection_create (options);
293 Z3950_connection_connect (c[i], host, 0);
295 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
296 printf ("%s error: %s (%d) %s\n", Z3950_connection_host(c[i]),
297 errmsg, error, addinfo);
301 static int cmd_parse (Z3950_connection *c, Z3950_resultset *r,
302 Z3950_options options,
308 cmd_len = next_token (buf, &cmd_str);
311 if (is_command ("quit", cmd_str, cmd_len))
313 else if (is_command ("set", cmd_str, cmd_len))
314 cmd_set (c, r, options, buf);
315 else if (is_command ("connect", cmd_str, cmd_len))
316 cmd_connect (c, r, options, buf);
317 else if (is_command ("search", cmd_str, cmd_len))
318 cmd_search (c, r, options, buf);
319 else if (is_command ("show", cmd_str, cmd_len))
320 cmd_show (c, r, options, buf);
321 else if (is_command ("close", cmd_str, cmd_len))
322 cmd_close (c, r, options, buf);
323 else if (is_command ("help", cmd_str, cmd_len))
324 cmd_help(c, r, options, buf);
326 printf ("unknown command %.*s\n", cmd_len, cmd_str);
330 void shell(Z3950_connection *c, Z3950_resultset *r, Z3950_options options)
335 const char *bp = buf;
336 #if HAVE_READLINE_READLINE_H
338 line_in=readline("ZOOM>");
341 #if HAVE_READLINE_HISTORY_H
343 add_history(line_in);
345 if(strlen(line_in) > 999) {
346 fprintf(stderr,"Input line too long\n");
352 printf ("ZOOM>"); fflush (stdout);
353 if (!fgets (buf, 999, stdin))
356 if (!cmd_parse (c, r, options, &bp))
361 int main (int argc, char **argv)
363 Z3950_options options = Z3950_options_create();
365 Z3950_connection z39_con[MAX_CON];
366 Z3950_resultset z39_res[MAX_CON];
367 for (i = 0; i<MAX_CON; i++)
373 for (i = 0; i<MAX_CON; i++)
377 for (i = 1; i<argc; i++)
379 const char *bp = argv[i];
380 res = cmd_parse(z39_con, z39_res, options, &bp);
381 if (res == 0) /* received quit */
384 if (res) /* do cmdline shell only if not quitting */
385 shell(z39_con, z39_res, options);
386 Z3950_options_destroy(options);
388 for (i = 0; i<MAX_CON; i++)
390 Z3950_connection_destroy(z39_con[i]);
391 Z3950_resultset_destroy(z39_res[i]);