Updated Version status. Updated all command line tools to return error
[m6w6/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 {
36 fprintf(stderr, "No Servers provided\n");
37 exit(1);
38 }
39 }
40
41 memc= memcached_create(NULL);
42 process_hash_option(memc, opt_hash);
43
44 servers= memcached_servers_parse(opt_servers);
45 memcached_server_push(memc, servers);
46 memcached_server_list_free(servers);
47
48 while (optind < argc)
49 {
50 if (opt_verbose)
51 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
52 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
53
54 if (rc != MEMCACHED_SUCCESS)
55 {
56 fprintf(stderr, "memrm: %s: memcache error %s",
57 argv[optind], memcached_strerror(memc, rc));
58 if (memc->cached_errno)
59 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
60 fprintf(stderr, "\n");
61 }
62
63 optind++;
64 }
65
66 memcached_free(memc);
67
68 if (opt_servers)
69 free(opt_servers);
70 if (opt_hash)
71 free(opt_hash);
72
73 return 0;
74 }
75
76
77 void options_parse(int argc, char *argv[])
78 {
79 memcached_programs_help_st help_options[]=
80 {
81 {0},
82 };
83
84 static struct option long_options[]=
85 {
86 {"version", no_argument, NULL, OPT_VERSION},
87 {"help", no_argument, NULL, OPT_HELP},
88 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
89 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
90 {"servers", required_argument, NULL, OPT_SERVERS},
91 {"expire", required_argument, NULL, OPT_EXPIRE},
92 {"hash", required_argument, NULL, OPT_HASH},
93 {0, 0, 0, 0},
94 };
95 int option_index= 0;
96 int option_rv;
97
98 while (1)
99 {
100 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
101 if (option_rv == -1) break;
102 switch (option_rv)
103 {
104 case 0:
105 break;
106 case OPT_VERBOSE: /* --verbose or -v */
107 opt_verbose = OPT_VERBOSE;
108 break;
109 case OPT_DEBUG: /* --debug or -d */
110 opt_verbose = OPT_DEBUG;
111 break;
112 case OPT_VERSION: /* --version or -V */
113 version_command(PROGRAM_NAME);
114 break;
115 case OPT_HELP: /* --help or -h */
116 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
117 break;
118 case OPT_SERVERS: /* --servers or -s */
119 opt_servers= strdup(optarg);
120 break;
121 case OPT_EXPIRE: /* --expire */
122 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
123 break;
124 case OPT_HASH:
125 opt_hash= strdup(optarg);
126 break;
127 case '?':
128 /* getopt_long already printed an error message. */
129 exit(1);
130 default:
131 abort();
132 }
133 }
134 }