Client doesn't set VAL_PROXY otherinfo unless -p is specified.
[yaz-moved-to-github.git] / client / client.c
index 9a43b95..00819c2 100644 (file)
@@ -3,7 +3,40 @@
  * See the file LICENSE for details.
  *
  * $Log: client.c,v $
- * Revision 1.118  2001-03-27 14:48:06  adam
+ * Revision 1.126  2001-08-06 20:22:23  adam
+ * Client doesn't set VAL_PROXY otherinfo unless -p is specified.
+ *
+ * Revision 1.125  2001/07/19 19:51:41  adam
+ * Added typecasts to make C++ happy.
+ *
+ * Revision 1.124  2001/07/04 20:13:51  ja7
+ * Added new commend "proxy" for ysing a yaz-proxy to connect to the target
+ * Added new command line option -p for setting the proxy host
+ *
+ * Revision 1.123  2001/06/11 12:54:47  heikki
+ * Displayng diags in EsResponse, even if they should not be there...
+ *
+ * Revision 1.122  2001/05/16 07:27:29  adam
+ * Reference id set for InitRequest.
+ *
+ * Revision 1.121  2001/04/22 12:26:17  ja7
+ * if Compiled with Command line history a open command is pushed into
+ * the command history if yaz-client is startet with a server on the
+ * command line. eg
+ *   yaz-clinet tcp:server:port/database
+ * hash the commend
+ *   open tcp:server:port/database
+ * in the command history on startup
+ *
+ * Added the refid set with setrefid to the update APDU
+ *
+ * Revision 1.120  2001/04/06 12:26:46  adam
+ * Optional CCL module. Moved atoi_n to marcdisp.h from yaz-util.h.
+ *
+ * Revision 1.119  2001/04/05 13:08:48  adam
+ * New configure options: --enable-module.
+ *
+ * Revision 1.118  2001/03/27 14:48:06  adam
  * Fixed scan for bad CCL.
  *
  * Revision 1.117  2001/03/25 21:55:12  adam
 #include <yaz/proto.h>
 #include <yaz/marcdisp.h>
 #include <yaz/diagbib1.h>
+#include <yaz/otherinfo.h>
 
 #include <yaz/pquery.h>
 
-#ifdef ASN_COMPILED
+#if YAZ_MODULE_ill
 #include <yaz/ill.h>
 #endif
 
-#if CCL2RPN
+#if YAZ_MODULE_ccl
 #include <yaz/yaz-ccl.h>
 #endif
 
@@ -442,9 +476,10 @@ 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";
-char* esPackageName = 0;
+static char* esPackageName = 0;
+static char* yazProxy = 0;
 
-static char last_cmd[100] = "?";
+static char last_cmd[32] = "?";
 static FILE *marcdump = 0;
 static char *refid = NULL;
 
@@ -456,7 +491,7 @@ typedef enum {
 
 static QueryType queryType = QueryType_Prefix;
 
-#if CCL2RPN
+#if YAZ_MODULE_ccl
 static CCL_bibset bibset;               /* CCL bibset handle */
 #endif
 
@@ -512,7 +547,7 @@ static Z_ReferenceId *set_refid (ODR out)
 
 /* INIT SERVICE ------------------------------- */
 
-static void send_initRequest()
+static void send_initRequest(const char* type_and_host)
 {
     Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
     Z_InitRequest *req = apdu->u.initRequest;
@@ -535,6 +570,12 @@ static void send_initRequest()
 
     req->idAuthentication = auth;
 
+    req->referenceId = set_refid (out);
+
+    if (yazProxy) 
+        yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
+        1, type_and_host);
+    
     send_apdu(apdu);
     printf("Sent initrequest.\n");
 }
@@ -656,7 +697,15 @@ int cmd_open(char *arg)
     if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
         return 0;
 
