Fix from Trond for deprecation
[m6w6/libmemcached] / clients / utilities.c
1 #include "libmemcached/common.h"
2 #include <stdio.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include "utilities.h"
6
7
8 long int timedif(struct timeval a, struct timeval b)
9 {
10 register int us, s;
11
12 us = (int)(a.tv_usec - b.tv_usec);
13 us /= 1000;
14 s = (int)(a.tv_sec - b.tv_sec);
15 s *= 1000;
16 return s + us;
17 }
18
19 void version_command(char *command_name)
20 {
21 printf("%s v%u.%u\n", command_name, 1, 0);
22 exit(0);
23 }
24
25 static char *lookup_help(memcached_options option)
26 {
27 switch (option)
28 {
29 case OPT_SERVERS: return("List which servers you wish to connect to.");
30 case OPT_VERSION: return("Display the version of the application and then exit.");
31 case OPT_HELP: return("Diplay this message and then exit.");
32 case OPT_VERBOSE: return("Give more details on the progression of the application.");
33 case OPT_DEBUG: return("Provide output only useful for debugging.");
34 case OPT_FLAG: return("Provide flag information for storage operation.");
35 case OPT_EXPIRE: return("Set the expire option for the object.");
36 case OPT_SET: return("Use set command with memcached when storing.");
37 case OPT_REPLACE: return("Use replace command with memcached when storing.");
38 case OPT_ADD: return("Use add command with memcached when storing.");
39 case OPT_SLAP_EXECUTE_NUMBER: return("Number of times to execute the given test.");
40 case OPT_SLAP_INITIAL_LOAD: return("Number of key pairs to load before executing tests.");
41 case OPT_SLAP_TEST: return("Test to run (currently \"get\" or \"set\").");
42 case OPT_SLAP_CONCURRENCY: return("Number of users to simulate with load.");
43 case OPT_SLAP_NON_BLOCK: return("Set TCP up to use non-blocking IO.");
44 case OPT_SLAP_TCP_NODELAY: return("Set TCP socket up to use nodelay.");
45 case OPT_FLUSH: return("Flush servers before running tests.");
46 case OPT_HASH: return("Select hash type.");
47 case OPT_BINARY: return("Switch to binary protocol.");
48 case OPT_ANALYZE: return("Analyze the provided servers.");
49 case OPT_UDP: return("Use UDP protocol when communicating with server.");
50 default: WATCHPOINT_ASSERT(0);
51 };
52
53 WATCHPOINT_ASSERT(0);
54 return "forgot to document this function :)";
55 }
56
57 void help_command(char *command_name, char *description,
58 const struct option *long_options,
59 memcached_programs_help_st *options __attribute__((unused)))
60 {
61 unsigned int x;
62
63 printf("%s v%u.%u\n\n", command_name, 1, 0);
64 printf("\t%s\n\n", description);
65 printf("Current options. A '=' means the option takes a value.\n\n");
66
67 for (x= 0; long_options[x].name; x++)
68 {
69 char *help_message;
70
71 printf("\t --%s%c\n", long_options[x].name,
72 long_options[x].has_arg ? '=' : ' ');
73 if ((help_message= lookup_help(long_options[x].val)))
74 printf("\t\t%s\n", help_message);
75 }
76
77 printf("\n");
78 exit(0);
79 }
80
81 void process_hash_option(memcached_st *memc, char *opt_hash)
82 {
83 uint64_t set;
84 memcached_return rc;
85
86 if (opt_hash == NULL)
87 return;
88
89 set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
90 if (!strcasecmp(opt_hash, "CRC"))
91 set= MEMCACHED_HASH_CRC;
92 else if (!strcasecmp(opt_hash, "FNV1_64"))
93 set= MEMCACHED_HASH_FNV1_64;
94 else if (!strcasecmp(opt_hash, "FNV1A_64"))
95 set= MEMCACHED_HASH_FNV1A_64;
96 else if (!strcasecmp(opt_hash, "FNV1_32"))
97 set= MEMCACHED_HASH_FNV1_32;
98 else if (!strcasecmp(opt_hash, "FNV1A_32"))
99 set= MEMCACHED_HASH_FNV1A_32;
100 else
101 {
102 fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
103 exit(1);
104 }
105
106 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
107 if (rc != MEMCACHED_SUCCESS)
108 {
109 fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
110 exit(1);
111 }
112 }
113