Merge Monty
[awesomized/libmemcached] / clients / memrm.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 <unistd.h>
15 #include <getopt.h>
16 #include <libmemcached/memcached.h>
17 #include <string.h>
18 #include "client_options.h"
19 #include "utilities.h"
20
21 static int opt_binary= 0;
22 static int opt_verbose= 0;
23 static time_t opt_expire= 0;
24 static char *opt_servers= NULL;
25 static char *opt_hash= NULL;
26
27 #define PROGRAM_NAME "memrm"
28 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
29
30 /* Prototypes */
31 static void options_parse(int argc, char *argv[]);
32
33 int main(int argc, char *argv[])
34 {
35 memcached_st *memc;
36 memcached_return_t rc;
37 memcached_server_st *servers;
38
39 options_parse(argc, argv);
40
41 if (!opt_servers)
42 {
43 char *temp;
44
45 if ((temp= getenv("MEMCACHED_SERVERS")))
46 opt_servers= strdup(temp);
47 else
48 {
49 fprintf(stderr, "No Servers provided\n");
50 exit(1);
51 }
52 }
53
54 memc= memcached_create(NULL);
55 process_hash_option(memc, opt_hash);
56
57 servers= memcached_servers_parse(opt_servers);
58 memcached_server_push(memc, servers);
59 memcached_server_list_free(servers);
60 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
61 (uint64_t) opt_binary);
62
63 while (optind < argc)
64 {
65 if (opt_verbose)
66 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
67 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
68
69 if (rc != MEMCACHED_SUCCESS)
70 {
71 fprintf(stderr, "memrm: %s: memcache error %s",
72 argv[optind], memcached_strerror(memc, rc));
73 if (memc->cached_errno)
74 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
75 fprintf(stderr, "\n");
76 }
77
78 optind++;
79 }
80
81 memcached_free(memc);
82
83 if (opt_servers)
84 free(opt_servers);
85 if (opt_hash)
86 free(opt_hash);
87
88 return 0;
89 }
90
91
92 static void options_parse(int argc, char *argv[])
93 {
94 memcached_programs_help_st help_options[]=
95 {
96 {0},
97 };
98
99 static struct option long_options[]=
100 {
101 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
102 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
103 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
104 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
105 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
106 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
107 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
108 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
109 {0, 0, 0, 0},
110 };
111 int option_index= 0;
112 int option_rv;
113
114 while (1)
115 {
116 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
117 if (option_rv == -1) break;
118 switch (option_rv)
119 {
120 case 0:
121 break;
122 case OPT_BINARY:
123 opt_binary = 1;
124 break;
125 case OPT_VERBOSE: /* --verbose or -v */
126 opt_verbose = OPT_VERBOSE;
127 break;
128 case OPT_DEBUG: /* --debug or -d */
129 opt_verbose = OPT_DEBUG;
130 break;
131 case OPT_VERSION: /* --version or -V */
132 version_command(PROGRAM_NAME);
133 break;
134 case OPT_HELP: /* --help or -h */
135 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
136 break;
137 case OPT_SERVERS: /* --servers or -s */
138 opt_servers= strdup(optarg);
139 break;
140 case OPT_EXPIRE: /* --expire */
141 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
142 break;
143 case OPT_HASH:
144 opt_hash= strdup(optarg);
145 break;
146 case '?':
147 /* getopt_long already printed an error message. */
148 exit(1);
149 default:
150 abort();
151 }
152 }
153 }