display_records() now displays the record syntax OID as well as its symbolic name...
[yaz-moved-to-github.git] / client / client.c
index f2d1b7c..c7c6e1b 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2003, Index Data
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.196 2003-05-22 17:01:33 mike Exp $
+ * $Id: client.c,v 1.201 2003-06-19 21:10:03 adam Exp $
  */
 
 #include <stdio.h>
 #define C_PROMPT "Z> "
 
 static char *codeset = 0;               /* character set for output */
-
+static int hex_dump = 0;
 static ODR out, in, print;              /* encoding and decoding streams */
-#ifdef THESE_ARE_NOT_USED
+#if HAVE_XML2
 static ODR srw_sr_odr_out = 0;
 static Z_SRW_PDU *srw_sr = 0;
-#endif /*THESE_ARE_NOT_USED*/
+#endif
 static FILE *apdu_file = 0;
 static FILE *ber_file = 0;
 static COMSTACK conn = 0;               /* our z-association */
@@ -87,7 +87,8 @@ static Z_InitResponse *session = 0;     /* session parameters */
 static char last_scan_line[512] = "0";
 static char last_scan_query[512] = "0";
 static char ccl_fields[512] = "default.bib";
-static char cql_fields[512] = "pqf.properties";
+/* ### How can I set this path to use wherever YAZ is installed? */
+static char cql_fields[512] = "/usr/local/share/yaz/etc/pqf.properties";
 static char *esPackageName = 0;
 static char *yazProxy = 0;
 static int kilobytes = 1024;
@@ -158,21 +159,21 @@ const char* query_type_as_string(QueryType q)
     }
 }
 
-
-void do_hex_dump(char* buf,int len) 
+static void do_hex_dump(const char* buf, int len) 
 {
-#if 0 
-    int i,x;
-    for( i=0; i<len ; i=i+16 ) 
-    {                  
-        printf(" %4.4d ",i);
-        for(x=0 ; i+x<len && x<16; ++x) 
-        {
-            printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
-        }
-        printf("\n");
+    if (hex_dump)
+    {
+       int i,x;
+       for( i=0; i<len ; i=i+16 ) 
+       {                       
+           printf(" %4.4d ",i);
+           for(x=0 ; i+x<len && x<16; ++x) 
+           {
+               printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
+           }
+           printf("\n");
+       }
     }
-#endif
 }
 
 void add_otherInfos(Z_APDU *a) 
@@ -715,31 +716,35 @@ static void display_record(Z_External *r)
                                         &result, &rlen)> 0)
                 {
                     char *from = 0;
-                    if (marcCharset && strcmp(marcCharset, "auto"))
-                        from = marcCharset;
-                    else
+                    if (marcCharset && !strcmp(marcCharset, "auto"))
                     {
                         if (ent->value == VAL_USMARC)
                         {
                             if (octet_buf[9] == 'a')
                                 from = "UTF-8";
                             else
-                                from = "MARC8";
+                                from = "MARC-8";
                         }
                         else
                             from = "ISO-8859-1";
                     }
+                   else if (marcCharset)
+                       from = marcCharset;
                     if (outputCharset && from)
                     {   
-                        printf ("convert from %s to %s\n", from, 
-                                outputCharset);
                         cd = yaz_iconv_open(outputCharset, from);
+                        printf ("convert from %s to %s", from, 
+                                outputCharset);
+                       if (!cd)
+                           printf (" unsupported\n");
+                       else
+                           printf ("\n");
                     }
                     if (!cd)
                         fwrite (result, 1, rlen, stdout);
                     else
                     {
-                        char outbuf[12];
+                        char outbuf[6];
                         size_t inbytesleft = rlen;
                         const char *inp = result;
                         
@@ -747,9 +752,11 @@ static void display_record(Z_External *r)
                         {
                             size_t outbytesleft = sizeof(outbuf);
                             char *outp = outbuf;
-                            size_t r = yaz_iconv (cd, (char**) &inp,
-                                                  &inbytesleft, 
-                                                  &outp, &outbytesleft);
+                           size_t r;
+
+                            r = yaz_iconv (cd, (char**) &inp,
+                                          &inbytesleft, 
+                                          &outp, &outbytesleft);
                             if (r == (size_t) (-1))
                             {
                                 int e = yaz_iconv_error(cd);
@@ -1047,7 +1054,6 @@ static int send_searchRequest(const char *arg)
     Z_External *ext;
     QueryType myQueryType = queryType;
     char pqfbuf[512];
-    char *addinfo;
 
     if (myQueryType == QueryType_CCL2RPN)
     {
@@ -1059,19 +1065,24 @@ static int send_searchRequest(const char *arg)
         }
     } else if (myQueryType == QueryType_CQL2RPN) {
        /* ### All this code should be wrapped in a utility function */
-       CQL_parser parser = cql_parser_create();
+       CQL_parser parser;
        struct cql_node *node;
+       const char *addinfo;
+       if (cqltrans == 0) {
+            printf("Can't use CQL: no translation file.  Try set_cqlfile\n");
+           return 0;
+       }
+       parser = cql_parser_create();
        if ((error = cql_parser_string(parser, arg)) != 0) {
-           /* ### must do better with the reporting here */
-            printf("CQL ERROR %d: presumably a syntax error?\n", error);
+            printf("Can't parse CQL: must be a syntax error\n");
             return 0;
        }
        node = cql_parser_result(parser);
        if ((error = cql_transform_buf(cqltrans, node, pqfbuf,
                                       sizeof pqfbuf)) != 0) {
            error = cql_transform_error(cqltrans, &addinfo);
-            printf ("Couldn't convert CQL to PQF: error #%d (addinfo=%s)\n",
-                   error, addinfo);
+           printf ("Can't convert CQL to PQF: %s (addinfo=%s)\n",
+                   cql_strerror(error), addinfo);
             return 0;
         }
        arg = pqfbuf;
@@ -2772,7 +2783,9 @@ int cmd_set_cqlfile(const char* arg)
         perror("unable to open CQL file");
        return 0;
     }
-    cql_transform_close(cqltrans);
+    if (cqltrans != 0)
+       cql_transform_close(cqltrans);
+
     cqltrans = newcqltrans;
     strcpy(cql_fields, arg);
     return 0;
@@ -2818,17 +2831,6 @@ int cmd_set_marcdump(const char* arg)
     return 1;
 }
 
-int cmd_set_proxy(const char* arg)
-{
-    if(yazProxy) free(yazProxy);
-    yazProxy=NULL;
-    
-    if(strlen(arg) > 1) {
-        yazProxy=strdup(arg);
-    }
-    return 1;
-}
-
 /* 
    this command takes 3 arge {name class oid} 
 */
@@ -2965,10 +2967,8 @@ static void initialize(void)
         fclose (inf);
     }
 
-    if ((cqltrans = cql_transform_open_fname(cql_fields)) == 0) {
-       /* ### There should be a better way to make an empty set! */
-       cqltrans = cql_transform_open_fname("/dev/null");
-    }
+    cqltrans = cql_transform_open_fname(cql_fields);
+    /* If this fails, no problem: we detect cqltrans == 0 later */
 
 #if HAVE_READLINE_READLINE_H
     rl_attempted_completion_function = (CPPFunction*)readline_completer;
@@ -3132,6 +3132,7 @@ void wait_and_handle_response()
         odr_reset(out);
         odr_reset(in); /* release APDU from last round */
         record_last = 0;
+        do_hex_dump(netbuffer, res);
         odr_setbuf(in, netbuffer, res, 0);
         
         if (!z_GDU(in, &gdu, 0, 0))
@@ -3144,8 +3145,9 @@ void wait_and_handle_response()
             fprintf(f, "---------\n");
             if (apdu_file)
                 z_GDU(print, &gdu, 0, 0);
-            close_session ();
-            break;
+            if (conn && cs_more(conn))
+                continue;
+           break;
         }
         if (ber_file)
             odr_dumpBER(ber_file, netbuffer, res);
@@ -3797,8 +3799,10 @@ int main(int argc, char **argv)
     codeset = nl_langinfo(CODESET);
 #endif
 #endif
+    if (codeset)
+       outputCharset = xstrdup(codeset);
 
-    while ((ret = options("k:c:q:a:b:m:v:p:u:t:V", argv, argc, &arg)) != -2)
+    while ((ret = options("k:c:q:a:b:m:v:p:u:t:Vx", argv, argc, &arg)) != -2)
     {
         switch (ret)
         {
@@ -3843,6 +3847,9 @@ int main(int argc, char **argv)
             else
                 apdu_file=fopen(arg, "a");
             break;
+       case 'x':
+           hex_dump = 1;
+           break;
         case 'p':
             yazProxy=strdup(arg);
             break;