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