2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
12 #include "libmemcached/common.h"
18 #include <libmemcached/memcached.h>
20 #include "utilities.h"
22 #define PROGRAM_NAME "memcat"
23 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
27 void options_parse(int argc
, char *argv
[]);
29 static int opt_binary
= 0;
30 static int opt_verbose
= 0;
31 static int opt_displayflag
= 0;
32 static char *opt_servers
= NULL
;
33 static char *opt_hash
= NULL
;
34 static char *opt_username
;
35 static char *opt_passwd
;
36 static char *opt_file
;
38 int main(int argc
, char *argv
[])
44 memcached_return_t rc
;
45 memcached_server_st
*servers
;
49 options_parse(argc
, argv
);
55 if ((temp
= getenv("MEMCACHED_SERVERS")))
56 opt_servers
= strdup(temp
);
59 fprintf(stderr
, "No Servers provided\n");
64 memc
= memcached_create(NULL
);
65 process_hash_option(memc
, opt_hash
);
67 servers
= memcached_servers_parse(opt_servers
);
69 memcached_server_push(memc
, servers
);
70 memcached_server_list_free(servers
);
71 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
72 (uint64_t)opt_binary
);
74 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
82 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
83 &string_length
, &flags
, &rc
);
84 if (rc
== MEMCACHED_SUCCESS
)
89 printf("key: %s\nflags: ", argv
[optind
]);
90 printf("%x\n", flags
);
95 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
96 argv
[optind
], flags
, string_length
);
101 fp
= fopen(opt_file
, "w");
108 written
= fwrite(string
, 1, string_length
, fp
);
109 if (written
!= string_length
) {
110 fprintf(stderr
, "error writing file (written %d, should be %d)\n", written
, string_length
);
116 fprintf(stderr
, "error closing file\n");
122 printf("%.*s\n", (int)string_length
, string
);
127 else if (rc
!= MEMCACHED_NOTFOUND
)
129 fprintf(stderr
, "memcat: %s: memcache error %s",
130 argv
[optind
], memcached_strerror(memc
, rc
));
131 if (memc
->cached_errno
)
132 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
133 fprintf(stderr
, "\n");
138 else // Unknown Issue
140 fprintf(stderr
, "memcat: %s not found\n", argv
[optind
]);
146 memcached_free(memc
);
159 void options_parse(int argc
, char *argv
[])
164 memcached_programs_help_st help_options
[]=
169 static struct option long_options
[]=
171 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
172 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
173 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
174 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
175 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
176 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
177 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
178 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
179 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
180 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
181 {(OPTIONSTRING
)"file", required_argument
, NULL
, OPT_FILE
},
187 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
188 if (option_rv
== -1) break;
196 case OPT_VERBOSE
: /* --verbose or -v */
197 opt_verbose
= OPT_VERBOSE
;
199 case OPT_DEBUG
: /* --debug or -d */
200 opt_verbose
= OPT_DEBUG
;
202 case OPT_VERSION
: /* --version or -V */
203 version_command(PROGRAM_NAME
);
205 case OPT_HELP
: /* --help or -h */
206 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
208 case OPT_SERVERS
: /* --servers or -s */
209 opt_servers
= strdup(optarg
);
212 opt_hash
= strdup(optarg
);
215 opt_username
= optarg
;
224 /* getopt_long already printed an error message. */