Optional CCL module. Moved atoi_n to marcdisp.h from yaz-util.h.
[yaz-moved-to-github.git] / client / client.c
index 4ff4a15..7fe8df2 100644 (file)
@@ -1,10 +1,46 @@
 /*
- * Copyright (c) 1995-2000, Index Data
+ * Copyright (c) 1995-2001, Index Data
  * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: client.c,v $
- * Revision 1.108  2000-11-23 10:58:32  adam
+ * 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
+ * Added odr_intdup. Ztest server returns TaskPackage for ItemUpdate.
+ *
+ * Revision 1.116  2001/03/21 12:43:36  adam
+ * Implemented cs_create_host. Better error reporting for SSL comstack.
+ *
+ * Revision 1.115  2001/03/13 18:10:58  adam
+ * Added option -c to set CCL config file.
+ *
+ * Revision 1.114  2001/02/21 13:46:53  adam
+ * C++ fixes.
+ *
+ * Revision 1.113  2001/02/21 09:41:15  adam
+ * Fixed problem with old codecs.
+ *
+ * Revision 1.112  2001/02/20 11:25:32  adam
+ * Added ill_get_APDU and ill_get_Cancel.
+ *
+ * Revision 1.111  2001/01/30 15:52:48  ja7
+ * added command for setting packageName in ES packages!
+ * command is named packagename default is NULL for server suplyed package name
+ *
+ * Revision 1.110  2001/01/29 11:19:05  adam
+ * Clients prints options in InitResponse.
+ *
+ * Revision 1.109  2001/01/22 10:41:47  adam
+ * Multiple attribute may be specified for sorging.
+ *
+ * Revision 1.108  2000/11/23 10:58:32  adam
  * SSL comstack support. Separate POSIX thread support library.
  *
  * Revision 1.107  2000/11/16 13:11:07  adam
 
 #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
 
@@ -411,6 +447,8 @@ static NMEM session_mem = NULL;      /* memory handle for init-response */
 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* esPackageName = 0;
 
 static char last_cmd[100] = "?";
 static FILE *marcdump = 0;
@@ -424,7 +462,7 @@ typedef enum {
 
 static QueryType queryType = QueryType_Prefix;
 
-#if CCL2RPN
+#if YAZ_MODULE_ccl
 static CCL_bibset bibset;               /* CCL bibset handle */
 #endif
 
@@ -539,6 +577,35 @@ static int process_initResponse(Z_InitResponse *res)
         }
         odr_reset (print);
     }
+    printf ("Options:");
+    if (ODR_MASK_GET(res->options, Z_Options_search))
+        printf (" search");
+    if (ODR_MASK_GET(res->options, Z_Options_present))
+        printf (" present");
+    if (ODR_MASK_GET(res->options, Z_Options_delSet))
+        printf (" delSet");
+    if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
+        printf (" resourceReport");
+    if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
+        printf (" resourceCtrl");
+    if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
+        printf (" accessCtrl");
+    if (ODR_MASK_GET(res->options, Z_Options_scan))
+        printf (" scan");
+    if (ODR_MASK_GET(res->options, Z_Options_sort))
+        printf (" sort");
+    if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
+        printf (" extendedServices");
+    if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
+        printf (" level1Segmentation");
+    if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
+        printf (" level2Segmentation");
+    if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
+        printf (" concurrentOperations");
+    if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
+        printf (" namedResultSets");
+    printf ("\n");
+    fflush (stdout);
     return 0;
 }
 
