Extended command 'record' so that XML representation of original record
[pazpar2-moved-to-github.git] / src / client.c
1 /* $Id: client.c,v 1.9 2007-06-15 06:45:39 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /** \file client.c
23     \brief Z39.50 client 
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <netdb.h>
33 #include <signal.h>
34 #include <ctype.h>
35 #include <assert.h>
36
37 #include <yaz/marcdisp.h>
38 #include <yaz/comstack.h>
39 #include <yaz/tcpip.h>
40 #include <yaz/proto.h>
41 #include <yaz/readconf.h>
42 #include <yaz/pquery.h>
43 #include <yaz/otherinfo.h>
44 #include <yaz/yaz-util.h>
45 #include <yaz/nmem.h>
46 #include <yaz/query-charset.h>
47 #include <yaz/querytowrbuf.h>
48 #include <yaz/oid_db.h>
49
50 #if HAVE_CONFIG_H
51 #include "cconfig.h"
52 #endif
53
54 #define USE_TIMING 0
55 #if USE_TIMING
56 #include <yaz/timing.h>
57 #endif
58
59 #include <netinet/in.h>
60
61 #include "pazpar2.h"
62
63 #include "client.h"
64 #include "connection.h"
65 #include "settings.h"
66
67 /** \brief Represents client state for a connection to one search target */
68 struct client {
69     struct session_database *database;
70     struct connection *connection;
71     struct session *session;
72     char *pquery; // Current search
73     int hits;
74     int records;
75     int setno;
76     int requestid;            // ID of current outstanding request
77     int diagnostic;
78     enum client_state state;
79     struct show_raw *show_raw;
80     struct client *next;     // next client in session or next in free list
81 };
82
83 struct show_raw {
84     int active; // whether this request has been sent to the server
85     int position;
86     char *syntax;
87     char *esn;
88     void (*error_handler)(void *data, const char *addinfo);
89     void (*record_handler)(void *data, const char *buf, size_t sz);
90     void *data;
91 };
92
93 static const char *client_states[] = {
94     "Client_Connecting",
95     "Client_Connected",
96     "Client_Idle",
97     "Client_Initializing",
98     "Client_Searching",
99     "Client_Presenting",
100     "Client_Error",
101     "Client_Failed",
102     "Client_Disconnected",
103     "Client_Stopped"
104 };
105
106 static struct client *client_freelist = 0;
107
108 static int send_apdu(struct client *c, Z_APDU *a)
109 {
110     return connection_send_apdu(client_get_connection(c), a);
111 }
112
113
114 const char *client_get_state_str(struct client *cl)
115 {
116     return client_states[cl->state];
117 }
118
119 enum client_state client_get_state(struct client *cl)
120 {
121     return cl->state;
122 }
123
124 void client_set_state(struct client *cl, enum client_state st)
125 {
126     cl->state = st;
127 }
128
129 static void client_show_raw_error(struct client *cl, const char *addinfo);
130
131 // Close connection and set state to error
132 void client_fatal(struct client *cl)
133 {
134     client_show_raw_error(cl, "client connection failure");
135     yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
136     connection_destroy(cl->connection);
137     cl->state = Client_Error;
138 }
139
140 struct connection *client_get_connection(struct client *cl)
141 {
142     return cl->connection;
143 }
144
145 struct session_database *client_get_database(struct client *cl)
146 {
147     return cl->database;
148 }
149
150 struct session *client_get_session(struct client *cl)
151 {
152     return cl->session;
153 }
154
155 const char *client_get_pquery(struct client *cl)
156 {
157     return cl->pquery;
158 }
159
160 void client_set_requestid(struct client *cl, int id)
161 {
162     cl->requestid = id;
163 }
164
165 int client_show_raw(struct client *cl, int position,
166                     const char *syntax, const char *esn,
167                     void *data,
168                     void (*error_handler)(void *data, const char *addinfo),
169                     void (*record_handler)(void *data, const char *buf,
170                                            size_t sz))
171 {
172     if (cl->show_raw)
173         return -1;
174     cl->show_raw = xmalloc(sizeof(*cl->show_raw));
175     cl->show_raw->position = position;
176     cl->show_raw->active = 0;
177     cl->show_raw->data = data;
178     cl->show_raw->error_handler = error_handler;
179     cl->show_raw->record_handler = record_handler;
180     if (syntax)
181         cl->show_raw->syntax = xstrdup(syntax);
182     else
183         cl->show_raw->syntax = 0;
184     if (esn)
185         cl->show_raw->esn = xstrdup(esn);
186     else
187         cl->show_raw->esn = 0;
188     client_continue(cl);
189     return 0;
190 }
191
192 static void client_show_raw_error(struct client *cl, const char *addinfo)
193 {
194     if (cl->show_raw)
195     {
196         cl->show_raw->error_handler(cl->show_raw->data, addinfo);
197         xfree(cl->show_raw);
198         cl->show_raw = 0;
199     }
200 }
201
202 static void client_show_raw_cancel(struct client *cl)
203 {
204     if (cl->show_raw)
205     {
206         cl->show_raw->error_handler(cl->show_raw->data, "cancel");
207         xfree(cl->show_raw);
208         cl->show_raw = 0;
209     }
210 }
211
212 void client_send_raw_present(struct client *cl)
213 {
214     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
215     int toget = 1;
216     int start = cl->show_raw->position;
217
218     assert(cl->show_raw);
219
220     yaz_log(YLOG_LOG, "Trying to present %d record(s) from %d",
221             toget, start);
222
223     a->u.presentRequest->resultSetStartPoint = &start;
224     a->u.presentRequest->numberOfRecordsRequested = &toget;
225
226     if (cl->show_raw->syntax)  // syntax is optional
227         a->u.presentRequest->preferredRecordSyntax =
228             yaz_string_to_oid_odr(yaz_oid_std(),
229                                   CLASS_RECSYN, cl->show_raw->syntax,
230                                   global_parameters.odr_out);
231     if (cl->show_raw->esn)  // element set is optional
232     {
233         Z_ElementSetNames *elementSetNames =
234             odr_malloc(global_parameters.odr_out, sizeof(*elementSetNames));
235         Z_RecordComposition *compo = 
236             odr_malloc(global_parameters.odr_out, sizeof(*compo));
237         a->u.presentRequest->recordComposition = compo;
238
239         compo->which = Z_RecordComp_simple;
240         compo->u.simple = elementSetNames;
241
242         elementSetNames->which = Z_ElementSetNames_generic;
243         elementSetNames->u.generic = 
244             odr_strdup(global_parameters.odr_out, cl->show_raw->esn);
245     }
246     if (send_apdu(cl, a) >= 0)
247     {
248         cl->show_raw->active = 1;
249         cl->state = Client_Presenting;
250     }
251     else
252     {
253         client_show_raw_error(cl, "send_apdu failed");
254         cl->state = Client_Error;
255     }
256     odr_reset(global_parameters.odr_out);
257 }
258
259 void client_send_present(struct client *cl)
260 {
261     struct session_database *sdb = client_get_database(cl);
262     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
263     int toget;
264     int start = cl->records + 1;
265     char *recsyn;
266
267     toget = global_parameters.chunk;
268     if (toget > global_parameters.toget - cl->records)
269         toget = global_parameters.toget - cl->records;
270     if (toget > cl->hits - cl->records)
271         toget = cl->hits - cl->records;
272
273     yaz_log(YLOG_DEBUG, "Trying to present %d record(s) from %d",
274             toget, start);
275
276     a->u.presentRequest->resultSetStartPoint = &start;
277     a->u.presentRequest->numberOfRecordsRequested = &toget;
278
279     if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
280     {
281         a->u.presentRequest->preferredRecordSyntax =
282             yaz_string_to_oid_odr(yaz_oid_std(),
283                                   CLASS_RECSYN, recsyn,
284                                   global_parameters.odr_out);
285     }
286
287     if (send_apdu(cl, a) >= 0)
288         cl->state = Client_Presenting;
289     else
290         cl->state = Client_Error;
291     odr_reset(global_parameters.odr_out);
292 }
293
294
295 void client_send_search(struct client *cl)
296 {
297     struct session *se = client_get_session(cl);
298     struct session_database *sdb = client_get_database(cl);
299     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
300     int ndb;
301     char **databaselist;
302     Z_Query *zquery;
303     int ssub = 0, lslb = 100000, mspn = 10;
304     char *recsyn = 0;
305     char *piggyback = 0;
306     char *queryenc = 0;
307     yaz_iconv_t iconv = 0;
308
309     yaz_log(YLOG_DEBUG, "Sending search to %s", sdb->database->url);
310
311     
312     // constructing RPN query
313     a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
314                                                     sizeof(Z_Query));
315     zquery->which = Z_Query_type_1;
316     zquery->u.type_1 = p_query_rpn(global_parameters.odr_out, 
317                                    client_get_pquery(cl));
318
319     // converting to target encoding
320     if ((queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING))){
321         iconv = yaz_iconv_open(queryenc, "UTF-8");
322         if (iconv){
323             yaz_query_charset_convert_rpnquery(zquery->u.type_1, 
324                                                global_parameters.odr_out, 
325                                                iconv);
326             yaz_iconv_close(iconv);
327         } else
328             yaz_log(YLOG_WARN, "Query encoding failed %s %s", 
329                     client_get_database(cl)->database->url, queryenc);
330     }
331
332     for (ndb = 0; sdb->database->databases[ndb]; ndb++)
333         ;
334     databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
335     for (ndb = 0; sdb->database->databases[ndb]; ndb++)
336         databaselist[ndb] = sdb->database->databases[ndb];
337
338     if (!(piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK)) 
339         || *piggyback == '1')
340     {
341         if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
342         {
343             a->u.searchRequest->preferredRecordSyntax =
344                 yaz_string_to_oid_odr(yaz_oid_std(),
345                                       CLASS_RECSYN, recsyn,
346                                       global_parameters.odr_out);
347         }
348         a->u.searchRequest->smallSetUpperBound = &ssub;
349         a->u.searchRequest->largeSetLowerBound = &lslb;
350         a->u.searchRequest->mediumSetPresentNumber = &mspn;
351     }
352     a->u.searchRequest->resultSetName = "Default";
353     a->u.searchRequest->databaseNames = databaselist;
354     a->u.searchRequest->num_databaseNames = ndb;
355
356     
357     {  //scope for sending and logging queries 
358         WRBUF wbquery = wrbuf_alloc();
359         yaz_query_to_wrbuf(wbquery, a->u.searchRequest->query);
360
361
362         if (send_apdu(cl, a) >= 0)
363         {
364             client_set_state(cl, Client_Searching);
365             client_set_requestid(cl, se->requestid);
366             yaz_log(YLOG_LOG, "SearchRequest %s %s %s", 
367                     client_get_database(cl)->database->url,
368                     queryenc ? queryenc : "UTF-8",
369                     wrbuf_cstr(wbquery));
370         }
371         else {
372             client_set_state(cl, Client_Error);
373             yaz_log(YLOG_WARN, "Failed SearchRequest %s  %s %s", 
374                     client_get_database(cl)->database->url, 
375                     queryenc ? queryenc : "UTF-8",
376                     wrbuf_cstr(wbquery));
377         }
378         
379         wrbuf_destroy(wbquery);
380     }    
381
382     odr_reset(global_parameters.odr_out);
383 }
384
385 void client_init_response(struct client *cl, Z_APDU *a)
386 {
387     Z_InitResponse *r = a->u.initResponse;
388
389     yaz_log(YLOG_DEBUG, "Init response %s", cl->database->database->url);
390
391     if (*r->result)
392     {
393         cl->state = Client_Idle;
394     }
395     else
396         cl->state = Client_Failed; // FIXME need to do something to the connection
397 }
398
399
400 static void ingest_raw_records(struct client *cl, Z_Records *r)
401 {
402     Z_NamePlusRecordList *rlist;
403     Z_NamePlusRecord *npr;
404     xmlDoc *doc;
405     xmlChar *buf_out;
406     int len_out;
407     if (r->which != Z_Records_DBOSD)
408     {
409         client_show_raw_error(cl, "non-surrogate diagnostics");
410         return;
411     }
412
413     rlist = r->u.databaseOrSurDiagnostics;
414     if (rlist->num_records != 1 || !rlist->records || !rlist->records[0])
415     {
416         client_show_raw_error(cl, "no records");
417         return;
418     }
419     npr = rlist->records[0];
420     if (npr->which != Z_NamePlusRecord_databaseRecord)
421     {
422         client_show_raw_error(cl, "surrogate diagnostic");
423         return;
424     }
425
426     doc = record_to_xml(client_get_database(cl), npr->u.databaseRecord);
427     if (!doc)
428     {
429         client_show_raw_error(cl, "unable to convert record to xml");
430         return;
431     }
432
433     xmlDocDumpMemory(doc, &buf_out, &len_out);
434
435     cl->show_raw->record_handler(cl->show_raw->data,
436                                  (const char *) buf_out, len_out);
437     
438     xmlFreeDoc(doc);
439     xfree(cl->show_raw);
440     cl->show_raw = 0;
441 }
442
443 static void ingest_records(struct client *cl, Z_Records *r)
444 {
445 #if USE_TIMING
446     yaz_timing_t t = yaz_timing_create();
447 #endif
448     struct record *rec;
449     struct session *s = client_get_session(cl);
450     Z_NamePlusRecordList *rlist;
451     int i;
452
453     if (r->which != Z_Records_DBOSD)
454         return;
455     rlist = r->u.databaseOrSurDiagnostics;
456     for (i = 0; i < rlist->num_records; i++)
457     {
458         Z_NamePlusRecord *npr = rlist->records[i];
459
460         cl->records++;
461         if (npr->which != Z_NamePlusRecord_databaseRecord)
462         {
463             yaz_log(YLOG_WARN, 
464                     "Unexpected record type, probably diagnostic %s",
465                     cl->database->database->url);
466             continue;
467         }
468
469         rec = ingest_record(cl, npr->u.databaseRecord, cl->records);
470         if (!rec)
471             continue;
472     }
473     if (rlist->num_records)
474         session_alert_watch(s, SESSION_WATCH_RECORDS);
475
476 #if USE_TIMING
477     yaz_timing_stop(t);
478     yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f", 
479             yaz_timing_get_real(t), yaz_timing_get_user(t),
480             yaz_timing_get_sys(t));
481     yaz_timing_destroy(&t);
482 #endif
483 }
484
485
486 void client_search_response(struct client *cl, Z_APDU *a)
487 {
488     struct session *se = cl->session;
489     Z_SearchResponse *r = a->u.searchResponse;
490
491     yaz_log(YLOG_DEBUG, "Search response %s (status=%d)", 
492             cl->database->database->url, *r->searchStatus);
493
494     if (*r->searchStatus)
495     {
496         cl->hits = *r->resultCount;
497         se->total_hits += cl->hits;
498         if (r->presentStatus && !*r->presentStatus && r->records)
499         {
500             yaz_log(YLOG_DEBUG, "Records in search response %s", 
501                     cl->database->database->url);
502             ingest_records(cl, r->records);
503         }
504         cl->state = Client_Idle;
505     }
506     else
507     {          /*"FAILED"*/
508         cl->hits = 0;
509         cl->state = Client_Error;
510         if (r->records) {
511             Z_Records *recs = r->records;
512             if (recs->which == Z_Records_NSD)
513             {
514                 yaz_log(YLOG_WARN,  
515                     "Search response: Non-surrogate diagnostic %s (%d)", 
516                     cl->database->database->url, 
517                     *recs->u.nonSurrogateDiagnostic->condition); 
518                 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
519                 cl->state = Client_Error;
520             }
521         }
522     }
523 }
524
525 void client_present_response(struct client *cl, Z_APDU *a)
526 {
527     Z_PresentResponse *r = a->u.presentResponse;
528
529     if (r->records) {
530         Z_Records *recs = r->records;
531         if (recs->which == Z_Records_NSD)
532         {
533             yaz_log(YLOG_WARN, "Non-surrogate diagnostic %s",
534                     cl->database->database->url);
535             cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
536             cl->state = Client_Error;
537             client_show_raw_error(cl, "non surrogate diagnostics");
538         }
539     }
540
541     if (!*r->presentStatus && cl->state != Client_Error)
542     {
543         yaz_log(YLOG_DEBUG, "Good Present response %s",
544                 cl->database->database->url);
545
546         // we can mix show raw and normal show ..
547         if (cl->show_raw && cl->show_raw->active)
548         {
549             cl->show_raw->active = 0; // no longer active
550             ingest_raw_records(cl, r->records);
551         }
552         else
553             ingest_records(cl, r->records);
554         cl->state = Client_Idle;
555     }
556     else if (*r->presentStatus) 
557     {
558         yaz_log(YLOG_WARN, "Bad Present response %s",
559                 cl->database->database->url);
560         cl->state = Client_Error;
561         client_show_raw_error(cl, "bad present response");
562     }
563 }
564
565 void client_close_response(struct client *cl, Z_APDU *a)
566 {
567     struct connection *co = cl->connection;
568     /* Z_Close *r = a->u.close; */
569
570     yaz_log(YLOG_WARN, "Close response %s", cl->database->database->url);
571
572     cl->state = Client_Failed;
573     connection_destroy(co);
574 }
575
576 int client_is_our_response(struct client *cl)
577 {
578     struct session *se = client_get_session(cl);
579
580     if (cl && (cl->requestid == se->requestid || 
581                cl->state == Client_Initializing))
582         return 1;
583     return 0;
584 }
585
586 // Set authentication token in init if one is set for the client
587 // TODO: Extend this to handle other schemes than open (should be simple)
588 static void init_authentication(struct client *cl, Z_InitRequest *req)
589 {
590     struct session_database *sdb = client_get_database(cl);
591     char *auth = session_setting_oneval(sdb, PZ_AUTHENTICATION);
592
593     if (*auth)
594     {
595         struct connection *co = client_get_connection(cl);
596         struct session *se = client_get_session(cl);
597         Z_IdAuthentication *idAuth = odr_malloc(global_parameters.odr_out,
598                 sizeof(*idAuth));
599         idAuth->which = Z_IdAuthentication_open;
600         idAuth->u.open = auth;
601         req->idAuthentication = idAuth;
602         connection_set_authentication(co, nmem_strdup(se->session_nmem, auth));
603     }
604 }
605
606 static void init_zproxy(struct client *cl, Z_InitRequest *req)
607 {
608     struct session_database *sdb = client_get_database(cl);
609     char *ztarget = sdb->database->url;
610     //char *ztarget = sdb->url;    
611     char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
612
613     if (*zproxy)
614         yaz_oi_set_string_oid(&req->otherInfo,
615                               global_parameters.odr_out,
616                               yaz_oid_userinfo_proxy,
617                               1, ztarget);
618 }
619
620
621 static void client_init_request(struct client *cl)
622 {
623     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
624
625     a->u.initRequest->implementationId = global_parameters.implementationId;
626     a->u.initRequest->implementationName = global_parameters.implementationName;
627     a->u.initRequest->implementationVersion =
628         global_parameters.implementationVersion;
629     ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
630     ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
631     ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
632
633     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
634     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
635     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
636
637     init_authentication(cl, a->u.initRequest);
638     init_zproxy(cl, a->u.initRequest);
639
640     if (send_apdu(cl, a) >= 0)
641         client_set_state(cl, Client_Initializing);
642     else
643         client_set_state(cl, Client_Error);
644     odr_reset(global_parameters.odr_out);
645 }
646
647 void client_continue(struct client *cl)
648 {
649     if (cl->state == Client_Connected) {
650         client_init_request(cl);
651     }
652
653     if (cl->state == Client_Idle)
654     {
655         struct session *se = client_get_session(cl);
656         if (cl->requestid != se->requestid && cl->pquery) {
657             // we'll have to abort this because result set is to be deleted
658             client_show_raw_cancel(cl);   
659             client_send_search(cl);
660         }
661         else if (cl->show_raw)
662         {
663             client_send_raw_present(cl);
664         }
665         else if (cl->hits > 0 && cl->records < global_parameters.toget &&
666             cl->records < cl->hits) {
667             client_send_present(cl);
668         }
669     }
670 }
671
672 struct client *client_create(void)
673 {
674     struct client *r;
675     if (client_freelist)
676     {
677         r = client_freelist;
678         client_freelist = client_freelist->next;
679     }
680     else
681         r = xmalloc(sizeof(struct client));
682     r->pquery = 0;
683     r->database = 0;
684     r->connection = 0;
685     r->session = 0;
686     r->hits = 0;
687     r->records = 0;
688     r->setno = 0;
689     r->requestid = -1;
690     r->diagnostic = 0;
691     r->state = Client_Disconnected;
692     r->show_raw = 0;
693     r->next = 0;
694     return r;
695 }
696
697 void client_destroy(struct client *c)
698 {
699     struct session *se = c->session;
700     if (c == se->clients)
701         se->clients = c->next;
702     else
703     {
704         struct client *cc;
705         for (cc = se->clients; cc && cc->next != c; cc = cc->next)
706             ;
707         if (cc)
708             cc->next = c->next;
709     }
710     if (c->connection)
711         connection_release(c->connection);
712     c->next = client_freelist;
713     client_freelist = c;
714 }
715
716 void client_set_connection(struct client *cl, struct connection *con)
717 {
718     cl->connection = con;
719 }
720
721 void client_disconnect(struct client *cl)
722 {
723     if (cl->state != Client_Idle)
724         cl->state = Client_Disconnected;
725     client_set_connection(cl, 0);
726 }
727
728 // Extract terms from query into null-terminated termlist
729 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
730 {
731     int num = 0;
732
733     pull_terms(nmem, query, termlist, &num);
734     termlist[num] = 0;
735 }
736
737 // Initialize CCL map for a target
738 static CCL_bibset prepare_cclmap(struct client *cl)
739 {
740     struct session_database *sdb = client_get_database(cl);
741     struct setting *s;
742     CCL_bibset res;
743
744     if (!sdb->settings)
745         return 0;
746     res = ccl_qual_mk();
747     for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
748     {
749         char *p = strchr(s->name + 3, ':');
750         if (!p)
751         {
752             yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
753             ccl_qual_rm(&res);
754             return 0;
755         }
756         p++;
757         ccl_qual_fitem(res, s->value, p);
758     }
759     return res;
760 }
761
762 // Parse the query given the settings specific to this client
763 int client_parse_query(struct client *cl, const char *query)
764 {
765     struct session *se = client_get_session(cl);
766     struct ccl_rpn_node *cn;
767     int cerror, cpos;
768     CCL_bibset ccl_map = prepare_cclmap(cl);
769
770     if (!ccl_map)
771         return -1;
772     cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
773     ccl_qual_rm(&ccl_map);
774     if (!cn)
775     {
776         cl->state = Client_Error;
777         yaz_log(YLOG_WARN, "Failed to parse query for %s",
778                          client_get_database(cl)->database->url);
779         return -1;
780     }
781     wrbuf_rewind(se->wrbuf);
782     ccl_pquery(se->wrbuf, cn);
783     xfree(cl->pquery);
784     cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
785
786     if (!se->relevance)
787     {
788         // Initialize relevance structure with query terms
789         char *p[512];
790         extract_terms(se->nmem, cn, p);
791         se->relevance = relevance_create(client_get_database(cl)->pct,
792                                          se->nmem, (const char **) p,
793                                          se->expected_maxrecs);
794     }
795
796     ccl_rpn_delete(cn);
797     return 0;
798 }
799
800 void client_set_session(struct client *cl, struct session *se)
801 {
802     cl->session = se;
803     cl->next = se->clients;
804     se->clients = cl;
805 }
806
807 int client_is_active(struct client *cl)
808 {
809     if (cl->connection && (cl->state == Client_Connecting ||
810                            cl->state == Client_Initializing ||
811                            cl->state == Client_Searching ||
812                            cl->state == Client_Presenting))
813         return 1;
814     return 0;
815 }
816
817 struct client *client_next_in_session(struct client *cl)
818 {
819     if (cl)
820         return cl->next;
821     return 0;
822
823 }
824
825 int client_get_hits(struct client *cl)
826 {
827     return cl->hits;
828 }
829
830 int client_get_num_records(struct client *cl)
831 {
832     return cl->records;
833 }
834
835 int client_get_diagnostic(struct client *cl)
836 {
837     return cl->diagnostic;
838 }
839
840 void client_set_database(struct client *cl, struct session_database *db)
841 {
842     cl->database = db;
843 }
844
845 struct host *client_get_host(struct client *cl)
846 {
847     return client_get_database(cl)->database->host;
848 }
849
850 const char *client_get_url(struct client *cl)
851 {
852     return client_get_database(cl)->database->url;
853 }
854
855 /*
856  * Local variables:
857  * c-basic-offset: 4
858  * indent-tabs-mode: nil
859  * End:
860  * vim: shiftwidth=4 tabstop=8 expandtab
861  */