+
+static Res zebra_open_res (ZebraHandle zh)
+{
+ Res res = 0;
+ char fname[512];
+ ASSERTZH;
+ zh->errCode=0;
+
+ if (zh->path_reg)
+ {
+ sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
+ res = res_open (fname, zh->service->global_res);
+ if (!res)
+ res = zh->service->global_res;
+ }
+ else if (*zh->reg_name == 0)
+ {
+ res = zh->service->global_res;
+ }
+ else
+ {
+ yaz_log (LOG_WARN, "no register root specified");
+ return 0; /* no path for register - fail! */
+ }
+ return res;
+}
+
+static void zebra_close_res (ZebraHandle zh)
+{
+ ASSERTZH;
+ zh->errCode=0;
+ if (zh->res != zh->service->global_res)
+ res_close (zh->res);
+ zh->res = 0;
+}
+
+static int zebra_select_register (ZebraHandle zh, const char *new_reg)
+{
+ ASSERTZH;
+ zh->errCode=0;
+ if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
+ return 0;
+ if (!zh->res)
+ {
+ assert (zh->reg == 0);
+ assert (*zh->reg_name == 0);
+ }
+ else
+ {
+ if (zh->reg)
+ {
+ resultSetInvalidate (zh);
+ zebra_register_close (zh->service, zh->reg);
+ zh->reg = 0;
+ }
+ zebra_close_res(zh);
+ }
+ xfree (zh->reg_name);
+ zh->reg_name = xstrdup (new_reg);
+
+ xfree (zh->path_reg);
+ zh->path_reg = 0;
+ if (zh->service->path_root)
+ {
+ zh->path_reg = xmalloc (strlen(zh->service->path_root) +
+ strlen(zh->reg_name) + 3);
+ strcpy (zh->path_reg, zh->service->path_root);
+ if (*zh->reg_name)
+ {
+ strcat (zh->path_reg, "/");
+ strcat (zh->path_reg, zh->reg_name);
+ }
+ }
+ zh->res = zebra_open_res (zh);
+
+ if (zh->lock_normal)
+ zebra_lock_destroy (zh->lock_normal);
+ zh->lock_normal = 0;
+
+ if (zh->lock_shadow)
+ zebra_lock_destroy (zh->lock_shadow);
+ zh->lock_shadow = 0;
+
+ if (zh->res)
+ {
+ char fname[512];
+ const char *lock_area =res_get (zh->res, "lockDir");
+
+ if (!lock_area && zh->path_reg)
+ res_put (zh->res, "lockDir", zh->path_reg);
+ sprintf (fname, "norm.%s.LCK", zh->reg_name);
+ zh->lock_normal =
+ zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
+
+ sprintf (fname, "shadow.%s.LCK", zh->reg_name);
+ zh->lock_shadow =
+ zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
+
+ }
+ return 1;
+}
+