@@ -576,7 +643,6 @@ int cmd_open(char *arg)
 {
     void *add;
     char type_and_host[101], base[101];
-    char *host = 0;
     CS_TYPE t;
 
     if (conn)
@@ -595,44 +661,28 @@ int cmd_open(char *arg)
     base[0] = '\0';
     if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
         return 0;
-    if (strncmp (type_and_host, "tcp:", 4) == 0)
-        host = type_and_host + 4;
-    else if (strncmp (type_and_host, "ssl:", 4) == 0)
-    {
-#if HAVE_OPENSSL_SSL_H
-       t = ssl_type;
-#else
-       printf ("SSL not supported\n");
-#endif
-        host = type_and_host + 4;
-    }
-    else
-        host = type_and_host;
-    if (*base)
-        cmd_base (base);
-    protocol = PROTO_Z3950;
 
-    if (!(conn = cs_create(t, 1, protocol)))
-    {
-        perror("cs_create");
-        return 0;
-    }
-    if (!(add = cs_straddr(conn, host)))
+    conn = cs_create_host(type_and_host, 1, &add);
+    if (!conn)
     {
-        perror(arg);
-        return 0;
+       printf ("Couldn't create comstack\n");
+       return 0;
     }
     printf("Connecting...");
     fflush(stdout);
     if (cs_connect(conn, add) < 0)
     {
-        perror("connect");
+       printf ("error = %s\n", cs_strerror(conn));
+       if (conn->cerrno == CSYSERR)
+           perror("system");
         cs_close(conn);
         conn = 0;
         return 0;
     }
     printf("Ok.\n");
     send_initRequest();
+    if (*base)
+        cmd_base (base);
     return 2;
 }
 
@@ -975,7 +1025,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
@@ -983,7 +1033,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);
@@ -1051,7 +1101,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);
@@ -1270,7 +1320,7 @@ void process_ESResponse(Z_ExtendedServicesResponse *res)
     }
 }
 
-#ifdef ASN_COMPILED
+#if YAZ_MODULE_ill
 
 const char *get_ill_element (void *clientData, const char *element)
 {
@@ -1324,7 +1374,8 @@ static Z_External *create_external_itemRequest()
         
         r->u.single_ASN1_type = (Odr_oct *)
             odr_malloc (out, sizeof(*r->u.single_ASN1_type));
-        r->u.single_ASN1_type->buf = odr_malloc (out, item_request_size);
+        r->u.single_ASN1_type->buf = (unsigned char *)
+           odr_malloc (out, item_request_size);
         r->u.single_ASN1_type->len = item_request_size;
         r->u.single_ASN1_type->size = item_request_size;
         memcpy (r->u.single_ASN1_type->buf, item_request_buf,
@@ -1333,18 +1384,13 @@ static Z_External *create_external_itemRequest()
     }
     return r;
 }
-#else
-static Z_External *create_external_itemRequest()
-{
-    return 0;
-}
 #endif
 
-#ifdef ASN_COMPILED
-static Z_External *create_external_ILLRequest()
+#ifdef YAZ_MODULE_ill
+static Z_External *create_external_ILL_APDU(int which)
 {
     struct ill_get_ctl ctl;
-    ILL_Request *req;
+    ILL_APDU *ill_apdu;
     Z_External *r = 0;
     int ill_request_size = 0;
     char *ill_request_buf = 0;
@@ -1353,14 +1399,14 @@ static Z_External *create_external_ILLRequest()
     ctl.clientData = 0;
     ctl.f = get_ill_element;
 
-    req = ill_get_ILLRequest(&ctl, "ill", 0);
+    ill_apdu = ill_get_APDU(&ctl, "ill", 0);
 
-    if (!ill_Request (out, &req, 0, 0))
+    if (!ill_APDU (out, &ill_apdu, 0, 0))
     {
         if (apdu_file)
         {
             printf ("-------------------\n");
-            ill_Request(print, &req, 0, 0);
+            ill_APDU(print, &ill_apdu, 0, 0);
             odr_reset(print);
             printf ("-------------------\n");
         }
@@ -1387,7 +1433,8 @@ static Z_External *create_external_ILLRequest()
         
         r->u.single_ASN1_type = (Odr_oct *)
             odr_malloc (out, sizeof(*r->u.single_ASN1_type));
-        r->u.single_ASN1_type->buf = odr_malloc (out, ill_request_size);
+        r->u.single_ASN1_type->buf = (unsigned char *)
+           odr_malloc (out, ill_request_size);
         r->u.single_ASN1_type->len = ill_request_size;
         r->u.single_ASN1_type->size = ill_request_size;
         memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
@@ -1395,11 +1442,6 @@ static Z_External *create_external_ILLRequest()
     }
     return r;
 }
-#else
-static Z_External *create_external_ILLRequest()
-{
-    return 0;
-}
 #endif
 
 
