zoomsh's shows 1 record by default
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 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, const char *type)
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, type, &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 (fwrite(render, 1, len, stdout) != (size_t) 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) != (size_t) 
219                                opac_len)
220                         printf("write to stdout failed\n");
221                 }
222             }
223         }
224     }
225 }
226
227 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
228                      ZOOM_options options,
229                      const char **args)
230 {
231     int i;
232     char start_str[10], count_str[10], render_str[60];
233     const char *type = "render";
234
235     if (next_token_copy(args, start_str, sizeof(start_str)) >= 0)
236         ZOOM_options_set(options, "start", start_str);
237
238     if (next_token_copy(args, count_str, sizeof(count_str)) <= 0)
239         strcpy(count_str, "1");
240     ZOOM_options_set(options, "count", count_str);
241
242     if (next_token_copy(args, render_str, sizeof(render_str)) >= 0)
243         type = render_str;
244
245     for (i = 0; i<MAX_CON; i++)
246         ZOOM_resultset_records(r[i], 0, atoi(start_str), atoi(count_str));
247     while (ZOOM_event(MAX_CON, c))
248         ;
249
250     for (i = 0; i<MAX_CON; i++)
251     {
252         int error;
253         const char *errmsg, *addinfo, *dset;
254         /* display errors if any */
255         if (!c[i])
256             continue;
257         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
258             printf("%s error: %s (%s:%d) %s\n",
259                    ZOOM_connection_option_get(c[i], "host"), errmsg,
260                    dset, error, addinfo);
261         else if (r[i])
262         {
263             /* OK, no major errors. Display records... */
264             int start = ZOOM_options_get_int(options, "start", 0);
265             int count = ZOOM_options_get_int(options, "count", 0);
266             display_records(c[i], r[i], start, count, type);
267         }
268     }
269     ZOOM_options_set(options, "count", "0");
270     ZOOM_options_set(options, "start", "0");
271 }
272
273 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
274                     ZOOM_options options,
275                     const char **args)
276 {
277     ZOOM_package p[MAX_CON];
278     char ext_type_str[10];
279     
280     int i;
281
282     if (next_token_copy(args, ext_type_str, sizeof(ext_type_str)) < 0)
283         return;
284     
285     for (i = 0; i<MAX_CON; i++)
286     {
287         if (c[i])
288         {
289             p[i] = ZOOM_connection_package(c[i], 0);
290             ZOOM_package_send(p[i], ext_type_str);
291         }
292         else
293             p[i] = 0;
294     }
295
296     while (ZOOM_event(MAX_CON, c))
297         ;
298
299     for (i = 0; i<MAX_CON; i++)
300     {
301         int error;
302         const char *errmsg, *addinfo, *dset;
303         /* display errors if any */
304         if (!p[i])
305             continue;
306         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
307             printf("%s error: %s (%s:%d) %s\n",
308                    ZOOM_connection_option_get(c[i], "host"), errmsg,
309                    dset, error, addinfo);
310         else if (p[i])
311         {
312             const char *v;
313             printf("ok\n");
314             v = ZOOM_package_option_get(p[i], "targetReference");
315             if (v)
316                 printf("targetReference: %s\n", v);
317             v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
318             if (v)
319                 printf("xmlUpdateDoc: %s\n", v);
320         }
321         ZOOM_package_destroy(p[i]);
322     }
323 }
324
325 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
326                       ZOOM_options options,
327                       const char **args)
328 {
329     yaz_log_init_level(YLOG_ALL);
330 }
331
332 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
333                        ZOOM_options options,
334                        const char **args)
335 {
336     ZOOM_query s;
337     const char *query_str = *args;
338     int i;
339     
340     s = ZOOM_query_create();
341     while (*query_str == ' ')
342         query_str++;
343     if (memcmp(query_str, "cql:", 4) == 0)
344     {
345         ZOOM_query_cql(s, query_str + 4);
346     }
347     else if (ZOOM_query_prefix(s, query_str))
348     {
349         printf("Bad PQF: %s\n", query_str);
350         return;
351     }
352     for (i = 0; i<MAX_CON; i++)
353     {
354
355         if (c[i])
356         {
357             ZOOM_resultset_destroy(r[i]);
358             r[i] = 0;
359         }
360         if (c[i])
361             r[i] = ZOOM_connection_search(c[i], s);
362     }
363     ZOOM_query_destroy(s);
364
365     while (ZOOM_event(MAX_CON, c))
366         ;
367
368     for (i = 0; i<MAX_CON; i++)
369     {
370         int error;
371         const char *errmsg, *addinfo, *dset;
372         /* display errors if any */
373         if (!c[i])
374             continue;
375         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
376             printf("%s error: %s (%s:%d) %s\n",
377                    ZOOM_connection_option_get(c[i], "host"), errmsg,
378                    dset, error, addinfo);
379         else if (r[i])
380         {
381             /* OK, no major errors. Look at the result count */
382             int start = ZOOM_options_get_int(options, "start", 0);
383             int count = ZOOM_options_get_int(options, "count", 0);
384
385             printf("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
386                    (long) ZOOM_resultset_size(r[i]));
387             /* and display */
388             display_records(c[i], r[i], start, count, "render");
389         }
390     }
391 }
392
393 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
394                      ZOOM_options options,
395                      const char **args)
396 {
397     const char *query_str = *args;
398     ZOOM_query query = ZOOM_query_create();
399     int i;
400     ZOOM_scanset s[MAX_CON];
401     
402     while (*query_str == ' ')
403         query_str++;
404
405     if (memcmp(query_str, "cql:", 4) == 0)
406     {
407         ZOOM_query_cql(query, query_str + 4);
408     }
409     else if (ZOOM_query_prefix(query, query_str))
410     {
411         printf("Bad PQF: %s\n", query_str);
412         return;
413     }
414
415     for (i = 0; i<MAX_CON; i++)
416     {
417         if (c[i])
418             s[i] = ZOOM_connection_scan1(c[i], query);
419         else
420             s[i] = 0;
421     }
422     ZOOM_query_destroy(query);
423
424     while (ZOOM_event(MAX_CON, c))
425         ;
426     for (i = 0; i<MAX_CON; i++)
427     {
428         int error;
429         const char *errmsg, *addinfo, *dset;
430         /* display errors if any */
431         if (!c[i])
432             continue;
433         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
434             printf("%s error: %s (%s:%d) %s\n",
435                    ZOOM_connection_option_get(c[i], "host"), errmsg,
436                    dset, error, addinfo);
437
438         if (s[i]) {
439             size_t p, sz = ZOOM_scanset_size(s[i]);
440             for (p = 0; p < sz; p++)
441             {
442                 int occ = 0;
443                 int len = 0;
444                 const char *term = ZOOM_scanset_display_term(s[i], p,
445                                                              &occ, &len);
446                 
447                 printf("%.*s %d\n", len, term, occ);
448             }            
449             ZOOM_scanset_destroy(s[i]);
450         }
451     }
452 }
453
454 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
455                      ZOOM_options options,
456                      const char **args)
457 {
458     const char *sort_spec = *args;
459     int i;
460     
461     while (*sort_spec == ' ')
462         sort_spec++;
463     
464     for (i = 0; i<MAX_CON; i++)
465     {
466         if (r[i])
467             ZOOM_resultset_sort(r[i], "yaz", sort_spec);
468     }
469     while (ZOOM_event(MAX_CON, c))
470         ;
471 }
472
473 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
474                      ZOOM_options options,
475                      const char **args)
476 {
477     printf("connect <zurl>\n");
478     printf("search <pqf>\n");
479     printf("show [<start> [<count> [<type]]]\n");
480     printf("scan <term>\n");
481     printf("quit\n");
482     printf("close <zurl>\n");
483     printf("ext <type>\n");
484     printf("set <option> [<value>]\n");
485     printf("get <option>\n");
486     printf("\n");
487     printf("options:\n");
488     printf(" start\n");
489     printf(" count\n");
490     printf(" databaseName\n");
491     printf(" preferredRecordSyntax\n");
492     printf(" proxy\n");
493     printf(" elementSetName\n");
494     printf(" maximumRecordSize\n");
495     printf(" preferredRecordSize\n");
496     printf(" async\n");
497     printf(" piggyback\n");
498     printf(" group\n");
499     printf(" user\n");
500     printf(" password\n");
501     printf(" implementationName\n");
502     printf(" charset\n");
503     printf(" lang\n");
504 }
505
506 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
507                         ZOOM_options options,
508                         const char **args)
509 {
510     int error;
511     const char *errmsg, *addinfo, *dset;
512     char host[60];
513     int j, i;
514     if (next_token_copy(args, host, sizeof(host)) < 0)
515     {
516         printf("missing host after connect\n");
517         return ;
518     }
519     for (j = -1, i = 0; i<MAX_CON; i++)
520     {
521         const char *h;
522         if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
523             !strcmp(h, host))
524         {
525             ZOOM_connection_destroy(c[i]);
526             break;
527         }
528         else if (c[i] == 0 && j == -1)
529             j = i;
530     }
531     if (i == MAX_CON)  /* no match .. */
532     {
533         if (j == -1)
534         {
535             printf("no more connection available\n");
536             return;
537         }
538         i = j;   /* OK, use this one is available */
539     }
540     c[i] = ZOOM_connection_create(options);
541     ZOOM_connection_connect(c[i], host, 0);
542         
543     if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
544         printf("%s error: %s (%s:%d) %s\n",
545                ZOOM_connection_option_get(c[i], "host"), errmsg,
546                dset, error, addinfo);
547 }
548
549 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
550                      ZOOM_options options, 
551                      const char **buf)
552 {
553     int cmd_len;
554     const char *cmd_str;
555
556     cmd_len = next_token(buf, &cmd_str);
557     if (cmd_len < 0)
558         return 1;
559     if (is_command("quit", cmd_str, cmd_len))
560         return 0;
561     else if (is_command("set", cmd_str, cmd_len))
562         cmd_set(c, r, options, buf);
563     else if (is_command("get", cmd_str, cmd_len))
564         cmd_get(c, r, options, buf);
565     else if (is_command("rget", cmd_str, cmd_len))
566         cmd_rget(c, r, options, buf);
567     else if (is_command("connect", cmd_str, cmd_len))
568         cmd_connect(c, r, options, buf);
569     else if (is_command("open", cmd_str, cmd_len))
570         cmd_connect(c, r, options, buf);
571     else if (is_command("search", cmd_str, cmd_len))
572         cmd_search(c, r, options, buf);
573     else if (is_command("find", cmd_str, cmd_len))
574         cmd_search(c, r, options, buf);
575     else if (is_command("show", cmd_str, cmd_len))
576         cmd_show(c, r, options, buf);
577     else if (is_command("close", cmd_str, cmd_len))
578         cmd_close(c, r, options, buf);
579     else if (is_command("help", cmd_str, cmd_len))
580         cmd_help(c, r, options, buf);
581     else if (is_command("ext", cmd_str, cmd_len))
582         cmd_ext(c, r, options, buf);
583     else if (is_command("debug", cmd_str, cmd_len))
584         cmd_debug(c, r, options, buf);
585     else if (is_command("scan", cmd_str, cmd_len))
586         cmd_scan(c, r, options, buf);
587     else if (is_command("sort", cmd_str, cmd_len))
588         cmd_sort(c, r, options, buf);
589     else
590         printf("unknown command %.*s\n", cmd_len, cmd_str);
591     return 2;
592 }
593
594 void shell(ZOOM_connection *c, ZOOM_resultset *r,
595            ZOOM_options options)
596 {
597     while (1)
598     {
599         char buf[1000];
600         char *cp;
601         const char *bp = buf;
602 #if HAVE_READLINE_READLINE_H
603         char* line_in;
604         line_in=readline("ZOOM>");
605         if (!line_in)
606             break;
607 #if HAVE_READLINE_HISTORY_H
608         if (*line_in)
609             add_history(line_in);
610 #endif
611         if(strlen(line_in) > 999) {
612             printf("Input line too long\n");
613             break;
614         };
615         strcpy(buf,line_in);
616         free(line_in);
617 #else    
618         printf("ZOOM>"); fflush(stdout);
619         if (!fgets(buf, 999, stdin))
620             break;
621 #endif 
622         if ((cp = strchr(buf, '\n')))
623             *cp = '\0';
624         if (!cmd_parse(c, r, options, &bp))
625             break;
626     }
627 }
628
629 static void zoomsh(int argc, char **argv)
630 {
631     ZOOM_options options = ZOOM_options_create();
632     int i, res;
633     ZOOM_connection z39_con[MAX_CON];
634     ZOOM_resultset  z39_res[MAX_CON];
635
636     for (i = 0; i<MAX_CON; i++)
637     {
638         z39_con[i] = 0;
639         z39_res[i] = 0;
640     }
641
642     for (i = 0; i<MAX_CON; i++)
643         z39_con[i] = 0;
644
645     res = 1;
646     for (i = 1; i<argc; i++)
647     {
648         const char *bp = argv[i];
649         res = cmd_parse(z39_con, z39_res, options, &bp);
650         if (res == 0)  /* received quit */
651             break;
652     }
653     if (res)  /* do cmdline shell only if not quitting */
654         shell(z39_con, z39_res, options);
655     ZOOM_options_destroy(options);
656
657     for (i = 0; i<MAX_CON; i++)
658     {
659         ZOOM_connection_destroy(z39_con[i]);
660         ZOOM_resultset_destroy(z39_res[i]);
661     }
662 }
663
664 int main(int argc, char **argv)
665 {
666     const char *maskstr = 0;
667     if (argc > 2 && !strcmp(argv[1], "-v"))
668     {
669         maskstr = argv[2];
670         argv += 2;
671         argc -= 2;
672     }
673     else if (argc > 1 && !strncmp(argv[1], "-v", 2))
674     {
675         maskstr = argv[1]+2;
676         argv++;
677         argc--;
678     }
679     if (maskstr)
680     {
681         int mask = yaz_log_mask_str(maskstr);
682         yaz_log_init_level(mask);
683     }
684     zoomsh(argc, argv);
685     exit(0);
686 }
687 /*
688  * Local variables:
689  * c-basic-offset: 4
690  * c-file-style: "Stroustrup"
691  * indent-tabs-mode: nil
692  * End:
693  * vim: shiftwidth=4 tabstop=8 expandtab
694  */
695