New windows NT/95 port using MSV5.0. Test server ztest now in
[yaz-moved-to-github.git] / ztest / ztest.c
1
2 /* little dummy-server */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <ctype.h>
7 #include <assert.h>
8
9 #include <backend.h>
10 #include <xmalloc.h>
11 #include <proto.h>
12
13 /* Specifically for NT Services - Shouldn't cause problems on UNIX */
14 #include "service.h"
15
16 Z_GenericRecord *read_grs1(FILE *f, ODR o);
17
18 bend_initresult *bend_init(bend_initrequest *q)
19 {
20     static bend_initresult r;
21     static char *dummy = "Hej fister";
22
23     r.errcode = 0;
24     r.errstring = 0;
25     r.handle = dummy;
26     return &r;
27 }
28
29 bend_searchresult *bend_search(void *handle, bend_searchrequest *q, int *fd)
30 {
31     static bend_searchresult r;
32
33     r.errcode = 0;
34     r.errstring = 0;
35     r.hits = rand() % 22;
36     return &r;
37 }
38
39 static int atoin (const char *buf, int n)
40 {
41     int val = 0;
42     while (--n >= 0)
43     {
44         if (isdigit(*buf))
45             val = val*10 + (*buf - '0');
46         buf++;
47     }
48     return val;
49 }
50
51 char *marc_read(FILE *inf)
52 {
53     char length_str[5];
54     size_t size;
55     char *buf;
56
57     if (fread (length_str, 1, 5, inf) != 5)
58         return NULL;
59     size = atoin (length_str, 5);
60     if (size <= 6)
61         return NULL;
62     if (!(buf = xmalloc (size+1)))
63         return NULL;
64     if (fread (buf+5, 1, size-5, inf) != (size-5))
65     {
66         xfree (buf);
67         return NULL;
68     }
69     memcpy (buf, length_str, 5);
70     buf[size] = '\0';
71     return buf;
72 }
73
74 static char *dummy_database_record (int num)
75 {
76     FILE *inf = fopen ("dummy-records", "r");
77     char *buf = 0;
78
79     if (!inf)
80         return NULL;
81     while (--num >= 0)
82     {
83         if (buf)
84            xfree(buf);
85         if (num == 98)
86         {
87             assert(buf = xmalloc(2101));
88             memset(buf, 'A', 2100);
89             buf[2100] = '\0';
90             break;
91         }
92         else
93             buf = marc_read (inf);
94         if (!num || !buf)
95             break;
96     }
97     fclose(inf);
98     if (num < 0)
99         return 0;
100     return buf;
101 }
102
103 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
104 {
105     FILE *f = fopen("dummy-grs", "r");
106     char line[512];
107     Z_GenericRecord *r = 0;
108     int n;
109
110     if (!f)
111         return 0;
112     while (fgets(line, 512, f))
113         if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
114         {
115             r = read_grs1(f, o);
116             break;
117         }
118     fclose(f);
119     return r;
120 }
121
122 bend_fetchresult *bend_fetch(void *handle, bend_fetchrequest *q, int *num)
123 {
124     static bend_fetchresult r;
125     static char *bbb = 0;
126
127     r.errstring = 0;
128     r.last_in_set = 0;
129     r.basename = "DUMMY";
130     if (bbb)
131     {
132     xfree(bbb);
133         bbb = 0;
134     }
135     
136     if (q->format == VAL_SUTRS)
137     {
138         char buf[100];
139
140         sprintf(buf, "This is dummy SUTRS record number %d\n", q->number);
141         assert(r.record = bbb = xmalloc(strlen(buf)+1));
142         strcpy(bbb, buf);
143         r.len = strlen(buf);
144     }
145 #if 0
146     else if (q->format == VAL_GRS1)
147     {
148         Z_GenericRecord *rec = odr_malloc(q->stream, sizeof(*rec));
149         Z_TaggedElement *t1 = odr_malloc(q->stream, sizeof(*t1));
150         Z_StringOrNumeric *s1 = odr_malloc(q->stream, sizeof(*s1));
151         Z_ElementData *c1 = odr_malloc(q->stream, sizeof(*c1));
152
153         rec->elements = odr_malloc(q->stream, sizeof(Z_TaggedElement*)*10);
154         rec->num_elements = 1;
155         rec->elements[0] = t1 ;
156         t1->tagType = odr_malloc(q->stream, sizeof(int));
157         *t1->tagType = 3;
158         t1->tagValue = s1;
159         s1->which = Z_StringOrNumeric_string;
160         s1->u.string = "title";
161         t1->tagOccurrence = 0;
162         t1->content = c1;
163         c1->which = Z_ElementData_string;
164         c1->u.string = "The Bad Seed and The Ugly Duckling";
165         t1->metaData = 0;
166         t1->appliedVariant = 0;
167         r.record = (char*) rec;
168         r.len = -1;
169     }
170 #endif
171     else if (q->format == VAL_GRS1)
172     {
173         r.len = -1;
174         r.record = (char*) dummy_grs_record(q->number, q->stream);
175         if (!r.record)
176         {
177             r.errcode = 13;
178             return &r;
179         }
180     }
181     else if (!(r.record = bbb = dummy_database_record(q->number)))
182     {
183         r.errcode = 13;
184         return &r;
185     }
186     else
187         r.len = strlen(r.record);
188     r.format = q->format;
189     r.errcode = 0;
190     return &r;
191 }
192
193 bend_deleteresult *bend_delete(void *handle, bend_deleterequest *q, int *num)
194 {
195     return 0;
196 }
197
198 #if 0
199 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
200 {
201     static struct scan_entry list[200];
202     static char buf[200][200];
203     static bend_scanresult r;
204     int i;
205
206     r.term_position = q->term_position;
207     r.num_entries = q->num_entries;
208     r.entries = list;
209     for (i = 0; i < r.num_entries; i++)
210     {
211         list[i].term = buf[i];
212         sprintf(list[i].term, "term-%d", i+1);
213         list[i].occurrences = rand() % 100000;
214     }
215     r.errcode = 0;
216     r.errstring = 0;
217     return &r;
218 }
219 #else
220 /*
221  * silly dummy-scan what reads words from a file.
222  */
223 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
224 {
225     static bend_scanresult r;
226     static FILE *f = 0;
227     static struct scan_entry list[200];
228     static char entries[200][80];
229     int hits[200];
230     char term[80], *p;
231     int i, pos;
232
233     r.errstring = 0;
234     r.entries = list;
235     r.status = BEND_SCAN_SUCCESS;
236     if (!f && !(f = fopen("dummy-words", "r")))
237     {
238         perror("dummy-words");
239         exit(1);
240     }
241     if (q->term->term->which != Z_Term_general)
242     {
243         r.errcode = 229; /* unsupported term type */
244         return &r;
245     }
246     if (q->term->term->u.general->len >= 80)
247     {
248         r.errcode = 11; /* term too long */
249         return &r;
250     }
251     if (q->num_entries > 200)
252     {
253         r.errcode = 31;
254         return &r;
255     }
256     memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
257     term[q->term->term->u.general->len] = '\0';
258     for (p = term; *p; p++)
259         if (islower(*p))
260             *p = toupper(*p);
261
262     fseek(f, 0, 0);
263     r.num_entries = 0;
264     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
265         i++, pos < 199 ? pos++ : (pos = 0))
266     {
267         if (!r.num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
268         {
269             if ((r.term_position = q->term_position) > i + 1)
270             {
271                 r.term_position = i + 1;
272                 r.status = BEND_SCAN_PARTIAL;
273             }
274             for (; r.num_entries < r.term_position; r.num_entries++)
275             {
276                 int po;
277
278                 po = pos - r.term_position + r.num_entries + 1; /* find pos */
279                 if (po < 0)
280                     po += 200;
281                 list[r.num_entries].term = entries[po];
282                 list[r.num_entries].occurrences = hits[po];
283             }
284         }
285         else if (r.num_entries)
286         {
287             list[r.num_entries].term = entries[pos];
288             list[r.num_entries].occurrences = hits[pos];
289             r.num_entries++;
290         }
291         if (r.num_entries >= q->num_entries)
292             break;
293     }
294     if (feof(f))
295         r.status = BEND_SCAN_PARTIAL;
296     return &r;
297 }
298
299 #endif
300
301 void bend_close(void *handle)
302 {
303     return;
304 }
305
306 typedef struct _Args
307 {
308     char **argv;
309     int argc;
310 } Args; 
311
312 static Args ArgDetails;
313
314 /* name of the executable */
315 #define SZAPPNAME            "server"
316
317 /* internal name of the service */
318 #define SZSERVICENAME        "Z3950 Test Server"
319
320 /* displayed name of the service */
321 #define SZSERVICEDISPLAYNAME "Z3950 Test Server"
322
323 /* list of service dependencies - "dep1\0dep2\0\0" */
324 #define SZDEPENDENCIES       ""
325
326 int main(int argc, char **argv)
327 {
328     /* Lets setup the Arg structure */
329     ArgDetails.argc = argc;
330     ArgDetails.argv = argv;
331
332 #ifdef WIN32
333
334     /* Now setup the service with the service controller */
335     SetupService(argc, argv, &ArgDetails, SZAPPNAME, SZSERVICENAME, SZSERVICEDISPLAYNAME, SZDEPENDENCIES);
336
337 #else /* WIN32 */
338
339     /* The service controller does the following for us under windows */
340     if (StartAppService(NULL, argc, argv))
341         RunAppService(NULL);
342
343     /* Ensure the service has been stopped */
344     StopAppService(NULL);
345
346 #endif /* WIN32 */
347
348     return(0);
349 }
350
351 int StartAppService(void *pHandle, int argc, char **argv)
352 {
353     /* Initializes the App */
354     return 1;
355 }
356
357 void RunAppService(void *pHandle)
358 {
359     Args *pArgs = (Args *)pHandle;
360
361     /* Starts the app running */
362     statserv_main(pArgs->argc, pArgs->argv);
363 }
364
365 void StopAppService(void *pHandle)
366 {
367     /* Stops the app */
368     statserv_closedown();
369 }