Enhance startup error messages

This commit is contained in:
pooler 2014-05-09 17:55:08 +02:00
parent 788e4a3746
commit d701fecd57

View file

@ -1448,7 +1448,7 @@ static void show_usage_and_exit(int status)
exit(status); exit(status);
} }
static void parse_arg (int key, char *arg) static void parse_arg(int key, char *arg, char *pname)
{ {
char *p; char *p;
int v, i; int v, i;
@ -1462,8 +1462,11 @@ static void parse_arg (int key, char *arg)
break; break;
} }
} }
if (i == ARRAY_SIZE(algo_names)) if (i == ARRAY_SIZE(algo_names)) {
fprintf(stderr, "%s: unknown algorithm -- '%s'\n",
pname, arg);
show_usage_and_exit(1); show_usage_and_exit(1);
}
break; break;
case 'B': case 'B':
opt_background = true; opt_background = true;
@ -1478,7 +1481,11 @@ static void parse_arg (int key, char *arg)
opt_config = json_load_file(arg, &err); opt_config = json_load_file(arg, &err);
#endif #endif
if (!json_is_object(opt_config)) { if (!json_is_object(opt_config)) {
applog(LOG_ERR, "JSON decode of %s failed", arg); if (err.line < 0)
fprintf(stderr, "%s: %s\n", pname, err.text);
else
fprintf(stderr, "%s: %s:%d: %s\n",
pname, arg, err.line, err.text);
exit(1); exit(1);
} }
break; break;
@ -1533,14 +1540,21 @@ static void parse_arg (int key, char *arg)
case 'o': /* --url */ case 'o': /* --url */
p = strstr(arg, "://"); p = strstr(arg, "://");
if (p) { if (p) {
if (strncasecmp(arg, "http://", 7) && strncasecmp(arg, "https://", 8) && if (strncasecmp(arg, "http://", 7) &&
strncasecmp(arg, "stratum+tcp://", 14)) strncasecmp(arg, "https://", 8) &&
strncasecmp(arg, "stratum+tcp://", 14)) {
fprintf(stderr, "%s: unknown protocol -- '%s'\n",
pname, arg);
show_usage_and_exit(1); show_usage_and_exit(1);
}
free(rpc_url); free(rpc_url);
rpc_url = strdup(arg); rpc_url = strdup(arg);
} else { } else {
if (!strlen(arg) || *arg == '/') if (!strlen(arg) || *arg == '/') {
fprintf(stderr, "%s: invalid URL -- '%s'\n",
pname, arg);
show_usage_and_exit(1); show_usage_and_exit(1);
}
free(rpc_url); free(rpc_url);
rpc_url = malloc(strlen(arg) + 8); rpc_url = malloc(strlen(arg) + 8);
sprintf(rpc_url, "http://%s", arg); sprintf(rpc_url, "http://%s", arg);
@ -1569,8 +1583,11 @@ static void parse_arg (int key, char *arg)
break; break;
case 'O': /* --userpass */ case 'O': /* --userpass */
p = strchr(arg, ':'); p = strchr(arg, ':');
if (!p) if (!p) {
fprintf(stderr, "%s: invalid username:password pair -- '%s'\n",
pname, arg);
show_usage_and_exit(1); show_usage_and_exit(1);
}
free(rpc_userpass); free(rpc_userpass);
rpc_userpass = strdup(arg); rpc_userpass = strdup(arg);
free(rpc_user); free(rpc_user);
@ -1622,12 +1639,17 @@ static void parse_arg (int key, char *arg)
break; break;
case 1013: /* --coinbase-addr */ case 1013: /* --coinbase-addr */
pk_script_size = address_to_script(pk_script, sizeof(pk_script), arg); pk_script_size = address_to_script(pk_script, sizeof(pk_script), arg);
if (!pk_script_size) if (!pk_script_size) {
fprintf(stderr, "%s: invalid address -- '%s'\n",
pname, arg);
show_usage_and_exit(1); show_usage_and_exit(1);
}
break; break;
case 1015: /* --coinbase-sig */ case 1015: /* --coinbase-sig */
if (strlen(arg) + 1 > sizeof(coinbase_sig)) if (strlen(arg) + 1 > sizeof(coinbase_sig)) {
fprintf(stderr, "%s: coinbase signature too long\n", pname);
show_usage_and_exit(1); show_usage_and_exit(1);
}
strcpy(coinbase_sig, arg); strcpy(coinbase_sig, arg);
break; break;
case 'S': case 'S':
@ -1642,7 +1664,7 @@ static void parse_arg (int key, char *arg)
} }
} }
static void parse_config(void) static void parse_config(char *pname)
{ {
int i; int i;
json_t *val; json_t *val;
@ -1664,13 +1686,15 @@ static void parse_config(void)
char *s = strdup(json_string_value(val)); char *s = strdup(json_string_value(val));
if (!s) if (!s)
break; break;
parse_arg(options[i].val, s); parse_arg(options[i].val, s, pname);
free(s); free(s);
} else if (!options[i].has_arg && json_is_true(val)) } else if (!options[i].has_arg && json_is_true(val))
parse_arg(options[i].val, ""); parse_arg(options[i].val, "", pname);
else else {
applog(LOG_ERR, "JSON option %s invalid", fprintf(stderr, "%s: invalid argument for option '%s'\n",
options[i].name); pname, options[i].name);
exit(1);
}
} }
} }
@ -1687,15 +1711,15 @@ static void parse_cmdline(int argc, char *argv[])
if (key < 0) if (key < 0)
break; break;
parse_arg(key, optarg); parse_arg(key, optarg, argv[0]);
} }
if (optind < argc) { if (optind < argc) {
fprintf(stderr, "%s: unsupported non-option argument '%s'\n", fprintf(stderr, "%s: unsupported non-option argument -- '%s'\n",
argv[0], argv[optind]); argv[0], argv[optind]);
show_usage_and_exit(1); show_usage_and_exit(1);
} }
parse_config(); parse_config(argv[0]);
} }
#ifndef WIN32 #ifndef WIN32