Build packages for Ubuntu Trusty Tahr 14.04 LTS
[yaz-moved-to-github.git] / zoom / zoomsh.c
index daa6cc7..fa4c0f8 100644 (file)
@@ -31,6 +31,8 @@
 #include <yaz/zoom.h>
 
 struct zoom_sh {
+    WRBUF strategy;
+    WRBUF criteria;
     ZOOM_options options;
     struct zoom_db *list;
 };
@@ -491,6 +493,43 @@ static int cmd_debug(struct zoom_sh *sh, const char **args)
     return 0;
 }
 
+static void display_search_result(struct zoom_db *db)
+{
+    const char *v;
+    int num;
+
+    v = ZOOM_resultset_option_get(db->res, "searchresult.size");
+    if (v && (num = atoi(v)))
+    {
+        int i;
+        printf("SearchResult-1:");
+        for (i = 0; i < num; i++)
+        {
+            const char *v;
+            char str[60];
+
+            if (i)
+                printf(",");
+
+            sprintf(str, "searchresult.%d.id", i);
+            v = ZOOM_resultset_option_get(db->res, str);
+            if (v)
+                printf(" id=%s", v);
+
+            sprintf(str, "searchresult.%d.subquery.term", i);
+            v = ZOOM_resultset_option_get(db->res, str);
+            if (v)
+                printf(" term=%s", v);
+
+            sprintf(str, "searchresult.%d.count", i);
+            v = ZOOM_resultset_option_get(db->res, str);
+            if (v)
+                printf(" cnt=%s", v);
+        }
+        printf("\n");
+    }
+}
+
 static int cmd_search(struct zoom_sh *sh, const char **args)
 {
     ZOOM_query s;
@@ -511,6 +550,21 @@ static int cmd_search(struct zoom_sh *sh, const char **args)
         ZOOM_query_destroy(s);
         return 1;
     }
+    if (sh->strategy && wrbuf_len(sh->strategy) && wrbuf_len(sh->criteria))
+    {
+        int r = ZOOM_query_sortby2(s, wrbuf_cstr(sh->strategy),
+                                   wrbuf_cstr(sh->criteria));
+        if (r)
+        {
+            if (r == -1)
+                printf("Bad sortby strategy: %s\n", wrbuf_cstr(sh->strategy));
+            else
+                printf("Bad sortby criteria: %s\n", wrbuf_cstr(sh->criteria));
+            ZOOM_query_destroy(s);
+            return 1;
+        }
+        printf("sortby added\n");
+    }
     for (db = sh->list; db; db = db->next)
     {
         if (db->con)
@@ -569,6 +623,7 @@ static int cmd_search(struct zoom_sh *sh, const char **args)
                     }
                 }
             }
+            display_search_result(db);
             /* and display */
             display_records(db->con, db->res, start, count, "render");
         }
@@ -647,6 +702,26 @@ static int cmd_scan(struct zoom_sh *sh, const char **args)
     return ret;
 }
 
+static int cmd_sortby(struct zoom_sh *sh, const char **args)
+{
+    WRBUF strategy;
+    const char *criteria;
+    if (!(strategy = next_token_new_wrbuf(args)))
+    {
+        printf("missing argument argument: strategy and criteria\n");
+        return 1;
+    }
+    criteria = *args;
+    while (*criteria == ' ')
+        criteria++;
+    wrbuf_destroy(sh->strategy);
+    sh->strategy = strategy;
+
+    wrbuf_rewind(sh->criteria);
+    wrbuf_puts(sh->criteria, criteria);
+    return 0;
+}
+
 static int cmd_sort(struct zoom_sh *sh, const char **args)
 {
     const char *sort_spec = *args;
@@ -667,6 +742,7 @@ static int cmd_help(struct zoom_sh *sh, const char **args)
 {
     printf("connect <zurl>\n");
     printf("search <pqf>\n");
+    printf("sortby <strategy> <criteria>\n");
     printf("show [<start> [<count> [<type]]]\n");
     printf("facets\n");
     printf("scan <term>\n");
@@ -777,6 +853,8 @@ static int cmd_parse(struct zoom_sh *sh, const char **buf)
         ret = cmd_connect(sh, buf);
     else if (is_command("search", cmd_str, cmd_len))
         ret = cmd_search(sh, buf);
+    else if (is_command("sortby", cmd_str, cmd_len))
+        ret = cmd_sortby(sh, buf);
     else if (is_command("facets", cmd_str, cmd_len))
         ret = cmd_facets(sh, buf);
     else if (is_command("find", cmd_str, cmd_len))
@@ -842,7 +920,10 @@ static int shell(struct zoom_sh *sh, int exit_on_error)
 #endif
         if (!line_in) /* no line buffer via readline or not enabled at all */
         {
-            printf("ZOOM>"); fflush(stdout);
+            if (isatty(0))
+            {
+                printf("ZOOM>"); fflush(stdout);
+            }
             if (!fgets(buf, sizeof(buf)-1, stdin))
             {
                 res = -1;
@@ -869,12 +950,14 @@ static int zoomsh(int argc, char **argv)
 
     sh.list = 0;
     sh.options = ZOOM_options_create();
+    sh.strategy = 0;
+    sh.criteria = wrbuf_alloc();
 
     while (res == 0)
     {
         int mask;
         char *arg = 0;
-        int option_ret = options("ev:", argv, argc, &arg);
+        int option_ret = options("a:ev:", argv, argc, &arg);
         const char *bp = arg;
         switch (option_ret)
         {
@@ -887,6 +970,9 @@ static int zoomsh(int argc, char **argv)
         case YAZ_OPTIONS_EOF:
             res = shell(&sh, exit_on_error);
             break;
+        case 'a':
+            ZOOM_options_set(sh.options, "apdufile", arg);
+            break;
         case 'e':
             exit_on_error = 1;
             break;
@@ -895,7 +981,7 @@ static int zoomsh(int argc, char **argv)
             yaz_log_init_level(mask);
             break;
         default:
-            fprintf(stderr, "zoomsh: [-e] [-v] [commands]\n");
+            fprintf(stderr, "zoomsh: [-a apdulog] [-e] [-v level] [commands]\n");
             res = 1;
         }
     }
@@ -909,6 +995,8 @@ static int zoomsh(int argc, char **argv)
         db = n;
     }
     ZOOM_options_destroy(sh.options);
+    wrbuf_destroy(sh.strategy);
+    wrbuf_destroy(sh.criteria);
     if (res == -1) /* quit .. which is not an error */
         res = 0;
     return res;