X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=client%2Fclient.c;h=a58a7b3461be31930ee2eb716f763891d42c2641;hp=d42b23beaebabd431344debec6776b387655a463;hb=0d42bb835c37442eeffa77f24ad2fdf876f8f4da;hpb=ee6ab2ee3a9ee1a8c65d7272ec7fba1d886f5af0 diff --git a/client/client.c b/client/client.c index d42b23b..a58a7b3 100644 --- a/client/client.c +++ b/client/client.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2008 Index Data + * Copyright (C) 1995-2009 Index Data * See the file LICENSE for details. */ /** \file client.c @@ -33,15 +33,6 @@ #include #endif -#if HAVE_OPENSSL_SSL_H -#include -#include -#include -#include -#include -#include -#endif - #ifdef WIN32 #include #include @@ -101,7 +92,10 @@ static Z_SRW_PDU *srw_sr = 0; static FILE *apdu_file = 0; static FILE *ber_file = 0; static COMSTACK conn = 0; /* our z-association */ + static Z_IdAuthentication *auth = 0; /* our current auth definition */ +static NMEM nmem_auth = NULL; + char *databaseNames[128]; int num_databaseNames = 0; static Z_External *record_last = 0; @@ -139,7 +133,6 @@ static char* yazLang = 0; static char last_cmd[32] = "?"; static FILE *marc_file = 0; static char *refid = NULL; -static char *last_open_command = NULL; static int auto_reconnect = 0; static int auto_wait = 1; static Odr_bitmask z3950_options; @@ -186,6 +179,10 @@ int cmd_querycharset(const char *arg); static void close_session(void); +static void marc_file_write(const char *buf, size_t sz); + +static void wait_and_handle_response(int one_response_only); + ODR getODROutputStream(void) { return out; @@ -204,14 +201,15 @@ const char* query_type_as_string(QueryType q) } } -static void do_hex_dump(const char* buf, int len) +static void do_hex_dump(const char* buf, size_t len) { if (hex_dump) { - int i,x; + size_t i; + int x; for (i = 0; i < len ; i = i+16 ) { - printf(" %4.4d ",i); + printf(" %4.4ld ", (long) i); for(x=0 ; i+xoptions, Z_Options_negotiationModel)) - { - Z_OtherInformation **p; - Z_OtherInformationUnit *p0; - - yaz_oi_APDU(apdu, &p); - - if ((p0=yaz_oi_update(p, out, NULL, 0, 0))) - { - p0->which = Z_OtherInfo_externallyDefinedInfo; - p0->information.externallyDefinedInfo = - yaz_set_proposal_charneg(out, 0, 0, 0, 0, 0); - } - - } if (send_apdu(apdu)) printf("Sent initrequest.\n"); } @@ -607,7 +595,7 @@ static int parse_cmd_doc(const char **arg, ODR out, char **buf, int *len) } else if ((*arg)[0] == '<') { - long fsize; + size_t fsize; FILE *inf; const char *fname; const char *arg_start = ++(*arg); @@ -677,23 +665,11 @@ static int cmd_base(const char *arg) return set_base(arg); } -void cmd_open_remember_last_open_command(const char* arg, char* new_open_command) -{ - if(last_open_command != arg) - { - if(last_open_command) xfree(last_open_command); - last_open_command = xstrdup(new_open_command); - } -} - int session_connect(const char *arg) { void *add; char type_and_host[101]; const char *basep = 0; -#if HAVE_OPENSSL_SSL_H - SSL *ssl; -#endif if (conn) { cs_close(conn); @@ -710,8 +686,6 @@ int session_connect(const char *arg) strncpy(type_and_host, arg, sizeof(type_and_host)-1); type_and_host[sizeof(type_and_host)-1] = '\0'; - cmd_open_remember_last_open_command(arg, type_and_host); - if (yazProxy) conn = cs_create_host(yazProxy, 1, &add); else @@ -746,30 +720,7 @@ int session_connect(const char *arg) return 0; } printf("OK.\n"); -#if HAVE_OPENSSL_SSL_H - if ((ssl = (SSL *) cs_get_ssl(conn))) - { - X509 *server_cert = SSL_get_peer_certificate (ssl); - - if (server_cert) - { - char *pem_buf; - int pem_len; - BIO *bio = BIO_new(BIO_s_mem()); - - /* get PEM buffer in memory */ - PEM_write_bio_X509(bio, server_cert); - pem_len = BIO_get_mem_data(bio, &pem_buf); - fwrite(pem_buf, pem_len, 1, stdout); - - /* print all info on screen .. */ - X509_print_fp(stdout, server_cert); - BIO_free(bio); - - X509_free (server_cert); - } - } -#endif + cs_print_session_info(conn); if (basep && *basep) set_base (basep); if (protocol == PROTO_Z3950) @@ -794,74 +745,65 @@ int cmd_open(const char *arg) return r; } -void try_reconnect(void) -{ - char* open_command; - - if(!( auto_reconnect && last_open_command) ) return ; - - open_command = (char *) xmalloc(strlen(last_open_command)+6); - strcpy(open_command, "open "); - - strcat(open_command, last_open_command); - - process_cmd_line(open_command); - - xfree(open_command); -} - int cmd_authentication(const char *arg) { - static Z_IdAuthentication au; - static char user[40], group[40], pass[40]; - static Z_IdPass idPass; + char **args; int r; - if (!*arg) - { - printf("Auth field set to null\n"); - auth = 0; - return 1; - } - r = sscanf(arg, "%39s %39s %39s", user, group, pass); + nmem_reset(nmem_auth); + nmem_strsplit_blank(nmem_auth, arg, &args, &r); + if (r == 0) { printf("Authentication set to null\n"); auth = 0; } - if (r == 1) + else if (r == 1) { - auth = &au; - if (!strcmp(user, "-")) { - au.which = Z_IdAuthentication_anonymous; + auth = (Z_IdAuthentication *) nmem_malloc(nmem_auth, sizeof(*auth)); + if (!strcmp(args[0], "-")) + { + auth->which = Z_IdAuthentication_anonymous; + auth->u.anonymous = odr_nullval(); printf("Authentication set to Anonymous\n"); - } else { - au.which = Z_IdAuthentication_open; - au.u.open = user; - printf("Authentication set to Open (%s)\n", user); + } + else + { + auth->which = Z_IdAuthentication_open; + auth->u.open = args[0]; + printf("Authentication set to Open (%s)\n", args[0]); } } - if (r == 2) - { - auth = &au; - au.which = Z_IdAuthentication_idPass; - au.u.idPass = &idPass; - idPass.groupId = NULL; - idPass.userId = !strcmp(user, "-") ? 0 : user; - idPass.password = !strcmp(group, "-") ? 0 : group; - printf("Authentication set to User (%s), Pass (%s)\n", user, group); + else if (r == 2) + { + auth = (Z_IdAuthentication *) nmem_malloc(nmem_auth, sizeof(*auth)); + auth->which = Z_IdAuthentication_idPass; + auth->u.idPass = (Z_IdPass *) + nmem_malloc(nmem_auth, sizeof(*auth->u.idPass)); + auth->u.idPass->groupId = NULL; + auth->u.idPass->userId = !strcmp(args[0], "-") ? 0 : args[0]; + auth->u.idPass->password = !strcmp(args[1], "-") ? 0 : args[1]; + printf("Authentication set to User (%s), Pass (%s)\n", + args[0], args[1]); + } + else if (r == 3) + { + auth = (Z_IdAuthentication*) nmem_malloc(nmem_auth, sizeof(*auth)); + auth->which = Z_IdAuthentication_idPass; + auth->u.idPass = (Z_IdPass *) + nmem_malloc(nmem_auth, sizeof(*auth->u.idPass)); + auth->u.idPass->groupId = args[1]; + auth->u.idPass->userId = args[0]; + auth->u.idPass->password = args[2]; + printf("Authentication set to User (%s), Group (%s), Pass (%s)\n", + args[0], args[1], args[2]); } - if (r == 3) + else { - auth = &au; - au.which = Z_IdAuthentication_idPass; - au.u.idPass = &idPass; - idPass.groupId = group; - idPass.userId = user; - idPass.password = pass; - printf("Authentication set to User (%s), Group (%s), Pass (%s)\n", - user, group, pass); + printf("Bad number of args to auth\n"); + auth = 0; } + return 1; } @@ -932,8 +874,8 @@ static void display_record(Z_External *r) { print_record((const unsigned char *) r->u.octet_aligned->buf, r->u.octet_aligned->len); - if (marc_file) - fwrite(r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, marc_file); + marc_file_write((const char *) r->u.octet_aligned->buf, + r->u.octet_aligned->len); } else if (oid && r->which == Z_External_octet) { @@ -993,7 +935,10 @@ static void display_record(Z_External *r) if (yaz_marc_decode_buf(mt, octet_buf, r->u.octet_aligned->len, &result, &rlen)> 0) { - fwrite(result, rlen, 1, stdout); + if (fwrite(result, rlen, 1, stdout) != 1) + { + printf("write to stdout failed\n"); + } } else { @@ -1011,8 +956,7 @@ static void display_record(Z_External *r) r->u.octet_aligned->len); } } - if (marc_file) - fwrite(octet_buf, 1, r->u.octet_aligned->len, marc_file); + marc_file_write(octet_buf, r->u.octet_aligned->len); } else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_sutrs)) { @@ -1022,8 +966,7 @@ static void display_record(Z_External *r) return; } print_record(r->u.sutrs->buf, r->u.sutrs->len); - if (marc_file) - fwrite(r->u.sutrs->buf, 1, r->u.sutrs->len, marc_file); + marc_file_write((const char *) r->u.sutrs->buf, r->u.sutrs->len); } else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_grs_1)) { @@ -2186,8 +2129,10 @@ static int cmd_update0(const char *arg) static int cmd_update_Z3950(int version, int action_no, const char *recid, char *rec_buf, int rec_len); +#if YAZ_HAVE_XML2 static int cmd_update_SRW(int action_no, const char *recid, char *rec_buf, int rec_len); +#endif static int cmd_update_common(const char *arg, int version) { @@ -2614,17 +2559,39 @@ static int cmd_find(const char *arg) } else { - if (!conn) + if (*cur_host && auto_reconnect) { - try_reconnect(); - - if (!conn) { - printf("Not connected yet\n"); - return 0; + int i = 0; + for (;;) + { + if (conn) + { + if (!send_searchRequest(arg)) + return 0; + wait_and_handle_response(0); + if (conn) + break; + } + if (++i == 2) + { + printf("Unable to reconnect\n"); + break; + } + session_connect(cur_host); + wait_and_handle_response(0); } + return 0; } - if (!send_searchRequest(arg)) + else if (conn) + { + if (!send_searchRequest(arg)) + return 0; + } + else + { + printf("Not connected yet\n"); return 0; + } } return 2; } @@ -2906,6 +2873,7 @@ void exit_client(int code) { file_history_save(file_history); file_history_destroy(&file_history); + nmem_destroy(nmem_auth); exit(code); } @@ -3247,15 +3215,13 @@ static int cmd_scan_common(const char *set, const char *arg) } else { - if (!conn) + if (*cur_host && !conn && auto_reconnect) { - try_reconnect(); - - if (!conn) { - printf("Session not initialized yet\n"); - return 0; - } + session_connect(cur_host); + wait_and_handle_response(0); } + if (!conn) + return 0; if (session_initResponse && !ODR_MASK_GET(session_initResponse->options, Z_Options_scan)) { @@ -3317,12 +3283,24 @@ int cmd_format(const char *arg) printf("Usage: format \n"); return 0; } + while (sscanf(cp, "%40s%n", form_str, &nor) >= 1 && nor > 0 + && idx < RECORDSYNTAX_MAX) + { + if (strcmp(form_str, "none") && + !yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, form_str, out)) + { + printf("Bad format: %s\n", form_str); + return 0; + } + cp += nor; + } for (i = 0; i < recordsyntax_size; i++) { xfree(recordsyntax_list[i]); recordsyntax_list[i] = 0; } - + + cp = arg; while (sscanf(cp, "%40s%n", form_str, &nor) >= 1 && nor > 0 && idx < RECORDSYNTAX_MAX) { @@ -3560,35 +3538,37 @@ int cmd_source(const char* arg, int echo ) FILE* includeFile; char line[102400], *cp; - if(strlen(arg)<1) { - fprintf(stderr,"Error in source command use a filename\n"); + if (strlen(arg) < 1) + { + fprintf(stderr, "Error in source command use a filename\n"); return -1; } includeFile = fopen(arg, "r"); - if(!includeFile) { - fprintf(stderr,"Unable to open file %s for reading\n",arg); + if (!includeFile) + { + fprintf(stderr, "Unable to open file %s for reading\n",arg); return -1; } - while(!feof(includeFile)) { - memset(line,0,sizeof(line)); - fgets(line,sizeof(line),includeFile); - - if(strlen(line) < 2) continue; - if(line[0] == '#') continue; + while (fgets(line, sizeof(line), includeFile)) + { + if (strlen(line) < 2) + continue; + if (line[0] == '#') + continue; if ((cp = strrchr(line, '\n'))) *cp = '\0'; - if( echo ) { - printf( "processing line: %s\n",line ); - }; + if (echo) + printf("processing line: %s\n", line); process_cmd_line(line); } - if(fclose(includeFile)<0) { + if (fclose(includeFile)) + { perror("unable to close include file"); exit(1); } @@ -3610,12 +3590,12 @@ int cmd_source_noecho(const char* arg) int cmd_subshell(const char* args) { - if(strlen(args)) - system(args); - else - system(getenv("SHELL")); - + int ret = system(strlen(args) ? args : getenv("SHELL")); printf("\n"); + if (ret) + { + printf("Exit %d\n", ret); + } return 1; } @@ -3746,6 +3726,16 @@ int cmd_set_marcdump(const char* arg) return 1; } +static void marc_file_write(const char *buf, size_t sz) +{ + if (marc_file) + { + if (fwrite(buf, 1, sz, marc_file) != sz) + { + perror("marcfile write"); + } + } +} /* this command takes 3 arge {name class oid} */ @@ -3926,9 +3916,8 @@ static void handle_srw_record(Z_SRW_record *rec) printf("\n"); if (rec->recordData_buf && rec->recordData_len) { - fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout); - if (marc_file) - fwrite(rec->recordData_buf, 1, rec->recordData_len, marc_file); + printf("%.*s", rec->recordData_len, rec->recordData_buf); + marc_file_write(rec->recordData_buf, rec->recordData_len); } else printf("No data!"); @@ -4089,7 +4078,7 @@ static void http_response(Z_HTTP_Response *hres) } #endif -void wait_and_handle_response(int one_response_only) +static void wait_and_handle_response(int one_response_only) { int reconnect_ok = 1; int res; @@ -4385,16 +4374,13 @@ int cmd_list_all(const char* args) { int i; /* connection options */ - if(conn) { - printf("Connected to : %s\n",last_open_command); - } else { - if(last_open_command) - printf("Not connected to : %s\n",last_open_command); - else - printf("Not connected : \n"); - - } - if(yazProxy) printf("using proxy : %s\n",yazProxy); + if (conn) + printf("Connected to : %s\n", cur_host); + else if (*cur_host) + printf("Not connected to : %s\n", cur_host); + else + printf("Not connected : \n"); + if (yazProxy) printf("using proxy : %s\n",yazProxy); printf("auto_reconnect : %s\n",auto_reconnect?"on":"off"); printf("auto_wait : %s\n",auto_wait?"on":"off"); @@ -4894,12 +4880,12 @@ static void client(void) static void show_version(void) { - char vstr[20]; + char vstr[20], sha1_str[41]; - yaz_version(vstr, 0); - printf("YAZ version: %s\n", YAZ_VERSION); - if (strcmp(vstr, YAZ_VERSION)) - printf("YAZ DLL/SO: %s\n", vstr); + yaz_version(vstr, sha1_str); + printf("YAZ version: %s %s\n", YAZ_VERSION, YAZ_VERSION_SHA1); + if (strcmp(sha1_str, YAZ_VERSION_SHA1)) + printf("YAZ DLL/SO: %s %s\n", vstr, sha1_str); exit(0); } @@ -4932,7 +4918,8 @@ int main(int argc, char **argv) ODR_MASK_SET(&z3950_options, Z_Options_sort); ODR_MASK_SET(&z3950_options, Z_Options_extendedServices); ODR_MASK_SET(&z3950_options, Z_Options_delSet); - ODR_MASK_SET(&z3950_options, Z_Options_negotiationModel); + + nmem_auth = nmem_create(); while ((ret = options("k:c:q:a:b:m:v:p:u:t:Vxd:f:", argv, argc, &arg)) != -2) { @@ -5062,6 +5049,7 @@ int main(int argc, char **argv) /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab