[7d4fbcd] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <unistd.h> |
---|
| 5 | #include <ctype.h> |
---|
| 6 | #include "owl.h" |
---|
| 7 | |
---|
[1aee7d9] | 8 | static const char fileIdent[] = "$Id$"; |
---|
| 9 | |
---|
[e1c4636] | 10 | static int in_regtest = 0; |
---|
| 11 | |
---|
[7d4fbcd] | 12 | #define OWLVAR_BOOL(name,default,docstring) \ |
---|
| 13 | { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", docstring, NULL, \ |
---|
| 14 | NULL, NULL, NULL, NULL, NULL } |
---|
| 15 | |
---|
| 16 | #define OWLVAR_BOOL_FULL(name,default,docstring,validate,set,get) \ |
---|
| 17 | { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", docstring, NULL, \ |
---|
| 18 | validate, set, NULL, get, NULL } |
---|
| 19 | |
---|
| 20 | #define OWLVAR_INT(name,default,docstring) \ |
---|
| 21 | { name, OWL_VARIABLE_INT, NULL, default, "<int>", docstring, NULL, \ |
---|
| 22 | NULL, NULL, NULL, NULL, NULL, NULL } |
---|
| 23 | |
---|
| 24 | #define OWLVAR_INT_FULL(name,default,docstring,validset,validate,set,get) \ |
---|
| 25 | { name, OWL_VARIABLE_INT, NULL, default, validset, docstring, NULL, \ |
---|
| 26 | validate, set, NULL, get, NULL, NULL } |
---|
| 27 | |
---|
| 28 | #define OWLVAR_PATH(name,default,docstring) \ |
---|
| 29 | { name, OWL_VARIABLE_STRING, default, 0, "<path>", docstring, NULL, \ |
---|
| 30 | NULL, NULL, NULL, NULL, NULL, NULL } |
---|
| 31 | |
---|
| 32 | #define OWLVAR_STRING(name,default,docstring) \ |
---|
| 33 | { name, OWL_VARIABLE_STRING, default, 0, "<string>", docstring, NULL, \ |
---|
| 34 | NULL, NULL, NULL, NULL, NULL, NULL } |
---|
| 35 | |
---|
| 36 | /* enums are really integers, but where validset is a comma-separated |
---|
| 37 | * list of strings which can be specified. The tokens, starting at 0, |
---|
| 38 | * correspond to the values that may be specified. */ |
---|
| 39 | #define OWLVAR_ENUM(name,default,docstring,validset) \ |
---|
| 40 | { name, OWL_VARIABLE_INT, NULL, default, validset, docstring, NULL, \ |
---|
| 41 | owl_variable_enum_validate, \ |
---|
| 42 | NULL, owl_variable_enum_set_fromstring, \ |
---|
| 43 | NULL, owl_variable_enum_get_tostring, \ |
---|
| 44 | NULL } |
---|
| 45 | |
---|
[217a43e] | 46 | #define OWLVAR_ENUM_FULL(name,default,docstring,validset,validate, set, get) \ |
---|
| 47 | { name, OWL_VARIABLE_INT, NULL, default, validset, docstring, NULL, \ |
---|
| 48 | validate, \ |
---|
| 49 | set, owl_variable_enum_set_fromstring, \ |
---|
| 50 | get, owl_variable_enum_get_tostring, \ |
---|
| 51 | NULL } |
---|
| 52 | |
---|
[7d4fbcd] | 53 | static owl_variable variables_to_init[] = { |
---|
| 54 | |
---|
| 55 | OWLVAR_BOOL( "personalbell" /* %OwlVarStub */, 0, |
---|
| 56 | "ring the terminal bell when personal messages are received" ), |
---|
| 57 | |
---|
| 58 | OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1, |
---|
| 59 | "enable / disable the terminal bell" ), |
---|
| 60 | |
---|
| 61 | OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG, |
---|
| 62 | "whether debugging is enabled", |
---|
| 63 | NULL, owl_variable_debug_set, NULL), |
---|
| 64 | |
---|
| 65 | OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1, |
---|
| 66 | "send a login message when owl starts" ), |
---|
| 67 | |
---|
| 68 | OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1, |
---|
| 69 | "send a logout message when owl exits" ), |
---|
| 70 | |
---|
| 71 | OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0, |
---|
| 72 | "display received pings" ), |
---|
| 73 | |
---|
| 74 | OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1, |
---|
| 75 | "send pings" ), |
---|
| 76 | |
---|
| 77 | OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1, |
---|
| 78 | "display outgoing messages" ), |
---|
| 79 | |
---|
| 80 | OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1, |
---|
| 81 | "load logins from .anyone on startup" ), |
---|
| 82 | |
---|
| 83 | OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0, |
---|
| 84 | "turn personal logging on or off" ), |
---|
| 85 | |
---|
| 86 | OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0, |
---|
| 87 | "turn class logging on or off" ), |
---|
| 88 | |
---|
[e1c4636] | 89 | OWLVAR_BOOL( "colorztext" /* %OwlVarStub */, 1, |
---|
| 90 | "allow @color() in zephyrs to change color" ), |
---|
| 91 | |
---|
[217a43e] | 92 | OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1, |
---|
| 93 | "don't send zephyrs on C-d (or disable if in the middle of the message if set to 'middle')", "off,middle,on", |
---|
[7d4fbcd] | 94 | NULL, owl_variable_disable_ctrl_d_set, NULL), |
---|
| 95 | |
---|
| 96 | OWLVAR_BOOL( "_burningears" /* %OwlVarStub:burningears */, 0, |
---|
| 97 | "[NOT YET IMPLEMENTED] beep on messages matching patterns" ), |
---|
| 98 | |
---|
| 99 | OWLVAR_BOOL( "_summarymode" /* %OwlVarStub:summarymode */, 0, |
---|
| 100 | "[NOT YET IMPLEMENTED]" ), |
---|
| 101 | |
---|
| 102 | OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people", |
---|
| 103 | "path for logging personal zephyrs" ), |
---|
| 104 | |
---|
| 105 | OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class", |
---|
| 106 | "path for logging class zephyrs" ), |
---|
| 107 | |
---|
| 108 | OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE, |
---|
| 109 | "path for logging debug messages when debugging is enabled" ), |
---|
| 110 | |
---|
| 111 | OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsig_exec */, NULL, |
---|
| 112 | "name of a program to run that will generate zsigs" ), |
---|
| 113 | |
---|
| 114 | OWLVAR_STRING( "zsig" /* %OwlVarStub */, "", |
---|
| 115 | "zephyr signature" ), |
---|
| 116 | |
---|
| 117 | OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "", |
---|
| 118 | "string to append to the end of the sepbar" ), |
---|
| 119 | |
---|
| 120 | OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0, |
---|
| 121 | "turn zaway on or off" ), |
---|
| 122 | |
---|
| 123 | OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, |
---|
| 124 | OWL_DEFAULT_ZAWAYMSG, |
---|
| 125 | "zaway msg for responding to zephyrs when away" ), |
---|
| 126 | |
---|
| 127 | OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, |
---|
| 128 | OWL_DEFAULT_ZAWAYMSG, |
---|
| 129 | "default zaway message" ), |
---|
| 130 | |
---|
| 131 | OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all", |
---|
| 132 | "home view to switch to after 'X'" ), |
---|
| 133 | |
---|
[d36f2cb] | 134 | OWLVAR_INT( "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70, |
---|
[039213e] | 135 | "maximum number of columns for M-q to fill text to (or unlimited if 0)" ), |
---|
[d36f2cb] | 136 | |
---|
[039213e] | 137 | OWLVAR_INT( "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 0, |
---|
| 138 | "maximum number of columns for line-wrapping (or unlimited if 0)" ), |
---|
[d36f2cb] | 139 | |
---|
[7d4fbcd] | 140 | OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, |
---|
| 141 | OWL_TYPWIN_SIZE, |
---|
| 142 | "number of lines in the typing window", "int > 0", |
---|
| 143 | owl_variable_int_validate_gt0, |
---|
| 144 | owl_variable_typewinsize_set, |
---|
| 145 | NULL /* use default for get */ |
---|
| 146 | ), |
---|
| 147 | |
---|
| 148 | OWLVAR_ENUM( "webbrowser" /* %OwlVarStub */, OWL_WEBBROWSER_NETSCAPE, |
---|
| 149 | "web browser to use to launch URLs", |
---|
| 150 | "none,netscape,galeon" ), |
---|
| 151 | |
---|
| 152 | OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0, |
---|
| 153 | "enable automatic following of the last zephyr" ), |
---|
| 154 | |
---|
| 155 | /* This MUST be last... */ |
---|
| 156 | { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } |
---|
| 157 | |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | /**************************************************************************/ |
---|
| 161 | /*********************** SPECIFIC TO VARIABLES ****************************/ |
---|
| 162 | /**************************************************************************/ |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | /* commonly useful */ |
---|
| 166 | |
---|
| 167 | int owl_variable_int_validate_gt0(owl_variable *v, void *newval) { |
---|
| 168 | if (newval == NULL) return(0); |
---|
| 169 | else if (*(int*)newval < 1) return(0); |
---|
| 170 | else return (1); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | int owl_variable_int_validate_positive(owl_variable *v, void *newval) { |
---|
| 174 | if (newval == NULL) return(0); |
---|
| 175 | else if (*(int*)newval < 0) return(0); |
---|
| 176 | else return (1); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /* typewinsize */ |
---|
| 180 | |
---|
| 181 | int owl_variable_typewinsize_set(owl_variable *v, void *newval) { |
---|
| 182 | int rv; |
---|
| 183 | rv = owl_variable_int_set_default(v, newval); |
---|
| 184 | if (0 == rv) owl_function_resize(); |
---|
| 185 | return(rv); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /* debug (cache value in g->debug) */ |
---|
| 189 | |
---|
| 190 | int owl_variable_debug_set(owl_variable *v, void *newval) { |
---|
| 191 | if (newval && (*(int*)newval == 1 || *(int*)newval == 0)) { |
---|
| 192 | g.debug = *(int*)newval; |
---|
| 193 | } |
---|
| 194 | return owl_variable_bool_set_default(v, newval); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /* note that changing the value of this will clobber |
---|
| 198 | * any user setting of this */ |
---|
| 199 | int owl_variable_disable_ctrl_d_set(owl_variable *v, void *newval) { |
---|
[e1c4636] | 200 | |
---|
| 201 | if (in_regtest) return owl_variable_int_set_default(v, newval); |
---|
| 202 | |
---|
[217a43e] | 203 | if (newval && !owl_context_is_startup(owl_global_get_context(&g))) { |
---|
| 204 | if (*(int*)newval == 2) { |
---|
[7d4fbcd] | 205 | owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char"); |
---|
[217a43e] | 206 | } else if (*(int*)newval == 1) { |
---|
| 207 | owl_function_command_norv("bindkey editmulti C-d command editmulti:done-or-delete"); |
---|
[7d4fbcd] | 208 | } else { |
---|
| 209 | owl_function_command_norv("bindkey editmulti C-d command editmulti:done"); |
---|
| 210 | } |
---|
| 211 | } |
---|
[217a43e] | 212 | return owl_variable_int_set_default(v, newval); |
---|
[7d4fbcd] | 213 | } |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | /**************************************************************************/ |
---|
| 217 | /****************************** GENERAL ***********************************/ |
---|
| 218 | /**************************************************************************/ |
---|
| 219 | |
---|
| 220 | int owl_variable_dict_setup(owl_vardict *vd) { |
---|
| 221 | owl_variable *cur; |
---|
| 222 | if (owl_dict_create(vd)) return(-1); |
---|
| 223 | for (cur = variables_to_init; cur->name != NULL; cur++) { |
---|
| 224 | switch (cur->type) { |
---|
| 225 | case OWL_VARIABLE_OTHER: |
---|
| 226 | cur->set_fn(cur, cur->pval_default); |
---|
| 227 | break; |
---|
| 228 | case OWL_VARIABLE_STRING: |
---|
| 229 | if (!cur->validate_fn) |
---|
| 230 | cur->validate_fn = owl_variable_string_validate_default; |
---|
| 231 | if (!cur->set_fn) |
---|
| 232 | cur->set_fn = owl_variable_string_set_default; |
---|
| 233 | if (!cur->set_fromstring_fn) |
---|
| 234 | cur->set_fromstring_fn = owl_variable_string_set_fromstring_default; |
---|
| 235 | if (!cur->get_fn) |
---|
| 236 | cur->get_fn = owl_variable_get_default; |
---|
| 237 | if (!cur->get_tostring_fn) |
---|
| 238 | cur->get_tostring_fn = owl_variable_string_get_tostring_default; |
---|
| 239 | if (!cur->free_fn) |
---|
| 240 | cur->free_fn = owl_variable_free_default; |
---|
| 241 | cur->set_fn(cur, cur->pval_default); |
---|
| 242 | break; |
---|
| 243 | case OWL_VARIABLE_BOOL: |
---|
| 244 | if (!cur->validate_fn) |
---|
| 245 | cur->validate_fn = owl_variable_bool_validate_default; |
---|
| 246 | if (!cur->set_fn) |
---|
| 247 | cur->set_fn = owl_variable_bool_set_default; |
---|
| 248 | if (!cur->set_fromstring_fn) |
---|
| 249 | cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default; |
---|
| 250 | if (!cur->get_fn) |
---|
| 251 | cur->get_fn = owl_variable_get_default; |
---|
| 252 | if (!cur->get_tostring_fn) |
---|
| 253 | cur->get_tostring_fn = owl_variable_bool_get_tostring_default; |
---|
| 254 | if (!cur->free_fn) |
---|
| 255 | cur->free_fn = owl_variable_free_default; |
---|
| 256 | cur->val = owl_malloc(sizeof(int)); |
---|
| 257 | cur->set_fn(cur, &cur->ival_default); |
---|
| 258 | break; |
---|
| 259 | case OWL_VARIABLE_INT: |
---|
| 260 | if (!cur->validate_fn) |
---|
| 261 | cur->validate_fn = owl_variable_int_validate_default; |
---|
| 262 | if (!cur->set_fn) |
---|
| 263 | cur->set_fn = owl_variable_int_set_default; |
---|
| 264 | if (!cur->set_fromstring_fn) |
---|
| 265 | cur->set_fromstring_fn = owl_variable_int_set_fromstring_default; |
---|
| 266 | if (!cur->get_fn) |
---|
| 267 | cur->get_fn = owl_variable_get_default; |
---|
| 268 | if (!cur->get_tostring_fn) |
---|
| 269 | cur->get_tostring_fn = owl_variable_int_get_tostring_default; |
---|
| 270 | if (!cur->free_fn) |
---|
| 271 | cur->free_fn = owl_variable_free_default; |
---|
| 272 | cur->val = owl_malloc(sizeof(int)); |
---|
| 273 | cur->set_fn(cur, &cur->ival_default); |
---|
| 274 | break; |
---|
| 275 | default: |
---|
| 276 | fprintf(stderr, "owl_variable_setup: invalid variable type\n"); |
---|
| 277 | return(-2); |
---|
| 278 | } |
---|
| 279 | owl_dict_insert_element(vd, cur->name, (void*)cur, NULL); |
---|
| 280 | } |
---|
| 281 | return 0; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | void owl_variable_dict_free(owl_vardict *d) { |
---|
| 285 | owl_dict_free_all(d, (void(*)(void*))owl_variable_free); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /* free the list with owl_variable_dict_namelist_free */ |
---|
| 289 | void owl_variable_dict_get_names(owl_vardict *d, owl_list *l) { |
---|
| 290 | owl_dict_get_keys(d, l); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | void owl_variable_dict_namelist_free(owl_list *l) { |
---|
| 294 | owl_list_free_all(l, owl_free); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | void owl_variable_free(owl_variable *v) { |
---|
| 298 | if (v->free_fn) v->free_fn(v); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | char *owl_variable_get_docstring(owl_variable *v) { |
---|
| 303 | return v->docstring; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | char *owl_variable_get_validsettings(owl_variable *v) { |
---|
| 307 | if (v->validsettings) { |
---|
| 308 | return v->validsettings; |
---|
| 309 | } else { |
---|
| 310 | return ""; |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | /* functions for getting and setting variable values */ |
---|
| 315 | |
---|
| 316 | /* returns 0 on success, prints a status msg if msg is true */ |
---|
| 317 | int owl_variable_set_fromstring(owl_vardict *d, char *name, char *value, int msg) { |
---|
| 318 | owl_variable *v; |
---|
| 319 | char buff2[1024]; |
---|
| 320 | if (!name) return(-1); |
---|
| 321 | v = owl_dict_find_element(d, name); |
---|
| 322 | if (v == NULL) { |
---|
| 323 | if (msg) owl_function_makemsg("Unknown variable %s", name); |
---|
| 324 | return -1; |
---|
| 325 | } |
---|
| 326 | if (!v->set_fromstring_fn) { |
---|
| 327 | if (msg) owl_function_makemsg("Variable %s is read-only", name); |
---|
| 328 | return -1; |
---|
| 329 | |
---|
| 330 | } |
---|
| 331 | if (0 != v->set_fromstring_fn(v, value)) { |
---|
| 332 | if (msg) owl_function_makemsg("Unable to set %s (must be %s)", name, |
---|
| 333 | owl_variable_get_validsettings(v)); |
---|
| 334 | return -1; |
---|
| 335 | } |
---|
| 336 | if (msg && v->get_tostring_fn) { |
---|
| 337 | v->get_tostring_fn(v, buff2, 1024, v->val); |
---|
| 338 | owl_function_makemsg("%s = '%s'", name, buff2); |
---|
| 339 | } |
---|
| 340 | return 0; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | int owl_variable_set_string(owl_vardict *d, char *name, char *newval) { |
---|
| 344 | owl_variable *v; |
---|
| 345 | if (!name) return(-1); |
---|
| 346 | v = owl_dict_find_element(d, name); |
---|
| 347 | if (v == NULL || !v->set_fn) return(-1); |
---|
| 348 | if (v->type!=OWL_VARIABLE_STRING) return(-1); |
---|
| 349 | return v->set_fn(v, (void*)newval); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | int owl_variable_set_int(owl_vardict *d, char *name, int newval) { |
---|
| 353 | owl_variable *v; |
---|
| 354 | if (!name) return(-1); |
---|
| 355 | v = owl_dict_find_element(d, name); |
---|
| 356 | if (v == NULL || !v->set_fn) return(-1); |
---|
| 357 | if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1); |
---|
| 358 | return v->set_fn(v, &newval); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | int owl_variable_set_bool_on(owl_vardict *d, char *name) { |
---|
| 362 | return owl_variable_set_int(d,name,1); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | int owl_variable_set_bool_off(owl_vardict *d, char *name) { |
---|
| 366 | return owl_variable_set_int(d,name,0); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | int owl_variable_get_tostring(owl_vardict *d, char *name, char *buf, int bufsize) { |
---|
| 370 | owl_variable *v; |
---|
| 371 | if (!name) return(-1); |
---|
| 372 | v = owl_dict_find_element(d, name); |
---|
| 373 | if (v == NULL || !v->get_tostring_fn) return(-1); |
---|
| 374 | return v->get_tostring_fn(v, buf, bufsize, v->val); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | int owl_variable_get_default_tostring(owl_vardict *d, char *name, char *buf, int bufsize) { |
---|
| 378 | owl_variable *v; |
---|
| 379 | if (!name) return(-1); |
---|
| 380 | v = owl_dict_find_element(d, name); |
---|
| 381 | if (v == NULL || !v->get_tostring_fn) return(-1); |
---|
| 382 | if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { |
---|
| 383 | return v->get_tostring_fn(v, buf, bufsize, &(v->ival_default)); |
---|
| 384 | } else { |
---|
| 385 | return v->get_tostring_fn(v, buf, bufsize, v->pval_default); |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | /* returns a reference */ |
---|
| 390 | void *owl_variable_get(owl_vardict *d, char *name, int require_type) { |
---|
| 391 | owl_variable *v; |
---|
| 392 | if (!name) return(NULL); |
---|
| 393 | v = owl_dict_find_element(d, name); |
---|
| 394 | if (v == NULL || !v->get_fn || v->type != require_type) return(NULL); |
---|
| 395 | return v->get_fn(v); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | /* returns a reference */ |
---|
| 399 | char *owl_variable_get_string(owl_vardict *d, char *name) { |
---|
| 400 | return (char*)owl_variable_get(d,name, OWL_VARIABLE_STRING); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | /* returns a reference */ |
---|
| 404 | void *owl_variable_get_other(owl_vardict *d, char *name) { |
---|
| 405 | return (char*)owl_variable_get(d,name, OWL_VARIABLE_OTHER); |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | int owl_variable_get_int(owl_vardict *d, char *name) { |
---|
| 409 | int *pi; |
---|
| 410 | pi = (int*)owl_variable_get(d,name,OWL_VARIABLE_INT); |
---|
| 411 | if (!pi) return(-1); |
---|
| 412 | return(*pi); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | int owl_variable_get_bool(owl_vardict *d, char *name) { |
---|
| 416 | int *pi; |
---|
| 417 | pi = (int*)owl_variable_get(d,name,OWL_VARIABLE_BOOL); |
---|
| 418 | if (!pi) return(-1); |
---|
| 419 | return(*pi); |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | #define OWL_VARIABLE_HELP_FMT " %-15s %-9s %-9s %s\n" |
---|
| 423 | |
---|
| 424 | /* appends to the end of the fmtext */ |
---|
| 425 | void owl_variable_get_summaryheader(owl_fmtext *fm) { |
---|
| 426 | char tmpbuff[512]; |
---|
| 427 | snprintf(tmpbuff, 512, |
---|
| 428 | OWL_VARIABLE_HELP_FMT OWL_VARIABLE_HELP_FMT, |
---|
| 429 | "name", "settings", "default", "meaning", |
---|
| 430 | "---------------", "--------", "-------", "-------"); |
---|
| 431 | owl_fmtext_append_bold(fm, tmpbuff); |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | void owl_variable_get_summary(owl_vardict *d, char *name, owl_fmtext *fm) { |
---|
| 435 | char defaultbuf[10]; |
---|
| 436 | char buf[1024]; |
---|
| 437 | int buflen = 1023; |
---|
| 438 | owl_variable *v; |
---|
| 439 | |
---|
| 440 | if (!name |
---|
| 441 | || (v = owl_dict_find_element(d, name)) == NULL |
---|
| 442 | || !v->get_fn) { |
---|
| 443 | snprintf(buf, buflen, " No such variable '%s'\n", name); |
---|
| 444 | owl_fmtext_append_normal(fm, buf); |
---|
| 445 | return; |
---|
| 446 | } |
---|
| 447 | if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { |
---|
| 448 | v->get_tostring_fn(v, defaultbuf, 10, &(v->ival_default)); |
---|
| 449 | } else { |
---|
| 450 | v->get_tostring_fn(v, defaultbuf, 10, v->pval_default); |
---|
| 451 | } |
---|
| 452 | snprintf(buf, buflen, OWL_VARIABLE_HELP_FMT, |
---|
| 453 | v->name, |
---|
| 454 | owl_variable_get_validsettings(v), |
---|
| 455 | defaultbuf, |
---|
| 456 | owl_variable_get_docstring(v)); |
---|
| 457 | owl_fmtext_append_normal(fm, buf); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | void owl_variable_get_help(owl_vardict *d, char *name, owl_fmtext *fm) { |
---|
| 461 | char buff[1024]; |
---|
| 462 | int bufflen = 1023; |
---|
| 463 | owl_variable *v; |
---|
| 464 | |
---|
| 465 | if (!name |
---|
| 466 | || (v = owl_dict_find_element(d, name)) == NULL |
---|
| 467 | || !v->get_fn) { |
---|
| 468 | owl_fmtext_append_normal(fm, "No such variable...\n"); |
---|
| 469 | return; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n"); |
---|
| 473 | owl_fmtext_append_normal(fm, OWL_TABSTR); |
---|
| 474 | owl_fmtext_append_normal(fm, name); |
---|
| 475 | owl_fmtext_append_normal(fm, " - "); |
---|
| 476 | owl_fmtext_append_normal(fm, v->docstring); |
---|
| 477 | owl_fmtext_append_normal(fm, "\n\n"); |
---|
| 478 | |
---|
| 479 | owl_fmtext_append_normal(fm, "Current: "); |
---|
| 480 | owl_variable_get_tostring(d, name, buff, bufflen); |
---|
| 481 | owl_fmtext_append_normal(fm, buff); |
---|
| 482 | owl_fmtext_append_normal(fm, "\n\n"); |
---|
| 483 | |
---|
| 484 | |
---|
| 485 | if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { |
---|
| 486 | v->get_tostring_fn(v, buff, bufflen, &(v->ival_default)); |
---|
| 487 | } else { |
---|
| 488 | v->get_tostring_fn(v, buff, bufflen, v->pval_default); |
---|
| 489 | } |
---|
| 490 | owl_fmtext_append_normal(fm, "Default: "); |
---|
| 491 | owl_fmtext_append_normal(fm, buff); |
---|
| 492 | owl_fmtext_append_normal(fm, "\n\n"); |
---|
| 493 | |
---|
| 494 | owl_fmtext_append_normal(fm, "Valid Settings: "); |
---|
| 495 | owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v)); |
---|
| 496 | owl_fmtext_append_normal(fm, "\n\n"); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | /**************************************************************************/ |
---|
| 503 | /*********************** GENERAL TYPE-SPECIFIC ****************************/ |
---|
| 504 | /**************************************************************************/ |
---|
| 505 | |
---|
| 506 | /* default common functions */ |
---|
| 507 | |
---|
| 508 | void *owl_variable_get_default(owl_variable *v) { |
---|
| 509 | return v->val; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | void owl_variable_free_default(owl_variable *v) { |
---|
| 513 | if (v->val) owl_free(v->val); |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | /* default functions for booleans */ |
---|
| 517 | |
---|
| 518 | int owl_variable_bool_validate_default(owl_variable *v, void *newval) { |
---|
| 519 | if (newval == NULL) return(0); |
---|
| 520 | else if (*(int*)newval==1 || *(int*)newval==0) return(1); |
---|
| 521 | else return (0); |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | int owl_variable_bool_set_default(owl_variable *v, void *newval) { |
---|
| 525 | if (v->validate_fn) { |
---|
| 526 | if (!v->validate_fn(v, newval)) return(-1); |
---|
| 527 | } |
---|
| 528 | *(int*)v->val = *(int*)newval; |
---|
| 529 | return(0); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | int owl_variable_bool_set_fromstring_default(owl_variable *v, char *newval) { |
---|
| 533 | int i; |
---|
| 534 | if (!strcmp(newval, "on")) i=1; |
---|
| 535 | else if (!strcmp(newval, "off")) i=0; |
---|
| 536 | else return(-1); |
---|
| 537 | return (v->set_fn(v, &i)); |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | int owl_variable_bool_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) { |
---|
| 541 | if (val == NULL) { |
---|
| 542 | snprintf(buf, bufsize, "<null>"); |
---|
| 543 | return -1; |
---|
| 544 | } else if (*(int*)val == 0) { |
---|
| 545 | snprintf(buf, bufsize, "off"); |
---|
| 546 | return 0; |
---|
| 547 | } else if (*(int*)val == 1) { |
---|
| 548 | snprintf(buf, bufsize, "on"); |
---|
| 549 | return 0; |
---|
| 550 | } else { |
---|
| 551 | snprintf(buf, bufsize, "<invalid>"); |
---|
| 552 | return -1; |
---|
| 553 | } |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | /* default functions for integers */ |
---|
| 557 | |
---|
| 558 | int owl_variable_int_validate_default(owl_variable *v, void *newval) { |
---|
| 559 | if (newval == NULL) return(0); |
---|
| 560 | else return (1); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | int owl_variable_int_set_default(owl_variable *v, void *newval) { |
---|
| 564 | if (v->validate_fn) { |
---|
| 565 | if (!v->validate_fn(v, newval)) return(-1); |
---|
| 566 | } |
---|
| 567 | *(int*)v->val = *(int*)newval; |
---|
| 568 | return(0); |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | int owl_variable_int_set_fromstring_default(owl_variable *v, char *newval) { |
---|
| 572 | int i; |
---|
| 573 | char *ep = "x"; |
---|
| 574 | i = strtol(newval, &ep, 10); |
---|
| 575 | if (*ep || ep==newval) return(-1); |
---|
| 576 | return (v->set_fn(v, &i)); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | int owl_variable_int_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) { |
---|
| 580 | if (val == NULL) { |
---|
| 581 | snprintf(buf, bufsize, "<null>"); |
---|
| 582 | return -1; |
---|
| 583 | } else { |
---|
| 584 | snprintf(buf, bufsize, "%d", *(int*)val); |
---|
| 585 | return 0; |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | /* default functions for enums (a variant of integers) */ |
---|
| 590 | |
---|
| 591 | int owl_variable_enum_validate(owl_variable *v, void *newval) { |
---|
| 592 | char **enums; |
---|
| 593 | int nenums, val; |
---|
| 594 | if (newval == NULL) return(0); |
---|
| 595 | enums = atokenize(v->validsettings, ",", &nenums); |
---|
| 596 | if (enums == NULL) return(0); |
---|
| 597 | atokenize_free(enums, nenums); |
---|
| 598 | val = *(int*)newval; |
---|
| 599 | if (val < 0 || val >= nenums) { |
---|
| 600 | return(0); |
---|
| 601 | } |
---|
| 602 | return(1); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | int owl_variable_enum_set_fromstring(owl_variable *v, char *newval) { |
---|
| 606 | char **enums; |
---|
| 607 | int nenums, i, val=-1; |
---|
| 608 | if (newval == NULL) return(-1); |
---|
| 609 | enums = atokenize(v->validsettings, ",", &nenums); |
---|
| 610 | if (enums == NULL) return(-1); |
---|
| 611 | for (i=0; i<nenums; i++) { |
---|
| 612 | if (0==strcmp(newval, enums[i])) { |
---|
| 613 | val = i; |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | atokenize_free(enums, nenums); |
---|
| 617 | if (val == -1) return(-1); |
---|
| 618 | return (v->set_fn(v, &val)); |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | int owl_variable_enum_get_tostring(owl_variable *v, char* buf, int bufsize, void *val) { |
---|
| 622 | char **enums; |
---|
| 623 | int nenums, i; |
---|
| 624 | |
---|
| 625 | if (val == NULL) { |
---|
| 626 | snprintf(buf, bufsize, "<null>"); |
---|
| 627 | return -1; |
---|
| 628 | } |
---|
| 629 | enums = atokenize(v->validsettings, ",", &nenums); |
---|
| 630 | i = *(int*)val; |
---|
| 631 | if (i<0 || i>=nenums) { |
---|
| 632 | snprintf(buf, bufsize, "<invalid:%d>",i); |
---|
| 633 | atokenize_free(enums, nenums); |
---|
| 634 | return(-1); |
---|
| 635 | } |
---|
| 636 | snprintf(buf, bufsize, "%s", enums[i]); |
---|
| 637 | return 0; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | /* default functions for stringeans */ |
---|
| 641 | |
---|
| 642 | int owl_variable_string_validate_default(struct _owl_variable *v, void *newval) { |
---|
| 643 | if (newval == NULL) return(0); |
---|
| 644 | else return (1); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | int owl_variable_string_set_default(owl_variable *v, void *newval) { |
---|
| 648 | if (v->validate_fn) { |
---|
| 649 | if (!v->validate_fn(v, newval)) return(-1); |
---|
| 650 | } |
---|
| 651 | if (v->val) owl_free(v->val); |
---|
| 652 | v->val = owl_strdup(newval); |
---|
| 653 | return(0); |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | int owl_variable_string_set_fromstring_default(owl_variable *v, char *newval) { |
---|
| 657 | return (v->set_fn(v, newval)); |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | int owl_variable_string_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) { |
---|
| 661 | if (val == NULL) { |
---|
| 662 | snprintf(buf, bufsize, "<null>"); |
---|
| 663 | return -1; |
---|
| 664 | } else { |
---|
| 665 | snprintf(buf, bufsize, "%s", (char*)val); |
---|
| 666 | return 0; |
---|
| 667 | } |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | |
---|
| 671 | |
---|
| 672 | /**************************************************************************/ |
---|
| 673 | /************************* REGRESSION TESTS *******************************/ |
---|
| 674 | /**************************************************************************/ |
---|
| 675 | |
---|
| 676 | #ifdef OWL_INCLUDE_REG_TESTS |
---|
| 677 | |
---|
| 678 | #define FAIL_UNLESS(desc,pred) printf("\t%-4s: %s\n", (pred)?"ok":(numfailed++,"FAIL"), desc) |
---|
| 679 | |
---|
| 680 | int owl_variable_regtest(void) { |
---|
| 681 | owl_vardict vd; |
---|
| 682 | int numfailed=0; |
---|
| 683 | char buf[1024]; |
---|
| 684 | |
---|
[e1c4636] | 685 | in_regtest = 1; |
---|
| 686 | |
---|
[7d4fbcd] | 687 | printf("BEGIN testing owl_variable\n"); |
---|
| 688 | FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd)); |
---|
| 689 | |
---|
| 690 | FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"personalbell")); |
---|
| 691 | FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble")); |
---|
| 692 | FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"personalbell", buf, 1024)); |
---|
| 693 | FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off")); |
---|
| 694 | FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"personalbell")); |
---|
| 695 | FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"personalbell")); |
---|
| 696 | FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"personalbell","off",0)); |
---|
| 697 | FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"personalbell")); |
---|
| 698 | FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"personalbell","xxx",0)); |
---|
| 699 | FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"personalbell")); |
---|
| 700 | |
---|
| 701 | |
---|
| 702 | FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath"))); |
---|
| 703 | FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee")); |
---|
| 704 | FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath"))); |
---|
| 705 | |
---|
| 706 | FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize")); |
---|
| 707 | FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble")); |
---|
| 708 | FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024)); |
---|
| 709 | FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8")); |
---|
| 710 | FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12)); |
---|
| 711 | FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize")); |
---|
| 712 | FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3)); |
---|
| 713 | FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize")); |
---|
| 714 | FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0)); |
---|
| 715 | FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize")); |
---|
| 716 | FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0)); |
---|
| 717 | FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0)); |
---|
| 718 | FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize")); |
---|
| 719 | |
---|
| 720 | FAIL_UNLESS("get enum", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser")); |
---|
| 721 | FAIL_UNLESS("get enum as string 1", 0==owl_variable_get_tostring(&vd,"webbrowser", buf, 1024)); |
---|
| 722 | FAIL_UNLESS("get enum as string 2", 0==strcmp(buf,"netscape")); |
---|
| 723 | FAIL_UNLESS("set enum 1", 0==owl_variable_set_int(&vd,"webbrowser",OWL_WEBBROWSER_GALEON)); |
---|
| 724 | FAIL_UNLESS("get enum 2", OWL_WEBBROWSER_GALEON==owl_variable_get_int(&vd,"webbrowser")); |
---|
| 725 | FAIL_UNLESS("set enum 1b", -1==owl_variable_set_int(&vd,"webbrowser",-3)); |
---|
| 726 | FAIL_UNLESS("set enum 1b", -1==owl_variable_set_int(&vd,"webbrowser",209)); |
---|
| 727 | FAIL_UNLESS("get enum 2b", OWL_WEBBROWSER_GALEON==owl_variable_get_int(&vd,"webbrowser")); |
---|
| 728 | FAIL_UNLESS("set enum 3", 0==owl_variable_set_fromstring(&vd,"webbrowser","none",0)); |
---|
| 729 | FAIL_UNLESS("get enum 4", OWL_WEBBROWSER_NONE==owl_variable_get_int(&vd,"webbrowser")); |
---|
| 730 | FAIL_UNLESS("set enum 5", 0==owl_variable_set_fromstring(&vd,"webbrowser","netscape",0)); |
---|
| 731 | FAIL_UNLESS("get enum 6", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser")); |
---|
| 732 | FAIL_UNLESS("set enum 7", -1==owl_variable_set_fromstring(&vd,"webbrowser","xxx",0)); |
---|
| 733 | FAIL_UNLESS("set enum 8", -1==owl_variable_set_fromstring(&vd,"webbrowser","",0)); |
---|
| 734 | FAIL_UNLESS("set enum 9", -1==owl_variable_set_fromstring(&vd,"webbrowser","netscapey",0)); |
---|
| 735 | FAIL_UNLESS("get enum 10", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser")); |
---|
| 736 | |
---|
| 737 | |
---|
| 738 | |
---|
| 739 | owl_variable_dict_free(&vd); |
---|
| 740 | |
---|
| 741 | if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); |
---|
| 742 | printf("END testing owl_variable (%d failures)\n", numfailed); |
---|
| 743 | return(numfailed); |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | |
---|
| 747 | #endif /* OWL_INCLUDE_REG_TESTS */ |
---|