source: vendor/current/lib/tdb/tools/tdbtool.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

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