Add a single plugin option to the plugin as well as registering it with the * command line options. */
| 1029 | /* Add a single plugin option to the plugin as well as registering it with the |
| 1030 | * command line options. */ |
| 1031 | static const char *plugin_opt_add(struct plugin *plugin, const char *buffer, |
| 1032 | const jsmntok_t *opt) |
| 1033 | { |
| 1034 | const jsmntok_t *nametok, *typetok, *defaulttok, *desctok, *deprtok; |
| 1035 | struct plugin_opt *popt; |
| 1036 | const char *name, *err; |
| 1037 | enum opt_type optflags = 0; |
| 1038 | bool set; |
| 1039 | |
| 1040 | nametok = json_get_member(buffer, opt, "name"); |
| 1041 | typetok = json_get_member(buffer, opt, "type"); |
| 1042 | desctok = json_get_member(buffer, opt, "description"); |
| 1043 | defaulttok = json_get_member(buffer, opt, "default"); |
| 1044 | deprtok = json_get_member(buffer, opt, "deprecated"); |
| 1045 | |
| 1046 | if (!typetok || !nametok || !desctok) { |
| 1047 | return tal_fmt(plugin, |
| 1048 | "An option is missing either \"name\", \"description\" or \"type\""); |
| 1049 | } |
| 1050 | |
| 1051 | popt = tal(plugin, struct plugin_opt); |
| 1052 | popt->plugin = plugin; |
| 1053 | popt->name = tal_fmt(popt, "--%s", |
| 1054 | json_strdup(tmpctx, buffer, nametok)); |
| 1055 | name = popt->name + 2; |
| 1056 | popt->def = NULL; |
| 1057 | popt->depr_start = popt->depr_end = NULL; |
| 1058 | |
| 1059 | /* Only allow sane option names */ |
| 1060 | if (strspn(name, "0123456789" "abcdefghijklmnopqrstuvwxyz" "_-") |
| 1061 | != strlen(name)) |
| 1062 | return tal_fmt(plugin, "Option \"name\" must be lowercase alphanumeric, plus _ or -'"); |
| 1063 | |
| 1064 | /* Don't allow duplicate names! */ |
| 1065 | if (opt_find_long(name, NULL)) { |
| 1066 | /* Fail hard on startup */ |
| 1067 | if (plugin->plugins->startup) |
| 1068 | fatal("error starting plugin '%s':" |
| 1069 | " option name '%s' is already taken", |
| 1070 | plugin->cmd, name); |
| 1071 | return tal_fmt(plugin, "Option \"%s\" already registered", |
| 1072 | name); |
| 1073 | } |
| 1074 | |
| 1075 | popt->description = json_strdup(popt, buffer, desctok); |
| 1076 | |
| 1077 | err = json_parse_deprecated(popt, buffer, deprtok, |
| 1078 | &popt->depr_start, &popt->depr_end); |
| 1079 | if (err) |
| 1080 | return tal_steal(plugin, err); |
| 1081 | |
| 1082 | err = bool_setting(plugin, popt->name, buffer, opt, "multi", &set); |
| 1083 | if (err) |
| 1084 | return err; |
| 1085 | if (set) |
| 1086 | optflags |= OPT_MULTI; |
| 1087 | |
| 1088 | err = bool_setting(plugin, popt->name, buffer, opt, "dynamic", &set); |
no test coverage detected