6a376c7c28a0250169a83b2bb8d44c2c9e629da3
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /** \file zoomsh.c
7     \brief ZOOM C command line tool (shell)
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 #include <yaz/comstack.h>
16
17 #if HAVE_READLINE_READLINE_H
18 #include <readline/readline.h> 
19 #endif
20 #if HAVE_READLINE_HISTORY_H
21 #include <readline/history.h>
22 #endif
23
24 #include <yaz/xmalloc.h>
25
26 #include <yaz/log.h>
27 #include <yaz/nmem.h>
28 #include <yaz/zoom.h>
29
30 #define MAX_CON 100
31
32 static int next_token(const char **cpp, const char **t_start)
33 {
34     int len = 0;
35     const char *cp = *cpp;
36     while (*cp == ' ')
37         cp++;
38     if (*cp == '"')
39     {
40         cp++;
41         *t_start = cp;
42         while (*cp && *cp != '"')
43         {
44             cp++;
45             len++;
46         }
47         if (*cp)
48             cp++;
49     }
50     else
51     {
52         *t_start = cp;
53         while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
54         {
55             cp++;
56             len++;
57         }
58         if (len == 0)
59             len = -1;
60     }
61     *cpp = cp;
62     return len;  /* return -1 if no token was read .. */
63 }
64
65 static int next_token_copy(const char **cpp, char *buf_out, int buf_max)
66 {
67     const char *start;
68     int len = next_token(cpp, &start);
69     if (len < 0)
70     {
71         *buf_out = 0;
72         return len;
73     }
74     if (len >= buf_max)
75         len = buf_max-1;
76     memcpy(buf_out, start, len);
77     buf_out[len] = '\0';
78     return len;
79 }
80
81 static int is_command(const char *cmd_str, const char *this_str, int this_len)
82 {
83     int cmd_len = strlen(cmd_str);
84     if (cmd_len != this_len)
85         return 0;
86     if (memcmp(cmd_str, this_str, cmd_len))
87         return 0;
88     return 1;
89 }
90
91 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
92                     ZOOM_options options,
93                     const char **args)
94 {
95     char key[40], val[80];
96
97     if (next_token_copy(args, key, sizeof(key)) < 0)
98     {
99         printf("missing argument for set\n");
100         return ;
101     }
102     if (next_token_copy(args, val, sizeof(val)) < 0)
103         ZOOM_options_set(options, key, 0);
104     else
105         ZOOM_options_set(options, key, val);
106 }
107
108 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
109                     ZOOM_options options,
110                     const char **args)
111 {
112     char key[40];
113     if (next_token_copy(args, key, sizeof(key)) < 0)
114     {
115         printf("missing argument for get\n");
116     }
117     else
118     {
119         const char *val = ZOOM_options_get(options, key);
120         printf("%s = %s\n", key, val ? val : "<null>");
121     }
122 }
123
124 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
125                      ZOOM_options options,
126                      const char **args)
127 {
128     char key[40];
129     if (next_token_copy(args, key, sizeof(key)) < 0)
130     {
131         printf("missing argument for get\n");
132     }
133     else
134     {
135         int i;
136         for (i = 0; i<MAX_CON; i++)
137         {
138             const char *val;
139             if (!r[i])
140                 continue;
141             
142             val = ZOOM_resultset_option_get(r[i], key);
143             printf("%s = %s\n", key, val ? val : "<null>");
144         }
145     }
146 }
147
148 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
149                       ZOOM_options options,
150                       const char **args)
151 {
152     char host[60];
153     int i;
154     next_token_copy(args, host, sizeof(host));
155     for (i = 0; i<MAX_CON; i++)
156     {
157         const char *h;
158         if (!c[i])
159             continue;
160         if ((h = ZOOM_connection_option_get(c[i], "host"))
161             && !strcmp(h, host))
162         {
163             ZOOM_connection_destroy(c[i]);
164             c[i] = 0;
165         }
166         else if (*host == '\0')
167         {
168             ZOOM_connection_destroy(c[i]);
169             c[i] = 0;
170         }
171     }
172 }
173
174 static void display_records(ZOOM_connection c,
175                             ZOOM_resultset r,
176                             int start, int count)
177 {
178     int i;
179     for (i = 0; i<count; i++)
180     {
181         int pos = i + start;
182         ZOOM_record rec = ZOOM_resultset_record(r, pos);
183         const char *db = ZOOM_record_get(rec, "database", 0);
184         
185         if (ZOOM_record_error(rec, 0, 0, 0))
186         {
187             const char *msg;
188             const char *addinfo;
189             const char *diagset;
190             int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
191             
192             printf("%d %s: %s (%s:%d) %s\n", pos, (db ? db : "unknown"),
193                    msg, diagset, error, addinfo ? addinfo : "none");
194         }
195         else
196         {
197             int len, opac_len;
198             const char *render = ZOOM_record_get(rec, "render", &len);
199             const char *opac_render = ZOOM_record_get(rec, "opac", &opac_len);
200             const char *syntax = ZOOM_record_get(rec, "syntax", 0);
201             const char *schema = ZOOM_record_get(rec, "schema", 0);
202             /* if rec is non-null, we got a record for display */
203             if (rec)
204             {
205                 printf("%d database=%s syntax=%s schema=%s\n",
206                        pos, (db ? db : "unknown"), syntax,
207                        schema ? schema : "unknown");
208                 if (render)
209                 {
210                     if (write(render, 1, len, stdout) != len)
211                     {
212                         printf("write to stdout failed\n");
213                     }
214                 }
215                 printf("\n");
216                 if (opac_render)
217                 {
218                     if (fwrite(opac_render, 1, opac_len, stdout) != opac_len)
219                         printf("write to stdout failed\n");
220                 }
221             }
222         }
223     }
224 }
225
226 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
227                      ZOOM_options options,
228                      const char **args)
229 {
230     int i;
231     char start_str[10], count_str[10];
232
233     if (next_token_copy(args, start_str, sizeof(start_str)) >= 0)
234         ZOOM_options_set(options, "start", start_str);
235
236     if (next_token_copy(args, count_str, sizeof(count_str)) >= 0)
237         ZOOM_options_set(options, "count", count_str);
238
239     for (i = 0; i<MAX_CON; i++)
240         ZOOM_resultset_records(r[i], 0, atoi(start_str), atoi(count_str));
241     while (ZOOM_event(MAX_CON, c))
242         ;
243
244     for (i = 0; i<MAX_CON; i++)
245     {
246         int error;
247         const char *errmsg, *addinfo, *dset;
248         /* display errors if any */
249         if (!c[i])
250             continue;
251         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
252             printf("%s error: %s (%s:%d) %s\n",
253                    ZOOM_connection_option_get(c[i], "host"), errmsg,
254                    dset, error, addinfo);
255         else if (r[i])
256         {
257             /* OK, no major errors. Display records... */
258             int start = ZOOM_options_get_int(options, "start", 0);
259             int count = ZOOM_options_get_int(options, "count", 0);
260             display_records(c[i], r[i], start, count);
261         }
262     }
263     ZOOM_options_set(options, "count", "0");
264     ZOOM_options_set(options, "start", "0");
265 }
266
267 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
268                     ZOOM_options options,
269                     const char **args)
270 {
271     ZOOM_package p[MAX_CON];
272     char ext_type_str[10];
273     
274     int i;
275
276     if (next_token_copy(args, ext_type_str, sizeof(ext_type_str)) < 0)
277         return;
278     
279     for (i = 0; i<MAX_CON; i++)
280     {
281         if (c[i])
282         {
283             p[i] = ZOOM_connection_package(c[i], 0);
284             ZOOM_package_send(p[i], ext_type_str);
285         }
286         else
287             p[i] = 0;
288     }
289
290     while (ZOOM_event(MAX_CON, c))
291         ;
292
293     for (i = 0; i<MAX_CON; i++)
294     {
295         int error;
296         const char *errmsg, *addinfo, *dset;
297         /* display errors if any */
298         if (!p[i])
299             continue;
300         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
301             printf("%s error: %s (%s:%d) %s\n",
302                    ZOOM_connection_option_get(c[i], "host"), errmsg,
303                    dset, error, addinfo);
304         else if (p[i])
305         {
306             const char *v;
307             printf("ok\n");
308             v = ZOOM_package_option_get(p[i], "targetReference");
309             if (v)
310                 printf("targetReference: %s\n", v);
311             v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
312             if (v)
313                 printf("xmlUpdateDoc: %s\n", v);
314         }
315         ZOOM_package_destroy(p[i]);
316     }
317 }
318
319 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
320                       ZOOM_options options,
321                       const char **args)
322 {
323     yaz_log_init_level(YLOG_ALL);
324 }
325
326 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
327                        ZOOM_options options,
328                        const char **args)
329 {
330     ZOOM_query s;
331     const char *query_str = *args;
332     int i;
333     
334     s = ZOOM_query_create();
335     while (*query_str == ' ')
336         query_str++;
337     if (memcmp(query_str, "cql:", 4) == 0)
338     {
339         ZOOM_query_cql(s, query_str + 4);
340     }
341     else if (ZOOM_query_prefix(s, query_str))
342     {
343         printf("Bad PQF: %s\n", query_str);
344         return;
345     }
346     for (i = 0; i<MAX_CON; i++)
347     {
348
349         if (c[i])
350         {
351             ZOOM_resultset_destroy(r[i]);
352             r[i] = 0;
353         }
354         if (c[i])
355             r[i] = ZOOM_connection_search(c[i], s);
356     }
357     ZOOM_query_destroy(s);
358
359     while (ZOOM_event(MAX_CON, c))
360         ;
361
362     for (i = 0; i<MAX_CON; i++)
363     {
364         int error;
365         const char *errmsg, *addinfo, *dset;
366         /* display errors if any */
367         if (!c[i])
368             continue;
369         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
370             printf("%s error: %s (%s:%d) %s\n",
371                    ZOOM_connection_option_get(c[i], "host"), errmsg,
372                    dset, error, addinfo);
373         else if (r[i])
374         {
375             /* OK, no major errors. Look at the result count */
376             int start = ZOOM_options_get_int(options, "start", 0);
377             int count = ZOOM_options_get_int(options, "count", 0);
378
379             printf("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
380                    (long) ZOOM_resultset_size(r[i]));
381             /* and display */
382             display_records(c[i], r[i], start, count);
383         }
384     }
385 }
386
387 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
388                      ZOOM_options options,
389                      const char **args)
390 {
391     const char *query_str = *args;
392     ZOOM_query query = ZOOM_query_create();
393     int i;
394     ZOOM_scanset s[MAX_CON];
395     
396     while (*query_str == ' ')
397         query_str++;
398
399     if (memcmp(query_str, "cql:", 4) == 0)
400     {
401         ZOOM_query_cql(query, query_str + 4);
402     }
403     else if (ZOOM_query_prefix(query, query_str))
404     {
405         printf("Bad PQF: %s\n", query_str);
406         return;
407     }
408
409     for (i = 0; i<MAX_CON; i++)
410     {
411         if (c[i])
412             s[i] = ZOOM_connection_scan1(c[i], query);
413         else
414             s[i] = 0;
415     }
416     ZOOM_query_destroy(query);
417
418     while (ZOOM_event(MAX_CON, c))
419         ;
420     for (i = 0; i<MAX_CON; i++)
421     {
422         int error;
423         const char *errmsg, *addinfo, *dset;
424         /* display errors if any */
425         if (!c[i])
426             continue;
427         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
428             printf("%s error: %s (%s:%d) %s\n",
429                    ZOOM_connection_option_get(c[i], "host"), errmsg,
430                    dset, error, addinfo);
431
432         if (s[i]) {
433             size_t p, sz = ZOOM_scanset_size(s[i]);
434             for (p = 0; p < sz; p++)
435             {
436                 int occ = 0;
437                 int len = 0;
438                 const char *term = ZOOM_scanset_display_term(s[i], p,
439                                                              &occ, &len);
440                 
441                 printf("%.*s %d\n", len, term, occ);
442             }            
443             ZOOM_scanset_destroy(s[i]);
444         }
445     }
446 }
447
448 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
449                      ZOOM_options options,
450                      const char **args)
451 {
452     const char *sort_spec = *args;
453     int i;
454     
455     while (*sort_spec == ' ')
456         sort_spec++;
457     
458     for (i = 0; i<MAX_CON; i++)
459     {
460         if (r[i])
461             ZOOM_resultset_sort(r[i], "yaz", sort_spec);
462     }
463     while (ZOOM_event(MAX_CON, c))
464         ;
465 }
466
467 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
468                      ZOOM_options options,
469                      const char **args)
470 {
471     printf("connect <zurl>\n");
472     printf("search <pqf>\n");
473     printf("show [<start> [<count>]\n");
474     printf("scan <term>\n");
475     printf("quit\n");
476     printf("close <zurl>\n");
477     printf("ext <type>\n");
478     printf("set <option> [<value>]\n");
479     printf("get <option>\n");
480     printf("\n");
481     printf("options:\n");
482     printf(" start\n");
483     printf(" count\n");
484     printf(" databaseName\n");
485     printf(" preferredRecordSyntax\n");
486     printf(" proxy\n");
487     printf(" elementSetName\n");
488     printf(" maximumRecordSize\n");
489     printf(" preferredRecordSize\n");
490     printf(" async\n");
491     printf(" piggyback\n");
492     printf(" group\n");
493     printf(" user\n");
494     printf(" password\n");
495     printf(" implementationName\n");
496     printf(" charset\n");
497     printf(" lang\n");
498 }
499
500 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
501                         ZOOM_options options,
502                         const char **args)
503 {
504     int error;
505     const char *errmsg, *addinfo, *dset;
506     char host[60];
507     int j, i;
508     if (next_token_copy(args, host, sizeof(host)) < 0)
509     {
510         printf("missing host after connect\n");
511         return ;
512     }
513     for (j = -1, i = 0; i<MAX_CON; i++)
514     {
515         const char *h;
516         if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
517             !strcmp(h, host))
518         {
519             ZOOM_connection_destroy(c[i]);
520             break;
521         }
522         else if (c[i] == 0 && j == -1)
523             j = i;
524     }
525     if (i == MAX_CON)  /* no match .. */
526     {
527         if (j == -1)
528         {
529             printf("no more connection available\n");
530             return;
531         }
532         i = j;   /* OK, use this one is available */
533     }
534     c[i] = ZOOM_connection_create(options);
535     ZOOM_connection_connect(c[i], host, 0);
536         
537     if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
538         printf("%s error: %s (%s:%d) %s\n",
539                ZOOM_connection_option_get(c[i], "host"), errmsg,
540                dset, error, addinfo);
541 }
542
543 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
544                      ZOOM_options options, 
545                      const char **buf)
546 {
547     int cmd_len;
548     const char *cmd_str;
549
550     cmd_len = next_token(buf, &cmd_str);
551     if (cmd_len < 0)
552         return 1;
553     if (is_command("quit", cmd_str, cmd_len))
554         return 0;
555     else if (is_command("set", cmd_str, cmd_len))
556         cmd_set(c, r, options, buf);
557     else if (is_command("get", cmd_str, cmd_len))
558         cmd_get(c, r, options, buf);
559     else if (is_command("rget", cmd_str, cmd_len))
560         cmd_rget(c, r, options, buf);
561     else if (is_command("connect", cmd_str, cmd_len))
562         cmd_connect(c, r, options, buf);
563     else if (is_command("open", cmd_str, cmd_len))
564         cmd_connect(c, r, options, buf);
565     else if (is_command("search", cmd_str, cmd_len))
566         cmd_search(c, r, options, buf);
567     else if (is_command("find", cmd_str, cmd_len))
568         cmd_search(c, r, options, buf);
569     else if (is_command("show", cmd_str, cmd_len))
570         cmd_show(c, r, options, buf);
571     else if (is_command("close", cmd_str, cmd_len))
572         cmd_close(c, r, options, buf);
573     else if (is_command("help", cmd_str, cmd_len))
574         cmd_help(c, r, options, buf);
575     else if (is_command("ext", cmd_str, cmd_len))
576         cmd_ext(c, r, options, buf);
577     else if (is_command("debug", cmd_str, cmd_len))
578         cmd_debug(c, r, options, buf);
579     else if (is_command("scan", cmd_str, cmd_len))
580         cmd_scan(c, r, options, buf);
581     else if (is_command("sort", cmd_str, cmd_len))
582         cmd_sort(c, r, options, buf);
583     else
584         printf("unknown command %.*s\n", cmd_len, cmd_str);
585     return 2;
586 }
587
588 void shell(ZOOM_connection *c, ZOOM_resultset *r,
589            ZOOM_options options)
590 {
591     while (1)
592     {
593         char buf[1000];
594         char *cp;
595         const char *bp = buf;
596 #if HAVE_READLINE_READLINE_H
597         char* line_in;
598         line_in=readline("ZOOM>");
599         if (!line_in)
600             break;
601 #if HAVE_READLINE_HISTORY_H
602         if (*line_in)
603             add_history(line_in);
604 #endif
605         if(strlen(line_in) > 999) {
606             printf("Input line too long\n");
607             break;
608         };
609         strcpy(buf,line_in);
610         free(line_in);
611 #else    
612         printf("ZOOM>"); fflush(stdout);
613         if (!fgets(buf, 999, stdin))
614             break;
615 #endif 
616         if ((cp = strchr(buf, '\n')))
617             *cp = '\0';
618         if (!cmd_parse(c, r, options, &bp))
619             break;
620     }
621 }
622
623 static void zoomsh(int argc, char **argv)
624 {
625     ZOOM_options options = ZOOM_options_create();
626     int i, res;
627     ZOOM_connection z39_con[MAX_CON];
628     ZOOM_resultset  z39_res[MAX_CON];
629
630     for (i = 0; i<MAX_CON; i++)
631     {
632         z39_con[i] = 0;
633         z39_res[i] = 0;
634     }
635
636     for (i = 0; i<MAX_CON; i++)
637         z39_con[i] = 0;
638
639     res = 1;
640     for (i = 1; i<argc; i++)
641     {
642         const char *bp = argv[i];
643         res = cmd_parse(z39_con, z39_res, options, &bp);
644         if (res == 0)  /* received quit */
645             break;
646     }
647     if (res)  /* do cmdline shell only if not quitting */
648         shell(z39_con, z39_res, options);
649     ZOOM_options_destroy(options);
650
651     for (i = 0; i<MAX_CON; i++)
652     {
653         ZOOM_connection_destroy(z39_con[i]);
654         ZOOM_resultset_destroy(z39_res[i]);
655     }
656 }
657
658 int main(int argc, char **argv)
659 {
660     const char *maskstr = 0;
661     if (argc > 2 && !strcmp(argv[1], "-v"))
662     {
663         maskstr = argv[2];
664         argv += 2;
665         argc -= 2;
666     }
667     else if (argc > 1 && !strncmp(argv[1], "-v", 2))
668     {
669         maskstr = argv[1]+2;
670         argv++;
671         argc--;
672     }
673     if (maskstr)
674     {
675         int mask = yaz_log_mask_str(maskstr);
676         yaz_log_init_level(mask);
677     }
678     zoomsh(argc, argv);
679     exit(0);
680 }
681 /*
682  * Local variables:
683  * c-basic-offset: 4
684  * indent-tabs-mode: nil
685  * End:
686  * vim: shiftwidth=4 tabstop=8 expandtab
687  */
688