f263768ed55b652564e4751290af48246a1c5d1c
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /*
2  * $Id: zoomsh.c,v 1.9 2002-05-18 09:52:37 oleg 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 (ZOOM_connection *c, ZOOM_resultset *r,
68                      ZOOM_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 = ZOOM_options_get(options, key);
81         printf ("%s = %s\n", key, val ? val : "<null>");
82     }
83     else
84         ZOOM_options_set(options, key, val);
85 }
86
87 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
88                        ZOOM_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 = ZOOM_connection_option_get(c[i], "host"))
100             && !strcmp (h, host))
101         {
102             ZOOM_connection_destroy (c[i]);
103             c[i] = 0;
104         }
105         else if (*host == '\0')
106         {
107             ZOOM_connection_destroy (c[i]);
108             c[i] = 0;
109         }
110     }
111 }
112
113 static void display_records (ZOOM_connection c,
114                              ZOOM_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         ZOOM_record rec = ZOOM_resultset_record (r, pos);
122         const char *db = ZOOM_record_get (rec, "database", 0);
123         int len;
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 */
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             printf ("\n");
133         }
134     }
135 }
136
137 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
138                       ZOOM_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         ZOOM_options_set (options, "start", start_str);
146
147     if (next_token_copy (args, count_str, sizeof(count_str)))
148         ZOOM_options_set (options, "count", count_str);
149
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))
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 = 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,
165                      error, addinfo);
166         else if (r[i])
167         {
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);
172         }
173     }
174 }
175
176 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
177                         ZOOM_options options,
178                         const char **args)
179 {
180     ZOOM_query s;
181     int i;
182     
183     s = ZOOM_query_create ();
184     if (ZOOM_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             ZOOM_resultset_destroy (r[i]);
194             r[i] = 0;
195         }
196         if (c[i])
197             r[i] = ZOOM_connection_search (c[i], s);
198     }
199
200     while (ZOOM_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 = 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,
213                      error, addinfo);
214         else if (r[i])
215         {
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);
219
220             printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
221                     ZOOM_resultset_size(r[i]));
222             /* and display */
223             display_records (c[i], r[i], start, count);
224         }
225     }
226     ZOOM_query_destroy (s);
227 }
228
229 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
230                       ZOOM_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     printf (" charset\n");
256     printf (" lang\n");
257 }
258
259 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
260                          ZOOM_options options,
261                          const char **args)
262 {
263     int error;
264     const char *errmsg, *addinfo;
265     char host[60];
266     int j, i;
267     if (!next_token_copy (args, host, sizeof(host)))
268     {
269         printf ("missing host after connect\n");
270         return ;
271     }
272     for (j = -1, i = 0; i<MAX_CON; i++)
273     {
274         const char *h;
275         if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
276             !strcmp (h, host))
277         {
278             ZOOM_connection_destroy (c[i]);
279             break;
280         }
281         else if (c[i] == 0 && j == -1)
282             j = i;
283     }
284     if (i == MAX_CON)  /* no match .. */
285     {
286         if (j == -1)
287         {
288             printf ("no more connection available\n");
289             return;
290         }
291         i = j;   /* OK, use this one is available */
292     }
293     c[i] = ZOOM_connection_create (options);
294     ZOOM_connection_connect (c[i], host, 0);
295
296     if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
297         printf ("%s error: %s (%d) %s\n",
298                 ZOOM_connection_option_get(c[i], "host"),
299                 errmsg, error, addinfo);
300     
301 }
302
303 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
304                       ZOOM_options options, 
305                       const char **buf)
306 {
307     int cmd_len;
308     const char *cmd_str;
309
310     cmd_len = next_token (buf, &cmd_str);
311     if (!cmd_len)
312         return 1;
313     if (is_command ("quit", cmd_str, cmd_len))
314         return 0;
315     else if (is_command ("set", cmd_str, cmd_len))
316         cmd_set (c, r, options, buf);
317     else if (is_command ("connect", cmd_str, cmd_len))
318         cmd_connect (c, r, options, buf);
319     else if (is_command ("search", cmd_str, cmd_len))
320         cmd_search (c, r, options, buf);
321     else if (is_command ("show", cmd_str, cmd_len))
322         cmd_show (c, r, options, buf);
323     else if (is_command ("close", cmd_str, cmd_len))
324         cmd_close (c, r, options, buf);
325     else if (is_command ("help", cmd_str, cmd_len))
326         cmd_help(c, r, options, buf);
327     else
328         printf ("unknown command %.*s\n", cmd_len, cmd_str);
329     return 2;
330 }
331
332 void shell(ZOOM_connection *c, ZOOM_resultset *r, ZOOM_options options)
333 {
334     while (1)
335     {
336         char buf[1000];
337         char *cp;
338         const char *bp = buf;
339 #if HAVE_READLINE_READLINE_H
340         char* line_in;
341         line_in=readline("ZOOM>");
342         if (!line_in)
343             break;
344 #if HAVE_READLINE_HISTORY_H
345         if (*line_in)
346             add_history(line_in);
347 #endif
348         if(strlen(line_in) > 999) {
349             fprintf(stderr,"Input line too long\n");
350             break;
351         };
352         strcpy(buf,line_in);
353         free (line_in);
354 #else    
355         printf ("ZOOM>"); fflush (stdout);
356         if (!fgets (buf, 999, stdin))
357             break;
358 #endif 
359         if ((cp = strchr(buf, '\n')))
360             *cp = '\0';
361         if (!cmd_parse (c, r, options, &bp))
362             break;
363     }
364 }
365
366 int main (int argc, char **argv)
367 {
368     ZOOM_options options = ZOOM_options_create();
369     int i, res;
370     ZOOM_connection z39_con[MAX_CON];
371     ZOOM_resultset  z39_res[MAX_CON];
372     for (i = 0; i<MAX_CON; i++)
373     {
374         z39_con[i] = 0;
375         z39_res[i] = 0;
376     }
377
378     for (i = 0; i<MAX_CON; i++)
379         z39_con[i] = 0;
380
381     res = 1;
382     for (i = 1; i<argc; i++)
383     {
384         const char *bp = argv[i];
385         res = cmd_parse(z39_con, z39_res, options, &bp);
386         if (res == 0)  /* received quit */
387             break;
388     }
389     if (res)  /* do cmdline shell only if not quitting */
390         shell(z39_con, z39_res, options);
391     ZOOM_options_destroy(options);
392
393     for (i = 0; i<MAX_CON; i++)
394     {
395         ZOOM_connection_destroy(z39_con[i]);
396         ZOOM_resultset_destroy(z39_res[i]);
397     }
398     exit (0);
399 }