Towards GPL
[idzebra-moved-to-github.git] / index / kdump.c
1 /* $Id: kdump.c,v 1.22 2002-08-02 19:26:55 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     }
63     return d;
64 }
65
66
67 static int read_one (FILE *inf, char *name, char *key, struct it_key *prevk)
68 {
69     int c;
70     int i = 0;
71     struct it_key itkey;
72     do
73     {
74         if ((c=getc(inf)) == EOF)
75             return 0;
76         name[i++] = c;
77     } while (c);
78     if (i > 1)
79         prevk->sysno = 0;
80     c = key_file_decode (inf);
81     key[0] = c & 1;
82     c = c >> 1;
83     itkey.sysno = c + prevk->sysno;
84     if (c)
85     {
86         prevk->sysno = itkey.sysno;
87         prevk->seqno = 0;
88     }
89     c = key_file_decode (inf);
90     itkey.seqno = c + prevk->seqno;
91     prevk->seqno = itkey.seqno;
92
93     memcpy (key+1, &itkey, sizeof(itkey));
94     return 1;
95 }
96
97 int main (int argc, char **argv)
98 {
99     int ret;
100     char *arg;
101     char *key_fname = NULL;
102     char key_string[IT_MAX_WORD];
103     char key_info[256];
104     ZebraMaps zm;
105     FILE *inf;
106     Res res = NULL;
107     struct it_key prevk;
108
109     prevk.sysno = 0;
110     prevk.seqno = 0;
111
112     prog = *argv;
113     while ((ret = options ("c:v:", argv, argc, &arg)) != -2)
114     {
115         if (ret == 0)
116         {
117             key_fname = arg;
118         }
119         else if (ret == 'v')
120         {
121             yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
122         }
123         else if (ret == 'c')
124         {
125             if (!(res = res_open (arg, 0)))
126             {
127                 logf(LOG_FATAL, "Failed to open resource file %s", arg);
128                 exit (1);
129             }
130         }
131         else
132         {
133             logf (LOG_FATAL, "Unknown option '-%s'", arg);
134             exit (1);
135         }
136     }
137     if (!key_fname)
138     {
139         fprintf (stderr, "kdump [-c config] [-v log] file\n");
140         exit (1);
141     }
142     if (!res)
143         res = res_open ("zebra.cfg", 0);
144     zm = zebra_maps_open (res, 0);
145     if (!(inf = fopen (key_fname, "r")))
146     {
147         logf (LOG_FATAL|LOG_ERRNO, "fopen %s", key_fname);
148         exit (1);
149     }
150     printf ("t  rg op  sysno seqno txt\n");
151     while (read_one (inf, key_string, key_info, &prevk))
152     {
153         struct it_key k;
154         int op;
155         char keybuf[IT_MAX_WORD+1];
156         char *to = keybuf;
157         const char *from = key_string;
158         int usedb_type = from[0];
159         int reg_type = from[1];
160
161         op = key_info[0];
162         memcpy (&k, 1+key_info, sizeof(k));
163
164         from += 2;  
165         while (*from)
166         {
167             const char *res = zebra_maps_output (zm, reg_type, &from);
168             if (!res)
169                 *to++ = *from++;
170             else
171                 while (*res)
172                     *to++ = *res++;
173         }
174         *to = '\0';
175         printf ("%c %3d %c %7d %5d %s\n", reg_type, usedb_type, op ? 'i':'d',
176                 k.sysno, k.seqno, keybuf);
177     }
178     zebra_maps_close (zm);
179     if (fclose (inf))
180     {
181         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", key_fname);
182         exit (1);
183     }
184     
185     exit (0);
186 }