~dave-terei/libmemcached/sasl-fixes

« back to all changes in this revision

Viewing changes to clients/memdump.cc

Improve tesing of command line apps

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
/* Prototypes */
36
36
static void options_parse(int argc, char *argv[]);
37
37
 
38
 
static int opt_binary=0;
 
38
static bool opt_binary=0;
39
39
static int opt_verbose= 0;
40
40
static char *opt_servers= NULL;
41
41
static char *opt_hash= NULL;
43
43
static char *opt_passwd;
44
44
 
45
45
/* Print the keys and counter how many were found */
46
 
static memcached_return_t key_printer(const memcached_st *ptr,
 
46
static memcached_return_t key_printer(const memcached_st *,
47
47
                                      const char *key, size_t key_length,
48
 
                                      void *context)
 
48
                                      void *)
49
49
{
50
 
  (void)ptr;(void)context;
51
 
  printf("%.*s\n", (uint32_t)key_length, key);
 
50
  std::cout.write(key, key_length);
 
51
  std::cout << std::endl;
52
52
 
53
53
  return MEMCACHED_SUCCESS;
54
54
}
55
55
 
56
56
int main(int argc, char *argv[])
57
57
{
58
 
  memcached_st *memc;
59
 
  memcached_return_t rc;
60
 
  memcached_server_st *servers;
61
58
  memcached_dump_fn callbacks[1];
62
59
 
63
60
  callbacks[0]= &key_printer;
64
61
 
65
62
  options_parse(argc, argv);
66
63
 
67
 
  memc= memcached_create(NULL);
 
64
  memcached_st *memc= memcached_create(NULL);
68
65
  process_hash_option(memc, opt_hash);
69
66
 
70
 
  if (!opt_servers)
 
67
  if (opt_servers == NULL)
71
68
  {
72
69
    char *temp;
73
70
 
74
71
    if ((temp= getenv("MEMCACHED_SERVERS")))
 
72
    {
75
73
      opt_servers= strdup(temp);
 
74
    }
76
75
    else
77
76
    {
78
 
      fprintf(stderr, "No Servers provided\n");
79
 
      exit(1);
 
77
      std::cerr << "No Servers provided" << std::endl;
 
78
      exit(EXIT_FAILURE);
80
79
    }
81
80
  }
82
81
 
 
82
  memcached_server_st *servers;
83
83
  if (opt_servers)
 
84
  {
84
85
    servers= memcached_servers_parse(opt_servers);
 
86
  }
85
87
  else
 
88
  {
86
89
    servers= memcached_servers_parse(argv[--argc]);
 
90
  }
87
91
 
88
92
  memcached_server_push(memc, servers);
89
93
  memcached_server_list_free(servers);
108
112
    }
109
113
  }
110
114
 
111
 
  rc= memcached_dump(memc, callbacks, NULL, 1);
 
115
  memcached_return_t rc= memcached_dump(memc, callbacks, NULL, 1);
112
116
 
113
 
  if (rc != MEMCACHED_SUCCESS)
 
117
  int exit_code= EXIT_SUCCESS;
 
118
  if (memcached_failed(rc))
114
119
  {
115
 
    fprintf(stderr, "memdump: memcache error %s", memcached_strerror(memc, rc));
116
 
    if (memcached_last_error_errno(memc))
117
 
      fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
118
 
    fprintf(stderr, "\n");
 
120
    if (opt_verbose)
 
121
    {
 
122
      std::cerr << "Failed to dump keys: " << memcached_last_error_message(memc) << std::endl;
 
123
    }
 
124
    exit_code= EXIT_FAILURE;
119
125
  }
120
126
 
121
127
  memcached_free(memc);
122
128
 
123
129
  if (opt_servers)
 
130
  {
124
131
    free(opt_servers);
 
132
  }
125
133
  if (opt_hash)
 
134
  {
126
135
    free(opt_hash);
 
136
  }
127
137
 
128
 
  return EXIT_SUCCESS;
 
138
  return exit_code;
129
139
}
130
140
 
131
141
static void options_parse(int argc, char *argv[])
132
142
{
133
 
  int option_index= 0;
134
 
  int option_rv;
135
 
 
136
143
  static struct option long_options[]=
137
144
    {
138
145
      {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
139
146
      {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
 
147
      {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
140
148
      {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
141
149
      {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
142
150
      {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
147
155
      {0, 0, 0, 0}
148
156
    };
149
157
 
 
158
  int option_index= 0;
 
159
  bool opt_version= false;
 
160
  bool opt_help= false;
150
161
  while (1)
151
162
  {
152
 
    option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
 
163
    int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
153
164
 
154
165
    if (option_rv == -1) break;
155
166
 
157
168
    {
158
169
    case 0:
159
170
      break;
 
171
 
160
172
    case OPT_BINARY:
161
 
      opt_binary = 1;
 
173
      opt_binary= true;
162
174
      break;
 
175
 
163
176
    case OPT_VERBOSE: /* --verbose or -v */
164
 
      opt_verbose = OPT_VERBOSE;
 
177
      opt_verbose= OPT_VERBOSE;
165
178
      break;
 
179
 
166
180
    case OPT_DEBUG: /* --debug or -d */
167
 
      opt_verbose = OPT_DEBUG;
 
181
      opt_verbose= OPT_DEBUG;
168
182
      break;
 
183
 
169
184
    case OPT_VERSION: /* --version or -V */
170
 
      version_command(PROGRAM_NAME);
 
185
      opt_verbose= true;
171
186
      break;
 
187
 
172
188
    case OPT_HELP: /* --help or -h */
173
 
      help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
 
189
      opt_help= true;
174
190
      break;
 
191
 
175
192
    case OPT_SERVERS: /* --servers or -s */
176
193
      opt_servers= strdup(optarg);
177
194
      break;
 
195
 
178
196
    case OPT_HASH:
179
197
      opt_hash= strdup(optarg);
180
198
      break;
 
199
 
181
200
    case OPT_USERNAME:
182
201
       opt_username= optarg;
183
202
       break;
 
203
 
184
204
    case OPT_PASSWD:
185
205
       opt_passwd= optarg;
186
206
       break;
 
207
 
 
208
    case OPT_QUIET:
 
209
      close_stdio();
 
210
      break;
 
211
 
187
212
    case '?':
188
213
      /* getopt_long already printed an error message. */
189
214
      exit(1);
191
216
      abort();
192
217
    }
193
218
  }
 
219
 
 
220
  if (opt_version)
 
221
  {
 
222
    version_command(PROGRAM_NAME);
 
223
    exit(EXIT_SUCCESS);
 
224
  }
 
225
 
 
226
  if (opt_help)
 
227
  {
 
228
    help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
 
229
    exit(EXIT_SUCCESS);
 
230
  }
194
231
}