270942b6de67d9d8c8fbbc79e403f182219d4469
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /*
2  * $Id: zoomsh.c,v 1.17 2003-02-24 13:14:49 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/log.h>
22 #include <yaz/zoom.h>
23
24 #define MAX_CON 100
25
26 static int next_token (const char **cpp, const char **t_start)
27 {
28     int len = 0;
29     const char *cp = *cpp;
30     while (*cp == ' ')
31         cp++;
32     *t_start = cp;
33     while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
34     {
35         cp++;
36         len++;
37     }
38     *cpp = cp;
39     return len;
40 }
41
42 static int next_token_copy (const char **cpp, char *buf_out, int buf_max)
43 {
44     const char *start;
45     int len = next_token (cpp, &start);
46     if (!len)
47     {
48         *buf_out = 0;
49         return 0;
50     }
51     if (len >= buf_max)
52         len = buf_max-1;
53     memcpy (buf_out, start, len);
54     buf_out[len] = '\0';
55     return len;
56 }
57
58 static int is_command (const char *cmd_str, const char *this_str, int this_len)
59 {
60     int cmd_len = strlen(cmd_str);
61     if (cmd_len != this_len)
62         return 0;
63     if (memcmp (cmd_str, this_str, cmd_len))
64         return 0;
65     return 1;
66 }
67
68 static void cmd_set (ZOOM_connection *c, ZOOM_resultset *r,
69                      ZOOM_options options,
70                      const char **args)
71 {
72     char key[40], val[80];
73
74     if (!next_token_copy (args, key, sizeof(key)))
75     {
76         printf ("missing argument for set\n");
77         return ;
78     }
79     if (!next_token_copy (args, val, sizeof(val)))
80         ZOOM_options_set(options, key, 0);
81     else
82         ZOOM_options_set(options, key, val);
83 }
84
85 static void cmd_get (ZOOM_connection *c, ZOOM_resultset *r,
86                      ZOOM_options options,
87                      const char **args)
88 {
89     char key[40];
90     if (!next_token_copy (args, key, sizeof(key)))
91     {
92         printf ("missing argument for get\n");
93     }
94     else
95     {
96         const char *val = ZOOM_options_get(options, key);
97         printf ("%s = %s\n", key, val ? val : "<null>");
98     }
99 }
100
101 static void cmd_close (ZOOM_connection *c, ZOOM_resultset *r,
102                        ZOOM_options options,
103                        const char **args)
104 {
105     char host[60];
106     int i;
107     next_token_copy (args, host, sizeof(host));
108     for (i = 0; i<MAX_CON; i++)
109     {
110         const char *h;
111         if (!c[i])
112             continue;
113         if ((h = ZOOM_connection_option_get(c[i], "host"))
114             && !strcmp (h, host))
115         {
116             ZOOM_connection_destroy (c[i]);
117             c[i] = 0;
118         }
119         else if (*host == '\0')
120         {
121             ZOOM_connection_destroy (c[i]);
122             c[i] = 0;
123         }
124     }
125 }
126
127 static void display_records (ZOOM_connection c,
128                              ZOOM_resultset r,
129                              int start, int count)
130 {
131     int i;
132     for (i = 0; i<count; i++)
133     {
134         int pos = i + start;
135         ZOOM_record rec = ZOOM_resultset_record (r, pos);
136         const char *db = ZOOM_record_get (rec, "database", 0);
137         int len;
138         const char *render = ZOOM_record_get (rec, "render", &len);
139         const char *syntax = ZOOM_record_get (rec, "syntax", 0);
140         /* if rec is non-null, we got a record for display */
141         if (rec)
142         {
143             printf ("%d %s %s\n", pos+1, (db ? db : "unknown"), syntax);
144             if (render)
145                 fwrite (render, 1, len, stdout);
146             printf ("\n");
147         }
148     }
149 }
150
151 static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
152                       ZOOM_options options,
153                       const char **args)
154 {
155     int i;
156     char start_str[10], count_str[10];
157
158     if (next_token_copy (args, start_str, sizeof(start_str)))
159         ZOOM_options_set (options, "start", start_str);
160
161     if (next_token_copy (args, count_str, sizeof(count_str)))
162         ZOOM_options_set (options, "count", count_str);
163
164     for (i = 0; i<MAX_CON; i++)
165         ZOOM_resultset_records (r[i], 0, atoi(start_str), atoi(count_str));
166     while (ZOOM_event (MAX_CON, c))
167         ;
168
169     for (i = 0; i<MAX_CON; i++)
170     {
171         int error;
172         const char *errmsg, *addinfo, *dset;
173         /* display errors if any */
174         if (!c[i])
175             continue;
176         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
177             printf ("%s error: %s (%s:%d) %s\n",
178                      ZOOM_connection_option_get(c[i], "host"), errmsg,
179                      dset, error, addinfo);
180         else if (r[i])
181         {
182             /* OK, no major errors. Display records... */
183             int start = ZOOM_options_get_int (options, "start", 0);
184             int count = ZOOM_options_get_int (options, "count", 0);
185             display_records (c[i], r[i], start, count);
186         }
187     }
188     ZOOM_options_set (options, "count", "0");
189     ZOOM_options_set (options, "start", "0");
190 }
191
192 static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
193                      ZOOM_options options,
194                      const char **args)
195 {
196     ZOOM_package p[MAX_CON];
197     
198     int i;
199     
200     for (i = 0; i<MAX_CON; i++)
201     {
202         if (c[i])
203         {
204             p[i] = ZOOM_connection_package (c[i], 0);
205             ZOOM_package_send(p[i], "itemorder");
206         }
207         else
208             p[i] = 0;
209     }
210
211     while (ZOOM_event (MAX_CON, c))
212         ;
213
214     for (i = 0; i<MAX_CON; i++)
215     {
216         int error;
217         const char *errmsg, *addinfo, *dset;
218         /* display errors if any */
219         if (!p[i])
220             continue;
221         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
222             printf ("%s error: %s (%s:%d) %s\n",
223                      ZOOM_connection_option_get(c[i], "host"), errmsg,
224                      dset, error, addinfo);
225         else if (p[i])
226         {
227             printf ("ok\n");
228         }
229         ZOOM_package_destroy (p[i]);
230     }
231 }
232
233 static void cmd_debug (ZOOM_connection *c, ZOOM_resultset *r,
234                        ZOOM_options options,
235                        const char **args)
236 {
237     yaz_log_init_level(LOG_ALL);
238 }
239
240 static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
241                         ZOOM_options options,
242                         const char **args)
243 {
244     ZOOM_query s;
245     const char *query_str = *args;
246     int i;
247     
248     s = ZOOM_query_create ();
249     while (*query_str == ' ')
250         query_str++;
251     if (memcmp(query_str, "cql:", 4) == 0)
252     {
253         ZOOM_query_cql (s, query_str + 4);
254     }
255     else if (ZOOM_query_prefix (s, query_str))
256     {
257         printf ("Bad PQF: %s\n", query_str);
258         return;
259     }
260     for (i = 0; i<MAX_CON; i++)
261     {
262         if (c[i])
263         {
264             ZOOM_resultset_destroy (r[i]);
265             r[i] = 0;
266         }
267         if (c[i])
268             r[i] = ZOOM_connection_search (c[i], s);
269     }
270
271     while (ZOOM_event (MAX_CON, c))
272         ;
273
274     for (i = 0; i<MAX_CON; i++)
275     {
276         int error;
277         const char *errmsg, *addinfo, *dset;
278         /* display errors if any */
279         if (!c[i])
280             continue;
281         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
282             printf ("%s error: %s (%s:%d) %s\n",
283                     ZOOM_connection_option_get(c[i], "host"), errmsg,
284                     dset, error, addinfo);
285         else if (r[i])
286         {
287             /* OK, no major errors. Look at the result count */
288             int start = ZOOM_options_get_int (options, "start", 0);
289             int count = ZOOM_options_get_int (options, "count", 0);
290
291             printf ("%s: %d hits\n", ZOOM_connection_option_get(c[i], "host"),
292                     ZOOM_resultset_size(r[i]));
293             /* and display */
294             display_records (c[i], r[i], start, count);
295         }
296     }
297     ZOOM_query_destroy (s);
298 }
299
300 static void cmd_scan (ZOOM_connection *c, ZOOM_resultset *r,
301                       ZOOM_options options,
302                       const char **args)
303 {
304     const char *start_term = *args;
305     int i;
306     ZOOM_scanset s[MAX_CON];
307     
308     while (*start_term == ' ')
309         start_term++;
310
311     for (i = 0; i<MAX_CON; i++)
312     {
313         if (c[i])
314             s[i] = ZOOM_connection_scan(c[i], start_term);
315         else
316             s[i] = 0;
317     }
318     while (ZOOM_event(MAX_CON, c))
319         ;
320     for (i = 0; i<MAX_CON; i++)
321     {
322         if (s[i]) {
323             size_t p, sz = ZOOM_scanset_size(s[i]);
324             for (p = 0; p < sz; p++)
325             {
326                 int  occ = 0;
327                 size_t len = 0;
328                 const char *term = ZOOM_scanset_term(s[i], p, &occ, &len);
329                 printf ("%.*s %d\n", len, term, occ);
330             }            
331             ZOOM_scanset_destroy(s[i]);
332         }
333     }
334 }
335
336 static void cmd_help (ZOOM_connection *c, ZOOM_resultset *r,
337                       ZOOM_options options,
338                       const char **args)
339 {
340     printf ("connect <zurl>\n");
341     printf ("search <pqf>\n");
342     printf ("show [<start> [<count>]\n");
343     printf ("scan <term>\n");
344     printf ("quit\n");
345     printf ("close <zurl>\n");
346     printf ("set <option> [<value>]\n");
347     printf ("get <option>\n");
348     printf ("\n");
349     printf ("options:\n");
350     printf (" start\n");
351     printf (" count\n");
352     printf (" databaseName\n");
353     printf (" preferredRecordSyntax\n");
354     printf (" proxy\n");
355     printf (" elementSetName\n");
356     printf (" maximumRecordSize\n");
357     printf (" preferredRecordSize\n");
358     printf (" async\n");
359     printf (" piggyback\n");
360     printf (" group\n");
361     printf (" user\n");
362     printf (" pass\n");
363     printf (" implementationName\n");
364     printf (" charset\n");
365     printf (" lang\n");
366 }
367
368 static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
369                          ZOOM_options options,
370                          const char **args)
371 {
372     int error;
373     const char *errmsg, *addinfo, *dset;
374     char host[60];
375     int j, i;
376     if (!next_token_copy (args, host, sizeof(host)))
377     {
378         printf ("missing host after connect\n");
379         return ;
380     }
381     for (j = -1, i = 0; i<MAX_CON; i++)
382     {
383         const char *h;
384         if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
385             !strcmp (h, host))
386         {
387             ZOOM_connection_destroy (c[i]);
388             break;
389         }
390         else if (c[i] == 0 && j == -1)
391             j = i;
392     }
393     if (i == MAX_CON)  /* no match .. */
394     {
395         if (j == -1)
396         {
397             printf ("no more connection available\n");
398             return;
399         }
400         i = j;   /* OK, use this one is available */
401     }
402     c[i] = ZOOM_connection_create (options);
403     ZOOM_connection_connect (c[i], host, 0);
404         
405     if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
406        printf ("%s error: %s (%s:%d) %s\n",
407             ZOOM_connection_option_get(c[i], "host"), errmsg,
408             dset, error, addinfo);
409 }
410
411 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
412                       ZOOM_options options, 
413                       const char **buf)
414 {
415     int cmd_len;
416     const char *cmd_str;
417
418     cmd_len = next_token (buf, &cmd_str);
419     if (!cmd_len)
420         return 1;
421     if (is_command ("quit", cmd_str, cmd_len))
422         return 0;
423     else if (is_command ("set", cmd_str, cmd_len))
424         cmd_set (c, r, options, buf);
425     else if (is_command ("get", cmd_str, cmd_len))
426         cmd_get (c, r, options, buf);
427     else if (is_command ("connect", cmd_str, cmd_len))
428         cmd_connect (c, r, options, buf);
429     else if (is_command ("open", cmd_str, cmd_len))
430         cmd_connect (c, r, options, buf);
431     else if (is_command ("search", cmd_str, cmd_len))
432         cmd_search (c, r, options, buf);
433     else if (is_command ("find", cmd_str, cmd_len))
434         cmd_search (c, r, options, buf);
435     else if (is_command ("show", cmd_str, cmd_len))
436         cmd_show (c, r, options, buf);
437     else if (is_command ("close", cmd_str, cmd_len))
438         cmd_close (c, r, options, buf);
439     else if (is_command ("help", cmd_str, cmd_len))
440         cmd_help(c, r, options, buf);
441     else if (is_command ("ext", cmd_str, cmd_len))
442         cmd_ext(c, r, options, buf);
443     else if (is_command ("debug", cmd_str, cmd_len))
444         cmd_debug(c, r, options, buf);
445     else if (is_command ("scan", cmd_str, cmd_len))
446         cmd_scan(c, r, options, buf);
447     else
448         printf ("unknown command %.*s\n", cmd_len, cmd_str);
449     return 2;
450 }
451
452 void shell(ZOOM_connection *c, ZOOM_resultset *r,
453            ZOOM_options options)
454 {
455     while (1)
456     {
457         char buf[1000];
458         char *cp;
459         const char *bp = buf;
460 #if HAVE_READLINE_READLINE_H
461         char* line_in;
462         line_in=readline("ZOOM>");
463         if (!line_in)
464             break;
465 #if HAVE_READLINE_HISTORY_H
466         if (*line_in)
467             add_history(line_in);
468 #endif
469         if(strlen(line_in) > 999) {
470             printf("Input line too long\n");
471             break;
472         };
473         strcpy(buf,line_in);
474         free (line_in);
475 #else    
476         printf ("ZOOM>"); fflush (stdout);
477         if (!fgets (buf, 999, stdin))
478             break;
479 #endif 
480         if ((cp = strchr(buf, '\n')))
481             *cp = '\0';
482         if (!cmd_parse (c, r, options, &bp))
483             break;
484     }
485 }
486
487 int main (int argc, char **argv)
488 {
489     ZOOM_options options = ZOOM_options_create();
490     int i, res;
491     ZOOM_connection z39_con[MAX_CON];
492     ZOOM_resultset  z39_res[MAX_CON];
493
494     nmem_init();
495     for (i = 0; i<MAX_CON; i++)
496     {
497         z39_con[i] = 0;
498         z39_res[i] = 0;
499     }
500
501     for (i = 0; i<MAX_CON; i++)
502         z39_con[i] = 0;
503
504     res = 1;
505     for (i = 1; i<argc; i++)
506     {
507         const char *bp = argv[i];
508         res = cmd_parse(z39_con, z39_res, options, &bp);
509         if (res == 0)  /* received quit */
510             break;
511     }
512     if (res)  /* do cmdline shell only if not quitting */
513         shell(z39_con, z39_res, options);
514     ZOOM_options_destroy(options);
515
516     for (i = 0; i<MAX_CON; i++)
517     {
518         ZOOM_connection_destroy(z39_con[i]);
519         ZOOM_resultset_destroy(z39_res[i]);
520     }
521     nmem_exit();
522     exit (0);
523 }