ignore yazhtml.xsl
[yaz-moved-to-github.git] / client / client.c
index 697dc0d..6157a37 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2003, Index Data
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.188 2003-04-29 21:13:08 adam Exp $
+ * $Id: client.c,v 1.191 2003-04-30 14:04:45 adam Exp $
  */
 
 #include <stdio.h>
@@ -398,16 +398,11 @@ static int process_initResponse(Z_InitResponse *res)
     return 0;
 }
 
-static int cmd_base(const char *arg)
+static int set_base(const char *arg)
 {
     int i;
     const char *cp;
 
-    if (!*arg)
-    {
-        printf("Usage: base <database> <database> ...\n");
-        return 0;
-    }
     for (i = 0; i<num_databaseNames; i++)
         xfree (databaseNames[i]);
     num_databaseNames = 0;
@@ -431,27 +426,31 @@ static int cmd_base(const char *arg)
             break;
         arg = cp+1;
     }
+    if (num_databaseNames == 0)
+    {
+        num_databaseNames = 1;
+        databaseNames[0] = xstrdup("");
+    }
     return 1;
 }
 
-void cmd_open_remember_last_open_command(const char* arg, char* new_open_command)
+static int cmd_base(const char *arg)
 {
-    if(last_open_command != arg) 
+    if (!*arg)
     {
-        if(last_open_command) xfree(last_open_command);
-        last_open_command = xstrdup(new_open_command);
+        printf("Usage: base <database> <database> ...\n");
+        return 0;
     }
+    return set_base(arg);
 }
 
-int cmd_open(const char *arg)
+void cmd_open_remember_last_open_command(const char* arg, char* new_open_command)
 {
-    static char cur_host[200];
-    if (arg)
+    if(last_open_command != arg) 
     {
-        strncpy (cur_host, arg, sizeof(cur_host)-1);
-        cur_host[sizeof(cur_host)-1] = 0;
+        if(last_open_command) xfree(last_open_command);
+        last_open_command = xstrdup(new_open_command);
     }
-    return session_connect(cur_host);
 }
 
 int session_connect(const char *arg)
@@ -461,8 +460,6 @@ int session_connect(const char *arg)
     const char *basep = 0;
     if (conn)
     {
-        printf("Already connected.\n");
-        
         cs_close (conn);
         conn = NULL;
         if (session_mem)
@@ -489,14 +486,19 @@ int session_connect(const char *arg)
     }
 #if HAVE_XML2
 #else
-    if (conn->protocol == PROTO_Z3950)
+    if (conn->protocol == PROTO_HTTP)
     {
-        printf ("SRW not enabled in this YAZ\n");
+        printf ("SRW/HTTP not enabled in this YAZ\n");
         cs_close(conn);
         conn = 0;
         return 0;
     }
 #endif
+    protocol = conn->protocol;
+    if (conn->protocol == PROTO_HTTP)
+        set_base("");
+    else
+        set_base("Default");
     printf("Connecting...");
     fflush(stdout);
     if (cs_connect(conn, add) < 0)
@@ -514,8 +516,8 @@ int session_connect(const char *arg)
     }
     printf("OK.\n");
     if (basep && *basep)
-        cmd_base (basep);
-    if (conn->protocol == PROTO_Z3950)
+        set_base (basep);
+    if (protocol == PROTO_Z3950)
     {
         send_initRequest(type_and_host);
         return 2;
@@ -523,6 +525,17 @@ int session_connect(const char *arg)
     return 0;
 }
 
