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
;
37 int main(int argc
, char *argv
[])
43 memcached_return_t rc
;
44 memcached_server_st
*servers
;
48 options_parse(argc
, argv
);
54 if ((temp
= getenv("MEMCACHED_SERVERS")))
55 opt_servers
= strdup(temp
);
58 fprintf(stderr
, "No Servers provided\n");
63 memc
= memcached_create(NULL
);
64 process_hash_option(memc
, opt_hash
);
66 servers
= memcached_servers_parse(opt_servers
);
68 memcached_server_push(memc
, servers
);
69 memcached_server_list_free(servers
);
70 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
71 (uint64_t)opt_binary
);
73 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
81 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
82 &string_length
, &flags
, &rc
);
83 if (rc
== MEMCACHED_SUCCESS
)
88 printf("key: %s\nflags: ", argv
[optind
]);
89 printf("%x\n", flags
);
94 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
95 argv
[optind
], flags
, string_length
);
96 printf("%.*s\n", (int)string_length
, string
);
100 else if (rc
!= MEMCACHED_NOTFOUND
)
102 fprintf(stderr
, "memcat: %s: memcache error %s",
103 argv
[optind
], memcached_strerror(memc
, rc
));
104 if (memc
->cached_errno
)
105 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
106 fprintf(stderr
, "\n");
111 else // Unknown Issue
113 fprintf(stderr
, "memcat: %s not found\n", argv
[optind
]);
119 memcached_free(memc
);
132 void options_parse(int argc
, char *argv
[])
137 memcached_programs_help_st help_options
[]=
142 static struct option long_options
[]=
144 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
145 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
146 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
147 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
148 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
149 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
150 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
151 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
152 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
153 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
159 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
160 if (option_rv
== -1) break;
168 case OPT_VERBOSE
: /* --verbose or -v */
169 opt_verbose
= OPT_VERBOSE
;
171 case OPT_DEBUG
: /* --debug or -d */
172 opt_verbose
= OPT_DEBUG
;
174 case OPT_VERSION
: /* --version or -V */
175 version_command(PROGRAM_NAME
);
177 case OPT_HELP
: /* --help or -h */
178 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
180 case OPT_SERVERS
: /* --servers or -s */
181 opt_servers
= strdup(optarg
);
184 opt_hash
= strdup(optarg
);
187 opt_username
= optarg
;
193 /* getopt_long already printed an error message. */