zoomsh: Refactor command show
[yaz-moved-to-github.git] / zoom / zoomsh.c
index d25c674..2c93b6e 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2009 Index Data
  * See the file LICENSE for details.
- *
- * $Id: zoomsh.c,v 1.48 2007-08-16 10:09:37 adam Exp $
  */
 
 /** \file zoomsh.c
@@ -14,8 +12,6 @@
 #include <string.h>
 #include <ctype.h>
 
-#include <yaz/comstack.h>
-
 #if HAVE_READLINE_READLINE_H
 #include <readline/readline.h> 
 #endif
 #include <readline/history.h>
 #endif
 
-#include <yaz/xmalloc.h>
-
 #include <yaz/log.h>
-#include <yaz/nmem.h>
 #include <yaz/zoom.h>
 
 #define MAX_CON 100
@@ -175,12 +168,12 @@ static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
 
 static void display_records(ZOOM_connection c,
                             ZOOM_resultset r,
-                            int start, int count)
+                            size_t start, size_t count, const char *type)
 {
-    int i;
-    for (i = 0; i<count; i++)
+    size_t i;
+    for (i = 0; i < count; i++)
     {
-        int pos = i + start;
+        size_t pos = i + start;
         ZOOM_record rec = ZOOM_resultset_record(r, pos);
         const char *db = ZOOM_record_get(rec, "database", 0);
         
@@ -191,28 +184,39 @@ static void display_records(ZOOM_connection c,
             const char *diagset;
             int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
             
-            printf("%d %s: %s (%s:%d) %s\n", pos, (db ? db : "unknown"),
-                   msg, diagset, error, addinfo);
+            printf("%lld %s: %s (%s:%d) %s\n", (long long) pos,
+                   (db ? db : "unknown"),
+                   msg, diagset, error, addinfo ? addinfo : "none");
         }
         else
         {
             int len, opac_len;
-            const char *render = ZOOM_record_get(rec, "render", &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 */
             if (rec)
             {
-                printf("%d %s %s\n",
-                       pos, (db ? db : "unknown"), syntax);
+                printf("%lld database=%s syntax=%s schema=%s\n",
+                       (long long) pos, (db ? db : "unknown"), syntax,
+                       schema ? schema : "unknown");
                 if (render)
-                    fwrite(render, 1, len, stdout);
+                {
+                    if (fwrite(render, 1, len, stdout) != (size_t) len)
+                    {
+                        printf("write to stdout failed\n");
+                    }
+                }
                 printf("\n");
                 if (opac_render)
-                    fwrite(opac_render, 1, opac_len, stdout);
+                {
+                    if (fwrite(opac_render, 1, opac_len, stdout) != (size_t) 
+                               opac_len)
+                        printf("write to stdout failed\n");
+                }
             }
         }
-            
     }
 }
 
@@ -221,20 +225,28 @@ static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
                      const char **args)
 {
     int i;
-    char start_str[10], count_str[10];
-
-    if (next_token_copy(args, start_str, sizeof(start_str)) >= 0)
-        ZOOM_options_set(options, "start", start_str);
+    size_t start = 0, count = 1;
+    const char *type = "render";
+    char render_str[40];
 
-    if (next_token_copy(args, count_str, sizeof(count_str)) >= 0)
-        ZOOM_options_set(options, "count", count_str);
+    {
+        char start_str[30], count_str[30];
+        if (next_token_copy(args, start_str, sizeof(start_str)) > 0)
+            start = atoi(start_str);
+        
+        if (next_token_copy(args, count_str, sizeof(count_str)) > 0)
+            count = atoi(count_str);
+        
+        if (next_token_copy(args, render_str, sizeof(render_str)) > 0)
+            type = render_str;
+    }
 
-    for (i = 0; i<MAX_CON; i++)
-        ZOOM_resultset_records(r[i], 0, atoi(start_str), atoi(count_str));
+    for (i = 0; i < MAX_CON; i++)
+        ZOOM_resultset_records(r[i], 0, start, count);
     while (ZOOM_event(MAX_CON, c))
         ;
 
-    for (i = 0; i<MAX_CON; i++)
+    for (i = 0; i < MAX_CON; i++)
     {
         int error;
         const char *errmsg, *addinfo, *dset;
@@ -248,13 +260,9 @@ static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
         else if (r[i])
         {
             /* OK, no major errors. Display records... */
-            int start = ZOOM_options_get_int(options, "start", 0);
-            int count = ZOOM_options_get_int(options, "count", 0);
-            display_records(c[i], r[i], start, count);
+            display_records(c[i], r[i], start, count, type);
         }
     }
-    ZOOM_options_set(options, "count", "0");
-    ZOOM_options_set(options, "start", "0");
 }
 
 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
@@ -369,10 +377,10 @@ static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
             int start = ZOOM_options_get_int(options, "start", 0);
             int count = ZOOM_options_get_int(options, "count", 0);
 
-            printf("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
-                   (long) ZOOM_resultset_size(r[i]));
+            printf("%s: %lld hits\n", ZOOM_connection_option_get(c[i], "host"),
+                   (long long int) ZOOM_resultset_size(r[i]));
             /* and display */
-            display_records(c[i], r[i], start, count);
+            display_records(c[i], r[i], start, count, "render");
         }
     }
 }
@@ -426,12 +434,11 @@ static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
             size_t p, sz = ZOOM_scanset_size(s[i]);
             for (p = 0; p < sz; p++)
             {
-                int occ = 0;
-                int len = 0;
+                size_t occ = 0;
+                size_t len = 0;
                 const char *term = ZOOM_scanset_display_term(s[i], p,
                                                              &occ, &len);
-                fwrite(term, 1, len, stdout);
-                printf(" %d\n", occ);
+                printf("%.*s %lld\n", (int) len, term, (long long int) occ);
             }            
             ZOOM_scanset_destroy(s[i]);
         }
@@ -463,7 +470,7 @@ static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
 {
     printf("connect <zurl>\n");
     printf("search <pqf>\n");
-    printf("show [<start> [<count>]\n");
+    printf("show [<start> [<count> [<type]]]\n");
     printf("scan <term>\n");
     printf("quit\n");
     printf("close <zurl>\n");
@@ -674,6 +681,7 @@ int main(int argc, char **argv)
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab