|
@ -201,6 +201,7 @@ static void human_help(char *buffer, const jsmntok_t *result) |
|
|
enum format { |
|
|
enum format { |
|
|
JSON, |
|
|
JSON, |
|
|
HUMAN, |
|
|
HUMAN, |
|
|
|
|
|
HELPLIST, |
|
|
DEFAULT_FORMAT, |
|
|
DEFAULT_FORMAT, |
|
|
RAW |
|
|
RAW |
|
|
}; |
|
|
}; |
|
@ -392,6 +393,25 @@ static void tal_error(const char *msg) |
|
|
abort(); |
|
|
abort(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static enum format delete_format_hint(const char *resp, |
|
|
|
|
|
jsmntok_t **toks, |
|
|
|
|
|
jsmntok_t *result) |
|
|
|
|
|
{ |
|
|
|
|
|
const jsmntok_t *hint; |
|
|
|
|
|
enum format format = JSON; |
|
|
|
|
|
|
|
|
|
|
|
hint = json_get_member(resp, result, "format-hint"); |
|
|
|
|
|
if (!hint) |
|
|
|
|
|
return format; |
|
|
|
|
|
|
|
|
|
|
|
if (json_tok_streq(resp, hint, "simple")) |
|
|
|
|
|
format = HUMAN; |
|
|
|
|
|
|
|
|
|
|
|
/* Don't let hint appear in the output! */ |
|
|
|
|
|
json_tok_remove(toks, result, hint, 1); |
|
|
|
|
|
return format; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) |
|
|
int main(int argc, char *argv[]) |
|
|
{ |
|
|
{ |
|
|
setup_locale(); |
|
|
setup_locale(); |
|
@ -453,16 +473,9 @@ int main(int argc, char *argv[]) |
|
|
method = "help"; |
|
|
method = "help"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (format == DEFAULT_FORMAT) { |
|
|
|
|
|
if (streq(method, "help")) |
|
|
|
|
|
format = HUMAN; |
|
|
|
|
|
else |
|
|
|
|
|
format = JSON; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Launch a manpage if we have a help command with an argument. We do
|
|
|
/* Launch a manpage if we have a help command with an argument. We do
|
|
|
* not need to have lightningd running in this case. */ |
|
|
* not need to have lightningd running in this case. */ |
|
|
if (streq(method, "help") && format == HUMAN && argc >= 3) { |
|
|
if (streq(method, "help") && format == DEFAULT_FORMAT && argc >= 3) { |
|
|
command = argv[2]; |
|
|
command = argv[2]; |
|
|
char *page = tal_fmt(ctx, "lightning-%s", command); |
|
|
char *page = tal_fmt(ctx, "lightning-%s", command); |
|
|
|
|
|
|
|
@ -591,19 +604,35 @@ int main(int argc, char *argv[]) |
|
|
json_tok_full_len(id), json_tok_full(resp, id)); |
|
|
json_tok_full_len(id), json_tok_full(resp, id)); |
|
|
|
|
|
|
|
|
if (!error || json_tok_is_null(resp, error)) { |
|
|
if (!error || json_tok_is_null(resp, error)) { |
|
|
// if we have specific help command
|
|
|
if (format == DEFAULT_FORMAT) { |
|
|
if (format == HUMAN) |
|
|
/* This works best when we order it. */ |
|
|
if (streq(method, "help") && command == NULL) |
|
|
if (streq(method, "help") && command == NULL) |
|
|
human_help(resp, result); |
|
|
format = HELPLIST; |
|
|
else |
|
|
else |
|
|
human_readable(resp, result, '\n'); |
|
|
/* Use offset of result to get non-const ptr */ |
|
|
else if (format == RAW) |
|
|
format = delete_format_hint(resp, &toks, |
|
|
|
|
|
/* const-washing */ |
|
|
|
|
|
toks + (result - toks)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch (format) { |
|
|
|
|
|
case HELPLIST: |
|
|
|
|
|
human_help(resp, result); |
|
|
|
|
|
break; |
|
|
|
|
|
case HUMAN: |
|
|
|
|
|
human_readable(resp, result, '\n'); |
|
|
|
|
|
break; |
|
|
|
|
|
case JSON: |
|
|
|
|
|
print_json(resp, result, ""); |
|
|
|
|
|
printf("\n"); |
|
|
|
|
|
break; |
|
|
|
|
|
case RAW: |
|
|
printf("%.*s\n", |
|
|
printf("%.*s\n", |
|
|
json_tok_full_len(result), |
|
|
json_tok_full_len(result), |
|
|
json_tok_full(resp, result)); |
|
|
json_tok_full(resp, result)); |
|
|
else { |
|
|
break; |
|
|
print_json(resp, result, ""); |
|
|
default: |
|
|
printf("\n"); |
|
|
abort(); |
|
|
} |
|
|
} |
|
|
tal_free(lightning_dir); |
|
|
tal_free(lightning_dir); |
|
|
tal_free(rpc_filename); |
|
|
tal_free(rpc_filename); |
|
|