-
Notifications
You must be signed in to change notification settings - Fork 2
/
rlmgr.c
465 lines (421 loc) · 10.5 KB
/
rlmgr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*************************************************************************************************
* Utility for debugging Relic and its applications
* Copyright (C) 2000-2007 Mikio Hirabayashi
* This file is part of QDBM, Quick Database Manager.
* QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; either version
* 2.1 of the License or any later version. QDBM is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License along with QDBM; if
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*************************************************************************************************/
#include <relic.h>
#include <cabin.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#undef TRUE
#define TRUE 1 /* boolean true */
#undef FALSE
#define FALSE 0 /* boolean false */
/* for RISC OS */
#if defined(__riscos__) || defined(__riscos)
#include <unixlib/local.h>
int __riscosify_control = __RISCOSIFY_NO_PROCESS;
#endif
/* global variables */
const char *progname; /* program name */
/* function prototypes */
int main(int argc, char **argv);
void usage(void);
char *hextoobj(const char *str, int *sp);
int runcreate(int argc, char **argv);
int runstore(int argc, char **argv);
int rundelete(int argc, char **argv);
int runfetch(int argc, char **argv);
int runlist(int argc, char **argv);
void pmyerror(const char *name, const char *msg);
void printobj(const char *obj, int size);
void printobjhex(const char *obj, int size);
int docreate(char *name);
int dostore(char *name, const char *kbuf, int ksiz, const char *vbuf, int vsiz, int ins);
int dodelete(char *name, const char *kbuf, int ksiz);
int dofetch(char *name, const char *kbuf, int ksiz, int ox, int nb);
int dolist(char *name, int ox);
/* main routine */
int main(int argc, char **argv){
int rv;
cbstdiobin();
progname = argv[0];
if(argc < 2) usage();
rv = 0;
if(!strcmp(argv[1], "create")){
rv = runcreate(argc, argv);
} else if(!strcmp(argv[1], "store")){
rv = runstore(argc, argv);
} else if(!strcmp(argv[1], "delete")){
rv = rundelete(argc, argv);
} else if(!strcmp(argv[1], "fetch")){
rv = runfetch(argc, argv);
} else if(!strcmp(argv[1], "list")){
rv = runlist(argc, argv);
} else {
usage();
}
return rv;
}
/* print the usage and exit */
void usage(void){
fprintf(stderr, "%s: administration utility for Relic\n", progname);
fprintf(stderr, "\n");
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s create name\n", progname);
fprintf(stderr, " %s store [-kx] [-vx|-vf] [-insert] name key val\n", progname);
fprintf(stderr, " %s delete [-kx] name key\n", progname);
fprintf(stderr, " %s fetch [-kx] [-ox] [-n] name key\n", progname);
fprintf(stderr, " %s list [-ox] name\n", progname);
fprintf(stderr, "\n");
exit(1);
}
/* create a binary object from a hexadecimal string */
char *hextoobj(const char *str, int *sp){
char *buf, mbuf[3];
int len, i, j;
len = strlen(str);
if(!(buf = malloc(len + 1))) return NULL;
j = 0;
for(i = 0; i < len; i += 2){
while(strchr(" \n\r\t\f\v", str[i])){
i++;
}
if((mbuf[0] = str[i]) == '\0') break;
if((mbuf[1] = str[i+1]) == '\0') break;
mbuf[2] = '\0';
buf[j++] = (char)strtol(mbuf, NULL, 16);
}
buf[j] = '\0';
*sp = j;
return buf;
}
/* parse arguments of create command */
int runcreate(int argc, char **argv){
char *name;
int i, rv;
name = NULL;
for(i = 2; i < argc; i++){
if(!name && argv[i][0] == '-'){
usage();
} else if(!name){
name = argv[i];
} else {
usage();
}
}
if(!name) usage();
rv = docreate(name);
return rv;
}
/* parse arguments of store command */
int runstore(int argc, char **argv){
char *name, *key, *val, *kbuf, *vbuf;
int i, kx, vx, vf, ins, ksiz, vsiz, rv;
name = NULL;
kx = FALSE;
vx = FALSE;
vf = FALSE;
ins = FALSE;
key = NULL;
val = NULL;
for(i = 2; i < argc; i++){
if(!name && argv[i][0] == '-'){
if(!strcmp(argv[i], "-kx")){
kx = TRUE;
} else if(!strcmp(argv[i], "-vx")){
vx = TRUE;
} else if(!strcmp(argv[i], "-vf")){
vf = TRUE;
} else if(!strcmp(argv[i], "-insert")){
ins = TRUE;
} else {
usage();
}
} else if(!name){
name = argv[i];
} else if(!key){
key = argv[i];
} else if(!val){
val = argv[i];
} else {
usage();
}
}
if(!name || !key || !val) usage();
if(kx){
kbuf = hextoobj(key, &ksiz);
} else {
kbuf = cbmemdup(key, -1);
ksiz = strlen(kbuf);
}
if(vx){
vbuf = hextoobj(val, &vsiz);
} else if(vf){
vbuf = cbreadfile(val, &vsiz);
} else {
vbuf = cbmemdup(val, -1);
vsiz = strlen(vbuf);
}
if(kbuf && vbuf){
rv = dostore(name, kbuf, ksiz, vbuf, vsiz, ins);
} else {
if(vf){
fprintf(stderr, "%s: %s: cannot read\n", progname, val);
} else {
fprintf(stderr, "%s: out of memory\n", progname);
}
rv = 1;
}
free(kbuf);
free(vbuf);
return rv;
}
/* parse arguments of delete command */
int rundelete(int argc, char **argv){
char *name, *key, *kbuf;
int i, kx, ksiz, rv;
name = NULL;
kx = FALSE;
key = NULL;
for(i = 2; i < argc; i++){
if(!name && argv[i][0] == '-'){
if(!strcmp(argv[i], "-kx")){
kx = TRUE;
} else {
usage();
}
} else if(!name){
name = argv[i];
} else if(!key){
key = argv[i];
} else {
usage();
}
}
if(!name || !key) usage();
if(kx){
kbuf = hextoobj(key, &ksiz);
} else {
kbuf = cbmemdup(key, -1);
ksiz = strlen(kbuf);
}
if(kbuf){
rv = dodelete(name, kbuf, ksiz);
} else {
fprintf(stderr, "%s: out of memory\n", progname);
rv = 1;
}
free(kbuf);
return rv;
}
/* parse arguments of fetch command */
int runfetch(int argc, char **argv){
char *name, *key, *kbuf;
int i, kx, ox, nb, ksiz, rv;
name = NULL;
kx = FALSE;
ox = FALSE;
nb = FALSE;
key = NULL;
for(i = 2; i < argc; i++){
if(!name && argv[i][0] == '-'){
if(!strcmp(argv[i], "-kx")){
kx = TRUE;
} else if(!strcmp(argv[i], "-ox")){
ox = TRUE;
} else if(!strcmp(argv[i], "-n")){
nb = TRUE;
} else {
usage();
}
} else if(!name){
name = argv[i];
} else if(!key){
key = argv[i];
} else {
usage();
}
}
if(!name || !key) usage();
if(kx){
kbuf = hextoobj(key, &ksiz);
} else {
kbuf = cbmemdup(key, -1);
ksiz = strlen(kbuf);
}
if(kbuf){
rv = dofetch(name, kbuf, ksiz, ox, nb);
} else {
fprintf(stderr, "%s: out of memory\n", progname);
rv = 1;
}
free(kbuf);
return rv;
}
/* parse arguments of list command */
int runlist(int argc, char **argv){
char *name;
int i, ox, rv;
name = NULL;
ox = FALSE;
for(i = 2; i < argc; i++){
if(!name && argv[i][0] == '-'){
if(!strcmp(argv[i], "-ox")){
ox = TRUE;
} else {
usage();
}
} else if(!name){
name = argv[i];
} else {
usage();
}
}
if(!name) usage();
rv = dolist(name, ox);
return rv;
}
/* print an error message */
void pmyerror(const char *name, const char *msg){
fprintf(stderr, "%s: %s: %s\n", progname, name, msg);
}
/* print an object */
void printobj(const char *obj, int size){
int i;
for(i = 0; i < size; i++){
putchar(obj[i]);
}
}
/* print an object as a hexadecimal string */
void printobjhex(const char *obj, int size){
int i;
for(i = 0; i < size; i++){
printf("%s%02X", i > 0 ? " " : "", ((const unsigned char *)obj)[i]);
}
}
/* perform create command */
int docreate(char *name){
DBM *db;
if(!(db = dbm_open(name, O_RDWR | O_CREAT | O_TRUNC, 00644))){
pmyerror(name, "dbm_open failed");
return 1;
}
dbm_close(db);
return 0;
}
/* perform store command */
int dostore(char *name, const char *kbuf, int ksiz, const char *vbuf, int vsiz, int ins){
DBM *db;
datum key, content;
int rv;
if(!(db = dbm_open(name, O_RDWR, 00644))){
pmyerror(name, "dbm_open failed");
return 1;
}
key.dptr = (char *)kbuf;
key.dsize = ksiz;
content.dptr = (char *)vbuf;
content.dsize = vsiz;
switch(dbm_store(db, key, content, ins ? DBM_INSERT : DBM_REPLACE)){
case 0:
rv = 0;
break;
case 1:
pmyerror(name, "dbm_store failed by insert");
rv = 1;
break;
default:
pmyerror(name, "dbm_store failed");
rv = 1;
break;
}
dbm_close(db);
return rv;
}
/* perform delete command */
int dodelete(char *name, const char *kbuf, int ksiz){
DBM *db;
datum key;
int rv;
if(!(db = dbm_open((char *)name, O_RDWR, 00644))){
pmyerror(name, "dbm_open failed");
return 1;
}
key.dptr = (char *)kbuf;
key.dsize = ksiz;
if(dbm_delete(db, key) == 0){
rv = 0;
} else {
pmyerror(name, "dbm_delete failed");
rv = 1;
}
dbm_close(db);
return rv;
}
/* perform fetch command */
int dofetch(char *name, const char *kbuf, int ksiz, int ox, int nb){
DBM *db;
datum key, content;
int rv;
if(!(db = dbm_open((char *)name, O_RDONLY, 00644))){
pmyerror(name, "dbm_open failed");
return 1;
}
key.dptr = (char *)kbuf;
key.dsize = ksiz;
content = dbm_fetch(db, key);
if(content.dptr){
if(ox){
printobjhex(content.dptr, content.dsize);
} else {
printobj(content.dptr, content.dsize);
}
if(!nb) putchar('\n');
rv = 0;
} else {
pmyerror(name, "dbm_fetch failed");
rv = 1;
}
dbm_close(db);
return rv;
}
/* perform list command */
int dolist(char *name, int ox){
DBM *db;
datum key, val;
if(!(db = dbm_open((char *)name, O_RDONLY, 00644))){
pmyerror(name, "dbm_open failed");
return 1;
}
for(key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db)){
val = dbm_fetch(db, key);
if(!val.dptr) break;
if(ox){
printobjhex(key.dptr, key.dsize);
putchar('\t');
printobjhex(val.dptr, val.dsize);
} else {
printobj(key.dptr, key.dsize);
putchar('\t');
printobj(val.dptr, val.dsize);
}
putchar('\n');
}
dbm_close(db);
return 0;
}
/* END OF FILE */