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