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