+int cmd_open(const char *arg)
+{
+    static char cur_host[200];
+    if (arg)
+    {
+        strncpy (cur_host, arg, sizeof(cur_host)-1);
+        cur_host[sizeof(cur_host)-1] = 0;
+    }
+    return session_connect(cur_host);
+}
+
 void try_reconnect() 
 {
     char* open_command;
@@ -891,7 +904,7 @@ static int send_srw(Z_SRW_PDU *sr)
 {
     const char *charset = 0;
     const char *host_port = 0;
-    const char *path = "/";
+    char *path = 0;
     char ctype[50];
     Z_SOAP_Handler h[2] = {
         {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
@@ -902,6 +915,9 @@ static int send_srw(Z_SRW_PDU *sr)
     Z_SOAP *p = odr_malloc(o, sizeof(*p));
     Z_GDU *gdu;
 
+    path = odr_malloc(out, strlen(databaseNames[0])+2);
+    *path = '/';
+    strcpy(path+1, databaseNames[0]);
 
     gdu = z_get_HTTP_Request(out);
     gdu->u.HTTP_Request->path = odr_strdup(out, path);
@@ -984,6 +1000,8 @@ static int send_SRW_searchRequest(const char *arg)
     }
     odr_reset(srw_sr_odr_out);
 
+    setno = 1;
+
     /* save this for later .. when fetching individual records */
     srw_sr = sr = yaz_srw_get(srw_sr_odr_out, Z_SRW_searchRetrieve_request);
     sr->u.request->query_type = Z_SRW_query_type_cql;
@@ -1650,7 +1668,7 @@ static int send_itemorder(const char *type, int itemno)
 
 static int only_z3950()
 {
-    if (conn && conn->protocol == PROTO_HTTP)
+    if (protocol == PROTO_HTTP)
     {
        printf ("Not supported by SRW\n");
        return 1;
@@ -1805,18 +1823,11 @@ static int cmd_find(const char *arg)
         printf("Find what?\n");
         return 0;
     }
-    if (!conn)
-    {
-        try_reconnect(); 
-        
-        if (!conn) {                                   
-            printf("Not connected yet\n");
-            return 0;
-        }
-    }
-    if (conn->protocol == PROTO_HTTP)
+    if (protocol == PROTO_HTTP)
     {
 #if HAVE_XML2
+        if (!conn)
+            cmd_open(0);
         if (!send_SRW_searchRequest(arg))
             return 0;
 #else
@@ -1825,6 +1836,15 @@ static int cmd_find(const char *arg)
     }
     else
     {
+        if (!conn)
+        {
+            try_reconnect(); 
+            
+            if (!conn) {                                       
+                printf("Not connected yet\n");
+                return 0;
+            }
+        }
         if (!send_searchRequest(arg))
             return 0;
     }
@@ -1930,7 +1950,6 @@ static int send_presentRequest(const char *arg)
     oident prefsyn;
     int nos = 1;
     int oid[OID_SIZE];
-    char *p;
     char setstring[100];
 
     req->referenceId = set_refid (out);
@@ -2029,13 +2048,6 @@ static void close_session (void)
         nmem_destroy (session_mem);
         session_mem = NULL;
     }
-    if (srw_sr)
-    {
-        odr_destroy(srw_sr_odr_out);
-        srw_sr_odr_out = 0;
-        srw_sr = 0;
-    }
-    assert (srw_sr_odr_out == 0);
     sent_close = 0;
     odr_reset(out);
     odr_reset(in);
@@ -2076,14 +2088,11 @@ void process_close(Z_Close *req)
 
 static int cmd_show(const char *arg)
 {
-    if (!conn)
-    {
-        printf("Not connected yet\n");
-        return 0;
-    }
-    if (conn->protocol == PROTO_HTTP)
+    if (protocol == PROTO_HTTP)
     {
 #if HAVE_XML2
+        if (!conn)
+            cmd_open(0);
         if (!send_SRW_presentRequest(arg))
             return 0;
 #else
@@ -2092,6 +2101,11 @@ static int cmd_show(const char *arg)
     }
     else
     {
+        if (!conn)
+        {
+            printf("Not connected yet\n");
+            return 0;
+        }
         if (!send_presentRequest(arg))
             return 0;
     }
@@ -2856,8 +2870,6 @@ static void initialize(void)
         ccl_qual_file (bibset, inf);
         fclose (inf);
     }
-    cmd_base("Default");
-
 #if HAVE_READLINE_READLINE_H
     rl_attempted_completion_function = (CPPFunction*)readline_completer;
 #endif
@@ -2893,7 +2905,6 @@ static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
         printf ("Number of hits: %d\n", *res->numberOfRecords);
     for (i = 0; i<res->num_records; i++)
     {
-        int pos;
         Z_SRW_record *rec = res->records + i;
 
         if (rec->recordPosition)
@@ -2902,7 +2913,7 @@ static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
             setno = *rec->recordPosition + 1;
         }
         if (rec->recordSchema)
-            printf (" scheam=%d", *rec->recordSchema);
+            printf (" schema=%s", rec->recordSchema);
         printf ("\n");
         if (rec->recordData_buf && rec->recordData_len)
         {
@@ -2912,14 +2923,6 @@ static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
     }
 }
 
-static void set_HTTP_error (int error,
-                            const char *addinfo, const char *addinfo2)
-{
-
-
-}
-
-
 static void http_response(Z_HTTP_Response *hres)
 {
     int ret = -1;
@@ -2927,8 +2930,6 @@ static void http_response(Z_HTTP_Response *hres)
                                                     "Content-Type");
     const char *connection_head = z_HTTP_header_lookup(hres->headers,
                                                        "Connection");
-    yaz_log (LOG_LOG, "http_response");
-
     if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
     {
         Z_SOAP *soap_package = 0;
@@ -2966,10 +2967,12 @@ static void http_response(Z_HTTP_Response *hres)
     if (ret)
     {
         if (hres->code != 200)
-            set_HTTP_error(hres->code, 0, 0);
+        {
+           printf ("HTTP Error Status=%d\n", hres->code);
+        }
         else
         {
-            printf ("decoding of SRW package failed\n");
+            printf ("Decoding of SRW package failed\n");
         }
         close_session();
     }
@@ -2998,9 +3001,8 @@ void wait_and_handle_response()
     
     while(conn)
     {
-        printf ("cs_get....\n");
         res = cs_get(conn, &netbuffer, &netbufferlen);
-        if (reconnect_ok && res <= 0 && conn->protocol == PROTO_HTTP)
+        if (reconnect_ok && res <= 0 && protocol == PROTO_HTTP)
         {
             cs_close(conn);
             conn = 0;
@@ -3030,7 +3032,6 @@ void wait_and_handle_response()
         record_last = 0;
         odr_setbuf(in, netbuffer, res, 0);
         
-        printf ("got input packet %d bytes\n", res);
         if (!z_GDU(in, &gdu, 0, 0))
         {
             FILE *f = ber_file ? ber_file : stdout;