-    conn = cs_create_host(type_and_host, 1, &add);
+    if(yazProxy) 
+    {
+       conn = cs_create_host(yazProxy, 1, &add);
+    } 
+    else 
+    { 
+       conn = cs_create_host(type_and_host, 1, &add);
+    }
+       
     if (!conn)
     {
        printf ("Couldn't create comstack\n");
@@ -674,7 +723,8 @@ int cmd_open(char *arg)
         return 0;
     }
     printf("Ok.\n");
-    send_initRequest();
+
+    send_initRequest(type_and_host);
     if (*base)
         cmd_base (base);
     return 2;
@@ -1019,7 +1069,7 @@ static int send_searchRequest(char *arg)
     Z_SearchRequest *req = apdu->u.searchRequest;
     Z_Query query;
     int oid[OID_SIZE];
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     struct ccl_rpn_node *rpn = NULL;
     int error, pos;
 #endif
@@ -1027,7 +1077,7 @@ static int send_searchRequest(char *arg)
     Z_RPNQuery *RPNquery;
     Odr_oct ccl_query;
 
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     if (queryType == QueryType_CCL2RPN)
     {
         rpn = ccl_find_str(bibset, arg, &error, &pos);
@@ -1095,7 +1145,7 @@ static int send_searchRequest(char *arg)
         ccl_query.buf = (unsigned char*) arg;
         ccl_query.len = strlen(arg);
         break;
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     case QueryType_CCL2RPN:
         query.which = Z_Query_type_1;
         RPNquery = ccl_rpn_query(out, rpn);
@@ -1312,9 +1362,14 @@ void process_ESResponse(Z_ExtendedServicesResponse *res)
         display_diagrecs(res->diagnostics, res->num_diagnostics);
         break;
     }
+    if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
+        (res->num_diagnostics != 0) ) {
+        display_diagrecs(res->diagnostics, res->num_diagnostics);
+    }
+
 }
 
-#ifdef ASN_COMPILED
+#if YAZ_MODULE_ill
 
 const char *get_ill_element (void *clientData, const char *element)
 {
@@ -1378,14 +1433,9 @@ static Z_External *create_external_itemRequest()
     }
     return r;
 }
-#else
-static Z_External *create_external_itemRequest()
-{
-    return 0;
-}
 #endif
 
-#ifdef ASN_COMPILED
+#ifdef YAZ_MODULE_ill
 static Z_External *create_external_ILL_APDU(int which)
 {
     struct ill_get_ctl ctl;
@@ -1441,11 +1491,6 @@ static Z_External *create_external_ILL_APDU(int which)
     }
     return r;
 }
-#else
-static Z_External *create_external_ILLRequest()
-{
-    return 0;
-}
 #endif
 
 
@@ -1496,7 +1541,7 @@ static Z_External *create_ItemOrderExternal(const char *type, int itemno)
         (int *) odr_malloc(out, sizeof(int));
     *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
 
-#ifdef ASN_COMPILED
+#if YAZ_MODULE_ill
     if (!strcmp (type, "item") || !strcmp(type, "2"))
     {
         printf ("using item-request\n");
@@ -1509,6 +1554,17 @@ static Z_External *create_ItemOrderExternal(const char *type, int itemno)
         r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 
             create_external_ILL_APDU(ILL_APDU_ILL_Request);
     }
+    else if (!strcmp(type, "xml") || !strcmp(type, "3"))
+    {
+       const char *xml_buf =
+               "<itemorder>\n"
+               "  <type>request</type>\n"
+               "  <libraryNo>000200</libraryNo>\n"
+               "  <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
+               "</itemorder>";
+        r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
+            z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
+    }
     else
         r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
 
@@ -1556,6 +1612,8 @@ static int cmd_update(char *arg)
     oid_ent_to_oid (&update_oid, oid);
     req->packageType = odr_oiddup(out,oid);
     req->packageName = esPackageName;
+    
+    req->referenceId = set_refid (out);
 
     r = req->taskSpecificParameters = (Z_External *)
         odr_malloc (out, sizeof(*r));
@@ -1871,9 +1929,9 @@ int send_scanrequest(const char *query, int pp, int num, const char *term)
     Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
     Z_ScanRequest *req = apdu->u.scanRequest;
     int use_rpn = 1;
