]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tools/tdbtool.c
3ae7866d2c63c6137bd7cb1f59ac64bc35cbd761
[ccan] / ccan / tdb2 / tools / tdbtool.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba database functions
4    Copyright (C) Andrew Tridgell              1999-2000
5    Copyright (C) Paul `Rusty' Russell              2000
6    Copyright (C) Jeremy Allison                    2000
7    Copyright (C) Andrew Esh                        2001
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <ccan/tdb2/tdb2.h>
24 #include <ccan/hash/hash.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdarg.h>
35
36 static int do_command(void);
37 const char *cmdname;
38 char *arg1, *arg2;
39 size_t arg1len, arg2len;
40 int bIterate = 0;
41 char *line;
42 TDB_DATA iterate_kbuf;
43 char cmdline[1024];
44 static int disable_mmap;
45
46 enum commands {
47         CMD_CREATE_TDB,
48         CMD_OPEN_TDB,
49         CMD_TRANSACTION_START,
50         CMD_TRANSACTION_COMMIT,
51         CMD_TRANSACTION_CANCEL,
52         CMD_ERASE,
53         CMD_DUMP,
54         CMD_INSERT,
55         CMD_MOVE,
56         CMD_STORE,
57         CMD_SHOW,
58         CMD_KEYS,
59         CMD_HEXKEYS,
60         CMD_DELETE,
61 #if 0
62         CMD_LIST_HASH_FREE,
63         CMD_LIST_FREE,
64 #endif
65         CMD_INFO,
66         CMD_MMAP,
67         CMD_SPEED,
68         CMD_FIRST,
69         CMD_NEXT,
70         CMD_SYSTEM,
71         CMD_CHECK,
72         CMD_QUIT,
73         CMD_HELP
74 };
75
76 typedef struct {
77         const char *name;
78         enum commands cmd;
79 } COMMAND_TABLE;
80
81 COMMAND_TABLE cmd_table[] = {
82         {"create",      CMD_CREATE_TDB},
83         {"open",        CMD_OPEN_TDB},
84 #if 0
85         {"transaction_start",   CMD_TRANSACTION_START},
86         {"transaction_commit",  CMD_TRANSACTION_COMMIT},
87         {"transaction_cancel",  CMD_TRANSACTION_CANCEL},
88 #endif
89         {"erase",       CMD_ERASE},
90         {"dump",        CMD_DUMP},
91         {"insert",      CMD_INSERT},
92         {"move",        CMD_MOVE},
93         {"store",       CMD_STORE},
94         {"show",        CMD_SHOW},
95         {"keys",        CMD_KEYS},
96         {"hexkeys",     CMD_HEXKEYS},
97         {"delete",      CMD_DELETE},
98 #if 0
99         {"list",        CMD_LIST_HASH_FREE},
100         {"free",        CMD_LIST_FREE},
101 #endif
102         {"info",        CMD_INFO},
103         {"speed",       CMD_SPEED},
104         {"mmap",        CMD_MMAP},
105         {"first",       CMD_FIRST},
106         {"1",           CMD_FIRST},
107         {"next",        CMD_NEXT},
108         {"n",           CMD_NEXT},
109         {"check",       CMD_CHECK},
110         {"quit",        CMD_QUIT},
111         {"q",           CMD_QUIT},
112         {"!",           CMD_SYSTEM},
113         {NULL,          CMD_HELP}
114 };
115
116 struct timeval tp1,tp2;
117
118 static void _start_timer(void)
119 {
120         gettimeofday(&tp1,NULL);
121 }
122
123 static double _end_timer(void)
124 {
125         gettimeofday(&tp2,NULL);
126         return((tp2.tv_sec - tp1.tv_sec) + 
127                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
128 }
129
130 static void tdb_log(struct tdb_context *tdb, enum tdb_log_level level,
131                     void *priv, const char *message)
132 {
133         fputs(message, stderr);
134 }
135
136 /* a tdb tool for manipulating a tdb database */
137
138 static struct tdb_context *tdb;
139
140 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
141 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
142 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
143
144 static void print_asc(const char *buf,int len)
145 {
146         int i;
147
148         /* We're probably printing ASCII strings so don't try to display
149            the trailing NULL character. */
150
151         if (buf[len - 1] == 0)
152                 len--;
153
154         for (i=0;i<len;i++)
155                 printf("%c",isprint(buf[i])?buf[i]:'.');
156 }
157
158 static void print_data(const char *buf,int len)
159 {
160         int i=0;
161         if (len<=0) return;
162         printf("[%03X] ",i);
163         for (i=0;i<len;) {
164                 printf("%02X ",(int)((unsigned char)buf[i]));
165                 i++;
166                 if (i%8 == 0) printf(" ");
167                 if (i%16 == 0) {      
168                         print_asc(&buf[i-16],8); printf(" ");
169                         print_asc(&buf[i-8],8); printf("\n");
170                         if (i<len) printf("[%03X] ",i);
171                 }
172         }
173         if (i%16) {
174                 int n;
175                 
176                 n = 16 - (i%16);
177                 printf(" ");
178                 if (n>8) printf(" ");
179                 while (n--) printf("   ");
180                 
181                 n = i%16;
182                 if (n > 8) n = 8;
183                 print_asc(&buf[i-(i%16)],n); printf(" ");
184                 n = (i%16) - n;
185                 if (n>0) print_asc(&buf[i-n],n); 
186                 printf("\n");    
187         }
188 }
189
190 static void help(void)
191 {
192         printf("\n"
193 "tdbtool: \n"
194 "  create    dbname     : create a database\n"
195 "  open      dbname     : open an existing database\n"
196 "  openjh    dbname     : open an existing database (jenkins hash)\n"
197 "  transaction_start    : start a transaction\n"
198 "  transaction_commit   : commit a transaction\n"
199 "  transaction_cancel   : cancel a transaction\n"
200 "  erase                : erase the database\n"
201 "  dump                 : dump the database as strings\n"
202 "  keys                 : dump the database keys as strings\n"
203 "  hexkeys              : dump the database keys as hex values\n"
204 "  info                 : print summary info about the database\n"
205 "  insert    key  data  : insert a record\n"
206 "  move      key  file  : move a record to a destination tdb\n"
207 "  store     key  data  : store a record (replace)\n"
208 "  show      key        : show a record by key\n"
209 "  delete    key        : delete a record by key\n"
210 "  list                 : print the database hash table and freelist\n"
211 "  free                 : print the database freelist\n"
212 "  check                : check the integrity of an opened database\n"
213 "  speed                : perform speed tests on the database\n"
214 "  ! command            : execute system command\n"
215 "  1 | first            : print the first record\n"
216 "  n | next             : print the next record\n"
217 "  q | quit             : terminate\n"
218 "  \\n                   : repeat 'next' command\n"
219 "\n");
220 }
221
222 static void terror(enum TDB_ERROR err, const char *why)
223 {
224         if (err != TDB_SUCCESS)
225                 printf("%s:%s\n", tdb_errorstr(err), why);
226         else
227                 printf("%s\n", why);
228 }
229
230 static void create_tdb(const char *tdbname)
231 {
232         union tdb_attribute log_attr;
233         log_attr.base.attr = TDB_ATTRIBUTE_LOG;
234         log_attr.base.next = NULL;
235         log_attr.log.log_fn = tdb_log;
236
237         if (tdb) tdb_close(tdb);
238         tdb = tdb_open(tdbname, (disable_mmap?TDB_NOMMAP:0),
239                        O_RDWR | O_CREAT | O_TRUNC, 0600, &log_attr);
240         if (!tdb) {
241                 printf("Could not create %s: %s\n", tdbname, strerror(errno));
242         }
243 }
244
245 static uint64_t jenkins_hash(const void *key, size_t len, uint64_t seed,
246                              void *priv)
247 {
248         return hash_any(key, len, seed);
249 }
250
251 static void open_tdb(const char *tdbname)
252 {
253         union tdb_attribute log_attr;
254         log_attr.base.attr = TDB_ATTRIBUTE_LOG;
255         log_attr.base.next = NULL;
256         log_attr.log.log_fn = tdb_log;
257
258         if (tdb) tdb_close(tdb);
259         tdb = tdb_open(tdbname, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
260                        &log_attr);
261         if (!tdb) {
262                 printf("Could not open %s: %s\n", tdbname, strerror(errno));
263         }
264 }
265
266 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
267 {
268         TDB_DATA key, dbuf;
269         enum TDB_ERROR ecode;
270
271         if ((keyname == NULL) || (keylen == 0)) {
272                 terror(TDB_SUCCESS, "need key");
273                 return;
274         }
275
276         key.dptr = (unsigned char *)keyname;
277         key.dsize = keylen;
278         dbuf.dptr = (unsigned char *)data;
279         dbuf.dsize = datalen;
280
281         ecode = tdb_store(tdb, key, dbuf, TDB_INSERT);
282         if (ecode) {
283                 terror(ecode, "insert failed");
284         }
285 }
286
287 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
288 {
289         TDB_DATA key, dbuf;
290         enum TDB_ERROR ecode;
291
292         if ((keyname == NULL) || (keylen == 0)) {
293                 terror(TDB_SUCCESS, "need key");
294                 return;
295         }
296
297         if ((data == NULL) || (datalen == 0)) {
298                 terror(TDB_SUCCESS, "need data");
299                 return;
300         }
301
302         key.dptr = (unsigned char *)keyname;
303         key.dsize = keylen;
304         dbuf.dptr = (unsigned char *)data;
305         dbuf.dsize = datalen;
306
307         printf("Storing key:\n");
308         print_rec(tdb, key, dbuf, NULL);
309
310         ecode = tdb_store(tdb, key, dbuf, TDB_REPLACE);
311         if (ecode) {
312                 terror(ecode, "store failed");
313         }
314 }
315
316 static void show_tdb(char *keyname, size_t keylen)
317 {
318         TDB_DATA key, dbuf;
319         enum TDB_ERROR ecode;
320
321         if ((keyname == NULL) || (keylen == 0)) {
322                 terror(TDB_SUCCESS, "need key");
323                 return;
324         }
325
326         key.dptr = (unsigned char *)keyname;
327         key.dsize = keylen;
328
329         ecode = tdb_fetch(tdb, key, &dbuf);
330         if (ecode) {
331                 terror(ecode, "fetch failed");
332                 return;
333         }
334         
335         print_rec(tdb, key, dbuf, NULL);
336         
337         free( dbuf.dptr );
338 }
339
340 static void delete_tdb(char *keyname, size_t keylen)
341 {
342         TDB_DATA key;
343         enum TDB_ERROR ecode;
344
345         if ((keyname == NULL) || (keylen == 0)) {
346                 terror(TDB_SUCCESS, "need key");
347                 return;
348         }
349
350         key.dptr = (unsigned char *)keyname;
351         key.dsize = keylen;
352
353         ecode = tdb_delete(tdb, key);
354         if (ecode) {
355                 terror(ecode, "delete failed");
356         }
357 }
358
359 static void move_rec(char *keyname, size_t keylen, char* tdbname)
360 {
361         TDB_DATA key, dbuf;
362         struct tdb_context *dst_tdb;
363         enum TDB_ERROR ecode;
364
365         if ((keyname == NULL) || (keylen == 0)) {
366                 terror(TDB_SUCCESS, "need key");
367                 return;
368         }
369
370         if ( !tdbname ) {
371                 terror(TDB_SUCCESS, "need destination tdb name");
372                 return;
373         }
374
375         key.dptr = (unsigned char *)keyname;
376         key.dsize = keylen;
377
378         ecode = tdb_fetch(tdb, key, &dbuf);
379         if (ecode) {
380                 terror(ecode, "fetch failed");
381                 return;
382         }
383         
384         print_rec(tdb, key, dbuf, NULL);
385         
386         dst_tdb = tdb_open(tdbname, 0, O_RDWR, 0600, NULL);
387         if ( !dst_tdb ) {
388                 terror(TDB_SUCCESS, "unable to open destination tdb");
389                 return;
390         }
391         
392         ecode = tdb_store( dst_tdb, key, dbuf, TDB_REPLACE);
393         if (ecode)
394                 terror(ecode, "failed to move record");
395         else
396                 printf("record moved\n");
397         
398         tdb_close( dst_tdb );
399 }
400
401 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
402 {
403         printf("\nkey %d bytes\n", (int)key.dsize);
404         print_asc((const char *)key.dptr, key.dsize);
405         printf("\ndata %d bytes\n", (int)dbuf.dsize);
406         print_data((const char *)dbuf.dptr, dbuf.dsize);
407         return 0;
408 }
409
410 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
411 {
412         printf("key %d bytes: ", (int)key.dsize);
413         print_asc((const char *)key.dptr, key.dsize);
414         printf("\n");
415         return 0;
416 }
417
418 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
419 {
420         printf("key %d bytes\n", (int)key.dsize);
421         print_data((const char *)key.dptr, key.dsize);
422         printf("\n");
423         return 0;
424 }
425
426 static int total_bytes;
427
428 static int traverse_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
429 {
430         total_bytes += dbuf.dsize;
431         return 0;
432 }
433
434 static void info_tdb(void)
435 {
436         enum TDB_ERROR ecode;
437         char *summary;
438
439         ecode = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &summary);
440
441         if (ecode) {
442                 terror(ecode, "Getting summary");
443         } else {
444                 printf("%s", summary);
445                 free(summary);
446         }
447 }
448
449 static void speed_tdb(const char *tlimit)
450 {
451         unsigned timelimit = tlimit?atoi(tlimit):0;
452         double t;
453         int ops;
454         if (timelimit == 0) timelimit = 5;
455
456         ops = 0;
457         printf("Testing store speed for %u seconds\n", timelimit);
458         _start_timer();
459         do {
460                 long int r = random();
461                 TDB_DATA key, dbuf;
462                 key.dptr = (unsigned char *)"store test";
463                 key.dsize = strlen((char *)key.dptr);
464                 dbuf.dptr = (unsigned char *)&r;
465                 dbuf.dsize = sizeof(r);
466                 tdb_store(tdb, key, dbuf, TDB_REPLACE);
467                 t = _end_timer();
468                 ops++;
469         } while (t < timelimit);
470         printf("%10.3f ops/sec\n", ops/t);
471
472         ops = 0;
473         printf("Testing fetch speed for %u seconds\n", timelimit);
474         _start_timer();
475         do {
476                 long int r = random();
477                 TDB_DATA key, dbuf;
478                 key.dptr = (unsigned char *)"store test";
479                 key.dsize = strlen((char *)key.dptr);
480                 dbuf.dptr = (unsigned char *)&r;
481                 dbuf.dsize = sizeof(r);
482                 tdb_fetch(tdb, key, &dbuf);
483                 t = _end_timer();
484                 ops++;
485         } while (t < timelimit);
486         printf("%10.3f ops/sec\n", ops/t);
487
488         ops = 0;
489         printf("Testing transaction speed for %u seconds\n", timelimit);
490         _start_timer();
491         do {
492                 long int r = random();
493                 TDB_DATA key, dbuf;
494                 key.dptr = (unsigned char *)"transaction test";
495                 key.dsize = strlen((char *)key.dptr);
496                 dbuf.dptr = (unsigned char *)&r;
497                 dbuf.dsize = sizeof(r);
498                 tdb_transaction_start(tdb);
499                 tdb_store(tdb, key, dbuf, TDB_REPLACE);
500                 tdb_transaction_commit(tdb);
501                 t = _end_timer();
502                 ops++;
503         } while (t < timelimit);
504         printf("%10.3f ops/sec\n", ops/t);
505
506         ops = 0;
507         printf("Testing traverse speed for %u seconds\n", timelimit);
508         _start_timer();
509         do {
510                 tdb_traverse(tdb, traverse_fn, NULL);
511                 t = _end_timer();
512                 ops++;
513         } while (t < timelimit);
514         printf("%10.3f ops/sec\n", ops/t);
515 }
516
517 static void toggle_mmap(void)
518 {
519         disable_mmap = !disable_mmap;
520         if (disable_mmap) {
521                 printf("mmap is disabled\n");
522         } else {
523                 printf("mmap is enabled\n");
524         }
525 }
526
527 static char *tdb_getline(const char *prompt)
528 {
529         static char thisline[1024];
530         char *p;
531         fputs(prompt, stdout);
532         thisline[0] = 0;
533         p = fgets(thisline, sizeof(thisline)-1, stdin);
534         if (p) p = strchr(p, '\n');
535         if (p) *p = 0;
536         return p?thisline:NULL;
537 }
538
539 static int do_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
540                      void *state)
541 {
542     return tdb_delete(the_tdb, key);
543 }
544
545 static void first_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
546 {
547         TDB_DATA dbuf;
548         enum TDB_ERROR ecode;
549         ecode = tdb_firstkey(the_tdb, pkey);
550         if (!ecode)
551                 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
552         if (ecode) terror(ecode, "fetch failed");
553         else {
554                 print_rec(the_tdb, *pkey, dbuf, NULL);
555         }
556 }
557
558 static void next_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
559 {
560         TDB_DATA dbuf;
561         enum TDB_ERROR ecode;
562         ecode = tdb_nextkey(the_tdb, pkey);
563
564         if (!ecode)
565                 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
566         if (ecode) 
567                 terror(ecode, "fetch failed");
568         else
569                 print_rec(the_tdb, *pkey, dbuf, NULL);
570 }
571
572 static void check_db(struct tdb_context *the_tdb)
573 {
574         if (!the_tdb) {
575                 printf("Error: No database opened!\n");
576         } else {
577                 if (tdb_check(the_tdb, NULL, NULL) != 0)
578                         printf("Integrity check for the opened database failed.\n");
579                 else
580                         printf("Database integrity is OK.\n");
581         }
582 }
583
584 static int do_command(void)
585 {
586         COMMAND_TABLE *ctp = cmd_table;
587         enum commands mycmd = CMD_HELP;
588         int cmd_len;
589
590         if (cmdname && strlen(cmdname) == 0) {
591                 mycmd = CMD_NEXT;
592         } else {
593                 while (ctp->name) {
594                         cmd_len = strlen(ctp->name);
595                         if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
596                                 mycmd = ctp->cmd;
597                                 break;
598                         }
599                         ctp++;
600                 }
601         }
602
603         switch (mycmd) {
604         case CMD_CREATE_TDB:
605                 bIterate = 0;
606                 create_tdb(arg1);
607                 return 0;
608         case CMD_OPEN_TDB:
609                 bIterate = 0;
610                 open_tdb(arg1);
611                 return 0;
612         case CMD_SYSTEM:
613                 /* Shell command */
614                 if (system(arg1) == -1) {
615                         terror(TDB_SUCCESS, "system() call failed\n");
616                 }
617                 return 0;
618         case CMD_QUIT:
619                 return 1;
620         default:
621                 /* all the rest require a open database */
622                 if (!tdb) {
623                         bIterate = 0;
624                         terror(TDB_SUCCESS, "database not open");
625                         help();
626                         return 0;
627                 }
628                 switch (mycmd) {
629                 case CMD_TRANSACTION_START:
630                         bIterate = 0;
631                         tdb_transaction_start(tdb);
632                         return 0;
633                 case CMD_TRANSACTION_COMMIT:
634                         bIterate = 0;
635                         tdb_transaction_commit(tdb);
636                         return 0;
637                 case CMD_TRANSACTION_CANCEL:
638                         bIterate = 0;
639                         tdb_transaction_cancel(tdb);
640                         return 0;
641                 case CMD_ERASE:
642                         bIterate = 0;
643                         tdb_traverse(tdb, do_delete_fn, NULL);
644                         return 0;
645                 case CMD_DUMP:
646                         bIterate = 0;
647                         tdb_traverse(tdb, print_rec, NULL);
648                         return 0;
649                 case CMD_INSERT:
650                         bIterate = 0;
651                         insert_tdb(arg1, arg1len,arg2,arg2len);
652                         return 0;
653                 case CMD_MOVE:
654                         bIterate = 0;
655                         move_rec(arg1,arg1len,arg2);
656                         return 0;
657                 case CMD_STORE:
658                         bIterate = 0;
659                         store_tdb(arg1,arg1len,arg2,arg2len);
660                         return 0;
661                 case CMD_SHOW:
662                         bIterate = 0;
663                         show_tdb(arg1, arg1len);
664                         return 0;
665                 case CMD_KEYS:
666                         tdb_traverse(tdb, print_key, NULL);
667                         return 0;
668                 case CMD_HEXKEYS:
669                         tdb_traverse(tdb, print_hexkey, NULL);
670                         return 0;
671                 case CMD_DELETE:
672                         bIterate = 0;
673                         delete_tdb(arg1,arg1len);
674                         return 0;
675 #if 0
676                 case CMD_LIST_HASH_FREE:
677                         tdb_dump_all(tdb);
678                         return 0;
679                 case CMD_LIST_FREE:
680                         tdb_printfreelist(tdb);
681                         return 0;
682 #endif
683                 case CMD_INFO:
684                         info_tdb();
685                         return 0;
686                 case CMD_SPEED:
687                         speed_tdb(arg1);
688                         return 0;
689                 case CMD_MMAP:
690                         toggle_mmap();
691                         return 0;
692                 case CMD_FIRST:
693                         bIterate = 1;
694                         first_record(tdb, &iterate_kbuf);
695                         return 0;
696                 case CMD_NEXT:
697                         if (bIterate)
698                                 next_record(tdb, &iterate_kbuf);
699                         return 0;
700                 case CMD_CHECK:
701                         check_db(tdb);
702                         return 0;
703                 case CMD_HELP:
704                         help();
705                         return 0;
706                 case CMD_CREATE_TDB:
707                 case CMD_OPEN_TDB:
708                 case CMD_SYSTEM:
709                 case CMD_QUIT:
710                         /*
711                          * unhandled commands.  cases included here to avoid compiler
712                          * warnings.
713                          */
714                         return 0;
715                 }
716         }
717
718         return 0;
719 }
720
721 static char *convert_string(char *instring, size_t *sizep)
722 {
723         size_t length = 0;
724         char *outp, *inp;
725         char temp[3];
726
727         outp = inp = instring;
728
729         while (*inp) {
730                 if (*inp == '\\') {
731                         inp++;
732                         if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
733                                 temp[0] = *inp++;
734                                 temp[1] = '\0';
735                                 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
736                                         temp[1] = *inp++;
737                                         temp[2] = '\0';
738                                 }
739                                 *outp++ = (char)strtol((const char *)temp,NULL,16);
740                         } else {
741                                 *outp++ = *inp++;
742                         }
743                 } else {
744                         *outp++ = *inp++;
745                 }
746                 length++;
747         }
748         *sizep = length;
749         return instring;
750 }
751
752 int main(int argc, char *argv[])
753 {
754         cmdname = "";
755         arg1 = NULL;
756         arg1len = 0;
757         arg2 = NULL;
758         arg2len = 0;
759
760         if (argv[1]) {
761                 cmdname = "open";
762                 arg1 = argv[1];
763                 do_command();
764                 cmdname =  "";
765                 arg1 = NULL;
766         }
767
768         switch (argc) {
769         case 1:
770         case 2:
771                 /* Interactive mode */
772                 while ((cmdname = tdb_getline("tdb> "))) {
773                         arg2 = arg1 = NULL;
774                         if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
775                                 arg1++;
776                                 arg2 = arg1;
777                                 while (*arg2) {
778                                         if (*arg2 == ' ') {
779                                                 *arg2++ = '\0';
780                                                 break;
781                                         }
782                                         if ((*arg2++ == '\\') && (*arg2 == ' ')) {
783                                                 arg2++;
784                                         }
785                                 }
786                         }
787                         if (arg1) arg1 = convert_string(arg1,&arg1len);
788                         if (arg2) arg2 = convert_string(arg2,&arg2len);
789                         if (do_command()) break;
790                 }
791                 break;
792         case 5:
793                 arg2 = convert_string(argv[4],&arg2len);
794         case 4:
795                 arg1 = convert_string(argv[3],&arg1len);
796         case 3:
797                 cmdname = argv[2];
798         default:
799                 do_command();
800                 break;
801         }
802
803         if (tdb) tdb_close(tdb);
804
805         return 0;
806 }