Changeset 353719a


Ignore:
Timestamp:
Feb 19, 2013, 8:29:04 PM (5 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master
Children:
5001a3d
Parents:
06e04a9
git-author:
David Benjamin <davidben@mit.edu> (06/09/12 16:07:20)
git-committer:
David Benjamin <davidben@mit.edu> (02/19/13 20:29:04)
Message:
Start of perlvariables iteration N+1

New plan: the C <-> perl interface deals entirely with strings. Storage
is entirely perl-side. C does not know and doesn't care about the
underlying perl-side types. Note that this means an "int" variable in C
land and an "int" variable in perl land have no relation.
Files:
2 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • Makefile.am

    r06e04a9 r353719a  
    3939
    4040CODELIST_SRCS=message.c mainwin.c popwin.c zephyr.c messagelist.c \
    41      commands.c global.c text.c fmtext.c gmarshal_funcs.c editwin.c \
     41     commands.c global.c text.c fmtext.c editwin.c \
    4242     util.c logging.c \
    4343     perlconfig.c keys.c functions.c zwrite.c viewwin.c help.c filter.c \
     
    5252BASE_SRCS = $(CODELIST_SRCS) $(NORMAL_SRCS)
    5353
    54 GEN_C = varstubs.c perlglue.c
    55 GEN_H = owl_prototypes.h
     54GEN_C = varstubs.c perlglue.c gmarshal_funcs.c
     55GEN_H = owl_prototypes.h gmarshal_funcs.h
    5656
    5757BUILT_SOURCES = $(GEN_C) $(GEN_H)
     
    7575        $(AM_V_GEN)perl $< $(sort $(filter-out $<,$+)) > $@
    7676
     77gmarshal_funcs.h: marshal_types
     78        glib-genmarshal --header $< > $@
     79gmarshal_funcs.c: marshal_types
     80        glib-genmarshal --body $< > $@
     81
    7782# For emacs flymake-mode
    7883check-syntax: proto
  • commands.c

    r8258ea5 r353719a  
    16051605    if (!silent) owl_function_error("Unknown variable '%s'", var);
    16061606  } else if (requirebool && owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     1607    // FIXME: The above won't work when we make perl variables OWL_VARIABLE_SV
    16071608    if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
    16081609  } else {
     
    16341635    if (!silent) owl_function_error("Unknown variable '%s'", var);
    16351636  } else if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     1637    // FIXME: The above won't work when we make perl variables OWL_VARIABLE_SV
    16361638    if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
    16371639  } else {
  • marshal_types

    r06e04a9 r353719a  
    1 # set_fn
    2 INT:STRING
    3 INT:INT
    4 INT:BOOL
    5 # get_fn
    6 INT:VOID
    7 BOOL:VOID
    8 STRING:VOID
    9 # validate_fn has same signature as set_fn
    101# set_fromstring_fn
    112INT:STRING
    123# get_tostring_fn
    13 STRING:BOOL
    14 STRING:INT
    15 STRING:STRING
    16 
     4STRING:VOID
  • owl.h

    r06e04a9 r353719a  
    235235  char *name;
    236236  int   type;  /* OWL_VARIABLE_* */
    237   GValue gval_default;
     237  char *default_str;            /* the default value as a string */
    238238  const char *validsettings;    /* documentation of valid settings */
    239239  char *summary;                /* summary of usage */
    240240  char *description;            /* detailed description */
    241241  GValue val;                    /* current value */
    242   GClosure *validate_fn;
    243                                 /* returns 1 if newval is valid */
    244   GClosure *set_fn;
    245                                 /* sets the variable to a value
    246                                  * of the appropriate type.
    247                                  * unless documented, this
    248                                  * should make a copy.
    249                                  * returns 0 on success. */
    250242  GClosure *set_fromstring_fn;
    251243                                /* sets the variable to a value
     
    254246                                 * should make a copy.
    255247                                 * returns 0 on success. */
    256   GClosure *get_fn;
     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  GCallback get_fn;
    257254                                /* returns a reference to the current value.
    258255                                 * WARNING:  this approach is hard to make
    259256                                 * thread-safe... */
    260   GClosure *get_tostring_fn;
    261                                 /* converts val to a string;
    262                                  * caller must free the result */
    263   GClosure *delete_fn;
    264                                 /* frees val as needed */
    265   GClosure *get_default_fn;
    266                                /* return the default value, as set at creation time */
    267  
     257  GCallback validate_fn;
     258                                /* returns 1 if newval is valid */
     259  GCallback set_fn;
     260                                /* sets the variable to a value
     261                                 * of the appropriate type.
     262                                 * unless documented, this
     263                                 * should make a copy.
     264                                 * returns 0 on success. */
    268265} owl_variable;
    269266
    270267typedef struct _owl_variable_init_params {
    271   char *name;
     268  const char *name;
    272269  int   type;  /* OWL_VARIABLE_* */
    273   void *pval_default;  /* for types other and string */
    274   int   ival_default;  /* for types int and bool     */
     270  const char *pval_default;     /* for string */
     271  int   ival_default;           /* for types int and bool     */
    275272  const char *validsettings;    /* documentation of valid settings */
    276   char *summary;                /* summary of usage */
    277   char *description;            /* detailed description */
     273  const char *summary;          /* summary of usage */
     274  const char *description;      /* detailed description */
    278275  void *val;                    /* current value */
    279276  GCallback validate_fn;
     
    298295                                /* converts val to a string;
    299296                                 * caller must free the result */
    300   GCallback delete_fn;
    301                                 /* frees val as needed */
    302   GCallback get_default_fn;
    303                                 /* return the default value as set at creation time */
    304297} owl_variable_init_params;
    305298
  • perlconfig.c

    r06e04a9 r353719a  
    559559  SvREFCNT_dec(v);
    560560}
    561 
    562 void owl_perl_delete_perl_variable(owl_variable *variable, void *data)
    563 {
    564   g_closure_unref(variable->delete_fn);
    565   variable->delete_fn = NULL;
    566   SvREFCNT_dec((SV*)data);
    567   owl_variable_delete(variable);
    568 }
  • perlglue.xs

    r06e04a9 r353719a  
    405405MODULE = BarnOwl                PACKAGE = BarnOwl::Internal
    406406
     407/* TODO: Revise this after churn is done.
    407408void
    408409new_variable_full(name, summary, desc, type, data, default_val, get_fn, get_default_fn, tostring_fn, validate_fn, set_fn, fromstring_fn)
     
    474475        PUTBACK;
    475476}
     477*/
    476478
    477479void
  • tester.c

    r6a20996 r353719a  
    337337  int numfailed=0;
    338338  char *value;
    339   const void *v;
    340339
    341340  printf("# BEGIN testing owl_variable\n");
     
    379378  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
    380379  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)));
    382380  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
    383381  owl_variable_set_string(var, "new val");
     
    386384  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
    387385  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)));
    389386  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
    390387  owl_variable_set_int(var, 17);
     
    393390  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
    394391  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)));
    396392  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
    397393  owl_variable_set_bool_off(var);
  • variable.c

    r06e04a9 r353719a  
    33#include "gmarshal_funcs.h"
    44
    5 static const GType owl_variable_gtype_map[] = {G_TYPE_POINTER,
    6                                                G_TYPE_INT, G_TYPE_BOOLEAN,
    7                                                G_TYPE_STRING };
     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
     10typedef const char *(*get_string_t)(const owl_variable *);
     11typedef int (*get_int_t)(const owl_variable *);
     12typedef gboolean (*get_bool_t)(const owl_variable *);
     13
     14typedef int (*set_string_t)(owl_variable *, const char *);
     15typedef int (*set_int_t)(owl_variable *, int);
     16typedef int (*set_bool_t)(owl_variable *, gboolean);
     17
     18typedef int (*validate_string_t)(owl_variable *, const char *);
     19typedef int (*validate_int_t)(owl_variable *, int);
     20typedef int (*validate_bool_t)(owl_variable *, gboolean);
    821
    922#define OWLVAR_BOOL(name,default,summary,description) \
    10         { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \
    11             NULL, NULL, NULL, NULL, NULL, NULL, NULL }
     23        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \
     24            NULL, NULL, NULL, NULL, NULL }
    1225
    1326#define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \
    14         { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \
    15             G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL, NULL, NULL }
     27        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \
     28            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
    1629
    1730#define OWLVAR_INT(name,default,summary,description) \
    18         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, "<int>", g_strdup(summary), g_strdup(description), NULL, \
    19             NULL, NULL, NULL, NULL, NULL, NULL, NULL }
     31        { name, OWL_VARIABLE_INT, NULL, default, "<int>", summary, description, NULL, \
     32            NULL, NULL, NULL, NULL, NULL }
    2033
    2134#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
    22         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
    23             G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL, NULL, NULL }
     35        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
     36            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
    2437
    2538#define OWLVAR_PATH(name,default,summary,description) \
    26         { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<path>", g_strdup(summary), g_strdup(description),  NULL, \
    27             NULL, NULL, NULL, NULL, NULL, NULL, NULL }
     39        { name, OWL_VARIABLE_STRING, default, 0, "<path>", summary, description,  NULL, \
     40            NULL, NULL, NULL, NULL, NULL }
    2841
    2942#define OWLVAR_STRING(name,default,summary,description) \
    30         { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<string>", g_strdup(summary), g_strdup(description), NULL, \
    31             NULL, NULL, NULL, NULL, NULL, NULL, NULL }
     43        { name, OWL_VARIABLE_STRING, default, 0, "<string>", summary, description, NULL, \
     44            NULL, NULL, NULL, NULL, NULL }
    3245
    3346#define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \
    34         { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, validset, g_strdup(summary), g_strdup(description), NULL, \
    35             G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL, NULL, NULL }
     47        { name, OWL_VARIABLE_STRING, default, 0, validset, summary, description, NULL, \
     48            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
    3649
    3750/* enums are really integers, but where validset is a comma-separated
     
    3952 * correspond to the values that may be specified. */
    4053#define OWLVAR_ENUM(name,default,summary,description,validset) \
    41         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
     54        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
    4255            G_CALLBACK(owl_variable_enum_validate),                     \
    4356            NULL, G_CALLBACK(owl_variable_enum_set_fromstring),         \
    4457            NULL, G_CALLBACK(owl_variable_enum_get_tostring),           \
    45             NULL, NULL }
     58            }
    4659
    4760#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
    48         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
     61        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
    4962            G_CALLBACK(validate),                                       \
    5063            G_CALLBACK(set), G_CALLBACK(owl_variable_enum_set_fromstring), \
    5164            G_CALLBACK(get), G_CALLBACK(owl_variable_enum_get_tostring), \
    52             NULL, NULL }
     65            }
    5366
    5467int owl_variable_add_defaults(owl_vardict *vd)
     
    439452  /* This MUST be last... */
    440453  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
    441     NULL, NULL, NULL, NULL, NULL, NULL }
     454    NULL, NULL, NULL, NULL, NULL }
    442455
    443456  };
    444457
    445   int ret = owl_variable_dict_add_from_list(vd, variables_to_init);
    446   owl_variable_init_params *var;
    447   for (var = variables_to_init; var->name != NULL; var++)
    448     owl_variable_cleanup_initializer(var);
    449   return ret;
     458  return owl_variable_dict_add_from_list(vd, variables_to_init);
    450459}
    451460
     
    457466/* commonly useful */
    458467
    459 int owl_variable_int_validate_gt0(const owl_variable *v, const int newval, void *dummy)
     468int owl_variable_int_validate_gt0(const owl_variable *v, int newval)
    460469{
    461470  return !(newval < 1);
    462471}
    463472
    464 int owl_variable_int_validate_positive(const owl_variable *v, const int newval, void *dummy)
     473int owl_variable_int_validate_positive(const owl_variable *v, int newval)
    465474{
    466475  return !(newval < 0);
     
    468477
    469478/* typewinsize */
    470 int owl_variable_typewinsize_set(owl_variable *v, const int newval, void *dummy)
     479int owl_variable_typewinsize_set(owl_variable *v, int newval)
    471480{
    472481  int rv;
    473   rv = owl_variable_int_set_default(v, newval, dummy);
     482  rv = owl_variable_int_set_default(v, newval);
    474483  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
    475484  return(rv);
     
    477486
    478487/* debug (cache value in g->debug) */
    479 int owl_variable_debug_set(owl_variable *v, const int newval, void *dummy)
     488int owl_variable_debug_set(owl_variable *v, int newval)
    480489{
    481490  if (newval == 1 || newval == 0) {
    482491    g.debug = newval;
    483492  }
    484   return owl_variable_bool_set_default(v, newval, dummy);
     493  return owl_variable_bool_set_default(v, newval);
    485494}
    486495
    487496/* When 'aaway' is changed, need to notify the AIM server */
    488 int owl_variable_aaway_set(owl_variable *v, const gboolean newval, void *dummy)
     497int owl_variable_aaway_set(owl_variable *v, gboolean newval)
    489498{
    490499  if (newval == 1) {
     
    493502    owl_aim_set_awaymsg("");
    494503  }
    495   return owl_variable_bool_set_default(v, newval, dummy);
    496 }
    497 
    498 int owl_variable_colorztext_set(owl_variable *v, const void *newval, void *dummy)
    499 {
    500   int ret = owl_variable_bool_set_default(v, newval, dummy);
     504  return owl_variable_bool_set_default(v, newval);
     505}
     506
     507int owl_variable_colorztext_set(owl_variable *v, const void *newval)
     508{
     509  int ret = owl_variable_bool_set_default(v, newval);
    501510  /* flush the format cache so that we see the update, but only if we're done initializing BarnOwl */
    502511  if (owl_global_get_msglist(&g) != NULL)
     
    509518}
    510519
    511 int owl_variable_pseudologins_set(owl_variable *v, const int newval, void *dummy)
     520int owl_variable_pseudologins_set(owl_variable *v, int newval)
    512521{
    513522  static guint timer = 0;
     
    523532    }
    524533  }
    525   return owl_variable_bool_set_default(v, newval, dummy);
     534  return owl_variable_bool_set_default(v, newval);
    526535}
    527536
    528537/* note that changing the value of this will clobber
    529538 * any user setting of this */
    530 int owl_variable_disable_ctrl_d_set(owl_variable *v, const int newval, void *dummy)
     539int owl_variable_disable_ctrl_d_set(owl_variable *v, int newval)
    531540{
    532541  if (!owl_context_is_startup(owl_global_get_context(&g))) {
     
    539548    }
    540549  } 
    541   return owl_variable_int_set_default(v, newval, dummy); 
    542 }
    543 
    544 int owl_variable_tty_set(owl_variable *v, const char *newval, void *dummy)
     550  return owl_variable_int_set_default(v, newval);
     551}
     552
     553int owl_variable_tty_set(owl_variable *v, const char *newval)
    545554{
    546555  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
    547   return(owl_variable_string_set_default(v, newval, dummy));
    548 }
    549 
    550 int owl_variable_default_exposure_set(owl_variable *v, const char *newval, void *dummy)
     556  return owl_variable_string_set_default(v, newval);
     557}
     558
     559int owl_variable_default_exposure_set(owl_variable *v, const char *newval)
    551560{
    552561  return owl_zephyr_set_default_exposure(newval);
    553562}
    554563
    555 const char *owl_variable_default_exposure_get(const owl_variable *v, void *dummy)
     564const char *owl_variable_default_exposure_get(const owl_variable *v)
    556565{
    557566  return owl_zephyr_get_default_exposure();
    558567}
    559568
    560 int owl_variable_exposure_set(owl_variable *v, const char *newval, void *dummy)
     569int owl_variable_exposure_set(owl_variable *v, const char *newval)
    561570{
    562571  int ret = owl_zephyr_set_exposure(newval);
    563572  if (ret != 0)
    564573    return ret;
    565   return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval), dummy);
     574  return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval));
    566575}
    567576
     
    576585
    577586CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v,
    578                                                       GCallback fn,
    579                                                       GClosureMarshal marshal) {
    580   GClosure *closure = g_cclosure_new(fn, NULL, NULL);
     587                                               GCallback fn,
     588                                               GClosureMarshal marshal) {
     589  GClosure *closure = g_cclosure_new_swap(fn, v, NULL);
    581590  g_closure_set_marshal(closure,marshal);
    582591  g_closure_ref(closure);
     
    594603                                                  marshal_func);        \
    595604  } while(0)
    596  
     605
     606#define DEFAULT_CB(a, b) (a ? a : G_CALLBACK(b))
     607
     608#define SET_CBS(type) \
     609      newvar->get_fn = DEFAULT_CB(init_params->get_fn, owl_variable_##type##_get_default); \
     610      newvar->set_fn = DEFAULT_CB(init_params->set_fn, owl_variable_##type##_set_default); \
     611      newvar->validate_fn = \
     612        DEFAULT_CB(init_params->validate_fn, owl_variable_##type##_validate_default);
    597613
    598614int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable_init_params *variables_to_init)
     
    608624    newvar->description = g_strdup(init_params->description);
    609625    newvar->validsettings = init_params->validsettings;
    610     GValue values[] = {{0}, {0}};
    611     GValue *value = values+1;
    612     GValue ret = {0};
    613626    GCallback fn = NULL;
    614627    switch (init_params->type) {
    615628    case OWL_VARIABLE_STRING:
    616       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn,
    617                               G_CALLBACK(owl_variable_string_get_default),
    618                               g_cclosure_user_marshal_STRING__VOID, fn);
    619       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn,
    620                               G_CALLBACK(owl_variable_string_set_default),
    621                               g_cclosure_user_marshal_INT__STRING, fn);
    622       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn,
    623                               G_CALLBACK(owl_variable_string_validate_default),
    624                               g_cclosure_user_marshal_INT__STRING, fn);
     629      SET_CBS(string);
    625630      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
    626631                              G_CALLBACK(owl_variable_string_set_fromstring_default),
     
    628633      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
    629634                              G_CALLBACK(owl_variable_string_get_tostring_default),
    630                               g_cclosure_user_marshal_STRING__STRING, fn);
    631       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn,
    632                               G_CALLBACK(owl_variable_string_get_default_default),
    633635                              g_cclosure_user_marshal_STRING__VOID, fn);
    634636
    635       g_value_init(value,G_TYPE_STRING);
    636       g_value_set_string(value, init_params->pval_default);
     637      g_value_init(&newvar->val, G_TYPE_STRING);
     638      owl_variable_set_string(newvar, init_params->pval_default);
    637639      break;
    638640    case OWL_VARIABLE_BOOL:
    639       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn,
    640                               G_CALLBACK(owl_variable_bool_get_default),
    641                               g_cclosure_user_marshal_BOOLEAN__VOID, fn);
    642       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn,
    643                               G_CALLBACK(owl_variable_bool_set_default),
    644                               g_cclosure_user_marshal_INT__BOOLEAN, fn);
    645       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn,
    646                               G_CALLBACK(owl_variable_bool_validate_default),
    647                               g_cclosure_user_marshal_INT__BOOLEAN, fn);
     641      SET_CBS(bool);
    648642      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
    649643                              G_CALLBACK(owl_variable_bool_set_fromstring_default),
     
    651645      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
    652646                              G_CALLBACK(owl_variable_bool_get_tostring_default),
    653                               g_cclosure_user_marshal_STRING__BOOLEAN, fn);
    654       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn,
    655                               G_CALLBACK(owl_variable_bool_get_default_default),
    656                               g_cclosure_user_marshal_BOOLEAN__VOID, fn);
    657 
    658       g_value_init(value,G_TYPE_BOOLEAN);
    659       g_value_set_boolean(value, !!(init_params->ival_default));
     647                              g_cclosure_user_marshal_STRING__VOID, fn);
     648
     649      g_value_init(&newvar->val, G_TYPE_BOOLEAN);
     650      owl_variable_set_bool(newvar, !!(init_params->ival_default));
    660651      break;
    661652    case OWL_VARIABLE_INT:
    662       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn,
    663                               G_CALLBACK(owl_variable_int_get_default),
    664                               g_cclosure_user_marshal_INT__VOID, fn);
    665       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn,
    666                               G_CALLBACK(owl_variable_int_set_default),
    667                               g_cclosure_user_marshal_INT__INT, fn);
    668       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn,
    669                               G_CALLBACK(owl_variable_int_validate_default),
    670                               g_cclosure_user_marshal_INT__INT, fn);
     653      SET_CBS(int);
    671654      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
    672655                              G_CALLBACK(owl_variable_int_set_fromstring_default),
     
    674657      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
    675658                              G_CALLBACK(owl_variable_int_get_tostring_default),
    676                               g_cclosure_user_marshal_STRING__INT, fn);
    677       OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn,
    678                               G_CALLBACK(owl_variable_int_get_default_default),
    679                               g_cclosure_user_marshal_INT__VOID, fn);
    680 
    681       g_value_init(value,G_TYPE_INT);
    682       g_value_set_int(value, init_params->ival_default);
     659                              g_cclosure_user_marshal_STRING__VOID, fn);
     660
     661      g_value_init(&newvar->val, G_TYPE_INT);
     662      owl_variable_set_int(newvar, init_params->ival_default);
    683663      break;
    684664    default:
     
    686666      return(-2);
    687667    }
    688     OWL_VARIABLE_SETUP_FUNC(newvar, init_params, delete_fn,
    689                             G_CALLBACK(owl_variable_delete_default),
    690                             g_cclosure_marshal_VOID__VOID, fn);
    691    
    692     g_value_init(&(newvar->gval_default), G_VALUE_TYPE(value));
    693     g_value_init(&(newvar->val), G_VALUE_TYPE(value));
    694     g_value_copy(value, &(newvar->gval_default));
    695     /* we have the value boxed up already *anyway*, so... */
    696     g_value_init(values, G_TYPE_POINTER);
    697     g_value_set_pointer(values, newvar);
    698     g_value_init(&ret, G_TYPE_INT);
    699     g_closure_invoke(newvar->set_fn, &ret, 2, values, NULL);
    700     g_value_unset(value);
     668
     669    /* record the initial value as a string */
     670    newvar->default_str = owl_variable_get_tostring(newvar);
     671
    701672    owl_dict_insert_element(vd, newvar->name, newvar, NULL);
    702673  }
     
    726697
    727698#define OWL_VARIABLE_SETUP_DEFAULT_FUNCS(variable, type, gtype) do { \
    728   variable->set_fn = owl_variable_make_closure(variable, \
    729     G_CALLBACK(owl_variable_##type##_set_default), \
    730     g_cclosure_user_marshal_INT__##gtype); \
    731   variable->get_fn = owl_variable_make_closure(variable, \
    732     G_CALLBACK(owl_variable_##type##_get_default), \
    733     g_cclosure_user_marshal_##gtype##__##VOID); \
    734   variable->validate_fn = owl_variable_make_closure(variable, \
    735     G_CALLBACK(owl_variable_##type##_validate_default), \
    736     g_cclosure_user_marshal_INT__##gtype); \
     699  variable->set_fn = G_CALLBACK(owl_variable_##type##_set_default); \
     700  variable->get_fn = G_CALLBACK(owl_variable_##type##_get_default); \
     701  variable->validate_fn = G_CALLBACK(owl_variable_##type##_validate_default); \
    737702  variable->set_fromstring_fn = owl_variable_make_closure(variable, \
    738703    G_CALLBACK(owl_variable_##type##_set_fromstring_default), \
     
    740705  variable->get_tostring_fn = owl_variable_make_closure(variable, \
    741706    G_CALLBACK(owl_variable_##type##_get_tostring_default), \
    742     g_cclosure_user_marshal_STRING__##gtype); \
    743   variable->get_default_fn = owl_variable_make_closure(variable, \
    744     G_CALLBACK(owl_variable_##type##_get_default_default), \
    745     g_cclosure_user_marshal_##gtype##__##VOID); \
    746   } while(0);
     707    g_cclosure_user_marshal_STRING__VOID); \
     708  } while(0)
    747709
    748710void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
    749711{
    750   GValue default_gvals[] = {{0}, {0}};
    751   GValue *default_gval = default_gvals+1;
    752   GValue retval = {0};
    753   g_value_init(default_gval, G_TYPE_STRING);
    754   g_value_init(&retval, G_TYPE_INT);
    755   g_value_set_static_string(default_gval, initval);
    756712  owl_variable *old = owl_variable_get_var(vd, name);
    757713  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
    758714    owl_variable_update(old, summ, desc);
    759     g_value_copy(default_gval, &(old->gval_default));
     715    /* g_value_copy(default_gval, &(old->gval_default)); */
     716    /* TODO: Resolve this after churn; this function is only called from perl
     717     * anyway. */
    760718  } else {
    761719    owl_variable * var = owl_variable_newvar(name, summ, desc);
    762720    var->type = OWL_VARIABLE_STRING;
    763721    var->validsettings = "<string>";
    764     g_value_init(&(var->val), G_TYPE_STRING);
    765     g_value_init(default_gvals, G_TYPE_POINTER);
    766     g_value_set_pointer(default_gvals, var);
    767     g_value_init(&(var->gval_default), G_TYPE_STRING);
    768     g_value_set_string(&(var->gval_default), initval);
     722    g_value_init(&var->val, G_TYPE_STRING);
    769723    OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, string, STRING);
    770724
    771     g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL);
     725    owl_variable_set_string(var, initval);
     726
     727    /* record the initial value as a string */
     728    var->default_str = owl_variable_get_tostring(var);
     729
    772730    owl_variable_dict_add_variable(vd, var);
    773731  }
     
    776734void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
    777735{
    778   GValue default_gvals[] = {{0}, {0}};
    779   GValue *default_gval = default_gvals+1;
    780   GValue retval = {0};
    781   g_value_init(default_gval, G_TYPE_INT);
    782   g_value_init(&retval, G_TYPE_INT);
    783   g_value_set_int(default_gval, initval);
    784736  owl_variable *old = owl_variable_get_var(vd, name);
    785737  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
    786738    owl_variable_update(old, summ, desc);
    787     g_value_copy(default_gval, &(old->gval_default));
     739    /* g_value_copy(default_gval, &(old->gval_default)); */
     740    /* TODO: Resolve this after churn; this function is only called from perl
     741     * anyway. */
    788742  } else {
    789743    owl_variable * var = owl_variable_newvar(name, summ, desc);
    790744    var->type = OWL_VARIABLE_INT;
    791745    var->validsettings = "<int>";
    792     g_value_init(&(var->val), G_TYPE_INT);
    793     g_value_init(&(var->gval_default), G_TYPE_INT);
     746    g_value_init(&var->val, G_TYPE_INT);
    794747    OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, int, INT);
    795     g_value_init(default_gvals, G_TYPE_POINTER);
    796     g_value_set_pointer(default_gvals, var);
    797     g_value_set_int(&(var->gval_default), initval);
    798     g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL);
    799     /*    g_value_unset(&retval); */
     748
     749    owl_variable_set_int(var, initval);
     750
     751    /* record the initial value as a string */
     752    var->default_str = owl_variable_get_tostring(var);
     753
    800754    owl_variable_dict_add_variable(vd, var);
    801755  }
     
    804758void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, gboolean initval)
    805759{
    806   GValue default_gvals[] = {{0}, {0}};
    807   GValue *default_gval = default_gvals+1;
    808   GValue retval = {0};
    809   g_value_init(default_gval, G_TYPE_BOOLEAN);
    810   g_value_init(&retval, G_TYPE_INT);
    811   g_value_set_boolean(default_gval, initval);
    812760  owl_variable *old = owl_variable_get_var(vd, name);
    813761  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
    814762    owl_variable_update(old, summ, desc);
    815     g_value_copy(default_gval, &(old->gval_default));
     763    /* g_value_copy(default_gval, &(old->gval_default)); */
     764    /* TODO: Resolve this after churn; this function is only called from perl
     765     * anyway. */
    816766  } else {
    817767    owl_variable * var = owl_variable_newvar(name, summ, desc);
    818768    var->type = OWL_VARIABLE_BOOL;
    819769    var->validsettings = "on,off";
    820     g_value_init(&(var->val), G_TYPE_BOOLEAN);
    821     g_value_init(&(var->gval_default), G_TYPE_BOOLEAN);
     770    g_value_init(&var->val, G_TYPE_BOOLEAN);
    822771    OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, bool, BOOLEAN);
    823772
    824     g_value_init(default_gvals, G_TYPE_POINTER);
    825     g_value_set_pointer(default_gvals, var);
    826     g_value_set_int(&(var->gval_default), initval);
    827     g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL); 
     773    owl_variable_set_bool(var, initval);
     774
     775    /* record the initial value as a string */
     776    var->default_str = owl_variable_get_tostring(var);
     777
    828778    owl_variable_dict_add_variable(vd, var);
    829779  }
     
    841791void owl_variable_cleanup(owl_variable *v)
    842792{
    843   GValue val = {0};
    844   if (v->delete_fn) {
    845     g_value_init(&val, G_TYPE_POINTER);
    846     g_value_set_pointer(&val, v);
    847     g_closure_invoke(v->delete_fn, NULL, 1, &val, NULL);
    848   }
    849793  g_free(v->name);
    850794  g_free(v->summary);
    851795  g_free(v->description);
    852   if (v->type == OWL_VARIABLE_STRING) {
    853     g_value_unset(&v->gval_default);
    854   }
    855   g_closure_unref(v->get_fn);
    856   g_closure_unref(v->set_fn);
    857   g_closure_unref(v->validate_fn);
     796  g_free(v->default_str);
     797  g_value_unset(&(v->val));
    858798  g_closure_unref(v->get_tostring_fn);
    859799  g_closure_unref(v->set_fromstring_fn);
    860800}
    861  
    862 void owl_variable_cleanup_initializer(owl_variable_init_params *v) {
    863   g_free(v->name);
    864   g_free(v->summary);
    865   g_free(v->description);
    866   if (v->type == OWL_VARIABLE_STRING)
    867     g_free(v->pval_default);
    868  }
    869801
    870802void owl_variable_delete(owl_variable *v)
     
    895827{
    896828  return v->type;
    897 }
    898 
    899 /* functions for getting and setting variable values */
    900 
    901 static CALLER_OWN  char *owl_variable_invoke_tostring(const owl_variable *v,
    902                                                      const GValue *value)
    903 {
    904   GValue values[] = { {0}, {0}};
    905   GValue *value_box = values+1;
    906   GValue tostring_box = {0};
    907   char *ret = NULL;
    908   gboolean need_to_free = false;
    909 
    910   g_value_init(values, G_TYPE_POINTER);
    911   g_value_set_pointer(values, (gpointer)v);
    912   g_value_init(&tostring_box, G_TYPE_STRING);
    913   if(value) {
    914     g_value_init(value_box, G_VALUE_TYPE(value));
    915     g_value_copy(value, value_box);
    916     need_to_free = true;
    917   } else {
    918     g_value_init(value_box, owl_variable_gtype_map[v->type]);
    919     g_closure_invoke(v->get_fn, value_box, 1, values, NULL);
    920   }
    921   g_closure_invoke(v->get_tostring_fn,&tostring_box, 2, values, NULL);
    922 
    923   ret = g_value_dup_string(&tostring_box);
    924   g_value_unset(&tostring_box);
    925   if(need_to_free) {
    926     g_value_unset(value_box);
    927   }
    928 
    929   return ret;
    930829}
    931830
     
    933832int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
    934833  char *tostring;
    935   GValue values[] = {{0},{0},{0}};
    936   GValue *value_box = values+1;
    937   GValue return_box = {0};
     834  GValue values[] = {G_VALUE_INIT, G_VALUE_INIT};
     835  GValue *value_box = &values[1];
     836  GValue return_box = G_VALUE_INIT;
    938837  int set_successfully = -1;
    939838  if (!v->set_fromstring_fn) {
     
    941840    return -1;   
    942841  }
     842  g_value_init(&values[0], G_TYPE_POINTER);
     843  g_value_set_pointer(&values[0], NULL);
    943844  g_value_init(value_box, G_TYPE_STRING);
    944   g_value_init(values, G_TYPE_POINTER);
    945   g_value_set_pointer(values, v);
     845  g_value_set_static_string(value_box, value);
    946846  g_value_init(&return_box, G_TYPE_INT);
    947   g_value_set_static_string(value_box, value);
    948847  g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL);
    949848  set_successfully = g_value_get_int(&return_box);
     
    953852  }
    954853  if (msg && (0 != set_successfully)) {
    955     tostring = owl_variable_invoke_tostring(v, NULL);
     854    tostring = owl_variable_get_tostring(v);
    956855    if (tostring) {
    957856      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
     
    964863  return set_successfully;
    965864}
    966 
    967 static int owl_variable_invoke_setter(owl_variable *v, const GValue *value) {
    968   GValue values[] = {{0},{0}};
    969   GValue return_box = {0};
    970   int ret = -1;
    971   g_value_init(&return_box, G_TYPE_INT);
    972   g_value_init(values, G_TYPE_POINTER);
    973   g_value_set_pointer(values, v);
    974   g_value_init(values+1, G_VALUE_TYPE(value));
    975   g_value_copy(value, values+1);
    976   g_closure_invoke(v->set_fn, &return_box, 2, values, NULL);
    977   ret = g_value_get_int(&return_box);
    978   /*  g_value_unset(&return_box); */
    979   g_value_unset(values+1);
    980   return ret;
    981 }
    982865 
    983866int owl_variable_set_string(owl_variable *v, const char *newval)
    984867{
    985   int ret = -1;
    986   GValue value_box = {0};
    987   if (v->type != OWL_VARIABLE_STRING) return -1;
    988   g_value_init(&value_box, G_TYPE_STRING);
    989   g_value_set_static_string(&value_box, newval);
    990   ret = owl_variable_invoke_setter(v, &value_box);
    991   g_value_unset(&value_box);
    992   return ret;
     868  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1);
     869
     870  set_string_t cb = (set_string_t) v->set_fn;
     871  return cb(v, newval);
    993872}
    994873
    995874int owl_variable_set_int(owl_variable *v, int newval)
    996875{
    997   int ret = -1;
    998   GValue value_box = {0};
    999   if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1;
    1000   g_value_init(&value_box, G_TYPE_INT);
    1001   g_value_set_int(&value_box, newval);
    1002   ret = owl_variable_invoke_setter(v, &value_box);
    1003   /* g_value_unset(&value_box); */
    1004   return ret;
     876  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1);
     877
     878  set_int_t cb = (set_int_t) v->set_fn;
     879  return cb(v, newval);
    1005880}
    1006881
    1007882int owl_variable_set_bool(owl_variable *v, gboolean newval) {
    1008   int ret = -1;
    1009   GValue value_box = {0};
    1010   if (v->type != OWL_VARIABLE_BOOL) return -1;
    1011   g_value_init(&value_box, G_TYPE_BOOLEAN);
    1012   g_value_set_int(&value_box, newval);
    1013   ret = owl_variable_invoke_setter(v, &value_box);
    1014   /* g_value_unset(&value_box); */
    1015   return ret;
     883  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1);
     884
     885  set_bool_t cb = (set_bool_t) v->set_fn;
     886  return cb(v, newval);
    1016887}
    1017888
     
    1030901CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
    1031902{
    1032   return owl_variable_invoke_tostring(v, NULL);
    1033 }
    1034 
    1035 CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v)
    1036 {
     903  GValue instance = G_VALUE_INIT;
     904  GValue tostring_box = G_VALUE_INIT;
    1037905  char *ret = NULL;
    1038   GValue default_value_box = {0};
    1039   GValue variable_box = {0};
    1040   g_value_init(&variable_box, G_TYPE_POINTER);
    1041   g_value_init(&default_value_box, owl_variable_gtype_map[v->type]);
    1042   g_value_set_pointer(&variable_box, (gpointer)v);
    1043   g_closure_invoke(v->get_default_fn, &default_value_box, 1,
    1044                    &variable_box, NULL); 
    1045   ret = owl_variable_invoke_tostring(v, &default_value_box);
    1046   g_value_unset(&default_value_box);
     906
     907  g_value_init(&instance, G_TYPE_POINTER);
     908  g_value_set_pointer(&instance, NULL);
     909  g_value_init(&tostring_box, G_TYPE_STRING);
     910  g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL);
     911
     912  ret = g_value_dup_string(&tostring_box);
     913  g_value_unset(&tostring_box);
     914
    1047915  return ret;
    1048916}
    1049917
     918const char *owl_variable_get_default_tostring(const owl_variable *v)
     919{
     920  return v->default_str;
     921}
     922
    1050923owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
    1051924{
     
    1053926}
    1054927
    1055 const GValue *owl_variable_get(const owl_variable *v, GValue *val)
    1056 {
    1057   GValue variable_box = {0};
    1058   g_value_init(&variable_box, G_TYPE_POINTER);
    1059   g_value_set_pointer(&variable_box,(gpointer)v);
    1060   g_closure_invoke(v->get_fn, val, 1, &variable_box, NULL); 
    1061   return val;
    1062 }
    1063 
    1064928const char *owl_variable_get_string(const owl_variable *v)
    1065929{
    1066   GValue value = {0};
    1067   if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) {
    1068     owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v));
    1069     return NULL;
    1070   }
    1071   g_value_init(&value, G_TYPE_STRING);
    1072   /* not a leak, since we don't own the underlying string */
    1073   return g_value_get_string(owl_variable_get(v,&value));
     930  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL);
     931
     932  get_string_t cb = (get_string_t) v->get_fn;
     933  return cb(v);
    1074934}
    1075935
    1076936int owl_variable_get_int(const owl_variable *v)
    1077937{
    1078   GValue value = {0};
    1079   if (owl_variable_get_type(v) != OWL_VARIABLE_INT) {
    1080     owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v));
    1081     return -1;
    1082   }
    1083   g_value_init(&value, G_TYPE_INT);
    1084   return g_value_get_int(owl_variable_get(v,&value));
    1085 }
    1086 
    1087 int owl_variable_get_bool(const owl_variable *v)
    1088 {
    1089   GValue value = {0};
    1090   if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
    1091     owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v));
    1092     return -1;
    1093   }
    1094   g_value_init(&value, G_TYPE_BOOLEAN);
    1095   return g_value_get_boolean(owl_variable_get(v,&value));
     938  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0);
     939
     940  get_int_t cb = (get_int_t) v->get_fn;
     941  return cb(v);
     942}
     943
     944gboolean owl_variable_get_bool(const owl_variable *v)
     945{
     946  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE);
     947
     948  get_bool_t cb = (get_bool_t) v->get_fn;
     949  return cb(v);
    1096950}
    1097951
    1098952void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
    1099953{
    1100   char *tostring = owl_variable_get_default_tostring(v);
     954  const char *default_str = owl_variable_get_default_tostring(v);
    1101955  char *default_buf;
    1102956
    1103   if (tostring)
    1104     default_buf = g_strdup_printf("'%s'", tostring);
     957  if (default_str)
     958    default_buf = g_strdup_printf("'%s'", default_str);
    1105959  else
    1106960    default_buf = g_strdup("<null>");
     
    1109963                            owl_variable_get_summary(v), default_buf);
    1110964  g_free(default_buf);
    1111   g_free(tostring);
    1112965}
    1113966
    1114967void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
    1115968  char *tostring;
     969  const char *default_str;
    1116970
    1117971  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
     
    1128982  owl_fmtext_append_normal(fm, "\n\n");
    1129983
    1130   tostring = owl_variable_get_default_tostring(v);
     984  default_str = owl_variable_get_default_tostring(v);
    1131985  owl_fmtext_append_normal(fm, "Default:        ");
    1132   owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
     986  owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>"));
    1133987  owl_fmtext_append_normal(fm, "\n\n");
    1134   g_free(tostring);
    1135988
    1136989  owl_fmtext_append_normal(fm, "Valid Settings: ");
     
    11541007/* default common functions */
    11551008
    1156 const char *owl_variable_string_get_default(const owl_variable *v, void *dummy) {
     1009const char *owl_variable_string_get_default(const owl_variable *v) {
    11571010  return g_value_get_string(&(v->val));
    11581011}
    11591012
    1160 const char *owl_variable_string_get_default_default(const owl_variable *v, void *dummy) {
    1161   return g_value_get_string(&(v->gval_default));
    1162 }
    1163 
    1164 
    1165 const int owl_variable_int_get_default(const owl_variable *v, void *dummy) {
     1013int owl_variable_int_get_default(const owl_variable *v) {
    11661014  return g_value_get_int(&(v->val));
    11671015}
    11681016
    1169 const int owl_variable_int_get_default_default(const owl_variable *v, void *dummy) {
    1170   return g_value_get_int(&(v->gval_default));
    1171 }
    1172 
    1173 
    1174 const gboolean owl_variable_bool_get_default(const owl_variable *v, void *dummy) {
     1017gboolean owl_variable_bool_get_default(const owl_variable *v) {
    11751018  return g_value_get_boolean(&(v->val));
    11761019}
    11771020
    1178 const gboolean owl_variable_bool_get_default_default(const owl_variable *v, void *dummy) {
    1179   return g_value_get_boolean(&(v->gval_default));
    1180 }
    1181 
    1182 
    1183 void owl_variable_delete_default(owl_variable *v, void *dummy)
    1184 {
    1185   g_value_unset(&(v->val));
    1186 }
    1187 
    11881021/* default functions for booleans */
    11891022
    1190 int owl_variable_bool_validate_default(const owl_variable *v, const gboolean newval, void *dummy) {
     1023int owl_variable_bool_validate_default(const owl_variable *v, gboolean newval) {
    11911024  return (newval == 1) || (newval == 0);
    11921025}
    11931026
    1194 static int owl_variable_invoke_validator(owl_variable *v, const GValue *newval)
    1195 {
    1196   GValue values[] = {{0},{0}};
    1197   GValue ret = {0};
    1198   g_value_init(&ret, G_TYPE_INT);
    1199   g_value_init(values, G_TYPE_POINTER);
    1200   g_value_set_pointer(values, v);
    1201   g_value_init(values+1, G_VALUE_TYPE(newval));
    1202   g_value_copy(newval, values+1);
    1203   g_closure_invoke(v->validate_fn, &ret, 2, values, NULL);
    1204   g_value_unset(values+1);
    1205   return g_value_get_int(&ret);
    1206 }
    1207 
    1208 int owl_variable_bool_set_default(owl_variable *v, const bool newval, void *dummy) {
    1209   GValue value = {0};
    1210   g_value_init(&value, G_TYPE_BOOLEAN);
    1211   g_value_set_boolean(&value, newval);
    1212   if (v->validate_fn) {
    1213     if (!owl_variable_invoke_validator(v,&value)) {
    1214       return(-1);
    1215     }
    1216   }
     1027int owl_variable_bool_set_default(owl_variable *v, bool newval) {
     1028  if (!((validate_bool_t)v->validate_fn)(v, newval))
     1029    return -1;
     1030
    12171031  g_value_set_boolean(&(v->val), newval);
    12181032  return(0);
     
    12211035int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
    12221036  gboolean i;
    1223   GValue value = {0};
    12241037  if (!strcmp(newval, "on")) {
    1225     i=1;
     1038    i = TRUE;
    12261039  } else if (!strcmp(newval, "off")) {
    1227     i=0;
     1040    i = FALSE;
    12281041  } else {
    12291042    return(-1);
    12301043  }
    1231   g_value_init(&value, G_TYPE_BOOLEAN);
    1232   g_value_set_boolean(&value, i);
    1233   return owl_variable_invoke_setter(v,&value);
    1234 }
    1235 
    1236 CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, const bool val, void *dummy)
    1237 {
     1044
     1045  return owl_variable_set_bool(v, i);
     1046}
     1047
     1048CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy)
     1049{
     1050  bool val = owl_variable_get_bool(v);
    12381051  if (val == 0) {
    12391052    return g_strdup("off");
     
    12471060/* default functions for integers */
    12481061
    1249 int owl_variable_int_validate_default(const owl_variable *v, const int newval, void *dummy)
     1062int owl_variable_int_validate_default(const owl_variable *v, int newval)
    12501063{
    12511064  return (1);
    12521065}
    12531066
    1254 int owl_variable_int_set_default(owl_variable *v, const int newval, void *dummy) {
    1255   GValue value = {0};
    1256   g_value_init(&value, G_TYPE_INT);
    1257   g_value_set_int(&value, newval);
    1258   if (v->validate_fn) {
    1259     if (!owl_variable_invoke_validator(v,&value)) {
    1260       return(-1);
    1261     }
    1262   }
     1067int owl_variable_int_set_default(owl_variable *v, int newval) {
     1068  if (!((validate_int_t)v->validate_fn)(v, newval))
     1069    return -1;
     1070
    12631071  g_value_set_int(&(v->val), newval);
    12641072  return(0);
     
    12681076  int i;
    12691077  char *ep;
    1270   GValue value = {0};
    12711078  i = strtol(newval, &ep, 10);
    12721079  if (*ep || ep==newval) return(-1);
    1273   g_value_init(&value, G_TYPE_INT);
    1274   g_value_set_int(&value, i);
    1275   return owl_variable_invoke_setter(v, &value);
    1276 }
    1277 
    1278 CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, const int val, void *dummy)
    1279 {
    1280   return g_strdup_printf("%d", val);
     1080  return owl_variable_set_int(v, i);
     1081}
     1082
     1083CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy)
     1084{
     1085  return g_strdup_printf("%d", owl_variable_get_int(v));
    12811086}
    12821087
    12831088/* default functions for enums (a variant of integers) */
    12841089
    1285 int owl_variable_enum_validate(const owl_variable *v, const int newval, void *dummy) { 
     1090int owl_variable_enum_validate(const owl_variable *v, int newval) {
    12861091  char **enums;
    12871092  int nenums, val;
     
    12991104  char **enums;
    13001105  int i, val=-1;
    1301   GValue box = {0};
    13021106  if (newval == NULL) return(-1);
    13031107  enums = g_strsplit_set(v->validsettings, ",", 0);
     
    13091113  g_strfreev(enums);
    13101114  if (val == -1) return(-1);
    1311   g_value_init(&box, G_TYPE_INT);
    1312   g_value_set_int(&box, val);
    1313   return owl_variable_invoke_setter(v, &box);
    1314 }
    1315 
    1316 CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, const int val, void *dummy)
     1115  return owl_variable_set_int(v, val);
     1116}
     1117
     1118CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy)
    13171119{
    13181120  char **enums;
     
    13221124  enums = g_strsplit_set(v->validsettings, ",", 0);
    13231125  nenums = g_strv_length(enums);
    1324   i = val;
     1126  i = owl_variable_get_int(v);
    13251127  if (i<0 || i>=nenums) {
    13261128    g_strfreev(enums);
     
    13341136/* default functions for stringeans */
    13351137
    1336 int owl_variable_string_validate_default(const struct _owl_variable *v, const char *newval, void *dummy) {
     1138int owl_variable_string_validate_default(const owl_variable *v, const char *newval) {
    13371139  if (newval == NULL) return(0);
    13381140  else return (1);
    13391141}
    13401142
    1341 int owl_variable_string_set_default(owl_variable *v, const char *newval, void *dummy) {
    1342   GValue value = {0};
    1343   g_value_init(&value, G_TYPE_STRING);
    1344   g_value_set_static_string(&value, newval);
    1345   if (v->validate_fn) {
    1346     if (!owl_variable_invoke_validator(v,&value)) {
    1347       return(-1);
    1348     }
    1349   }
    1350 
    1351   /* set_string dups the string for us */
     1143int owl_variable_string_set_default(owl_variable *v, const char *newval) {
     1144  if (!((validate_string_t)v->validate_fn)(v, newval))
     1145    return -1;
     1146
    13521147  g_value_set_string(&(v->val), newval);
    13531148  return(0);
     
    13561151int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy)
    13571152{
    1358   GValue val = {0};
    1359   int ret = -1;
    1360   g_value_init(&val, G_TYPE_STRING);
    1361   /* we don't need to dup the string because we don't own it and the setter function we invoke will DTRT */
    1362   g_value_set_static_string(&val, newval);
    1363   ret = owl_variable_invoke_setter(v, &val);
    1364   g_value_unset(&val);
    1365   return ret;
    1366 }
    1367 
    1368 CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, const char *val, void *dummy)
    1369 {
    1370   if(val) {
    1371     return g_strdup(val);
    1372   } else {
    1373     return g_strdup("");
    1374   }
    1375 }
    1376 
     1153  return owl_variable_set_string(v, newval);
     1154}
     1155
     1156CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy)
     1157{
     1158  return g_strdup(owl_variable_get_string(v));
     1159}
     1160
Note: See TracChangeset for help on using the changeset viewer.