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