Stylesheet support.
[yaz-moved-to-github.git] / client / client.c
index 4fddacf..907bf3c 100644 (file)
@@ -1,8 +1,8 @@
 /* 
- * Copyright (c) 1995-2003, Index Data
+ * Copyright (c) 1995-2004, Index Data
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.214 2003-12-09 17:13:35 adam Exp $
+ * $Id: client.c,v 1.222 2004-01-07 20:36:44 adam Exp $
  */
 
 #include <stdio.h>
@@ -81,7 +81,7 @@ static Z_ElementSetNames *elementSetNames = 0;
 static int setno = 1;                   /* current set offset */
 static enum oid_proto protocol = PROTO_Z3950;      /* current app protocol */
 static enum oid_value recordsyntax = VAL_USMARC;
-static char *schema = 0;
+static char *record_schema = 0;
 static int sent_close = 0;
 static NMEM session_mem = NULL;         /* memory handle for init-response */
 static Z_InitResponse *session = 0;     /* session parameters */
@@ -117,7 +117,7 @@ typedef enum {
 static QueryType queryType = QueryType_Prefix;
 
 static CCL_bibset bibset;               /* CCL bibset handle */
-static cql_transform_t cqltrans;       /* CQL qualifier-set handle */
+static cql_transform_t cqltrans;       /* CQL context-set handle */
 
 #if HAVE_READLINE_COMPLETION_OVER
 
@@ -1131,7 +1131,7 @@ static int send_srw(Z_SRW_PDU *sr)
     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},
+        {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
         {0, 0, 0}
     };
     ODR o = odr_createmem(ODR_ENCODE);
@@ -1189,7 +1189,7 @@ static int send_srw(Z_SRW_PDU *sr)
     ret = z_soap_codec_enc(o, &p,
                            &gdu->u.HTTP_Request->content_buf,
                            &gdu->u.HTTP_Request->content_len, h,
-                           charset);
+                           charset, 0);
 
     if (z_GDU(out, &gdu, 0, 0))
     {
@@ -1197,8 +1197,12 @@ static int send_srw(Z_SRW_PDU *sr)
         char *buf_out;
         int len_out;
         int r;
-       if (apdu_file && !z_GDU(print, &gdu, 0, 0))
-            printf ("Failed to print outgoing APDU\n");
+       if (apdu_file)
+       {
+           if (!z_GDU(print, &gdu, 0, 0))
+               printf ("Failed to print outgoing APDU\n");
+           odr_reset(print);
+       }
         buf_out = odr_getbuf(out, &len_out, 0);
         
         /* we don't odr_reset(out), since we may need the buffer again */
@@ -1238,8 +1242,8 @@ static int send_SRW_searchRequest(const char *arg)
     sr = yaz_srw_get(out, Z_SRW_searchRetrieve_request);
     sr->u.request->query_type = Z_SRW_query_type_cql;
     sr->u.request->query.cql = odr_strdup(out, arg);
-    if (schema)
-        sr->u.request->recordSchema = schema;
+    if (record_schema)
+        sr->u.request->recordSchema = record_schema;
     return send_srw(sr);
 }
 #endif
@@ -2133,6 +2137,28 @@ static int cmd_itemorder(const char *arg)
     return 2;
 }
 
