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