X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fzoom-c.c;h=05aff9c1d7929bae1727032d490ac2a94decb178;hb=48fdd95e1a026cfcc20ddd2329f0bda940f936e2;hp=4b12506dfc47dfebb1efb547034cad63b77d5fa4;hpb=9378c985aa6ea1ec9d5ddd6c34978051312dd035;p=yaz-moved-to-github.git diff --git a/src/zoom-c.c b/src/zoom-c.c index 4b12506..05aff9c 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 2000-2004, Index Data + * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: zoom-c.c,v 1.34 2004-12-21 00:34:31 adam Exp $ + * $Id: zoom-c.c,v 1.41 2005-05-17 11:48:36 adam Exp $ */ /** * \file zoom-c.c @@ -24,12 +24,24 @@ #include #include +#if HAVE_SYS_TYPES_H +#include +#endif +#if HAVE_SYS_TIME_H +#include +#endif #if HAVE_SYS_POLL_H #include #endif +#if HAVE_SYS_SELECT_H +#include +#endif +#ifdef WIN32 +#include +#endif -static int log_level=0; -static int log_level_initialized=0; +static int log_level = 0; +static int log_level_initialized = 0; typedef enum { zoom_pending, @@ -99,8 +111,8 @@ static void set_dset_error (ZOOM_connection c, int error, char *cp; if (!log_level_initialized) { - log_level=yaz_log_module_level("zoom"); - log_level_initialized=1; + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; } xfree (c->addinfo); @@ -238,8 +250,8 @@ ZOOM_connection_create (ZOOM_options options) if (!log_level_initialized) { - log_level=yaz_log_module_level("zoom"); - log_level_initialized=1; + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; } c->proto = PROTO_Z3950; @@ -287,54 +299,20 @@ static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options, int *num) { char **databaseNames; - const char *c; - int no = 2; const char *cp = ZOOM_options_get (options, "databaseName"); if (!cp || !*cp) { if (strncmp (con->host_port, "unix:", 5) == 0) - cp = strchr (con->host_port+5, ':'); + cp = strchr(con->host_port+5, ':'); else - cp = strchr (con->host_port, '/'); + cp = strchr(con->host_port, '/'); if (cp) cp++; } - if (cp) - { - c = cp; - while ((c = strchr(c, '+'))) - { - c++; - no++; - } - } - else + if (!cp) cp = "Default"; - databaseNames = (char**) - odr_malloc (con->odr_out, no * sizeof(*databaseNames)); - no = 0; - while (*cp) - { - c = strchr (cp, '+'); - if (!c) - c = cp + strlen(cp); - else if (c == cp) - { - cp++; - continue; - } - /* cp ptr to first char of db name, c is char - following db name */ - databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp); - memcpy (databaseNames[no], cp, c-cp); - databaseNames[no++][c-cp] = '\0'; - cp = c; - if (*cp) - cp++; - } - databaseNames[no] = NULL; - *num = no; + nmem_strsplit(con->odr_out->mem, "+", cp, &databaseNames, num); return databaseNames; } @@ -356,8 +334,8 @@ ZOOM_connection_connect(ZOOM_connection c, if (!log_level_initialized) { - log_level=yaz_log_module_level("zoom"); - log_level_initialized=1; + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; } if (c->cs) @@ -542,8 +520,8 @@ ZOOM_resultset ZOOM_resultset_create () if (!log_level_initialized) { - log_level=yaz_log_module_level("zoom"); - log_level_initialized=1; + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; } yaz_log (log_level, "ZOOM_resultset_create r = %p", r); @@ -831,8 +809,8 @@ static zoom_ret do_connect (ZOOM_connection c) const char *effective_host; if (!log_level_initialized) { - log_level=yaz_log_module_level("zoom"); - log_level_initialized=1; + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; } @@ -931,7 +909,7 @@ static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out) val = ZOOM_options_get (c->options, buf); if (!val) break; - cp = strchr (val, ':'); + cp = strchr(val, ':'); if (!cp) continue; len = cp - val; @@ -1037,7 +1015,7 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) ZOOM_options_get(c->options, "implementationName"), odr_prepend(c->odr_out, "ZOOM-C", ireq->implementationName)); - version = odr_strdup(c->odr_out, "$Revision: 1.34 $"); + version = odr_strdup(c->odr_out, "$Revision: 1.41 $"); if (strlen(version) > 10) /* check for unexpanded CVS strings */ version[strlen(version)-2] = '\0'; ireq->implementationVersion = odr_prepend(c->odr_out, @@ -1095,7 +1073,7 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) if (c->proxy) yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out, VAL_PROXY, 1, c->host_port); - if (c->charset||c->lang) + if (c->charset || c->lang) { Z_OtherInformation **oi; Z_OtherInformationUnit *oi_unit; @@ -1104,14 +1082,26 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0))) { + char **charsets_addresses = 0; + char **langs_addresses = 0; + int charsets_count = 0; + int langs_count = 0; + + if (c->charset) + nmem_strsplit_blank(c->odr_out->mem, c->charset, + &charsets_addresses, &charsets_count); + if (c->lang) + nmem_strsplit_blank(c->odr_out->mem, c->lang, + &langs_addresses, &langs_count); ODR_MASK_SET(ireq->options, Z_Options_negotiationModel); - oi_unit->which = Z_OtherInfo_externallyDefinedInfo; oi_unit->information.externallyDefinedInfo = - yaz_set_proposal_charneg - (c->odr_out, - (const char **)&c->charset, (c->charset) ? 1:0, - (const char **)&c->lang, (c->lang) ? 1:0, 1); + yaz_set_proposal_charneg(c->odr_out, + (const char **) charsets_addresses, + charsets_count, + (const char **) langs_addresses, + langs_count, + 1); } } assert (apdu); @@ -1396,20 +1386,11 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c) return send_APDU (c, apdu); } -static void response_diag (ZOOM_connection c, Z_DiagRec *p) +static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r) { int oclass; - Z_DefaultDiagFormat *r; char *addinfo = 0; - - xfree (c->addinfo); - c->addinfo = 0; - if (p->which != Z_DiagRec_defaultFormat) - { - set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); - return; - } - r = p->u.defaultFormat; + switch (r->which) { case Z_DefaultDiagFormat_v2Addinfo: @@ -1419,11 +1400,21 @@ static void response_diag (ZOOM_connection c, Z_DiagRec *p) addinfo = r->u.v3Addinfo; break; } + xfree (c->addinfo); + c->addinfo = 0; set_dset_error(c, *r->condition, yaz_z3950oid_to_str(r->diagnosticSetId, &oclass), addinfo, 0); } +static void response_diag(ZOOM_connection c, Z_DiagRec *p) +{ + if (p->which != Z_DiagRec_defaultFormat) + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); + else + response_default_diag(c, p->u.defaultFormat); +} + ZOOM_API(ZOOM_record) ZOOM_record_clone (ZOOM_record srec) { @@ -1498,7 +1489,7 @@ static const char *marc_iconv_return(ZOOM_record rec, int marc_type, if (record_charset && *record_charset) { /* Use "from,to" or just "from" */ - const char *cp =strchr(record_charset, ','); + const char *cp = strchr(record_charset, ','); int clen = strlen(record_charset); if (cp && cp[1]) { @@ -1552,7 +1543,7 @@ static const char *record_iconv_return(ZOOM_record rec, int *len, if (record_charset && *record_charset) { /* Use "from,to" or just "from" */ - const char *cp =strchr(record_charset, ','); + const char *cp = strchr(record_charset, ','); int clen = strlen(record_charset); if (cp && cp[1]) { @@ -1950,13 +1941,7 @@ static void handle_records (ZOOM_connection c, Z_Records *sr, return; } if (sr && sr->which == Z_Records_NSD) - { - Z_DiagRec dr, *dr_p = &dr; - dr.which = Z_DiagRec_defaultFormat; - dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic; - - response_diag (c, dr_p); - } + response_default_diag(c, sr->u.nonSurrogateDiagnostic); else if (sr && sr->which == Z_Records_multipleNSD) { if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1) @@ -1990,8 +1975,6 @@ static void handle_records (ZOOM_connection c, Z_Records *sr, Z_NamePlusRecord *myrec = zget_surrogateDiagRec(resultset->odr, 0, 14, 0); record_cache_add(resultset, myrec, resultset->start); - yaz_log(YLOG_LOG, "pseudo record 1, at pos %d", - resultset->start); } } else if (present_phase) @@ -2000,8 +1983,6 @@ static void handle_records (ZOOM_connection c, Z_Records *sr, Z_NamePlusRecord *myrec = zget_surrogateDiagRec(resultset->odr, 0, 14, 0); record_cache_add(resultset, myrec, resultset->start); - yaz_log(YLOG_LOG, "pseudo record 1, at pos %d", - resultset->start); } } } @@ -2463,7 +2444,7 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p) Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req)); const char *str; - req->which=Z_IOItemOrder_esRequest; + req->which = Z_IOItemOrder_esRequest; req->u.esRequest = (Z_IORequest *) odr_malloc(p->odr_out,sizeof(Z_IORequest)); @@ -2549,17 +2530,17 @@ Z_APDU *create_admin_package(ZOOM_package p, int type, toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *) odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep)); - toKeep->which=type; + toKeep->which = type; toKeep->databaseName = odr_strdup(p->odr_out, first_db); - toKeep->u.create=odr_nullval(); + toKeep->u.create = odr_nullval(); apdu->u.extendedServicesRequest->taskSpecificParameters = r; r->u.adminService->u.esRequest->notToKeep = notToKeep = (Z_ESAdminOriginPartNotToKeep *) odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->notToKeep)); - notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow; - notToKeep->u.recordsWillFollow=odr_nullval(); + notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow; + notToKeep->u.recordsWillFollow = odr_nullval(); if (toKeepP) *toKeepP = toKeep; if (notToKeepP) @@ -2903,6 +2884,34 @@ static int es_response (ZOOM_connection c, return 1; } +static void interpret_init_diag(ZOOM_connection c, + Z_DiagnosticFormat *diag) +{ + if (diag->num > 0) + { + Z_DiagnosticFormat_s *ds = diag->elements[0]; + if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec) + response_default_diag(c, ds->u.defaultDiagRec); + } +} + + +static void interpret_otherinformation_field(ZOOM_connection c, + Z_OtherInformation *ui) +{ + int i; + for (i = 0; i < ui->num_elements; i++) + { + Z_OtherInformationUnit *unit = ui->list[i]; + if (unit->which == Z_OtherInfo_externallyDefinedInfo && + unit->information.externallyDefinedInfo && + unit->information.externallyDefinedInfo->which == + Z_External_diag1) + { + interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1); + } + } +} static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) { @@ -2935,7 +2944,12 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) initrs->implementationVersion : ""); if (!*initrs->result) { - set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); + Z_External *uif = initrs->userInformationField; + + set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */ + + if (uif && uif->which == Z_External_userInfo1) + interpret_otherinformation_field(c, uif->u.userInfo1); } else { @@ -2964,7 +2978,7 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) if (p) { - char *charset=NULL, *lang=NULL; + char *charset = NULL, *lang = NULL; int sel; yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel); @@ -2977,6 +2991,10 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) if (lang) ZOOM_connection_option_set (c, "negotiation-lang", lang); + + ZOOM_connection_option_set ( + c, "negotiation-charset-in-effect-for-records", + (sel != 0) ? "1" : "0"); nmem_destroy(tmpmem); } } @@ -3242,7 +3260,7 @@ static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out) ZOOM_connection_put_event (c, event); yaz_log (log_level, "do_write_ex len=%d", len_out); - if ((r=cs_put (c->cs, buf_out, len_out)) < 0) + if ((r = cs_put(c->cs, buf_out, len_out)) < 0) { if (c->reconnect_ok) { @@ -3479,7 +3497,8 @@ ZOOM_connection_last_event(ZOOM_connection cs) ZOOM_API(int) ZOOM_event (int no, ZOOM_connection *cs) { - int timeout = 5000; + int timeout = 30; /* default timeout in seconds */ + int timeout_set = 0; /* whether it was overriden at all */ #if HAVE_SYS_POLL_H struct pollfd pollfds[1024]; ZOOM_connection poll_cs[1024]; @@ -3536,10 +3555,18 @@ ZOOM_event (int no, ZOOM_connection *cs) continue; if (max_fd < fd) max_fd = fd; - - this_timeout = ZOOM_options_get_int (c->options, "timeout", -1); - if (this_timeout != -1 && this_timeout < timeout) - timeout = this_timeout; + + /* -1 is used for indefinite timeout (no timeout), so -2 here. */ + this_timeout = ZOOM_options_get_int (c->options, "timeout", -2); + if (this_timeout != -2) + { + /* ensure the minimum timeout is used */ + if (!timeout_set) + timeout = this_timeout; + else if (this_timeout != -1 && this_timeout < timeout) + timeout = this_timeout; + timeout_set = 1; + } #if HAVE_SYS_POLL_H if (mask) { @@ -3575,14 +3602,11 @@ ZOOM_event (int no, ZOOM_connection *cs) } #endif } - if (timeout >= 5000) - timeout = 30; - if (!nfds) return 0; #if HAVE_SYS_POLL_H - r = poll (pollfds, nfds, timeout * 1000); + r = poll (pollfds, nfds, (timeout == -1 ? -1 : timeout * 1000)); for (i = 0; i