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