Changeset 863f4c4


Ignore:
Timestamp:
Jul 3, 2011, 8:57:20 PM (13 years ago)
Author:
GitHub Merge Button <merge-button@github.com>
Parents:
82e93c9 (diff), 785ee77 (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.
Message:
Merge 785ee770a72bcdda39fd541a90d8c640fa5c76ca into 82e93c95ea82cdc4725e91b4d3ac6ac4f0b75f24
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    re6d7e4e rca749a9  
    15721572  int  silent=0;
    15731573  int requirebool=0;
     1574  owl_variable *v;
    15741575
    15751576  if (argc == 1) {
     
    15941595    return NULL;
    15951596  }
    1596   owl_variable_set_fromstring(owl_global_get_vardict(&g), var, val, !silent, requirebool);
     1597
     1598  v = owl_variable_get_var(owl_global_get_vardict(&g), var);
     1599  if (v == NULL) {
     1600    if (!silent) owl_function_error("Unknown variable '%s'", var);
     1601  } else if (requirebool && owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     1602    if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
     1603  } else {
     1604    owl_variable_set_fromstring(v, val, !silent);
     1605  }
    15971606  return NULL;
    15981607}
     
    16001609char *owl_command_unset(int argc, const char *const *argv, const char *buff)
    16011610{
     1611  owl_variable *v;
    16021612  const char *var, *val;
    16031613  int  silent=0;
     
    16141624    return NULL;
    16151625  }
    1616   owl_variable_set_fromstring(owl_global_get_vardict(&g), var, val, !silent, 1);
     1626
     1627  v = owl_variable_get_var(owl_global_get_vardict(&g), var);
     1628  if (v == NULL) {
     1629    if (!silent) owl_function_error("Unknown variable '%s'", var);
     1630  } else if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     1631    if (!silent) owl_function_error("Variable '%s' is not a boolean", var);
     1632  } else {
     1633    owl_variable_set_fromstring(v, val, !silent);
     1634  }
    16171635  return NULL;
    16181636}
     
    16221640  const char *var;
    16231641  char *value;
     1642  const owl_variable *v;
    16241643
    16251644  if (argc==1) {
     
    16331652  var=argv[1];
    16341653   
    1635   value = owl_variable_get_tostring(owl_global_get_vardict(&g), var);
    1636   if (value) {
    1637     owl_function_makemsg("%s = '%s'", var, value);
     1654  v = owl_variable_get_var(owl_global_get_vardict(&g), var);
     1655  if (v) {
     1656    value = owl_variable_get_tostring(v);
     1657    if (value == NULL)
     1658      owl_function_makemsg("%s = <null>", var);
     1659    else
     1660      owl_function_makemsg("%s = '%s'", var, value);
    16381661    g_free(value);
    16391662  } else {
     
    25052528}
    25062529
    2507 char *owl_command_getvar(int argc, const char *const *argv, const char *buff)
    2508 {
     2530CALLER_OWN char *owl_command_getvar(int argc, const char *const *argv, const char *buff)
     2531{
     2532  const owl_variable *v;
    25092533  if (argc != 2) {
    25102534    owl_function_makemsg("Wrong number of arguments for %s", argv[0]);
    25112535    return NULL;
    25122536  }
    2513   return owl_variable_get_tostring(owl_global_get_vardict(&g), argv[1]);
     2537  v = owl_variable_get_var(owl_global_get_vardict(&g), argv[1]);
     2538  if (v == NULL) return NULL;
     2539  return owl_variable_get_tostring(v);
    25142540}
    25152541
  • functions.c

    rb8a3e00 rca749a9  
    15721572void owl_function_printallvars(void)
    15731573{
     1574  const owl_variable *v;
    15741575  const char *name;
    15751576  char *var;
     
    15851586    if (name && name[0]!='_') {
    15861587      g_string_append_printf(str, "\n%-20s = ", name);
    1587       var = owl_variable_get_tostring(owl_global_get_vardict(&g), name);
     1588      v = owl_variable_get_var(owl_global_get_vardict(&g), name);
     1589      var = owl_variable_get_tostring(v);
    15881590      if (var) {
    1589         g_string_append(str, var);
    1590         g_free(var);
     1591        g_string_append(str, var);
     1592        g_free(var);
     1593      } else {
     1594        g_string_append(str, "<null>");
    15911595      }
    15921596    }
     
    16011605void owl_function_show_variables(void)
    16021606{
     1607  const owl_variable *v;
    16031608  GPtrArray *varnames;
    16041609  owl_fmtext fm; 
     
    16131618    varname = varnames->pdata[i];
    16141619    if (varname && varname[0]!='_') {
    1615       owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
     1620      v = owl_variable_get_var(owl_global_get_vardict(&g), varname);
     1621      owl_variable_describe(v, &fm);
    16161622    }
    16171623  }
     
    16231629void owl_function_show_variable(const char *name)
    16241630{
     1631  const owl_variable *v;
    16251632  owl_fmtext fm; 
    16261633
    16271634  owl_fmtext_init_null(&fm);
    1628   owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
     1635  v = owl_variable_get_var(owl_global_get_vardict(&g), name);
     1636  if (v)
     1637    owl_variable_get_help(v, &fm);
     1638  else
     1639    owl_fmtext_append_normal(&fm, "No such variable...\n");
    16291640  owl_function_popless_fmtext(&fm);
    16301641  owl_fmtext_cleanup(&fm);
  • help.c

    rce68f23 rca54fd6  
    44void owl_help(void)
    55{
     6  const owl_variable *v;
    67  owl_fmtext fm;
    78  const char *varname;
     
    133134    varname = varnames->pdata[i];
    134135    if (varname && varname[0]!='_') {
    135       owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
     136      v = owl_variable_get_var(owl_global_get_vardict(&g), varname);
     137      owl_variable_describe(v, &fm);
    136138    }
    137139  }
  • perl/lib/BarnOwl.pm

    rb8a3e00 r785ee77  
    570570{
    571571  my $zsig = getvar('zsig');
    572   if (!$zsig) {
    573       if (my $zsigproc = getvar('zsigproc')) {
     572  if (!defined($zsig) || $zsig eq '') {
     573      my $zsigproc = getvar('zsigproc');
     574      if (defined($zsigproc) && $zsigproc ne '') {
    574575          $zsig = `$zsigproc`;
    575576      } elsif (!defined($zsig = get_zephyr_variable('zwrite-signature'))) {
  • stubgen.pl

    rfd03b12 rca54fd6  
    1414    if ($vartype =~ /^BOOL/) {
    1515        print "void owl_global_set_${altvarname}_on(owl_global *g) {\n";
    16         print "  owl_variable_set_bool_on(&g->vars, \"$varname\");\n}\n";
     16        print "  owl_variable_set_bool_on(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    1717        print "void owl_global_set_${altvarname}_off(owl_global *g) {\n";
    18         print "  owl_variable_set_bool_off(&g->vars, \"$varname\");\n}\n";
     18        print "  owl_variable_set_bool_off(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    1919        print "int owl_global_is_$altvarname(const owl_global *g) {\n";
    20         print "  return owl_variable_get_bool(&g->vars, \"$varname\");\n}\n";
     20        print "  return owl_variable_get_bool(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    2121    } elsif ($vartype =~ /^PATH/ or $vartype =~ /^STRING/) {
    2222        print "void owl_global_set_$altvarname(owl_global *g, const char *text) {\n";
    23         print "  owl_variable_set_string(&g->vars, \"$varname\", text);\n}\n";
     23        print "  owl_variable_set_string(owl_variable_get_var(&g->vars, \"$varname\"), text);\n}\n";
    2424        print "const char *owl_global_get_$altvarname(const owl_global *g) {\n";
    25         print "  return owl_variable_get_string(&g->vars, \"$varname\");\n}\n";
     25        print "  return owl_variable_get_string(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    2626    } elsif ($vartype =~ /^INT/ or $vartype =~ /^ENUM/) {
    2727        print "void owl_global_set_$altvarname(owl_global *g, int n) {\n";
    28         print "  owl_variable_set_int(&g->vars, \"$varname\", n);\n}\n";
     28        print "  owl_variable_set_int(owl_variable_get_var(&g->vars, \"$varname\"), n);\n}\n";
    2929        print "int owl_global_get_$altvarname(const owl_global *g) {\n";
    30         print "  return owl_variable_get_int(&g->vars, \"$varname\");\n}\n";
     30        print "  return owl_variable_get_int(owl_variable_get_var(&g->vars, \"$varname\"));\n}\n";
    3131    }
    3232    }
  • tester.c

    r25891a8 rb4a678a  
    276276int owl_variable_regtest(void) {
    277277  owl_vardict vd;
     278  owl_variable *var;
    278279  int numfailed=0;
    279280  char *value;
     
    283284  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
    284285
    285   FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
    286   FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
     286  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
     287  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
     288  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
    287289  FAIL_UNLESS("get bool as string",
    288               !strcmp((value = owl_variable_get_tostring(&vd,"rxping")), "off"));
     290              !strcmp((value = owl_variable_get_tostring(var)), "off"));
    289291  g_free(value);
    290   FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
    291   FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
    292   FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
    293   FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
    294   FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
    295   FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
    296 
    297 
    298   FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
    299   FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
    300   FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
    301 
    302   FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
    303   FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
     292  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
     293  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
     294  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
     295  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
     296  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
     297  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
     298
     299
     300  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
     301  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
     302  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
     303  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
     304
     305  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
     306  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
     307  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
    304308  FAIL_UNLESS("get int as string",
    305               !strcmp((value = owl_variable_get_tostring(&vd,"typewinsize")), "8"));
     309              !strcmp((value = owl_variable_get_tostring(var)), "8"));
    306310  g_free(value);
    307   FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
    308   FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
    309   FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
    310   FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
    311   FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
    312   FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
    313   FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
    314   FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
    315   FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
     311  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
     312  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
     313  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
     314  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
     315  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
     316  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
     317  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
     318  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
     319  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
    316320
    317321  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
    318   FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
    319   FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
    320   owl_variable_set_string(&vd, "stringvar", "new val");
    321   FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
     322  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
     323  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));
     324  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
     325  owl_variable_set_string(var, "new val");
     326  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
    322327
    323328  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
    324   FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
    325   FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
    326   owl_variable_set_int(&vd, "intvar", 17);
    327   FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
     329  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
     330  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
     331  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
     332  owl_variable_set_int(var, 17);
     333  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
    328334
    329335  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
    330   FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
    331   FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
    332   owl_variable_set_bool_off(&vd, "boolvar");
    333   FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
     336  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
     337  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));
     338  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
     339  owl_variable_set_bool_off(var);
     340  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
     341
     342  owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL);
     343  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
     344  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
     345  g_free(value);
     346  var = owl_variable_get_var(&vd, "zsigproc");
     347  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
     348  g_free(value);
    334349
    335350  owl_variable_dict_cleanup(&vd);
  • variable.c

    rce68f23 rfa981f3  
    662662}
    663663
    664 void owl_variable_dict_newvar_string(owl_vardict * vd, const char *name, const char *summ, const char * desc, const char * initval) {
    665   owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_STRING);
    666   if(old) {
     664void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
     665{
     666  owl_variable *old = owl_variable_get_var(vd, name);
     667  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
    667668    owl_variable_update(old, summ, desc);
    668669    g_free(old->pval_default);
     
    671672    owl_variable * var = owl_variable_newvar(name, summ, desc);
    672673    var->type = OWL_VARIABLE_STRING;
     674    var->validsettings = "<string>";
    673675    var->pval_default = g_strdup(initval);
    674676    var->set_fn = owl_variable_string_set_default;
     
    682684}
    683685
    684 void owl_variable_dict_newvar_int(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
    685   owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_INT);
    686   if(old) {
     686void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
     687{
     688  owl_variable *old = owl_variable_get_var(vd, name);
     689  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
    687690    owl_variable_update(old, summ, desc);
    688691    old->ival_default = initval;
     
    690693    owl_variable * var = owl_variable_newvar(name, summ, desc);
    691694    var->type = OWL_VARIABLE_INT;
     695    var->validsettings = "<int>";
    692696    var->ival_default = initval;
    693697    var->validate_fn = owl_variable_int_validate_default;
     
    703707}
    704708
    705 void owl_variable_dict_newvar_bool(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
    706   owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_BOOL);
    707   if(old) {
     709void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, bool initval)
     710{
     711  owl_variable *old = owl_variable_get_var(vd, name);
     712  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
    708713    owl_variable_update(old, summ, desc);
    709714    old->ival_default = initval;
     
    711716    owl_variable * var = owl_variable_newvar(name, summ, desc);
    712717    var->type = OWL_VARIABLE_BOOL;
     718    var->validsettings = "on,off";
    713719    var->ival_default = initval;
    714720    var->validate_fn = owl_variable_bool_validate_default;
     
    750756
    751757
     758const char *owl_variable_get_name(const owl_variable *v)
     759{
     760  return v->name;
     761}
     762
    752763const char *owl_variable_get_description(const owl_variable *v) {
    753764  return v->description;
     
    759770
    760771const char *owl_variable_get_validsettings(const owl_variable *v) {
    761   if (v->validsettings) {
    762     return v->validsettings;
    763   } else {
    764     return "";
    765   }
     772  return v->validsettings;
     773}
     774
     775int owl_variable_get_type(const owl_variable *v)
     776{
     777  return v->type;
    766778}
    767779
     
    769781
    770782/* returns 0 on success, prints a status msg if msg is true */
    771 int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) {
    772   owl_variable *v;
     783int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
    773784  char *tostring;
    774   if (!name) return(-1);
    775   v = owl_dict_find_element(d, name);
    776   if (v == NULL) {
    777     if (msg) owl_function_error("Unknown variable %s", name);
     785  if (!v->set_fromstring_fn) {
     786    if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v));
     787    return -1;   
     788  }
     789  if (0 != v->set_fromstring_fn(v, value)) {
     790    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
     791                                owl_variable_get_validsettings(v));
    778792    return -1;
    779793  }
    780   if (!v->set_fromstring_fn) {
    781     if (msg) owl_function_error("Variable %s is read-only", name);
    782     return -1;   
    783   }
    784   if (requirebool && v->type!=OWL_VARIABLE_BOOL) {
    785     if (msg) owl_function_error("Variable %s is not a boolean", name);
    786     return -1;   
    787   }
    788   if (0 != v->set_fromstring_fn(v, value)) {
    789     if (msg) owl_function_error("Unable to set %s (must be %s)", name,
    790                                   owl_variable_get_validsettings(v));
    791     return -1;
    792   }
    793   if (msg && v->get_tostring_fn) {
     794  if (msg) {
    794795    tostring = v->get_tostring_fn(v, v->get_fn(v));
    795     owl_function_makemsg("%s = '%s'", name, tostring);
     796    if (tostring)
     797      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
     798    else
     799      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
    796800    g_free(tostring);
    797801  }   
     
    799803}
    800804 
    801 int owl_variable_set_string(owl_vardict *d, const char *name, const char *newval) {
    802   owl_variable *v;
    803   if (!name) return(-1);
    804   v = owl_dict_find_element(d, name);
    805   if (v == NULL || !v->set_fn) return(-1);
    806   if (v->type!=OWL_VARIABLE_STRING) return(-1);
     805int owl_variable_set_string(owl_variable *v, const char *newval)
     806{
     807  if (v->type != OWL_VARIABLE_STRING) return -1;
    807808  return v->set_fn(v, newval);
    808809}
    809810 
    810 int owl_variable_set_int(owl_vardict *d, const char *name, int newval) {
    811   owl_variable *v;
    812   if (!name) return(-1);
    813   v = owl_dict_find_element(d, name);
    814   if (v == NULL || !v->set_fn) return(-1);
    815   if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1);
     811int owl_variable_set_int(owl_variable *v, int newval)
     812{
     813  if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1;
    816814  return v->set_fn(v, &newval);
    817815}
    818816 
    819 int owl_variable_set_bool_on(owl_vardict *d, const char *name) {
    820   return owl_variable_set_int(d,name,1);
    821 }
    822 
    823 int owl_variable_set_bool_off(owl_vardict *d, const char *name) {
    824   return owl_variable_set_int(d,name,0);
    825 }
    826 
    827 CALLER_OWN char *owl_variable_get_tostring(const owl_vardict *d, const char *name)
    828 {
    829   owl_variable *v;
    830   if (!name) return NULL;
    831   v = owl_dict_find_element(d, name);
    832   if (v == NULL || !v->get_tostring_fn) return NULL;
     817int owl_variable_set_bool_on(owl_variable *v)
     818{
     819  if (v->type != OWL_VARIABLE_BOOL) return -1;
     820  return owl_variable_set_int(v, true);
     821}
     822
     823int owl_variable_set_bool_off(owl_variable *v)
     824{
     825  if (v->type != OWL_VARIABLE_BOOL) return -1;
     826  return owl_variable_set_int(v, false);
     827}
     828
     829CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
     830{
    833831  return v->get_tostring_fn(v, v->get_fn(v));
    834832}
    835833
    836 CALLER_OWN char *owl_variable_get_default_tostring(const owl_vardict *d, const char *name)
    837 {
    838   owl_variable *v;
    839   if (!name) return NULL;
    840   v = owl_dict_find_element(d, name);
    841   if (v == NULL || !v->get_tostring_fn) return NULL;
     834CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v)
     835{
    842836  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    843837    return v->get_tostring_fn(v, &(v->ival_default));
     
    847841}
    848842
    849 owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name, int require_type) {
    850   owl_variable *v;
    851   if (!name) return(NULL);
    852   v = owl_dict_find_element(d, name);
    853   if (v == NULL || !v->get_fn || v->type != require_type) return(NULL);
    854   return v;
     843owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
     844{
     845  return owl_dict_find_element(d, name);
    855846}
    856847
    857848/* returns a reference */
    858 const void *owl_variable_get(const owl_vardict *d, const char *name, int require_type) {
    859   owl_variable *v = owl_variable_get_var(d, name, require_type);
    860   if(v == NULL) return NULL;
     849const void *owl_variable_get(const owl_variable *v)
     850{
    861851  return v->get_fn(v);
    862852}
    863853
     854const char *owl_variable_get_string(const owl_variable *v)
     855{
     856  if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) {
     857    owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v));
     858    return NULL;
     859  }
     860  return owl_variable_get(v);
     861}
     862
    864863/* returns a reference */
    865 const char *owl_variable_get_string(const owl_vardict *d, const char *name) {
    866   return owl_variable_get(d,name, OWL_VARIABLE_STRING);
    867 }
    868 
    869 /* returns a reference */
    870 const void *owl_variable_get_other(const owl_vardict *d, const char *name) {
    871   return owl_variable_get(d,name, OWL_VARIABLE_OTHER);
    872 }
    873 
    874 int owl_variable_get_int(const owl_vardict *d, const char *name) {
    875   const int *pi;
    876   pi = owl_variable_get(d,name,OWL_VARIABLE_INT);
    877   if (!pi) return(-1);
    878   return(*pi);
    879 }
    880 
    881 int owl_variable_get_bool(const owl_vardict *d, const char *name) {
    882   const int *pi;
    883   pi = owl_variable_get(d,name,OWL_VARIABLE_BOOL);
    884   if (!pi) return(-1);
    885   return(*pi);
    886 }
    887 
    888 void owl_variable_describe(const owl_vardict *d, const char *name, owl_fmtext *fm) {
     864const void *owl_variable_get_other(const owl_variable *v)
     865{
     866  if (owl_variable_get_type(v) != OWL_VARIABLE_OTHER) {
     867    owl_function_error("Variable '%s' is not type other.", owl_variable_get_name(v));
     868    return NULL;
     869  }
     870  return owl_variable_get(v);
     871}
     872
     873int owl_variable_get_int(const owl_variable *v)
     874{
     875  if (owl_variable_get_type(v) != OWL_VARIABLE_INT) {
     876    owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v));
     877    return -1;
     878  }
     879  const int *pi = owl_variable_get(v);
     880  if (!pi) return -1;
     881  return *pi;
     882}
     883
     884int owl_variable_get_bool(const owl_variable *v)
     885{
     886  if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
     887    owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v));
     888    return -1;
     889  }
     890  const int *pi = owl_variable_get(v);
     891  if (!pi) return -1;
     892  return *pi;
     893}
     894
     895void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
     896{
     897  char *tostring = owl_variable_get_default_tostring(v);
    889898  char *default_buf;
    890   owl_variable *v;
    891 
    892   if (!name
    893       || (v = owl_dict_find_element(d, name)) == NULL
    894       || !v->get_fn) {
    895     owl_fmtext_appendf_normal(fm, "     No such variable '%s'\n", name);
    896     return;
    897   }
    898   if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    899     default_buf = v->get_tostring_fn(v, &(v->ival_default));
    900   } else {
    901     default_buf = v->get_tostring_fn(v, v->pval_default);
    902   }
    903   owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: '%s')\n",
    904                             v->name,
    905                             owl_variable_get_summary(v), default_buf);
     899
     900  if (tostring)
     901    default_buf = g_strdup_printf("'%s'", tostring);
     902  else
     903    default_buf = g_strdup("<null>");
     904  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
     905                            owl_variable_get_name(v),
     906                            owl_variable_get_summary(v), default_buf);
    906907  g_free(default_buf);
    907 }
    908 
    909 void owl_variable_get_help(const owl_vardict *d, const char *name, owl_fmtext *fm) {
     908  g_free(tostring);
     909}
     910
     911void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
    910912  char *tostring;
    911   owl_variable *v;
    912 
    913   if (!name
    914       || (v = owl_dict_find_element(d, name)) == NULL
    915       || !v->get_fn) {
    916     owl_fmtext_append_normal(fm, "No such variable...\n");
    917     return;
    918   }
    919913
    920914  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
    921915  owl_fmtext_append_normal(fm, OWL_TABSTR);
    922   owl_fmtext_append_normal(fm, name);
     916  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
    923917  owl_fmtext_append_normal(fm, " - ");
    924   owl_fmtext_append_normal(fm, v->summary);
     918  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
    925919  owl_fmtext_append_normal(fm, "\n\n");
    926920
    927921  owl_fmtext_append_normal(fm, "Current:        ");
    928   tostring = owl_variable_get_tostring(d, name);
    929   owl_fmtext_append_normal(fm, tostring);
     922  tostring = owl_variable_get_tostring(v);
     923  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
    930924  g_free(tostring);
    931925  owl_fmtext_append_normal(fm, "\n\n");
    932926
    933927
    934   if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
    935     tostring = v->get_tostring_fn(v, &(v->ival_default));
    936   } else {
    937     tostring = v->get_tostring_fn(v, v->pval_default);
    938   }
     928  tostring = owl_variable_get_default_tostring(v);
    939929  owl_fmtext_append_normal(fm, "Default:        ");
    940   owl_fmtext_append_normal(fm, tostring);
     930  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
    941931  owl_fmtext_append_normal(fm, "\n\n");
    942932
     
    998988{
    999989  if (val == NULL) {
    1000     return g_strdup("<null>");
     990    return NULL;
    1001991  } else if (*(const int*)val == 0) {
    1002992    return g_strdup("off");
     
    10341024{
    10351025  if (val == NULL) {
    1036     return g_strdup("<null>");
     1026    return NULL;
    10371027  } else {
    10381028    return g_strdup_printf("%d", *(const int*)val);
     
    10781068
    10791069  if (val == NULL) {
    1080     return g_strdup("<null>");
     1070    return NULL;
    10811071  }
    10821072  enums = g_strsplit_set(v->validsettings, ",", 0);
     
    11141104CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, const void *val)
    11151105{
    1116   if (val == NULL) {
    1117     return g_strdup("<null>");
    1118   } else {
    1119     return g_strdup((const char*)val);
    1120   }
    1121 }
    1122 
     1106  return g_strdup((const char*)val);
     1107}
     1108
Note: See TracChangeset for help on using the changeset viewer.