- if (c->cs)
- return cs_fileno(c->cs);
- return -1;
-}
-
-int z3950_connection_mask(ZOOM_connection c)
-{
- if (c->cs)
- return c->mask;
- return 0;
-}
-
-static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
-{
- int i;
- for (i = 0; i<200; i++)
- {
- size_t len;
- Z_OtherInformation **oi;
- char buf[80];
- const char *val;
- const char *cp;
- int oidval;
-
- sprintf(buf, "otherInfo%d", i);
- val = ZOOM_options_get(c->options, buf);
- if (!val)
- break;
- cp = strchr(val, ':');
- if (!cp)
- continue;
- len = cp - val;
- if (len >= sizeof(buf))
- len = sizeof(buf)-1;
- memcpy(buf, val, len);
- buf[len] = '\0';
- oidval = oid_getvalbyname(buf);
- if (oidval == VAL_NONE)
- continue;
-
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
- }
-}
-
-static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
-{
- assert(a);
- if (c->cookie_out)
- {
- Z_OtherInformation **oi;
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
- }
- if (c->client_IP)
- {
- Z_OtherInformation **oi;
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
- }
- otherInfo_attach(c, a, out);
- if (!z_APDU(out, &a, 0, 0))
- {
- FILE *outf = fopen("/tmp/apdu.txt", "a");
- if (a && outf)
- {
- ODR odr_pr = odr_createmem(ODR_PRINT);
- fprintf(outf, "a=%p\n", a);
- odr_setprint(odr_pr, outf);
- z_APDU(odr_pr, &a, 0, 0);
- odr_destroy(odr_pr);
- }
- yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
- set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
- odr_reset(out);
- return -1;
- }
- yaz_log(log_details, "%p encoding_APDU encoding OK", c);
- return 0;
-}
-
-static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
-{
- ZOOM_Event event;
- assert(a);
- if (encode_APDU(c, a, c->odr_out))
- return zoom_complete;
- yaz_log(log_details, "%p send APDU type=%d", c, a->which);
- c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
- event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event(c, event);
- odr_reset(c->odr_out);
- return do_write(c);
-}
-
-/* returns 1 if PDU was sent OK (still pending )
- 0 if PDU was not sent OK (nothing to wait for)
-*/
-
-static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
-{
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
- Z_InitRequest *ireq = apdu->u.initRequest;
- Z_IdAuthentication *auth = (Z_IdAuthentication *)
- odr_malloc(c->odr_out, sizeof(*auth));
- const char *auth_groupId = ZOOM_options_get(c->options, "group");
- const char *auth_userId = ZOOM_options_get(c->options, "user");
- const char *auth_password = ZOOM_options_get(c->options, "password");
- char *version;
-
- /* support the pass for backwards compatibility */
- if (!auth_password)
- auth_password = ZOOM_options_get(c->options, "pass");
-
- ODR_MASK_SET(ireq->options, Z_Options_search);
- ODR_MASK_SET(ireq->options, Z_Options_present);
- ODR_MASK_SET(ireq->options, Z_Options_scan);
- ODR_MASK_SET(ireq->options, Z_Options_sort);
- ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
- ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
-
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
-
- /* Index Data's Z39.50 Implementor Id is 81 */
- ireq->implementationId =
- odr_prepend(c->odr_out,
- ZOOM_options_get(c->options, "implementationId"),
- odr_prepend(c->odr_out, "81", ireq->implementationId));
-
- ireq->implementationName =
- odr_prepend(c->odr_out,
- ZOOM_options_get(c->options, "implementationName"),
- odr_prepend(c->odr_out, "ZOOM-C",
- ireq->implementationName));
-
- version = odr_strdup(c->odr_out, "$Revision: 1.98 $");
- if (strlen(version) > 10) /* check for unexpanded CVS strings */
- version[strlen(version)-2] = '\0';
- ireq->implementationVersion =
- odr_prepend(c->odr_out,
- ZOOM_options_get(c->options, "implementationVersion"),
- odr_prepend(c->odr_out, &version[11],
- ireq->implementationVersion));
-
- *ireq->maximumRecordSize =
- ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
- *ireq->preferredMessageSize =
- ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
-
- if (auth_groupId || auth_password)
- {
- Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
- int i = 0;
- pass->groupId = 0;
- if (auth_groupId && *auth_groupId)
- {
- pass->groupId = (char *)
- odr_malloc(c->odr_out, strlen(auth_groupId)+1);
- strcpy(pass->groupId, auth_groupId);
- i++;
- }
- pass->userId = 0;
- if (auth_userId && *auth_userId)
- {
- pass->userId = (char *)
- odr_malloc(c->odr_out, strlen(auth_userId)+1);
- strcpy(pass->userId, auth_userId);
- i++;
- }
- pass->password = 0;
- if (auth_password && *auth_password)
- {
- pass->password = (char *)
- odr_malloc(c->odr_out, strlen(auth_password)+1);
- strcpy(pass->password, auth_password);
- i++;
- }
- if (i)
- {
- auth->which = Z_IdAuthentication_idPass;
- auth->u.idPass = pass;
- ireq->idAuthentication = auth;
- }
- }
- else if (auth_userId)
- {
- auth->which = Z_IdAuthentication_open;
- auth->u.open = (char *)
- odr_malloc(c->odr_out, strlen(auth_userId)+1);
- strcpy(auth->u.open, auth_userId);
- ireq->idAuthentication = auth;
- }
- if (c->proxy)
- yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
- VAL_PROXY, 1, c->host_port);
- if (c->charset || c->lang)
- {
- Z_OtherInformation **oi;
- Z_OtherInformationUnit *oi_unit;
-
- yaz_oi_APDU(apdu, &oi);
-
- if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
- {
- ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
- oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
- oi_unit->information.externallyDefinedInfo =
- yaz_set_proposal_charneg_list(c->odr_out, " ",
- c->charset, c->lang, 1);
- }
- }
- assert(apdu);
- return send_APDU(c, apdu);
-}
-
-#if YAZ_HAVE_XML2
-static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
-{
- Z_GDU *gdu;
- ZOOM_Event event;
-
- gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
-
- if (c->sru_mode == zoom_sru_get)
- {
- yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
- }
- else if (c->sru_mode == zoom_sru_post)
- {
- yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
- }
- else if (c->sru_mode == zoom_sru_soap)
- {
- yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
- }
- if (!z_GDU(c->odr_out, &gdu, 0, 0))
- return zoom_complete;
- c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
-
- event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event(c, event);
- odr_reset(c->odr_out);
- return do_write(c);
-}
-#endif
-
-#if YAZ_HAVE_XML2
-static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
-{
- int i;
- int *start, *count;
- ZOOM_resultset resultset = 0;
- Z_SRW_PDU *sr = 0;
- const char *option_val = 0;
-
- if (c->error) /* don't continue on error */
- return zoom_complete;
- assert(c->tasks);
- switch(c->tasks->which)
- {
- case ZOOM_TASK_SEARCH:
- resultset = c->tasks->u.search.resultset;
- resultset->setname = xstrdup("default");
- ZOOM_options_set(resultset->options, "setname", resultset->setname);
- start = &c->tasks->u.search.start;
- count = &c->tasks->u.search.count;
- break;
- case ZOOM_TASK_RETRIEVE:
- resultset = c->tasks->u.retrieve.resultset;
-
- start = &c->tasks->u.retrieve.start;
- count = &c->tasks->u.retrieve.count;
-
- if (*start >= resultset->size)
- return zoom_complete;
- if (*start + *count > resultset->size)
- *count = resultset->size - *start;
-
- for (i = 0; i < *count; i++)
- {
- ZOOM_record rec =
- record_cache_lookup(resultset, i + *start,
- c->tasks->u.retrieve.syntax,
- c->tasks->u.retrieve.elementSetName);
- if (!rec)
- break;
- else
- {
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
- ZOOM_connection_put_event(c, event);
- }
- }
- *start += i;
- *count -= i;
-
- if (*count == 0)
- return zoom_complete;
- break;
- default:
- return zoom_complete;
- }
- assert(resultset->query);
-
- sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
-
- if (resultset->query->z_query->which == Z_Query_type_104
- && resultset->query->z_query->u.type_104->which == Z_External_CQL)
- {
- sr->u.request->query_type = Z_SRW_query_type_cql;
- sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
- }
- else if (resultset->query->z_query->which == Z_Query_type_1 &&
- resultset->query->z_query->u.type_1)
- {
- sr->u.request->query_type = Z_SRW_query_type_pqf;
- sr->u.request->query.pqf = resultset->query->query_string;
- }
- else
- {
- set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
- return zoom_complete;
- }
- sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
- sr->u.request->maximumRecords = odr_intdup(
- c->odr_out, resultset->step>0 ? resultset->step : *count);
- sr->u.request->recordSchema = resultset->schema;
-
- option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
- if (option_val)
- sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
-
- option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
- if (option_val)
- sr->extra_args = odr_strdup(c->odr_out, option_val);
- return send_srw(c, sr);
-}
-#else
-static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
-{
- return zoom_complete;
-}
-#endif
-
-static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
-{
- ZOOM_resultset r;
- int lslb, ssub, mspn;
- const char *syntax;
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
- Z_SearchRequest *search_req = apdu->u.searchRequest;
- const char *elementSetName;
- const char *smallSetElementSetName;
- const char *mediumSetElementSetName;
-
- assert(c->tasks);
- assert(c->tasks->which == ZOOM_TASK_SEARCH);
-
- r = c->tasks->u.search.resultset;
-
- yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
-
- elementSetName =
- ZOOM_options_get(r->options, "elementSetName");
- smallSetElementSetName =
- ZOOM_options_get(r->options, "smallSetElementSetName");
- mediumSetElementSetName =
- ZOOM_options_get(r->options, "mediumSetElementSetName");
-
- if (!smallSetElementSetName)
- smallSetElementSetName = elementSetName;
-
- if (!mediumSetElementSetName)
- mediumSetElementSetName = elementSetName;
-
- assert(r);
- assert(r->query);
-
- /* prepare query for the search request */
- search_req->query = r->query->z_query;
- if (!search_req->query)
- {
- set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
- return zoom_complete;
- }
-
- search_req->databaseNames = r->databaseNames;
- search_req->num_databaseNames = r->num_databaseNames;
-
- /* get syntax (no need to provide unless piggyback is in effect) */
- syntax = c->tasks->u.search.syntax;
-
- lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
- ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
- mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
- if (lslb != -1 && ssub != -1 && mspn != -1)
- {
- /* So're a Z39.50 expert? Let's hope you don't do sort */
- *search_req->largeSetLowerBound = lslb;
- *search_req->smallSetUpperBound = ssub;
- *search_req->mediumSetPresentNumber = mspn;
- }
- else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
- && r->piggyback && !r->r_sort_spec && !r->schema)
- {
- /* Regular piggyback - do it unless we're going to do sort */
- *search_req->largeSetLowerBound = 2000000000;
- *search_req->smallSetUpperBound = 1;
- *search_req->mediumSetPresentNumber =
- r->step>0 ? r->step : c->tasks->u.search.count;
- }
- else
- {
- /* non-piggyback. Need not provide elementsets or syntaxes .. */
- smallSetElementSetName = 0;
- mediumSetElementSetName = 0;
- syntax = 0;
- }
- if (smallSetElementSetName && *smallSetElementSetName)
- {
- Z_ElementSetNames *esn = (Z_ElementSetNames *)
- odr_malloc(c->odr_out, sizeof(*esn));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
- search_req->smallSetElementSetNames = esn;
- }
- if (mediumSetElementSetName && *mediumSetElementSetName)
- {
- Z_ElementSetNames *esn =(Z_ElementSetNames *)
- odr_malloc(c->odr_out, sizeof(*esn));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
- search_req->mediumSetElementSetNames = esn;
- }
- if (syntax)
- search_req->preferredRecordSyntax =
- yaz_str_to_z3950oid(c->odr_out, CLASS_RECSYN, syntax);
-
- if (!r->setname)
- {
- if (c->support_named_resultsets)
- {
- char setname[14];
- int ord;
- /* find the lowest unused ordinal so that we re-use
- result sets on the server. */
- for (ord = 1; ; ord++)
- {
- ZOOM_resultset rp;
- sprintf(setname, "%d", ord);
- for (rp = c->resultsets; rp; rp = rp->next)
- if (rp->setname && !strcmp(rp->setname, setname))
- break;
- if (!rp)
- break;
- }
- r->setname = xstrdup(setname);
- yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
- "set %s", c, r->setname);
- }
- else
- {
- yaz_log(log_details, "%p ZOOM_connection_send_search: using "
- "default set", c);
- r->setname = xstrdup("default");
- }
- ZOOM_options_set(r->options, "setname", r->setname);
- }
- search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
- return send_APDU(c, apdu);
-}
-
-static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
-{
- int oclass;
- char *addinfo = 0;
-
- switch (r->which)
- {
- case Z_DefaultDiagFormat_v2Addinfo:
- addinfo = r->u.v2Addinfo;
- break;
- case Z_DefaultDiagFormat_v3Addinfo:
- 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)
-{
- char *buf;
- int size;
- ODR odr_enc;
- ZOOM_record nrec;
-
- odr_enc = odr_createmem(ODR_ENCODE);
- if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
- return 0;
- buf = odr_getbuf(odr_enc, &size, 0);
-
- nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
- nrec->odr = odr_createmem(ODR_DECODE);
- nrec->wrbuf_marc = 0;
- nrec->wrbuf_iconv = 0;
- nrec->wrbuf_opac = 0;
- odr_setbuf(nrec->odr, buf, size, 0);
- z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
-
- odr_destroy(odr_enc);
- return nrec;
-}
-
-ZOOM_API(ZOOM_record)
- ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
-{
- const char *syntax =
- ZOOM_options_get(s->options, "preferredRecordSyntax");
- const char *elementSetName =
- ZOOM_options_get(s->options, "elementSetName");
-
- return record_cache_lookup(s, pos, syntax, elementSetName);
-}
-
-ZOOM_API(ZOOM_record)
- ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
-{
- ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
-
- if (!rec)
- {
- /*
- * MIKE: I think force_sync should always be zero, but I don't
- * want to make this change until I get the go-ahead from
- * Adam, in case something depends on the old synchronous
- * behaviour.
- */
- int force_sync = 1;
- if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
- ZOOM_resultset_retrieve(r, force_sync, pos, 1);
- rec = ZOOM_resultset_record_immediate(r, pos);
- }
- return rec;
-}
-
-ZOOM_API(void)
- ZOOM_record_destroy(ZOOM_record rec)
-{
- if (!rec)
- return;
- if (rec->wrbuf_marc)
- wrbuf_free(rec->wrbuf_marc, 1);
- if (rec->wrbuf_iconv)
- wrbuf_free(rec->wrbuf_iconv, 1);
- if (rec->wrbuf_opac)
- wrbuf_free(rec->wrbuf_opac, 1);
- odr_destroy(rec->odr);
- xfree(rec);
-}
-
-static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
- int *len,
- const char *buf, int sz,
- const char *record_charset)
-{
- char to[40];
- char from[40];
- yaz_iconv_t cd = 0;
- yaz_marc_t mt = yaz_marc_create();
-
- *from = '\0';
- strcpy(to, "UTF-8");
- if (record_charset && *record_charset)
- {
- /* Use "from,to" or just "from" */
- const char *cp = strchr(record_charset, ',');
- int clen = strlen(record_charset);
- if (cp && cp[1])
- {
- strncpy( to, cp+1, sizeof(to)-1);
- to[sizeof(to)-1] = '\0';
- clen = cp - record_charset;
- }
- if (clen > sizeof(from)-1)
- clen = sizeof(from)-1;
-
- if (clen)
- strncpy(from, record_charset, clen);
- from[clen] = '\0';
- }
-
- if (*from && *to)
- {
- cd = yaz_iconv_open(to, from);
- yaz_marc_iconv(mt, cd);
- }
-
- yaz_marc_xml(mt, marc_type);
- if (!rec->wrbuf_marc)
- rec->wrbuf_marc = wrbuf_alloc();
- wrbuf_rewind(rec->wrbuf_marc);
- if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf_marc) > 0)
- {
- yaz_marc_destroy(mt);
- if (cd)
- yaz_iconv_close(cd);
- if (len)
- *len = wrbuf_len(rec->wrbuf_marc);
- return wrbuf_buf(rec->wrbuf_marc);
- }
- yaz_marc_destroy(mt);
- if (cd)
- yaz_iconv_close(cd);
- return 0;
-}
-
-static const char *record_iconv_return(ZOOM_record rec, int *len,
- const char *buf, int sz,
- const char *record_charset)
-{
- char to[40];
- char from[40];
- yaz_iconv_t cd = 0;
-
- *from = '\0';
- strcpy(to, "UTF-8");
- if (record_charset && *record_charset)
- {
- /* Use "from,to" or just "from" */
- const char *cp = strchr(record_charset, ',');
- int clen = strlen(record_charset);
- if (cp && cp[1])
- {
- strncpy( to, cp+1, sizeof(to)-1);
- to[sizeof(to)-1] = '\0';
- clen = cp - record_charset;
- }
- if (clen > sizeof(from)-1)
- clen = sizeof(from)-1;
-
- if (clen)
- strncpy(from, record_charset, clen);
- from[clen] = '\0';
- }
-
- if (*from && *to && (cd = yaz_iconv_open(to, from)))
- {
- char outbuf[12];
- size_t inbytesleft = sz;
- const char *inp = buf;
-
- if (!rec->wrbuf_iconv)
- rec->wrbuf_iconv = wrbuf_alloc();
-
- wrbuf_rewind(rec->wrbuf_iconv);
-
- while (inbytesleft)
- {
- size_t outbytesleft = sizeof(outbuf);
- char *outp = outbuf;
- size_t r = yaz_iconv(cd, (char**) &inp,
- &inbytesleft,
- &outp, &outbytesleft);
- if (r == (size_t) (-1))
- {
- int e = yaz_iconv_error(cd);
- if (e != YAZ_ICONV_E2BIG)
- break;
- }
- wrbuf_write(rec->wrbuf_iconv, outbuf, outp - outbuf);
- }
- wrbuf_puts(rec->wrbuf_iconv, "");
- buf = wrbuf_buf(rec->wrbuf_iconv);
- sz = wrbuf_len(rec->wrbuf_iconv);
- yaz_iconv_close(cd);
- }
- if (len)
- *len = sz;
- return buf;
-}
-
-ZOOM_API(int)
- ZOOM_record_error(ZOOM_record rec, const char **cp,
- const char **addinfo, const char **diagset)
-{
- Z_NamePlusRecord *npr;
-
- if (!rec)
- return 0;
- npr = rec->npr;
- if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
- {
- Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
- int error = YAZ_BIB1_UNSPECIFIED_ERROR;
- const char *add = 0;
-
- if (diag_rec->which == Z_DiagRec_defaultFormat)
- {
- Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
- int oclass;
-
- error = *ddf->condition;
- switch (ddf->which)
- {
- case Z_DefaultDiagFormat_v2Addinfo:
- add = ddf->u.v2Addinfo;
- break;
- case Z_DefaultDiagFormat_v3Addinfo:
- add = ddf->u.v3Addinfo;
- break;
- }
- if (diagset)
- *diagset = yaz_z3950oid_to_str(ddf->diagnosticSetId, &oclass);
- }
- else
- {
- if (diagset)
- *diagset = "Bib-1";
- }
- if (addinfo)
- *addinfo = add ? add : "";
- if (cp)
- *cp = diagbib1_str(error);
- return error;
- }
- return 0;
-}
-
-ZOOM_API(const char *)
- ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
-{
- char type[40];
- char charset[40];
- char xpath[512];
- const char *cp;
- int i;
- Z_NamePlusRecord *npr;
-
- if (len)
- *len = 0; /* default return */
-
- if (!rec)
- return 0;
- npr = rec->npr;
- if (!npr)
- return 0;
-
- cp = type_spec;
- for (i = 0; cp[i] && i < sizeof(type)-1; i++)
- {
- if (cp[i] == ';' || cp[i] == ' ')
- break;
- type[i] = cp[i];
- }
- type[i] = '\0';
- charset[0] = '\0';
- while (type_spec[i] == ';')
- {
- i++;
- while (type_spec[i] == ' ')
- i++;
- if (!strncmp(type_spec+i, "charset=", 8))
- {
- int j = 0;
- i = i + 8; /* skip charset= */
- for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
- {
- if (type_spec[i] == ';' || type_spec[i] == ' ')
- break;
- charset[j] = cp[i];
- }
- charset[j] = '\0';
- }
- else if (!strncmp(type_spec+i, "xpath=", 6))
- {
- int j = 0;
- i = i + 6;
- for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
- xpath[j] = cp[i];
- xpath[j] = '\0';
- }
- while (type_spec[i] == ' ')
- i++;
- }
- if (!strcmp(type, "database"))
- {
- if (len)
- *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
- return npr->databaseName;
- }
- else if (!strcmp(type, "syntax"))
- {
- const char *desc = 0;
- if (npr->which == Z_NamePlusRecord_databaseRecord)
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- oident *ent = oid_getentbyoid(r->direct_reference);
- if (ent)
- desc = ent->desc;
- }
- if (!desc)
- desc = "none";
- if (len)
- *len = strlen(desc);
- return desc;
- }
- if (npr->which != Z_NamePlusRecord_databaseRecord)
- return 0;
-
- /* from now on - we have a database record .. */
- if (!strcmp(type, "render"))
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- oident *ent = oid_getentbyoid(r->direct_reference);
-
- /* render bibliographic record .. */
- if (r->which == Z_External_OPAC)
- {
- r = r->u.opac->bibliographicRecord;
- if (!r)
- return 0;
- ent = oid_getentbyoid(r->direct_reference);
- }
- if (r->which == Z_External_sutrs)
- return record_iconv_return(rec, len,
- (char*) r->u.sutrs->buf,
- r->u.sutrs->len,
- charset);
- else if (r->which == Z_External_octet)
- {
- const char *ret_buf;
- switch (ent->value)
- {
- case VAL_SOIF:
- case VAL_HTML:
- case VAL_SUTRS:
- break;
- case VAL_TEXT_XML:
- case VAL_APPLICATION_XML:
- break;
- default:
- ret_buf = marc_iconv_return(
- rec, YAZ_MARC_LINE, len,
- (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- charset);
- if (ret_buf)
- return ret_buf;
- }
- return record_iconv_return(rec, len,
- (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- charset);
- }
- else if (r->which == Z_External_grs1)
- {
- if (!rec->wrbuf_marc)
- rec->wrbuf_marc = wrbuf_alloc();
- wrbuf_rewind(rec->wrbuf_marc);
- yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
- return record_iconv_return(rec, len,
- wrbuf_buf(rec->wrbuf_marc),
- wrbuf_len(rec->wrbuf_marc),
- charset);
- }
- return 0;
- }
- else if (!strcmp(type, "xml"))
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- oident *ent = oid_getentbyoid(r->direct_reference);
-
- /* render bibliographic record .. */
- if (r->which == Z_External_OPAC)
- {
- r = r->u.opac->bibliographicRecord;
- if (!r)
- return 0;
- ent = oid_getentbyoid(r->direct_reference);
- }
-
- if (r->which == Z_External_sutrs)
- return record_iconv_return(rec, len,
- (const char *) r->u.sutrs->buf,
- r->u.sutrs->len,
- charset);
- else if (r->which == Z_External_octet)
- {
- const char *ret_buf;
- int marc_decode_type = YAZ_MARC_MARCXML;
-
- switch (ent->value)
- {
- case VAL_SOIF:
- case VAL_HTML:
- case VAL_SUTRS:
- break;
- case VAL_TEXT_XML:
- case VAL_APPLICATION_XML:
- break;
- default:
- ret_buf = marc_iconv_return(
- rec, marc_decode_type, len,
- (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- charset);
- if (ret_buf)
- return ret_buf;
- }
- return record_iconv_return(rec, len,
- (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- charset);
- }
- else if (r->which == Z_External_grs1)
- {
- if (len) *len = 5;
- return "GRS-1";
- }
- return 0;
- }
- else if (!strcmp(type, "raw"))
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
-
- if (r->which == Z_External_sutrs)
- {
- if (len) *len = r->u.sutrs->len;
- return (const char *) r->u.sutrs->buf;
- }
- else if (r->which == Z_External_octet)
- {
- if (len) *len = r->u.octet_aligned->len;
- return (const char *) r->u.octet_aligned->buf;
- }
- else /* grs-1, explain, OPAC, ... */
- {
- if (len) *len = -1;
- return (const char *) npr->u.databaseRecord;
- }
- return 0;
- }
- else if (!strcmp (type, "ext"))
- {
- if (len) *len = -1;
- return (const char *) npr->u.databaseRecord;
- }
- else if (!strcmp (type, "opac"))
-
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- if (r->which == Z_External_OPAC)
- {
- if (!rec->wrbuf_opac)
- rec->wrbuf_opac = wrbuf_alloc();
- wrbuf_rewind(rec->wrbuf_opac);
- yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
- return record_iconv_return(rec, len,
- wrbuf_buf(rec->wrbuf_opac),
- wrbuf_len(rec->wrbuf_opac),
- charset);
- }
- }
- return 0;
-}
-
-static int strcmp_null(const char *v1, const char *v2)
-{
- if (!v1 && !v2)
- return 0;
- if (!v1 || !v2)
- return -1;
- return strcmp(v1, v2);
-}
-
-static size_t record_hash(int pos)
-{
- if (pos < 0)
- pos = 0;
- return pos % RECORD_HASH_SIZE;
-}
-
-static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
- int pos,
- const char *syntax, const char *elementSetName)
-{
- ZOOM_record_cache rc;
-
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
- ZOOM_connection_put_event(r->connection, event);
-
- for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
- {
- if (pos == rc->pos)
- {
- if (strcmp_null(r->schema, rc->schema))
- continue;
- if (strcmp_null(elementSetName,rc->elementSetName))
- continue;
- if (strcmp_null(syntax, rc->syntax))
- continue;
- /* not destroying rc->npr (it's handled by nmem )*/
- rc->rec.npr = npr;
- /* keeping wrbuf_marc too */
- return;
- }
- }
- rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
- rc->rec.npr = npr;
- rc->rec.odr = 0;
- rc->rec.wrbuf_marc = 0;
- rc->rec.wrbuf_iconv = 0;
- rc->rec.wrbuf_opac = 0;
- if (elementSetName)
- rc->elementSetName = odr_strdup(r->odr, elementSetName);
- else
- rc->elementSetName = 0;
-
- if (syntax)
- rc->syntax = odr_strdup(r->odr, syntax);
- else
- rc->syntax = 0;
-
- if (r->schema)
- rc->schema = odr_strdup(r->odr, r->schema);
- else
- rc->schema = 0;
-
- rc->pos = pos;
- rc->next = r->record_hash[record_hash(pos)];
- r->record_hash[record_hash(pos)] = rc;
-}
-
-static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
- const char *syntax,
- const char *elementSetName)
-{
- ZOOM_record_cache rc;
-
- for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
- {
- if (pos == rc->pos)
- {
- if (strcmp_null(r->schema, rc->schema))
- continue;
- if (strcmp_null(elementSetName,rc->elementSetName))
- continue;
- if (strcmp_null(syntax, rc->syntax))
- continue;
- return &rc->rec;
- }
- }
- return 0;
-}
-
-static void handle_records(ZOOM_connection c, Z_Records *sr,
- int present_phase)
-{
- ZOOM_resultset resultset;
- int *start, *count;
- const char *syntax = 0, *elementSetName = 0;
-
- if (!c->tasks)
- return ;
- switch (c->tasks->which)
- {
- case ZOOM_TASK_SEARCH:
- resultset = c->tasks->u.search.resultset;
- start = &c->tasks->u.search.start;
- count = &c->tasks->u.search.count;
- syntax = c->tasks->u.search.syntax;
- elementSetName = c->tasks->u.search.elementSetName;
- break;
- case ZOOM_TASK_RETRIEVE:
- resultset = c->tasks->u.retrieve.resultset;
- start = &c->tasks->u.retrieve.start;
- count = &c->tasks->u.retrieve.count;
- syntax = c->tasks->u.retrieve.syntax;
- elementSetName = c->tasks->u.retrieve.elementSetName;
- break;
- default:
- return;
- }
- if (sr && sr->which == Z_Records_NSD)
- response_default_diag(c, sr->u.nonSurrogateDiagnostic);
- else if (sr && sr->which == Z_Records_multipleNSD)
- {
- if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
- response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
- else
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- }
- else
- {
- if (*count + *start > resultset->size)
- *count = resultset->size - *start;
- if (*count < 0)
- *count = 0;
- if (sr && sr->which == Z_Records_DBOSD)
- {
- int i;
- NMEM nmem = odr_extract_mem(c->odr_in);
- Z_NamePlusRecordList *p =
- sr->u.databaseOrSurDiagnostics;
- for (i = 0; i<p->num_records; i++)
- {
- record_cache_add(resultset, p->records[i], i + *start,
- syntax, elementSetName);
- }
- *count -= i;
- *start += i;
- yaz_log(log_details,
- "handle_records resultset=%p start=%d count=%d",
- resultset, *start, *count);
-
- /* transfer our response to search_nmem .. we need it later */
- nmem_transfer(resultset->odr->mem, nmem);
- nmem_destroy(nmem);
- if (present_phase && p->num_records == 0)
- {
- /* present response and we didn't get any records! */
- Z_NamePlusRecord *myrec =
- zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
- record_cache_add(resultset, myrec, *start,
- syntax, elementSetName);
- }
- }
- else if (present_phase)
- {
- /* present response and we didn't get any records! */
- Z_NamePlusRecord *myrec =
- zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
- record_cache_add(resultset, myrec, *start, syntax, elementSetName);
- }
- }
-}
-
-static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
-{
- handle_records(c, pr->records, 1);
-}
-
-static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
- Z_Term *term)
-{
- switch (term->which)
- {
- case Z_Term_general:
- ZOOM_options_setl(opt, name,
- (const char *)(term->u.general->buf),
- term->u.general->len);
- break;
- case Z_Term_characterString:
- ZOOM_options_set(opt, name, term->u.characterString);
- break;
- case Z_Term_numeric:
- ZOOM_options_set_int(opt, name, *term->u.numeric);
- break;
- }
-}
-
-static void handle_queryExpression(ZOOM_options opt, const char *name,
- Z_QueryExpression *exp)
-{
- char opt_name[80];
-
- switch (exp->which)
- {
- case Z_QueryExpression_term:
- if (exp->u.term && exp->u.term->queryTerm)
- {
- sprintf(opt_name, "%s.term", name);
- handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
- }
- break;
- case Z_QueryExpression_query:
- break;
- }
-}
-
-static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
- Z_OtherInformation *o)
-{
- int i;
- for (i = 0; o && i < o->num_elements; i++)
- {
- if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
- {
- Z_External *ext = o->list[i]->information.externallyDefinedInfo;
-
- if (ext->which == Z_External_searchResult1)
- {
- int j;
- Z_SearchInfoReport *sr = ext->u.searchResult1;
-
- if (sr->num)
- ZOOM_options_set_int(
- resultset->options, "searchresult.size", sr->num);
-
- for (j = 0; j < sr->num; j++)
- {
- Z_SearchInfoReport_s *ent =
- ext->u.searchResult1->elements[j];
- char pref[80];
-
- sprintf(pref, "searchresult.%d", j);
-
- if (ent->subqueryId)
- {
- char opt_name[80];
- sprintf(opt_name, "%s.id", pref);
- ZOOM_options_set(resultset->options, opt_name,
- ent->subqueryId);
- }
- if (ent->subqueryExpression)
- {
- char opt_name[80];
- sprintf(opt_name, "%s.subquery", pref);
- handle_queryExpression(resultset->options, opt_name,
- ent->subqueryExpression);
- }
- if (ent->subqueryInterpretation)
- {
- char opt_name[80];
- sprintf(opt_name, "%s.interpretation", pref);
- handle_queryExpression(resultset->options, opt_name,
- ent->subqueryInterpretation);
- }
- if (ent->subqueryRecommendation)
- {
- char opt_name[80];
- sprintf(opt_name, "%s.recommendation", pref);
- handle_queryExpression(resultset->options, opt_name,
- ent->subqueryRecommendation);
- }
- if (ent->subqueryCount)
- {
- char opt_name[80];
- sprintf(opt_name, "%s.count", pref);
- ZOOM_options_set_int(resultset->options, opt_name,
- *ent->subqueryCount);
- }
- }
- }
- }
- }
-}
-
-static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
-{
- ZOOM_resultset resultset;
- ZOOM_Event event;
-
- if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
- return ;
-
- event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
- ZOOM_connection_put_event(c, event);
-
- resultset = c->tasks->u.search.resultset;
-
- handle_searchResult(c, resultset, sr->additionalSearchInfo);
-
- resultset->size = *sr->resultCount;
- handle_records(c, sr->records, 0);
-}
-
-static void sort_response(ZOOM_connection c, Z_SortResponse *res)
-{
- if (res->diagnostics && res->num_diagnostics > 0)
- response_diag(c, res->diagnostics[0]);
-}
-
-static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
-{
- NMEM nmem = odr_extract_mem(c->odr_in);
- ZOOM_scanset scan;
-
- if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
- return 0;
- scan = c->tasks->u.scan.scan;
-
- if (res->entries && res->entries->nonsurrogateDiagnostics)
- response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
- scan->scan_response = res;
- nmem_transfer(scan->odr->mem, nmem);
- if (res->stepSize)
- ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
- if (res->positionOfTerm)
- ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
- if (res->scanStatus)
- ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
- if (res->numberOfEntriesReturned)
- ZOOM_options_set_int(scan->options, "number",
- *res->numberOfEntriesReturned);
- nmem_destroy(nmem);
- return 1;
-}