Number of Extended Services Requests logged.
[yaz-moved-to-github.git] / ztest / ztest.c
1 /*
2  * Copyright (c) 1995-2001, Index Data.
3  * See the file LICENSE for details.
4  *
5  * NT Service interface by
6  *    Chas Woodfield, Fretwell Downing Datasystems.
7  *
8  * $Log: ztest.c,v $
9  * Revision 1.43  2001-06-28 09:27:25  adam
10  * Number of Extended Services Requests logged.
11  *
12  * Revision 1.42  2001/04/06 12:26:46  adam
13  * Optional CCL module. Moved atoi_n to marcdisp.h from yaz-util.h.
14  *
15  * Revision 1.41  2001/04/05 13:08:48  adam
16  * New configure options: --enable-module.
17  *
18  * Revision 1.40  2001/03/25 21:55:13  adam
19  * Added odr_intdup. Ztest server returns TaskPackage for ItemUpdate.
20  *
21  * Revision 1.39  2001/03/12 14:40:57  adam
22  * Minor change of print of item update info.
23  *
24  * Revision 1.38  2001/02/21 13:46:54  adam
25  * C++ fixes.
26  *
27  * Revision 1.37  2001/02/20 11:25:32  adam
28  * Added ill_get_APDU and ill_get_Cancel.
29  *
30  * Revision 1.36  2001/01/30 21:34:18  adam
31  * Added step-size for Scan backend interface.
32  *
33  * Revision 1.35  2000/11/23 10:58:33  adam
34  * SSL comstack support. Separate POSIX thread support library.
35  *
36  * Revision 1.34  2000/09/04 08:58:15  adam
37  * Added prefix yaz_ for most logging utility functions.
38  *
39  * Revision 1.33  2000/08/10 08:41:26  adam
40  * Fixes for ILL.
41  *
42  * Revision 1.32  2000/04/05 07:39:55  adam
43  * Added shared library support (libtool).
44  *
45  * Revision 1.31  2000/01/31 13:15:21  adam
46  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
47  * that some characters are not surrounded by spaces in resulting term.
48  * ILL-code updates.
49  *
50  * Revision 1.30  2000/01/13 23:05:50  adam
51  * Fixed tagging for member requester-CHECKED-IN in ILL ASN.1 spec.
52  *
53  * Revision 1.29  2000/01/12 14:36:07  adam
54  * Added printing stream (ODR) for backend functions.
55  *
56  * Revision 1.28  1999/12/16 23:36:19  adam
57  * Implemented ILL protocol. Minor updates ASN.1 compiler.
58  *
59  * Revision 1.27  1999/11/30 13:47:12  adam
60  * Improved installation. Moved header files to include/yaz.
61  *
62  * Revision 1.26  1999/08/27 09:40:32  adam
63  * Renamed logf function to yaz_log. Removed VC++ project files.
64  *
65  * Revision 1.25  1999/06/01 14:29:12  adam
66  * Work on Extended Services.
67  *
68  * Revision 1.24  1999/05/27 13:07:54  adam
69  * Fix.
70  *
71  * Revision 1.23  1999/05/27 13:02:20  adam
72  * Assigned OID for old DB Update (VAL_DBUPDATE0).
73  *
74  * Revision 1.22  1999/05/26 13:49:12  adam
75  * DB Update implemented in client (very basic).
76  *
77  * Revision 1.21  1998/12/15 12:45:42  adam
78  * Minor change.
79  *
80  * Revision 1.20  1998/12/14 14:48:05  adam
81  * Fixed memory leak - happened when fetching MARC records.
82  *
83  * Revision 1.19  1998/10/20 15:16:22  adam
84  * Minor change to prevent warning.
85  *
86  * Revision 1.18  1998/10/20 15:13:45  adam
87  * Minor fix regarding output for Item Order.
88  *
89  * Revision 1.17  1998/10/18 22:33:35  quinn
90  * Added diagnostic dump of Item Order Eservice.
91  *
92  * Revision 1.16  1998/10/15 08:26:23  adam
93  * Added type cast to make C++ happy.
94  *
95  * Revision 1.15  1998/10/13 20:05:57  adam
96  * Minor change.
97  *
98  * Revision 1.14  1998/10/13 16:12:25  adam
99  * Added support for Surrogate Diagnostics for Scan Term entries.
100  *
101  * Revision 1.13  1998/08/19 16:10:09  adam
102  * Changed som member names of DeleteResultSetRequest/Response.
103  *
104  * Revision 1.12  1998/07/20 12:38:44  adam
105  * Implemented delete result set service to server API.
106  *
107  * Revision 1.11  1998/06/09 13:55:08  adam
108  * Minor changes.
109  *
110  * Revision 1.10  1998/05/27 16:55:54  adam
111  * Minor changes.
112  *
113  * Revision 1.9  1998/03/31 11:07:45  adam
114  * Furhter work on UNIverse resource report.
115  * Added Extended Services handling in frontend server.
116  *
117  * Revision 1.8  1998/02/11 11:53:36  adam
118  * Changed code so that it compiles as C++.
119  *
120  * Revision 1.7  1998/02/10 11:03:57  adam
121  * Added support for extended handlers in backend server interface.
122  *
123  * Revision 1.6  1998/01/29 13:16:02  adam
124  * Added dummy sort in test server.
125  *
126  * Revision 1.5  1997/11/07 13:31:58  adam
127  * Added NT Service name part of statserv_options_block. Moved NT
128  * service utility to server library.
129  *
130  * Revision 1.4  1997/09/17 12:10:43  adam
131  * YAZ version 1.4.
132  *
133  * Revision 1.3  1997/09/09 10:10:20  adam
134  * Another MSV5.0 port. Changed projects to include proper
135  * library/include paths.
136  * Server starts server in test-mode when no options are given.
137  *
138  * Revision 1.2  1997/09/04 13:50:31  adam
139  * Bug fix in ztest.
140  *
141  */
142
143 /*
144  * Demonstration of simple server
145  */
146
147 #include <stdio.h>
148 #include <stdlib.h>
149 #include <ctype.h>
150
151 #include <yaz/backend.h>
152 #include <yaz/log.h>
153
154 #if YAZ_MODULE_ill
155 #include <yaz/ill.h>
156 #endif
157
158 Z_GenericRecord *read_grs1(FILE *f, ODR o);
159
160 int ztest_search (void *handle, bend_search_rr *rr);
161 int ztest_sort (void *handle, bend_sort_rr *rr);
162 int ztest_present (void *handle, bend_present_rr *rr);
163 int ztest_esrequest (void *handle, bend_esrequest_rr *rr);
164 int ztest_delete (void *handle, bend_delete_rr *rr);
165
166 int ztest_search (void *handle, bend_search_rr *rr)
167 {
168     rr->hits = rand() % 22;
169     return 0;
170 }
171
172 int ztest_present (void *handle, bend_present_rr *rr)
173 {
174     return 0;
175 }
176
177 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
178 {
179     int *counter = handle;  /* user-defined handle - created in bend_init */
180
181     yaz_log(LOG_LOG, "ESRequest no %d", *counter);
182
183     (*counter)++;
184
185     if (rr->esr->packageName)
186         yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
187     yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
188
189
190     yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
191
192     if (!rr->esr->taskSpecificParameters)
193     {
194         yaz_log (LOG_WARN, "No task specific parameters");
195     }
196     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
197     {
198         Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
199         yaz_log (LOG_LOG, "Received ItemOrder");
200         switch (it->which)
201         {
202 #ifdef ASN_COMPILED
203         case Z_IOItemOrder_esRequest:
204 #else
205         case Z_ItemOrder_esRequest:
206 #endif
207         {
208             Z_IORequest *ir = it->u.esRequest;
209             Z_IOOriginPartToKeep *k = ir->toKeep;
210             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
211             
212             if (k && k->contact)
213             {
214                 if (k->contact->name)
215                     yaz_log(LOG_LOG, "contact name %s", k->contact->name);
216                 if (k->contact->phone)
217                     yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
218                 if (k->contact->email)
219                     yaz_log(LOG_LOG, "contact email %s", k->contact->email);
220             }
221             if (k->addlBilling)
222             {
223                 yaz_log(LOG_LOG, "Billing info (not shown)");
224             }
225             
226             if (n->resultSetItem)
227             {
228                 yaz_log(LOG_LOG, "resultsetItem");
229                 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
230                 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
231             }
232 #if YAZ_MODULE_ill
233             if (n->itemRequest)
234             {
235                 Z_External *r = (Z_External*) n->itemRequest;
236                 ILL_ItemRequest *item_req = 0;
237                 ILL_APDU *ill_apdu = 0;
238                 if (r->direct_reference)
239                 {
240                     oident *ent = oid_getentbyoid(r->direct_reference);
241                     if (ent)
242                         yaz_log(LOG_LOG, "OID %s", ent->desc);
243                     if (ent && ent->value == VAL_TEXT_XML)
244                     {
245                         yaz_log (LOG_LOG, "ILL XML request");
246                         if (r->which == Z_External_octet)
247                             yaz_log (LOG_LOG, "%.*s", r->u.octet_aligned->len,
248                                      r->u.octet_aligned->buf); 
249                     }
250                     if (ent && ent->value == VAL_ISO_ILL_1)
251                     {
252                         yaz_log (LOG_LOG, "Decode ItemRequest begin");
253                         if (r->which == ODR_EXTERNAL_single)
254                         {
255                             odr_setbuf(rr->decode,
256                                        (char *) r->u.single_ASN1_type->buf,
257                                        r->u.single_ASN1_type->len, 0);
258                             
259                             if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
260                             {
261                                 yaz_log (LOG_LOG,
262                                     "Couldn't decode ItemRequest %s near %d",
263                                        odr_errmsg(odr_geterror(rr->decode)),
264                                        odr_offset(rr->decode));
265 #if 0
266                                 yaz_log(LOG_LOG, "PDU dump:");
267                                 odr_dumpBER(yaz_log_file(),
268                                      r->u.single_ASN1_type->buf,
269                                      r->u.single_ASN1_type->len);
270 #endif
271                             }
272                             else
273                                 yaz_log(LOG_LOG, "Decode ItemRequest OK");
274                             if (rr->print)
275                             {
276                                 ill_ItemRequest (rr->print, &item_req, 0,
277                                     "ItemRequest");
278                                 odr_reset (rr->print);
279                             }
280                         }
281                         if (!item_req && r->which == ODR_EXTERNAL_single)
282                         {
283                             yaz_log (LOG_LOG, "Decode ILL APDU begin");
284                             odr_setbuf(rr->decode,
285                                        (char*) r->u.single_ASN1_type->buf,
286                                        r->u.single_ASN1_type->len, 0);
287                             
288                             if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
289                             {
290                                 yaz_log (LOG_LOG,
291                                     "Couldn't decode ILL APDU %s near %d",
292                                        odr_errmsg(odr_geterror(rr->decode)),
293                                        odr_offset(rr->decode));
294                                 yaz_log(LOG_LOG, "PDU dump:");
295                                 odr_dumpBER(yaz_log_file(),
296                                      (char *) r->u.single_ASN1_type->buf,
297                                      r->u.single_ASN1_type->len);
298                             }
299                             else
300                                 yaz_log(LOG_LOG, "Decode ILL APDU OK");
301                             if (rr->print)
302                             {
303                                 ill_APDU (rr->print, &ill_apdu, 0,
304                                     "ILL APDU");
305                                 odr_reset (rr->print);
306                             }
307                         }
308                     }
309                 }
310                 if (item_req)
311                 {
312                     yaz_log (LOG_LOG, "ILL protocol version = %d",
313                              *item_req->protocol_version_num);
314                 }
315             }
316 #endif
317         }
318         break;
319         }
320     }
321     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
322     {
323         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
324         yaz_log (LOG_LOG, "Received DB Update");
325         if (up->which == Z_IUUpdate_esRequest)
326         {
327             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
328             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
329             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
330             
331             yaz_log (LOG_LOG, "action");
332             if (toKeep->action)
333             {
334                 switch (*toKeep->action)
335                 {
336                 case Z_IUOriginPartToKeep_recordInsert:
337                     yaz_log (LOG_LOG, " recordInsert");
338                     break;
339                 case Z_IUOriginPartToKeep_recordReplace:
340                     yaz_log (LOG_LOG, " recordReplace");
341                     break;
342                 case Z_IUOriginPartToKeep_recordDelete:
343                     yaz_log (LOG_LOG, " recordDelete");
344                     break;
345                 case Z_IUOriginPartToKeep_elementUpdate:
346                     yaz_log (LOG_LOG, " elementUpdate");
347                     break;
348                 case Z_IUOriginPartToKeep_specialUpdate:
349                     yaz_log (LOG_LOG, " specialUpdate");
350                     break;
351                 default:
352                     yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
353                 }
354             }
355             if (toKeep->databaseName)
356             {
357                 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
358                 if (!strcmp(toKeep->databaseName, "fault"))
359                 {
360                     rr->errcode = 109;
361                     rr->errstring = toKeep->databaseName;
362                 }
363                 if (!strcmp(toKeep->databaseName, "accept"))
364                     rr->errcode = -1;
365             }
366             if (toKeep)
367             {
368                 Z_External *ext = odr_malloc (rr->stream, sizeof(*ext));
369                 Z_IUOriginPartToKeep *keep =
370                     odr_malloc (rr->stream, sizeof(*keep));
371                 Z_IUTargetPart *targetPart =
372                     odr_malloc (rr->stream, sizeof(*targetPart));
373                 rr->taskPackage = odr_malloc (rr->stream, sizeof(*rr->taskPackage));
374                 rr->taskPackage->packageType =
375                     odr_oiddup (rr->stream, rr->esr->packageType);
376                 rr->taskPackage->packageName = 0;
377                 rr->taskPackage->userId = 0;
378                 rr->taskPackage->retentionTime = 0;
379                 rr->taskPackage->permissions = 0;
380                 rr->taskPackage->description = 0;
381                 rr->taskPackage->targetReference = (Odr_oct *)
382                     odr_malloc (rr->stream, sizeof(Odr_oct));
383                 rr->taskPackage->targetReference->buf =
384                     odr_strdup (rr->stream, "123");
385                 rr->taskPackage->targetReference->len =
386                     rr->taskPackage->targetReference->size =
387                     strlen(rr->taskPackage->targetReference->buf);
388                 rr->taskPackage->creationDateTime = 0;
389                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
390                 rr->taskPackage->packageDiagnostics = 0;
391                 rr->taskPackage->taskSpecificParameters = ext;
392
393                 ext->direct_reference =
394                     odr_oiddup (rr->stream, rr->esr->packageType);
395                 ext->indirect_reference = 0;
396                 ext->descriptor = 0;
397                 ext->which = Z_External_update;
398                 ext->u.update = (Z_IUUpdate *)
399                     odr_malloc (rr->stream, sizeof(*ext->u.update));
400                 ext->u.update->which = Z_IUUpdate_taskPackage;
401                 ext->u.update->u.taskPackage =  (Z_IUUpdateTaskPackage *)
402                     odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
403                 ext->u.update->u.taskPackage->originPart = keep;
404                 ext->u.update->u.taskPackage->targetPart = targetPart;
405
406                 keep->action = odr_malloc (rr->stream, sizeof(int));
407                 *keep->action = *toKeep->action;
408                 keep->databaseName =
409                     odr_strdup (rr->stream, toKeep->databaseName);
410                 keep->schema = 0;
411                 keep->elementSetName = 0;
412                 keep->actionQualifier = 0;
413
414                 targetPart->updateStatus = odr_intdup (rr->stream, 1);
415                 targetPart->num_globalDiagnostics = 0;
416                 targetPart->globalDiagnostics = odr_nullval();
417                 targetPart->num_taskPackageRecords = 0;
418                 targetPart->taskPackageRecords = odr_nullval();
419             }
420             if (notToKeep)
421             {
422                 int i;
423                 for (i = 0; i < notToKeep->num; i++)
424                 {
425                     Z_External *rec = notToKeep->elements[i]->record;
426
427                     if (rec->direct_reference)
428                     {
429                         struct oident *oident;
430                         oident = oid_getentbyoid(rec->direct_reference);
431                         if (oident)
432                             yaz_log (LOG_LOG, "record %d type %s", i,
433                                      oident->desc);
434                     }
435                     switch (rec->which)
436                     {
437                     case Z_External_sutrs:
438                         if (rec->u.octet_aligned->len > 170)
439                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
440                                      rec->u.sutrs->len,
441                                      rec->u.sutrs->buf);
442                         else
443                             yaz_log (LOG_LOG, "%d bytes:\n%s",
444                                      rec->u.sutrs->len,
445                                      rec->u.sutrs->buf);
446                         break;
447                     case Z_External_octet        :
448                         if (rec->u.octet_aligned->len > 170)
449                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
450                                      rec->u.octet_aligned->len,
451                                      rec->u.octet_aligned->buf);
452                         else
453                             yaz_log (LOG_LOG, "%d bytes\n%s",
454                                      rec->u.octet_aligned->len,
455                                      rec->u.octet_aligned->buf);
456                     }
457                 }
458             }
459         }
460     }
461     else
462     {
463         yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
464                  rr->esr->taskSpecificParameters->which);
465         
466     }
467     return 0;
468 }
469
470 int ztest_delete (void *handle, bend_delete_rr *rr)
471 {
472     if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
473         rr->delete_status = Z_DeleteStatus_success;
474     else
475         rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
476     return 0;
477 }
478
479 /* Our sort handler really doesn't sort... */
480 int ztest_sort (void *handle, bend_sort_rr *rr)
481 {
482     rr->errcode = 0;
483     rr->sort_status = Z_SortStatus_success;
484     return 0;
485 }
486
487 static int atoin (const char *buf, int n)
488 {
489     int val = 0;
490     while (--n >= 0)
491     {
492         if (isdigit(*buf))
493             val = val*10 + (*buf - '0');
494         buf++;
495     }
496     return val;
497 }
498
499 char *marc_read(FILE *inf, ODR odr)
500 {
501     char length_str[5];
502     size_t size;
503     char *buf;
504
505     if (fread (length_str, 1, 5, inf) != 5)
506         return NULL;
507     size = atoin (length_str, 5);
508     if (size <= 6)
509         return NULL;
510     if (!(buf = (char*) odr_malloc (odr, size+1)))
511         return NULL;
512     if (fread (buf+5, 1, size-5, inf) != (size-5))
513     {
514         xfree (buf);
515         return NULL;
516     }
517     memcpy (buf, length_str, 5);
518     buf[size] = '\0';
519     return buf;
520 }
521
522 static char *dummy_database_record (int num, ODR odr)
523 {
524     FILE *inf = fopen ("dummy-records", "r");
525     char *buf = 0;
526
527     if (!inf)
528         return NULL;
529     while (--num >= 0)
530     {
531         if (num == 98)
532         {
533             buf = (char*) odr_malloc(odr, 2101);
534             memset(buf, 'A', 2100);
535             buf[2100] = '\0';
536             break;
537         }
538         else
539             buf = marc_read (inf, odr);
540         if (!num || !buf)
541             break;
542     }
543     fclose(inf);
544     if (num < 0)
545         return 0;
546     return buf;
547 }
548
549 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
550 {
551     FILE *f = fopen("dummy-grs", "r");
552     char line[512];
553     Z_GenericRecord *r = 0;
554     int n;
555
556     if (!f)
557         return 0;
558     while (fgets(line, 512, f))
559         if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
560         {
561             r = read_grs1(f, o);
562             break;
563         }
564     fclose(f);
565     return r;
566 }
567
568 int ztest_fetch(void *handle, bend_fetch_rr *r)
569 {
570     char *cp;
571     r->errstring = 0;
572     r->last_in_set = 0;
573     r->basename = "DUMMY";
574     r->output_format = r->request_format;  
575     if (r->request_format == VAL_SUTRS)
576     {
577 #if 0
578 /* this section returns a huge record (for testing non-blocking write, etc) */
579         r->len = 980000;
580         r->record = odr_malloc (r->stream, r->len);
581         memset (r->record, 'x', r->len);
582 #else
583 /* this section returns a small record */
584         char buf[100];
585
586         sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
587
588         r->len = strlen(buf);
589         r->record = (char *) odr_malloc (r->stream, r->len+1);
590         strcpy(r->record, buf);
591 #endif
592     }
593     else if (r->request_format == VAL_GRS1)
594     {
595         r->len = -1;
596         r->record = (char*) dummy_grs_record(r->number, r->stream);
597         if (!r->record)
598         {
599             r->errcode = 13;
600             return 0;
601         }
602     }
603     else if ((cp = dummy_database_record(r->number, r->stream)))
604     {
605         r->len = strlen(cp);
606         r->record = cp;
607         r->output_format = VAL_USMARC;
608     }
609     else
610     {
611         r->errcode = 13;
612         return 0;
613     }
614     r->errcode = 0;
615     return 0;
616 }
617
618 /*
619  * silly dummy-scan what reads words from a file.
620  */
621 int ztest_scan(void *handle, bend_scan_rr *q)
622 {
623     static FILE *f = 0;
624     static struct scan_entry list[200];
625     static char entries[200][80];
626     int hits[200];
627     char term[80], *p;
628     int i, pos;
629     int term_position_req = q->term_position;
630     int num_entries_req = q->num_entries;
631
632     q->errcode = 0;
633     q->errstring = 0;
634     q->entries = list;
635     q->status = BEND_SCAN_SUCCESS;
636     if (!f && !(f = fopen("dummy-words", "r")))
637     {
638         perror("dummy-words");
639         exit(1);
640     }
641     if (q->term->term->which != Z_Term_general)
642     {
643         q->errcode = 229; /* unsupported term type */
644         return 0;
645     }
646     if (*q->step_size != 0)
647     {
648         q->errcode = 205; /*Only zero step size supported for Scan */
649         return 0;
650     }
651     if (q->term->term->u.general->len >= 80)
652     {
653         q->errcode = 11; /* term too long */
654         return 0;
655     }
656     if (q->num_entries > 200)
657     {
658         q->errcode = 31;
659         return 0;
660     }
661     memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
662     term[q->term->term->u.general->len] = '\0';
663     for (p = term; *p; p++)
664         if (islower(*p))
665             *p = toupper(*p);
666
667     fseek(f, 0, SEEK_SET);
668     q->num_entries = 0;
669
670     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
671         i++, pos < 199 ? pos++ : (pos = 0))
672     {
673         if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
674         {
675             if ((q->term_position = term_position_req) > i + 1)
676             {
677                 q->term_position = i + 1;
678                 q->status = BEND_SCAN_PARTIAL;
679             }
680             for (; q->num_entries < q->term_position; q->num_entries++)
681             {
682                 int po;
683
684                 po = pos - q->term_position + q->num_entries+1; /* find pos */
685                 if (po < 0)
686                     po += 200;
687
688                 if (!strcmp (term, "SD") && q->num_entries == 2)
689                 {
690                     list[q->num_entries].term = entries[pos];
691                     list[q->num_entries].occurrences = -1;
692                     list[q->num_entries].errcode = 233;
693                     list[q->num_entries].errstring = "SD for Scan Term";
694                 }
695                 else
696                 {
697                     list[q->num_entries].term = entries[po];
698                     list[q->num_entries].occurrences = hits[po];
699                 }
700             }
701         }
702         else if (q->num_entries)
703         {
704             list[q->num_entries].term = entries[pos];
705             list[q->num_entries].occurrences = hits[pos];
706             q->num_entries++;
707         }
708         if (q->num_entries >= num_entries_req)
709             break;
710     }
711     if (feof(f))
712         q->status = BEND_SCAN_PARTIAL;
713     return 0;
714 }
715
716 bend_initresult *bend_init(bend_initrequest *q)
717 {
718     bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
719     int *counter = (int *) xmalloc (sizeof(int));
720
721     *counter = 0;
722     r->errcode = 0;
723     r->errstring = 0;
724     r->handle = counter;         /* user handle, in this case a simple int */
725     q->bend_sort = ztest_sort;              /* register sort handler */
726     q->bend_search = ztest_search;          /* register search handler */
727     q->bend_present = ztest_present;        /* register present handle */
728     q->bend_esrequest = ztest_esrequest;
729     q->bend_delete = ztest_delete;
730     q->bend_fetch = ztest_fetch;
731     q->bend_scan = ztest_scan;
732     return r;
733 }
734
735 void bend_close(void *handle)
736 {
737     xfree (handle);              /* release our user-defined handle */
738     return;
739 }
740
741 int main(int argc, char **argv)
742 {
743     return statserv_main(argc, argv, bend_init, bend_close);
744 }