Fix memory leak for sortKeys.buf.
[idzebra-moved-to-github.git] / index / zebraapi.c
1 /* $Id: zebraapi.c,v 1.85 2003-02-27 22:55:40 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 #include <assert.h>
26 #include <stdio.h>
27 #ifdef WIN32
28 #include <io.h>
29 #include <process.h>
30 #include <direct.h>
31 #else
32 #include <unistd.h>
33 #endif
34
35 #include <yaz/diagbib1.h>
36 #include "index.h"
37 #include <charmap.h>
38
39 /* simple asserts to validate the most essential input args */
40 #define ASSERTZH assert(zh && zh->service)
41 #define ASSERTZHRES assert(zh && zh->service && zh->res)
42 #define ASSERTZS assert(zs)
43
44 static Res zebra_open_res (ZebraHandle zh);
45 static void zebra_close_res (ZebraHandle zh);
46
47
48 static void zebra_chdir (ZebraService zs)
49 {
50     const char *dir ;
51     ASSERTZS;
52     dir = res_get (zs->global_res, "chdir");
53     if (!dir)
54         return;
55     logf (LOG_DEBUG, "chdir %s", dir);
56 #ifdef WIN32
57     _chdir(dir);
58 #else
59     chdir (dir);
60 #endif
61 }
62
63 static void zebra_flush_reg (ZebraHandle zh)
64 {
65     ASSERTZH;
66     zh->errCode=0;
67     zebraExplain_flush (zh->reg->zei, zh);
68     
69     extract_flushWriteKeys (zh);
70     zebra_index_merge (zh);
71 }
72
73 static struct zebra_register *zebra_register_open (ZebraService zs, 
74                                                    const char *name,
75                                                    int rw, int useshadow,
76                                                    Res res,
77                                                    const char *reg_path);
78 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
79
80 ZebraHandle zebra_open (ZebraService zs)
81 {
82     ZebraHandle zh;
83     const char *default_encoding;
84     ASSERTZS;
85
86     if (!zs)
87         return 0;
88
89     zh = (ZebraHandle) xmalloc (sizeof(*zh));
90     yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
91
92     zh->service = zs;
93     zh->reg = 0;          /* no register attached yet */
94     zh->sets = 0;
95     zh->destroyed = 0;
96     zh->errCode = 0;
97     zh->errString = 0;
98     zh->res = 0; 
99
100     zh->reg_name = xstrdup ("");
101     zh->path_reg = 0;
102     zh->num_basenames = 0;
103     zh->basenames = 0;
104
105     zh->trans_no = 0;
106
107     zh->lock_normal = 0;
108     zh->lock_shadow = 0;
109
110     zh->admin_databaseName = 0;
111
112     zh->shadow_enable = 1;
113
114     default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
115     zh->record_encoding = xstrdup (default_encoding);
116
117     zh->iconv_to_utf8 =
118         yaz_iconv_open ("UTF-8", default_encoding);
119     if (zh->iconv_to_utf8 == 0)
120         yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
121            default_encoding);
122     zh->iconv_from_utf8 =
123         yaz_iconv_open (default_encoding, "UTF-8");
124     if (zh->iconv_to_utf8 == 0)
125         yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
126            default_encoding);
127
128     zebra_mutex_cond_lock (&zs->session_lock);
129
130     zh->next = zs->sessions;
131     zs->sessions = zh;
132
133     zebra_mutex_cond_unlock (&zs->session_lock);
134
135     return zh;
136 }
137
138 ZebraService zebra_start (const char *configName)
139 {
140     Res res;
141
142     yaz_log (LOG_LOG, "zebra_start %s", configName);
143
144     if ((res = res_open (configName, 0)))
145     {
146         ZebraService zh = xmalloc (sizeof(*zh));
147
148         yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
149         
150         zh->global_res = res;
151         zh->configName = xstrdup(configName);
152         zh->sessions = 0;
153         
154         zebra_chdir (zh);
155         
156         zebra_mutex_cond_init (&zh->session_lock);
157         if (!res_get (zh->global_res, "passwd"))
158             zh->passwd_db = NULL;
159         else
160         {
161             zh->passwd_db = passwd_db_open ();
162             if (!zh->passwd_db)
163                 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
164             else
165                 passwd_db_file (zh->passwd_db,
166                                 res_get (zh->global_res, "passwd"));
167         }
168         zh->path_root = res_get (zh->global_res, "root");
169         return zh;
170     }
171     return 0;
172 }
173
174 static
175 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
176                                             int rw, int useshadow, Res res,
177                                             const char *reg_path)
178 {
179     struct zebra_register *reg;
180     int record_compression = REC_COMPRESS_NONE;
181     char *recordCompression = 0;
182
183     ASSERTZS;
184     
185     reg = xmalloc (sizeof(*reg));
186
187     assert (name);
188     reg->name = xstrdup (name);
189
190     reg->seqno = 0;
191     reg->last_val = 0;
192
193     assert (res);
194
195     yaz_log (LOG_DEBUG, "zebra_register_open rw = %d useshadow=%d p=%p",
196              rw, useshadow, reg);
197
198     reg->dh = data1_createx (DATA1_FLAG_XML);
199     if (!reg->dh)
200         return 0;
201     reg->bfs = bfs_create (res_get (res, "register"), reg_path);
202     if (!reg->bfs)
203     {
204         data1_destroy(reg->dh);
205         return 0;
206     }
207     if (useshadow)
208         bf_cache (reg->bfs, res_get (res, "shadow"));
209     data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
210                                             DEFAULT_PROFILE_PATH));
211     data1_set_tabroot (reg->dh, reg_path);
212     reg->recTypes = recTypes_init (reg->dh);
213     recTypes_default_handlers (reg->recTypes);
214
215     reg->zebra_maps = zebra_maps_open (res, reg_path);
216     reg->rank_classes = NULL;
217
218     reg->key_buf = 0;
219
220     reg->keys.buf_max = 0;
221     reg->keys.buf = 0;
222     reg->sortKeys.buf = 0;
223     reg->sortKeys.buf_max = 0;
224
225     reg->records = 0;
226     reg->dict = 0;
227     reg->sortIdx = 0;
228     reg->isams = 0;
229     reg->matchDict = 0;
230     reg->isam = 0;
231     reg->isamc = 0;
232     reg->isamd = 0;
233     reg->isamb = 0;
234     reg->zei = 0;
235     reg->matchDict = 0;
236     reg->key_file_no = 0;
237     
238     zebraRankInstall (reg, rank1_class);
239     zebraRankInstall (reg, rankzv_class);
240
241     recordCompression = res_get_def (res, "recordCompression", "none");
242     if (!strcmp (recordCompression, "none"))
243         record_compression = REC_COMPRESS_NONE;
244     if (!strcmp (recordCompression, "bzip2"))
245         record_compression = REC_COMPRESS_BZIP2;
246
247     if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
248     {
249         logf (LOG_WARN, "rec_open");
250         return 0;
251     }
252     if (rw)
253     {
254         reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
255     }
256     if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
257     {
258         logf (LOG_WARN, "dict_open");
259         return 0;
260     }
261     if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
262     {
263         logf (LOG_WARN, "sortIdx_open");
264         return 0;
265     }
266     if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
267     {
268         struct ISAMS_M_s isams_m;
269         if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
270                                       key_isams_m(res, &isams_m))))
271         {
272             logf (LOG_WARN, "isams_open");
273             return 0;
274         }
275     }
276     if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
277     {
278         if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
279                                   sizeof (struct it_key), res)))
280         {
281             logf (LOG_WARN, "is_open");
282             return 0;
283         }
284     }
285     if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
286     {
287         struct ISAMC_M_s isamc_m;
288         if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
289                                     rw, key_isamc_m(res, &isamc_m))))
290         {
291             logf (LOG_WARN, "isc_open");
292             return 0;
293         }
294     }
295     if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
296     {
297         struct ISAMD_M_s isamd_m;
298         
299         if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
300                                       rw, key_isamd_m(res, &isamd_m))))
301         {
302             logf (LOG_WARN, "isamd_open");
303             return 0;
304         }
305     }
306     if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
307     {
308         struct ISAMC_M_s isamc_m;
309         
310         if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
311                                        rw, key_isamc_m(res, &isamc_m), 0)))
312         {
313             logf (LOG_WARN, "isamb_open");
314             return 0;
315         }
316     }
317     if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
318     {
319         struct ISAMC_M_s isamc_m;
320         
321         if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
322                                        rw, key_isamc_m(res, &isamc_m), 1)))
323         {
324             logf (LOG_WARN, "isamb_open");
325             return 0;
326         }
327     }
328     if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
329     {
330         struct ISAMC_M_s isamc_m;
331         
332         if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
333                                        rw, key_isamc_m(res, &isamc_m), -1)))
334         {
335             logf (LOG_WARN, "isamb_open");
336             return 0;
337         }
338     }
339     reg->zei = zebraExplain_open (reg->records, reg->dh,
340                                   res, rw, reg,
341                                   explain_extract);
342     if (!reg->zei)
343     {
344         logf (LOG_WARN, "Cannot obtain EXPLAIN information");
345         return 0;
346     }
347     reg->active = 2;
348     yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
349     return reg;
350 }
351
352 void zebra_admin_shutdown (ZebraHandle zh)
353 {
354     ASSERTZH;
355     zh->errCode=0;
356
357     zebra_mutex_cond_lock (&zh->service->session_lock);
358     zh->service->stop_flag = 1;
359     zebra_mutex_cond_unlock (&zh->service->session_lock);
360 }
361
362 void zebra_admin_start (ZebraHandle zh)
363 {
364     ZebraService zs;
365     ASSERTZH;
366     zh->errCode=0;
367     zs = zh->service;
368     zebra_mutex_cond_lock (&zs->session_lock);
369     zebra_mutex_cond_unlock (&zs->session_lock);
370 }
371
372 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
373 {
374     ASSERTZS;
375     yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
376     reg->stop_flag = 0;
377     zebra_chdir (zs);
378     if (reg->records)
379     {
380         zebraExplain_close (reg->zei);
381         dict_close (reg->dict);
382         if (reg->matchDict)
383             dict_close (reg->matchDict);
384         sortIdx_close (reg->sortIdx);
385         if (reg->isams)
386             isams_close (reg->isams);
387         if (reg->isam)
388             is_close (reg->isam);
389         if (reg->isamc)
390             isc_close (reg->isamc);
391         if (reg->isamd)
392             isamd_close (reg->isamd);
393         if (reg->isamb)
394             isamb_close (reg->isamb);
395         rec_close (&reg->records);
396     }
397
398     recTypes_destroy (reg->recTypes);
399     zebra_maps_close (reg->zebra_maps);
400     zebraRankDestroy (reg);
401     bfs_destroy (reg->bfs);
402     data1_destroy (reg->dh);
403
404     xfree (reg->sortKeys.buf);
405     xfree (reg->keys.buf);
406
407     xfree (reg->key_buf);
408     xfree (reg->name);
409     xfree (reg);
410     yaz_log(LOG_DEBUG, "zebra_register_close 2");
411 }
412
413 void zebra_stop(ZebraService zs)
414 {
415     if (!zs)
416         return ;
417     yaz_log (LOG_LOG, "zebra_stop");
418
419     while (zs->sessions)
420     {
421         zebra_close (zs->sessions);
422     }
423         
424     zebra_mutex_cond_destroy (&zs->session_lock);
425
426     if (zs->passwd_db)
427         passwd_db_close (zs->passwd_db);
428
429     res_close (zs->global_res);
430     xfree (zs->configName);
431     xfree (zs->path_root);
432     xfree (zs);
433 }
434
435 void zebra_close (ZebraHandle zh)
436 {
437     ZebraService zs;
438     struct zebra_session **sp;
439     int i;
440
441     if (!zh)
442         return;
443     ASSERTZH;
444     zh->errCode=0;
445     
446     zs = zh->service;
447     yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
448     if (!zh)
449         return ;
450     resultSetDestroy (zh, -1, 0, 0);
451
452     if (zh->reg)
453         zebra_register_close (zh->service, zh->reg);
454     zebra_close_res (zh);
455
456     xfree (zh->record_encoding);
457
458     for (i = 0; i < zh->num_basenames; i++)
459         xfree (zh->basenames[i]);
460     xfree (zh->basenames);
461
462     if (zh->iconv_to_utf8 != 0)
463         yaz_iconv_close (zh->iconv_to_utf8);
464     if (zh->iconv_from_utf8 != 0)
465         yaz_iconv_close (zh->iconv_from_utf8);
466
467     xfree (zh->admin_databaseName);
468     zebra_mutex_cond_lock (&zs->session_lock);
469     zebra_lock_destroy (zh->lock_normal);
470     zebra_lock_destroy (zh->lock_shadow);
471     sp = &zs->sessions;
472     while (1)
473     {
474         assert (*sp);
475         if (*sp == zh)
476         {
477             *sp = (*sp)->next;
478             break;
479         }
480         sp = &(*sp)->next;
481     }
482     zebra_mutex_cond_unlock (&zs->session_lock);
483     xfree (zh->reg_name);
484     zh->service=0; /* more likely to trigger an assert */
485     xfree (zh);
486 }
487
488 struct map_baseinfo {
489     ZebraHandle zh;
490     NMEM mem;
491     int num_bases;
492     char **basenames;
493     int new_num_bases;
494     char **new_basenames;
495     int new_num_max;
496 };
497
498 static Res zebra_open_res (ZebraHandle zh)
499 {
500     Res res = 0;
501     char fname[512];
502     ASSERTZH;
503     zh->errCode=0;
504
505     if (zh->path_reg)
506     {
507         sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
508         res = res_open (fname, zh->service->global_res);
509         if (!res)
510             res = zh->service->global_res;
511     }
512     else if (*zh->reg_name == 0)
513     {
514         res = zh->service->global_res;
515     }
516     else
517     {
518         yaz_log (LOG_WARN, "no register root specified");
519         return 0;  /* no path for register - fail! */
520     }
521     return res;
522 }
523
524 static void zebra_close_res (ZebraHandle zh)
525 {
526     ASSERTZH;
527     zh->errCode=0;
528     if (zh->res != zh->service->global_res)
529         res_close (zh->res);
530     zh->res = 0;
531 }
532
533 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
534 {
535     ASSERTZH;
536     zh->errCode=0;
537     if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
538         return 0;
539     if (!zh->res)
540     {
541         assert (zh->reg == 0);
542         assert (*zh->reg_name == 0);
543     }
544     else
545     {
546         if (zh->reg)
547         {
548             resultSetInvalidate (zh);
549             zebra_register_close (zh->service, zh->reg);
550             zh->reg = 0;
551         }
552         zebra_close_res(zh);
553     }
554     xfree (zh->reg_name);
555     zh->reg_name = xstrdup (new_reg);
556
557     xfree (zh->path_reg);
558     zh->path_reg = 0;
559     if (zh->service->path_root)
560     {
561         zh->path_reg = xmalloc (strlen(zh->service->path_root) + 
562                                 strlen(zh->reg_name) + 3);
563         strcpy (zh->path_reg, zh->service->path_root);
564         if (*zh->reg_name)
565         {
566             strcat (zh->path_reg, "/");
567             strcat (zh->path_reg, zh->reg_name);
568         }
569     }
570     zh->res = zebra_open_res (zh);
571     
572     if (zh->lock_normal)
573         zebra_lock_destroy (zh->lock_normal);
574     zh->lock_normal = 0;
575
576     if (zh->lock_shadow)
577         zebra_lock_destroy (zh->lock_shadow);
578     zh->lock_shadow = 0;
579
580     if (zh->res)
581     {
582         char fname[512];
583         const char *lock_area  =res_get (zh->res, "lockDir");
584         
585         if (!lock_area && zh->path_reg)
586             res_put (zh->res, "lockDir", zh->path_reg);
587         sprintf (fname, "norm.%s.LCK", zh->reg_name);
588         zh->lock_normal =
589             zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
590         
591         sprintf (fname, "shadow.%s.LCK", zh->reg_name);
592         zh->lock_shadow =
593             zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
594
595     }
596     return 1;
597 }
598
599 void map_basenames_func (void *vp, const char *name, const char *value)
600 {
601     struct map_baseinfo *p = (struct map_baseinfo *) vp;
602     int i, no;
603     char fromdb[128], todb[8][128];
604     
605     no =
606         sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
607                 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
608                 todb[5], todb[6], todb[7]);
609     if (no < 2)
610         return ;
611     no--;
612     for (i = 0; i<p->num_bases; i++)
613         if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
614         {
615             p->basenames[i] = 0;
616             for (i = 0; i < no; i++)
617             {
618                 if (p->new_num_bases == p->new_num_max)
619                     return;
620                 p->new_basenames[(p->new_num_bases)++] = 
621                     nmem_strdup (p->mem, todb[i]);
622             }
623             return;
624         }
625 }
626
627 void map_basenames (ZebraHandle zh, ODR stream,
628                     int *num_bases, char ***basenames)
629 {
630     struct map_baseinfo info;
631     struct map_baseinfo *p = &info;
632     int i;
633     ASSERTZH;
634     zh->errCode=0;
635
636     info.zh = zh;
637     info.num_bases = *num_bases;
638     info.basenames = *basenames;
639     info.new_num_max = 128;
640     info.new_num_bases = 0;
641     info.new_basenames = (char **)
642         odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
643     info.mem = stream->mem;
644
645     res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
646     
647     for (i = 0; i<p->num_bases; i++)
648         if (p->basenames[i] && p->new_num_bases < p->new_num_max)
649         {
650             p->new_basenames[(p->new_num_bases)++] = 
651                 nmem_strdup (p->mem, p->basenames[i]);
652         }
653     *num_bases = info.new_num_bases;
654     *basenames = info.new_basenames;
655     for (i = 0; i<*num_bases; i++)
656         logf (LOG_LOG, "base %s", (*basenames)[i]);
657 }
658
659 int zebra_select_database (ZebraHandle zh, const char *basename)
660 {
661     ASSERTZH;
662     zh->errCode=0;
663     return zebra_select_databases (zh, 1, &basename);
664 }
665
666 int zebra_select_databases (ZebraHandle zh, int num_bases,
667                             const char **basenames)
668 {
669     int i;
670     const char *cp;
671     int len = 0;
672     char *new_reg = 0;
673     ASSERTZH;
674     zh->errCode=0;
675     
676     if (num_bases < 1)
677     {
678         zh->errCode = 23;
679         return -1;
680     }
681     for (i = 0; i < zh->num_basenames; i++)
682         xfree (zh->basenames[i]);
683     xfree (zh->basenames);
684     
685     zh->num_basenames = num_bases;
686     zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
687     for (i = 0; i < zh->num_basenames; i++)
688         zh->basenames[i] = xstrdup (basenames[i]);
689
690     cp = strrchr(basenames[0], '/');
691     if (cp)
692     {
693         len = cp - basenames[0];
694         new_reg = xmalloc (len + 1);
695         memcpy (new_reg, basenames[0], len);
696         new_reg[len] = '\0';
697     }
698     else
699         new_reg = xstrdup ("");
700     for (i = 1; i<num_bases; i++)
701     {
702         const char *cp1;
703
704         cp1 = strrchr (basenames[i], '/');
705         if (cp)
706         {
707             if (!cp1)
708             {
709                 zh->errCode = 23;
710                 return -1;
711             }
712             if (len != cp1 - basenames[i] ||
713                 memcmp (basenames[i], new_reg, len))
714             {
715                 zh->errCode = 23;
716                 return -1;
717             }
718         }
719         else
720         {
721             if (cp1)
722             {
723                 zh->errCode = 23;
724                 return -1;
725             }
726         }
727     }
728     zebra_select_register (zh, new_reg);
729     xfree (new_reg);
730     if (!zh->res)
731     {
732         zh->errCode = 109;
733         return -1;
734     }
735     if (!zh->lock_normal || !zh->lock_shadow)
736     {
737         zh->errCode = 2;
738         return -1;
739     }
740     return 0;
741 }
742
743 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
744                        Z_RPNQuery *query, const char *setname, int *hits)
745 {
746     ASSERTZH;
747     zh->errCode=0;
748     zh->hits = 0;
749     *hits = 0;
750
751     if (zebra_begin_read (zh))
752         return;
753     resultSetAddRPN (zh, decode, stream, query, 
754                      zh->num_basenames, zh->basenames, setname);
755
756     zebra_end_read (zh);
757
758     *hits = zh->hits;
759 }
760
761 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
762                              const char *setname, Z_RecordComposition *comp,
763                              oid_value input_format, int num_recs,
764                              ZebraRetrievalRecord *recs)
765 {
766     ZebraPosSet poset;
767     int i, *pos_array;
768     ASSERTZH;
769     zh->errCode=0;
770
771     if (!zh->res)
772     {
773         zh->errCode = 30;
774         zh->errString = odr_strdup (stream, setname);
775         return;
776     }
777     
778     zh->errCode = 0;
779
780     if (zebra_begin_read (zh))
781         return;
782
783     pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
784     for (i = 0; i<num_recs; i++)
785         pos_array[i] = recs[i].position;
786     poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
787     if (!poset)
788     {
789         logf (LOG_DEBUG, "zebraPosSetCreate error");
790         zh->errCode = 30;
791         zh->errString = nmem_strdup (stream->mem, setname);
792     }
793     else
794     {
795         for (i = 0; i<num_recs; i++)
796         {
797             if (poset[i].term)
798             {
799                 recs[i].errCode = 0;
800                 recs[i].format = VAL_SUTRS;
801                 recs[i].len = strlen(poset[i].term);
802                 recs[i].buf = poset[i].term;
803                 recs[i].base = poset[i].db;
804             }
805             else if (poset[i].sysno)
806             {
807                 recs[i].errCode =
808                     zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
809                                         stream, input_format, comp,
810                                         &recs[i].format, &recs[i].buf,
811                                         &recs[i].len,
812                                         &recs[i].base);
813                 recs[i].errString = NULL;
814             }
815             else
816             {
817                 char num_str[20];
818
819                 sprintf (num_str, "%d", pos_array[i]);  
820                 zh->errCode = 13;
821                 zh->errString = odr_strdup (stream, num_str);
822                 break;
823             }
824         }
825         zebraPosSetDestroy (zh, poset, num_recs);
826     }
827     zebra_end_read (zh);
828     xfree (pos_array);
829 }
830
831 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
832                  oid_value attributeset,
833                  int *position, int *num_entries, ZebraScanEntry **entries,
834                  int *is_partial)
835 {
836     ASSERTZH;
837     zh->errCode=0;
838     if (zebra_begin_read (zh))
839     {
840         *entries = 0;
841         *num_entries = 0;
842         return;
843     }
844     rpn_scan (zh, stream, zapt, attributeset,
845               zh->num_basenames, zh->basenames, position,
846               num_entries, entries, is_partial);
847     zebra_end_read (zh);
848 }
849
850 void zebra_sort (ZebraHandle zh, ODR stream,
851                  int num_input_setnames, const char **input_setnames,
852                  const char *output_setname, Z_SortKeySpecList *sort_sequence,
853                  int *sort_status)
854 {
855     ASSERTZH;
856     zh->errCode=0;
857     if (zebra_begin_read (zh))
858         return;
859     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
860                    output_setname, sort_sequence, sort_status);
861     zebra_end_read(zh);
862 }
863
864 int zebra_deleleResultSet(ZebraHandle zh, int function,
865                           int num_setnames, char **setnames,
866                           int *statuses)
867 {
868     int i, status;
869     ASSERTZH;
870     zh->errCode=0;
871     if (zebra_begin_read(zh))
872         return Z_DeleteStatus_systemProblemAtTarget;
873     switch (function)
874     {
875     case Z_DeleteRequest_list:
876         resultSetDestroy (zh, num_setnames, setnames, statuses);
877         break;
878     case Z_DeleteRequest_all:
879         resultSetDestroy (zh, -1, 0, statuses);
880         break;
881     }
882     zebra_end_read (zh);
883     status = Z_DeleteStatus_success;
884     for (i = 0; i<num_setnames; i++)
885         if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
886             status = statuses[i];
887     return status;
888 }
889
890 int zebra_errCode (ZebraHandle zh)
891 {
892     if (zh)
893         return zh->errCode;
894     return 0; 
895 }
896
897 const char *zebra_errString (ZebraHandle zh)
898 {
899     if (zh)
900         return diagbib1_str (zh->errCode);
901     return "";
902 }
903
904 char *zebra_errAdd (ZebraHandle zh)
905 {
906     if (zh)
907         return zh->errString;
908     return "";
909 }
910
911 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
912 {
913     ZebraService zs;
914     ASSERTZH;
915     zh->errCode=0;
916     zs= zh->service;
917     if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
918     {
919         logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
920         return 0;
921     }
922
923     logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
924     return 1;
925 }
926
927 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
928                                const char *record_type)
929 {
930     ASSERTZH;
931     zh->errCode=0;
932     if (zebra_select_database(zh, database))
933         return;
934     zebra_begin_trans (zh);
935     xfree (zh->admin_databaseName);
936     zh->admin_databaseName = xstrdup(database);
937 }
938
939 void zebra_admin_import_end (ZebraHandle zh)
940 {
941     ASSERTZH;
942     zh->errCode=0;
943     zebra_end_trans (zh);
944 }
945
946 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
947 {
948     int sysno;
949     int i;
950     ASSERTZH;
951     zh->errCode=0;
952     for (i = 0; i<segment->num_segmentRecords; i++)
953     {
954         Z_NamePlusRecord *npr = segment->segmentRecords[i];
955         const char *databaseName = npr->databaseName;
956
957         if (!databaseName)
958             databaseName = zh->admin_databaseName;
959         printf ("--------------%d--------------------\n", i);
960         if (npr->which == Z_NamePlusRecord_intermediateFragment)
961         {
962             Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
963             if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
964             {
965                 Odr_oct *oct = fragment->u.notExternallyTagged;
966                 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
967                         oct->buf);
968                 
969                 sysno = 0;
970                 extract_rec_in_mem (zh, "grs.sgml",
971                                     oct->buf, oct->len,
972                                     databaseName,
973                                     0 /* delete_flag */,
974                                     0 /* test_mode */,
975                                     &sysno /* sysno */,
976                                     1 /* store_keys */,
977                                     1 /* store_data */,
978                                     0 /* match criteria */);
979             }
980         }
981     }
982 }
983
984 int zebra_admin_exchange_record (ZebraHandle zh,
985                                  const char *database,
986                                  const char *rec_buf,
987                                  size_t rec_len,
988                                  const char *recid_buf, size_t recid_len,
989                                  int action)
990 {
991     int sysno = 0;
992     char *rinfo = 0;
993     char recid_z[256];
994     ASSERTZH;
995     zh->errCode=0;
996
997     if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
998         return -1;
999     memcpy (recid_z, recid_buf, recid_len);
1000     recid_z[recid_len] = 0;
1001
1002     rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1003     if (rinfo)
1004     {
1005         if (action == 1)  /* fail if insert */
1006             return -1;
1007         memcpy (&sysno, rinfo+1, sizeof(sysno));
1008     }
1009     else
1010     {
1011         if (action == 2 || action == 3) /* fail if delete or update */
1012             return -1;
1013     }
1014     extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1015                         action == 3 ? 1 : 0 /* delete flag */,
1016                         0, &sysno, 1, 1, 0);
1017     if (action == 1)
1018     {
1019         dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1020     }
1021     else if (action == 3)
1022     {
1023         dict_delete (zh->reg->matchDict, recid_z);
1024     }
1025     return 0;
1026 }
1027
1028 void zebra_admin_create (ZebraHandle zh, const char *database)
1029 {
1030     ZebraService zs;
1031     ASSERTZH;
1032     zh->errCode=0;
1033
1034     if (zebra_select_database (zh, database))
1035         return;
1036     zebra_begin_trans (zh);
1037
1038     zs = zh->service;
1039     /* announce database */
1040     if (zebraExplain_newDatabase (zh->reg->zei, database, 0 
1041                                   /* explainDatabase */))
1042     {
1043         zh->errCode = 224;
1044         zh->errString = "database already exist";
1045     }
1046     zebra_end_trans (zh);
1047 }
1048
1049 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1050                        const char *input_str, int input_len,
1051                        char *output_str, int output_len)
1052 {
1053     WRBUF wrbuf;
1054     ASSERTZH;
1055     zh->errCode=0;
1056     if (!zh->reg->zebra_maps)
1057         return -1;
1058     wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1059                           input_str, input_len);
1060     if (!wrbuf)
1061         return -2;
1062     if (wrbuf_len(wrbuf) >= output_len)
1063         return -3;
1064     if (wrbuf_len(wrbuf))
1065         memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1066     output_str[wrbuf_len(wrbuf)] = '\0';
1067     return wrbuf_len(wrbuf);
1068 }
1069
1070
1071 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1072 {
1073     char state_fname[256];
1074     char *fname;
1075     long p = getpid();
1076     FILE *f;
1077     ASSERTZH;
1078     zh->errCode=0;
1079
1080     sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1081     fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1082     f = fopen (fname, "w");
1083
1084     yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1085     fprintf (f, "%c %d %ld\n", val, seqno, p);
1086     fclose (f);
1087     xfree (fname);
1088 }
1089
1090 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1091 {
1092     char state_fname[256];
1093     char *fname;
1094     FILE *f;
1095
1096     ASSERTZH;
1097     zh->errCode=0;
1098     sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1099     fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1100     f = fopen (fname, "r");
1101     *val = 'o';
1102     *seqno = 0;
1103
1104     if (f)
1105     {
1106         fscanf (f, "%c %d", val, seqno);
1107         fclose (f);
1108     }
1109     xfree (fname);
1110 }
1111
1112 int zebra_begin_read (ZebraHandle zh)
1113 {
1114     int dirty = 0;
1115     char val;
1116     int seqno;
1117     ASSERTZH;
1118
1119     assert (zh->res);
1120
1121     (zh->trans_no)++;
1122
1123     if (zh->trans_no != 1)
1124     {
1125         zebra_flush_reg (zh);
1126         return 0;
1127     }
1128     zh->errCode=0;
1129 #if HAVE_SYS_TIMES_H
1130     times (&zh->tms1);
1131 #endif
1132     if (!zh->res)
1133     {
1134         (zh->trans_no)--;
1135         zh->errCode = 109;
1136         return -1;
1137     }
1138     if (!zh->lock_normal || !zh->lock_shadow)
1139     {
1140         (zh->trans_no)--;
1141         zh->errCode = 2;
1142         return -1;
1143     }
1144     zebra_get_state (zh, &val, &seqno);
1145     if (val == 'd')
1146         val = 'o';
1147
1148     if (!zh->reg)
1149         dirty = 1;
1150     else if (seqno != zh->reg->seqno)
1151     {
1152         yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1153                  seqno, zh->reg->seqno);
1154         dirty = 1;
1155     }
1156     else if (zh->reg->last_val != val)
1157     {
1158         yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1159                  val, zh->reg->last_val);
1160         dirty = 1;
1161     }
1162     if (!dirty)
1163         return 0;
1164
1165     if (val == 'c')
1166         zebra_lock_r (zh->lock_shadow);
1167     else
1168         zebra_lock_r (zh->lock_normal);
1169     
1170     if (zh->reg)
1171         zebra_register_close (zh->service, zh->reg);
1172     zh->reg = zebra_register_open (zh->service, zh->reg_name,
1173                                    0, val == 'c' ? 1 : 0,
1174                                    zh->res, zh->path_reg);
1175     if (!zh->reg)
1176     {
1177         zh->errCode = 109;
1178         return -1;
1179     }
1180     zh->reg->last_val = val;
1181     zh->reg->seqno = seqno;
1182
1183     return 0;
1184 }
1185
1186 void zebra_end_read (ZebraHandle zh)
1187 {
1188     ASSERTZH;
1189     (zh->trans_no)--;
1190
1191     if (zh->trans_no != 0)
1192         return;
1193 #if HAVE_SYS_TIMES_H
1194     times (&zh->tms2);
1195     logf (LOG_LOG, "user/system: %ld/%ld",
1196                     (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1197                     (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1198
1199 #endif
1200
1201     zebra_unlock (zh->lock_normal);
1202     zebra_unlock (zh->lock_shadow);
1203 }
1204
1205 void zebra_begin_trans (ZebraHandle zh)
1206 {
1207     int pass;
1208     int seqno = 0;
1209     char val = '?';
1210     const char *rval = 0;
1211     ASSERTZHRES;
1212
1213     assert (zh->res);
1214
1215     (zh->trans_no++);
1216     if (zh->trans_no != 1)
1217     {
1218         return;
1219     }
1220     zh->errCode=0;
1221     
1222     yaz_log (LOG_LOG, "zebra_begin_trans");
1223
1224     zh->records_inserted = 0;
1225     zh->records_updated = 0;
1226     zh->records_deleted = 0;
1227     zh->records_processed = 0;
1228
1229 #if HAVE_SYS_TIMES_H
1230     times (&zh->tms1);
1231 #endif
1232     
1233     /* lock */
1234     if (zh->shadow_enable)
1235         rval = res_get (zh->res, "shadow");
1236
1237     for (pass = 0; pass < 2; pass++)
1238     {
1239         if (rval)
1240         {
1241             zebra_lock_r (zh->lock_normal);
1242             zebra_lock_w (zh->lock_shadow);
1243         }
1244         else
1245         {
1246             zebra_lock_w (zh->lock_normal);
1247             zebra_lock_w (zh->lock_shadow);
1248         }
1249         
1250         zebra_get_state (zh, &val, &seqno);
1251         if (val == 'c')
1252         {
1253             yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1254             zebra_unlock (zh->lock_shadow);
1255             zebra_unlock (zh->lock_normal);
1256             zebra_commit (zh);
1257             continue;
1258         }
1259         else if (val == 'd')
1260         {
1261             if (rval)
1262             {
1263                 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1264                                          zh->path_reg);
1265                 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1266                 bf_commitClean (bfs, rval);
1267                 bfs_destroy (bfs);
1268             }
1269             else
1270             {
1271                 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1272             }
1273         }
1274         break;
1275     }
1276     if (pass == 2)
1277     {
1278         yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1279         abort();
1280         return;
1281     }
1282     zebra_set_state (zh, 'd', seqno);
1283
1284     zh->reg = zebra_register_open (zh->service, zh->reg_name,
1285                                    1, rval ? 1 : 0, zh->res,
1286                                    zh->path_reg);
1287
1288     zh->reg->seqno = seqno;
1289 }
1290
1291 void zebra_end_trans (ZebraHandle zh)
1292 {
1293     char val;
1294     int seqno;
1295     const char *rval;
1296     ASSERTZH;
1297
1298     zh->trans_no--;
1299     if (zh->trans_no != 0)
1300         return;
1301     yaz_log (LOG_LOG, "zebra_end_trans");
1302     rval = res_get (zh->res, "shadow");
1303
1304     zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1305
1306     zebra_flush_reg (zh);
1307
1308     zebra_register_close (zh->service, zh->reg);
1309     zh->reg = 0;
1310
1311     yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d", 
1312              zh->records_processed, zh->records_inserted,
1313              zh->records_updated, zh->records_deleted);
1314
1315     zebra_get_state (zh, &val, &seqno);
1316     if (val != 'd')
1317     {
1318         BFiles bfs = bfs_create (rval, zh->path_reg);
1319         yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1320         bf_commitClean (bfs, rval);
1321         bfs_destroy (bfs);
1322     }
1323     if (!rval)
1324         seqno++;
1325     zebra_set_state (zh, 'o', seqno);
1326
1327     zebra_unlock (zh->lock_shadow);
1328     zebra_unlock (zh->lock_normal);
1329
1330 #if HAVE_SYS_TIMES_H
1331     times (&zh->tms2);
1332     logf (LOG_LOG, "user/system: %ld/%ld",
1333                     (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1334                     (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1335
1336 #endif
1337 }
1338
1339 void zebra_repository_update (ZebraHandle zh)
1340 {
1341     ASSERTZH;
1342     zh->errCode=0;
1343     logf (LOG_LOG, "updating %s", zh->rGroup.path);
1344     repositoryUpdate (zh);    
1345 }
1346
1347 void zebra_repository_delete (ZebraHandle zh)
1348 {
1349     ASSERTZH;
1350     zh->errCode=0;
1351     logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1352     repositoryDelete (zh);
1353 }
1354
1355 void zebra_repository_show (ZebraHandle zh)
1356 {
1357     ASSERTZH;
1358     zh->errCode=0;
1359     repositoryShow (zh);
1360 }
1361
1362 int zebra_commit (ZebraHandle zh)
1363 {
1364     int seqno;
1365     char val;
1366     const char *rval;
1367     BFiles bfs;
1368     ASSERTZH;
1369     zh->errCode=0;
1370
1371     if (!zh->res)
1372     {
1373         zh->errCode = 109;
1374         return -1;
1375     }
1376     rval = res_get (zh->res, "shadow");    
1377     if (!rval)
1378     {
1379         logf (LOG_WARN, "Cannot perform commit");
1380         logf (LOG_WARN, "No shadow area defined");
1381         return 0;
1382     }
1383
1384     zebra_lock_w (zh->lock_normal);
1385     zebra_lock_r (zh->lock_shadow);
1386
1387     bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1388
1389     zebra_get_state (zh, &val, &seqno);
1390
1391     if (rval && *rval)
1392         bf_cache (bfs, rval);
1393     if (bf_commitExists (bfs))
1394     {
1395         zebra_set_state (zh, 'c', seqno);
1396
1397         logf (LOG_LOG, "commit start");
1398         bf_commitExec (bfs);
1399 #ifndef WIN32
1400         sync ();
1401 #endif
1402         logf (LOG_LOG, "commit clean");
1403         bf_commitClean (bfs, rval);
1404         seqno++;
1405         zebra_set_state (zh, 'o', seqno);
1406     }
1407     else
1408     {
1409         logf (LOG_LOG, "nothing to commit");
1410     }
1411     bfs_destroy (bfs);
1412
1413     zebra_unlock (zh->lock_shadow);
1414     zebra_unlock (zh->lock_normal);
1415     return 0;
1416 }
1417
1418 int zebra_init (ZebraHandle zh)
1419 {
1420     const char *rval;
1421     BFiles bfs = 0;
1422     ASSERTZH;
1423     zh->errCode=0;
1424
1425     if (!zh->res)
1426     {
1427         zh->errCode = 109;
1428         return -1;
1429     }
1430     rval = res_get (zh->res, "shadow");
1431
1432     bfs = bfs_create (res_get (zh->service->global_res, "register"),
1433                       zh->path_reg);
1434     if (rval && *rval)
1435         bf_cache (bfs, rval);
1436     
1437     bf_reset (bfs);
1438     bfs_destroy (bfs);
1439     zebra_set_state (zh, 'o', 0);
1440     return 0;
1441 }
1442
1443 int zebra_compact (ZebraHandle zh)
1444 {
1445     BFiles bfs;
1446     ASSERTZH;
1447     zh->errCode=0;
1448     if (!zh->res)
1449     {
1450         zh->errCode = 109;
1451         return -1;
1452     }
1453     bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1454     inv_compact (bfs);
1455     bfs_destroy (bfs);
1456     return 0;
1457 }
1458
1459 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1460 {
1461     int sysno = 0;
1462     int olderr;
1463     ASSERTZH;
1464     zh->errCode=0;
1465     zebra_begin_trans (zh);
1466     if (zh->errCode)
1467       return 0; /* bad sysno */
1468     extract_rec_in_mem (zh, "grs.sgml",
1469                         buf, len,
1470                         "Default",  /* database */
1471                         0 /* delete_flag */,
1472                         0 /* test_mode */,
1473                         &sysno /* sysno */,
1474                         1 /* store_keys */,
1475                         1 /* store_data */,
1476                         0 /* match criteria */);
1477     olderr=zh->errCode;
1478     zebra_end_trans (zh);
1479     if (olderr)
1480       zh->errCode=olderr; 
1481     return sysno;
1482 }
1483
1484 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1485 {
1486     ASSERTZH;
1487     zh->errCode=0;
1488     memcpy (&zh->rGroup, rg, sizeof(*rg));
1489 }
1490
1491 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1492 {
1493     ASSERTZH;
1494     *code = zh->errCode;
1495     *addinfo = zh->errString;
1496 }
1497
1498 void zebra_shadow_enable (ZebraHandle zh, int value)
1499 {
1500     ASSERTZH;
1501     zh->errCode=0;
1502     zh->shadow_enable = value;
1503 }
1504
1505 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1506 {
1507     ASSERTZH;
1508     zh->errCode=0;
1509     xfree (zh->record_encoding);
1510
1511     /*
1512      * Fixme!
1513      */
1514
1515     if (zh->iconv_to_utf8 != 0)
1516         yaz_iconv_close(zh->iconv_to_utf8);
1517     if (zh->iconv_from_utf8 != 0)
1518         yaz_iconv_close(zh->iconv_from_utf8);
1519     
1520     zh->record_encoding = xstrdup (encoding);
1521     
1522     logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1523     
1524     zh->iconv_to_utf8 =
1525         yaz_iconv_open ("UTF-8", encoding);
1526     if (zh->iconv_to_utf8 == 0)
1527         yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1528     zh->iconv_from_utf8 =
1529         yaz_iconv_open (encoding, "UTF-8");
1530     if (zh->iconv_to_utf8 == 0)
1531         yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1532
1533     return 0;
1534 }
1535
1536 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1537 {
1538     ASSERTZH;
1539     zh->errCode=0;
1540     res_put(zh->res, name, value);
1541 }
1542
1543 const char *zebra_get_resource(ZebraHandle zh,
1544                                 const char *name, const char *defaultvalue)
1545 {
1546     ASSERTZH;
1547     zh->errCode=0;
1548     return res_get_def( zh->res, name, (char *)defaultvalue);
1549 }