Remove two unused struct decls
[pazpar2-moved-to-github.git] / src / client.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file client.c
21     \brief Z39.50 client 
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <pthread.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <signal.h>
38 #include <assert.h>
39
40 #include <yaz/marcdisp.h>
41 #include <yaz/comstack.h>
42 #include <yaz/tcpip.h>
43 #include <yaz/proto.h>
44 #include <yaz/readconf.h>
45 #include <yaz/pquery.h>
46 #include <yaz/otherinfo.h>
47 #include <yaz/yaz-util.h>
48 #include <yaz/nmem.h>
49 #include <yaz/query-charset.h>
50 #include <yaz/querytowrbuf.h>
51 #include <yaz/oid_db.h>
52 #include <yaz/diagbib1.h>
53 #include <yaz/snprintf.h>
54 #include <yaz/rpn2cql.h>
55
56 #define USE_TIMING 0
57 #if USE_TIMING
58 #include <yaz/timing.h>
59 #endif
60
61 #include "ppmutex.h"
62 #include "session.h"
63 #include "parameters.h"
64 #include "client.h"
65 #include "connection.h"
66 #include "settings.h"
67 #include "relevance.h"
68 #include "incref.h"
69
70 /* client counting (1) , disable client counting (0) */
71 #if 1
72 static YAZ_MUTEX g_mutex = 0;
73 static int no_clients = 0;
74
75 static void client_use(int delta)
76 {
77     if (!g_mutex)
78         yaz_mutex_create(&g_mutex);
79     yaz_mutex_enter(g_mutex);
80     no_clients += delta;
81     yaz_mutex_leave(g_mutex);
82     yaz_log(YLOG_LOG, "%s clients=%d", delta > 0 ? "INC" : "DEC", no_clients);
83 }
84 #else
85 #define client_use(x)
86 #endif
87
88 /** \brief Represents client state for a connection to one search target */
89 struct client {
90     struct session_database *database;
91     struct connection *connection;
92     struct session *session;
93     char *pquery; // Current search
94     char *cqlquery; // used for SRU targets only
95     Odr_int hits;
96     int record_offset;
97     int maxrecs;
98     int startrecs;
99     int diagnostic;
100     enum client_state state;
101     struct show_raw *show_raw;
102     ZOOM_resultset resultset;
103     YAZ_MUTEX mutex;
104     int ref_count;
105 };
106
107 struct show_raw {
108     int active; // whether this request has been sent to the server
109     int position;
110     int binary;
111     char *syntax;
112     char *esn;
113     void (*error_handler)(void *data, const char *addinfo);
114     void (*record_handler)(void *data, const char *buf, size_t sz);
115     void *data;
116     struct show_raw *next;
117 };
118
119 static const char *client_states[] = {
120     "Client_Connecting",
121     "Client_Idle",
122     "Client_Working",
123     "Client_Error",
124     "Client_Failed",
125     "Client_Disconnected"
126 };
127
128 const char *client_get_state_str(struct client *cl)
129 {
130     return client_states[cl->state];
131 }
132
133 enum client_state client_get_state(struct client *cl)
134 {
135     return cl->state;
136 }
137
138 void client_set_state(struct client *cl, enum client_state st)
139 {
140     cl->state = st;
141     /* no need to check for all client being non-active if this one
142        already is. Note that session_active_clients also LOCKS session */
143 #if 0
144     if (!client_is_active(cl) && cl->session)
145     {
146         int no_active = session_active_clients(cl->session);
147         if (no_active == 0)
148             session_alert_watch(cl->session, SESSION_WATCH_SHOW);
149     }
150 #endif
151 }
152
153 static void client_show_raw_error(struct client *cl, const char *addinfo);
154
155 struct connection *client_get_connection(struct client *cl)
156 {
157     return cl->connection;
158 }
159
160 struct session_database *client_get_database(struct client *cl)
161 {
162     return cl->database;
163 }
164
165 struct session *client_get_session(struct client *cl)
166 {
167     return cl->session;
168 }
169
170 const char *client_get_pquery(struct client *cl)
171 {
172     return cl->pquery;
173 }
174
175 static void client_send_raw_present(struct client *cl);
176 static int nativesyntax_to_type(struct session_database *sdb, char *type,
177                                 ZOOM_record rec);
178
179 static void client_show_immediate(
180     ZOOM_resultset resultset, struct session_database *sdb, int position,
181     void *data,
182     void (*error_handler)(void *data, const char *addinfo),
183     void (*record_handler)(void *data, const char *buf, size_t sz),
184     int binary)
185 {
186     ZOOM_record rec = 0;
187     char type[80];
188     const char *buf;
189     int len;
190
191     if (!resultset)
192     {
193         error_handler(data, "no resultset");
194         return;
195     }
196     rec = ZOOM_resultset_record(resultset, position-1);
197     if (!rec)
198     {
199         error_handler(data, "no record");
200         return;
201     }
202     if (binary)
203         strcpy(type, "raw");
204     else
205         nativesyntax_to_type(sdb, type, rec);
206     buf = ZOOM_record_get(rec, type, &len);
207     if (!buf)
208     {
209         error_handler(data, "no record");
210         return;
211     }
212     record_handler(data, buf, len);
213 }
214
215
216 int client_show_raw_begin(struct client *cl, int position,
217                           const char *syntax, const char *esn,
218                           void *data,
219                           void (*error_handler)(void *data, const char *addinfo),
220                           void (*record_handler)(void *data, const char *buf,
221                                                  size_t sz),
222                           int binary)
223 {
224     if (syntax == 0 && esn == 0)
225         client_show_immediate(cl->resultset, client_get_database(cl),
226                               position, data,
227                               error_handler, record_handler,
228                               binary);
229     else
230     {
231         struct show_raw *rr, **rrp;
232
233         if (!cl->connection)
234             return -1;
235     
236
237         rr = xmalloc(sizeof(*rr));
238         rr->position = position;
239         rr->active = 0;
240         rr->data = data;
241         rr->error_handler = error_handler;
242         rr->record_handler = record_handler;
243         rr->binary = binary;
244         if (syntax)
245             rr->syntax = xstrdup(syntax);
246         else
247             rr->syntax = 0;
248         if (esn)
249             rr->esn = xstrdup(esn);
250         else
251             rr->esn = 0;
252         rr->next = 0;
253         
254         for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
255             ;
256         *rrp = rr;
257         
258         if (cl->state == Client_Failed)
259         {
260             client_show_raw_error(cl, "client failed");
261         }
262         else if (cl->state == Client_Disconnected)
263         {
264             client_show_raw_error(cl, "client disconnected");
265         }
266         else
267         {
268             client_send_raw_present(cl);
269         }
270     }
271     return 0;
272 }
273
274 static void client_show_raw_delete(struct show_raw *r)
275 {
276     xfree(r->syntax);
277     xfree(r->esn);
278     xfree(r);
279 }
280
281 void client_show_raw_remove(struct client *cl, void *data)
282 {
283     struct show_raw *rr = data;
284     struct show_raw **rrp = &cl->show_raw;
285     while (*rrp != rr)
286         rrp = &(*rrp)->next;
287     if (*rrp)
288     {
289         *rrp = rr->next;
290         client_show_raw_delete(rr);
291     }
292 }
293
294 void client_show_raw_dequeue(struct client *cl)
295 {
296     struct show_raw *rr = cl->show_raw;
297
298     cl->show_raw = rr->next;
299     client_show_raw_delete(rr);
300 }
301
302 static void client_show_raw_error(struct client *cl, const char *addinfo)
303 {
304     while (cl->show_raw)
305     {
306         cl->show_raw->error_handler(cl->show_raw->data, addinfo);
307         client_show_raw_dequeue(cl);
308     }
309 }
310
311 static void client_send_raw_present(struct client *cl)
312 {
313     struct session_database *sdb = client_get_database(cl);
314     struct connection *co = client_get_connection(cl);
315     ZOOM_resultset set = cl->resultset;
316
317     int offset = cl->show_raw->position;
318     const char *syntax = 0;
319     const char *elements = 0;
320
321     assert(cl->show_raw);
322     assert(set);
323
324     yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
325             client_get_url(cl), 1, offset);
326
327     if (cl->show_raw->syntax)
328         syntax = cl->show_raw->syntax;
329     else
330         syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
331     ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
332
333     if (cl->show_raw->esn)
334         elements = cl->show_raw->esn;
335     else
336         elements = session_setting_oneval(sdb, PZ_ELEMENTS);
337     if (elements && *elements)
338         ZOOM_resultset_option_set(set, "elementSetName", elements);
339
340     ZOOM_resultset_records(set, 0, offset-1, 1);
341     cl->show_raw->active = 1;
342
343     connection_continue(co);
344 }
345
346 static int nativesyntax_to_type(struct session_database *sdb, char *type,
347                                 ZOOM_record rec)
348 {
349     const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
350
351     if (s && *s)
352     {
353         if (!strncmp(s, "iso2709", 7))
354         {
355             const char *cp = strchr(s, ';');
356             yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
357         }
358         else if (!strncmp(s, "xml", 3))
359         {
360             strcpy(type, "xml");
361         }
362         else if (!strncmp(s, "txml", 4))
363         {
364             const char *cp = strchr(s, ';');
365             yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
366         }
367         else
368             return -1;
369         return 0;
370     }
371     else  /* attempt to deduce structure */
372     {
373         const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
374         if (syntax)
375         {
376             if (!strcmp(syntax, "XML"))
377             {
378                 strcpy(type, "xml");
379                 return 0;
380             }
381             else if (!strcmp(syntax, "TXML"))
382                 {
383                     strcpy(type, "txml");
384                     return 0;
385                 }
386             else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
387             {
388                 strcpy(type, "xml; charset=marc8-s");
389                 return 0;
390             }
391             else return -1;
392         }
393         else return -1;
394     }
395 }
396
397 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
398 {
399     const char *buf;
400     int len;
401     char type[80];
402
403     if (cl->show_raw->binary)
404         strcpy(type, "raw");
405     else
406     {
407         struct session_database *sdb = client_get_database(cl);
408         nativesyntax_to_type(sdb, type, rec);
409     }
410
411     buf = ZOOM_record_get(rec, type, &len);
412     cl->show_raw->record_handler(cl->show_raw->data,  buf, len);
413     client_show_raw_dequeue(cl);
414 }
415
416 void client_search_response(struct client *cl)
417 {
418     struct connection *co = cl->connection;
419     struct session *se = cl->session;
420     ZOOM_connection link = connection_get_link(co);
421     ZOOM_resultset resultset = cl->resultset;
422     const char *error, *addinfo = 0;
423     
424     if (ZOOM_connection_error(link, &error, &addinfo))
425     {
426         cl->hits = 0;
427         client_set_state(cl, Client_Error);
428         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
429                 error, addinfo, client_get_url(cl));
430     }
431     else
432     {
433         cl->record_offset = cl->startrecs;
434         cl->hits = ZOOM_resultset_size(resultset);
435         if (se)
436             se->total_hits += cl->hits;
437     }
438 }
439
440 void client_got_records(struct client *cl)
441 {
442     struct session *se = cl->session;
443     if (se)
444     {
445         client_unlock(cl);
446         session_alert_watch(se, SESSION_WATCH_SHOW);
447         session_alert_watch(se, SESSION_WATCH_RECORD);
448         client_lock(cl);
449     }
450 }
451
452 void client_record_response(struct client *cl)
453 {
454     struct connection *co = cl->connection;
455     ZOOM_connection link = connection_get_link(co);
456     ZOOM_resultset resultset = cl->resultset;
457     const char *error, *addinfo;
458
459     if (ZOOM_connection_error(link, &error, &addinfo))
460     {
461         client_set_state(cl, Client_Error);
462         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
463             error, addinfo, client_get_url(cl));
464     }
465     else
466     {
467         ZOOM_record rec = 0;
468         const char *msg, *addinfo;
469         
470         if (cl->show_raw && cl->show_raw->active)
471         {
472             if ((rec = ZOOM_resultset_record(resultset,
473                                              cl->show_raw->position-1)))
474             {
475                 cl->show_raw->active = 0;
476                 ingest_raw_record(cl, rec);
477             }
478             else
479             {
480                 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
481                         cl->show_raw->position-1);
482             }
483         }
484         else
485         {
486             int offset = cl->record_offset;
487             if ((rec = ZOOM_resultset_record(resultset, offset)))
488             {
489                 cl->record_offset++;
490                 if (cl->session == 0)
491                     ;
492                 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
493                 {
494                     yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
495                             msg, addinfo, client_get_url(cl),
496                             cl->record_offset);
497                 }
498                 else
499                 {
500                     struct session_database *sdb = client_get_database(cl);
501                     NMEM nmem = nmem_create();
502                     const char *xmlrec;
503                     char type[80];
504
505                     if (nativesyntax_to_type(sdb, type, rec))
506                         yaz_log(YLOG_WARN, "Failed to determine record type");
507                     xmlrec = ZOOM_record_get(rec, type, NULL);
508                     if (!xmlrec)
509                         yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
510                                 client_get_url(cl));
511                     else
512                     {
513                         if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
514                             yaz_log(YLOG_WARN, "Failed to ingest from %s",
515                                     client_get_url(cl));
516                     }
517                     nmem_destroy(nmem);
518                 }
519             }
520             else
521             {
522                 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
523                         offset);
524             }
525         }
526     }
527 }
528
529 void client_start_search(struct client *cl)
530 {
531     struct session_database *sdb = client_get_database(cl);
532     struct connection *co = client_get_connection(cl);
533     ZOOM_connection link = connection_get_link(co);
534     ZOOM_resultset rs;
535     char *databaseName = sdb->database->databases[0];
536     const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
537     const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
538     const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
539     const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
540     const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
541     const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
542     const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
543     char maxrecs_str[24], startrecs_str[24];
544
545     assert(link);
546
547     cl->hits = -1;
548     cl->record_offset = 0;
549     cl->diagnostic = 0;
550     client_set_state(cl, Client_Working);
551
552     if (*opt_piggyback)
553         ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
554     else
555         ZOOM_connection_option_set(link, "piggyback", "1");
556     if (*opt_queryenc)
557         ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
558     if (*opt_sru && *opt_elements)
559         ZOOM_connection_option_set(link, "schema", opt_elements);
560     else if (*opt_elements)
561         ZOOM_connection_option_set(link, "elementSetName", opt_elements);
562     if (*opt_requestsyn)
563         ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
564
565     if (!*opt_maxrecs)
566     {
567         sprintf(maxrecs_str, "%d", cl->maxrecs);
568         opt_maxrecs = maxrecs_str;
569     }
570     ZOOM_connection_option_set(link, "count", opt_maxrecs);
571
572
573     if (atoi(opt_maxrecs) > 20)
574         ZOOM_connection_option_set(link, "presentChunk", "20");
575     else
576         ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
577
578     sprintf(startrecs_str, "%d", cl->startrecs);
579     ZOOM_connection_option_set(link, "start", startrecs_str);
580
581     if (databaseName)
582         ZOOM_connection_option_set(link, "databaseName", databaseName);
583
584     if (cl->cqlquery)
585     {
586         ZOOM_query q = ZOOM_query_create();
587         yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
588         ZOOM_query_cql(q, cl->cqlquery);
589         if (*opt_sort)
590             ZOOM_query_sortby(q, opt_sort);
591         rs = ZOOM_connection_search(link, q);
592         ZOOM_query_destroy(q);
593     }
594     else
595     {
596         yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
597         rs = ZOOM_connection_search_pqf(link, cl->pquery);
598     }
599     ZOOM_resultset_destroy(cl->resultset);
600     cl->resultset = rs;
601     connection_continue(co);
602 }
603
604 struct client *client_create(void)
605 {
606     struct client *r = xmalloc(sizeof(*r));
607     r->maxrecs = 100;
608     r->startrecs = 0;
609     r->pquery = 0;
610     r->cqlquery = 0;
611     r->database = 0;
612     r->connection = 0;
613     r->session = 0;
614     r->hits = 0;
615     r->record_offset = 0;
616     r->diagnostic = 0;
617     r->state = Client_Disconnected;
618     r->show_raw = 0;
619     r->resultset = 0;
620     r->mutex = 0;
621     pazpar2_mutex_create(&r->mutex, "client");
622
623     r->ref_count = 1;
624     client_use(1);
625     
626     return r;
627 }
628
629 void client_lock(struct client *c)
630 {
631     yaz_mutex_enter(c->mutex);
632 }
633
634 void client_unlock(struct client *c)
635 {
636     yaz_mutex_leave(c->mutex);
637 }
638
639 void client_incref(struct client *c)
640 {
641     pazpar2_incref(&c->ref_count, c->mutex);
642     yaz_log(YLOG_LOG, "client_incref c=%p %s cnt=%d",
643             c, client_get_url(c), c->ref_count);
644 }
645
646 int client_destroy(struct client *c)
647 {
648     if (c)
649     {
650         yaz_log(YLOG_LOG, "client_destroy c=%p %s cnt=%d",
651                 c, client_get_url(c), c->ref_count);
652         if (!pazpar2_decref(&c->ref_count, c->mutex))
653         {
654             xfree(c->pquery);
655             c->pquery = 0;
656             xfree(c->cqlquery);
657             c->cqlquery = 0;
658             assert(!c->connection);
659             assert(!c->resultset);
660             
661             yaz_mutex_destroy(&c->mutex);
662             xfree(c);
663             client_use(-1);
664             return 1;
665         }
666     }
667     return 0;
668 }
669
670 void client_set_connection(struct client *cl, struct connection *con)
671 {
672     if (cl->resultset)
673     {
674         ZOOM_resultset_destroy(cl->resultset);
675         cl->resultset = 0;
676     }
677     if (con)
678     {
679         assert(cl->connection == 0);
680         cl->connection = con;
681         client_incref(cl);
682     }
683     else
684     {
685         cl->connection = con;
686         client_destroy(cl);
687     }
688 }
689
690 void client_disconnect(struct client *cl)
691 {
692     if (cl->state != Client_Idle)
693         client_set_state(cl, Client_Disconnected);
694     client_set_connection(cl, 0);
695 }
696
697 // Extract terms from query into null-terminated termlist
698 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
699 {
700     int num = 0;
701
702     pull_terms(nmem, query, termlist, &num);
703     termlist[num] = 0;
704 }
705
706 // Initialize CCL map for a target
707 static CCL_bibset prepare_cclmap(struct client *cl)
708 {
709     struct session_database *sdb = client_get_database(cl);
710     struct setting *s;
711     CCL_bibset res;
712
713     if (!sdb->settings)
714         return 0;
715     res = ccl_qual_mk();
716     for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
717     {
718         char *p = strchr(s->name + 3, ':');
719         if (!p)
720         {
721             yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
722             ccl_qual_rm(&res);
723             return 0;
724         }
725         p++;
726         ccl_qual_fitem(res, s->value, p);
727     }
728     return res;
729 }
730
731 // returns a xmalloced CQL query corresponding to the pquery in client
732 static char *make_cqlquery(struct client *cl)
733 {
734     cql_transform_t cqlt = cql_transform_create();
735     Z_RPNQuery *zquery;
736     char *r;
737     WRBUF wrb = wrbuf_alloc();
738     int status;
739     ODR odr_out = odr_createmem(ODR_ENCODE);
740
741     zquery = p_query_rpn(odr_out, cl->pquery);
742     yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
743     if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
744     {
745         yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
746         r = 0;
747     }
748     else
749     {
750         r = xstrdup(wrbuf_cstr(wrb));
751     }     
752     wrbuf_destroy(wrb);
753     odr_destroy(odr_out);
754     cql_transform_close(cqlt);
755     return r;
756 }
757
758 // Parse the query given the settings specific to this client
759 int client_parse_query(struct client *cl, const char *query)
760 {
761     struct session *se = client_get_session(cl);
762     struct session_database *sdb = client_get_database(cl);
763     struct ccl_rpn_node *cn;
764     int cerror, cpos;
765     CCL_bibset ccl_map = prepare_cclmap(cl);
766     const char *sru = session_setting_oneval(sdb, PZ_SRU);
767     const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
768     const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
769
770     if (!ccl_map)
771         return -1;
772
773     cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
774     ccl_qual_rm(&ccl_map);
775     if (!cn)
776     {
777         client_set_state(cl, Client_Error);
778         yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
779                 query,
780                 client_get_database(cl)->database->url);
781         return -1;
782     }
783     wrbuf_rewind(se->wrbuf);
784     if (*pqf_prefix)
785     {
786         wrbuf_puts(se->wrbuf, pqf_prefix);
787         wrbuf_puts(se->wrbuf, " ");
788     }
789     if (!pqf_strftime || !*pqf_strftime)
790         ccl_pquery(se->wrbuf, cn);
791     else
792     {
793         time_t cur_time = time(0);
794         struct tm *tm =  localtime(&cur_time);
795         char tmp_str[300];
796         const char *cp = tmp_str;
797
798         /* see man strftime(3) for things .. In particular %% gets converted
799          to %.. And That's our original query .. */
800         strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
801         for (; *cp; cp++)
802         {
803             if (cp[0] == '%')
804                 ccl_pquery(se->wrbuf, cn);
805             else
806                 wrbuf_putc(se->wrbuf, cp[0]);
807         }
808     }
809     xfree(cl->pquery);
810     cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
811
812     xfree(cl->cqlquery);
813     if (*sru)
814     {
815         if (!(cl->cqlquery = make_cqlquery(cl)))
816             return -1;
817     }
818     else
819         cl->cqlquery = 0;
820
821     if (!se->relevance)
822     {
823         // Initialize relevance structure with query terms
824         char *p[512];
825         extract_terms(se->nmem, cn, p);
826         se->relevance = relevance_create(
827             se->service->relevance_pct,
828             se->nmem, (const char **) p);
829     }
830
831     ccl_rpn_delete(cn);
832     return 0;
833 }
834
835 void client_set_session(struct client *cl, struct session *se)
836 {
837     cl->session = se;
838 }
839
840 int client_is_active(struct client *cl)
841 {
842     if (cl->connection && (cl->state == Client_Connecting ||
843                            cl->state == Client_Working))
844         return 1;
845     return 0;
846 }
847
848 Odr_int client_get_hits(struct client *cl)
849 {
850     return cl->hits;
851 }
852
853 int client_get_num_records(struct client *cl)
854 {
855     return cl->record_offset;
856 }
857
858 void client_set_diagnostic(struct client *cl, int diagnostic)
859 {
860     cl->diagnostic = diagnostic;
861 }
862
863 int client_get_diagnostic(struct client *cl)
864 {
865     return cl->diagnostic;
866 }
867
868 void client_set_database(struct client *cl, struct session_database *db)
869 {
870     cl->database = db;
871 }
872
873 struct host *client_get_host(struct client *cl)
874 {
875     return client_get_database(cl)->database->host;
876 }
877
878 const char *client_get_url(struct client *cl)
879 {
880     if (cl->database)
881         return client_get_database(cl)->database->url;
882     else
883         return "NOURL";
884 }
885
886 void client_set_maxrecs(struct client *cl, int v)
887 {
888     cl->maxrecs = v;
889 }
890
891 void client_set_startrecs(struct client *cl, int v)
892 {
893     cl->startrecs = v;
894 }
895
896 /*
897  * Local variables:
898  * c-basic-offset: 4
899  * c-file-style: "Stroustrup"
900  * indent-tabs-mode: nil
901  * End:
902  * vim: shiftwidth=4 tabstop=8 expandtab
903  */
904