00001
00002
00003
00004
00005
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <ctype.h>
00013 #if HAVE_UNISTD_H
00014 #include <unistd.h>
00015 #endif
00016
00017 #include <yaz/log.h>
00018 #include <yaz/backend.h>
00019 #include <yaz/ill.h>
00020 #include <yaz/diagbib1.h>
00021
00022 #include "ztest.h"
00023
00024 static int log_level=0;
00025 static int log_level_set=0;
00026
00027 int ztest_search(void *handle, bend_search_rr *rr);
00028 int ztest_sort(void *handle, bend_sort_rr *rr);
00029 int ztest_present(void *handle, bend_present_rr *rr);
00030 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
00031 int ztest_delete(void *handle, bend_delete_rr *rr);
00032
00041 static int get_term_hit(Z_RPNStructure *s)
00042 {
00043 int h = -1;
00044 switch(s->which)
00045 {
00046 case Z_RPNStructure_simple:
00047 if (s->u.simple->which == Z_Operand_APT)
00048 {
00049 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
00050 if (apt->term->which == Z_Term_general)
00051 {
00052 Odr_oct *oct = apt->term->u.general;
00053 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
00054 h = atoi_n((const char *) oct->buf, oct->len);
00055 }
00056 }
00057 break;
00058 case Z_RPNStructure_complex:
00059 h = get_term_hit(s->u.complex->s1);
00060 if (h == -1)
00061 h = get_term_hit(s->u.complex->s2);
00062 break;
00063 }
00064 return h;
00065 }
00066
00076 static int get_hit_count(Z_Query *q)
00077 {
00078 int h = -1;
00079 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
00080 h = get_term_hit(q->u.type_1->RPNStructure);
00081 if (h == -1)
00082 h = rand() % 24;
00083 return h;
00084 }
00085
00086 int ztest_search(void *handle, bend_search_rr *rr)
00087 {
00088 if (rr->num_bases != 1)
00089 {
00090 rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
00091 return 0;
00092 }
00093
00094 if (!yaz_matchstr (rr->basenames[0], "Default"))
00095 ;
00096 else if(!yaz_matchstr (rr->basenames[0], "Slow"))
00097 {
00098 #if HAVE_UNISTD_H
00099
00100 int i;
00101 for (i = 0; i<3; i++)
00102 {
00103 if (!bend_assoc_is_alive(rr->association))
00104 {
00105 yaz_log(YLOG_LOG, "search aborted");
00106 break;
00107 }
00108 sleep(1);
00109 }
00110 #endif
00111 rr->estimated_hit_count = 1;
00112 }
00113 else
00114 {
00115 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
00116 rr->errstring = rr->basenames[0];
00117 return 0;
00118 }
00119
00120 rr->hits = get_hit_count(rr->query);
00121 return 0;
00122 }
00123
00124
00125
00126 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
00127 {
00128
00129 int *counter = (int*) handle;
00130
00131 yaz_log(log_level, "ESRequest no %d", *counter);
00132
00133 (*counter)++;
00134
00135 if (rr->esr->packageName)
00136 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
00137 yaz_log(log_level, "Waitaction: %d", *rr->esr->waitAction);
00138
00139
00140 yaz_log(log_level, "function: %d", *rr->esr->function);
00141
00142 if (!rr->esr->taskSpecificParameters)
00143 {
00144 yaz_log (log_level, "No task specific parameters");
00145 }
00146 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
00147 {
00148 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
00149 yaz_log (log_level, "Received ItemOrder");
00150 if (it->which == Z_IOItemOrder_esRequest)
00151 {
00152 Z_IORequest *ir = it->u.esRequest;
00153 Z_IOOriginPartToKeep *k = ir->toKeep;
00154 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
00155 const char *xml_in_response = 0;
00156
00157 if (k && k->contact)
00158 {
00159 if (k->contact->name)
00160 yaz_log(log_level, "contact name %s", k->contact->name);
00161 if (k->contact->phone)
00162 yaz_log(log_level, "contact phone %s", k->contact->phone);
00163 if (k->contact->email)
00164 yaz_log(log_level, "contact email %s", k->contact->email);
00165 }
00166 if (k->addlBilling)
00167 {
00168 yaz_log(log_level, "Billing info (not shown)");
00169 }
00170
00171 if (n->resultSetItem)
00172 {
00173 yaz_log(log_level, "resultsetItem");
00174 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
00175 yaz_log(log_level, "item: %d", *n->resultSetItem->item);
00176 }
00177 if (n->itemRequest)
00178 {
00179 Z_External *r = (Z_External*) n->itemRequest;
00180 ILL_ItemRequest *item_req = 0;
00181 ILL_APDU *ill_apdu = 0;
00182 if (r->direct_reference)
00183 {
00184 char oid_name_str[OID_STR_MAX];
00185 oid_class oclass;
00186 const char *oid_name =
00187 yaz_oid_to_string_buf(r->direct_reference,
00188 &oclass, oid_name_str);
00189 if (oid_name)
00190 yaz_log(log_level, "OID %s", oid_name);
00191 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
00192 {
00193 yaz_log (log_level, "ILL XML request");
00194 if (r->which == Z_External_octet)
00195 yaz_log (log_level, "%.*s",
00196 r->u.octet_aligned->len,
00197 r->u.octet_aligned->buf);
00198 xml_in_response = "<dummy>x</dummy>";
00199 }
00200 if (!oid_oidcmp(r->direct_reference,
00201 yaz_oid_general_isoill_1))
00202 {
00203 yaz_log (log_level, "Decode ItemRequest begin");
00204 if (r->which == ODR_EXTERNAL_single)
00205 {
00206 odr_setbuf(rr->decode,
00207 (char *) r->u.single_ASN1_type->buf,
00208 r->u.single_ASN1_type->len, 0);
00209
00210 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
00211 {
00212 yaz_log (log_level,
00213 "Couldn't decode ItemRequest %s near %ld",
00214 odr_errmsg(odr_geterror(rr->decode)),
00215 (long) odr_offset(rr->decode));
00216 }
00217 else
00218 yaz_log(log_level, "Decode ItemRequest OK");
00219 if (rr->print)
00220 {
00221 ill_ItemRequest (rr->print, &item_req, 0,
00222 "ItemRequest");
00223 odr_reset (rr->print);
00224 }
00225 }
00226 if (!item_req && r->which == ODR_EXTERNAL_single)
00227 {
00228 yaz_log (log_level, "Decode ILL APDU begin");
00229 odr_setbuf(rr->decode,
00230 (char*) r->u.single_ASN1_type->buf,
00231 r->u.single_ASN1_type->len, 0);
00232
00233 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
00234 {
00235 yaz_log (log_level,
00236 "Couldn't decode ILL APDU %s near %ld",
00237 odr_errmsg(odr_geterror(rr->decode)),
00238 (long) odr_offset(rr->decode));
00239 yaz_log(log_level, "PDU dump:");
00240 odr_dumpBER(yaz_log_file(),
00241 (char *) r->u.single_ASN1_type->buf,
00242 r->u.single_ASN1_type->len);
00243 }
00244 else
00245 yaz_log(log_level, "Decode ILL APDU OK");
00246 if (rr->print)
00247 {
00248 ill_APDU (rr->print, &ill_apdu, 0,
00249 "ILL APDU");
00250 odr_reset (rr->print);
00251 }
00252 }
00253 }
00254 }
00255 if (item_req)
00256 {
00257 yaz_log (log_level, "ILL protocol version = %d",
00258 *item_req->protocol_version_num);
00259 }
00260 }
00261 if (k)
00262 {
00263
00264 Z_External *ext = (Z_External *)
00265 odr_malloc (rr->stream, sizeof(*ext));
00266 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
00267 odr_malloc (rr->stream, sizeof(*keep));
00268 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
00269 odr_malloc (rr->stream, sizeof(*targetPart));
00270
00271 rr->taskPackage = (Z_TaskPackage *)
00272 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
00273 rr->taskPackage->packageType =
00274 odr_oiddup (rr->stream, rr->esr->packageType);
00275 rr->taskPackage->packageName = 0;
00276 rr->taskPackage->userId = 0;
00277 rr->taskPackage->retentionTime = 0;
00278 rr->taskPackage->permissions = 0;
00279 rr->taskPackage->description = 0;
00280 rr->taskPackage->targetReference = (Odr_oct *)
00281 odr_malloc (rr->stream, sizeof(Odr_oct));
00282 rr->taskPackage->targetReference->buf =
00283 (unsigned char *) odr_strdup (rr->stream, "911");
00284 rr->taskPackage->targetReference->len =
00285 rr->taskPackage->targetReference->size =
00286 strlen((char *) (rr->taskPackage->targetReference->buf));
00287 rr->taskPackage->creationDateTime = 0;
00288 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
00289 rr->taskPackage->packageDiagnostics = 0;
00290 rr->taskPackage->taskSpecificParameters = ext;
00291
00292 ext->direct_reference =
00293 odr_oiddup (rr->stream, rr->esr->packageType);
00294 ext->indirect_reference = 0;
00295 ext->descriptor = 0;
00296 ext->which = Z_External_itemOrder;
00297 ext->u.itemOrder = (Z_ItemOrder *)
00298 odr_malloc (rr->stream, sizeof(*ext->u.update));
00299 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
00300 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
00301 odr_malloc (rr->stream, sizeof(Z_IOTaskPackage));
00302 ext->u.itemOrder->u.taskPackage->originPart = k;
00303 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
00304
00305 if (xml_in_response)
00306 targetPart->itemRequest =
00307 z_ext_record_xml(rr->stream, xml_in_response,
00308 strlen(xml_in_response));
00309 else
00310 targetPart->itemRequest = 0;
00311
00312 targetPart->statusOrErrorReport = 0;
00313 targetPart->auxiliaryStatus = 0;
00314 }
00315 }
00316 }
00317 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
00318 {
00319 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
00320 yaz_log (log_level, "Received DB Update");
00321 if (up->which == Z_IUUpdate_esRequest)
00322 {
00323 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
00324 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
00325 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
00326
00327 yaz_log (log_level, "action");
00328 if (toKeep->action)
00329 {
00330 switch (*toKeep->action)
00331 {
00332 case Z_IUOriginPartToKeep_recordInsert:
00333 yaz_log (log_level, " recordInsert");
00334 break;
00335 case Z_IUOriginPartToKeep_recordReplace:
00336 yaz_log (log_level, " recordReplace");
00337 break;
00338 case Z_IUOriginPartToKeep_recordDelete:
00339 yaz_log (log_level, " recordDelete");
00340 break;
00341 case Z_IUOriginPartToKeep_elementUpdate:
00342 yaz_log (log_level, " elementUpdate");
00343 break;
00344 case Z_IUOriginPartToKeep_specialUpdate:
00345 yaz_log (log_level, " specialUpdate");
00346 break;
00347 default:
00348 yaz_log (log_level, " unknown (%d)", *toKeep->action);
00349 }
00350 }
00351 if (toKeep->databaseName)
00352 {
00353 yaz_log (log_level, "database: %s", toKeep->databaseName);
00354 if (!strcmp(toKeep->databaseName, "fault"))
00355 {
00356 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
00357 rr->errstring = toKeep->databaseName;
00358 }
00359 if (!strcmp(toKeep->databaseName, "accept"))
00360 rr->errcode = -1;
00361 }
00362 if (toKeep)
00363 {
00364 Z_External *ext = (Z_External *)
00365 odr_malloc (rr->stream, sizeof(*ext));
00366 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
00367 odr_malloc (rr->stream, sizeof(*keep));
00368 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
00369 odr_malloc (rr->stream, sizeof(*targetPart));
00370
00371 rr->taskPackage = (Z_TaskPackage *)
00372 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
00373 rr->taskPackage->packageType =
00374 odr_oiddup (rr->stream, rr->esr->packageType);
00375 rr->taskPackage->packageName = 0;
00376 rr->taskPackage->userId = 0;
00377 rr->taskPackage->retentionTime = 0;
00378 rr->taskPackage->permissions = 0;
00379 rr->taskPackage->description = 0;
00380 rr->taskPackage->targetReference = (Odr_oct *)
00381 odr_malloc (rr->stream, sizeof(Odr_oct));
00382 rr->taskPackage->targetReference->buf =
00383 (unsigned char *) odr_strdup (rr->stream, "123");
00384 rr->taskPackage->targetReference->len =
00385 rr->taskPackage->targetReference->size =
00386 strlen((char *) (rr->taskPackage->targetReference->buf));
00387 rr->taskPackage->creationDateTime = 0;
00388 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
00389 rr->taskPackage->packageDiagnostics = 0;
00390 rr->taskPackage->taskSpecificParameters = ext;
00391
00392 ext->direct_reference =
00393 odr_oiddup (rr->stream, rr->esr->packageType);
00394 ext->indirect_reference = 0;
00395 ext->descriptor = 0;
00396 ext->which = Z_External_update;
00397 ext->u.update = (Z_IUUpdate *)
00398 odr_malloc (rr->stream, sizeof(*ext->u.update));
00399 ext->u.update->which = Z_IUUpdate_taskPackage;
00400 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
00401 odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
00402 ext->u.update->u.taskPackage->originPart = keep;
00403 ext->u.update->u.taskPackage->targetPart = targetPart;
00404
00405 keep->action = (int *) odr_malloc (rr->stream, sizeof(int));
00406 *keep->action = *toKeep->action;
00407 keep->databaseName =
00408 odr_strdup (rr->stream, toKeep->databaseName);
00409 keep->schema = 0;
00410 keep->elementSetName = 0;
00411 keep->actionQualifier = 0;
00412
00413 targetPart->updateStatus = odr_intdup (rr->stream, 1);
00414 targetPart->num_globalDiagnostics = 0;
00415 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
00416 targetPart->num_taskPackageRecords = 1;
00417 targetPart->taskPackageRecords =
00418 (Z_IUTaskPackageRecordStructure **)
00419 odr_malloc (rr->stream,
00420 sizeof(Z_IUTaskPackageRecordStructure *));
00421 targetPart->taskPackageRecords[0] =
00422 (Z_IUTaskPackageRecordStructure *)
00423 odr_malloc (rr->stream,
00424 sizeof(Z_IUTaskPackageRecordStructure));
00425
00426 targetPart->taskPackageRecords[0]->which =
00427 Z_IUTaskPackageRecordStructure_record;
00428 targetPart->taskPackageRecords[0]->u.record =
00429 z_ext_record_sutrs(rr->stream, "test", 4);
00430 targetPart->taskPackageRecords[0]->correlationInfo = 0;
00431 targetPart->taskPackageRecords[0]->recordStatus =
00432 odr_intdup (rr->stream,
00433 Z_IUTaskPackageRecordStructure_success);
00434 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
00435 = 0;
00436
00437 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
00438 }
00439 if (notToKeep)
00440 {
00441 int i;
00442 for (i = 0; i < notToKeep->num; i++)
00443 {
00444 Z_External *rec = notToKeep->elements[i]->record;
00445
00446 if (rec->direct_reference)
00447 {
00448 char oid_name_str[OID_STR_MAX];
00449 const char *oid_name
00450 = oid_name = yaz_oid_to_string_buf(
00451 rec->direct_reference, 0,
00452 oid_name_str);
00453 if (oid_name)
00454 yaz_log (log_level, "record %d type %s", i,
00455 oid_name);
00456 }
00457 switch (rec->which)
00458 {
00459 case Z_External_sutrs:
00460 if (rec->u.octet_aligned->len > 170)
00461 yaz_log (log_level, "%d bytes:\n%.168s ...",
00462 rec->u.sutrs->len,
00463 rec->u.sutrs->buf);
00464 else
00465 yaz_log (log_level, "%d bytes:\n%s",
00466 rec->u.sutrs->len,
00467 rec->u.sutrs->buf);
00468 break;
00469 case Z_External_octet :
00470 if (rec->u.octet_aligned->len > 170)
00471 yaz_log (log_level, "%d bytes:\n%.168s ...",
00472 rec->u.octet_aligned->len,
00473 rec->u.octet_aligned->buf);
00474 else
00475 yaz_log (log_level, "%d bytes\n%s",
00476 rec->u.octet_aligned->len,
00477 rec->u.octet_aligned->buf);
00478 }
00479 }
00480 }
00481 }
00482 }
00483 return 0;
00484 }
00485
00486
00487 int ztest_delete (void *handle, bend_delete_rr *rr)
00488 {
00489 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
00490 rr->delete_status = Z_DeleteStatus_success;
00491 else
00492 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
00493 return 0;
00494 }
00495
00496
00497 int ztest_sort (void *handle, bend_sort_rr *rr)
00498 {
00499 rr->errcode = 0;
00500 rr->sort_status = Z_SortResponse_success;
00501 return 0;
00502 }
00503
00504
00505
00506 int ztest_present (void *handle, bend_present_rr *rr)
00507 {
00508 return 0;
00509 }
00510
00511
00512 int ztest_fetch(void *handle, bend_fetch_rr *r)
00513 {
00514 char *cp;
00515 const Odr_oid *oid = r->request_format;
00516
00517 r->last_in_set = 0;
00518 r->basename = "Default";
00519 r->output_format = r->request_format;
00520
00521 if (!oid || yaz_oid_is_iso2709(oid))
00522 {
00523 cp = dummy_marc_record(r->number, r->stream);
00524 if (!cp)
00525 {
00526 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
00527 return 0;
00528 }
00529 else
00530 {
00531 r->len = strlen(cp);
00532 r->record = cp;
00533 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
00534 }
00535 }
00536 else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
00537 {
00538 cp = dummy_marc_record(r->number, r->stream);
00539 if (!cp)
00540 {
00541 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
00542 return 0;
00543 }
00544 r->record = (char *) dummy_opac(r->number, r->stream, cp);
00545 r->len = -1;
00546 }
00547 else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
00548 {
00549
00550 char buf[100];
00551
00552 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
00553
00554 r->len = strlen(buf);
00555 r->record = (char *) odr_malloc (r->stream, r->len+1);
00556 strcpy(r->record, buf);
00557 }
00558 else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
00559 {
00560 r->len = -1;
00561 r->record = (char*) dummy_grs_record(r->number, r->stream);
00562 if (!r->record)
00563 {
00564 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
00565 return 0;
00566 }
00567 }
00568 else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
00569 {
00570 char fname[20];
00571 FILE *f;
00572 long size;
00573
00574 sprintf (fname, "part.%d.ps", r->number);
00575 f = fopen(fname, "rb");
00576 if (!f)
00577 {
00578 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
00579 return 0;
00580 }
00581 fseek (f, 0L, SEEK_END);
00582 size = ftell (f);
00583 if (size <= 0 || size >= 5000000)
00584 {
00585 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
00586 return 0;
00587 }
00588 fseek (f, 0L, SEEK_SET);
00589 r->record = (char*) odr_malloc (r->stream, size);
00590 r->len = size;
00591 fread (r->record, size, 1, f);
00592 fclose (f);
00593 }
00594 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
00595 {
00596 if ((cp = dummy_xml_record (r->number, r->stream)))
00597 {
00598 r->len = strlen(cp);
00599 r->record = cp;
00600 }
00601 else
00602 {
00603 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
00604 r->surrogate_flag = 1;
00605 return 0;
00606 }
00607 }
00608 else
00609 {
00610 char buf[OID_STR_MAX];
00611 r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
00612 r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
00613 return 0;
00614 }
00615 r->errcode = 0;
00616 return 0;
00617 }
00618
00619
00620
00621
00622 int ztest_scan(void *handle, bend_scan_rr *q)
00623 {
00624 static FILE *f = 0;
00625 static struct scan_entry list[200];
00626 static char entries[200][80];
00627 int hits[200];
00628 char term[80], *p;
00629 int i, pos;
00630 int term_position_req = q->term_position;
00631 int num_entries_req = q->num_entries;
00632
00633
00634 if (!yaz_matchstr (q->basenames[0], "Default"))
00635 ;
00636 else if(!yaz_matchstr (q->basenames[0], "Slow"))
00637 {
00638 #if HAVE_UNISTD_H
00639 sleep(3);
00640 #endif
00641 ;
00642 }
00643 else
00644 {
00645 q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
00646 q->errstring = q->basenames[0];
00647 return 0;
00648 }
00649
00650 q->errcode = 0;
00651 q->errstring = 0;
00652 q->entries = list;
00653 q->status = BEND_SCAN_SUCCESS;
00654 if (!f && !(f = fopen("dummy-words", "r")))
00655 {
00656 perror("dummy-words");
00657 exit(1);
00658 }
00659 if (q->num_entries > 200)
00660 {
00661 q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
00662 return 0;
00663 }
00664 if (q->term)
00665 {
00666 int len;
00667 if (q->term->term->which != Z_Term_general)
00668 {
00669 q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
00670 return 0;
00671 }
00672 if (*q->step_size != 0)
00673 {
00674 q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
00675 return 0;
00676 }
00677 len = q->term->term->u.general->len;
00678 if (len >= sizeof(term))
00679 len = sizeof(term)-1;
00680 memcpy(term, q->term->term->u.general->buf, len);
00681 term[len] = '\0';
00682 }
00683 else if (q->scanClause)
00684 {
00685 strncpy(term, q->scanClause, sizeof(term)-1);
00686 term[sizeof(term)-1] = '\0';
00687 }
00688 else
00689 strcpy(term, "0");
00690
00691 for (p = term; *p; p++)
00692 if (islower(*(unsigned char *) p))
00693 *p = toupper(*p);
00694
00695 fseek(f, 0, SEEK_SET);
00696 q->num_entries = 0;
00697
00698 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
00699 i++, pos < 199 ? pos++ : (pos = 0))
00700 {
00701 if (!q->num_entries && strcmp(entries[pos], term) >= 0)
00702 {
00703 if ((q->term_position = term_position_req) > i + 1)
00704 {
00705 q->term_position = i + 1;
00706 q->status = BEND_SCAN_PARTIAL;
00707 }
00708 for (; q->num_entries < q->term_position; q->num_entries++)
00709 {
00710 int po;
00711
00712 po = pos - q->term_position + q->num_entries+1;
00713 if (po < 0)
00714 po += 200;
00715
00716 if (!strcmp (term, "SD") && q->num_entries == 2)
00717 {
00718 list[q->num_entries].term = entries[pos];
00719 list[q->num_entries].occurrences = -1;
00720 list[q->num_entries].errcode =
00721 YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
00722 list[q->num_entries].errstring = "SD for Scan Term";
00723 }
00724 else
00725 {
00726 list[q->num_entries].term = entries[po];
00727 list[q->num_entries].occurrences = hits[po];
00728 }
00729 }
00730 }
00731 else if (q->num_entries)
00732 {
00733 list[q->num_entries].term = entries[pos];
00734 list[q->num_entries].occurrences = hits[pos];
00735 q->num_entries++;
00736 }
00737 if (q->num_entries >= num_entries_req)
00738 break;
00739 }
00740 if (feof(f))
00741 q->status = BEND_SCAN_PARTIAL;
00742 return 0;
00743 }
00744
00745 int ztest_explain(void *handle, bend_explain_rr *rr)
00746 {
00747 if (rr->database && !strcmp(rr->database, "Default"))
00748 {
00749 rr->explain_buf = "<explain>\n"
00750 "\t<serverInfo>\n"
00751 "\t\t<host>localhost</host>\n"
00752 "\t\t<port>210</port>\n"
00753 "\t</serverInfo>\n"
00754 "</explain>\n";
00755 }
00756 return 0;
00757 }
00758
00759 int ztest_update(void *handle, bend_update_rr *rr)
00760 {
00761 rr->operation_status = "success";
00762 return 0;
00763 }
00764
00765 bend_initresult *bend_init(bend_initrequest *q)
00766 {
00767 bend_initresult *r = (bend_initresult *)
00768 odr_malloc (q->stream, sizeof(*r));
00769 int *counter = (int *) xmalloc (sizeof(int));
00770
00771 if (!log_level_set)
00772 {
00773 log_level=yaz_log_module_level("ztest");
00774 log_level_set=1;
00775 }
00776
00777 *counter = 0;
00778 r->errcode = 0;
00779 r->errstring = 0;
00780 r->handle = counter;
00781 q->bend_sort = ztest_sort;
00782 q->bend_search = ztest_search;
00783 q->bend_present = ztest_present;
00784 q->bend_esrequest =