Merge
[m6w6/libmemcached] / clients / memcat.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "libmemcached/common.h"
13 #include <stdio.h>
14 #include <inttypes.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <getopt.h>
18 #include <libmemcached/memcached.h>
19
20 #include "utilities.h"
21
22 #define PROGRAM_NAME "memcat"
23 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
24
25
26 /* Prototypes */
27 void options_parse(int argc, char *argv[]);
28
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
37 int main(int argc, char *argv[])
38 {
39 memcached_st *memc;
40 char *string;
41 size_t string_length;
42 uint32_t flags;
43 memcached_return_t rc;
44 memcached_server_st *servers;
45
46 int return_code= 0;
47
48 options_parse(argc, argv);
49
50 if (!opt_servers)
51 {
52 char *temp;
53
54 if ((temp= getenv("MEMCACHED_SERVERS")))
55 opt_servers= strdup(temp);
56 else
57 {
58 fprintf(stderr, "No Servers provided\n");
59 exit(1);
60 }
61 }
62
63 memc= memcached_create(NULL);
64 process_hash_option(memc, opt_hash);
65
66 servers= memcached_servers_parse(opt_servers);
67
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);
72
73 if (!initialize_sasl(memc, opt_username, opt_passwd))
74 {
75 memcached_free(memc);
76 return 1;
77 }
78
79 while (optind < argc)
80 {
81 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
82 &string_length, &flags, &rc);
83 if (rc == MEMCACHED_SUCCESS)
84 {
85 if (opt_displayflag)
86 {
87 if (opt_verbose)
88 printf("key: %s\nflags: ", argv[optind]);
89 printf("%x\n", flags);
90 }
91 else
92 {
93 if (opt_verbose)
94 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
95 argv[optind], flags, string_length);
96 printf("%.*s\n", (int)string_length, string);
97 free(string);
98 }
99 }
100 else if (rc != MEMCACHED_NOTFOUND)
101 {
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");
107
108 return_code= -1;
109 break;
110 }
111 else // Unknown Issue
112 {
113 fprintf(stderr, "memcat: %s not found\n", argv[optind]);
114 return_code= -1;
115 }
116 optind++;
117 }
118
119 memcached_free(memc);
120
121 if (opt_servers)
122 free(opt_servers);
123 if (opt_hash)
124 free(opt_hash);
125
126 shutdown_sasl();
127
128 return return_code;
129 }
130
131
132 void options_parse(int argc, char *argv[])
133 {
134 int option_index= 0;
135 int option_rv;
136
137 memcached_programs_help_st help_options[]=
138 {
139 {0},
140 };
141
142 static struct option long_options[]=
143 {
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},
154 {0, 0, 0, 0},
155 };
156
157 while (1)
158 {
159 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
160 if (option_rv == -1) break;
161 switch (option_rv)
162 {
163 case 0:
164 break;
165 case OPT_BINARY:
166 opt_binary = 1;
167 break;
168 case OPT_VERBOSE: /* --verbose or -v */
169 opt_verbose = OPT_VERBOSE;
170 break;
171 case OPT_DEBUG: /* --debug or -d */
172 opt_verbose = OPT_DEBUG;
173 break;
174 case OPT_VERSION: /* --version or -V */
175 version_command(PROGRAM_NAME);
176 break;
177 case OPT_HELP: /* --help or -h */
178 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
179 break;
180 case OPT_SERVERS: /* --servers or -s */
181 opt_servers= strdup(optarg);
182 break;
183 case OPT_HASH:
184 opt_hash= strdup(optarg);
185 break;
186 case OPT_USERNAME:
187 opt_username= optarg;
188 break;
189 case OPT_PASSWD:
190 opt_passwd= optarg;
191 break;
192 case '?':
193 /* getopt_long already printed an error message. */
194 exit(1);
195 default:
196 abort();
197 }
198 }
199 }