Refactor zoomsh to use YAZ options
[yaz-moved-to-github.git] / zoom / zoomsh.c
index 466633e..c72eaa2 100644 (file)
@@ -1,17 +1,20 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2010 Index Data
+ * Copyright (C) 1995-2011 Index Data
  * See the file LICENSE for details.
  */
-
 /** \file zoomsh.c
     \brief ZOOM C command line tool (shell)
 */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 #include <yaz/wrbuf.h>
+#include <yaz/log.h>
+#include <yaz/options.h>
 
 #if HAVE_READLINE_READLINE_H
 #include <readline/readline.h> 
 
 #define MAX_CON 100
 
-static int next_token(const char **cpp, const char **t_start)
+static void process_events(ZOOM_connection *c)
+{
+    int i;
+
+    yaz_log(YLOG_DEBUG, "process_events");
+    while ((i = ZOOM_event(MAX_CON, c)) != 0)
+    {
+        int peek = ZOOM_connection_peek_event(c[i-1]);
+        int event = ZOOM_connection_last_event(c[i-1]);
+        yaz_log(YLOG_DEBUG, "no = %d peek = %d event = %d %s", i-1,
+                peek,
+                event,
+                ZOOM_get_event_str(event));
+    }
+}
+
+static int next_token_chars(const char **cpp, const char **t_start,
+                            const char *tok_chars)
 {
     int len = 0;
     const char *cp = *cpp;
@@ -46,7 +66,7 @@ static int next_token(const char **cpp, const char **t_start)
     else
     {
         *t_start = cp;
-        while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
+        while (*cp && !strchr(tok_chars, *cp))
         {
             cp++;
             len++;
@@ -58,6 +78,12 @@ static int next_token(const char **cpp, const char **t_start)
     return len;  /* return -1 if no token was read .. */
 }
 
+static int next_token(const char **cpp, const char **t_start)
+{
+    return next_token_chars(cpp, t_start, "\r\n ");
+}
+
+
 static WRBUF next_token_new_wrbuf(const char **cpp)
 {
     WRBUF w = 0;
@@ -85,18 +111,18 @@ static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
                     ZOOM_options options,
                     const char **args)
 {
-    WRBUF key, val;
+    WRBUF key;
+    const char *val_buf;
+    int val_len;
 
     if (!(key = next_token_new_wrbuf(args)))
     {
         printf("missing argument for set\n");
         return ;
     }
-    if ((val = next_token_new_wrbuf(args)))
-    {
-        ZOOM_options_set(options, wrbuf_cstr(key), wrbuf_cstr(val));
-        wrbuf_destroy(val);
-    }
+    val_len = next_token_chars(args, &val_buf, "");
+    if (val_len != -1)
+        ZOOM_options_setl(options, wrbuf_cstr(key), val_buf, val_len);
     else
         ZOOM_options_set(options, wrbuf_cstr(key), 0);
     wrbuf_destroy(key);
@@ -196,9 +222,8 @@ static void display_records(ZOOM_connection c,
         }
         else
         {
-            int len, opac_len;
+            int len;
             const char *render = ZOOM_record_get(rec, type, &len);
-            const char *opac_render = ZOOM_record_get(rec, "opac", &opac_len);
             const char *syntax = ZOOM_record_get(rec, "syntax", 0);
             const char *schema = ZOOM_record_get(rec, "schema", 0);
             /* if rec is non-null, we got a record for display */
@@ -215,12 +240,6 @@ static void display_records(ZOOM_connection c,
                     }
                 }
                 printf("\n");
-                if (opac_render)
-                {
-                    if (fwrite(opac_render, 1, opac_len, stdout) != (size_t) 
-                               opac_len)
-                        printf("write to stdout failed\n");
-                }
             }
         }
     }
@@ -256,8 +275,7 @@ static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
 
     for (i = 0; i < MAX_CON; i++)
         ZOOM_resultset_records(r[i], 0, start, count);
-    while (ZOOM_event(MAX_CON, c))
-        ;
+    process_events(c);
 
     for (i = 0; i < MAX_CON; i++)
     {
@@ -281,6 +299,51 @@ static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
        
 }
 
+static void display_facets(ZOOM_facet_field *facets, int count) {
+    int index;
+    printf("Facets: \n");
+    for (index = 0; index <  count; index++) {
+        int term_index;
+        const char *facet_name = ZOOM_facet_field_name(facets[index]);
+        printf("  %s: \n", facet_name);
+        for (term_index = 0; term_index < ZOOM_facet_field_term_count(facets[index]); term_index++) {
+            int freq = 0;
+            const char *term = ZOOM_facet_field_get_term(facets[index], term_index, &freq);
+            printf("    %s(%d) \n", term,  freq);
+        }
+    }
+}
+
+static void cmd_facets(ZOOM_connection *c, ZOOM_resultset *r,
+                     ZOOM_options options,
+                     const char **args)
+{
+    int i;
+
+    process_events(c);
+
+    for (i = 0; i < MAX_CON; i++)
+    {
+        int error;
+        const char *errmsg, *addinfo, *dset;
+        /* display errors if any */
+        if (!c[i])
+            continue;
+        if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
+            printf("%s error: %s (%s:%d) %s\n",
+                   ZOOM_connection_option_get(c[i], "host"), errmsg,
+                   dset, error, addinfo);
+        else if (r[i])
+        {
+            int num_facets = ZOOM_resultset_facets_size(r[i]);
+            if (num_facets) {
+                ZOOM_facet_field  *facets = ZOOM_resultset_facets(r[i]);
+                display_facets(facets, num_facets);
+            }
+        }
+    }
+}
+
 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
                     ZOOM_options options,
                     const char **args)
