Commit | Line | Data |
---|---|---|
463ee0b2 LW |
1 | #include <stdio.h> |
2 | #include <sys/file.h> | |
3 | #ifdef SDBM | |
17f28c40 | 4 | #include "EXTERN.h" |
463ee0b2 LW |
5 | #include "sdbm.h" |
6 | #else | |
7 | #include <ndbm.h> | |
8 | #endif | |
9 | #include <string.h> | |
10 | ||
11 | #ifdef BSD42 | |
12 | #define strchr index | |
13 | #endif | |
14 | ||
15 | extern int getopt(); | |
16 | extern char *strchr(); | |
17 | extern void oops(); | |
18 | ||
19 | char *progname; | |
20 | ||
21 | static int rflag; | |
22 | static char *usage = "%s [-R] cat | look |... dbmname"; | |
23 | ||
24 | #define DERROR 0 | |
25 | #define DLOOK 1 | |
26 | #define DINSERT 2 | |
27 | #define DDELETE 3 | |
28 | #define DCAT 4 | |
29 | #define DBUILD 5 | |
30 | #define DPRESS 6 | |
31 | #define DCREAT 7 | |
32 | ||
33 | #define LINEMAX 8192 | |
34 | ||
35 | typedef struct { | |
36 | char *sname; | |
37 | int scode; | |
38 | int flags; | |
39 | } cmd; | |
40 | ||
41 | static cmd cmds[] = { | |
42 | ||
43 | "fetch", DLOOK, O_RDONLY, | |
44 | "get", DLOOK, O_RDONLY, | |
45 | "look", DLOOK, O_RDONLY, | |
46 | "add", DINSERT, O_RDWR, | |
47 | "insert", DINSERT, O_RDWR, | |
48 | "store", DINSERT, O_RDWR, | |
49 | "delete", DDELETE, O_RDWR, | |
50 | "remove", DDELETE, O_RDWR, | |
51 | "dump", DCAT, O_RDONLY, | |
52 | "list", DCAT, O_RDONLY, | |
53 | "cat", DCAT, O_RDONLY, | |
54 | "creat", DCREAT, O_RDWR | O_CREAT | O_TRUNC, | |
55 | "new", DCREAT, O_RDWR | O_CREAT | O_TRUNC, | |
56 | "build", DBUILD, O_RDWR | O_CREAT, | |
57 | "squash", DPRESS, O_RDWR, | |
58 | "compact", DPRESS, O_RDWR, | |
59 | "compress", DPRESS, O_RDWR | |
60 | }; | |
61 | ||
62 | #define CTABSIZ (sizeof (cmds)/sizeof (cmd)) | |
63 | ||
64 | static cmd *parse(); | |
65 | static void badk(), doit(), prdatum(); | |
66 | ||
67 | int | |
68 | main(argc, argv) | |
69 | int argc; | |
70 | char *argv[]; | |
71 | { | |
72 | int c; | |
73 | register cmd *act; | |
74 | extern int optind; | |
75 | extern char *optarg; | |
76 | ||
77 | progname = argv[0]; | |
78 | ||
79 | while ((c = getopt(argc, argv, "R")) != EOF) | |
80 | switch (c) { | |
81 | case 'R': /* raw processing */ | |
82 | rflag++; | |
83 | break; | |
84 | ||
85 | default: | |
86 | oops("usage: %s", usage); | |
87 | break; | |
88 | } | |
89 | ||
90 | if ((argc -= optind) < 2) | |
91 | oops("usage: %s", usage); | |
92 | ||
93 | if ((act = parse(argv[optind])) == NULL) | |
94 | badk(argv[optind]); | |
95 | optind++; | |
96 | doit(act, argv[optind]); | |
97 | return 0; | |
98 | } | |
99 | ||
100 | static void | |
101 | doit(act, file) | |
102 | register cmd *act; | |
103 | char *file; | |
104 | { | |
105 | datum key; | |
106 | datum val; | |
107 | register DBM *db; | |
108 | register char *op; | |
109 | register int n; | |
110 | char *line; | |
111 | #ifdef TIME | |
112 | long start; | |
113 | extern long time(); | |
114 | #endif | |
115 | ||
116 | if ((db = dbm_open(file, act->flags, 0644)) == NULL) | |
117 | oops("cannot open: %s", file); | |
118 | ||
119 | if ((line = (char *) malloc(LINEMAX)) == NULL) | |
120 | oops("%s: cannot get memory", "line alloc"); | |
121 | ||
122 | switch (act->scode) { | |
123 | ||
124 | case DLOOK: | |
125 | while (fgets(line, LINEMAX, stdin) != NULL) { | |
126 | n = strlen(line) - 1; | |
127 | line[n] = 0; | |
128 | key.dptr = line; | |
129 | key.dsize = n; | |
130 | val = dbm_fetch(db, key); | |
131 | if (val.dptr != NULL) { | |
132 | prdatum(stdout, val); | |
133 | putchar('\n'); | |
134 | continue; | |
135 | } | |
136 | prdatum(stderr, key); | |
137 | fprintf(stderr, ": not found.\n"); | |
138 | } | |
139 | break; | |
140 | case DINSERT: | |
141 | break; | |
142 | case DDELETE: | |
143 | while (fgets(line, LINEMAX, stdin) != NULL) { | |
144 | n = strlen(line) - 1; | |
145 | line[n] = 0; | |
146 | key.dptr = line; | |
147 | key.dsize = n; | |
148 | if (dbm_delete(db, key) == -1) { | |
149 | prdatum(stderr, key); | |
150 | fprintf(stderr, ": not found.\n"); | |
151 | } | |
152 | } | |
153 | break; | |
154 | case DCAT: | |
155 | for (key = dbm_firstkey(db); key.dptr != 0; | |
156 | key = dbm_nextkey(db)) { | |
157 | prdatum(stdout, key); | |
158 | putchar('\t'); | |
159 | prdatum(stdout, dbm_fetch(db, key)); | |
160 | putchar('\n'); | |
161 | } | |
162 | break; | |
163 | case DBUILD: | |
164 | #ifdef TIME | |
165 | start = time(0); | |
166 | #endif | |
167 | while (fgets(line, LINEMAX, stdin) != NULL) { | |
168 | n = strlen(line) - 1; | |
169 | line[n] = 0; | |
170 | key.dptr = line; | |
171 | if ((op = strchr(line, '\t')) != 0) { | |
172 | key.dsize = op - line; | |
173 | *op++ = 0; | |
174 | val.dptr = op; | |
175 | val.dsize = line + n - op; | |
176 | } | |
177 | else | |
178 | oops("bad input; %s", line); | |
179 | ||
180 | if (dbm_store(db, key, val, DBM_REPLACE) < 0) { | |
181 | prdatum(stderr, key); | |
182 | fprintf(stderr, ": "); | |
183 | oops("store: %s", "failed"); | |
184 | } | |
185 | } | |
186 | #ifdef TIME | |
187 | printf("done: %d seconds.\n", time(0) - start); | |
188 | #endif | |
189 | break; | |
190 | case DPRESS: | |
191 | break; | |
192 | case DCREAT: | |
193 | break; | |
194 | } | |
195 | ||
196 | dbm_close(db); | |
197 | } | |
198 | ||
199 | static void | |
200 | badk(word) | |
201 | char *word; | |
202 | { | |
203 | register int i; | |
204 | ||
205 | if (progname) | |
206 | fprintf(stderr, "%s: ", progname); | |
207 | fprintf(stderr, "bad keywd %s. use one of\n", word); | |
208 | for (i = 0; i < (int)CTABSIZ; i++) | |
209 | fprintf(stderr, "%-8s%c", cmds[i].sname, | |
210 | ((i + 1) % 6 == 0) ? '\n' : ' '); | |
211 | fprintf(stderr, "\n"); | |
212 | exit(1); | |
213 | /*NOTREACHED*/ | |
214 | } | |
215 | ||
216 | static cmd * | |
217 | parse(str) | |
218 | register char *str; | |
219 | { | |
220 | register int i = CTABSIZ; | |
221 | register cmd *p; | |
222 | ||
223 | for (p = cmds; i--; p++) | |
224 | if (strcmp(p->sname, str) == 0) | |
225 | return p; | |
226 | return NULL; | |
227 | } | |
228 | ||
229 | static void | |
230 | prdatum(stream, d) | |
231 | FILE *stream; | |
232 | datum d; | |
233 | { | |
234 | register int c; | |
235 | register char *p = d.dptr; | |
236 | register int n = d.dsize; | |
237 | ||
238 | while (n--) { | |
239 | c = *p++ & 0377; | |
240 | if (c & 0200) { | |
241 | fprintf(stream, "M-"); | |
242 | c &= 0177; | |
243 | } | |
244 | if (c == 0177 || c < ' ') | |
245 | fprintf(stream, "^%c", (c == 0177) ? '?' : c + '@'); | |
246 | else | |
247 | putc(c, stream); | |
248 | } | |
249 | } | |
250 | ||
251 |