+static int cmd_explain(const char *arg)
+{
+    if (protocol != PROTO_HTTP)
+       return 0;
+#if HAVE_XML2
+    if (!conn)
+       cmd_open(0);
+    if (conn)
+    {
+       Z_SRW_PDU *sr = 0;
+       
+       setno = 1;
+       
+       /* save this for later .. when fetching individual records */
+       sr = yaz_srw_get(out, Z_SRW_explain_request);
+       send_srw(sr);
+       return 2;
+    }
+#endif
+    return 0;
+}
+    
 static int cmd_find(const char *arg)
 {
     if (!*arg)
@@ -2145,6 +2171,8 @@ static int cmd_find(const char *arg)
 #if HAVE_XML2
         if (!conn)
             cmd_open(0);
+       if (!conn)
+           return 0;
         if (!send_SRW_searchRequest(arg))
             return 0;
 #else
@@ -2283,13 +2311,13 @@ static int send_presentRequest(const char *arg)
     req->preferredRecordSyntax =
         odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
 
-    if (schema)
+    if (record_schema)
     {
         oident prefschema;
 
         prefschema.proto = protocol;
         prefschema.oclass = CLASS_SCHEMA;
-        prefschema.value = oid_getvalbyname(schema);
+        prefschema.value = oid_getvalbyname(record_schema);
 
         req->recordComposition = &compo;
         compo.which = Z_RecordComp_complex;
@@ -2350,8 +2378,8 @@ static int send_SRW_presentRequest(const char *arg)
     parse_show_args(arg, setstring, &setno, &nos);
     sr->u.request->startRecord = odr_intdup(out, setno);
     sr->u.request->maximumRecords = odr_intdup(out, nos);
-    if (schema)
-        sr->u.request->recordSchema = schema;
+    if (record_schema)
+        sr->u.request->recordSchema = record_schema;
     return send_srw(sr);
 }
 #endif
@@ -2411,6 +2439,8 @@ static int cmd_show(const char *arg)
 #if HAVE_XML2
         if (!conn)
             cmd_open(0);
+       if (!conn)
+           return 0;
         if (!send_SRW_presentRequest(arg))
             return 0;
 #else
@@ -2727,10 +2757,10 @@ int cmd_scan(const char *arg)
 
 int cmd_schema(const char *arg)
 {
-    xfree(schema);
-    schema = 0;
+    xfree(record_schema);
+    record_schema = 0;
     if (arg && *arg)
-        schema = xstrdup(arg);
+        record_schema = xstrdup(arg);
     return 1;
 }
 
@@ -3248,6 +3278,28 @@ struct timeval tv_start, tv_end;
 #endif
 
 #if HAVE_XML2
+static void handle_srw_record(Z_SRW_record *rec)
+{
+    if (rec->recordPosition)
+    {
+       printf ("pos=%d", *rec->recordPosition);
+       setno = *rec->recordPosition + 1;
+    }
+    if (rec->recordSchema)
+       printf (" schema=%s", rec->recordSchema);
+    printf ("\n");
+    if (rec->recordData_buf && rec->recordData_len)
+       fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout);
+    else
+       printf ("No data!");
+    printf("\n");
+}
+
+static void handle_srw_explain_response(Z_SRW_explainResponse *res)
+{
+    handle_srw_record(&res->record);
+}
+
 static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
 {
     int i;
@@ -3266,23 +3318,7 @@ static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
     if (res->numberOfRecords)
         printf ("Number of hits: %d\n", *res->numberOfRecords);
     for (i = 0; i<res->num_records; i++)
-    {
-        Z_SRW_record *rec = res->records + i;
-
-        if (rec->recordPosition)
-        {
-            printf ("pos=%d", *rec->recordPosition);
-            setno = *rec->recordPosition + 1;
-        }
-        if (rec->recordSchema)
-            printf (" schema=%s", rec->recordSchema);
-        printf ("\n");
-        if (rec->recordData_buf && rec->recordData_len)
-        {
-            fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout);
-            printf ("\n");
-        }
-    }
+       handle_srw_record(res->records + i);
 }
 
 static void http_response(Z_HTTP_Response *hres)
@@ -3297,7 +3333,7 @@ static void http_response(Z_HTTP_Response *hres)
         Z_SOAP *soap_package = 0;
         ODR o = odr_createmem(ODR_DECODE);
         Z_SOAP_Handler soap_handlers[2] = {
-            {"http://www.loc.gov/zing/srw/v1.0/", 0,
+            {"http://www.loc.gov/zing/srw/", 0,
              (Z_SOAP_fun) yaz_srw_codec},
             {0, 0, 0}
         };
@@ -3310,10 +3346,12 @@ static void http_response(Z_HTTP_Response *hres)
             Z_SRW_PDU *sr = soap_package->u.generic->p;
             if (sr->which == Z_SRW_searchRetrieve_response)
                 handle_srw_response(sr->u.response);
+            else if (sr->which == Z_SRW_explain_response)
+                handle_srw_explain_response(sr->u.explain_response);
             else
                 ret = -1;
         }
-        else if (!ret && (soap_package->which == Z_SOAP_fault
+        else if (soap_package && (soap_package->which == Z_SOAP_fault
                           || soap_package->which == Z_SOAP_error))
         {
             printf ("HTTP Error Status=%d\n", hres->code);
@@ -3321,6 +3359,9 @@ static void http_response(Z_HTTP_Response *hres)
                     soap_package->u.fault->fault_code);
             printf ("SOAP Fault string %s\n", 
                     soap_package->u.fault->fault_string);
+           if (soap_package->u.fault->details)
+               printf ("SOAP Details %s\n", 
+                       soap_package->u.fault->details);
         }
         else
             ret = -1;
@@ -3409,7 +3450,10 @@ void wait_and_handle_response()
             odr_dumpBER(f, netbuffer, res);
             fprintf(f, "---------\n");
             if (apdu_file)
+           {
                 z_GDU(print, &gdu, 0, 0);
+               odr_reset(print);
+           }
             if (conn && cs_more(conn))
                 continue;
            break;
@@ -3555,7 +3599,6 @@ int cmd_set_otherinfo(const char* args)
         printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
     }
     
-    
     oidval = oid_getvalbyname (oid);
     if(oidval == -1 ) {
         printf("Error in set_otherinfo command unknown oid %s \n",oid);
@@ -3658,7 +3701,7 @@ int cmd_list_all(const char* args) {
     
     /* print present related options */
     printf("Format               : %s\n",yaz_z3950_oid_value_to_str(recordsyntax,CLASS_RECSYN));
-    printf("Schema               : %s\n",schema);
+    printf("Schema               : %s\n",record_schema ? record_schema : "not set");
     printf("Elements             : %s\n",elementSetNames?elementSetNames->u.generic:"");
     
     /* loging options */
@@ -3769,6 +3812,7 @@ static struct {
     {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
     {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
     {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
+    {"explain", cmd_explain, "", NULL, 0, NULL},
     {"help", cmd_help, "", NULL,0,NULL},
     {0,0,0,0,0,0}
 };