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