Changeset 01ff87d for variable.c


Ignore:
Timestamp:
Aug 15, 2009, 7:16:47 PM (15 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
4db2355
Parents:
96cdeaf (diff), 369a7f0 (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 branch 'andersk/const'
File:
1 edited

Legend:

Unmodified
Added
Removed
  • variable.c

    r3b42640 r01ff87d  
    375375/* commonly useful */
    376376
    377 int owl_variable_int_validate_gt0(owl_variable *v, void *newval)
     377int owl_variable_int_validate_gt0(const owl_variable *v, const void *newval)
    378378{
    379379  if (newval == NULL) return(0);
    380   else if (*(int*)newval < 1) return(0);
     380  else if (*(const int*)newval < 1) return(0);
    381381  else return (1);
    382382}
    383383
    384 int owl_variable_int_validate_positive(owl_variable *v, void *newval)
     384int owl_variable_int_validate_positive(const owl_variable *v, const void *newval)
    385385{
    386386  if (newval == NULL) return(0);
    387   else if (*(int*)newval < 0) return(0);
     387  else if (*(const int*)newval < 0) return(0);
    388388  else return (1);
    389389}
    390390
    391391/* typewinsize */
    392 int owl_variable_typewinsize_set(owl_variable *v, void *newval)
     392int owl_variable_typewinsize_set(owl_variable *v, const void *newval)
    393393{
    394394  int rv;
     
    399399
    400400/* debug (cache value in g->debug) */
    401 int owl_variable_debug_set(owl_variable *v, void *newval)
     401int owl_variable_debug_set(owl_variable *v, const void *newval)
    402402{
    403   if (newval && (*(int*)newval == 1 || *(int*)newval == 0)) {
    404     g.debug = *(int*)newval;
     403  if (newval && (*(const int*)newval == 1 || *(const int*)newval == 0)) {
     404    g.debug = *(const int*)newval;
    405405  }
    406406  return owl_variable_bool_set_default(v, newval);
     
    408408
    409409/* When 'aaway' is changed, need to notify the AIM server */
    410 int owl_variable_aaway_set(owl_variable *v, void *newval)
     410int owl_variable_aaway_set(owl_variable *v, const void *newval)
    411411{
    412412  if (newval) {
    413     if (*(int*)newval == 1) {
     413    if (*(const int*)newval == 1) {
    414414      owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g));
    415     } else if (*(int*)newval == 0) {
     415    } else if (*(const int*)newval == 0) {
    416416      owl_aim_set_awaymsg("");
    417417    }
     
    420420}
    421421
    422 int owl_variable_pseudologins_set(owl_variable *v, void *newval)
     422int owl_variable_pseudologins_set(owl_variable *v, const void *newval)
    423423{
    424424  if (newval) {
    425     if (*(int*)newval == 1) {
     425    if (*(const int*)newval == 1) {
    426426      owl_function_zephyr_buddy_check(0);
    427427    }
     
    432432/* note that changing the value of this will clobber
    433433 * any user setting of this */
    434 int owl_variable_disable_ctrl_d_set(owl_variable *v, void *newval)
     434int owl_variable_disable_ctrl_d_set(owl_variable *v, const void *newval)
    435435{
    436436
     
    438438
    439439  if (newval && !owl_context_is_startup(owl_global_get_context(&g))) {
    440     if (*(int*)newval == 2) {
     440    if (*(const int*)newval == 2) {
    441441      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
    442     } else if (*(int*)newval == 1) {
     442    } else if (*(const int*)newval == 1) {
    443443      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
    444444    } else {
     
    449449}
    450450
    451 int owl_variable_tty_set(owl_variable *v, void *newval)
     451int owl_variable_tty_set(owl_variable *v, const void *newval)
    452452{
    453453  owl_zephyr_set_locationinfo(owl_global_get_hostname(&g), newval);
     
    531531}
    532532
    533 owl_variable * owl_variable_newvar(char *name, char *summary, char * description) {
     533owl_variable * owl_variable_newvar(const char *name, const char *summary, const char * description) {
    534534  owl_variable * var = owl_malloc(sizeof(owl_variable));
    535535  memset(var, 0, sizeof(owl_variable));
     
    540540}
    541541
    542 void owl_variable_update(owl_variable *var, char *summary, char *desc) {
     542void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
    543543  if(var->summary) owl_free(var->summary);
    544544  var->summary = owl_strdup(summary);
     
    547547}
    548548
    549 void owl_variable_dict_newvar_string(owl_vardict * vd, char *name, char *summ, char * desc, char * initval) {
     549void owl_variable_dict_newvar_string(owl_vardict * vd, const char *name, const char *summ, const char * desc, const char * initval) {
    550550  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_STRING);
    551551  if(old) {
     
    567567}
    568568
    569 void owl_variable_dict_newvar_int(owl_vardict * vd, char *name, char *summ, char * desc, int initval) {
     569void owl_variable_dict_newvar_int(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
    570570  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_INT);
    571571  if(old) {
     
    588588}
    589589
    590 void owl_variable_dict_newvar_bool(owl_vardict * vd, char *name, char *summ, char * desc, int initval) {
     590void owl_variable_dict_newvar_bool(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
    591591  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_BOOL);
    592592  if(old) {
     
    614614
    615615/* free the list with owl_variable_dict_namelist_free */
    616 void owl_variable_dict_get_names(owl_vardict *d, owl_list *l) {
     616void owl_variable_dict_get_names(const owl_vardict *d, owl_list *l) {
    617617  owl_dict_get_keys(d, l);
    618618}
     
    628628
    629629
    630 char *owl_variable_get_description(owl_variable *v) {
     630const char *owl_variable_get_description(const owl_variable *v) {
    631631  return v->description;
    632632}
    633633
    634 char *owl_variable_get_summary(owl_variable *v) {
     634const char *owl_variable_get_summary(const owl_variable *v) {
    635635  return v->summary;
    636636}
    637637
    638 char *owl_variable_get_validsettings(owl_variable *v) {
     638const char *owl_variable_get_validsettings(const owl_variable *v) {
    639639  if (v->validsettings) {
    640640    return v->validsettings;
     
    647647
    648648/* returns 0 on success, prints a status msg if msg is true */
    649 int owl_variable_set_fromstring(owl_vardict *d, char *name, char *value, int msg, int requirebool) {
     649int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) {
    650650  owl_variable *v;
    651651  char buff2[1024];
     
    676676}
    677677 
    678 int owl_variable_set_string(owl_vardict *d, char *name, char *newval) {
     678int owl_variable_set_string(owl_vardict *d, const char *name, const char *newval) {
    679679  owl_variable *v;
    680680  if (!name) return(-1);
     
    685685}
    686686 
    687 int owl_variable_set_int(owl_vardict *d, char *name, int newval) {
     687int owl_variable_set_int(owl_vardict *d, const char *name, int newval) {
    688688  owl_variable *v;
    689689  if (!name) return(-1);
     
    694694}
    695695 
    696 int owl_variable_set_bool_on(owl_vardict *d, char *name) {
     696int owl_variable_set_bool_on(owl_vardict *d, const char *name) {
    697697  return owl_variable_set_int(d,name,1);
    698698}
    699699
    700 int owl_variable_set_bool_off(owl_vardict *d, char *name) {
     700int owl_variable_set_bool_off(owl_vardict *d, const char *name) {
    701701  return owl_variable_set_int(d,name,0);
    702702}
    703703
    704 int owl_variable_get_tostring(owl_vardict *d, char *name, char *buf, int bufsize) {
     704int owl_variable_get_tostring(const owl_vardict *d, const char *name, char *buf, int bufsize) {
    705705  owl_variable *v;
    706706  if (!name) return(-1);
     
    710710}
    711711
    712 int owl_variable_get_default_tostring(owl_vardict *d, char *name, char *buf, int bufsize) {
     712int owl_variable_get_default_tostring(const owl_vardict *d, const char *name, char *buf, int bufsize) {
    713713  owl_variable *v;
    714714  if (!name) return(-1);
     
    722722}
    723723
    724 owl_variable *owl_variable_get_var(owl_vardict *d, char *name, int require_type) {
     724owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name, int require_type) {
    725725  owl_variable *v;
    726726  if (!name) return(NULL);
     
    731731
    732732/* returns a reference */
    733 void *owl_variable_get(owl_vardict *d, char *name, int require_type) {
     733const void *owl_variable_get(const owl_vardict *d, const char *name, int require_type) {
    734734  owl_variable *v = owl_variable_get_var(d, name, require_type);
    735735  if(v == NULL) return NULL;
     
    738738
    739739/* returns a reference */
    740 char *owl_variable_get_string(owl_vardict *d, char *name) {
     740const char *owl_variable_get_string(const owl_vardict *d, const char *name) {
    741741  return owl_variable_get(d,name, OWL_VARIABLE_STRING);
    742742}
    743743
    744744/* returns a reference */
    745 void *owl_variable_get_other(owl_vardict *d, char *name) {
     745const void *owl_variable_get_other(const owl_vardict *d, const char *name) {
    746746  return owl_variable_get(d,name, OWL_VARIABLE_OTHER);
    747747}
    748748
    749 int owl_variable_get_int(owl_vardict *d, char *name) {
    750   int *pi;
     749int owl_variable_get_int(const owl_vardict *d, const char *name) {
     750  const int *pi;
    751751  pi = owl_variable_get(d,name,OWL_VARIABLE_INT);
    752752  if (!pi) return(-1);
     
    754754}
    755755
    756 int owl_variable_get_bool(owl_vardict *d, char *name) {
    757   int *pi;
     756int owl_variable_get_bool(const owl_vardict *d, const char *name) {
     757  const int *pi;
    758758  pi = owl_variable_get(d,name,OWL_VARIABLE_BOOL);
    759759  if (!pi) return(-1);
     
    761761}
    762762
    763 void owl_variable_describe(owl_vardict *d, char *name, owl_fmtext *fm) {
     763void owl_variable_describe(const owl_vardict *d, const char *name, owl_fmtext *fm) {
    764764  char defaultbuf[50];
    765765  char buf[1024];
     
    785785}
    786786
    787 void owl_variable_get_help(owl_vardict *d, char *name, owl_fmtext *fm) {
     787void owl_variable_get_help(const owl_vardict *d, const char *name, owl_fmtext *fm) {
    788788  char buff[1024];
    789789  int bufflen = 1023;
     
    839839/* default common functions */
    840840
    841 void *owl_variable_get_default(owl_variable *v) {
     841const void *owl_variable_get_default(const owl_variable *v) {
    842842  return v->val;
    843843}
     
    849849/* default functions for booleans */
    850850
    851 int owl_variable_bool_validate_default(owl_variable *v, void *newval) {
     851int owl_variable_bool_validate_default(const owl_variable *v, const void *newval) {
    852852  if (newval == NULL) return(0);
    853   else if (*(int*)newval==1 || *(int*)newval==0) return(1);
     853  else if (*(const int*)newval==1 || *(const int*)newval==0) return(1);
    854854  else return (0);
    855855}
    856856
    857 int owl_variable_bool_set_default(owl_variable *v, void *newval) {
     857int owl_variable_bool_set_default(owl_variable *v, const void *newval) {
    858858  if (v->validate_fn) {
    859859    if (!v->validate_fn(v, newval)) return(-1);
    860860  }
    861   *(int*)v->val = *(int*)newval;
     861  *(int*)v->val = *(const int*)newval;
    862862  return(0);
    863863}
    864864
    865 int owl_variable_bool_set_fromstring_default(owl_variable *v, char *newval) {
     865int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval) {
    866866  int i;
    867867  if (!strcmp(newval, "on")) i=1;
     
    871871}
    872872
    873 int owl_variable_bool_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
     873int owl_variable_bool_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
    874874  if (val == NULL) {
    875875    snprintf(buf, bufsize, "<null>");
    876876    return -1;
    877   } else if (*(int*)val == 0) {
     877  } else if (*(const int*)val == 0) {
    878878    snprintf(buf, bufsize, "off");
    879879    return 0;
    880   } else if (*(int*)val == 1) {
     880  } else if (*(const int*)val == 1) {
    881881    snprintf(buf, bufsize, "on");
    882882    return 0;
     
    889889/* default functions for integers */
    890890
    891 int owl_variable_int_validate_default(owl_variable *v, void *newval) {
     891int owl_variable_int_validate_default(const owl_variable *v, const void *newval) {
    892892  if (newval == NULL) return(0);
    893893  else return (1);
    894894}
    895895
    896 int owl_variable_int_set_default(owl_variable *v, void *newval) {
     896int owl_variable_int_set_default(owl_variable *v, const void *newval) {
    897897  if (v->validate_fn) {
    898898    if (!v->validate_fn(v, newval)) return(-1);
    899899  }
    900   *(int*)v->val = *(int*)newval;
     900  *(int*)v->val = *(const int*)newval;
    901901  return(0);
    902902}
    903903
    904 int owl_variable_int_set_fromstring_default(owl_variable *v, char *newval) {
     904int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval) {
    905905  int i;
    906   char *ep = "x";
    907   i = strtol(newval, &ep, 10);
     906  const char *ep = "x";
     907  i = strtol(newval, (char **)&ep, 10);
    908908  if (*ep || ep==newval) return(-1);
    909909  return (v->set_fn(v, &i));
    910910}
    911911
    912 int owl_variable_int_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
     912int owl_variable_int_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
    913913  if (val == NULL) {
    914914    snprintf(buf, bufsize, "<null>");
    915915    return -1;
    916916  } else {
    917     snprintf(buf, bufsize, "%d", *(int*)val);
     917    snprintf(buf, bufsize, "%d", *(const int*)val);
    918918    return 0;
    919919  }
     
    922922/* default functions for enums (a variant of integers) */
    923923
    924 int owl_variable_enum_validate(owl_variable *v, void *newval) { 
     924int owl_variable_enum_validate(const owl_variable *v, const void *newval) { 
    925925  char **enums;
    926926  int nenums, val;
     
    929929  if (enums == NULL) return(0);
    930930  atokenize_free(enums, nenums);
    931   val = *(int*)newval;
     931  val = *(const int*)newval;
    932932  if (val < 0 || val >= nenums) {
    933933    return(0);
     
    936936}
    937937
    938 int owl_variable_enum_set_fromstring(owl_variable *v, char *newval) {
     938int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval) {
    939939  char **enums;
    940940  int nenums, i, val=-1;
     
    952952}
    953953
    954 int owl_variable_enum_get_tostring(owl_variable *v, char* buf, int bufsize, void *val) {
     954int owl_variable_enum_get_tostring(const owl_variable *v, char* buf, int bufsize, const void *val) {
    955955  char **enums;
    956956  int nenums, i;
     
    961961  }
    962962  enums = atokenize(v->validsettings, ",", &nenums);
    963   i = *(int*)val;
     963  i = *(const int*)val;
    964964  if (i<0 || i>=nenums) {
    965965    snprintf(buf, bufsize, "<invalid:%d>",i);
     
    973973/* default functions for stringeans */
    974974
    975 int owl_variable_string_validate_default(struct _owl_variable *v, void *newval) {
     975int owl_variable_string_validate_default(const struct _owl_variable *v, const void *newval) {
    976976  if (newval == NULL) return(0);
    977977  else return (1);
    978978}
    979979
    980 int owl_variable_string_set_default(owl_variable *v, void *newval) {
     980int owl_variable_string_set_default(owl_variable *v, const void *newval) {
    981981  if (v->validate_fn) {
    982982    if (!v->validate_fn(v, newval)) return(-1);
     
    987987}
    988988
    989 int owl_variable_string_set_fromstring_default(owl_variable *v, char *newval) {
     989int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval) {
    990990  return (v->set_fn(v, newval));
    991991}
    992992
    993 int owl_variable_string_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
     993int owl_variable_string_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
    994994  if (val == NULL) {
    995995    snprintf(buf, bufsize, "<null>");
    996996    return -1;
    997997  } else {
    998     snprintf(buf, bufsize, "%s", (char*)val);
     998    snprintf(buf, bufsize, "%s", (const char*)val);
    999999    return 0;
    10001000  }
     
    10151015  int numfailed=0;
    10161016  char buf[1024];
    1017   owl_variable * v;
     1017  const void *v;
    10181018
    10191019  in_regtest = 1;
Note: See TracChangeset for help on using the changeset viewer.