Replace argp with getopt_long
This commit is contained in:
parent
59f1fb2ec8
commit
b7cc9b68ad
2 changed files with 64 additions and 34 deletions
94
cpu-miner.c
94
cpu-miner.c
|
@ -18,7 +18,7 @@
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <argp.h>
|
#include <getopt.h>
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include "miner.h"
|
#include "miner.h"
|
||||||
|
|
||||||
|
@ -44,32 +44,42 @@ static char *rpc_url = DEF_RPC_URL;
|
||||||
static char *userpass = DEF_RPC_USERPASS;
|
static char *userpass = DEF_RPC_USERPASS;
|
||||||
|
|
||||||
|
|
||||||
static struct argp_option options[] = {
|
struct option_help {
|
||||||
{ "debug", 'D', NULL, 0,
|
const char *name;
|
||||||
"Enable debug output" },
|
const char *helptext;
|
||||||
|
};
|
||||||
|
|
||||||
{ "protocol-dump", 'P', NULL, 0,
|
static struct option_help options_help[] = {
|
||||||
"Verbose dump of protocol-level activities" },
|
{ "help",
|
||||||
|
"(-h) Display this help text" },
|
||||||
|
|
||||||
{ "threads", 't', "N", 0,
|
{ "debug",
|
||||||
"Number of miner threads (default: 1)" },
|
"(-D) Enable debug output" },
|
||||||
|
|
||||||
{ "url", 1001, "URL", 0,
|
{ "protocol-dump",
|
||||||
|
"(-P) Verbose dump of protocol-level activities" },
|
||||||
|
|
||||||
|
{ "threads",
|
||||||
|
"(-t N) Number of miner threads (default: 1)" },
|
||||||
|
|
||||||
|
{ "url",
|
||||||
"URL for bitcoin JSON-RPC server "
|
"URL for bitcoin JSON-RPC server "
|
||||||
"(default: " DEF_RPC_URL ")" },
|
"(default: " DEF_RPC_URL ")" },
|
||||||
|
|
||||||
{ "userpass", 1002, "USER:PASS", 0,
|
{ "userpass",
|
||||||
"Username:Password pair for bitcoin JSON-RPC server "
|
"Username:Password pair for bitcoin JSON-RPC server "
|
||||||
"(default: " DEF_RPC_USERPASS ")" },
|
"(default: " DEF_RPC_USERPASS ")" },
|
||||||
{ }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char doc[] =
|
static struct option options[] = {
|
||||||
PROGRAM_NAME " - CPU miner for bitcoin";
|
{ "help", 0, NULL, 'h' },
|
||||||
|
{ "debug", 0, NULL, 'D' },
|
||||||
static error_t parse_opt (int key, char *arg, struct argp_state *state);
|
{ "protocol-dump", 0, NULL, 'P' },
|
||||||
|
{ "threads", 1, NULL, 't' },
|
||||||
static const struct argp argp = { options, parse_opt, NULL, doc };
|
{ "url", 1, NULL, 1001 },
|
||||||
|
{ "userpass", 1, NULL, 1002 },
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
|
||||||
struct work {
|
struct work {
|
||||||
unsigned char data[128];
|
unsigned char data[128];
|
||||||
|
@ -274,7 +284,22 @@ static void *miner_thread(void *dummy)
|
||||||
return NULL;
|
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;
|
int v;
|
||||||
|
|
||||||
|
@ -288,33 +313,39 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
|
||||||
case 't':
|
case 't':
|
||||||
v = atoi(arg);
|
v = atoi(arg);
|
||||||
if (v < 1 || v > 9999) /* sanity check */
|
if (v < 1 || v > 9999) /* sanity check */
|
||||||
argp_usage(state);
|
show_usage();
|
||||||
|
|
||||||
opt_n_threads = v;
|
opt_n_threads = v;
|
||||||
break;
|
break;
|
||||||
case 1001: /* --url */
|
case 1001: /* --url */
|
||||||
if (strncmp(arg, "http://", 7) &&
|
if (strncmp(arg, "http://", 7) &&
|
||||||
strncmp(arg, "https://", 8))
|
strncmp(arg, "https://", 8))
|
||||||
argp_usage(state);
|
show_usage();
|
||||||
|
|
||||||
rpc_url = arg;
|
rpc_url = arg;
|
||||||
break;
|
break;
|
||||||
case 1002: /* --userpass */
|
case 1002: /* --userpass */
|
||||||
if (!strchr(arg, ':'))
|
if (!strchr(arg, ':'))
|
||||||
argp_usage(state);
|
show_usage();
|
||||||
|
|
||||||
userpass = arg;
|
userpass = arg;
|
||||||
break;
|
break;
|
||||||
case ARGP_KEY_ARG:
|
|
||||||
argp_usage(state); /* too many args */
|
|
||||||
break;
|
|
||||||
case ARGP_KEY_END:
|
|
||||||
break;
|
|
||||||
default:
|
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)
|
static void calc_stats(void)
|
||||||
|
@ -339,15 +370,10 @@ static void calc_stats(void)
|
||||||
|
|
||||||
int main (int argc, char *argv[])
|
int main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
error_t aprc;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* parse command line */
|
/* parse command line */
|
||||||
aprc = argp_parse(&argp, argc, argv, 0, NULL, NULL);
|
parse_cmdline(argc, argv);
|
||||||
if (aprc) {
|
|
||||||
fprintf(stderr, "argp_parse failed: %s\n", strerror(aprc));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* set our priority to the highest (aka "nicest, least intrusive") */
|
/* set our priority to the highest (aka "nicest, least intrusive") */
|
||||||
if (setpriority(PRIO_PROCESS, 0, 19))
|
if (setpriority(PRIO_PROCESS, 0, 19))
|
||||||
|
|
4
miner.h
4
miner.h
|
@ -4,6 +4,10 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
|
|
||||||
|
#ifndef ARRAY_SIZE
|
||||||
|
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||||
|
#endif
|
||||||
|
|
||||||
extern bool opt_protocol;
|
extern bool opt_protocol;
|
||||||
extern json_t *json_rpc_call(const char *url, const char *userpass,
|
extern json_t *json_rpc_call(const char *url, const char *userpass,
|
||||||
const char *rpc_req);
|
const char *rpc_req);
|
||||||
|
|
Loading…
Reference in a new issue