-
Notifications
You must be signed in to change notification settings - Fork 103
/
memcat.c
317 lines (280 loc) · 8.7 KB
/
memcat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2010 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Example program using libcouchbase_mget.
*
* @author Trond Norbye
* @todo add documentation
*/
// @todo figure out what I need to include for win32 in the headers!
#include "config.h"
#include <getopt.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <libcouchbase/couchbase.h>
#ifdef WIN32
#define PRIu64 "llu"
static bool isatty(int a) {
(void)a;
return true;
}
static char *getpass(const char *prompt)
{
size_t len;
static char buffer[1024];
fprintf(stdout, "%s", prompt);
fflush(stdout);
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
return NULL;
}
len = strlen(buffer) - 1;
while (buffer[len] == '\r' || buffer[len] == '\n') {
buffer[len] = '\0';
--len;
}
return buffer;
}
#endif
static void usage(char cmd, const void *arg, void *cookie);
static void set_char_ptr(char cmd, const void *arg, void *cookie) {
const char **myptr = cookie;
*myptr = arg;
(void)cmd;
}
const char *host = "localhost:8091";
const char *username = NULL;
const char *passwd = NULL;
const char *bucket = NULL;
const char *filename = "-";
static void set_auth_data(char cmd, const void *arg, void *cookie) {
(void)cmd;
(void)cookie;
username = arg;
if (isatty(fileno(stdin))) {
char prompt[80];
snprintf(prompt, sizeof(prompt), "Please enter password for %s: ", username);
passwd = getpass(prompt);
if (passwd == NULL) {
exit(EXIT_FAILURE);
}
} else {
size_t len;
char buffer[80];
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
exit(EXIT_FAILURE);
}
len = strlen(buffer) - 1;
while (len > 0 && isspace(buffer[len])) {
buffer[len] = '\0';
--len;
}
if (len == 0) {
exit(EXIT_FAILURE);
}
passwd = strdup(buffer);
}
}
typedef void (*OPTION_HANDLER)(char cmd, const void *arg, void *cookie);
static struct {
const char *name;
const char *description;
bool argument;
char letter;
OPTION_HANDLER handler;
void *cookie;
} my_options[256];
static void setup_options(void)
{
my_options['?'].name = "help";
my_options['?'].description = "\t-?\tPrint program usage information";
my_options['?'].argument = false;
my_options['?'].letter = '?';
my_options['?'].handler = usage;
my_options['u'].name = "username";
my_options['u'].description = "\t-u nm\tSpecify username";
my_options['u'].argument = true;
my_options['u'].letter = 'u';
my_options['u'].handler = set_auth_data;
my_options['h'].name = "host";
my_options['h'].description = "\t-h host\tHost to read configuration from";
my_options['h'].argument = true;
my_options['h'].letter = 'h';
my_options['h'].handler = set_char_ptr;
my_options['h'].cookie = &host;
my_options['b'].name = "bucket";
my_options['b'].description = "\t-b bucket\tThe bucket to connect to";
my_options['b'].argument = true;
my_options['b'].letter = 'b';
my_options['b'].handler = set_char_ptr;
my_options['b'].cookie = &bucket;
my_options['o'].name = "file";
my_options['o'].description = "\t-o filename\tSend the output to this file";
my_options['o'].argument = true;
my_options['o'].letter = 'o';
my_options['o'].handler = set_char_ptr;
my_options['o'].cookie = &filename;
}
/**
* Handle all of the command line options the user passed on the command line.
* Please note that this function will set optind to point to the first option
*
* @param argc Argument count
* @param argv Argument vector
*/
static void handle_options(int argc, char **argv) {
struct option opts[256];
int ii = 0;
char shortopts[128];
int jj = 0;
int kk = 0;
int c;
memset(opts, 0, sizeof(opts));
memset(shortopts, 0, sizeof(shortopts));
setup_options();
for (ii = 0; ii < 256; ++ii) {
if (my_options[ii].name != NULL) {
opts[jj].name = (char*)my_options[ii].name;
opts[jj].has_arg = my_options[ii].argument ? required_argument : no_argument;
opts[jj].val = my_options[ii].letter;
shortopts[kk++] = (char)opts[jj++].val;
if (my_options[ii].argument) {
shortopts[kk++] = ':';
}
}
}
while ((c = getopt_long(argc, argv, shortopts, opts, NULL)) != EOF) {
if (my_options[c].handler != NULL) {
my_options[c].handler((char)c, optarg, my_options[c].cookie);
} else {
usage((char)c, NULL, NULL);
}
}
}
FILE *output;
static void get_callback(libcouchbase_t instance,
const void *cookie,
libcouchbase_error_t error,
const void *key, size_t nkey,
const void *bytes, size_t nbytes,
uint32_t flags, uint64_t cas)
{
(void)instance;
(void)cookie;
(void)bytes;
if (error == LIBCOUCHBASE_SUCCESS) {
fprintf(stdout, "Found <");
fwrite(key, nkey, 1, stdout);
fprintf(stdout, "> size: %zu flags %04x cas: %"PRIu64"\n",
nbytes, flags, cas);
fwrite(bytes, nbytes, 1, output);
} else {
fprintf(stderr, "Missing <");
fwrite(key, nkey, 1, stderr);
fprintf(stderr, ">\n");
}
}
static void error_callback(libcouchbase_t instance,
libcouchbase_error_t error,
const char *errinfo)
{
(void)instance;
fprintf(stderr, "Error %d", error);
if (errinfo) {
fprintf(stderr, ": %s", errinfo);
}
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
struct libcouchbase_io_opt_st *io;
libcouchbase_t instance;
char** keys;
size_t* nkey;
size_t jj;
int ii;
libcouchbase_error_t ret;
handle_options(argc, argv);
if (strcmp(filename, "-") == 0) {
output = stdout;
} else {
output = fopen(filename, "w");
if (output == NULL) {
fprintf(stderr, "Failed to open %s: %s\n", filename,
strerror(errno));
return 1;
}
}
keys = calloc((size_t)(argc - optind), sizeof(char*));;
nkey = calloc((size_t)(argc - optind), sizeof(size_t));;
jj = 0;
for (ii = optind; ii < argc; ++ii, ++jj) {
keys[jj] = argv[ii];
nkey[jj] = strlen(keys[jj]);
}
io = libcouchbase_create_io_ops(LIBCOUCHBASE_IO_OPS_DEFAULT, NULL, NULL);
if (io == NULL) {
fprintf(stderr, "Failed to create IO instance\n");
return 1;
}
instance = libcouchbase_create(host, username,
passwd, bucket, io);
if (instance == NULL) {
fprintf(stderr, "Failed to create libcouchbase instance\n");
return 1;
}
(void)libcouchbase_set_error_callback(instance, error_callback);
ret = libcouchbase_connect(instance);
if (ret != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed to connect libcouchbase instance to server: %s\n",
libcouchbase_strerror(instance, ret));
return 1;
}
// Wait for the connect to compelete
libcouchbase_wait(instance);
(void)libcouchbase_set_get_callback(instance, get_callback);
if (libcouchbase_mget(instance, NULL, jj,
(const void * const *)keys,
nkey, NULL) != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed to send requests\n");
return 1;
}
libcouchbase_wait(instance);
free(keys);
free(nkey);
return 0;
}
static void usage(char cmd, const void *arg, void *cookie)
{
int ii;
(void)cmd;
(void)arg;
(void)cookie;
fprintf(stderr, "Usage: ./memcat [options] keys\n");
for (ii = 0; ii < 256; ++ii) {
if (my_options[ii].name != NULL) {
fprintf(stderr, "%s\n", my_options[ii].description);
}
}
exit(EXIT_FAILURE);
}