@@ -1450,6 +1492,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;
 
+#if YAZ_MODULE_ill
     if (!strcmp (type, "item") || !strcmp(type, "2"))
     {
         printf ("using item-request\n");
@@ -1460,16 +1503,31 @@ static Z_External *create_ItemOrderExternal(const char *type, int itemno)
     {
         printf ("using ILL-request\n");
         r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 
-            create_external_ILLRequest();
+            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;
+
+#else
+    r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
+#endif
     return r;
 }
 
 static int send_itemorder(const char *type, int itemno)
 {
-    Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
+    Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
     oident ItemOrderRequest;
 
@@ -1477,7 +1535,7 @@ static int send_itemorder(const char *type, int itemno)
     ItemOrderRequest.oclass = CLASS_EXTSERV;
     ItemOrderRequest.value = VAL_ITEMORDER;
     req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
-    req->packageName = "1.Extendedserveq";
+    req->packageName = esPackageName;
 
     req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
 
@@ -1504,7 +1562,7 @@ static int cmd_update(char *arg)
     update_oid.value = VAL_DBUPDATE;
     oid_ent_to_oid (&update_oid, oid);
     req->packageType = odr_oiddup(out,oid);
-    req->packageName = "1.Extendedserveq";
+    req->packageName = esPackageName;
 
     r = req->taskSpecificParameters = (Z_External *)
         odr_malloc (out, sizeof(*r));
@@ -1820,9 +1878,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;
@@ -1833,7 +1891,7 @@ int send_scanrequest(const char *query, int pp, int num, const char *term)
         if (error)
         {
             printf("CCL ERROR: %s\n", ccl_err_msg(error));
-            return 0;
+            return -1;
         }
         use_rpn = 0;
         bib1.proto = PROTO_Z3950;
@@ -1843,7 +1901,7 @@ int send_scanrequest(const char *query, int pp, int num, const char *term)
         if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
         {
             printf("Couldn't convert CCL to Scan term\n");
-            return 0;
+            return -1;
         }
         ccl_rpn_delete (rpn);
     }
@@ -1861,7 +1919,7 @@ int send_scanrequest(const char *query, int pp, int num, const char *term)
             req->termListAndStartPoint->term->u.general)
         {
             req->termListAndStartPoint->term->u.general->buf =
-                odr_strdup(out, term);
+                (unsigned char *) odr_strdup(out, term);
             req->termListAndStartPoint->term->u.general->len =
                 req->termListAndStartPoint->term->u.general->size =
                 strlen(term);
@@ -1883,7 +1941,7 @@ int send_sortrequest(char *arg, int newset)
     Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
         odr_malloc (out, sizeof(*sksl));
     char setstring[32];
-    char sort_string[32], sort_flags[32];
+    char sort_string_buf[32], sort_flags[32];
     int off;
     int oid[OID_SIZE];
     oident bib1;
@@ -1922,11 +1980,12 @@ int send_sortrequest(char *arg, int newset)
     bib1.proto = protocol;
     bib1.oclass = CLASS_ATTSET;
     bib1.value = VAL_BIB1;
-    while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2 
+    while ((sscanf (arg, "%31s %31s%n", sort_string_buf, sort_flags, &off)) == 2 
            && off > 1)
     {
         int i;
         char *sort_string_sep;
+       char *sort_string = sort_string_buf;
         Z_SortKeySpec *sks = (Z_SortKeySpec *)odr_malloc (out, sizeof(*sks));
         Z_SortKey *sk = (Z_SortKey *)odr_malloc (out, sizeof(*sk));
 
@@ -1938,24 +1997,37 @@ int send_sortrequest(char *arg, int newset)
         
         if ((sort_string_sep = strchr (sort_string, '=')))
         {
-            Z_AttributeElement *el = (Z_AttributeElement *)odr_malloc (out, sizeof(*el));
+           int i = 0;
             sk->which = Z_SortKey_sortAttributes;
             sk->u.sortAttributes =
                 (Z_SortAttributes *)odr_malloc (out, sizeof(*sk->u.sortAttributes));
             sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
             sk->u.sortAttributes->list =
                 (Z_AttributeList *)odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
-            sk->u.sortAttributes->list->num_attributes = 1;
             sk->u.sortAttributes->list->attributes =
-                (Z_AttributeElement **)odr_malloc (out,
-                            sizeof(*sk->u.sortAttributes->list->attributes));
-            sk->u.sortAttributes->list->attributes[0] = el;
-            el->attributeSet = 0;
-            el->attributeType = (int *)odr_malloc (out, sizeof(*el->attributeType));
-            *el->attributeType = atoi (sort_string);
-            el->which = Z_AttributeValue_numeric;
-            el->value.numeric = (int *)odr_malloc (out, sizeof(*el->value.numeric));
-            *el->value.numeric = atoi (sort_string_sep + 1);
+                (Z_AttributeElement **)
+               odr_malloc (out, 10 *
+                           sizeof(*sk->u.sortAttributes->list->attributes));
+           while (i < 10 && sort_string && sort_string_sep)
+           {
+               Z_AttributeElement *el = (Z_AttributeElement *)
+                   odr_malloc (out, sizeof(*el));
+               sk->u.sortAttributes->list->attributes[i] = el;
+               el->attributeSet = 0;
+               el->attributeType = (int *)odr_malloc (out, sizeof(*el->attributeType));
+               *el->attributeType = atoi (sort_string);
+               el->which = Z_AttributeValue_numeric;
+               el->value.numeric = (int *)odr_malloc (out, sizeof(*el->value.numeric));
+               *el->value.numeric = atoi (sort_string_sep + 1);
+               i++;
+               sort_string = strchr(sort_string, ',');
+               if (sort_string)
+               {
+                   sort_string++;
+                   sort_string_sep = strchr (sort_string, '=');
+               }
+           }
+            sk->u.sortAttributes->list->num_attributes = i;
         }
         else
         {
@@ -2230,7 +2302,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
@@ -2239,7 +2311,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;
@@ -2275,9 +2347,20 @@ int cmd_close(char *arg)
     return 2;
 }
 
+int cmd_packagename(char* arg) {
+    xfree (esPackageName);
+    esPackageName = NULL;
+    if (*arg)
+    {
+        esPackageName = (char *) xmalloc (strlen(arg)+1);
+        strcpy (esPackageName, arg);
+    }
+    return 1;
+};
+
 static void initialize(void)
 {
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     FILE *inf;
 #endif
     nmem_init();
@@ -2292,9 +2375,9 @@ static void initialize(void)
     if (apdu_file)
         odr_setprint(print, apdu_file);
 
-#if CCL2RPN
+#if YAZ_MODULE_ccl
     bibset = ccl_qual_mk (); 
-    inf = fopen ("default.bib", "r");
+    inf = fopen (ccl_fields, "r");
     if (inf)
     {
         ccl_qual_file (bibset, inf);
@@ -2336,6 +2419,7 @@ static int client(int wait)
         {"refid", cmd_refid, "<id>"},
         {"itemorder", cmd_itemorder, "ill|item <itemno>"},
         {"update", cmd_update, "<item>"},
+       {"packagename", cmd_packagename, "<packagename>"},
 #ifdef ASN_COMPILED
         /* Server Admin Functions */
         {"adm-reindex", cmd_adm_reindex, "<database-name>"},
@@ -2543,7 +2627,7 @@ int main(int argc, char **argv)
     int ret;
     int opened = 0;
 
-    while ((ret = options("a:m:v:", argv, argc, &arg)) != -2)
+    while ((ret = options("c:a:m:v:", argv, argc, &arg)) != -2)
     {
         switch (ret)
         {
@@ -2562,6 +2646,10 @@ int main(int argc, char **argv)
                 exit (1);
             }
             break;
+       case 'c':
+           strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
+           ccl_fields[sizeof(ccl_fields)-1] = '\0';
+           break;
         case 'a':
             if (!strcmp(arg, "-"))
                 apdu_file=stderr;
@@ -2573,7 +2661,7 @@ int main(int argc, char **argv)
             break;
         default:
             fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
-                             "[<server-addr>]\n",
+                             "[-c cclfields] [<server-addr>]\n",
                      prog);
             exit (1);
         }