69e319585b41cd8cb40d0dd183fbd1e2e69015a9
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /*
2  * $Id: zoomsh.c,v 1.4 2001-11-15 08:58:29 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_host(c[i])) && !strcmp (h, host))
100         {
101             Z3950_connection_destroy (c[i]);
102             c[i] = 0;
103         }
104         else if (*host == '\0')
105         {
106             Z3950_connection_destroy (c[i]);
107             c[i] = 0;
108         }
109     }
110 }
111
112 static void display_records (Z3950_connection c,
113                              Z3950_resultset r,
114                              int start, int count)
115 {
116     int i;
117     for (i = 0; i<count; i++)
118     {
119         int pos = i + start;
120         Z3950_record rec = Z3950_resultset_record (r, pos);
121         const char *db = Z3950_record_get (rec, "database", 0);
122         int len;
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 */
126         if (rec)
127         {
128             printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
129             if (render)
130                 fwrite (render, 1, len, stdout);
131             putchar ('\n');
132         }
133     }
134 }
135
136 static void cmd_show (Z3950_connection *c, Z3950_resultset *r,
137                       Z3950_options options,
138                       const char **args)
139 {
140     int i;
141     char start_str[10], count_str[10];
142
143     if (next_token_copy (args, start_str, sizeof(start_str)))
144         Z3950_options_set (options, "start", start_str);
145
146     if (next_token_copy (args, count_str, sizeof(count_str)))
147         Z3950_options_set (options, "count", count_str);
148
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))
152         ;
153
154     for (i = 0; i<MAX_CON; i++)
155     {
156         int error;
157         const char *errmsg, *addinfo;
158         /* display errors if any */
159         if (!c[i])
160             continue;
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,
164                      error, addinfo);
165         else if (r[i])
166         {
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);
171         }
172     }
173 }
174
175 static void cmd_search (Z3950_connection *c, Z3950_resultset *r,
176                         Z3950_options options,
177                         const char **args)
178 {
179     Z3950_query s;
180     int i;
181     
182     s = Z3950_query_create ();
183     if (Z3950_query_prefix (s, *args))
184     {
185         fprintf (stderr, "Bad PQF: %s\n", *args);
186         return;
187     }
188     for (i = 0; i<MAX_CON; i++)
189     {
190         if (c[i])
191         {
192             Z3950_resultset_destroy (r[i]);
193             r[i] = 0;
194         }
195         if (c[i])
196             r[i] = Z3950_connection_search (c[i], s);
197     }
198
199     while (Z3950_event (MAX_CON, c))
200         ;
201
202     for (i = 0; i<MAX_CON; i++)
203     {
204         int error;
205         const char *errmsg, *addinfo;
206         /* display errors if any */
207         if (!c[i])
208             continue;
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,
212                      error, addinfo);
213         else if (r[i])
214         {
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);
218
219             printf ("%s: %d hits\n", Z3950_connection_host(c[i]),
220                     Z3950_resultset_size(r[i]));
221             /* and display */
222             display_records (c[i], r[i], start, count);
223         }
224     }
225     Z3950_query_destroy (s);
226 }
227
228 static void cmd_help (Z3950_connection *c, Z3950_resultset *r,
229                       Z3950_options options,
230                       const char **args)
231 {
232     printf ("connect <zurl>\n");
233     printf ("search <pqf>\n");
234     printf ("show [<start> [<count>]\n");
235     printf ("quit\n");
236     printf ("close <zurl>\n");
237     printf ("set <option> [<value>]]\n");
238     printf ("\n");
239     printf ("options:\n");
240     printf (" start\n");
241     printf (" count\n");
242     printf (" databaseName\n");
243     printf (" preferredRecordSyntax\n");
244     printf (" proxy\n");
245     printf (" elementSetName\n");
246     printf (" maximumRecordSize\n");
247     printf (" preferredRecordSize\n");
248     printf (" async\n");
249     printf (" piggyback\n");
250     printf (" group\n");
251     printf (" user\n");
252     printf (" pass\n");
253     printf (" implementationName\n");
254 }
255
256 static void cmd_connect (Z3950_connection *c, Z3950_resultset *r,
257                          Z3950_options options,
258                          const char **args)
259 {
260     int error;
261     const char *errmsg, *addinfo;
262     char host[60];
263     int j, i;
264     if (!next_token_copy (args, host, sizeof(host)))
265     {
266         printf ("missing host after connect\n");
267         return ;
268     }
269     for (j = -1, i = 0; i<MAX_CON; i++)
270     {
271         const char *h;
272         if (c[i] && (h = Z3950_connection_host(c[i])) &&
273             !strcmp (h, host))
274         {
275             Z3950_connection_destroy (c[i]);
276             break;
277         }
278         else if (c[i] == 0 && j == -1)
279             j = i;
280     }
281     if (i == MAX_CON)  /* no match .. */
282     {
283         if (j == -1)
284         {
285             printf ("no more connection available\n");
286             return;
287         }
288         i = j;   /* OK, use this one is available */
289     }
290     c[i] = Z3950_connection_create (options);
291     Z3950_connection_connect (c[i], host, 0);
292
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);
296     
297 }
298
299 static int cmd_parse (Z3950_connection *c, Z3950_resultset *r,
300                       Z3950_options options, 
301                       const char **buf)
302 {
303     int cmd_len;
304     const char *cmd_str;
305
306     cmd_len = next_token (buf, &cmd_str);
307     if (!cmd_len)
308         return 1;
309     if (is_command ("quit", cmd_str, cmd_len))
310         return 0;
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);
323     else
324         printf ("unknown command %.*s\n", cmd_len, cmd_str);
325     return 2;
326 }
327
328 void shell(Z3950_connection *c, Z3950_resultset *r, Z3950_options options)
329 {
330     while (1)
331     {
332         char buf[1000];
333         const char *bp = buf;
334 #if HAVE_READLINE_READLINE_H
335         char* line_in;
336         line_in=readline("ZOOM>");
337         if (!line_in)
338             break;
339 #if HAVE_READLINE_HISTORY_H
340         if (*line_in)
341             add_history(line_in);
342 #endif
343         if(strlen(line_in) > 999) {
344             fprintf(stderr,"Input line too long\n");
345             break;
346         };
347         strcpy(buf,line_in);
348         free (line_in);
349 #else    
350         printf ("ZOOM>"); fflush (stdout);
351         if (!fgets (buf, 999, stdin))
352             break;
353 #endif 
354         if (!cmd_parse (c, r, options, &bp))
355             break;
356     }
357 }
358
359 int main (int argc, char **argv)
360 {
361     Z3950_options options = Z3950_options_create();
362     int i, res;
363     Z3950_connection z39_con[MAX_CON];
364     Z3950_resultset  z39_res[MAX_CON];
365     for (i = 0; i<MAX_CON; i++)
366     {
367         z39_con[i] = 0;
368         z39_res[i] = 0;
369     }
370
371     for (i = 0; i<MAX_CON; i++)
372         z39_con[i] = 0;
373
374     res = 1;
375     for (i = 1; i<argc; i++)
376     {
377         const char *bp = argv[i];
378         res = cmd_parse(z39_con, z39_res, options, &bp);
379         if (res == 0)  /* received quit */
380             break;
381     }
382     if (res)  /* do cmdline shell only if not quitting */
383         shell(z39_con, z39_res, options);
384     Z3950_options_destroy(options);
385
386     for (i = 0; i<MAX_CON; i++)
387     {
388         Z3950_connection_destroy(z39_con[i]);
389         Z3950_resultset_destroy(z39_res[i]);
390     }
391     exit (0);
392 }