Replace argp with getopt_long
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
diff --git a/cpu-miner.c b/cpu-miner.c
index da6e007..05b5a9a 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -18,7 +18,7 @@
#include <sys/time.h>
#include <sys/resource.h>
#include <pthread.h>
-#include <argp.h>
+#include <getopt.h>
#include <jansson.h>
#include "miner.h"
@@ -44,32 +44,42 @@ static char *rpc_url = DEF_RPC_URL;
static char *userpass = DEF_RPC_USERPASS;
-static struct argp_option options[] = {
- { "debug", 'D', NULL, 0,
- "Enable debug output" },
+struct option_help {
+ const char *name;
+ const char *helptext;
+};
+
+static struct option_help options_help[] = {
+ { "help",
+ "(-h) Display this help text" },
- { "protocol-dump", 'P', NULL, 0,
- "Verbose dump of protocol-level activities" },
+ { "debug",
+ "(-D) Enable debug output" },
- { "threads", 't', "N", 0,
- "Number of miner threads (default: 1)" },
+ { "protocol-dump",
+ "(-P) Verbose dump of protocol-level activities" },
- { "url", 1001, "URL", 0,
+ { "threads",
+ "(-t N) Number of miner threads (default: 1)" },
+
+ { "url",
"URL for bitcoin JSON-RPC server "
"(default: " DEF_RPC_URL ")" },
- { "userpass", 1002, "USER:PASS", 0,
+ { "userpass",
"Username:Password pair for bitcoin JSON-RPC server "
"(default: " DEF_RPC_USERPASS ")" },
- { }
};
-static const char doc[] =
-PROGRAM_NAME " - CPU miner for bitcoin";
-
-static error_t parse_opt (int key, char *arg, struct argp_state *state);
-
-static const struct argp argp = { options, parse_opt, NULL, doc };
+static struct option options[] = {
+ { "help", 0, NULL, 'h' },
+ { "debug", 0, NULL, 'D' },
+ { "protocol-dump", 0, NULL, 'P' },
+ { "threads", 1, NULL, 't' },
+ { "url", 1, NULL, 1001 },
+ { "userpass", 1, NULL, 1002 },
+ { }
+};
struct work {
unsigned char data[128];
@@ -274,7 +284,22 @@ static void *miner_thread(void *dummy)
return NULL;
}
-static error_t parse_opt (int key, char *arg, struct argp_state *state)
+static void show_usage(void)
+{
+ int i;
+
+ printf("Summary: minerd [options]\n\nSupported options:\n");
+ for (i = 0; i < ARRAY_SIZE(options_help); i++) {
+ struct option_help *h;
+
+ h = &options_help[i];
+ printf("--%s\n%s\n\n", h->name, h->helptext);
+ }
+
+ exit(1);
+}
+
+static void parse_arg (int key, char *arg)
{
int v;
@@ -288,33 +313,39 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
case 't':
v = atoi(arg);
if (v < 1 || v > 9999) /* sanity check */
- argp_usage(state);
+ show_usage();
opt_n_threads = v;
break;
case 1001: /* --url */
if (strncmp(arg, "http://", 7) &&
strncmp(arg, "https://", 8))
- argp_usage(state);
+ show_usage();
rpc_url = arg;
break;
case 1002: /* --userpass */
if (!strchr(arg, ':'))
- argp_usage(state);
+ show_usage();
userpass = arg;
break;
- case ARGP_KEY_ARG:
- argp_usage(state); /* too many args */
- break;
- case ARGP_KEY_END:
- break;
default:
- return ARGP_ERR_UNKNOWN;
+ show_usage();
}
+}
- return 0;
+static void parse_cmdline(int argc, char *argv[])
+{
+ int key;
+
+ while (1) {
+ key = getopt_long(argc, argv, "DPt:h?", options, NULL);
+ if (key < 0)
+ break;
+
+ parse_arg(key, optarg);
+ }
}
static void calc_stats(void)
@@ -339,15 +370,10 @@ static void calc_stats(void)
int main (int argc, char *argv[])
{
- error_t aprc;
int i;
/* parse command line */
- aprc = argp_parse(&argp, argc, argv, 0, NULL, NULL);
- if (aprc) {
- fprintf(stderr, "argp_parse failed: %s\n", strerror(aprc));
- return 1;
- }
+ parse_cmdline(argc, argv);
/* set our priority to the highest (aka "nicest, least intrusive") */
if (setpriority(PRIO_PROCESS, 0, 19))
diff --git a/miner.h b/miner.h
index 1e5af4f..dc84c1c 100644
--- a/miner.h
+++ b/miner.h
@@ -4,6 +4,10 @@
#include <stdbool.h>
#include <jansson.h>
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
extern bool opt_protocol;
extern json_t *json_rpc_call(const char *url, const char *userpass,
const char *rpc_req);