compiler: use everywhere.
[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 <stdlib.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <stdarg.h>
34
35 static int do_command(void);
36 const char *cmdname;
37 char *arg1, *arg2;
38 size_t arg1len, arg2len;
39 int bIterate = 0;
40 char *line;
41 TDB_DATA iterate_kbuf;
42 char cmdline[1024];
43 static int disable_mmap;
44
45 enum commands {
46         CMD_CREATE_TDB,
47         CMD_OPEN_TDB,
48         CMD_TRANSACTION_START,
49         CMD_TRANSACTION_COMMIT,
50         CMD_TRANSACTION_CANCEL,
51         CMD_ERASE,
52         CMD_DUMP,
53         CMD_INSERT,
54         CMD_MOVE,
55         CMD_STORE,
56         CMD_SHOW,
57         CMD_KEYS,
58         CMD_HEXKEYS,
59         CMD_DELETE,
60         CMD_LIST_HASH_FREE,
61         CMD_LIST_FREE,
62         CMD_INFO,
63         CMD_MMAP,
64         CMD_SPEED,
65         CMD_FIRST,
66         CMD_NEXT,
67         CMD_SYSTEM,
68         CMD_CHECK,
69         CMD_QUIT,
70         CMD_HELP
71 };
72
73 typedef struct {
74         const char *name;
75         enum commands cmd;
76 } COMMAND_TABLE;
77
78 COMMAND_TABLE cmd_table[] = {
79         {"create",      CMD_CREATE_TDB},
80         {"open",        CMD_OPEN_TDB},
81         {"transaction_start",   CMD_TRANSACTION_START},
82         {"transaction_commit",  CMD_TRANSACTION_COMMIT},
83         {"transaction_cancel",  CMD_TRANSACTION_CANCEL},
84         {"erase",       CMD_ERASE},
85         {"dump",        CMD_DUMP},
86         {"insert",      CMD_INSERT},
87         {"move",        CMD_MOVE},
88         {"store",       CMD_STORE},
89         {"show",        CMD_SHOW},
90         {"keys",        CMD_KEYS},
91         {"hexkeys",     CMD_HEXKEYS},
92         {"delete",      CMD_DELETE},
93         {"list",        CMD_LIST_HASH_FREE},
94         {"free",        CMD_LIST_FREE},
95         {"info",        CMD_INFO},
96         {"speed",       CMD_SPEED},
97         {"mmap",        CMD_MMAP},
98         {"first",       CMD_FIRST},
99         {"1",           CMD_FIRST},
100         {"next",        CMD_NEXT},
101         {"n",           CMD_NEXT},
102         {"check",       CMD_CHECK},
103         {"quit",        CMD_QUIT},
104         {"q",           CMD_QUIT},
105         {"!",           CMD_SYSTEM},
106         {NULL,          CMD_HELP}
107 };
108
109 struct timeval tp1,tp2;
110
111 static void _start_timer(void)
112 {
113         gettimeofday(&tp1,NULL);
114 }
115
116 static double _end_timer(void)
117 {
118         gettimeofday(&tp2,NULL);
119         return((tp2.tv_sec - tp1.tv_sec) + 
120                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
121 }
122
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 "  openjh    dbname     : open an existing database (jenkins hash)\n"
193 "  transaction_start    : start a transaction\n"
194 "  transaction_commit   : commit a transaction\n"
195 "  transaction_cancel   : cancel a transaction\n"
196 "  erase                : erase the database\n"
197 "  dump                 : dump the database as strings\n"
198 "  keys                 : dump the database keys as strings\n"
199 "  hexkeys              : dump the database keys as hex values\n"
200 "  info                 : print summary info about the database\n"
201 "  insert    key  data  : insert a record\n"
202 "  move      key  file  : move a record to a destination tdb\n"
203 "  store     key  data  : store a record (replace)\n"
204 "  show      key        : show a record by key\n"
205 "  delete    key        : delete a record by key\n"
206 "  list                 : print the database hash table and freelist\n"
207 "  free                 : print the database freelist\n"
208 "  check                : check the integrity of an opened 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) == -1) {
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) == -1) {
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 ) == -1 ) {
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, TDB_SUMMARY_HISTOGRAMS);
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         unsigned timelimit = tlimit?atoi(tlimit):0;
428         double t;
429         int ops;
430         if (timelimit == 0) timelimit = 5;
431
432         ops = 0;
433         printf("Testing store speed for %u seconds\n", timelimit);
434         _start_timer();
435         do {
436                 long int r = random();
437                 TDB_DATA key, dbuf;
438                 key.dptr = (unsigned char *)"store test";
439                 key.dsize = strlen((char *)key.dptr);
440                 dbuf.dptr = (unsigned char *)&r;
441                 dbuf.dsize = sizeof(r);
442                 tdb_store(tdb, key, dbuf, TDB_REPLACE);
443                 t = _end_timer();
444                 ops++;
445         } while (t < timelimit);
446         printf("%10.3f ops/sec\n", ops/t);
447
448         ops = 0;
449         printf("Testing fetch speed for %u seconds\n", timelimit);
450         _start_timer();
451         do {
452                 long int r = random();
453                 TDB_DATA key, dbuf;
454                 key.dptr = (unsigned char *)"store test";
455                 key.dsize = strlen((char *)key.dptr);
456                 dbuf.dptr = (unsigned char *)&r;
457                 dbuf.dsize = sizeof(r);
458                 tdb_fetch(tdb, key);
459                 t = _end_timer();
460                 ops++;
461         } while (t < timelimit);
462         printf("%10.3f ops/sec\n", ops/t);
463
464         ops = 0;
465         printf("Testing transaction speed for %u seconds\n", timelimit);
466         _start_timer();
467         do {
468                 long int r = random();
469                 TDB_DATA key, dbuf;
470                 key.dptr = (unsigned char *)"transaction test";
471                 key.dsize = strlen((char *)key.dptr);
472                 dbuf.dptr = (unsigned char *)&r;
473                 dbuf.dsize = sizeof(r);
474                 tdb_transaction_start(tdb);
475                 tdb_store(tdb, key, dbuf, TDB_REPLACE);
476                 tdb_transaction_commit(tdb);
477                 t = _end_timer();
478                 ops++;
479         } while (t < timelimit);
480         printf("%10.3f ops/sec\n", ops/t);
481
482         ops = 0;
483         printf("Testing traverse speed for %u seconds\n", timelimit);
484         _start_timer();
485         do {
486                 tdb_traverse(tdb, traverse_fn, NULL);
487                 t = _end_timer();
488                 ops++;
489         } while (t < timelimit);
490         printf("%10.3f ops/sec\n", ops/t);
491 }
492
493 static void toggle_mmap(void)
494 {
495         disable_mmap = !disable_mmap;
496         if (disable_mmap) {
497                 printf("mmap is disabled\n");
498         } else {
499                 printf("mmap is enabled\n");
500         }
501 }
502
503 static char *tdb_getline(const char *prompt)
504 {
505         static char thisline[1024];
506         char *p;
507         fputs(prompt, stdout);
508         thisline[0] = 0;
509         p = fgets(thisline, sizeof(thisline)-1, stdin);
510         if (p) p = strchr(p, '\n');
511         if (p) *p = 0;
512         return p?thisline:NULL;
513 }
514
515 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
516                      void *state)
517 {
518     return tdb_delete(the_tdb, key);
519 }
520
521 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
522 {
523         TDB_DATA dbuf;
524         *pkey = tdb_firstkey(the_tdb);
525         
526         dbuf = tdb_fetch(the_tdb, *pkey);
527         if (!dbuf.dptr) terror("fetch failed");
528         else {
529                 print_rec(the_tdb, *pkey, dbuf, NULL);
530         }
531 }
532
533 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
534 {
535         TDB_DATA dbuf;
536         *pkey = tdb_nextkey(the_tdb, *pkey);
537         
538         dbuf = tdb_fetch(the_tdb, *pkey);
539         if (!dbuf.dptr) 
540                 terror("fetch failed");
541         else
542                 print_rec(the_tdb, *pkey, dbuf, NULL);
543 }
544
545 static void check_db(TDB_CONTEXT *the_tdb)
546 {
547         if (!the_tdb) {
548                 printf("Error: No database opened!\n");
549         } else {
550                 if (tdb_check(the_tdb, NULL, NULL) == -1)
551                         printf("Integrity check for the opened database failed.\n");
552                 else
553                         printf("Database integrity is OK.\n");
554         }
555 }
556
557 static int do_command(void)
558 {
559         COMMAND_TABLE *ctp = cmd_table;
560         enum commands mycmd = CMD_HELP;
561         int cmd_len;
562
563         if (cmdname && strlen(cmdname) == 0) {
564                 mycmd = CMD_NEXT;
565         } else {
566                 while (ctp->name) {
567                         cmd_len = strlen(ctp->name);
568                         if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
569                                 mycmd = ctp->cmd;
570                                 break;
571                         }
572                         ctp++;
573                 }
574         }
575
576         switch (mycmd) {
577         case CMD_CREATE_TDB:
578                 bIterate = 0;
579                 create_tdb(arg1);
580                 return 0;
581         case CMD_OPEN_TDB:
582                 bIterate = 0;
583                 open_tdb(arg1);
584                 return 0;
585         case CMD_SYSTEM:
586                 /* Shell command */
587                 if (system(arg1) == -1) {
588                         terror("system() call failed\n");
589                 }
590                 return 0;
591         case CMD_QUIT:
592                 return 1;
593         default:
594                 /* all the rest require a open database */
595                 if (!tdb) {
596                         bIterate = 0;
597                         terror("database not open");
598                         help();
599                         return 0;
600                 }
601                 switch (mycmd) {
602                 case CMD_TRANSACTION_START:
603                         bIterate = 0;
604                         tdb_transaction_start(tdb);
605                         return 0;
606                 case CMD_TRANSACTION_COMMIT:
607                         bIterate = 0;
608                         tdb_transaction_commit(tdb);
609                         return 0;
610                 case CMD_TRANSACTION_CANCEL:
611                         bIterate = 0;
612                         tdb_transaction_cancel(tdb);
613                         return 0;
614                 case CMD_ERASE:
615                         bIterate = 0;
616                         tdb_traverse(tdb, do_delete_fn, NULL);
617                         return 0;
618                 case CMD_DUMP:
619                         bIterate = 0;
620                         tdb_traverse(tdb, print_rec, NULL);
621                         return 0;
622                 case CMD_INSERT:
623                         bIterate = 0;
624                         insert_tdb(arg1, arg1len,arg2,arg2len);
625                         return 0;
626                 case CMD_MOVE:
627                         bIterate = 0;
628                         move_rec(arg1,arg1len,arg2);
629                         return 0;
630                 case CMD_STORE:
631                         bIterate = 0;
632                         store_tdb(arg1,arg1len,arg2,arg2len);
633                         return 0;
634                 case CMD_SHOW:
635                         bIterate = 0;
636                         show_tdb(arg1, arg1len);
637                         return 0;
638                 case CMD_KEYS:
639                         tdb_traverse(tdb, print_key, NULL);
640                         return 0;
641                 case CMD_HEXKEYS:
642                         tdb_traverse(tdb, print_hexkey, NULL);
643                         return 0;
644                 case CMD_DELETE:
645                         bIterate = 0;
646                         delete_tdb(arg1,arg1len);
647                         return 0;
648                 case CMD_LIST_HASH_FREE:
649                         tdb_dump_all(tdb);
650                         return 0;
651                 case CMD_LIST_FREE:
652                         tdb_printfreelist(tdb);
653                         return 0;
654                 case CMD_INFO:
655                         info_tdb();
656                         return 0;
657                 case CMD_SPEED:
658                         speed_tdb(arg1);
659                         return 0;
660                 case CMD_MMAP:
661                         toggle_mmap();
662                         return 0;
663                 case CMD_FIRST:
664                         bIterate = 1;
665                         first_record(tdb, &iterate_kbuf);
666                         return 0;
667                 case CMD_NEXT:
668                         if (bIterate)
669                                 next_record(tdb, &iterate_kbuf);
670                         return 0;
671                 case CMD_CHECK:
672                         check_db(tdb);
673                         return 0;
674                 case CMD_HELP:
675                         help();
676                         return 0;
677                 case CMD_CREATE_TDB:
678                 case CMD_OPEN_TDB:
679                 case CMD_SYSTEM:
680                 case CMD_QUIT:
681                         /*
682                          * unhandled commands.  cases included here to avoid compiler
683                          * warnings.
684                          */
685                         return 0;
686                 }
687         }
688
689         return 0;
690 }
691
692 static char *convert_string(char *instring, size_t *sizep)
693 {
694         size_t length = 0;
695         char *outp, *inp;
696         char temp[3];
697
698         outp = inp = instring;
699
700         while (*inp) {
701                 if (*inp == '\\') {
702                         inp++;
703                         if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
704                                 temp[0] = *inp++;
705                                 temp[1] = '\0';
706                                 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
707                                         temp[1] = *inp++;
708                                         temp[2] = '\0';
709                                 }
710                                 *outp++ = (char)strtol((const char *)temp,NULL,16);
711                         } else {
712                                 *outp++ = *inp++;
713                         }
714                 } else {
715                         *outp++ = *inp++;
716                 }
717                 length++;
718         }
719         *sizep = length;
720         return instring;
721 }
722
723 int main(int argc, char *argv[])
724 {
725         cmdname = "";
726         arg1 = NULL;
727         arg1len = 0;
728         arg2 = NULL;
729         arg2len = 0;
730
731         if (argv[1]) {
732                 cmdname = "open";
733                 arg1 = argv[1];
734                 do_command();
735                 cmdname =  "";
736                 arg1 = NULL;
737         }
738
739         switch (argc) {
740         case 1:
741         case 2:
742                 /* Interactive mode */
743                 while ((cmdname = tdb_getline("tdb> "))) {
744                         arg2 = arg1 = NULL;
745                         if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
746                                 arg1++;
747                                 arg2 = arg1;
748                                 while (*arg2) {
749                                         if (*arg2 == ' ') {
750                                                 *arg2++ = '\0';
751                                                 break;
752                                         }
753                                         if ((*arg2++ == '\\') && (*arg2 == ' ')) {
754                                                 arg2++;
755                                         }
756                                 }
757                         }
758                         if (arg1) arg1 = convert_string(arg1,&arg1len);
759                         if (arg2) arg2 = convert_string(arg2,&arg2len);
760                         if (do_command()) break;
761                 }
762                 break;
763         case 5:
764                 arg2 = convert_string(argv[4],&arg2len);
765         case 4:
766                 arg1 = convert_string(argv[3],&arg1len);
767         case 3:
768                 cmdname = argv[2];
769         default:
770                 do_command();
771                 break;
772         }
773
774         if (tdb) tdb_close(tdb);
775
776         return 0;
777 }