mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
Instead of passing the line number behind the '\0' of an artificial command-line argument, store it in a global variable.
While it is still a bit of a hack, this makes the code much easier to read.
This commit is contained in:
parent
b857b2e843
commit
71c39e96f3
1 changed files with 43 additions and 16 deletions
|
@ -481,6 +481,8 @@ static void setup_default_config(struct lightningd *ld)
|
||||||
|
|
||||||
|
|
||||||
/* FIXME: make this nicer! */
|
/* FIXME: make this nicer! */
|
||||||
|
static int config_parse_line_number = 0;
|
||||||
|
|
||||||
static void config_log_stderr_exit(const char *fmt, ...)
|
static void config_log_stderr_exit(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
char *msg;
|
char *msg;
|
||||||
|
@ -488,16 +490,19 @@ static void config_log_stderr_exit(const char *fmt, ...)
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
|
|
||||||
/* This is the format we expect: mangle it to remove '--'. */
|
/* This is the format we expect:*/
|
||||||
if (streq(fmt, "%s: %.*s: %s")) {
|
if (streq(fmt, "%s: %.*s: %s")) {
|
||||||
const char *argv0 = va_arg(ap, const char *);
|
const char *argv0 = va_arg(ap, const char *);
|
||||||
unsigned int len = va_arg(ap, unsigned int);
|
unsigned int len = va_arg(ap, unsigned int);
|
||||||
const char *arg = va_arg(ap, const char *);
|
const char *arg = va_arg(ap, const char *);
|
||||||
const char *problem = va_arg(ap, const char *);
|
const char *problem = va_arg(ap, const char *);
|
||||||
|
|
||||||
|
assert(argv0 != NULL);
|
||||||
assert(arg != NULL);
|
assert(arg != NULL);
|
||||||
msg = tal_fmt(NULL, "%s line %s: %.*s: %s",
|
assert(problem != NULL);
|
||||||
argv0, arg+strlen(arg)+1, len-2, arg+2, problem);
|
/*mangle it to remove '--' and add the line number.*/
|
||||||
|
msg = tal_fmt(NULL, "%s line %d: %.*s: %s",
|
||||||
|
argv0, config_parse_line_number, len-2, arg+2, problem);
|
||||||
} else {
|
} else {
|
||||||
msg = tal_vfmt(NULL, fmt, ap);
|
msg = tal_vfmt(NULL, fmt, ap);
|
||||||
}
|
}
|
||||||
|
@ -510,7 +515,8 @@ static void config_log_stderr_exit(const char *fmt, ...)
|
||||||
static void opt_parse_from_config(struct lightningd *ld)
|
static void opt_parse_from_config(struct lightningd *ld)
|
||||||
{
|
{
|
||||||
char *contents, **lines;
|
char *contents, **lines;
|
||||||
char **argv;
|
char **all_args; /*For each line: either argument string or NULL*/
|
||||||
|
char *argv[3];
|
||||||
int i, argc;
|
int i, argc;
|
||||||
|
|
||||||
contents = grab_file(ld, "config");
|
contents = grab_file(ld, "config");
|
||||||
|
@ -526,26 +532,47 @@ static void opt_parse_from_config(struct lightningd *ld)
|
||||||
|
|
||||||
lines = tal_strsplit(contents, contents, "\r\n", STR_NO_EMPTY);
|
lines = tal_strsplit(contents, contents, "\r\n", STR_NO_EMPTY);
|
||||||
|
|
||||||
/* We have to keep argv around, since opt will point into it */
|
/* We have to keep all_args around, since opt will point into it */
|
||||||
argv = tal_arr(ld, char *, argc = 1);
|
all_args = tal_arr(ld, char *, tal_count(lines) - 1);
|
||||||
argv[0] = "lightning config file";
|
|
||||||
|
|
||||||
for (i = 0; i < tal_count(lines) - 1; i++) {
|
for (i = 0; i < tal_count(lines) - 1; i++) {
|
||||||
if (strstarts(lines[i], "#"))
|
if (strstarts(lines[i], "#")) {
|
||||||
continue;
|
all_args[i] = NULL;
|
||||||
/* Only valid forms are "foo" and "foo=bar" */
|
}
|
||||||
tal_resize(&argv, argc+1);
|
else {
|
||||||
/* Stash line number after nul. */
|
/* Only valid forms are "foo" and "foo=bar" */
|
||||||
argv[argc++] = tal_fmt(argv, "--%s%c%u", lines[i], 0, i+1);
|
all_args[i] = tal_fmt(all_args, "--%s", lines[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tal_resize(&argv, argc+1);
|
|
||||||
|
/*
|
||||||
|
For each line we construct a fake argc,argv commandline.
|
||||||
|
argv[1] is the only element that changes between iterations.
|
||||||
|
*/
|
||||||
|
argc = 2;
|
||||||
|
argv[0] = "lightning config file";
|
||||||
argv[argc] = NULL;
|
argv[argc] = NULL;
|
||||||
|
|
||||||
opt_early_parse(argc, argv, config_log_stderr_exit);
|
for (i = 0; i < tal_count(all_args); i++) {
|
||||||
|
if(all_args[i] != NULL) {
|
||||||
|
config_parse_line_number = i + 1;
|
||||||
|
argv[1] = all_args[i];
|
||||||
|
opt_early_parse(argc, argv, config_log_stderr_exit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Now we can set up defaults, depending on whether testnet or not */
|
/* Now we can set up defaults, depending on whether testnet or not */
|
||||||
setup_default_config(ld);
|
setup_default_config(ld);
|
||||||
|
|
||||||
opt_parse(&argc, argv, config_log_stderr_exit);
|
for (i = 0; i < tal_count(all_args); i++) {
|
||||||
|
if(all_args[i] != NULL) {
|
||||||
|
config_parse_line_number = i + 1;
|
||||||
|
argv[1] = all_args[i];
|
||||||
|
opt_parse(&argc, argv, config_log_stderr_exit);
|
||||||
|
argc = 2; /* opt_parse might have changed it */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tal_free(contents);
|
tal_free(contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue