Explicitly initialise cqltrans to 0
[yaz-moved-to-github.git] / client / client.c
index 1055bff..ab6f759 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.325 2007-01-24 11:50:18 adam Exp $
+ * $Id: client.c,v 1.331 2007-03-28 16:35:47 mike Exp $
  */
 /** \file client.c
  *  \brief yaz-client program
@@ -13,6 +13,9 @@
 #include <assert.h>
 #include <time.h>
 #include <ctype.h>
+#ifndef WIN32
+#include <signal.h>
+#endif
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
@@ -155,7 +158,7 @@ typedef enum {
 static QueryType queryType = QueryType_Prefix;
 
 static CCL_bibset bibset;               /* CCL bibset handle */
-static cql_transform_t cqltrans;        /* CQL context-set handle */
+static cql_transform_t cqltrans = 0; /* CQL context-set handle */
 
 #if HAVE_READLINE_COMPLETION_OVER
 
@@ -935,66 +938,45 @@ static void display_record(Z_External *r)
 #endif
                 )
             {
-                char *result;
-                int rlen;
+                const char *result;
+                size_t rlen;
                 yaz_iconv_t cd = 0;
                 yaz_marc_t mt = yaz_marc_create();
-                    
-                if (yaz_marc_decode_buf(mt, octet_buf,r->u.octet_aligned->len,
-                                        &result, &rlen)> 0)
+                const char *from = 0;
+
+                if (marcCharset && !strcmp(marcCharset, "auto"))
                 {
-                    char *from = 0;
-                    if (marcCharset && !strcmp(marcCharset, "auto"))
+                    if (ent->value == VAL_USMARC)
                     {
-                        if (ent->value == VAL_USMARC)
-                        {
-                            if (octet_buf[9] == 'a')
-                                from = "UTF-8";
-                            else
-                                from = "MARC-8";
-                        }
-                        else
-                            from = "ISO-8859-1";
-                    }
-                    else if (marcCharset)
-                        from = marcCharset;
-                    if (outputCharset && from)
-                    {   
-                        cd = yaz_iconv_open(outputCharset, from);
-                        printf ("convert from %s to %s", from, 
-                                outputCharset);
-                        if (!cd)
-                            printf (" unsupported\n");
+                        if (octet_buf[9] == 'a')
+                            from = "UTF-8";
                         else
-                            printf ("\n");
+                            from = "MARC-8";
                     }
+                    else
+                        from = "ISO-8859-1";
+                }
+                else if (marcCharset)
+                    from = marcCharset;
+                if (outputCharset && from)
+                {   
+                    cd = yaz_iconv_open(outputCharset, from);
+                    printf ("convert from %s to %s", from, 
+                            outputCharset);
                     if (!cd)
-                        fwrite (result, 1, rlen, stdout);
+                        printf (" unsupported\n");
                     else
                     {
-                        char outbuf[6];
-                        size_t inbytesleft = rlen;
-                        const char *inp = result;
-                        
-                        while (inbytesleft)
-                        {
-                            size_t outbytesleft = sizeof(outbuf);
-                            char *outp = outbuf;
-                            size_t r;
-
-                            r = yaz_iconv (cd, (char**) &inp,
-                                           &inbytesleft, 
-                                           &outp, &outbytesleft);
-                            if (r == (size_t) (-1))
-                            {
-                                int e = yaz_iconv_error(cd);
-                                if (e != YAZ_ICONV_E2BIG)
-                                    break;
-                            }
-                            fwrite (outbuf, outp - outbuf, 1, stdout);
-                        }
+                        yaz_marc_iconv(mt, cd);
+                        printf ("\n");
                     }
                 }
+                    
+                if (yaz_marc_decode_buf(mt, octet_buf, r->u.octet_aligned->len,
+                                        &result, &rlen)> 0)
+                {
+                    fwrite (result, rlen, 1, stdout);
+                }
                 else
                 {
                     printf ("bad MARC. Dumping as it is:\n");
@@ -1035,8 +1017,8 @@ static void display_record(Z_External *r)
         }
         w = wrbuf_alloc();
         yaz_display_grs1(w, r->u.grs1, 0);
-        puts (wrbuf_buf(w));
-        wrbuf_free(w, 1);
+        puts (wrbuf_cstr(w));
+        wrbuf_destroy(w);
     }
     else if (ent && ent->value == VAL_OPAC)
     {
@@ -1320,19 +1302,16 @@ static char *encode_SRW_term(ODR o, const char *q)
     cd = yaz_iconv_open("UTF-8", in_charset);
     if (!cd)
     {
-        wrbuf_free(w, 1);
+        wrbuf_destroy(w);
         return odr_strdup(o, q);
     }
     wrbuf_iconv_write(w, cd, q, strlen(q));
     if (wrbuf_len(w))
-    {
-        int len = wrbuf_len(w);
-        res = odr_strdupn(o, wrbuf_buf(w), len);
-    }
+        res = odr_strdup(o, wrbuf_cstr(w));
     else
         res = odr_strdup(o, q);    
     yaz_iconv_close(cd);
-    wrbuf_free(w, 1);
+    wrbuf_destroy(w);
     return res;
 }
 
@@ -2152,20 +2131,22 @@ static int cmd_update_SRW(int action_no, const char *recid,
 
 static int cmd_update_common(const char *arg, int version)
 {
-    char action[20], recid[20];
+    char action[20], recid_buf[20];
+    const char *recid = 0;
     char *rec_buf;
     int rec_len;
     int action_no;
     int noread = 0;
 
     *action = 0;
-    *recid = 0;
-    sscanf (arg, "%19s %19s%n", action, recid, &noread);
+    *recid_buf = 0;
+    sscanf (arg, "%19s %19s%n", action, recid_buf, &noread);
     if (noread == 0)
     {
-        printf("Update must be followed by action and recid\n");
+        printf("Use: update action recid [fname]\n");
         printf(" where action is one of insert,replace,delete.update\n");
-        printf(" recid is some record ID (any string)\n");
+        printf(" recid is some record ID. Use none for no ID\n");
+        printf(" fname is file of record to be updated\n");
         return 0;
     }
 
@@ -2184,6 +2165,9 @@ static int cmd_update_common(const char *arg, int version)
         return 0;
     }
 
+    if (strcmp(recid_buf, "none")) /* none means no record ID */
+        recid = recid_buf;
+
     arg += noread;
     if (parse_cmd_doc(&arg, out, &rec_buf, &rec_len, 1) == 0)
         return 0;
@@ -2295,7 +2279,7 @@ static int cmd_update_Z3950(int version, int action_no, const char *recid,
         notToKeep->elements[0] = (Z_IU0SuppliedRecords_elem *)
             odr_malloc(out, sizeof(**notToKeep->elements));
         notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
-        if (*recid && strcmp(recid, "none"))
+        if (recid)
         {
             notToKeep->elements[0]->u.opaque = (Odr_oct *)
                 odr_malloc (out, sizeof(Odr_oct));
@@ -2337,7 +2321,7 @@ static int cmd_update_Z3950(int version, int action_no, const char *recid,
         notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
             odr_malloc(out, sizeof(**notToKeep->elements));
         notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
-        if (*recid)
+        if (recid)
         {
             notToKeep->elements[0]->u.opaque = (Odr_oct *)
                 odr_malloc (out, sizeof(Odr_oct));
@@ -4700,12 +4684,23 @@ char **readline_completer(char *text, int start, int end)
 }
 #endif
 
+#ifndef WIN32
+void ctrl_c_handler(int x)
+{
+    exit_client(0);
+}
+#endif
+
 static void client(void)
 {
     char line[10240];
 
     line[10239] = '\0';
 
+#ifndef WIN32
+    signal(SIGINT, ctrl_c_handler);
+#endif
+
 #if HAVE_GETTIMEOFDAY
     gettimeofday (&tv_start, 0);
 #endif