Returns NULL if invalid value for that type */
| 813 | |
| 814 | /* Returns NULL if invalid value for that type */ |
| 815 | static struct plugin_opt_value *plugin_opt_value(const tal_t *ctx, |
| 816 | const char *type, |
| 817 | const char *arg) |
| 818 | { |
| 819 | struct plugin_opt_value *v = tal(ctx, struct plugin_opt_value); |
| 820 | |
| 821 | v->as_str = tal_strdup(v, arg); |
| 822 | if (streq(type, "int")) { |
| 823 | long long l; |
| 824 | char *endp; |
| 825 | |
| 826 | errno = 0; |
| 827 | l = strtoll(arg, &endp, 0); |
| 828 | if (errno || *endp) |
| 829 | return tal_free(v); |
| 830 | v->as_int = l; |
| 831 | |
| 832 | /* Check if the number did not fit in `s64` (in case `long long` |
| 833 | * is a bigger type). */ |
| 834 | if (v->as_int != l) |
| 835 | return tal_free(v); |
| 836 | } else if (streq(type, "bool")) { |
| 837 | /* valid values are 'true', 'True', '1', '0', 'false', 'False', or '' */ |
| 838 | if (streq(arg, "true") || streq(arg, "True") || streq(arg, "1")) { |
| 839 | v->as_bool = true; |
| 840 | } else if (streq(arg, "false") || streq(arg, "False") |
| 841 | || streq(arg, "0")) { |
| 842 | v->as_bool = false; |
| 843 | } else |
| 844 | return tal_free(v); |
| 845 | } else if (streq(type, "flag")) { |
| 846 | v->as_bool = true; |
| 847 | } |
| 848 | |
| 849 | return v; |
| 850 | } |
| 851 | |
| 852 | char *plugin_opt_flag_set(struct plugin_opt *popt) |
| 853 | { |
no test coverage detected