2 * $Id: zoomsh.c,v 1.3 2001-11-06 17:05:19 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 const char *db = Z3950_resultset_get (r, pos, "database", 0);
122 const char *rec = Z3950_resultset_get (r, pos, "render", &len);
123 const char *syntax = Z3950_resultset_get (r, pos, "syntax", 0);
124 /* if rec is non-null, we got a record for display */
127 printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
129 fwrite (rec, 1, len, stdout);
135 static void cmd_show (Z3950_connection *c, Z3950_resultset *r,
136 Z3950_options options,
140 char start_str[10], count_str[10];
142 if (next_token_copy (args, start_str, sizeof(start_str)))
143 Z3950_options_set (options, "start", start_str);
145 if (next_token_copy (args, count_str, sizeof(count_str)))
146 Z3950_options_set (options, "count", count_str);
148 for (i = 0; i<MAX_CON; i++)
149 Z3950_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
150 while (Z3950_event (MAX_CON, c))
153 for (i = 0; i<MAX_CON; i++)
156 const char *errmsg, *addinfo;
157 /* display errors if any */
160 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
161 fprintf (stderr, "%s error: %s (%d) %s\n",
162 Z3950_connection_host(c[i]), errmsg,
166 /* OK, no major errors. Display records... */
167 int start = Z3950_options_get_int (options, "start", 0);
168 int count = Z3950_options_get_int (options, "count", 0);
169 display_records (c[i], r[i], start, count);
174 static void cmd_search (Z3950_connection *c, Z3950_resultset *r,
175 Z3950_options options,
181 s = Z3950_query_create ();
182 if (Z3950_query_prefix (s, *args))
184 fprintf (stderr, "Bad PQF: %s\n", *args);
187 for (i = 0; i<MAX_CON; i++)
191 Z3950_resultset_destroy (r[i]);
195 r[i] = Z3950_connection_search (c[i], s);
198 while (Z3950_event (MAX_CON, c))
201 for (i = 0; i<MAX_CON; i++)
204 const char *errmsg, *addinfo;
205 /* display errors if any */
208 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
209 fprintf (stderr, "%s error: %s (%d) %s\n",
210 Z3950_connection_host(c[i]), errmsg,
214 /* OK, no major errors. Look at the result count */
215 int start = Z3950_options_get_int (options, "start", 0);
216 int count = Z3950_options_get_int (options, "count", 0);
218 printf ("%s: %d hits\n", Z3950_connection_host(c[i]),
219 Z3950_resultset_size(r[i]));
221 display_records (c[i], r[i], start, count);
224 Z3950_query_destroy (s);
227 static void cmd_help (Z3950_connection *c, Z3950_resultset *r,
228 Z3950_options options,
231 printf ("connect <zurl>\n");
232 printf ("search <pqf>\n");
233 printf ("show [<start> [<count>]\n");
235 printf ("close <zurl>\n");
236 printf ("set <option> [<value>]]\n");
238 printf ("options:\n");
241 printf (" databaseName\n");
242 printf (" preferredRecordSyntax\n");
244 printf (" elementSetName\n");
245 printf (" maximumRecordSize\n");
246 printf (" preferredRecordSize\n");
248 printf (" piggyback\n");
252 printf (" implementationName\n");
255 static void cmd_connect (Z3950_connection *c, Z3950_resultset *r,
256 Z3950_options options,
260 const char *errmsg, *addinfo;
263 if (!next_token_copy (args, host, sizeof(host)))
265 printf ("missing host after connect\n");
268 for (j = -1, i = 0; i<MAX_CON; i++)
271 if (c[i] && (h = Z3950_connection_host(c[i])) &&
274 Z3950_connection_destroy (c[i]);
277 else if (c[i] == 0 && j == -1)
280 if (i == MAX_CON) /* no match .. */
284 printf ("no more connection available\n");
287 i = j; /* OK, use this one is available */
289 c[i] = Z3950_connection_create (options);
290 Z3950_connection_connect (c[i], host, 0);
292 if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
293 printf ("%s error: %s (%d) %s\n", Z3950_connection_host(c[i]),
294 errmsg, error, addinfo);
298 static int cmd_parse (Z3950_connection *c, Z3950_resultset *r,
299 Z3950_options options,
305 cmd_len = next_token (buf, &cmd_str);
308 if (is_command ("quit", cmd_str, cmd_len))
310 else if (is_command ("set", cmd_str, cmd_len))
311 cmd_set (c, r, options, buf);
312 else if (is_command ("connect", cmd_str, cmd_len))
313 cmd_connect (c, r, options, buf);
314 else if (is_command ("search", cmd_str, cmd_len))
315 cmd_search (c, r, options, buf);
316 else if (is_command ("show", cmd_str, cmd_len))
317 cmd_show (c, r, options, buf);
318 else if (is_command ("close", cmd_str, cmd_len))
319 cmd_close (c, r, options, buf);
320 else if (is_command ("help", cmd_str, cmd_len))
321 cmd_help(c, r, options, buf);
323 printf ("unknown command %.*s\n", cmd_len, cmd_str);
327 void shell(Z3950_connection *c, Z3950_resultset *r, Z3950_options options)
332 const char *bp = buf;
333 #if HAVE_READLINE_READLINE_H
335 line_in=readline("ZOOM>");
338 #if HAVE_READLINE_HISTORY_H
340 add_history(line_in);
342 if(strlen(line_in) > 999) {
343 fprintf(stderr,"Input line too long\n");
349 printf ("ZOOM>"); fflush (stdout);
350 if (!fgets (buf, 999, stdin))
353 if (!cmd_parse (c, r, options, &bp))
358 int main (int argc, char **argv)
360 Z3950_options options = Z3950_options_create();
362 Z3950_connection z39_con[MAX_CON];
363 Z3950_resultset z39_res[MAX_CON];
364 for (i = 0; i<MAX_CON; i++)
370 for (i = 0; i<MAX_CON; i++)
374 for (i = 1; i<argc; i++)
376 const char *bp = argv[i];
377 res = cmd_parse(z39_con, z39_res, options, &bp);
378 if (res == 0) /* received quit */
381 if (res) /* do cmdline shell only if not quitting */
382 shell(z39_con, z39_res, options);
383 Z3950_options_destroy(options);
385 for (i = 0; i<MAX_CON; i++)
387 Z3950_connection_destroy(z39_con[i]);
388 Z3950_resultset_destroy(z39_res[i]);