zoomsh: sortby command
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /** \file zoomsh.c
6     \brief ZOOM C command line tool (shell)
7 */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #if HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <yaz/wrbuf.h>
20 #include <yaz/log.h>
21 #include <yaz/options.h>
22
23 #if HAVE_READLINE_READLINE_H
24 #include <readline/readline.h>
25 #endif
26 #if HAVE_READLINE_HISTORY_H
27 #include <readline/history.h>
28 #endif
29
30 #include <yaz/log.h>
31 #include <yaz/zoom.h>
32
33 struct zoom_sh {
34     WRBUF strategy;
35     WRBUF criteria;
36     ZOOM_options options;
37     struct zoom_db *list;
38 };
39
40 struct zoom_db {
41     ZOOM_connection con;
42     ZOOM_resultset res;
43     struct zoom_db *next;
44 };
45
46 #define MAX_CON 100
47
48 static void process_events(struct zoom_sh *sh)
49 {
50     ZOOM_connection *c;
51     int i, number;
52     struct zoom_db *db;
53
54     for (number = 0, db = sh->list; db; db = db->next)
55         if (db->con)
56             number++;
57     c = xmalloc(sizeof(*c) * number);
58
59     for (i = 0, db = sh->list; db; db = db->next)
60         if (db->con)
61             c[i++] = db->con;
62
63     yaz_log(YLOG_DEBUG, "process_events");
64     while ((i = ZOOM_event(number, c)) != 0)
65     {
66         int peek = ZOOM_connection_peek_event(c[i-1]);
67         int event = ZOOM_connection_last_event(c[i-1]);
68         yaz_log(YLOG_DEBUG, "no = %d peek = %d event = %d %s", i-1,
69                 peek,
70                 event,
71                 ZOOM_get_event_str(event));
72     }
73     xfree(c);
74 }
75
76 static int next_token_chars(const char **cpp, const char **t_start,
77                             const char *tok_chars)
78 {
79     int len = 0;
80     const char *cp = *cpp;
81     while (*cp == ' ')
82         cp++;
83     if (*cp == '"')
84     {
85         cp++;
86         *t_start = cp;
87         while (*cp && *cp != '"')
88         {
89             cp++;
90             len++;
91         }
92         if (*cp)
93             cp++;
94     }
95     else
96     {
97         *t_start = cp;
98         while (*cp && !strchr(tok_chars, *cp))
99         {
100             cp++;
101             len++;
102         }
103         if (len == 0)
104             len = -1;
105     }
106     *cpp = cp;
107     return len;  /* return -1 if no token was read .. */
108 }
109
110 static int next_token(const char **cpp, const char **t_start)
111 {
112     return next_token_chars(cpp, t_start, "\r\n ");
113 }
114
115
116 static WRBUF next_token_new_wrbuf(const char **cpp)
117 {
118     WRBUF w = 0;
119     const char *start;
120     int len = next_token(cpp, &start);
121     if (len < 0)
122         return 0;
123     w = wrbuf_alloc();
124     if (len > 0)
125         wrbuf_write(w, start, len);
126     return w;
127 }
128
129 static int is_command(const char *cmd_str, const char *this_str, int this_len)
130 {
131     int cmd_len = strlen(cmd_str);
132     if (cmd_len != this_len)
133         return 0;
134     if (memcmp(cmd_str, this_str, cmd_len))
135         return 0;
136     return 1;
137 }
138
139 static int cmd_set(struct zoom_sh *sh, const char **args)
140 {
141     WRBUF key;
142     const char *val_buf;
143     int val_len;
144
145     if (!(key = next_token_new_wrbuf(args)))
146     {
147         printf("missing argument for set\n");
148         return 1;
149     }
150     val_len = next_token_chars(args, &val_buf, "");
151     if (val_len != -1)
152         ZOOM_options_setl(sh->options, wrbuf_cstr(key), val_buf, val_len);
153     else
154         ZOOM_options_set(sh->options, wrbuf_cstr(key), 0);
155     wrbuf_destroy(key);
156     return 0;
157 }
158
159 static int cmd_get(struct zoom_sh *sh, const char **args)
160 {
161     WRBUF key;
162     if (!(key = next_token_new_wrbuf(args)))
163     {
164         printf("missing argument for get\n");
165         return 1;
166     }
167     else
168     {
169         const char *val = ZOOM_options_get(sh->options, wrbuf_cstr(key));
170         printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
171         wrbuf_destroy(key);
172     }
173     return 0;
174 }
175
176 static int cmd_shell(struct zoom_sh *sh, const char **args)
177 {
178     int ret = system(*args);
179     if (ret)
180          printf("system command returned %d\n", ret);
181     return 0;
182 }
183
184 static int cmd_rget(struct zoom_sh *sh, const char **args)
185 {
186     WRBUF key;
187     if (!(key = next_token_new_wrbuf(args)))
188     {
189         printf("missing argument for get\n");
190         return 1;
191     }
192     else
193     {
194         struct zoom_db *db = sh->list;
195         for (; db; db = db->next)
196         {
197             if (db->res)
198             {
199                 const char *val =
200                     ZOOM_resultset_option_get(db->res, wrbuf_cstr(key));
201                 printf("%s = %s\n", wrbuf_cstr(key), val ? val : "<null>");
202             }
203         }
204         wrbuf_destroy(key);
205     }
206     return 0;
207 }
208
209 static int cmd_close(struct zoom_sh *sh, const char **args)
210 {
211     struct zoom_db **dbp;
212     WRBUF host;
213     host = next_token_new_wrbuf(args);
214
215     for (dbp = &sh->list; *dbp; )
216     {
217         const char *h;
218         struct zoom_db *db = *dbp;
219         if (!db->con || !host ||
220             ((h = ZOOM_connection_option_get(db->con, "host"))
221              && !strcmp(h, wrbuf_cstr(host))))
222         {
223             *dbp = (*dbp)->next;
224             ZOOM_connection_destroy(db->con);
225             ZOOM_resultset_destroy(db->res);
226             xfree(db);
227         }
228         else
229             dbp = &(*dbp)->next;
230     }
231     if (host)
232         wrbuf_destroy(host);
233     return 0;
234 }
235
236 static void display_records(ZOOM_connection c,
237                             ZOOM_resultset r,
238                             size_t start, size_t count, const char *type)
239 {
240     size_t i;
241     for (i = 0; i < count; i++)
242     {
243         size_t pos = i + start;
244         ZOOM_record rec = ZOOM_resultset_record(r, pos);
245         const char *db = ZOOM_record_get(rec, "database", 0);
246
247         if (ZOOM_record_error(rec, 0, 0, 0))
248         {
249             const char *msg;
250             const char *addinfo;
251             const char *diagset;
252             int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
253
254             printf("%lld %s: %s (%s:%d) %s\n", (long long) pos,
255                    (db ? db : "unknown"),
256                    msg, diagset, error, addinfo ? addinfo : "none");
257         }
258         else
259         {
260             int len;
261             const char *render = ZOOM_record_get(rec, type, &len);
262             const char *syntax = ZOOM_record_get(rec, "syntax", 0);
263             const char *schema = ZOOM_record_get(rec, "schema", 0);
264             /* if rec is non-null, we got a record for display */
265             if (rec)
266             {
267                 printf("%lld database=%s syntax=%s schema=%s\n",
268                        (long long) pos, (db ? db : "unknown"), syntax,
269                        schema ? schema : "unknown");
270                 if (render)
271                 {
272                     if (fwrite(render, 1, len, stdout) != (size_t) len)
273                     {
274                         printf("write to stdout failed\n");
275                     }
276                 }
277                 printf("\n");
278             }
279         }
280     }
281 }
282
283 static int cmd_show(struct zoom_sh *sh, const char **args)
284 {
285     size_t start = 0, count = 1;
286     const char *type = "render";
287     WRBUF render_str = 0;
288     int ret = 0;
289     struct zoom_db *db;
290
291     {
292         WRBUF tmp;
293
294         if ((tmp = next_token_new_wrbuf(args)))
295         {
296             start = atoi(wrbuf_cstr(tmp));
297             wrbuf_destroy(tmp);
298         }
299
300         if ((tmp = next_token_new_wrbuf(args)))
301         {
302             count = atoi(wrbuf_cstr(tmp));
303             wrbuf_destroy(tmp);
304         }
305         render_str = next_token_new_wrbuf(args);
306     }
307     if (render_str)
308         type = wrbuf_cstr(render_str);
309
310     for (db = sh->list; db; db = db->next)
311         if (db->res)
312             ZOOM_resultset_records(db->res, 0, start, count);
313     process_events(sh);
314
315     for (db = sh->list; db; db = db->next)
316     {
317         int error;
318         const char *errmsg, *addinfo, *dset;
319         /* display errors if any */
320         if (!db->con)
321             continue;
322         if ((error = ZOOM_connection_error_x(db->con, &errmsg, &addinfo, &dset)))
323         {
324             printf("%s error: %s (%s:%d) %s\n",
325                    ZOOM_connection_option_get(db->con, "host"), errmsg,
326                    dset, error, addinfo);
327             ret = 1;
328         }
329         else if (db->res)
330         {
331             /* OK, no major errors. Display records... */
332             display_records(db->con, db->res, start, count, type);
333         }
334     }
335     if (render_str)
336         wrbuf_destroy(render_str);
337     return ret;
338 }
339
340 static void display_facets(ZOOM_facet_field *facets, int count)
341 {
342     int i;
343     printf("Facets: \n");
344     for (i = 0; i < count; i++)
345     {
346         int j;
347         const char *facet_name = ZOOM_facet_field_name(facets[i]);
348         printf("  %s: \n", facet_name);
349         for (j = 0; j < ZOOM_facet_field_term_count(facets[i]); j++)
350         {
351             int freq = 0;
352             const char *term = ZOOM_facet_field_get_term(facets[i], j, &freq);
353             printf("    %s(%d) \n", term,  freq);
354         }
355     }
356 }
357
358 static int cmd_facets(struct zoom_sh *sh, const char **args)
359 {
360     struct zoom_db *db;
361     int ret = 0;
362
363     process_events(sh);
364
365     for (db = sh->list; db; db = db->next)
366     {
367         int error;
368         const char *errmsg, *addinfo, *dset;
369         /* display errors if any */
370         if (!db->con)
371             continue;
372         if ((error = ZOOM_connection_error_x(db->con, &errmsg, &addinfo,
373                                              &dset)))
374         {
375             printf("%s error: %s (%s:%d) %s\n",
376                    ZOOM_connection_option_get(db->con, "host"), errmsg,
377                    dset, error, addinfo);
378             ret = 1;
379         }
380         else if (db->res)
381         {
382             int num_facets = ZOOM_resultset_facets_size(db->res);
383             if (num_facets) {
384                 ZOOM_facet_field  *facets = ZOOM_resultset_facets(db->res);
385                 display_facets(facets, num_facets);
386             }
387         }
388     }
389     return ret;
390 }
391
392 static int cmd_suggestions(struct zoom_sh *sh, const char **args)
393 {
394     struct zoom_db *db;
395     int ret = 0;
396
397     process_events(sh);
398
399     for (db = sh->list; db; db = db->next)
400     {
401         int error;
402         const char *errmsg, *addinfo, *dset;
403         /* display errors if any */
404         if (!db->con)
405             continue;
406         if ((error = ZOOM_connection_error_x(db->con, &errmsg, &addinfo,
407                                              &dset)))
408         {
409             printf("%s error: %s (%s:%d) %s\n",
410                    ZOOM_connection_option_get(db->con, "host"), errmsg,
411                    dset, error, addinfo);
412             ret = 1;
413         }
414         else if (db->res)
415         {
416             const char *suggestions =
417                 ZOOM_resultset_option_get(db->res, "suggestions");
418             if (suggestions)
419                 printf("Suggestions: \n%s\n", suggestions);
420         }
421     }
422     return ret;
423 }
424
425 static int cmd_ext(struct zoom_sh *sh, const char **args)
426 {
427     int ret = 0;
428     ZOOM_package *p = 0;
429     struct zoom_db *db;
430     int i, number;
431     WRBUF ext_type_str = next_token_new_wrbuf(args);
432
433     if (!ext_type_str)
434     {
435         printf("es: missing type "
436                "(itemorder, create, drop, commit, update, xmlupdate)\n");
437         return 1;
438     }
439     for (number = 0, db = sh->list; db; db = db->next)
440         if (db->con)
441             number++;
442
443     p = xmalloc(sizeof(*p) * number);
444
445     for (i = 0, db = sh->list; db; db = db->next)
446         if (db->con)
447         {
448             p[i] = ZOOM_connection_package(db->con, 0);
449             ZOOM_package_send(p[i], ext_type_str ? wrbuf_cstr(ext_type_str):0);
450             i++;
451         }
452
453     process_events(sh);
454
455     for (i = 0, db = sh->list; db; db = db->next)
456     {
457         int error;
458         const char *errmsg, *addinfo, *dset;
459         /* display errors if any */
460         if (!db->con)
461             continue;
462         if ((error = ZOOM_connection_error_x(db->con, &errmsg, &addinfo,
463                                              &dset)))
464         {
465             printf("%s error: %s (%s:%d) %s\n",
466                    ZOOM_connection_option_get(db->con, "host"), errmsg,
467                    dset, error, addinfo);
468             ret = 1;
469         }
470         else if (p[i])
471         {
472             const char *v;
473             printf("ok\n");
474             v = ZOOM_package_option_get(p[i], "targetReference");
475             if (v)
476                 printf("targetReference: %s\n", v);
477             v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
478             if (v)
479                 printf("xmlUpdateDoc: %s\n", v);
480         }
481         ZOOM_package_destroy(p[i]);
482         i++;
483     }
484     if (ext_type_str)
485         wrbuf_destroy(ext_type_str);
486     xfree(p);
487     return ret;
488 }
489
490 static int cmd_debug(struct zoom_sh *sh, const char **args)
491 {
492     yaz_log_init_level(YLOG_ALL);
493     return 0;
494 }
495
496 static int cmd_search(struct zoom_sh *sh, const char **args)
497 {
498     ZOOM_query s;
499     const char *query_str = *args;
500     int ret = 0;
501     struct zoom_db *db;
502
503     s = ZOOM_query_create();
504     while (*query_str == ' ')
505         query_str++;
506     if (memcmp(query_str, "cql:", 4) == 0)
507     {
508         ZOOM_query_cql(s, query_str + 4);
509     }
510     else if (ZOOM_query_prefix(s, query_str))
511     {
512         printf("Bad PQF: %s\n", query_str);
513         ZOOM_query_destroy(s);
514         return 1;
515     }
516     if (sh->strategy && wrbuf_len(sh->strategy) && wrbuf_len(sh->criteria))
517     {
518         int r = ZOOM_query_sortby2(s, wrbuf_cstr(sh->strategy),
519                                    wrbuf_cstr(sh->criteria));
520         if (r)
521         {
522             if (r == -1)
523                 printf("Bad sortby strategy: %s\n", wrbuf_cstr(sh->strategy));
524             else
525                 printf("Bad sortby criteria: %s\n", wrbuf_cstr(sh->criteria));
526             ZOOM_query_destroy(s);
527             return 1;
528         }
529         printf("sortby added\n");
530     }
531     for (db = sh->list; db; db = db->next)
532     {
533         if (db->con)
534         {
535             ZOOM_resultset_destroy(db->res);
536             db->res = ZOOM_connection_search(db->con, s);
537         }
538     }
539     ZOOM_query_destroy(s);
540
541     process_events(sh);
542
543     for (db = sh->list; db; db = db->next)
544     {
545         int error;
546         const char *errmsg, *addinfo, *dset;
547         /* display errors if any */
548         if (!db->con)
549             continue;
550         if ((error = ZOOM_connection_error_x(db->con, &errmsg, &addinfo,
551                                              &dset)))
552         {
553             printf("%s error: %s (%s:%d) %s\n",
554                    ZOOM_connection_option_get(db->con, "host"), errmsg,
555                    dset, error, addinfo);
556             ret = 1;
557         }
558         else if (db->res)
559         {
560             /* OK, no major errors. Look at the result count */
561             int start = ZOOM_options_get_int(sh->options, "start", 0);
562             int count = ZOOM_options_get_int(sh->options, "count", 0);
563             int facet_num;
564
565             printf("%s: %lld hits\n", ZOOM_connection_option_get(db->con,
566                                                                  "host"),
567                    (long long int) ZOOM_resultset_size(db->res));
568
569             facet_num = ZOOM_resultset_facets_size(db->res);
570             if (facet_num)
571             {
572                 ZOOM_facet_field *facets = ZOOM_resultset_facets(db->res);
573                 int facet_idx;
574                 for (facet_idx = 0; facet_idx < facet_num; facet_idx++)
575                 {
576                     const char *name = ZOOM_facet_field_name(facets[facet_idx]);
577                     size_t term_idx;
578                     size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
579                     printf("facet: %s\n", name);
580                     for (term_idx = 0; term_idx < term_num; term_idx++ )
581                     {
582                         int freq;
583                         const char *term =
584                             ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
585                         printf("term: %s %d\n", term, freq);
586                     }
587                 }
588             }
589             /* and display */
590             display_records(db->con, db->res, start, count, "render");
591         }
592     }
593     return ret;
594 }
595
596 static int cmd_scan(struct zoom_sh *sh, const char **args)
597 {
598     const char *query_str = *args;
599     ZOOM_query query = ZOOM_query_create();
600     int i, number;
601     int ret = 0;
602     ZOOM_scanset *s = 0;
603     struct zoom_db *db;
604
605     while (*query_str == ' ')
606         query_str++;
607
608     if (memcmp(query_str, "cql:", 4) == 0)
609     {
610         ZOOM_query_cql(query, query_str + 4);
611     }
612     else if (ZOOM_query_prefix(query, query_str))
613     {
614         printf("Bad PQF: %s\n", query_str);
615         ZOOM_query_destroy(query);
616         return 1;
617     }
618
619     for (number = 0, db = sh->list; db; db = db->next)
620         if (db->con)
621             number++;
622
623     s = xmalloc(sizeof(*s) * number);
624
625     for (i = 0, db = sh->list; db; db = db->next)
626         if (db->con)
627             s[i++] = ZOOM_connection_scan1(db->con, query);
628
629     ZOOM_query_destroy(query);
630
631     process_events(sh);
632
633     for (i = 0, db = sh->list; db; db = db->next)
634     {
635         int error;
636         const char *errmsg, *addinfo, *dset;
637         /* display errors if any */
638         if (!db->con)
639             continue;
640         if ((error = ZOOM_connection_error_x(db->con, &errmsg, &addinfo,
641                                              &dset)))
642         {
643             printf("%s error: %s (%s:%d) %s\n",
644                    ZOOM_connection_option_get(db->con, "host"), errmsg,
645                    dset, error, addinfo);
646             ret = 1;
647         }
648         if (s[i])
649         {
650             size_t p, sz = ZOOM_scanset_size(s[i]);
651             for (p = 0; p < sz; p++)
652             {
653                 size_t occ = 0;
654                 size_t len = 0;
655                 const char *term = ZOOM_scanset_display_term(s[i], p,
656                                                              &occ, &len);
657                 printf("%.*s %lld\n", (int) len, term, (long long int) occ);
658             }
659             ZOOM_scanset_destroy(s[i]);
660         }
661         i++;
662     }
663     xfree(s);
664     return ret;
665 }
666
667 static int cmd_sortby(struct zoom_sh *sh, const char **args)
668 {
669     WRBUF strategy;
670     const char *criteria;
671     if (!(strategy = next_token_new_wrbuf(args)))
672     {
673         printf("missing argument argument: strategy and criteria\n");
674         return 1;
675     }
676     criteria = *args;
677     while (*criteria == ' ')
678         criteria++;
679     wrbuf_destroy(sh->strategy);
680     sh->strategy = strategy;
681
682     wrbuf_rewind(sh->criteria);
683     wrbuf_puts(sh->criteria, criteria);
684     return 0;
685 }
686
687 static int cmd_sort(struct zoom_sh *sh, const char **args)
688 {
689     const char *sort_spec = *args;
690     int ret = 0;
691     struct zoom_db *db;
692
693     while (*sort_spec == ' ')
694         sort_spec++;
695
696     for (db = sh->list; db; db = db->next)
697         if (db->res)
698             ZOOM_resultset_sort(db->res, "yaz", sort_spec);
699     process_events(sh);
700     return ret;
701 }
702
703 static int cmd_help(struct zoom_sh *sh, const char **args)
704 {
705     printf("connect <zurl>\n");
706     printf("search <pqf>\n");
707     printf("sortby <strategy> <criteria>\n");
708     printf("show [<start> [<count> [<type]]]\n");
709     printf("facets\n");
710     printf("scan <term>\n");
711     printf("quit\n");
712     printf("close <zurl>\n");
713     printf("ext <type>\n");
714     printf("set <option> [<value>]\n");
715     printf("get <option>\n");
716     printf("shell cmdline\n");
717     printf("\n");
718     printf("options:\n");
719     printf(" start\n");
720     printf(" count\n");
721     printf(" databaseName\n");
722     printf(" preferredRecordSyntax\n");
723     printf(" proxy\n");
724     printf(" elementSetName\n");
725     printf(" maximumRecordSize\n");
726     printf(" preferredRecordSize\n");
727     printf(" async\n");
728     printf(" piggyback\n");
729     printf(" group\n");
730     printf(" user\n");
731     printf(" password\n");
732     printf(" implementationName\n");
733     printf(" charset\n");
734     printf(" lang\n");
735     printf(" timeout\n");
736     printf(" facets\n");
737     printf(" extraArgs\n");
738     printf(" suggestions\n");
739     return 0;
740 }
741
742 static int cmd_connect(struct zoom_sh *sh, const char **args)
743 {
744     int ret = 0;
745     int error;
746     const char *errmsg, *addinfo, *dset;
747     struct zoom_db *db;
748     WRBUF host = next_token_new_wrbuf(args);
749     if (!host)
750     {
751         printf("missing host after connect\n");
752         return 1;
753     }
754     for (db = sh->list; db; db = db->next)
755     {
756         const char *h;
757         if (db->con && (h = ZOOM_connection_option_get(db->con, "host")) &&
758             !strcmp(h, wrbuf_cstr(host)))
759         {
760             ZOOM_connection_destroy(db->con);
761             break;
762         }
763     }
764     if (!db)  /* no match .. */
765     {
766         db = xmalloc(sizeof(*db));
767         db->res = 0;
768         db->next = sh->list;
769         sh->list = db;
770     }
771     db->con = ZOOM_connection_create(sh->options);
772
773     ZOOM_connection_connect(db->con, wrbuf_cstr(host), 0);
774
775     process_events(sh);
776
777     if ((error = ZOOM_connection_error_x(db->con, &errmsg, &addinfo, &dset)))
778     {
779         printf("%s error: %s (%s:%d) %s\n",
780                ZOOM_connection_option_get(db->con, "host"), errmsg,
781                dset, error, addinfo);
782         ret = 1;
783     }
784     wrbuf_destroy(host);
785     return ret;
786 }
787
788 /** \brief parse and execute zoomsh command
789     \param sh ZOOM shell
790     \param buf command string and arguments
791     \retval 0 OK
792     \retval 1 failure to execute
793     \retval -1 EOF (no more commands or quit seen)
794 */
795 static int cmd_parse(struct zoom_sh *sh, const char **buf)
796 {
797     int cmd_len;
798     const char *cmd_str;
799     int ret = 0;
800
801     cmd_len = next_token(buf, &cmd_str);
802     if (cmd_len < 0)
803         return 0;
804     if (is_command("quit", cmd_str, cmd_len))
805         return -1;
806     else if (is_command("set", cmd_str, cmd_len))
807         ret = cmd_set(sh, buf);
808     else if (is_command("get", cmd_str, cmd_len))
809         ret = cmd_get(sh, buf);
810     else if (is_command("rget", cmd_str, cmd_len))
811         ret = cmd_rget(sh, buf);
812     else if (is_command("connect", cmd_str, cmd_len))
813         ret = cmd_connect(sh, buf);
814     else if (is_command("open", cmd_str, cmd_len))
815         ret = cmd_connect(sh, buf);
816     else if (is_command("search", cmd_str, cmd_len))
817         ret = cmd_search(sh, buf);
818     else if (is_command("sortby", cmd_str, cmd_len))
819         ret = cmd_sortby(sh, buf);
820     else if (is_command("facets", cmd_str, cmd_len))
821         ret = cmd_facets(sh, buf);
822     else if (is_command("find", cmd_str, cmd_len))
823         ret = cmd_search(sh, buf);
824     else if (is_command("show", cmd_str, cmd_len))
825         ret = cmd_show(sh, buf);
826     else if (is_command("suggestions", cmd_str, cmd_len))
827         ret = cmd_suggestions(sh, buf);
828     else if (is_command("close", cmd_str, cmd_len))
829         ret = cmd_close(sh, buf);
830     else if (is_command("help", cmd_str, cmd_len))
831         ret = cmd_help(sh, buf);
832     else if (is_command("ext", cmd_str, cmd_len))
833         ret = cmd_ext(sh, buf);
834     else if (is_command("debug", cmd_str, cmd_len))
835         ret = cmd_debug(sh, buf);
836     else if (is_command("scan", cmd_str, cmd_len))
837         ret = cmd_scan(sh, buf);
838     else if (is_command("sort", cmd_str, cmd_len))
839         ret = cmd_sort(sh, buf);
840     else if (is_command("shell", cmd_str, cmd_len))
841         ret = cmd_shell(sh, buf);
842     else
843     {
844         printf("unknown command %.*s\n", cmd_len, cmd_str);
845         ret = 1;
846     }
847     return ret;
848 }
849
850 static int shell(struct zoom_sh *sh, int exit_on_error)
851 {
852     int res = 0;
853     while (res == 0)
854     {
855         char buf[100000];
856         char *cp;
857         const char *bp = buf;
858         char *line_in = 0;
859 #if HAVE_READLINE_READLINE_H
860         if (isatty(0))
861         {
862             line_in = readline("ZOOM>");
863             if (!line_in)
864             {
865                 putchar('\n');
866                 res = -1;
867                 break;
868             }
869 #if HAVE_READLINE_HISTORY_H
870             if (*line_in)
871                 add_history(line_in);
872 #endif
873             if (strlen(line_in) > sizeof(buf)-1)
874             {
875                 printf("Input line too long\n");
876                 res = 1;
877                 break;
878             }
879             strcpy(buf,line_in);
880             free(line_in);
881         }
882 #endif
883         if (!line_in) /* no line buffer via readline or not enabled at all */
884         {
885             printf("ZOOM>"); fflush(stdout);
886             if (!fgets(buf, sizeof(buf)-1, stdin))
887             {
888                 res = -1;
889                 break;
890             }
891         }
892         if ((cp = strchr(buf, '\n')))
893             *cp = '\0';
894         res = cmd_parse(sh, &bp);
895         if (res == -1)
896             break;
897         if (!exit_on_error && res > 0)
898             res = 0;
899     }
900     return res;
901 }
902
903 static int zoomsh(int argc, char **argv)
904 {
905     int res = 0; /* -1: EOF; 0 = OK, > 0 ERROR */
906     int exit_on_error = 0;
907     struct zoom_db *db;
908     struct zoom_sh sh;
909
910     sh.list = 0;
911     sh.options = ZOOM_options_create();
912     sh.strategy = 0;
913     sh.criteria = wrbuf_alloc();
914
915     while (res == 0)
916     {
917         int mask;
918         char *arg = 0;
919         int option_ret = options("ev:", argv, argc, &arg);
920         const char *bp = arg;
921         switch (option_ret)
922         {
923         case 0:
924             res = cmd_parse(&sh, &bp);
925             /* returns res == -1 on quit */
926             if (!exit_on_error && res > 0)
927                 res = 0;  /* hide error */
928             break;
929         case YAZ_OPTIONS_EOF:
930             res = shell(&sh, exit_on_error);
931             break;
932         case 'e':
933             exit_on_error = 1;
934             break;
935         case 'v':
936             mask = yaz_log_mask_str(arg);
937             yaz_log_init_level(mask);
938             break;
939         default:
940             fprintf(stderr, "zoomsh: [-e] [-v] [commands]\n");
941             res = 1;
942         }
943     }
944
945     for (db = sh.list; db; )
946     {
947         struct zoom_db *n = db->next;
948         ZOOM_connection_destroy(db->con);
949         ZOOM_resultset_destroy(db->res);
950         xfree(db);
951         db = n;
952     }
953     ZOOM_options_destroy(sh.options);
954     wrbuf_destroy(sh.strategy);
955     wrbuf_destroy(sh.criteria);
956     if (res == -1) /* quit .. which is not an error */
957         res = 0;
958     return res;
959 }
960
961 int main(int argc, char **argv)
962 {
963     int ret = zoomsh(argc, argv);
964     exit(ret);
965 }
966 /*
967  * Local variables:
968  * c-basic-offset: 4
969  * c-file-style: "Stroustrup"
970  * indent-tabs-mode: nil
971  * End:
972  * vim: shiftwidth=4 tabstop=8 expandtab
973  */
974