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