Removed Z3950_connection_host.
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /*
2  * $Id: zoomsh.c,v 1.5 2001-11-16 09:52:39 adam Exp $
3  *
4  * ZOOM-C Shell
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11
12 #if HAVE_READLINE_READLINE_H
13 #include <readline/readline.h> 
14 #endif
15 #if HAVE_READLINE_HISTORY_H
16 #include <readline/history.h>
17 #endif
18
19 #include <yaz/xmalloc.h>
20
21 #include <yaz/zoom.h>
22
23 #define MAX_CON 100
24
25 static int next_token (const char **cpp, const char **t_start)
26 {
27     int len = 0;
28     const char *cp = *cpp;
29     while (*cp == ' ')
30         cp++;
31     *t_start = cp;
32     while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
33     {
34         cp++;
35         len++;
36     }
37     *cpp = cp;
38     return len;
39 }
40
41 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
42 {
43     const char *start;
44     int len = next_token (cpp, &start);
45     if (!len)
46     {
47         *buf_out = 0;
48         return 0;
49     }
50     if (len >= buf_max)
51         len = buf_max-1;
52     memcpy (buf_out, start, len);
53     buf_out[len] = '\0';
54     return len;
55 }
56
57 static int is_command (const char *cmd_str, const char *this_str, int this_len)
58 {
59     int cmd_len = strlen(cmd_str);
60     if (cmd_len != this_len)
61         return 0;
62     if (memcmp (cmd_str, this_str, cmd_len))
63         return 0;
64     return 1;
65 }
66
67 static void cmd_set (Z3950_connection *c, Z3950_resultset *r,
68                      Z3950_options options,
69                      const char **args)
70 {
71     char key[40], val[80];
72
73     if (!next_token_copy (args, key, sizeof(key)))
74     {
75         printf ("missing argument for set\n");
76         return ;
77     }
78     if (!next_token_copy (args, val, sizeof(val)))
79     {
80         const char *val = Z3950_options_get(options, key);
81         printf ("%s = %s\n", key, val ? val : "<null>");
82     }
83     else
84         Z3950_options_set(options, key, val);
85 }
86
87 static void cmd_close (Z3950_connection *c, Z3950_resultset *r,
88                        Z3950_options options,
89                        const char **args)
90 {
91     char host[60];
92     int i;
93     next_token_copy (args, host, sizeof(host));
94     for (i = 0; i<MAX_CON; i++)
95     {
96         const char *h;
97         if (!c[i])
98             continue;
99         if ((h = Z3950_connection_option_get(c[i], "host"))
100             && !strcmp (h, host))
101         {
102             Z3950_connection_destroy (c[i]);
103             c[i] = 0;
104         }
105         else if (*host == '\0')
106         {
107             Z3950_connection_destroy (c[i]);
108             c[i] = 0;
109         }
110     }
111 }
112
113 static void display_records (Z3950_connection c,
114                              Z3950_resultset r,
115                              int start, int count)
116 {
117     int i;
118     for (i = 0; i<count; i++)
119     {
120         int pos = i + start;
121         Z3950_record rec = Z3950_resultset_record (r, pos);
122         const char *db = Z3950_record_get (rec, "database", 0);
123         int len;
124         const char *render = Z3950_record_get (rec, "render", &len);
125         const char *syntax = Z3950_record_get (rec, "syntax", 0);
126         /* if rec is non-null, we got a record for display */
127         if (rec)
128         {
129             printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
130             if (render)
131                 fwrite (render, 1, len, stdout);
132             putchar ('\n');
133         }
134     }
135 }
136
137 static void cmd_show (Z3950_connection *c, Z3950_resultset *r,
138                       Z3950_options options,
139                       const char **args)
140 {
141     int i;
142     char start_str[10], count_str[10];
143
144     if (next_token_copy (args, start_str, sizeof(start_str)))
145         Z3950_options_set (options, "start", start_str);
146
147     if (next_token_copy (args, count_str, sizeof(count_str)))
148         Z3950_options_set (options, "count", count_str);
149
150     for (i = 0; i<MAX_CON; i++)
151         Z3950_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
152     while (Z3950_event (MAX_CON, c))
153         ;
154
155     for (i = 0; i<MAX_CON; i++)
156     {
157         int error;
158         const char *errmsg, *addinfo;
159         /* display errors if any */
160         if (!c[i])
161             continue;
162         if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
163             fprintf (stderr, "%s error: %s (%d) %s\n",
164                      Z3950_connection_option_get(c[i], "host"), errmsg,
165                      error, addinfo);
166         else if (r[i])
167         {
168             /* OK, no major errors. Display records... */
169             int start = Z3950_options_get_int (options, "start", 0);
170             int count = Z3950_options_get_int (options, "count", 0);
171             display_records (c[i], r[i], start, count);
172         }
173     }
174 }
175
176 static void cmd_search (Z3950_connection *c, Z3950_resultset *r,
177                         Z3950_options options,
178                         const char **args)
179 {
180     Z3950_query s;
181     int i;
182     
183     s = Z3950_query_create ();
184     if (Z3950_query_prefix (s, *args))
185     {
186         fprintf (stderr, "Bad PQF: %s\n", *args);
187         return;
188     }
189     for (i = 0; i<MAX_CON; i++)
190     {
191         if (c[i])
192         {
193             Z3950_resultset_destroy (r[i]);
194             r[i] = 0;
195         }
196         if (c[i])
197             r[i] = Z3950_connection_search (c[i], s);
198     }
199
200     while (Z3950_event (MAX_CON, c))
201         ;
202
203     for (i = 0; i<MAX_CON; i++)
204     {
205         int error;
206         const char *errmsg, *addinfo;
207         /* display errors if any */
208         if (!c[i])
209             continue;
210         if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
211             fprintf (stderr, "%s error: %s (%d) %s\n",
212                      Z3950_connection_option_get(c[i], "host"), errmsg,
213                      error, addinfo);
214         else if (r[i])
215         {
216             /* OK, no major errors. Look at the result count */
217             int start = Z3950_options_get_int (options, "start", 0);
218             int count = Z3950_options_get_int (options, "count", 0);
219
220             printf ("%s: %d hits\n", Z3950_connection_option_get(c[i], "host"),
221                     Z3950_resultset_size(r[i]));
222             /* and display */
223             display_records (c[i], r[i], start, count);
224         }
225     }
226     Z3950_query_destroy (s);
227 }
228
229 static void cmd_help (Z3950_connection *c, Z3950_resultset *r,
230                       Z3950_options options,
231                       const char **args)
232 {
233     printf ("connect <zurl>\n");
234     printf ("search <pqf>\n");
235     printf ("show [<start> [<count>]\n");
236     printf ("quit\n");
237     printf ("close <zurl>\n");
238     printf ("set <option> [<value>]]\n");
239     printf ("\n");
240     printf ("options:\n");
241     printf (" start\n");
242     printf (" count\n");
243     printf (" databaseName\n");
244     printf (" preferredRecordSyntax\n");
245     printf (" proxy\n");
246     printf (" elementSetName\n");
247     printf (" maximumRecordSize\n");
248     printf (" preferredRecordSize\n");
249     printf (" async\n");
250     printf (" piggyback\n");
251     printf (" group\n");
252     printf (" user\n");
253     printf (" pass\n");
254     printf (" implementationName\n");
255 }
256
257 static void cmd_connect (Z3950_connection *c, Z3950_resultset *r,
258                          Z3950_options options,
259                          const char **args)
260 {
261     int error;
262     const char *errmsg, *addinfo;
263     char host[60];
264     int j, i;
265     if (!next_token_copy (args, host, sizeof(host)))
266     {
267         printf ("missing host after connect\n");
268         return ;
269     }
270     for (j = -1, i = 0; i<MAX_CON; i++)
271     {
272         const char *h;
273         if (c[i] && (h = Z3950_connection_option_get(c[i], "host")) &&
274             !strcmp (h, host))
275         {
276             Z3950_connection_destroy (c[i]);
277             break;
278         }
279         else if (c[i] == 0 && j == -1)
280             j = i;
281     }
282     if (i == MAX_CON)  /* no match .. */
283     {
284         if (j == -1)
285         {
286             printf ("no more connection available\n");
287             return;
288         }
289         i = j;   /* OK, use this one is available */
290     }
291     c[i] = Z3950_connection_create (options);
292     Z3950_connection_connect (c[i], host, 0);
293
294     if ((error = Z3950_connection_error(c[i], &errmsg, &addinfo)))
295         printf ("%s error: %s (%d) %s\n",
296                 Z3950_connection_option_get(c[i], "host"),
297                 errmsg, error, addinfo);
298     
299 }
300
301 static int cmd_parse (Z3950_connection *c, Z3950_resultset *r,
302                       Z3950_options options, 
303                       const char **buf)
304 {
305     int cmd_len;
306     const char *cmd_str;
307
308     cmd_len = next_token (buf, &cmd_str);
309     if (!cmd_len)
310         return 1;
311     if (is_command ("quit", cmd_str, cmd_len))
312         return 0;
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);
325     else
326         printf ("unknown command %.*s\n", cmd_len, cmd_str);
327     return 2;
328 }
329
330 void shell(Z3950_connection *c, Z3950_resultset *r, Z3950_options options)
331 {
332     while (1)
333     {
334         char buf[1000];
335         const char *bp = buf;
336 #if HAVE_READLINE_READLINE_H
337         char* line_in;
338         line_in=readline("ZOOM>");
339         if (!line_in)
340             break;
341 #if HAVE_READLINE_HISTORY_H
342         if (*line_in)
343             add_history(line_in);
344 #endif
345         if(strlen(line_in) > 999) {
346             fprintf(stderr,"Input line too long\n");
347             break;
348         };
349         strcpy(buf,line_in);
350         free (line_in);
351 #else    
352         printf ("ZOOM>"); fflush (stdout);
353         if (!fgets (buf, 999, stdin))
354             break;
355 #endif 
356         if (!cmd_parse (c, r, options, &bp))
357             break;
358     }
359 }
360
361 int main (int argc, char **argv)
362 {
363     Z3950_options options = Z3950_options_create();
364     int i, res;
365     Z3950_connection z39_con[MAX_CON];
366     Z3950_resultset  z39_res[MAX_CON];
367     for (i = 0; i<MAX_CON; i++)
368     {
369         z39_con[i] = 0;
370         z39_res[i] = 0;
371     }
372
373     for (i = 0; i<MAX_CON; i++)
374         z39_con[i] = 0;
375
376     res = 1;
377     for (i = 1; i<argc; i++)
378     {
379         const char *bp = argv[i];
380         res = cmd_parse(z39_con, z39_res, options, &bp);
381         if (res == 0)  /* received quit */
382             break;
383     }
384     if (res)  /* do cmdline shell only if not quitting */
385         shell(z39_con, z39_res, options);
386     Z3950_options_destroy(options);
387
388     for (i = 0; i<MAX_CON; i++)
389     {
390         Z3950_connection_destroy(z39_con[i]);
391         Z3950_resultset_destroy(z39_res[i]);
392     }
393     exit (0);
394 }