+#if YAZ_MODULE_ccl
     int oid[OID_SIZE];
     
-#if CCL2RPN
     if (queryType == QueryType_CCL2RPN)
     {
         oident bib1;
@@ -2295,7 +2353,7 @@ int cmd_querytype (char *arg)
         queryType = QueryType_CCL;
     else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
         queryType = QueryType_Prefix;
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
         queryType = QueryType_CCL2RPN;
 #endif
@@ -2304,7 +2362,7 @@ int cmd_querytype (char *arg)
         printf ("Querytype must be one of:\n");
         printf (" prefix         - Prefix query\n");
         printf (" ccl            - CCL query\n");
-#if CCL2RPN
+#if YAZ_MODULE_ccl
         printf (" ccl2rpn        - CCL query converted to RPN\n");
 #endif
         return 0;
@@ -2351,9 +2409,20 @@ int cmd_packagename(char* arg) {
     return 1;
 };
 
+int cmd_proxy(char* arg) {
+    xfree (yazProxy);
+    yazProxy = NULL;
+    if (*arg)
+    {
+        yazProxy = (char *) xmalloc (strlen(arg)+1);
+        strcpy (yazProxy, arg);
+    } 
+    return 1;
+};
+
 static void initialize(void)
 {
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     FILE *inf;
 #endif
     nmem_init();
@@ -2368,7 +2437,7 @@ static void initialize(void)
     if (apdu_file)
         odr_setprint(print, apdu_file);
 
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     bibset = ccl_qual_mk (); 
     inf = fopen (ccl_fields, "r");
     if (inf)
@@ -2413,6 +2482,7 @@ static int client(int wait)
         {"itemorder", cmd_itemorder, "ill|item <itemno>"},
         {"update", cmd_update, "<item>"},
        {"packagename", cmd_packagename, "<packagename>"},
+       {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
 #ifdef ASN_COMPILED
         /* Server Admin Functions */
         {"adm-reindex", cmd_adm_reindex, "<database-name>"},
@@ -2442,7 +2512,7 @@ static int client(int wait)
 #ifdef USE_SELECT
         fd_set input;
 #endif
-        char line[1024], word[1024], arg[1024];
+        char line[1024], word[32], arg[1024];
         
 #ifdef USE_SELECT
         FD_ZERO(&input);
@@ -2485,7 +2555,7 @@ static int client(int wait)
             gettimeofday (&tv_start, 0);
 #endif
 
-            if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
+            if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
             {
                 strcpy(word, last_cmd);
                 *arg = '\0';
@@ -2620,7 +2690,7 @@ int main(int argc, char **argv)
     int ret;
     int opened = 0;
 
-    while ((ret = options("c:a:m:v:", argv, argc, &arg)) != -2)
+    while ((ret = options("c:a:m:v:p:", argv, argc, &arg)) != -2)
     {
         switch (ret)
         {
@@ -2628,8 +2698,17 @@ int main(int argc, char **argv)
             if (!opened)
             {
                 initialize ();
-                if (cmd_open (arg) == 2)
-                    opened = 1;
+                if (cmd_open (arg) == 2) {
+#if HAVE_READLINE_HISTORY_H
+                 char* tmp=(char*)malloc(strlen(arg)+6);
+                 *tmp=0;
+                 strcat(tmp,"open ");
+                 strcat(tmp,arg);
+                 add_history(tmp);
+                 free(tmp);
+#endif
+                 opened = 1;
+               };
             }
             break;
         case 'm':
@@ -2649,12 +2728,15 @@ int main(int argc, char **argv)
             else
                 apdu_file=fopen(arg, "a");
             break;
+       case 'p':
+           yazProxy=strdup(arg);
+           break;
         case 'v':
             yaz_log_init (yaz_log_mask_str(arg), "", NULL);
             break;
         default:
             fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
-                             "[-c cclfields] [<server-addr>]\n",
+                             "[-c cclfields] [-p <proxy-addr>] [<server-addr>]\n",
                      prog);
             exit (1);
         }