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