yaz-client: fail bad show args
[yaz-moved-to-github.git] / client / client.c
index 87a87e6..1230cdb 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2009 Index Data
+ * Copyright (C) 1995-2010 Index Data
  * See the file LICENSE for details.
  */
 /** \file client.c
@@ -141,7 +141,7 @@ static int scan_stepSize = 0;
 static int scan_position = 1;
 static int scan_size = 20;
 static char cur_host[200];
-static int last_hit_count = 0;
+static Odr_int last_hit_count = 0;
 
 typedef enum {
     QueryType_Prefix,
@@ -1200,6 +1200,25 @@ static int send_srw(Z_SRW_PDU *sr)
 
     gdu = z_get_HTTP_Request_host_path(out, host_port, path);
 
+    if (auth)
+    {
+        if (auth->which == Z_IdAuthentication_open)
+        {
+            char **darray;
+            int num;
+            nmem_strsplit(out->mem, "/", auth->u.open, &darray, &num);
+            if (num >= 1)
+                sr->username = darray[0];
+            if (num >= 2)
+                sr->password = darray[1];
+        }
+        else if (auth->which == Z_IdAuthentication_idPass)
+        {
+            sr->username = auth->u.idPass->userId;
+            sr->password = auth->u.idPass->password;
+        }
+    }
+
     if (!yaz_matchstr(sru_method, "get"))
     {
         yaz_sru_get_encode(gdu->u.HTTP_Request, sr, out, charset);
@@ -1945,7 +1964,7 @@ static Z_External *create_external_itemRequest(void)
     return r;
 }
 
-static Z_External *create_external_ILL_APDU(int which)
+static Z_External *create_external_ILL_APDU(void)
 {
     struct ill_get_ctl ctl;
     ILL_APDU *ill_apdu;
@@ -2047,7 +2066,7 @@ static Z_External *create_ItemOrderExternal(const char *type, int itemno,
     {
         printf("using ILL-request\n");
         r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
-            create_external_ILL_APDU(ILL_APDU_ILL_Request);
+            create_external_ILL_APDU();
     }
     else if (!strcmp(type, "xml") || !strcmp(type, "3"))
     {
@@ -2764,36 +2783,50 @@ static int cmd_setnames(const char *arg)
 
 /* PRESENT SERVICE ----------------------------- */
 
-static void parse_show_args(const char *arg_c, char *setstring,
-                            Odr_int *start, Odr_int *number)
+static int parse_show_args(const char *arg_c, char *setstring,
+                           Odr_int *start, Odr_int *number)
 {
-    char arg[40];
-    char *p;
+    char *end_ptr;
 
-    strncpy(arg, arg_c, sizeof(arg)-1);
-    arg[sizeof(arg)-1] = '\0';
+    if (setnumber >= 0)
+        sprintf(setstring, "%d", setnumber);
+    else
+        *setstring = '\0';
 
-    if ((p = strchr(arg, '+')))
+    if (!strcmp(arg_c, "all"))
     {
-        *number = atoi(p + 1);
-        *p = '\0';
+        *number = last_hit_count;
+        *start = 1;
     }
-    if (*arg)
+    *start = odr_strtol(arg_c, &end_ptr, 10);
+    if (end_ptr == arg_c || *end_ptr == '\0')
+        return 1;
+    while (isspace(*(unsigned char *)end_ptr))
+        end_ptr++;
+    if (*end_ptr != '+')
     {
-        if (!strcmp(arg, "all"))
-        {
-            *number = last_hit_count;
-            *start = 1;
-        }
-        else
-            *start = atoi(arg);
+        printf("Bad show arg: expected +. Got %s\n", end_ptr);
+        return 0;
     }
-    if (p && (p=strchr(p+1, '+')))
-        strcpy(setstring, p+1);
-    else if (setnumber >= 0)
-        sprintf(setstring, "%d", setnumber);
-    else
-        *setstring = '\0';
+    end_ptr++;
+    arg_c = end_ptr;
+    *number = odr_strtol(arg_c, &end_ptr, 10);
+    if (end_ptr == arg_c)
+    {
+        printf("Bad show arg: expected number after +\n");
+        return 0;
+    }
+    if (*end_ptr == '\0')
+        return 1;
+    while (isspace(*(unsigned char *)end_ptr))
+        end_ptr++;
+    if (*end_ptr != '+')
+    {
+        printf("Bad show arg: + expected. Got %s\n", end_ptr);
+        return 0;
+    }
+    strcpy(setstring, end_ptr+1);
+    return 1;
 }
 
 static int send_presentRequest(const char *arg)
@@ -2806,7 +2839,8 @@ static int send_presentRequest(const char *arg)
 
     req->referenceId = set_refid(out);
 
-    parse_show_args(arg, setstring, &setno, &nos);
+    if (!parse_show_args(arg, setstring, &setno, &nos))
+        return 0;
     if (*setstring)
         req->resultSetId = setstring;
 
@@ -2897,7 +2931,8 @@ static int send_SRW_presentRequest(const char *arg)
 
     if (!sr)
         return 0;
-    parse_show_args(arg, setstring, &setno, &nos);
+    if (!parse_show_args(arg, setstring, &setno, &nos))
+        return 0;
     sr->u.request->startRecord = odr_intdup(out, setno);
     sr->u.request->maximumRecords = odr_intdup(out, nos);
     if (record_schema)
@@ -4965,7 +5000,10 @@ static void client(void)
         {
             line_in=readline(C_PROMPT);
             if (!line_in)
+            {
+                putchar('\n');
                 break;
+            }
 #if HAVE_READLINE_HISTORY_H
             if (*line_in)
                 add_history(line_in);