Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow perl variables to participate in argumentless set/unset

  • Loading branch information...
commit 3b9ca713910ca1c21db2aa8293da541e66eaa97c 1 parent 69f74c2
@davidben davidben authored
View
6 commands.c
@@ -1603,8 +1603,7 @@ char *owl_command_set(int argc, const char *const *argv, const char *buff)
v = owl_variable_get_var(owl_global_get_vardict(&g), var);
if (v == NULL) {
if (!silent) owl_function_error("Unknown variable '%s'", var);
- } else if (requirebool && owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
- // FIXME: The above won't work when we make perl variables OWL_VARIABLE_SV
+ } else if (requirebool && !v->takes_on_off) {
if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
} else {
owl_variable_set_fromstring(v, val, !silent);
@@ -1633,8 +1632,7 @@ char *owl_command_unset(int argc, const char *const *argv, const char *buff)
v = owl_variable_get_var(owl_global_get_vardict(&g), var);
if (v == NULL) {
if (!silent) owl_function_error("Unknown variable '%s'", var);
- } else if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
- // FIXME: The above won't work when we make perl variables OWL_VARIABLE_SV
+ } else if (!v->takes_on_off) {
if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
} else {
owl_variable_set_fromstring(v, val, !silent);
View
1  owl.h
@@ -238,6 +238,7 @@ typedef struct _owl_variable {
char *validsettings; /* documentation of valid settings */
char *summary; /* summary of usage */
char *description; /* detailed description */
+ bool takes_on_off; /* allow passing on/off in argument-less set/unset */
GClosure *set_fromstring_fn;
/* sets the variable to a value
* of the appropriate type.
View
21 perl/lib/BarnOwl.pm
@@ -425,29 +425,30 @@ A longer description of the function of the variable
=cut
sub new_variable_int {
- unshift @_, 0, "<int>", sub { "$_[0]" }, # to string
- sub { $_[0] =~ /^-?[0-9]+$/ }, # validate
- sub { 0 + $_[0] }; # from string
+ unshift @_, 0, "<int>", 0, sub { "$_[0]" }, # to string
+ sub { $_[0] =~ /^-?[0-9]+$/ }, # validate
+ sub { 0 + $_[0] }; # from string
goto \&_new_variable;
}
sub new_variable_bool {
- unshift @_, 0, "on,off", sub { $_[0] ? "on" : "off" }, # to string
- sub { $_[0] eq "on" || $_[0] eq "off" }, # validate
- sub { $_[0] eq "on" }; # from string
+ unshift @_, 0, "on,off", 1, sub { $_[0] ? "on" : "off" }, # to string
+ sub { $_[0] eq "on" || $_[0] eq "off" }, # validate
+ sub { $_[0] eq "on" }; # from string
goto \&_new_variable;
}
sub new_variable_string {
- unshift @_, "", "<string>", sub { $_[0] }, # to string
- sub { 1 }, # validate
- sub { $_[0] }; # from string
+ unshift @_, "", "<string>", 0, sub { $_[0] }, # to string
+ sub { 1 }, # validate
+ sub { $_[0] }; # from string
goto \&_new_variable;
}
sub _new_variable {
my $default_default = shift;
my $validsettings = shift;
+ my $takes_on_off = shift;
my $tostring_fn = shift;
my $validate_fn = shift;
my $fromstring_fn = shift;
@@ -472,7 +473,7 @@ sub _new_variable {
};
BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $validsettings,
- $get_tostring_fn, $set_fromstring_fn, undef);
+ $takes_on_off, $get_tostring_fn, $set_fromstring_fn, undef);
}
=head2 quote LIST
View
5 perlglue.xs
@@ -405,11 +405,12 @@ new_command(name, func, summary, usage, description)
MODULE = BarnOwl PACKAGE = BarnOwl::Internal
void
-new_variable(name, summary, description, validsettings, get_tostring_fn, set_fromstring_fn, data)
+new_variable(name, summary, description, validsettings, takes_on_off, get_tostring_fn, set_fromstring_fn, data)
const char *name
const char *summary
const char *description
const char *validsettings
+ int takes_on_off
SV *get_tostring_fn
SV *set_fromstring_fn
SV *data
@@ -425,7 +426,7 @@ new_variable(name, summary, description, validsettings, get_tostring_fn, set_fro
}
owl_variable_dict_newvar_other(owl_global_get_vardict(&g),
- name, summary, description, validsettings,
+ name, summary, description, validsettings, takes_on_off,
perl_closure_new(get_tostring_fn, data, false),
perl_closure_new(set_fromstring_fn, data, false));
}
View
4 variable.c
@@ -618,6 +618,7 @@ int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable_init_params *v
for (init_params = variables_to_init; init_params->name; init_params++) {
newvar = g_new0(owl_variable, 1);
newvar->type = init_params->type;
+ newvar->takes_on_off = (newvar->type == OWL_VARIABLE_BOOL);
/* strdup all the strings so we can delete them consistently. */
newvar->name = g_strdup(init_params->name);
newvar->summary = g_strdup(init_params->summary);
@@ -679,13 +680,14 @@ void owl_variable_dict_add_variable(owl_vardict * vardict,
owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
}
-void owl_variable_dict_newvar_other(owl_vardict *vd, const char *name, const char *summary, const char *description, const char *validsettings, GClosure *get_tostring_fn, GClosure *set_fromstring_fn)
+void owl_variable_dict_newvar_other(owl_vardict *vd, const char *name, const char *summary, const char *description, const char *validsettings, bool takes_on_off, GClosure *get_tostring_fn, GClosure *set_fromstring_fn)
{
owl_variable *var = g_new0(owl_variable, 1);
var->name = g_strdup(name);
var->summary = g_strdup(summary);
var->description = g_strdup(description);
var->validsettings = g_strdup(validsettings);
+ var->takes_on_off = takes_on_off;
var->get_tostring_fn = g_closure_ref(get_tostring_fn);
g_closure_sink(get_tostring_fn);
Please sign in to comment.
Something went wrong with that request. Please try again.