Browse Source

common/configdir: simply supply defaults, leave parsing to programs.

We're going to get tricky with lightingd's parsing next, so split it out.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
pull/2938/head
Rusty Russell 6 years ago
parent
commit
0b5b1faff5
  1. 14
      cli/lightning-cli.c
  2. 16
      common/configdir.c
  3. 7
      common/configdir.h
  4. 10
      lightningd/options.c

14
cli/lightning-cli.c

@ -392,12 +392,13 @@ int main(int argc, char *argv[])
int fd, i; int fd, i;
size_t off; size_t off;
const char *method; const char *method;
char *cmd, *resp, *idstr, *rpc_filename; char *cmd, *resp, *idstr;
struct sockaddr_un addr; struct sockaddr_un addr;
jsmntok_t *toks; jsmntok_t *toks;
const jsmntok_t *result, *error, *id; const jsmntok_t *result, *error, *id;
char *lightning_dir;
const tal_t *ctx = tal(NULL, char); const tal_t *ctx = tal(NULL, char);
char *lightning_dir = default_configdir(ctx);
char *rpc_filename = default_rpcfile(ctx);
jsmn_parser parser; jsmn_parser parser;
int parserr; int parserr;
enum format format = DEFAULT_FORMAT; enum format format = DEFAULT_FORMAT;
@ -409,7 +410,14 @@ int main(int argc, char *argv[])
jsmn_init(&parser); jsmn_init(&parser);
opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn); opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn);
configdir_register_opts(ctx, &lightning_dir, &rpc_filename);
opt_register_arg("--lightning-dir=<dir>", opt_set_talstr, opt_show_charp,
&lightning_dir,
"Set working directory. All other files are relative to this");
opt_register_arg("--rpc-file", opt_set_talstr, opt_show_charp,
&rpc_filename,
"Set JSON-RPC socket (or /dev/tty)");
opt_register_noarg("--help|-h", opt_usage_and_exit, opt_register_noarg("--help|-h", opt_usage_and_exit,
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands"); "<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");

16
common/configdir.c

@ -11,7 +11,7 @@ char *opt_set_talstr(const char *arg, char **p)
return opt_set_charp(tal_strdup(NULL, arg), p); return opt_set_charp(tal_strdup(NULL, arg), p);
} }
static char *default_configdir(const tal_t *ctx) char *default_configdir(const tal_t *ctx)
{ {
char *path; char *path;
const char *env = getenv("HOME"); const char *env = getenv("HOME");
@ -22,17 +22,7 @@ static char *default_configdir(const tal_t *ctx)
return path; return path;
} }
void configdir_register_opts(const tal_t *ctx, char *default_rpcfile(const tal_t *ctx)
char **configdir, char **rpc_filename)
{ {
*configdir = default_configdir(ctx); return tal_strdup(ctx, "lightning-rpc");
*rpc_filename = tal_strdup(ctx, "lightning-rpc");
opt_register_early_arg("--lightning-dir=<dir>", opt_set_talstr, opt_show_charp,
configdir,
"Set working directory. All other files are relative to this");
opt_register_arg("--rpc-file", opt_set_talstr, opt_show_charp,
rpc_filename,
"Set JSON-RPC socket (or /dev/tty)");
} }

7
common/configdir.h

@ -6,7 +6,10 @@
/* Helper for options which are tal() strings. */ /* Helper for options which are tal() strings. */
char *opt_set_talstr(const char *arg, char **p); char *opt_set_talstr(const char *arg, char **p);
void configdir_register_opts(const tal_t *ctx, /* The default configuration dir: ~/.lightning */
char **config_dir, char **rpc_filename); char *default_configdir(const tal_t *ctx);
/* The default rpc filename: lightning-rpc */
char *default_rpcfile(const tal_t *ctx);
#endif /* LIGHTNING_COMMON_CONFIGDIR_H */ #endif /* LIGHTNING_COMMON_CONFIGDIR_H */

10
lightningd/options.c

@ -836,6 +836,10 @@ void register_opts(struct lightningd *ld)
{ {
opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn); opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn);
ld->rpc_filename = default_rpcfile(ld);
opt_register_arg("--rpc-file", opt_set_talstr, opt_show_charp,
&ld->rpc_filename,
"Set JSON-RPC socket (or /dev/tty)");
opt_register_noarg("--help|-h", opt_lightningd_usage, ld, opt_register_noarg("--help|-h", opt_lightningd_usage, ld,
"Print this message."); "Print this message.");
opt_register_early_noarg("--test-daemons-only", opt_register_early_noarg("--test-daemons-only",
@ -878,7 +882,6 @@ void register_opts(struct lightningd *ld)
opt_register_logging(ld); opt_register_logging(ld);
opt_register_version(); opt_register_version();
configdir_register_opts(ld, &ld->config_dir, &ld->rpc_filename);
config_register_opts(ld); config_register_opts(ld);
#if DEVELOPER #if DEVELOPER
dev_register_opts(ld); dev_register_opts(ld);
@ -942,6 +945,11 @@ void handle_early_opts(struct lightningd *ld, int argc, char *argv[])
* later by opt_parse_from_config. */ * later by opt_parse_from_config. */
setup_default_config(ld); setup_default_config(ld);
ld->config_dir = default_configdir(ld);
opt_register_early_arg("--lightning-dir=<dir>", opt_set_talstr, opt_show_charp,
&ld->config_dir,
"Set working directory. All other files are relative to this");
/* Get any configdir/testnet options first. */ /* Get any configdir/testnet options first. */
opt_early_parse_incomplete(argc, argv, opt_log_stderr_exit); opt_early_parse_incomplete(argc, argv, opt_log_stderr_exit);

Loading…
Cancel
Save