|
1 /** |
|
2 * $Id: dbtool.c 51 2008-01-10 00:19:39Z mbroeker $ |
|
3 * $URL: http://localhost/svn/c/mcbot/trunk/src/dbtool.c $ |
|
4 * |
|
5 */ |
|
6 |
|
7 #include <stdio.h> |
|
8 #include <stdlib.h> |
|
9 #include <getopt.h> |
|
10 |
|
11 #include <database.h> |
|
12 |
|
13 #ifndef FILE_NAME |
|
14 #define FILE_NAME "/var/lib/nobody/data/mcbot.dat" |
|
15 #endif |
|
16 |
|
17 void help (char *prgname) |
|
18 { |
|
19 printf ("Usage: %s [[-a|-r] -k <key> -v <value>]\n", prgname); |
|
20 printf (" %s [-d <key>]\n", prgname); |
|
21 printf (" %s [-f <file>]\n", prgname); |
|
22 printf (" %s [-s <key>]\n", prgname); |
|
23 printf (" %s [-h|-?]\n\n", prgname); |
|
24 printf ("Administration tool for adding/replacing, deleting, ... of DataBase Entries.\n\n"); |
|
25 printf ("Options:\n"); |
|
26 printf (" -a\t\tadds a new key <key> with value <value> to the db\n"); |
|
27 printf (" -r\t\treplaces a key <key> with value <value> in the db\n"); |
|
28 printf (" -d\t\tdeletes a <key> from the db\n"); |
|
29 printf (" -f\t\tsets the db file\n"); |
|
30 printf (" -l\t\tlists the db entries\n"); |
|
31 printf (" -s\t\tsearches the db entry <key>\n"); |
|
32 printf (" -V\t\treorganizes the db\n"); |
|
33 printf (" -h\t\tprints this help\n"); |
|
34 printf (" -?\t\tprints this help\n\n"); |
|
35 printf ("Report bugs to mbroeker@largo.homelinux.org\n"); |
|
36 exit (0); |
|
37 } |
|
38 |
|
39 #define ADD 1 |
|
40 #define REPLACE 2 |
|
41 #define DELETE 3 |
|
42 #define SEARCH 4 |
|
43 #define LIST 5 |
|
44 #define VACCUUM 6 |
|
45 |
|
46 int main (int argc, char **argv) |
|
47 { |
|
48 int i; |
|
49 int mode = 0; |
|
50 char *key = NULL; |
|
51 char *value = NULL; |
|
52 char *file_name = NULL; |
|
53 |
|
54 while ((i = getopt (argc, argv, "arlVh?d:f:k:s:v:")) > 0) { |
|
55 switch (i) { |
|
56 case '?': |
|
57 case 'h': |
|
58 help (argv[0]); |
|
59 break; |
|
60 case 'a': |
|
61 mode = ADD; |
|
62 break; |
|
63 case 'r': |
|
64 mode = REPLACE; |
|
65 break; |
|
66 case 'd': |
|
67 mode = DELETE; |
|
68 key = optarg; |
|
69 break; |
|
70 case 'f': |
|
71 file_name = optarg; |
|
72 break; |
|
73 case 'k': |
|
74 key = optarg; |
|
75 break; |
|
76 case 's': |
|
77 key = optarg; |
|
78 mode = SEARCH; |
|
79 break; |
|
80 case 'v': |
|
81 value = optarg; |
|
82 break; |
|
83 case 'l': |
|
84 mode = LIST; |
|
85 break; |
|
86 case 'V': |
|
87 mode = VACCUUM; |
|
88 break; |
|
89 default: |
|
90 printf ("Unknown Option %c\n", i); |
|
91 } |
|
92 } |
|
93 |
|
94 for (i = optind; i < argc; i++) { |
|
95 printf ("Unknown Parameter: %s\n", argv[i]); |
|
96 } |
|
97 |
|
98 if (!file_name) |
|
99 file_name = FILE_NAME; |
|
100 |
|
101 switch (mode) { |
|
102 case ADD: |
|
103 if ((key != NULL) && (value != NULL)) |
|
104 printf ("%s\n", db_insert (file_name, key, value, 0)); |
|
105 else |
|
106 help (argv[0]); |
|
107 break; |
|
108 case REPLACE: |
|
109 if ((key != NULL) && (value != NULL)) |
|
110 printf ("%s\n", db_insert (file_name, key, value, 1)); |
|
111 else |
|
112 help (argv[0]); |
|
113 break; |
|
114 case DELETE: |
|
115 if (key != NULL) |
|
116 printf ("%s\n", db_remove (file_name, key)); |
|
117 else |
|
118 help (argv[0]); |
|
119 break; |
|
120 case SEARCH: |
|
121 if (key != NULL) |
|
122 printf ("%s\n", db_lookup (file_name, key)); |
|
123 else |
|
124 help (argv[0]); |
|
125 break; |
|
126 case LIST: |
|
127 printf ("%s\n", db_list (file_name)); |
|
128 break; |
|
129 case VACCUUM: |
|
130 printf ("%s\n", db_vaccuum (file_name)); |
|
131 break; |
|
132 default: |
|
133 printf ("Nothing to do: Try %s -h\n", argv[0]); |
|
134 } |
|
135 |
|
136 return EXIT_SUCCESS; |
|
137 } |