2 * $Id: zoomsh.c,v 1.8 2002-02-20 14:40:42 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 (ZOOM_connection *c, ZOOM_resultset *r,
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 = ZOOM_options_get(options, key);
81 printf ("%s = %s\n", key, val ? val : "<null>");
84 ZOOM_options_set(options, key, val);
87 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
93 next_token_copy (args, host, sizeof(host));
94 for (i = 0; i<MAX_CON; i++)
99 if ((h = ZOOM_connection_option_get(c[i], "host"))
100 && !strcmp (h, host))
102 ZOOM_connection_destroy (c[i]);
105 else if (*host == '\0')
107 ZOOM_connection_destroy (c[i]);
113 static void display_records (ZOOM_connection c,
115 int start, int count)
118 for (i = 0; i<count; i++)
121 ZOOM_record rec = ZOOM_resultset_record (r, pos);
122 const char *db = ZOOM_record_get (rec, "database", 0);
124 const char *render = ZOOM_record_get (rec, "render", &len);
125 const char *syntax = ZOOM_record_get (rec, "syntax", 0);
126 /* if rec is non-null, we got a record for display */
129 printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
131 fwrite (render, 1, len, stdout);
137 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
138 ZOOM_options options,
142 char start_str[10], count_str[10];
144 if (next_token_copy (args, start_str, sizeof(start_str)))
145 ZOOM_options_set (options, "start", start_str);
147 if (next_token_copy (args, count_str, sizeof(count_str)))
148 ZOOM_options_set (options, "count", count_str);
150 for (i = 0; i<MAX_CON; i++)
151 ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
152 while (ZOOM_event (MAX_CON, c))
155 for (i = 0; i<MAX_CON; i++)
158 const char *errmsg, *addinfo;
159 /* display errors if any */
162 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
163 fprintf (stderr, "%s error: %s (%d) %s\n",
164 ZOOM_connection_option_get(c[i], "host"), errmsg,
168 /* OK, no major errors. Display records... */
169 int start = ZOOM_options_get_int (options, "start", 0);
170 int count = ZOOM_options_get_int (options, "count", 0);
171 display_records (c[i], r[i], start, count);
176 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
177 ZOOM_options options,
183 s = ZOOM_query_create ();
184 if (ZOOM_query_prefix (s, *args))
186 fprintf (stderr, "Bad PQF: %s\n", *args);
189 for (i = 0; i<MAX_CON; i++)
193 ZOOM_resultset_destroy (r[i]);
197 r[i] = ZOOM_connection_search (c[i], s);
200 while (ZOOM_event (MAX_CON, c))
203 for (i = 0; i<MAX_CON; i++)
206 const char *errmsg, *addinfo;
207 /* display errors if any */
210 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
211 fprintf (stderr, "%s error: %s (%d) %s\n",
212 ZOOM_connection_option_get(c[i], "host"), errmsg,
216 /* OK, no major errors. Look at the result count */
217 int start = ZOOM_options_get_int (options, "start", 0);
218 int count = ZOOM_options_get_int (options, "count", 0);
220 printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
221 ZOOM_resultset_size(r[i]));
223 display_records (c[i], r[i], start, count);
226 ZOOM_query_destroy (s);
229 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
230 ZOOM_options options,
233 printf ("connect <zurl>\n");
234 printf ("search <pqf>\n");
235 printf ("show [<start> [<count>]\n");
237 printf ("close <zurl>\n");
238 printf ("set <option> [<value>]]\n");
240 printf ("options:\n");
243 printf (" databaseName\n");
244 printf (" preferredRecordSyntax\n");
246 printf (" elementSetName\n");
247 printf (" maximumRecordSize\n");
248 printf (" preferredRecordSize\n");
250 printf (" piggyback\n");
254 printf (" implementationName\n");
257 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
258 ZOOM_options options,
262 const char *errmsg, *addinfo;
265 if (!next_token_copy (args, host, sizeof(host)))
267 printf ("missing host after connect\n");
270 for (j = -1, i = 0; i<MAX_CON; i++)
273 if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
276 ZOOM_connection_destroy (c[i]);
279 else if (c[i] == 0 && j == -1)
282 if (i == MAX_CON) /* no match .. */
286 printf ("no more connection available\n");
289 i = j; /* OK, use this one is available */
291 c[i] = ZOOM_connection_create (options);
292 ZOOM_connection_connect (c[i], host, 0);
294 if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
295 printf ("%s error: %s (%d) %s\n",
296 ZOOM_connection_option_get(c[i], "host"),
297 errmsg, error, addinfo);
301 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
302 ZOOM_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(ZOOM_connection *c, ZOOM_resultset *r, ZOOM_options options)
336 const char *bp = buf;
337 #if HAVE_READLINE_READLINE_H
339 line_in=readline("ZOOM>");
342 #if HAVE_READLINE_HISTORY_H
344 add_history(line_in);
346 if(strlen(line_in) > 999) {
347 fprintf(stderr,"Input line too long\n");
353 printf ("ZOOM>"); fflush (stdout);
354 if (!fgets (buf, 999, stdin))
357 if ((cp = strchr(buf, '\n')))
359 if (!cmd_parse (c, r, options, &bp))
364 int main (int argc, char **argv)
366 ZOOM_options options = ZOOM_options_create();
368 ZOOM_connection z39_con[MAX_CON];
369 ZOOM_resultset z39_res[MAX_CON];
370 for (i = 0; i<MAX_CON; i++)
376 for (i = 0; i<MAX_CON; i++)
380 for (i = 1; i<argc; i++)
382 const char *bp = argv[i];
383 res = cmd_parse(z39_con, z39_res, options, &bp);
384 if (res == 0) /* received quit */
387 if (res) /* do cmdline shell only if not quitting */
388 shell(z39_con, z39_res, options);
389 ZOOM_options_destroy(options);
391 for (i = 0; i<MAX_CON; i++)
393 ZOOM_connection_destroy(z39_con[i]);
394 ZOOM_resultset_destroy(z39_res[i]);