Changeset f3ac1ae
- Timestamp:
- Feb 23, 2013, 7:29:16 PM (12 years ago)
- Parents:
- bbe7d4a (diff), 104a4eb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 3 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
Makefile.am
rc266281 r353719a 39 39 40 40 CODELIST_SRCS=message.c mainwin.c popwin.c zephyr.c messagelist.c \ 41 commands.c global.c text.c fmtext.c editwin.c util.c logging.c \ 41 commands.c global.c text.c fmtext.c editwin.c \ 42 util.c logging.c \ 42 43 perlconfig.c keys.c functions.c zwrite.c viewwin.c help.c filter.c \ 43 44 regex.c history.c view.c dict.c variable.c filterelement.c pair.c \ … … 45 46 aim.c buddy.c buddylist.c style.c errqueue.c \ 46 47 zbuddylist.c popexec.c select.c wcwidth.c \ 47 mainpanel.c msgwin.c sepbar.c editcontext.c signal.c 48 mainpanel.c msgwin.c sepbar.c editcontext.c signal.c closures.c 48 49 49 50 NORMAL_SRCS = filterproc.c window.c windowcb.c … … 51 52 BASE_SRCS = $(CODELIST_SRCS) $(NORMAL_SRCS) 52 53 53 GEN_C = varstubs.c perlglue.c 54 GEN_H = owl_prototypes.h 54 GEN_C = varstubs.c perlglue.c gmarshal_funcs.c 55 GEN_H = owl_prototypes.h gmarshal_funcs.h 55 56 56 57 BUILT_SOURCES = $(GEN_C) $(GEN_H) … … 74 75 $(AM_V_GEN)perl $< $(sort $(filter-out $<,$+)) > $@ 75 76 77 gmarshal_funcs.h: marshal_types 78 glib-genmarshal --header $< > $@ 79 gmarshal_funcs.c: marshal_types 80 glib-genmarshal --body $< > $@ 81 76 82 # For emacs flymake-mode 77 83 check-syntax: proto -
commands.c
r8258ea5 r3b9ca71 1604 1604 if (v == NULL) { 1605 1605 if (!silent) owl_function_error("Unknown variable '%s'", var); 1606 } else if (requirebool && owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {1606 } else if (requirebool && !v->takes_on_off) { 1607 1607 if (!silent) owl_function_error("Variable '%s' is not a boolean", var); 1608 1608 } else { … … 1633 1633 if (v == NULL) { 1634 1634 if (!silent) owl_function_error("Unknown variable '%s'", var); 1635 } else if ( owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {1635 } else if (!v->takes_on_off) { 1636 1636 if (!silent) owl_function_error("Variable '%s' is not a boolean", var); 1637 1637 } else { -
configure.ac
r58b6ce5 r06e04a9 127 127 AX_PROG_PERL_MODULES([PAR],, 128 128 [AC_MSG_WARN([PAR.pm not found. Loadable modules will be disabled.])]) 129 AX_PROG_PERL_MODULES([ExtUtils::Depends],, 130 [AC_MSG_ERROR([cannot find perl module ExtUtils::Depends])]) 129 131 130 132 dnl Add CFLAGS and LIBS for glib-2.0 … … 139 141 prefix="${ac_default_prefix}" 140 142 fi 143 144 dnl Add CFLAGS for glib-perl 145 GLIB_PERL_CFLAGS=`perl -MExtUtils::Depends -e 'my $e = ExtUtils::Depends->new("BarnOwl","Glib"); my %h = $e->get_makefile_vars; print $h{"INC"}'` 146 AC_MSG_NOTICE([Adding glib-perl CFLAGS ${GLIB_PERL_CFLAGS}]) 147 AM_CFLAGS="${GLIB_PERL_CFLAGS} ${AM_CFLAGS}" 141 148 142 149 dnl Checks for typedefs, structures, and compiler characteristics. -
owl.h
rc42a8d1 rbbc31e4 235 235 char *name; 236 236 int type; /* OWL_VARIABLE_* */ 237 void *pval_default; /* for types other and string */ 238 int ival_default; /* for types int and bool */ 239 const char *validsettings; /* documentation of valid settings */ 237 char *default_str; /* the default value as a string */ 238 char *validsettings; /* documentation of valid settings */ 240 239 char *summary; /* summary of usage */ 241 240 char *description; /* detailed description */ 242 void *val; /* current value */ 243 int (*validate_fn)(const struct _owl_variable *v, const void *newval); 241 bool takes_on_off; /* allow passing on/off in argument-less set/unset */ 242 GClosure *set_fromstring_fn; 243 /* sets the variable to a value 244 * of the appropriate type. 245 * unless documented, this 246 * should make a copy. 247 * returns 0 on success. */ 248 GClosure *get_tostring_fn; 249 /* converts val to a string; 250 * caller must free the result */ 251 252 /* These are only valid for OWL_VARIABLE_{INT,BOOL,STRING} */ 253 GValue val; /* current value, if default get_fn/set_fn */ 254 255 GCallback get_fn; 256 /* returns a reference to the current value. 257 * WARNING: this approach is hard to make 258 * thread-safe... */ 259 GCallback validate_fn; 244 260 /* returns 1 if newval is valid */ 245 int (*set_fn)(struct _owl_variable *v, const void *newval);261 GCallback set_fn; 246 262 /* sets the variable to a value 247 * of the appropriate type. 248 * unless documented, this 249 * should make a copy. 250 * returns 0 on success. */ 251 int (*set_fromstring_fn)(struct _owl_variable *v, const char *newval); 252 /* sets the variable to a value 253 * of the appropriate type. 254 * unless documented, this 255 * should make a copy. 256 * returns 0 on success. */ 257 const void *(*get_fn)(const struct _owl_variable *v); 258 /* returns a reference to the current value. 259 * WARNING: this approach is hard to make 260 * thread-safe... */ 261 CALLER_OWN char *(*get_tostring_fn)(const struct _owl_variable *v, const void *val); 262 /* converts val to a string; 263 * caller must free the result */ 264 void (*delete_fn)(struct _owl_variable *v); 265 /* frees val as needed */ 263 * of the appropriate type. 264 * unless documented, this 265 * should make a copy. 266 * returns 0 on success. */ 266 267 } owl_variable; 268 267 269 268 270 typedef struct _owl_input { … … 614 616 #endif 615 617 618 /* We have to dynamically bind these ourselves */ 619 extern gboolean (*gvalue_from_sv) (GValue * value, SV * sv); 620 extern SV * (*sv_from_gvalue) (const GValue * value); 621 extern GClosure * (*perl_closure_new) (SV * callback, SV * data, gboolean swap); 622 623 616 624 #endif /* INC_BARNOWL_OWL_H */ -
perl/lib/BarnOwl.pm
r374089a r104a4eb 18 18 new_command 19 19 new_variable_int new_variable_bool new_variable_string 20 new_variable_enum 20 21 quote redisplay); 21 22 our %EXPORT_TAGS = (all => \@EXPORT_OK); … … 402 403 =head2 new_variable_string NAME [{ARGS}] 403 404 404 Add a new owl variable, either an int, a bool, or a string, with the 405 =head2 new_variable_enum NAME [{ARGS}] 406 407 Add a new owl variable, either an int, a bool, a string, or an enum with the 405 408 specified name. 406 409 407 ARGS can optionally contain the following keys: 410 For new_variable_enum, ARGS is required to contain a validsettings key pointing 411 to an array reference. For all four, it can optionally contain the following 412 keys: 408 413 409 414 =over 4 … … 423 428 =back 424 429 430 In addition, new_variable_string optionally accepts a string validsettings 431 parameter, in case people want to set it to "<path>". 432 425 433 =cut 426 434 427 435 sub new_variable_int { 428 unshift @_, \&BarnOwl::Internal::new_variable_int, 0; 429 goto \&_new_variable; 436 my ($name, $args) = @_; 437 my $storage = defined($args->{default}) ? $args->{default} : 0; 438 BarnOwl::new_variable_full($name, { 439 %{$args}, 440 get_tostring => sub { "$storage" }, 441 set_fromstring => sub { 442 die "Expected integer" unless $_[0] =~ /^-?[0-9]+$/; 443 $storage = 0 + $_[0]; 444 }, 445 validsettings => "<int>", 446 takes_on_off => 0, 447 }); 430 448 } 431 449 432 450 sub new_variable_bool { 433 unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; 434 goto \&_new_variable; 451 my ($name, $args) = @_; 452 my $storage = defined($args->{default}) ? $args->{default} : 0; 453 BarnOwl::new_variable_full($name, { 454 %{$args}, 455 get_tostring => sub { $storage ? "on" : "off" }, 456 set_fromstring => sub { 457 die "Valid settings are on/off" unless $_[0] eq "on" || $_[0] eq "off"; 458 $storage = $_[0] eq "on"; 459 }, 460 validsettings => "on,off", 461 takes_on_off => 1, 462 }); 435 463 } 436 464 437 465 sub new_variable_string { 438 unshift @_, \&BarnOwl::Internal::new_variable_string, ""; 439 goto \&_new_variable; 440 } 441 442 sub _new_variable { 443 my $func = shift; 444 my $default_default = shift; 466 my ($name, $args) = @_; 467 my $storage = defined($args->{default}) ? $args->{default} : ""; 468 BarnOwl::new_variable_full($name, { 469 # Allow people to override this one if they /reaaally/ want to for 470 # some reason. Though we still reserve the right to interpret this 471 # value in interesting ways for tab-completion purposes. 472 validsettings => "<string>", 473 %{$args}, 474 get_tostring => sub { $storage }, 475 set_fromstring => sub { $storage = $_[0]; }, 476 takes_on_off => 0, 477 }); 478 } 479 480 sub new_variable_enum { 481 my ($name, $args) = @_; 482 483 # Gather the valid settings. 484 die "validsettings is required" unless defined($args->{validsettings}); 485 my %valid; 486 map { $valid{$_} = 1 } @{$args->{validsettings}}; 487 488 my $storage = (defined($args->{default}) ? 489 $args->{default} : 490 $args->{validsettings}->[0]); 491 BarnOwl::new_variable_full($name, { 492 %{$args}, 493 get_tostring => sub { $storage }, 494 set_fromstring => sub { 495 die "Invalid input" unless $valid{$_[0]}; 496 $storage = $_[0]; 497 }, 498 validsettings => join(",", @{$args->{validsettings}}) 499 }); 500 } 501 502 =head2 new_variable_full NAME {ARGS} 503 504 Create a variable, in full generality. The keyword arguments have types below: 505 506 get_tostring : () -> string 507 set_fromstring : string -> int 508 -- optional -- 509 summary : string 510 description : string 511 validsettings : string 512 takes_on_off : int 513 514 The get/set functions are required. Note that the caller manages storage for the 515 variable. get_tostring/set_fromstring both convert AND store the value. 516 set_fromstring dies on failure. 517 518 If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set 519 takes_on_off to 1. This makes :set VAR and :unset VAR work. set_fromstring will 520 be called with those arguments. 521 522 =cut 523 524 sub new_variable_full { 445 525 my $name = shift; 446 526 my $args = shift || {}; 447 527 my %args = ( 448 summary 528 summary => "", 449 529 description => "", 450 default => $default_default, 530 takes_on_off => 0, 531 validsettings => "<string>", 451 532 %{$args}); 452 $func->($name, $args{default}, $args{summary}, $args{description}); 533 534 die "get_tostring required" unless $args{get_tostring}; 535 die "set_fromstring required" unless $args{set_fromstring}; 536 537 # Strip off the bogus dummy argument. Aargh perl-Glib. 538 my $get_tostring_fn = sub { $args{get_tostring}->() }; 539 my $set_fromstring_fn = sub { 540 my ($dummy, $val) = @_; 541 # Translate from user-supplied die-on-failure callback to expected 542 # non-zero on error. Less of a nuisance than interacting with ERRSV. 543 eval { $args{set_fromstring}->($val) }; 544 # TODO: Consider changing B::I::new_variable to expect string|NULL with 545 # string as the error message. That can then be translated to a GError in 546 # owl_variable_set_fromstring. For now the string is ignored. 547 return ($@ ? -1 : 0); 548 }; 549 550 BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $args{validsettings}, 551 $args{takes_on_off}, $get_tostring_fn, $set_fromstring_fn, undef); 453 552 } 454 553 -
perlconfig.c
r6401db3 re5210c9 331 331 char *err; 332 332 const char *args[4] = {"", "-e", "0;", NULL}; 333 const char *dlerr; 333 334 AV *inc; 334 335 char *path; … … 388 389 g_free(path); 389 390 391 /* Load up perl-Glib. */ 392 eval_pv("use Glib;", FALSE); 393 394 /* Now, before BarnOwl tries to use them, get the relevant function pointers out. */ 395 dlerr = owl_closure_init(); 396 if (dlerr) { 397 return g_strdup(dlerr); 398 } 399 400 /* And now it's safe to import BarnOwl. */ 390 401 eval_pv("use BarnOwl;", FALSE); 391 402 -
perlglue.xs
r374089a r3b9ca71 402 402 } 403 403 404 void 405 new_variable_string(name, ival, summ, desc) 406 const char * name 407 const char * ival 408 const char * summ 409 const char * desc 410 CODE: 411 owl_variable_dict_newvar_string(owl_global_get_vardict(&g), 412 name, 413 summ, 414 desc, 415 ival); 416 417 void 418 new_variable_int(name, ival, summ, desc) 419 const char * name 420 int ival 421 const char * summ 422 const char * desc 423 CODE: 424 owl_variable_dict_newvar_int(owl_global_get_vardict(&g), 425 name, 426 summ, 427 desc, 428 ival); 429 430 void 431 new_variable_bool(name, ival, summ, desc) 432 const char * name 433 int ival 434 const char * summ 435 const char * desc 436 CODE: 437 owl_variable_dict_newvar_bool(owl_global_get_vardict(&g), 438 name, 439 summ, 440 desc, 441 ival); 404 405 MODULE = BarnOwl PACKAGE = BarnOwl::Internal 406 407 void 408 new_variable(name, summary, description, validsettings, takes_on_off, get_tostring_fn, set_fromstring_fn, data) 409 const char *name 410 const char *summary 411 const char *description 412 const char *validsettings 413 int takes_on_off 414 SV *get_tostring_fn 415 SV *set_fromstring_fn 416 SV *data 417 CODE: 418 { 419 /* data is somewhat redundant given we can create closures, but oh 420 * well. Might be convenient sometimes. */ 421 if(!SV_IS_CODEREF(get_tostring_fn)) { 422 croak("To-string function must be a coderef!"); 423 } 424 if(!SV_IS_CODEREF(set_fromstring_fn)) { 425 croak("From-string function must be a coderef!"); 426 } 427 428 owl_variable_dict_newvar_other(owl_global_get_vardict(&g), 429 name, summary, description, validsettings, takes_on_off, 430 perl_closure_new(get_tostring_fn, data, false), 431 perl_closure_new(set_fromstring_fn, data, false)); 432 } 442 433 443 434 void -
tester.c
r6a20996 r6a8b519 337 337 int numfailed=0; 338 338 char *value; 339 const void *v;340 339 341 340 printf("# BEGIN testing owl_variable\n"); 342 FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));341 owl_variable_dict_setup(&vd); 343 342 344 343 FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping"))); … … 377 376 FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var)); 378 377 379 owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval"); 378 FAIL_UNLESS("get enum var", NULL != (var = owl_variable_get_var(&vd, "scrollmode"))); 379 FAIL_UNLESS("get enum", OWL_SCROLLMODE_NORMAL == owl_variable_get_int(var)); 380 FAIL_UNLESS("get enum as string", 381 !strcmp((value = owl_variable_get_tostring(var)), "normal")); 382 g_free(value); 383 FAIL_UNLESS("set enum 1", 0 == owl_variable_set_int(var, OWL_SCROLLMODE_TOP)); 384 FAIL_UNLESS("get enum 1", OWL_SCROLLMODE_TOP == owl_variable_get_int(var)); 385 FAIL_UNLESS("set enum 2a", -1 == owl_variable_set_int(var, -1)); 386 FAIL_UNLESS("set enum 2b", -1 == owl_variable_set_int(var, OWL_SCROLLMODE_PAGEDCENTER + 1)); 387 FAIL_UNLESS("get enum 2", OWL_SCROLLMODE_TOP == owl_variable_get_int(var)); 388 FAIL_UNLESS("set enum 3", 0 == owl_variable_set_fromstring(var, "center", 0)); 389 FAIL_UNLESS("get enum 4", OWL_SCROLLMODE_CENTER == owl_variable_get_int(var)); 390 FAIL_UNLESS("set enum 5", -1 == owl_variable_set_fromstring(var, "bogus", 0)); 391 FAIL_UNLESS("set enum 6", -1 == owl_variable_set_fromstring(var, "", 0)); 392 FAIL_UNLESS("get enum 7", OWL_SCROLLMODE_CENTER == owl_variable_get_int(var)); 393 394 owl_variable_dict_newvar_string(&vd, "stringvar", "testval", "", ""); 380 395 FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar"))); 381 FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));382 396 FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var))); 383 397 owl_variable_set_string(var, "new val"); 384 398 FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var))); 385 399 386 owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);400 owl_variable_dict_newvar_int(&vd, "intvar", 47, "", ""); 387 401 FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar"))); 388 FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));389 402 FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var)); 390 403 owl_variable_set_int(var, 17); 391 404 FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var)); 392 405 393 owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);406 owl_variable_dict_newvar_bool(&vd, "boolvar", true, "", ""); 394 407 FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar"))); 395 FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));396 408 FAIL_UNLESS("get new bool val", owl_variable_get_bool(var)); 397 409 owl_variable_set_bool_off(var); 398 410 FAIL_UNLESS("update bool val", !owl_variable_get_bool(var)); 399 411 400 owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL);412 owl_variable_dict_newvar_string(&vd, "nullstringvar", NULL, "", ""); 401 413 FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar"))); 402 414 FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var))); … … 405 417 FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var))); 406 418 g_free(value); 419 420 owl_variable_dict_newvar_enum(&vd, "enumvar", 0, "", "", "a,b,c,d"); 421 FAIL_UNLESS("get new enum var", NULL != (var = owl_variable_get_var(&vd, "enumvar"))); 422 FAIL_UNLESS("get new enum val", 0 == owl_variable_get_int(var)); 423 owl_variable_set_fromstring(var, "c", 0); 424 FAIL_UNLESS("update enum val", 2 == owl_variable_get_int(var)); 407 425 408 426 owl_variable_dict_cleanup(&vd); -
variable.c
r8258ea5 r6a8b519 1 1 #include "owl.h" 2 2 #include <stdio.h> 3 4 #define OWLVAR_BOOL(name,default,summary,description) \ 5 { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \ 6 NULL, NULL, NULL, NULL, NULL, NULL } 7 8 #define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \ 9 { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \ 10 validate, set, NULL, get, NULL, NULL } 11 12 #define OWLVAR_INT(name,default,summary,description) \ 13 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, "<int>", g_strdup(summary), g_strdup(description), NULL, \ 14 NULL, NULL, NULL, NULL, NULL, NULL } 3 #include "gmarshal_funcs.h" 4 5 /* TODO(davidben): When we can require 2.30 and up, remove this. */ 6 #ifndef G_VALUE_INIT 7 #define G_VALUE_INIT { 0, { { 0 } } } 8 #endif 9 10 typedef const char *(*get_string_t)(const owl_variable *); 11 typedef int (*get_int_t)(const owl_variable *); 12 typedef bool (*get_bool_t)(const owl_variable *); 13 14 typedef int (*set_string_t)(owl_variable *, const char *); 15 typedef int (*set_int_t)(owl_variable *, int); 16 typedef int (*set_bool_t)(owl_variable *, bool); 17 18 typedef int (*validate_string_t)(const owl_variable *, const char *); 19 typedef int (*validate_int_t)(const owl_variable *, int); 20 typedef int (*validate_bool_t)(const owl_variable *, bool); 21 22 static void owl_variable_dict_newvar_bool_full(owl_vardict *vd, 23 const char *name, 24 bool default_val, 25 const char *summary, 26 const char *description, 27 validate_bool_t validate_fn, 28 set_bool_t set_fn, 29 get_bool_t get_fn); 30 31 static void owl_variable_dict_newvar_string_full(owl_vardict *vd, 32 const char *name, 33 const char *default_val, 34 const char *summary, 35 const char *description, 36 const char *validsettings, 37 validate_string_t validate_fn, 38 set_string_t set_fn, 39 get_string_t get_fn); 40 41 static void owl_variable_dict_newvar_int_full(owl_vardict *vd, 42 const char *name, 43 int default_val, 44 const char *summary, 45 const char *description, 46 const char *validsettings, 47 validate_int_t validate_fn, 48 set_int_t set_fn, 49 get_int_t get_fn); 50 51 static void owl_variable_dict_newvar_enum_full(owl_vardict *vd, 52 const char *name, 53 int default_val, 54 const char *summary, 55 const char *description, 56 const char *validsettings, 57 validate_int_t validate_fn, 58 set_int_t set_fn, 59 get_int_t get_fn); 60 61 #define OWLVAR_BOOL(name, default, summary, description) \ 62 owl_variable_dict_newvar_bool(vd, name, default, summary, description) 63 64 #define OWLVAR_BOOL_FULL(name, default, summary, description, validate, set, get) \ 65 owl_variable_dict_newvar_bool_full(vd, name, default, summary, description, \ 66 validate, set, get) 67 68 #define OWLVAR_INT(name, default, summary, description) \ 69 owl_variable_dict_newvar_int(vd, name, default, summary, description) 15 70 16 71 #define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \ 17 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \ 18 validate, set, NULL, get, NULL, NULL } 19 20 #define OWLVAR_PATH(name,default,summary,description) \ 21 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<path>", g_strdup(summary), g_strdup(description), NULL, \ 22 NULL, NULL, NULL, NULL, NULL, NULL } 23 24 #define OWLVAR_STRING(name,default,summary,description) \ 25 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<string>", g_strdup(summary), g_strdup(description), NULL, \ 26 NULL, NULL, NULL, NULL, NULL, NULL } 27 28 #define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \ 29 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, validset, g_strdup(summary), g_strdup(description), NULL, \ 30 validate, set, NULL, get, NULL, NULL } 72 owl_variable_dict_newvar_int_full(vd, name, default, summary, description, \ 73 validset, validate, set, get) 74 75 #define OWLVAR_PATH(name, default, summary, description) \ 76 owl_variable_dict_newvar_path(vd, name, default, summary, description) 77 78 #define OWLVAR_STRING(name, default, summary, description) \ 79 owl_variable_dict_newvar_string(vd, name, default, summary, description) 80 81 #define OWLVAR_STRING_FULL(name, default, validset, summary, description, validate, set, get) \ 82 owl_variable_dict_newvar_string_full(vd, name, default, summary, description, \ 83 validset, validate, set, get) 31 84 32 85 /* enums are really integers, but where validset is a comma-separated 33 86 * list of strings which can be specified. The tokens, starting at 0, 34 87 * correspond to the values that may be specified. */ 35 #define OWLVAR_ENUM(name,default,summary,description,validset) \ 36 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \ 37 owl_variable_enum_validate, \ 38 NULL, owl_variable_enum_set_fromstring, \ 39 NULL, owl_variable_enum_get_tostring, \ 40 NULL } 88 #define OWLVAR_ENUM(name, default, summary, description, validset) \ 89 owl_variable_dict_newvar_enum(vd, name, default, summary, description, validset) 41 90 42 91 #define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \ 43 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \ 44 validate, \ 45 set, owl_variable_enum_set_fromstring, \ 46 get, owl_variable_enum_get_tostring, \ 47 NULL } 48 49 int owl_variable_add_defaults(owl_vardict *vd) 50 { 51 owl_variable variables_to_init[] = { 52 92 owl_variable_dict_newvar_enum_full(vd, name, default, summary, description, \ 93 validset, validate, set, get) 94 95 void owl_variable_add_defaults(owl_vardict *vd) 96 { 53 97 OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off", 54 98 "ring the terminal bell when personal messages are received", 55 99 "Can be set to 'on', 'off', or the name of a filter which\n" 56 "messages need to match in order to ring the bell") ,100 "messages need to match in order to ring the bell"); 57 101 58 102 OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1, 59 "enable / disable the terminal bell", "" ) ,103 "enable / disable the terminal bell", "" ); 60 104 61 105 OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG, … … 63 107 "If set to 'on', debugging messages are logged to the\n" 64 108 "file specified by the debugfile variable.\n", 65 NULL, owl_variable_debug_set, NULL) ,109 NULL, owl_variable_debug_set, NULL); 66 110 67 111 OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1, 68 "send a login message when BarnOwl starts", "" ) ,112 "send a login message when BarnOwl starts", "" ); 69 113 70 114 OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1, 71 "send a logout message when BarnOwl exits", "" ) ,115 "send a logout message when BarnOwl exits", "" ); 72 116 73 117 OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0, 74 "display received pings", "" ) ,118 "display received pings", "" ); 75 119 76 120 OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1, 77 "send pings", "" ) ,121 "send pings", "" ); 78 122 79 123 OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0, 80 "disable printing information in the separator bar", "" ) ,124 "disable printing information in the separator bar", "" ); 81 125 82 126 OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1, 83 "strip kerberos instance for reply", "") ,127 "strip kerberos instance for reply", ""); 84 128 85 129 OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1, 86 "strip leading and trailing newlines", "") ,130 "strip leading and trailing newlines", ""); 87 131 88 132 OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1, 89 "display outgoing messages", "" ) ,133 "display outgoing messages", "" ); 90 134 91 135 OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1, 92 "load logins from .anyone on startup", "" ) ,136 "load logins from .anyone on startup", "" ); 93 137 94 138 OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0, … … 97 141 "logged in the directory specified\n" 98 142 "by the 'logpath' variable. The filename in that\n" 99 "directory is derived from the sender of the message.\n" ) ,143 "directory is derived from the sender of the message.\n" ); 100 144 101 145 OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0, … … 105 149 "by the 'classlogpath' variable.\n" 106 150 "The filename in that directory is derived from\n" 107 "the name of the class to which the message was sent.\n" ) ,151 "the name of the class to which the message was sent.\n" ); 108 152 109 153 OWLVAR_ENUM( "loggingdirection" /* %OwlVarStub */, OWL_LOGGING_DIRECTION_BOTH, … … 114 158 "is selected both incoming and outgoing messages are\n" 115 159 "logged.", 116 "both,in,out") ,160 "both,in,out"); 117 161 118 162 OWLVAR_BOOL_FULL( "colorztext" /* %OwlVarStub */, 1, 119 163 "allow @color() in zephyrs to change color", 120 NULL, NULL, owl_variable_colorztext_set, NULL) ,164 NULL, NULL, owl_variable_colorztext_set, NULL); 121 165 122 166 OWLVAR_BOOL( "fancylines" /* %OwlVarStub */, 1, … … 124 168 "If turned off, dashes, pipes and pluses will be used\n" 125 169 "to draw lines on the screen. Useful when the terminal\n" 126 "is causing problems" ) ,170 "is causing problems" ); 127 171 128 172 OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1, 129 173 "Do automatic zcrypt processing", 130 "" ) ,174 "" ); 131 175 132 176 OWLVAR_BOOL_FULL( "pseudologins" /* %OwlVarStub */, 0, … … 136 180 "but sent no login message, or a user is not present that sent no\n" 137 181 "logout message, a pseudo login or logout message will be created\n", 138 NULL, owl_variable_pseudologins_set, NULL) ,182 NULL, owl_variable_pseudologins_set, NULL); 139 183 140 184 OWLVAR_BOOL( "ignorelogins" /* %OwlVarStub */, 0, … … 142 186 "When this is enabled, BarnOwl will print login and logout notifications\n" 143 187 "for AIM, zephyr, or other protocols. If disabled BarnOwl will not print\n" 144 "login or logout notifications.\n") ,188 "login or logout notifications.\n"); 145 189 146 190 OWLVAR_STRING( "logfilter" /* %OwlVarStub */, "", … … 151 195 "variables like logging, classlogging, loglogins, loggingdirection,\n" 152 196 "etc. If you want this variable to control all logging, make sure\n" 153 "all other logging variables are in their default state.\n") ,197 "all other logging variables are in their default state.\n"); 154 198 155 199 OWLVAR_BOOL( "loglogins" /* %OwlVarStub */, 0, … … 157 201 "When this is enabled, BarnOwl will log login and logout notifications\n" 158 202 "for AIM, zephyr, or other protocols. If disabled BarnOwl will not print\n" 159 "login or logout notifications.\n") ,203 "login or logout notifications.\n"); 160 204 161 205 OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1, … … 169 213 "in the editmulti keymap.\n", 170 214 "off,middle,on", 171 NULL, owl_variable_disable_ctrl_d_set, NULL) ,215 NULL, owl_variable_disable_ctrl_d_set, NULL); 172 216 173 217 OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people", 174 218 "path for logging personal zephyrs", 175 219 "Specifies a directory which must exist.\n" 176 "Files will be created in the directory for each sender.\n") ,220 "Files will be created in the directory for each sender.\n"); 177 221 178 222 OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class", 179 223 "path for logging class zephyrs", 180 224 "Specifies a directory which must exist.\n" 181 "Files will be created in the directory for each class.\n") ,225 "Files will be created in the directory for each class.\n"); 182 226 183 227 OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE, 184 228 "path for logging debug messages when debugging is enabled", 185 229 "This file will be logged to if 'debug' is set to 'on'.\n" 186 "BarnOwl will append a dot and the current process's pid to the filename.") ,230 "BarnOwl will append a dot and the current process's pid to the filename."); 187 231 188 232 OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL, … … 192 236 "See the documentation for 'zsig' for more information about\n" 193 237 "how the outgoing zsig is chosen." 194 ) ,238 ); 195 239 196 240 OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL, … … 198 242 "The named program will be run when BarnOwl receives new\n" 199 243 "messages. It will not be run again until the first\n" 200 "instance exits") ,244 "instance exits"); 201 245 202 246 OWLVAR_STRING( "zsender" /* %OwlVarStub */, "", … … 205 249 "zephyrs. If this is unset, it will use your Kerberos\n" 206 250 "principal. Note that customizing the sender name will\n" 207 "cause your zephyrs to be sent unauthenticated.") ,251 "cause your zephyrs to be sent unauthenticated."); 208 252 209 253 OWLVAR_STRING( "zsigfunc" /* %OwlVarStub */, "BarnOwl::default_zephyr_signature()", … … 212 256 "explicit zsig. The default setting implements the policy\n" 213 257 "described in the documentation for the 'zsig' variable.\n" 214 "See also BarnOwl::random_zephyr_signature().\n") ,258 "See also BarnOwl::random_zephyr_signature().\n"); 215 259 216 260 OWLVAR_STRING( "zsig" /* %OwlVarStub */, "", … … 219 263 "unset, 'zsigproc' will be run to generate a zsig. If that is\n" 220 264 "also unset, the 'zwrite-signature' zephyr variable will be\n" 221 "used instead.\n") ,265 "used instead.\n"); 222 266 223 267 OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "", … … 225 269 "The sepbar is the bar separating the top and bottom\n" 226 270 "of the BarnOwl screen. Any string specified here will\n" 227 "be displayed on the right of the sepbar\n") ,271 "be displayed on the right of the sepbar\n"); 228 272 229 273 OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0, 230 "turn zaway on or off", "" ) ,274 "turn zaway on or off", "" ); 231 275 232 276 OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 233 277 OWL_DEFAULT_ZAWAYMSG, 234 "zaway msg for responding to zephyrs when away", "" ) ,278 "zaway msg for responding to zephyrs when away", "" ); 235 279 236 280 OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 237 281 OWL_DEFAULT_ZAWAYMSG, 238 "default zaway message", "" ) ,282 "default zaway message", "" ); 239 283 240 284 OWLVAR_BOOL_FULL( "aaway" /* %OwlVarStub */, 0, 241 285 "Set AIM away status", 242 286 "", 243 NULL, owl_variable_aaway_set, NULL) ,287 NULL, owl_variable_aaway_set, NULL); 244 288 245 289 OWLVAR_STRING( "aaway_msg" /* %OwlVarStub */, 246 290 OWL_DEFAULT_AAWAYMSG, 247 "AIM away msg for responding when away", "" ) ,291 "AIM away msg for responding when away", "" ); 248 292 249 293 OWLVAR_STRING( "aaway_msg_default" /* %OwlVarStub */, 250 294 OWL_DEFAULT_AAWAYMSG, 251 "default AIM away message", "" ) ,295 "default AIM away message", "" ); 252 296 253 297 OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all", 254 298 "home view to switch to after 'X' and 'V'", 255 "SEE ALSO: view, filter\n" ) ,299 "SEE ALSO: view, filter\n" ); 256 300 257 301 OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none", 258 302 "filter on which to trigger alert actions", 259 "" ) ,303 "" ); 260 304 261 305 OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop", 262 306 "BarnOwl command to execute for alert actions", 263 "" ) ,307 "" ); 264 308 265 309 OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "<string>", "tty name for zephyr location", "", 266 NULL, owl_variable_tty_set, NULL) ,310 NULL, owl_variable_tty_set, NULL); 267 311 268 312 OWLVAR_STRING( "default_style" /* %OwlVarStub */, "default", … … 275 319 " perl - legacy perl interface\n" 276 320 "\nSEE ALSO: style, show styles, view -s <style>\n" 277 ) ,321 ); 278 322 279 323 … … 282 326 "This specifies the maximum number of columns for M-q to fill text\n" 283 327 "to. If set to 0, M-q will wrap to the width of the window, and\n" 284 "values less than 0 disable M-q entirely.\n") ,328 "values less than 0 disable M-q entirely.\n"); 285 329 286 330 OWLVAR_INT( "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70, … … 291 335 "\n" 292 336 "As a courtesy to recipients, it is recommended that outgoing\n" 293 "Zephyr messages be no wider than 70 columns.\n") ,337 "Zephyr messages be no wider than 70 columns.\n"); 294 338 295 339 OWLVAR_INT( "aim_ignorelogin_timer" /* %OwlVarStub */, 15, … … 298 342 "AIM login before allowing the receipt of AIM login notifications.\n" 299 343 "By default this is set to 15. If you would like to view login\n" 300 "notifications of buddies as soon as you login, set it to 0 instead.") ,344 "notifications of buddies as soon as you login, set it to 0 instead."); 301 345 302 346 … … 311 355 owl_variable_typewinsize_set, 312 356 NULL /* use default for get */ 313 ) ,357 ); 314 358 315 359 OWLVAR_INT( "typewindelta" /* %OwlVarStub */, 0, … … 321 365 "typewinsize to 1.\n\n" 322 366 "This works a lot better with a non-default scrollmode;\n" 323 "try :set scrollmode pagedcenter.\n") ,367 "try :set scrollmode pagedcenter.\n"); 324 368 325 369 OWLVAR_ENUM( "scrollmode" /* %OwlVarStub */, OWL_SCROLLMODE_NORMAL, … … 350 394 " the screen will be paged up or down and\n" 351 395 " the cursor will be near the center.\n", 352 "normal,top,neartop,center,paged,pagedcenter" ) ,396 "normal,top,neartop,center,paged,pagedcenter" ); 353 397 354 398 OWLVAR_BOOL( "narrow-related" /* %OwlVarStub:narrow_related */, 1, … … 358 402 "for Zephyr, this controls whether to narrow to e.g. class-help or\n" 359 403 "class-help.d alone, or to related-class-help, which includes\n" 360 "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" ) ,404 "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" ); 361 405 362 406 OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0, … … 365 409 "continue to follow the last message if this is set.\n" 366 410 "Note that this is currently risky as you might accidentally\n" 367 "delete a message right as it came in.\n" ) ,411 "delete a message right as it came in.\n" ); 368 412 369 413 OWLVAR_STRING_FULL( "default_exposure" /* %OwlVarStub */, "", … … 374 418 "~/.zephyr.vars.\n" 375 419 "See the description of exposure for the values this can be.", 376 NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get ) ,420 NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get ); 377 421 378 422 OWLVAR_STRING_FULL( "exposure" /* %OwlVarStub */, "", … … 430 474 " personal subscriptions will be entered for the\n" 431 475 " user.\n", 432 NULL, owl_variable_exposure_set, NULL /* use default for get */ ), 433 434 /* This MUST be last... */ 435 { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL, 436 NULL, NULL, NULL, NULL, NULL, NULL } 437 438 }; 439 440 int ret = owl_variable_dict_add_from_list(vd, variables_to_init); 441 owl_variable *var; 442 for (var = variables_to_init; var->name != NULL; var++) 443 owl_variable_cleanup(var); 444 return ret; 476 NULL, owl_variable_exposure_set, NULL /* use default for get */ ); 445 477 } 446 478 … … 452 484 /* commonly useful */ 453 485 454 int owl_variable_int_validate_gt0(const owl_variable *v, const void *newval) 455 { 456 if (newval == NULL) return(0); 457 else if (*(const int*)newval < 1) return(0); 458 else return (1); 459 } 460 461 int owl_variable_int_validate_positive(const owl_variable *v, const void *newval) 462 { 463 if (newval == NULL) return(0); 464 else if (*(const int*)newval < 0) return(0); 465 else return (1); 486 int owl_variable_int_validate_gt0(const owl_variable *v, int newval) 487 { 488 return !(newval < 1); 489 } 490 491 int owl_variable_int_validate_positive(const owl_variable *v, int newval) 492 { 493 return !(newval < 0); 466 494 } 467 495 468 496 /* typewinsize */ 469 int owl_variable_typewinsize_set(owl_variable *v, const void *newval)497 int owl_variable_typewinsize_set(owl_variable *v, int newval) 470 498 { 471 499 int rv; … … 476 504 477 505 /* debug (cache value in g->debug) */ 478 int owl_variable_debug_set(owl_variable *v, const void *newval) 479 { 480 if (newval && (*(const int*)newval == 1 || *(const int*)newval == 0)) { 481 g.debug = *(const int*)newval; 506 int owl_variable_debug_set(owl_variable *v, bool newval) 507 { 508 g.debug = newval; 509 return owl_variable_bool_set_default(v, newval); 510 } 511 512 /* When 'aaway' is changed, need to notify the AIM server */ 513 int owl_variable_aaway_set(owl_variable *v, bool newval) 514 { 515 if (newval) { 516 owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g)); 517 } else { 518 owl_aim_set_awaymsg(""); 482 519 } 483 520 return owl_variable_bool_set_default(v, newval); 484 521 } 485 522 486 /* When 'aaway' is changed, need to notify the AIM server */ 487 int owl_variable_aaway_set(owl_variable *v, const void *newval) 488 { 489 if (newval) { 490 if (*(const int*)newval == 1) { 491 owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g)); 492 } else if (*(const int*)newval == 0) { 493 owl_aim_set_awaymsg(""); 494 } 495 } 496 return owl_variable_bool_set_default(v, newval); 497 } 498 499 int owl_variable_colorztext_set(owl_variable *v, const void *newval) 523 int owl_variable_colorztext_set(owl_variable *v, bool newval) 500 524 { 501 525 int ret = owl_variable_bool_set_default(v, newval); … … 510 534 } 511 535 512 int owl_variable_pseudologins_set(owl_variable *v, const void *newval)536 int owl_variable_pseudologins_set(owl_variable *v, bool newval) 513 537 { 514 538 static guint timer = 0; 515 539 if (newval) { 516 if (*(const int*)newval == 1) { 517 owl_function_zephyr_buddy_check(0); 518 if (timer == 0) { 519 timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL); 520 } 521 } else { 522 if (timer != 0) { 523 g_source_remove(timer); 524 timer = 0; 525 } 540 owl_function_zephyr_buddy_check(0); 541 if (timer == 0) { 542 timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL); 543 } 544 } else { 545 if (timer != 0) { 546 g_source_remove(timer); 547 timer = 0; 526 548 } 527 549 } … … 531 553 /* note that changing the value of this will clobber 532 554 * any user setting of this */ 533 int owl_variable_disable_ctrl_d_set(owl_variable *v, const void *newval)534 { 535 if ( newval &&!owl_context_is_startup(owl_global_get_context(&g))) {536 if ( *(const int*)newval == 2) {555 int owl_variable_disable_ctrl_d_set(owl_variable *v, int newval) 556 { 557 if (!owl_context_is_startup(owl_global_get_context(&g))) { 558 if (newval == 2) { 537 559 owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char"); 538 } else if ( *(const int*)newval == 1) {560 } else if (newval == 1) { 539 561 owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete"); 540 562 } else { … … 542 564 } 543 565 } 544 return owl_variable_int_set_default(v, newval); 545 } 546 547 int owl_variable_tty_set(owl_variable *v, const void*newval)566 return owl_variable_int_set_default(v, newval); 567 } 568 569 int owl_variable_tty_set(owl_variable *v, const char *newval) 548 570 { 549 571 owl_zephyr_set_locationinfo(g_get_host_name(), newval); 550 return (owl_variable_string_set_default(v, newval));551 } 552 553 int owl_variable_default_exposure_set(owl_variable *v, const void*newval)572 return owl_variable_string_set_default(v, newval); 573 } 574 575 int owl_variable_default_exposure_set(owl_variable *v, const char *newval) 554 576 { 555 577 return owl_zephyr_set_default_exposure(newval); 556 578 } 557 579 558 const void*owl_variable_default_exposure_get(const owl_variable *v)580 const char *owl_variable_default_exposure_get(const owl_variable *v) 559 581 { 560 582 return owl_zephyr_get_default_exposure(); 561 583 } 562 584 563 int owl_variable_exposure_set(owl_variable *v, const void*newval)585 int owl_variable_exposure_set(owl_variable *v, const char *newval) 564 586 { 565 587 int ret = owl_zephyr_set_exposure(newval); … … 573 595 /**************************************************************************/ 574 596 575 intowl_variable_dict_setup(owl_vardict *vd) {597 void owl_variable_dict_setup(owl_vardict *vd) { 576 598 owl_dict_create(vd); 577 return owl_variable_add_defaults(vd); 578 } 579 580 int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable *variables_to_init) 581 { 582 owl_variable *var, *cur; 583 for (var = variables_to_init; var->name != NULL; var++) { 584 cur = g_new(owl_variable, 1); 585 *cur = *var; 586 /* strdup all the strings so we can delete them consistently. */ 587 cur->name = g_strdup(var->name); 588 cur->summary = g_strdup(var->summary); 589 cur->description = g_strdup(var->description); 590 switch (cur->type) { 591 case OWL_VARIABLE_OTHER: 592 cur->set_fn(cur, cur->pval_default); 593 break; 594 case OWL_VARIABLE_STRING: 595 if (!cur->validate_fn) 596 cur->validate_fn = owl_variable_string_validate_default; 597 if (!cur->set_fn) 598 cur->set_fn = owl_variable_string_set_default; 599 if (!cur->set_fromstring_fn) 600 cur->set_fromstring_fn = owl_variable_string_set_fromstring_default; 601 if (!cur->get_fn) 602 cur->get_fn = owl_variable_get_default; 603 if (!cur->get_tostring_fn) 604 cur->get_tostring_fn = owl_variable_string_get_tostring_default; 605 if (!cur->delete_fn) 606 cur->delete_fn = owl_variable_delete_default; 607 cur->pval_default = g_strdup(var->pval_default); 608 cur->set_fn(cur, cur->pval_default); 609 break; 610 case OWL_VARIABLE_BOOL: 611 if (!cur->validate_fn) 612 cur->validate_fn = owl_variable_bool_validate_default; 613 if (!cur->set_fn) 614 cur->set_fn = owl_variable_bool_set_default; 615 if (!cur->set_fromstring_fn) 616 cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default; 617 if (!cur->get_fn) 618 cur->get_fn = owl_variable_get_default; 619 if (!cur->get_tostring_fn) 620 cur->get_tostring_fn = owl_variable_bool_get_tostring_default; 621 if (!cur->delete_fn) 622 cur->delete_fn = owl_variable_delete_default; 623 cur->val = g_new(int, 1); 624 cur->set_fn(cur, &cur->ival_default); 625 break; 626 case OWL_VARIABLE_INT: 627 if (!cur->validate_fn) 628 cur->validate_fn = owl_variable_int_validate_default; 629 if (!cur->set_fn) 630 cur->set_fn = owl_variable_int_set_default; 631 if (!cur->set_fromstring_fn) 632 cur->set_fromstring_fn = owl_variable_int_set_fromstring_default; 633 if (!cur->get_fn) 634 cur->get_fn = owl_variable_get_default; 635 if (!cur->get_tostring_fn) 636 cur->get_tostring_fn = owl_variable_int_get_tostring_default; 637 if (!cur->delete_fn) 638 cur->delete_fn = owl_variable_delete_default; 639 cur->val = g_new(int, 1); 640 cur->set_fn(cur, &cur->ival_default); 641 break; 642 default: 643 fprintf(stderr, "owl_variable_setup: invalid variable type\n"); 644 return(-2); 645 } 646 owl_dict_insert_element(vd, cur->name, cur, NULL); 647 } 648 return 0; 599 owl_variable_add_defaults(vd); 600 } 601 602 CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v, 603 GCallback fn, 604 GClosureMarshal marshal) { 605 GClosure *closure = g_cclosure_new_swap(fn, v, NULL); 606 g_closure_set_marshal(closure,marshal); 607 g_closure_ref(closure); 608 g_closure_sink(closure); 609 return closure; 649 610 } 650 611 … … 654 615 } 655 616 656 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description) 657 { 658 owl_variable * var = g_new0(owl_variable, 1);617 static owl_variable *owl_variable_newvar(int type, const char *name, const char *summary, const char *description, const char *validsettings) { 618 owl_variable *var = g_new0(owl_variable, 1); 619 var->type = type; 659 620 var->name = g_strdup(name); 660 621 var->summary = g_strdup(summary); 661 622 var->description = g_strdup(description); 623 var->validsettings = g_strdup(validsettings); 662 624 return var; 663 625 } 664 626 665 void owl_variable_update(owl_variable *var, const char *summary, const char *desc) { 666 g_free(var->summary); 667 var->summary = g_strdup(summary); 668 g_free(var->description); 669 var->description = g_strdup(desc); 670 } 671 672 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval) 673 { 674 owl_variable *old = owl_variable_get_var(vd, name); 675 if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) { 676 owl_variable_update(old, summ, desc); 677 g_free(old->pval_default); 678 old->pval_default = g_strdup(initval); 679 } else { 680 owl_variable * var = owl_variable_newvar(name, summ, desc); 681 var->type = OWL_VARIABLE_STRING; 682 var->validsettings = "<string>"; 683 var->pval_default = g_strdup(initval); 684 var->set_fn = owl_variable_string_set_default; 685 var->set_fromstring_fn = owl_variable_string_set_fromstring_default; 686 var->get_fn = owl_variable_get_default; 687 var->get_tostring_fn = owl_variable_string_get_tostring_default; 688 var->delete_fn = owl_variable_delete_default; 689 var->set_fn(var, initval); 690 owl_variable_dict_add_variable(vd, var); 691 } 692 } 693 694 void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval) 695 { 696 owl_variable *old = owl_variable_get_var(vd, name); 697 if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) { 698 owl_variable_update(old, summ, desc); 699 old->ival_default = initval; 700 } else { 701 owl_variable * var = owl_variable_newvar(name, summ, desc); 702 var->type = OWL_VARIABLE_INT; 703 var->validsettings = "<int>"; 704 var->ival_default = initval; 705 var->validate_fn = owl_variable_int_validate_default; 706 var->set_fn = owl_variable_int_set_default; 707 var->set_fromstring_fn = owl_variable_int_set_fromstring_default; 708 var->get_fn = owl_variable_get_default; 709 var->get_tostring_fn = owl_variable_int_get_tostring_default; 710 var->delete_fn = owl_variable_delete_default; 711 var->val = g_new(int, 1); 712 var->set_fn(var, &initval); 713 owl_variable_dict_add_variable(vd, var); 714 } 715 } 716 717 void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval) 718 { 719 owl_variable *old = owl_variable_get_var(vd, name); 720 if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) { 721 owl_variable_update(old, summ, desc); 722 old->ival_default = initval; 723 } else { 724 owl_variable * var = owl_variable_newvar(name, summ, desc); 725 var->type = OWL_VARIABLE_BOOL; 726 var->validsettings = "on,off"; 727 var->ival_default = initval; 728 var->validate_fn = owl_variable_bool_validate_default; 729 var->set_fn = owl_variable_bool_set_default; 730 var->set_fromstring_fn = owl_variable_bool_set_fromstring_default; 731 var->get_fn = owl_variable_get_default; 732 var->get_tostring_fn = owl_variable_bool_get_tostring_default; 733 var->delete_fn = owl_variable_delete_default; 734 var->val = g_new(int, 1); 735 var->set_fn(var, &initval); 736 owl_variable_dict_add_variable(vd, var); 737 } 627 static void owl_variable_dict_newvar_int_full(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validsettings, validate_int_t validate_fn, set_int_t set_fn, get_int_t get_fn) 628 { 629 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary, 630 description, validsettings); 631 var->takes_on_off = false; 632 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default); 633 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default); 634 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_int_validate_default); 635 636 var->get_tostring_fn = owl_variable_make_closure( 637 var, G_CALLBACK(owl_variable_int_get_tostring_default), 638 g_cclosure_user_marshal_STRING__VOID); 639 var->set_fromstring_fn = owl_variable_make_closure( 640 var, G_CALLBACK(owl_variable_int_set_fromstring_default), 641 g_cclosure_user_marshal_INT__STRING); 642 643 g_value_init(&var->val, G_TYPE_INT); 644 owl_variable_set_int(var, default_val); 645 646 var->default_str = owl_variable_get_tostring(var); 647 owl_variable_dict_add_variable(vd, var); 648 } 649 650 void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description) { 651 owl_variable_dict_newvar_int_full(vd, name, default_val, summary, description, 652 "<int>", NULL, NULL, NULL); 653 } 654 655 static void owl_variable_dict_newvar_bool_full(owl_vardict *vd, const char *name, bool default_val, const char *summary, const char *description, validate_bool_t validate_fn, set_bool_t set_fn, get_bool_t get_fn) 656 { 657 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_BOOL, name, summary, 658 description, "on,off"); 659 var->takes_on_off = true; 660 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_bool_get_default); 661 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_bool_set_default); 662 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_bool_validate_default); 663 664 var->get_tostring_fn = owl_variable_make_closure( 665 var, G_CALLBACK(owl_variable_bool_get_tostring_default), 666 g_cclosure_user_marshal_STRING__VOID); 667 var->set_fromstring_fn = owl_variable_make_closure( 668 var, G_CALLBACK(owl_variable_bool_set_fromstring_default), 669 g_cclosure_user_marshal_INT__STRING); 670 671 g_value_init(&var->val, G_TYPE_BOOLEAN); 672 owl_variable_set_bool(var, default_val); 673 674 var->default_str = owl_variable_get_tostring(var); 675 owl_variable_dict_add_variable(vd, var); 676 } 677 678 void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, bool default_val, const char *summary, const char *description) { 679 owl_variable_dict_newvar_bool_full(vd, name, default_val, summary, description, 680 NULL, NULL, NULL); 681 } 682 683 static void owl_variable_dict_newvar_string_full(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description, const char *validsettings, validate_string_t validate_fn, set_string_t set_fn, get_string_t get_fn) 684 { 685 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_STRING, name, summary, 686 description, validsettings); 687 var->takes_on_off = false; 688 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_string_get_default); 689 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_string_set_default); 690 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_string_validate_default); 691 692 var->get_tostring_fn = owl_variable_make_closure( 693 var, G_CALLBACK(owl_variable_string_get_tostring_default), 694 g_cclosure_user_marshal_STRING__VOID); 695 var->set_fromstring_fn = owl_variable_make_closure( 696 var, G_CALLBACK(owl_variable_string_set_fromstring_default), 697 g_cclosure_user_marshal_INT__STRING); 698 699 g_value_init(&var->val, G_TYPE_STRING); 700 owl_variable_set_string(var, default_val); 701 702 var->default_str = owl_variable_get_tostring(var); 703 owl_variable_dict_add_variable(vd, var); 704 } 705 706 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) { 707 owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description, 708 "<string>", NULL, NULL, NULL); 709 } 710 711 void owl_variable_dict_newvar_path(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) { 712 owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description, 713 "<path>", NULL, NULL, NULL); 714 } 715 716 static void owl_variable_dict_newvar_enum_full(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validsettings, validate_int_t validate_fn, set_int_t set_fn, get_int_t get_fn) 717 { 718 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary, 719 description, validsettings); 720 var->takes_on_off = false; 721 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default); 722 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default); 723 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_enum_validate); 724 725 var->get_tostring_fn = owl_variable_make_closure( 726 var, G_CALLBACK(owl_variable_enum_get_tostring), 727 g_cclosure_user_marshal_STRING__VOID); 728 var->set_fromstring_fn = owl_variable_make_closure( 729 var, G_CALLBACK(owl_variable_enum_set_fromstring), 730 g_cclosure_user_marshal_INT__STRING); 731 732 g_value_init(&var->val, G_TYPE_INT); 733 owl_variable_set_int(var, default_val); 734 735 var->default_str = owl_variable_get_tostring(var); 736 owl_variable_dict_add_variable(vd, var); 737 } 738 739 void owl_variable_dict_newvar_enum(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validset) { 740 owl_variable_dict_newvar_enum_full(vd, name, default_val, summary, description, 741 validset, NULL, NULL, NULL); 742 } 743 744 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) 745 { 746 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_OTHER, name, summary, 747 description, validsettings); 748 var->takes_on_off = takes_on_off; 749 750 var->get_tostring_fn = g_closure_ref(get_tostring_fn); 751 g_closure_sink(get_tostring_fn); 752 753 var->set_fromstring_fn = g_closure_ref(set_fromstring_fn); 754 g_closure_sink(set_fromstring_fn); 755 756 var->default_str = owl_variable_get_tostring(var); 757 758 /* Note: this'll overwrite any existing variable of that name, even a C one, 759 but it's consistent with previous behavior and commands. */ 760 owl_variable_dict_add_variable(vd, var); 738 761 } 739 762 … … 747 770 } 748 771 749 void owl_variable_cleanup(owl_variable *v) 750 { 751 if (v->delete_fn) v->delete_fn(v); 772 void owl_variable_delete(owl_variable *v) 773 { 752 774 g_free(v->name); 753 775 g_free(v->summary); 754 776 g_free(v->description); 755 if (v->type == OWL_VARIABLE_STRING) 756 g_free(v->pval_default); 757 } 758 759 void owl_variable_delete(owl_variable *v) 760 { 761 owl_variable_cleanup(v); 777 g_free(v->default_str); 778 g_free(v->validsettings); 779 g_value_unset(&(v->val)); 780 g_closure_unref(v->get_tostring_fn); 781 g_closure_unref(v->set_fromstring_fn); 782 762 783 g_free(v); 763 784 } … … 785 806 return v->type; 786 807 } 787 788 /* functions for getting and setting variable values */789 808 790 809 /* returns 0 on success, prints a status msg if msg is true */ 791 810 int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) { 792 811 char *tostring; 793 if (!v->set_fromstring_fn) { 794 if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v)); 795 return -1; 796 } 797 if (0 != v->set_fromstring_fn(v, value)) { 812 GValue values[] = {G_VALUE_INIT, G_VALUE_INIT}; 813 GValue return_box = G_VALUE_INIT; 814 int set_successfully; 815 816 g_value_init(&values[0], G_TYPE_POINTER); 817 g_value_set_pointer(&values[0], NULL); 818 g_value_init(&values[1], G_TYPE_STRING); 819 g_value_set_static_string(&values[1], value); 820 g_value_init(&return_box, G_TYPE_INT); 821 g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL); 822 823 set_successfully = g_value_get_int(&return_box); 824 if (0 != set_successfully) { 798 825 if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v), 799 826 owl_variable_get_validsettings(v)); 800 return -1; 827 } else if (msg) { 828 tostring = owl_variable_get_tostring(v); 829 if (tostring) { 830 owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring); 831 } else { 832 owl_function_makemsg("%s = <null>", owl_variable_get_name(v)); 833 } 834 g_free(tostring); 801 835 } 802 if (msg) { 803 tostring = v->get_tostring_fn(v, v->get_fn(v)); 804 if (tostring) 805 owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring); 806 else 807 owl_function_makemsg("%s = <null>", owl_variable_get_name(v)); 808 g_free(tostring); 809 } 810 return 0; 836 837 g_value_unset(&return_box); 838 g_value_unset(&values[1]); 839 g_value_unset(&values[0]); 840 return set_successfully; 811 841 } 812 842 813 843 int owl_variable_set_string(owl_variable *v, const char *newval) 814 844 { 815 if (v->type != OWL_VARIABLE_STRING) return -1; 816 return v->set_fn(v, newval); 817 } 818 845 g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1); 846 847 set_string_t cb = (set_string_t) v->set_fn; 848 return cb(v, newval); 849 } 850 819 851 int owl_variable_set_int(owl_variable *v, int newval) 820 852 { 821 if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1; 822 return v->set_fn(v, &newval); 823 } 824 853 g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1); 854 855 set_int_t cb = (set_int_t) v->set_fn; 856 return cb(v, newval); 857 } 858 859 int owl_variable_set_bool(owl_variable *v, bool newval) { 860 g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1); 861 862 set_bool_t cb = (set_bool_t) v->set_fn; 863 return cb(v, newval); 864 } 865 825 866 int owl_variable_set_bool_on(owl_variable *v) 826 867 { 827 868 if (v->type != OWL_VARIABLE_BOOL) return -1; 828 return owl_variable_set_ int(v, true);869 return owl_variable_set_bool(v, true); 829 870 } 830 871 … … 832 873 { 833 874 if (v->type != OWL_VARIABLE_BOOL) return -1; 834 return owl_variable_set_ int(v, false);875 return owl_variable_set_bool(v, false); 835 876 } 836 877 837 878 CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v) 838 879 { 839 return v->get_tostring_fn(v, v->get_fn(v)); 840 } 841 842 CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v) 843 { 844 if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { 845 return v->get_tostring_fn(v, &(v->ival_default)); 846 } else { 847 return v->get_tostring_fn(v, v->pval_default); 848 } 880 GValue instance = G_VALUE_INIT; 881 GValue tostring_box = G_VALUE_INIT; 882 char *ret = NULL; 883 884 g_value_init(&instance, G_TYPE_POINTER); 885 g_value_set_pointer(&instance, NULL); 886 g_value_init(&tostring_box, G_TYPE_STRING); 887 g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL); 888 889 ret = g_value_dup_string(&tostring_box); 890 891 g_value_unset(&tostring_box); 892 g_value_unset(&instance); 893 return ret; 894 } 895 896 const char *owl_variable_get_default_tostring(const owl_variable *v) 897 { 898 return v->default_str; 849 899 } 850 900 … … 854 904 } 855 905 856 /* returns a reference */857 const void *owl_variable_get(const owl_variable *v)858 {859 return v->get_fn(v);860 }861 862 906 const char *owl_variable_get_string(const owl_variable *v) 863 907 { 864 if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) { 865 owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v)); 866 return NULL; 867 } 868 return owl_variable_get(v); 869 } 870 871 /* returns a reference */ 872 const void *owl_variable_get_other(const owl_variable *v) 873 { 874 if (owl_variable_get_type(v) != OWL_VARIABLE_OTHER) { 875 owl_function_error("Variable '%s' is not type other.", owl_variable_get_name(v)); 876 return NULL; 877 } 878 return owl_variable_get(v); 908 g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL); 909 910 get_string_t cb = (get_string_t) v->get_fn; 911 return cb(v); 879 912 } 880 913 881 914 int owl_variable_get_int(const owl_variable *v) 882 915 { 883 if (owl_variable_get_type(v) != OWL_VARIABLE_INT) { 884 owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v)); 885 return -1; 886 } 887 const int *pi = owl_variable_get(v); 888 if (!pi) return -1; 889 return *pi; 890 } 891 892 int owl_variable_get_bool(const owl_variable *v) 893 { 894 if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) { 895 owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v)); 896 return -1; 897 } 898 const int *pi = owl_variable_get(v); 899 if (!pi) return -1; 900 return *pi; 916 g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0); 917 918 get_int_t cb = (get_int_t) v->get_fn; 919 return cb(v); 920 } 921 922 bool owl_variable_get_bool(const owl_variable *v) 923 { 924 g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE); 925 926 get_bool_t cb = (get_bool_t) v->get_fn; 927 return cb(v); 901 928 } 902 929 903 930 void owl_variable_describe(const owl_variable *v, owl_fmtext *fm) 904 931 { 905 c har *tostring= owl_variable_get_default_tostring(v);932 const char *default_str = owl_variable_get_default_tostring(v); 906 933 char *default_buf; 907 934 908 if ( tostring)909 default_buf = g_strdup_printf("'%s'", tostring);935 if (default_str) 936 default_buf = g_strdup_printf("'%s'", default_str); 910 937 else 911 938 default_buf = g_strdup("<null>"); … … 914 941 owl_variable_get_summary(v), default_buf); 915 942 g_free(default_buf); 916 g_free(tostring);917 943 } 918 944 919 945 void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) { 920 946 char *tostring; 947 const char *default_str; 921 948 922 949 owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n"); … … 933 960 owl_fmtext_append_normal(fm, "\n\n"); 934 961 935 936 tostring = owl_variable_get_default_tostring(v); 962 default_str = owl_variable_get_default_tostring(v); 937 963 owl_fmtext_append_normal(fm, "Default: "); 938 owl_fmtext_append_normal(fm, ( tostring ? tostring: "<null>"));964 owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>")); 939 965 owl_fmtext_append_normal(fm, "\n\n"); 940 966 … … 948 974 owl_fmtext_append_normal(fm, "\n\n"); 949 975 } 950 g_free(tostring);951 976 } 952 977 … … 960 985 /* default common functions */ 961 986 962 const void *owl_variable_get_default(const owl_variable *v) { 963 return v->val; 964 } 965 966 void owl_variable_delete_default(owl_variable *v) 967 { 968 g_free(v->val); 987 const char *owl_variable_string_get_default(const owl_variable *v) { 988 return g_value_get_string(&(v->val)); 989 } 990 991 int owl_variable_int_get_default(const owl_variable *v) { 992 return g_value_get_int(&(v->val)); 993 } 994 995 bool owl_variable_bool_get_default(const owl_variable *v) { 996 return g_value_get_boolean(&(v->val)); 969 997 } 970 998 971 999 /* default functions for booleans */ 972 1000 973 int owl_variable_bool_validate_default(const owl_variable *v, const void *newval) { 974 if (newval == NULL) return(0); 975 else if (*(const int*)newval==1 || *(const int*)newval==0) return(1); 976 else return (0); 977 } 978 979 int owl_variable_bool_set_default(owl_variable *v, const void *newval) { 980 if (v->validate_fn) { 981 if (!v->validate_fn(v, newval)) return(-1); 1001 int owl_variable_bool_validate_default(const owl_variable *v, bool newval) { 1002 return (newval == 1) || (newval == 0); 1003 } 1004 1005 int owl_variable_bool_set_default(owl_variable *v, bool newval) { 1006 if (!((validate_bool_t)v->validate_fn)(v, newval)) 1007 return -1; 1008 1009 g_value_set_boolean(&(v->val), newval); 1010 return(0); 1011 } 1012 1013 int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) { 1014 bool i; 1015 if (!strcmp(newval, "on")) { 1016 i = true; 1017 } else if (!strcmp(newval, "off")) { 1018 i = false; 1019 } else { 1020 return(-1); 982 1021 } 983 *(int*)v->val = *(const int*)newval; 984 return(0); 985 } 986 987 int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval) { 988 int i; 989 if (!strcmp(newval, "on")) i=1; 990 else if (!strcmp(newval, "off")) i=0; 991 else return(-1); 992 return (v->set_fn(v, &i)); 993 } 994 995 CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, const void *val) 996 { 997 if (val == NULL) { 998 return NULL; 999 } else if (*(const int*)val == 0) { 1022 1023 return owl_variable_set_bool(v, i); 1024 } 1025 1026 CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy) 1027 { 1028 bool val = owl_variable_get_bool(v); 1029 if (val == 0) { 1000 1030 return g_strdup("off"); 1001 } else if ( *(const int*)val == 1) {1031 } else if (val == 1) { 1002 1032 return g_strdup("on"); 1003 1033 } else { … … 1008 1038 /* default functions for integers */ 1009 1039 1010 int owl_variable_int_validate_default(const owl_variable *v, const void *newval) {1011 if (newval == NULL) return(0); 1012 elsereturn (1);1013 } 1014 1015 int owl_variable_int_set_default(owl_variable *v, const void *newval) {1016 if ( v->validate_fn) {1017 if (!v->validate_fn(v, newval)) return(-1);1018 } 1019 *(int*)v->val = *(const int*)newval;1040 int owl_variable_int_validate_default(const owl_variable *v, int newval) 1041 { 1042 return (1); 1043 } 1044 1045 int owl_variable_int_set_default(owl_variable *v, int newval) { 1046 if (!((validate_int_t)v->validate_fn)(v, newval)) 1047 return -1; 1048 1049 g_value_set_int(&(v->val), newval); 1020 1050 return(0); 1021 1051 } 1022 1052 1023 int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval ) {1053 int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) { 1024 1054 int i; 1025 1055 char *ep; 1026 1056 i = strtol(newval, &ep, 10); 1027 1057 if (*ep || ep==newval) return(-1); 1028 return (v->set_fn(v, &i)); 1029 } 1030 1031 CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, const void *val) 1032 { 1033 if (val == NULL) { 1034 return NULL; 1035 } else { 1036 return g_strdup_printf("%d", *(const int*)val); 1037 } 1058 return owl_variable_set_int(v, i); 1059 } 1060 1061 CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy) 1062 { 1063 return g_strdup_printf("%d", owl_variable_get_int(v)); 1038 1064 } 1039 1065 1040 1066 /* default functions for enums (a variant of integers) */ 1041 1067 1042 int owl_variable_enum_validate(const owl_variable *v, const void *newval) {1068 int owl_variable_enum_validate(const owl_variable *v, int newval) { 1043 1069 char **enums; 1044 1070 int nenums, val; 1045 if (newval == NULL) return(0);1046 1071 enums = g_strsplit_set(v->validsettings, ",", 0); 1047 1072 nenums = g_strv_length(enums); 1048 1073 g_strfreev(enums); 1049 val = *(const int*)newval;1074 val = newval; 1050 1075 if (val < 0 || val >= nenums) { 1051 1076 return(0); … … 1054 1079 } 1055 1080 1056 int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval ) {1081 int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval, void *dummy) { 1057 1082 char **enums; 1058 1083 int i, val=-1; … … 1066 1091 g_strfreev(enums); 1067 1092 if (val == -1) return(-1); 1068 return (v->set_fn(v, &val));1069 } 1070 1071 CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, const void *val)1093 return owl_variable_set_int(v, val); 1094 } 1095 1096 CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy) 1072 1097 { 1073 1098 char **enums; … … 1075 1100 char *tostring; 1076 1101 1077 if (val == NULL) {1078 return NULL;1079 }1080 1102 enums = g_strsplit_set(v->validsettings, ",", 0); 1081 1103 nenums = g_strv_length(enums); 1082 i = *(const int*)val;1104 i = owl_variable_get_int(v); 1083 1105 if (i<0 || i>=nenums) { 1084 1106 g_strfreev(enums); … … 1092 1114 /* default functions for stringeans */ 1093 1115 1094 int owl_variable_string_validate_default(const struct _owl_variable *v, const void*newval) {1116 int owl_variable_string_validate_default(const owl_variable *v, const char *newval) { 1095 1117 if (newval == NULL) return(0); 1096 1118 else return (1); 1097 1119 } 1098 1120 1099 int owl_variable_string_set_default(owl_variable *v, const void *newval) { 1100 if (v->validate_fn) { 1101 if (!v->validate_fn(v, newval)) return(-1); 1102 } 1103 g_free(v->val); 1104 v->val = g_strdup(newval); 1121 int owl_variable_string_set_default(owl_variable *v, const char *newval) { 1122 if (!((validate_string_t)v->validate_fn)(v, newval)) 1123 return -1; 1124 1125 g_value_set_string(&(v->val), newval); 1105 1126 return(0); 1106 1127 } 1107 1128 1108 int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval) { 1109 return (v->set_fn(v, newval)); 1110 } 1111 1112 CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, const void *val) 1113 { 1114 return g_strdup((const char*)val); 1115 } 1116 1129 int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 1130 { 1131 return owl_variable_set_string(v, newval); 1132 } 1133 1134 CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy) 1135 { 1136 return g_strdup(owl_variable_get_string(v)); 1137 } 1138 -
zephyr.c
r9b3167b rbbe7d4a 291 291 struct stat statbuff; 292 292 293 subs = g_new(ZSubscription_t, subSize);294 293 subsfile = owl_zephyr_dotfile(".zephyr.subs", filename); 295 294 … … 307 306 if (!file) 308 307 return -1; 308 309 subs = g_new(ZSubscription_t, subSize); 309 310 while (owl_getline(&buffer, file)) { 310 311 if (buffer[0] == '#' || buffer[0] == '\n')
Note: See TracChangeset
for help on using the changeset viewer.