@@ -300,8 +363,7 @@ static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
             p[i] = 0;
     }
 
-    while (ZOOM_event(MAX_CON, c))
-        ;
+    process_events(c);
 
     for (i = 0; i<MAX_CON; i++)
     {
@@ -371,8 +433,7 @@ static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
     }
     ZOOM_query_destroy(s);
 
-    while (ZOOM_event(MAX_CON, c))
-        ;
+    process_events(c);
 
     for (i = 0; i<MAX_CON; i++)
     {
@@ -390,9 +451,31 @@ static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
             /* OK, no major errors. Look at the result count */
             int start = ZOOM_options_get_int(options, "start", 0);
             int count = ZOOM_options_get_int(options, "count", 0);
+            int facet_num;
 
             printf("%s: %lld hits\n", ZOOM_connection_option_get(c[i], "host"),
                    (long long int) ZOOM_resultset_size(r[i]));
+            
+            facet_num = ZOOM_resultset_facets_size(r[i]);
+            if (facet_num)
+            {
+                ZOOM_facet_field *facets = ZOOM_resultset_facets(r[i]);
+                int facet_idx;
+                for (facet_idx = 0; facet_idx < facet_num; facet_idx++)
+                {
+                    const char *name = ZOOM_facet_field_name(facets[facet_idx]);
+                    size_t term_idx;
+                    size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
+                    printf("facet: %s\n", name);
+                    for (term_idx = 0; term_idx < term_num; term_idx++ )
+                    {
+                        int freq;
+                        const char *term =
+                            ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
+                        printf("term: %s %d\n", term, freq);
+                    }
+                }
+            }
             /* and display */
             display_records(c[i], r[i], start, count, "render");
         }
@@ -430,8 +513,8 @@ static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
     }
     ZOOM_query_destroy(query);
 
-    while (ZOOM_event(MAX_CON, c))
-        ;
+    process_events(c);
+
     for (i = 0; i<MAX_CON; i++)
     {
         int error;
@@ -474,8 +557,7 @@ static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
         if (r[i])
             ZOOM_resultset_sort(r[i], "yaz", sort_spec);
     }
-    while (ZOOM_event(MAX_CON, c))
-        ;
+    process_events(c);
 }
 
 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
@@ -485,6 +567,7 @@ static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
     printf("connect <zurl>\n");
     printf("search <pqf>\n");
     printf("show [<start> [<count> [<type]]]\n");
+    printf("facets\n");
     printf("scan <term>\n");
     printf("quit\n");
     printf("close <zurl>\n");
@@ -510,6 +593,7 @@ static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
     printf(" charset\n");
     printf(" lang\n");
     printf(" timeout\n");
+    printf(" facets\n");
 }
 
 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
@@ -581,6 +665,8 @@ static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
         cmd_connect(c, r, options, buf);
     else if (is_command("search", cmd_str, cmd_len))
         cmd_search(c, r, options, buf);
+    else if (is_command("facets", cmd_str, cmd_len))
+        cmd_facets(c, r, options, buf);
     else if (is_command("find", cmd_str, cmd_len))
         cmd_search(c, r, options, buf);
     else if (is_command("show", cmd_str, cmd_len))
@@ -639,8 +725,8 @@ void shell(ZOOM_connection *c, ZOOM_resultset *r,
 
 static void zoomsh(int argc, char **argv)
 {
-    ZOOM_options options = ZOOM_options_create();
-    int i, res;
+    ZOOM_options zoom_options = ZOOM_options_create();
+    int i, res = 1;
     ZOOM_connection z39_con[MAX_CON];
     ZOOM_resultset  z39_res[MAX_CON];
 
@@ -649,21 +735,31 @@ static void zoomsh(int argc, char **argv)
         z39_con[i] = 0;
         z39_res[i] = 0;
     }
-
-    for (i = 0; i<MAX_CON; i++)
-        z39_con[i] = 0;
-
-    res = 1;
-    for (i = 1; i<argc; i++)
+    while (res)
     {
-        const char *bp = argv[i];
-        res = cmd_parse(z39_con, z39_res, options, &bp);
-        if (res == 0)  /* received quit */
+        int mask;
+        char *arg = 0;
+        int option_ret = options("v:", argv, argc, &arg);
+        const char *bp = arg;
+        switch (option_ret)
+        {
+        case 0:
+            res = cmd_parse(z39_con, z39_res, zoom_options, &bp);
+            /* returns res == 0 on quit */
             break;
+        case YAZ_OPTIONS_EOF:
+            shell(z39_con, z39_res, zoom_options);
+            res = 0;
+            break;
+        case 'v':
+            mask = yaz_log_mask_str(arg);
+            yaz_log_init_level(mask);
+            break;
+        default:
+            fprintf(stderr, "zoomsh: [-v] [commands]\n");
+            res = 0;
+        }
     }
-    if (res)  /* do cmdline shell only if not quitting */
-        shell(z39_con, z39_res, options);
-    ZOOM_options_destroy(options);
 
     for (i = 0; i<MAX_CON; i++)
     {
@@ -674,24 +770,6 @@ static void zoomsh(int argc, char **argv)
 
 int main(int argc, char **argv)
 {
-    const char *maskstr = 0;
-    if (argc > 2 && !strcmp(argv[1], "-v"))
-    {
-        maskstr = argv[2];
-        argv += 2;
-        argc -= 2;
-    }
-    else if (argc > 1 && !strncmp(argv[1], "-v", 2))
-    {
-        maskstr = argv[1]+2;
-        argv++;
-        argc--;
-    }
-    if (maskstr)
-    {
-        int mask = yaz_log_mask_str(maskstr);
-        yaz_log_init_level(mask);
-    }
     zoomsh(argc, argv);
     exit(0);
 }