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