Initial support for the binary protocol
[awesomized/libmemcached] / clients / memflush.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <getopt.h>
5 #include <libmemcached/memcached.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
14 #define PROGRAM_NAME "memflush"
15 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
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 {
36 fprintf(stderr, "No Servers provided\n");
37 exit(1);
38 }
39 }
40
41 memc= memcached_create(NULL);
42
43 servers= memcached_servers_parse(opt_servers);
44 memcached_server_push(memc, servers);
45 memcached_server_list_free(servers);
46 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
47
48 rc = memcached_flush(memc, opt_expire);
49 if (rc != MEMCACHED_SUCCESS)
50 {
51 fprintf(stderr, "memflush: memcache error %s",
52 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 memcached_free(memc);
59
60 free(opt_servers);
61
62 return 0;
63 }
64
65
66 void options_parse(int argc, char *argv[])
67 {
68 memcached_programs_help_st help_options[]=
69 {
70 {0},
71 };
72
73 static struct option long_options[]=
74 {
75 {"version", no_argument, NULL, OPT_VERSION},
76 {"help", no_argument, NULL, OPT_HELP},
77 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
78 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
79 {"servers", required_argument, NULL, OPT_SERVERS},
80 {"expire", required_argument, NULL, OPT_EXPIRE},
81 {"binary", no_argument, NULL, OPT_BINARY},
82 {0, 0, 0, 0},
83 };
84 int option_index= 0;
85 int option_rv;
86
87 while (1)
88 {
89 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
90 if (option_rv == -1) break;
91 switch (option_rv)
92 {
93 case 0:
94 break;
95 case OPT_BINARY:
96 opt_binary = 1;
97 break;
98 case OPT_VERBOSE: /* --verbose or -v */
99 opt_verbose = OPT_VERBOSE;
100 break;
101 case OPT_DEBUG: /* --debug or -d */
102 opt_verbose = OPT_DEBUG;
103 break;
104 case OPT_VERSION: /* --version or -V */
105 version_command(PROGRAM_NAME);
106 break;
107 case OPT_HELP: /* --help or -h */
108 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
109 break;
110 case OPT_SERVERS: /* --servers or -s */
111 opt_servers= strdup(optarg);
112 break;
113 case OPT_EXPIRE: /* --expire */
114 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
115 break;
116 case '?':
117 /* getopt_long already printed an error message. */
118 exit(1);
119 default:
120 abort();
121 }
122 }
123 }