Zebra server checks for zebrasrv.pid and refuses to start if it is already
[idzebra-moved-to-github.git] / index / kdump.c
1 /* $Id: kdump.c,v 1.23 2003-05-24 22:35:11 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 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #ifdef WIN32
28 #include <io.h>
29 #else
30 #include <unistd.h>
31 #endif
32 #include <assert.h>
33
34 #include <charmap.h>
35 #include "index.h"
36
37 char *prog;
38
39
40 int key_file_decode (FILE *f)
41 {
42     int c, d;
43
44     c = getc (f);
45     switch (c & 192) 
46     {
47     case 0:
48         d = c;
49         break;
50     case 64:
51         d = ((c&63) << 8) + (getc (f) & 0xff);
52         break;
53     case 128:
54         d = ((c&63) << 8) + (getc (f) & 0xff);
55         d = (d << 8) + (getc (f) & 0xff);
56         break;
57     case 192:
58         d = ((c&63) << 8) + (getc (f) & 0xff);
59         d = (d << 8) + (getc (f) & 0xff);
60         d = (d << 8) + (getc (f) & 0xff);
61         break;
62     default:
63         d = 0;
64         assert (0);
65     }
66     return d;
67 }
68
69
70 static int read_one (FILE *inf, char *name, char *key, struct it_key *prevk)
71 {
72     int c;
73     int i = 0;
74     struct it_key itkey;
75     do
76     {
77         if ((c=getc(inf)) == EOF)
78             return 0;
79         name[i++] = c;
80     } while (c);
81     if (i > 1)
82         prevk->sysno = 0;
83     c = key_file_decode (inf);
84     key[0] = c & 1;
85     c = c >> 1;
86     itkey.sysno = c + prevk->sysno;
87     if (c)
88     {
89         prevk->sysno = itkey.sysno;
90         prevk->seqno = 0;
91     }
92     c = key_file_decode (inf);
93     itkey.seqno = c + prevk->seqno;
94     prevk->seqno = itkey.seqno;
95
96     memcpy (key+1, &itkey, sizeof(itkey));
97     return 1;
98 }
99
100 int main (int argc, char **argv)
101 {
102     int ret;
103     char *arg;
104     char *key_fname = NULL;
105     char key_string[IT_MAX_WORD];
106     char key_info[256];
107     ZebraMaps zm;
108     FILE *inf;
109     Res res = NULL;
110     struct it_key prevk;
111
112     prevk.sysno = 0;
113     prevk.seqno = 0;
114
115     prog = *argv;
116     while ((ret = options ("c:v:", argv, argc, &arg)) != -2)
117     {
118         if (ret == 0)
119         {
120             key_fname = arg;
121         }
122         else if (ret == 'v')
123         {
124             yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
125         }
126         else if (ret == 'c')
127         {
128             if (!(res = res_open (arg, 0)))
129             {
130                 logf(LOG_FATAL, "Failed to open resource file %s", arg);
131                 exit (1);
132             }
133         }
134         else
135         {
136             logf (LOG_FATAL, "Unknown option '-%s'", arg);
137             exit (1);
138         }
139     }
140     if (!key_fname)
141     {
142         fprintf (stderr, "kdump [-c config] [-v log] file\n");
143         exit (1);
144     }
145     if (!res)
146         res = res_open ("zebra.cfg", 0);
147     zm = zebra_maps_open (res, 0);
148     if (!(inf = fopen (key_fname, "r")))
149     {
150         logf (LOG_FATAL|LOG_ERRNO, "fopen %s", key_fname);
151         exit (1);
152     }
153     printf ("t  rg op  sysno seqno txt\n");
154     while (read_one (inf, key_string, key_info, &prevk))
155     {
156         struct it_key k;
157         int op;
158         char keybuf[IT_MAX_WORD+1];
159         char *to = keybuf;
160         const char *from = key_string;
161         int usedb_type = from[0];
162         int reg_type = from[1];
163
164         op = key_info[0];
165         memcpy (&k, 1+key_info, sizeof(k));
166
167         from += 2;  
168         while (*from)
169         {
170             const char *res = zebra_maps_output (zm, reg_type, &from);
171             if (!res)
172                 *to++ = *from++;
173             else
174                 while (*res)
175                     *to++ = *res++;
176         }
177         *to = '\0';
178         printf ("%c %3d %c %7d %5d %s\n", reg_type, usedb_type, op ? 'i':'d',
179                 k.sysno, k.seqno, keybuf);
180     }
181     zebra_maps_close (zm);
182     if (fclose (inf))
183     {
184         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", key_fname);
185         exit (1);
186     }
187     
188     exit (0);
189 }