-}
-
-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;
-
- if (sr->resultSetStatus)
- {
- ZOOM_options_set_int(resultset->options, "resultSetStatus",
- *sr->resultSetStatus);
- }
- if (sr->presentStatus)
- {
- ZOOM_options_set_int(resultset->options, "presentStatus",
- *sr->presentStatus);
- }
- 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;
-}
-
-static zoom_ret send_sort(ZOOM_connection c,
- ZOOM_resultset resultset)
-{
- if (c->error)
- resultset->r_sort_spec = 0;
- if (resultset->r_sort_spec)
- {
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
- Z_SortRequest *req = apdu->u.sortRequest;
-
- req->num_inputResultSetNames = 1;
- req->inputResultSetNames = (Z_InternationalString **)
- odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
- req->inputResultSetNames[0] =
- odr_strdup(c->odr_out, resultset->setname);
- req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
- req->sortSequence = resultset->r_sort_spec;
- resultset->r_sort_spec = 0;
- return send_APDU(c, apdu);
- }
- return zoom_complete;
-}
-
-static zoom_ret send_present(ZOOM_connection c)
-{
- Z_APDU *apdu = 0;
- Z_PresentRequest *req = 0;
- int i = 0;
- const char *syntax = 0;
- const char *elementSetName = 0;
- ZOOM_resultset resultset;
- int *start, *count;
-
- if (!c->tasks)
- {
- yaz_log(log_details, "%p send_present no tasks", c);
- return zoom_complete;
- }
-
- 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 zoom_complete;
- }
- yaz_log(log_details, "%p send_present start=%d count=%d",
- c, *start, *count);
-
- if (*start < 0 || *count < 0 || *start + *count > resultset->size)
- {
- set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
- "", 0);
- }
- if (c->error) /* don't continue on error */
- return zoom_complete;
- yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
- resultset, *start, *count);
-
- for (i = 0; i < *count; i++)
- {
- ZOOM_record rec =
- record_cache_lookup(resultset, i + *start, syntax, 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)
- {
- yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
- return zoom_complete;
- }
-
- apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
- req = apdu->u.presentRequest;
-
- if (i)
- yaz_log(log_details, "%p send_present skip=%d", c, i);
-
- *req->resultSetStartPoint = *start + 1;
- *req->numberOfRecordsRequested = resultset->step>0 ?
- resultset->step : *count;
- if (*req->numberOfRecordsRequested + *start > resultset->size)
- *req->numberOfRecordsRequested = resultset->size - *start;
- assert(*req->numberOfRecordsRequested > 0);
-
- if (syntax && *syntax)
- req->preferredRecordSyntax =
- zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
-
- if (resultset->schema && *resultset->schema)
- {
- Z_RecordComposition *compo = (Z_RecordComposition *)
- odr_malloc(c->odr_out, sizeof(*compo));
-
- req->recordComposition = compo;
- compo->which = Z_RecordComp_complex;
- compo->u.complex = (Z_CompSpec *)
- odr_malloc(c->odr_out, sizeof(*compo->u.complex));
- compo->u.complex->selectAlternativeSyntax = (bool_t *)
- odr_malloc(c->odr_out, sizeof(bool_t));
- *compo->u.complex->selectAlternativeSyntax = 0;
-
- compo->u.complex->generic = (Z_Specification *)
- odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
-
- compo->u.complex->generic->which = Z_Schema_oid;
- compo->u.complex->generic->schema.oid = (Odr_oid *)
- zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
-
- if (!compo->u.complex->generic->schema.oid)
- {
- /* OID wasn't a schema! Try record syntax instead. */
-
- compo->u.complex->generic->schema.oid = (Odr_oid *)
- zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
- }
- if (elementSetName && *elementSetName)
- {
- compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
- odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
- compo->u.complex->generic->elementSpec->which =
- Z_ElementSpec_elementSetName;
- compo->u.complex->generic->elementSpec->u.elementSetName =
- odr_strdup(c->odr_out, elementSetName);
- }
- else
- compo->u.complex->generic->elementSpec = 0;
- compo->u.complex->num_dbSpecific = 0;
- compo->u.complex->dbSpecific = 0;
- compo->u.complex->num_recordSyntax = 0;
- compo->u.complex->recordSyntax = 0;
- }
- else if (elementSetName && *elementSetName)
- {
- Z_ElementSetNames *esn = (Z_ElementSetNames *)
- odr_malloc(c->odr_out, sizeof(*esn));
- Z_RecordComposition *compo = (Z_RecordComposition *)
- odr_malloc(c->odr_out, sizeof(*compo));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup(c->odr_out, elementSetName);
- compo->which = Z_RecordComp_simple;
- compo->u.simple = esn;
- req->recordComposition = compo;
- }
- req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
- return send_APDU(c, apdu);
-}
-
-ZOOM_API(ZOOM_scanset)
- ZOOM_connection_scan_pqf(ZOOM_connection c, const char *start)
-{
- ZOOM_scanset s;
- ZOOM_query q = ZOOM_query_create();
-
- ZOOM_query_prefix(q, start);
-
- s = ZOOM_connection_scan(c, q);
- ZOOM_query_destroy(q);
- return s;
-
-}
-
-ZOOM_API(ZOOM_scanset)
- ZOOM_connection_scan(ZOOM_connection c, ZOOM_query q)
-{
- char *start;
- char *freeme = 0;
- ZOOM_scanset scan = 0;
-
- /*
- * We need to check the query-type, so we can recognise CQL and
- * CCL and compile them into a form that we can use here. The
- * ZOOM_query structure has no explicit `type' member, but
- * inspection of the ZOOM_query_prefix() and ZOOM_query_cql()
- * functions shows how the structure is set up in each case.
- */
- if (!q->z_query)
- return 0;
- else if (q->z_query->which == Z_Query_type_1)
- {
- yaz_log(log_api, "%p ZOOM_connection_scan1 q=%p PQF '%s'",
- c, q, q->query_string);
- start = q->query_string;
- }
- else if (q->z_query->which == Z_Query_type_104)
- {
- yaz_log(log_api, "%p ZOOM_connection_scan1 q=%p CQL '%s'",
- c, q, q->query_string);
- start = freeme = cql2pqf(c, q->query_string);
- if (start == 0)
- return 0;
- }
- else
- {
- yaz_log(YLOG_FATAL, "%p ZOOM_connection_scan1 q=%p unknown type '%s'",
- c, q, q->query_string);
- abort();
- }
-
- scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
- scan->connection = c;
- scan->odr = odr_createmem(ODR_DECODE);
- scan->options = ZOOM_options_create_with_parent(c->options);
- scan->refcount = 1;
- scan->scan_response = 0;
- scan->termListAndStartPoint =
- p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet, start);
- xfree(freeme);
-
- scan->databaseNames = set_DatabaseNames(c, c->options,
- &scan->num_databaseNames,
- scan->odr);
- if (scan->termListAndStartPoint != 0)
- {
- ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
- task->u.scan.scan = scan;
-
- (scan->refcount)++;
- if (!c->async)
- {
- while (ZOOM_event(1, &c))
- ;
- }
- }
- return scan;
-}
-
-ZOOM_API(void)
- ZOOM_scanset_destroy(ZOOM_scanset scan)
-{
- if (!scan)
- return;
- (scan->refcount)--;
- if (scan->refcount == 0)
- {
- odr_destroy(scan->odr);
-
- ZOOM_options_destroy(scan->options);
- xfree(scan);
- }
-}
-
-static zoom_ret send_package(ZOOM_connection c)
-{
- ZOOM_Event event;
-
- yaz_log(log_details, "%p send_package", c);
- if (!c->tasks)
- return zoom_complete;
- assert (c->tasks->which == ZOOM_TASK_PACKAGE);
-
- event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event(c, event);
-
- c->buf_out = c->tasks->u.package->buf_out;
- c->len_out = c->tasks->u.package->len_out;
-
- return do_write(c);
-}
-
-static zoom_ret send_scan(ZOOM_connection c)
-{
- ZOOM_scanset scan;
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
- Z_ScanRequest *req = apdu->u.scanRequest;
-
- yaz_log(log_details, "%p send_scan", c);
- if (!c->tasks)
- return zoom_complete;
- assert (c->tasks->which == ZOOM_TASK_SCAN);
- scan = c->tasks->u.scan.scan;
-
- req->termListAndStartPoint = scan->termListAndStartPoint;
- req->attributeSet = scan->attributeSet;
-
- *req->numberOfTermsRequested =
- ZOOM_options_get_int(scan->options, "number", 10);
-
- req->preferredPositionInResponse =
- odr_intdup(c->odr_out,
- ZOOM_options_get_int(scan->options, "position", 1));
-
- req->stepSize =
- odr_intdup(c->odr_out,
- ZOOM_options_get_int(scan->options, "stepSize", 0));
-
- req->databaseNames = scan->databaseNames;
- req->num_databaseNames = scan->num_databaseNames;
-
- return send_APDU(c, apdu);
-}
-
-ZOOM_API(size_t)
- ZOOM_scanset_size(ZOOM_scanset scan)
-{
- if (!scan || !scan->scan_response || !scan->scan_response->entries)
- return 0;
- return scan->scan_response->entries->num_entries;
-}
-
-ZOOM_API(const char *)
- ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
- int *occ, int *len)
-{
- const char *term = 0;
- size_t noent = ZOOM_scanset_size(scan);
- Z_ScanResponse *res = scan->scan_response;
-
- *len = 0;
- *occ = 0;
- if (pos >= noent)
- return 0;
- if (res->entries->entries[pos]->which == Z_Entry_termInfo)
- {
- Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
-
- if (t->term->which == Z_Term_general)
- {
- term = (const char *) t->term->u.general->buf;
- *len = t->term->u.general->len;
- }
- *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
- }
- return term;
-}
-
-ZOOM_API(const char *)
- ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
- int *occ, int *len)
-{
- const char *term = 0;
- size_t noent = ZOOM_scanset_size(scan);
- Z_ScanResponse *res = scan->scan_response;
-
- *len = 0;
- *occ = 0;
- if (pos >= noent)
- return 0;
- if (res->entries->entries[pos]->which == Z_Entry_termInfo)
- {
- Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
-
- if (t->displayTerm)
- {
- term = t->displayTerm;
- *len = strlen(term);
- }
- else if (t->term->which == Z_Term_general)
- {
- term = (const char *) t->term->u.general->buf;
- *len = t->term->u.general->len;
- }
- *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
- }
- return term;
-}
-
-ZOOM_API(const char *)
- ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
-{
- return ZOOM_options_get(scan->options, key);
-}
-
-ZOOM_API(int)
- ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
- const char *val)
-{
- ZOOM_options_set(scan->options, key, val);
- return 0;
-}
-
-static Z_APDU *create_es_package(ZOOM_package p, int type)
-{
- const char *str;
- Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
- Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
-
- *req->function = Z_ExtendedServicesRequest_create;
-
- str = ZOOM_options_get(p->options, "package-name");
- if (str && *str)
- req->packageName = nmem_strdup(p->odr_out->mem, str);
-
- str = ZOOM_options_get(p->options, "user-id");
- if (str)
- req->userId = nmem_strdup(p->odr_out->mem, str);
-
- req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
- type);
-
- str = ZOOM_options_get(p->options, "function");
- if (str)
- {
- if (!strcmp (str, "create"))
- *req->function = 1;
- if (!strcmp (str, "delete"))
- *req->function = 2;
- if (!strcmp (str, "modify"))
- *req->function = 3;
- }
- return apdu;
-}
-
-static const char *ill_array_lookup(void *clientData, const char *idx)
-{
- ZOOM_package p = (ZOOM_package) clientData;
- return ZOOM_options_get(p->options, idx+4);
-}
-
-static Z_External *encode_ill_request(ZOOM_package p)
-{
- ODR out = p->odr_out;
- ILL_Request *req;
- Z_External *r = 0;
- struct ill_get_ctl ctl;
-
- ctl.odr = p->odr_out;
- ctl.clientData = p;
- ctl.f = ill_array_lookup;
-
- req = ill_get_ILLRequest(&ctl, "ill", 0);
-
- if (!ill_Request(out, &req, 0, 0))
- {
- int ill_request_size;
- char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
- if (ill_request_buf)
- odr_setbuf(out, ill_request_buf, ill_request_size, 1);
- return 0;
- }
- else
- {
- oident oid;
- int illRequest_size = 0;
- char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
-
- oid.proto = PROTO_GENERAL;
- oid.oclass = CLASS_GENERAL;
- oid.value = VAL_ISO_ILL_1;
-
- r = (Z_External *) odr_malloc(out, sizeof(*r));
- r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
- r->indirect_reference = 0;
- r->descriptor = 0;
- r->which = Z_External_single;
-
- r->u.single_ASN1_type =
- odr_create_Odr_oct(out,
- (unsigned char *)illRequest_buf,
- illRequest_size);
- }
- return r;
-}
-
-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->u.esRequest = (Z_IORequest *)
- odr_malloc(p->odr_out,sizeof(Z_IORequest));
-
- /* to keep part ... */
- req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
- odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
- req->u.esRequest->toKeep->supplDescription = 0;
- req->u.esRequest->toKeep->contact = (Z_IOContact *)
- odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
-
- str = ZOOM_options_get(p->options, "contact-name");
- req->u.esRequest->toKeep->contact->name = str ?
- nmem_strdup(p->odr_out->mem, str) : 0;
-
- str = ZOOM_options_get(p->options, "contact-phone");
- req->u.esRequest->toKeep->contact->phone = str ?
- nmem_strdup(p->odr_out->mem, str) : 0;
-
- str = ZOOM_options_get(p->options, "contact-email");
- req->u.esRequest->toKeep->contact->email = str ?
- nmem_strdup(p->odr_out->mem, str) : 0;
-
- req->u.esRequest->toKeep->addlBilling = 0;
-
- /* not to keep part ... */
- req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
- odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
-
- str = ZOOM_options_get(p->options, "itemorder-setname");
- if (!str)
- str = "default";
-
- if (!*str)
- req->u.esRequest->notToKeep->resultSetItem = 0;
- else
- {
- req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
- odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
-
- req->u.esRequest->notToKeep->resultSetItem->resultSetId =
- nmem_strdup(p->odr_out->mem, str);
- req->u.esRequest->notToKeep->resultSetItem->item =
- (int *) odr_malloc(p->odr_out, sizeof(int));
-
- str = ZOOM_options_get(p->options, "itemorder-item");
- *req->u.esRequest->notToKeep->resultSetItem->item =
- (str ? atoi(str) : 1);
- }
-
- str = ZOOM_options_get(p->options, "doc");
- if (str)
- req->u.esRequest->notToKeep->itemRequest =
- z_ext_record(p->odr_out, VAL_TEXT_XML, str, strlen(str));
- else
- req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
-
- return req;
-}
-
-Z_APDU *create_admin_package(ZOOM_package p, int type,
- Z_ESAdminOriginPartToKeep **toKeepP,
- Z_ESAdminOriginPartNotToKeep **notToKeepP)
-{
- Z_APDU *apdu = create_es_package(p, VAL_ADMINSERVICE);
- if (apdu)