1 |
/* |
2 |
The Malete project - the Z39.2/Z39.50 database framework of OpenIsis. |
3 |
Version 0.9.x (patchlevel see file Version) |
4 |
Copyright (C) 2001-2004 by Erik Grziwotz, erik@openisis.org |
5 |
|
6 |
This library is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU Lesser General Public |
8 |
License as published by the Free Software Foundation; either |
9 |
version 2.1 of the License, or (at your option) any later version. |
10 |
|
11 |
This library is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14 |
See the GNU Lesser General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU Lesser General Public |
17 |
License along with this library; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
20 |
see README for more information |
21 |
EOH */ |
22 |
|
23 |
/* |
24 |
$Id: malete.c,v 1.9 2004/11/12 10:43:50 kripke Exp $ |
25 |
malete main |
26 |
*/ |
27 |
|
28 |
#include "../tool/tool.h" |
29 |
|
30 |
static int usage () |
31 |
{ |
32 |
eRr( 0, "malete "TOOL_VERSION" core "CORE_VERSION" pw "PW_VERSION |
33 |
#ifndef BUILD_NOCDS |
34 |
" cds" |
35 |
#endif |
36 |
#ifndef BUILD_NOIIF |
37 |
" iif" |
38 |
#endif |
39 |
); |
40 |
return 0; |
41 |
} |
42 |
|
43 |
|
44 |
static int qCb ( QLoop *self ) |
45 |
{ |
46 |
Ptr p; |
47 |
unsigned i, vsz = self->qdx->vsz; |
48 |
const unsigned char *v = self->vals; |
49 |
char buf[512]; |
50 |
int len = qRdKey(self->qdx, buf, sizeof buf, &self->cur); |
51 |
|
52 |
for ( i=self->nvals; i--; v += vsz) { |
53 |
qRdVal(&p, v, self->qdx->ptr); |
54 |
eOut( 0, "%.*s\t%u\t%d\t%u\t%u", len, buf, |
55 |
p.rid, p.tag, p.pos>>16, p.pos&0xffff); |
56 |
} |
57 |
return 0; |
58 |
} /* qCb */ |
59 |
|
60 |
|
61 |
|
62 |
/* ************************************************************ |
63 |
*/ |
64 |
|
65 |
int main ( int argc, const char **argv ) |
66 |
{ |
67 |
List arg; |
68 |
Ses s; |
69 |
List rec; |
70 |
Fld opt; |
71 |
Db *db[2] = {0,0}; |
72 |
char *p; |
73 |
int i; |
74 |
|
75 |
lInit(&rec, "test"); |
76 |
CINIT(&arg); |
77 |
sInit(ses = &s); |
78 |
|
79 |
if ( LOG_VERBOSE >= env.log ) |
80 |
SCPY(env.err, env.opt); |
81 |
|
82 |
for ( i=1; i<3 && i<LLEN(env.opt); i++ ) { /* open 1 or 2 dbs */ |
83 |
if ( VPRI(&opt, env.opt->fld+i) ) { |
84 |
if ( !(db[i-1] = dOpen(p = VDUPZ( &opt ))) ) |
85 |
return 1; |
86 |
mFree( p ); |
87 |
} |
88 |
} |
89 |
|
90 |
if ( !VPRI(&opt, env.opt->fld) ) |
91 |
return usage(); |
92 |
|
93 |
switch ( opt.val[0] ) { |
94 |
case 'c': |
95 |
#ifndef BUILD_NOCDS |
96 |
if ( VEQZ(&opt, "cdsimp") ) { |
97 |
if ( !db[0] ) return cdsuse(); |
98 |
cdsimp(db[0], env.opt->fld); |
99 |
} |
100 |
else if ( VEQZ(&opt, "cdsexp") ) { |
101 |
if ( !db[0] ) return cdsuse(); |
102 |
cdsexp(db[0], env.opt->fld); |
103 |
} |
104 |
#endif |
105 |
break; |
106 |
case 'd': |
107 |
if ( VEQZ(&opt, "dbinfo") ) { |
108 |
if ( !db[0] ) goto dbs1; |
109 |
eRr(0, "db %s path %s has %d records", |
110 |
db[0]->nam, db[0]->pat, db[0]->rdx.mid); |
111 |
i = db[0]->rdx.typ; |
112 |
eRr(0, "mrx type 0x%02x: %d pos + %d len + %d fields = %d bytes", |
113 |
i, 4+(i>>4), 3+(i>>2&3), i&3, db[0]->rdx.ptl); |
114 |
i = db[0]->qdx.typ; |
115 |
eRr(0, |
116 |
"mqd type 0x%02x: blocksize %d, %d byte %s values, %d byte %scompressed keys", |
117 |
i, 1<<((i&QDX_SIZMSK)+QDX_LEAFSH), |
118 |
db[0]->qdx.vsz, QDX_LEAFPV&i ? "plain" : "pointer", |
119 |
db[0]->qdx.ksz, QDX_COMPRS&i ? "" : "un"); |
120 |
i = db[0]->qdx.ptr; |
121 |
if ( !(QDX_LEAFPV&db[0]->qdx.typ) ) |
122 |
eRr(0, |
123 |
"mqd pointers 0x%02x: %d record id + %d tag + %d pos bytes %s first", |
124 |
i, QDX_RIDMIN+((QDX_RIDMSK&i)>>QDX_RIDSH), (QDX_TAGMSK&i)>>QDX_TAGSH, |
125 |
QDX_POSMSK&i, QDX_FULTXT&i ? "tag" : "rid"); |
126 |
if ( db[0]->opt ) |
127 |
SCPYR(env.out, db[0]->opt); |
128 |
} |
129 |
break; |
130 |
case 'h': |
131 |
return usage(); |
132 |
case 'i': |
133 |
#ifndef BUILD_NOIIF |
134 |
if ( VEQZ(&opt, "iifimp") ) { |
135 |
if ( !db[0] ) return iifuse(); |
136 |
iifimp(db[0], env.opt->fld); |
137 |
} |
138 |
else if ( VEQZ(&opt, "iifexp") ) { |
139 |
if ( !db[0] ) return iifuse(); |
140 |
iifexp(db[0], env.opt->fld); |
141 |
} |
142 |
#endif |
143 |
break; |
144 |
case 'q': |
145 |
if ( VEQZ(&opt, "qdump") ) { |
146 |
QLoop ql; |
147 |
if ( !db[0] ) goto dbs1; |
148 |
env.out->snk = fSinkl; |
149 |
memset( &ql, 0, sizeof(ql) ); |
150 |
ql.qcb = qCb; |
151 |
ql.qdx = &db[0]->qdx; |
152 |
qLoop( &ql ); |
153 |
} |
154 |
else if ( VEQZ(&opt, "qload") ) { |
155 |
if ( !db[0] ) goto dbs1; |
156 |
qLoadf(&db[0]->qdx, &env.in); |
157 |
} |
158 |
else if ( VEQZ(&opt, "qchk") ) { |
159 |
QLoop ql; |
160 |
if ( !db[0] ) goto dbs1; |
161 |
memset( &ql, 0, sizeof(ql) ); |
162 |
ql.qcb = 0; |
163 |
ql.qdx = &db[0]->qdx; |
164 |
qLoop( &ql ); |
165 |
} |
166 |
else if ( VEQZ(&opt, "qset") ) { |
167 |
Key key; |
168 |
FIL_DEFBUF(env.in); |
169 |
if ( !db[0] ) goto dbs1; |
170 |
memset( &key, 0, sizeof(key) ); |
171 |
key.val.len = db[0]->qdx.vsz; |
172 |
while ( fGets(&fb) && fb.l ) { |
173 |
if ( qMkKeyVal(&db[0]->qdx, &key, fb.p, fb.l) ) { |
174 |
eRr(LOG_VERBOSE, "bad key at line %d offset %d", fb.n, fb.o); |
175 |
continue; |
176 |
} |
177 |
if ( qSet(&db[0]->qdx, &key) ) |
178 |
break; |
179 |
} |
180 |
eRr(LOG_INFO, "did %d lines %d bytes", fb.n, fb.o); |
181 |
} |
182 |
break; |
183 |
case 'r': |
184 |
if ( VEQZ(&opt, "rdump") ) { |
185 |
if ( !db[0] ) goto dbs1; |
186 |
for (i=1; i<=db[0]->rdx.mid; i++) { |
187 |
dRead(&env.out->lst, db[0], i); |
188 |
SEOR(env.out); |
189 |
} |
190 |
} |
191 |
else if ( VEQZ(&opt, "rload") ) { |
192 |
FIL_DEFBUF(env.in); |
193 |
while ( fGetr(&env.out->lst, &fb) ) { |
194 |
if ( db[0] ) |
195 |
dWrite(db[0], env.out->lst.fld, 0); |
196 |
SEOR(env.out); |
197 |
} |
198 |
} |
199 |
break; |
200 |
case 's': |
201 |
if ( VEQZ(&opt, "server") ) |
202 |
return server(); |
203 |
} |
204 |
|
205 |
return 0; |
206 |
dbs1: |
207 |
eRr(ERR_INVAL, "command %.*s needs a db", opt.len, opt.val); |
208 |
return 1; |
209 |
} /* main */ |