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