Changes in variable.c [6a8b519:8258ea5]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
variable.c
r6a8b519 r8258ea5 1 1 #include "owl.h" 2 2 #include <stdio.h> 3 #include "gmarshal_funcs.h" 4 5 /* TODO(davidben): When we can require 2.30 and up, remove this. */ 6 #ifndef G_VALUE_INIT 7 #define G_VALUE_INIT { 0, { { 0 } } } 8 #endif 9 10 typedef const char *(*get_string_t)(const owl_variable *); 11 typedef int (*get_int_t)(const owl_variable *); 12 typedef bool (*get_bool_t)(const owl_variable *); 13 14 typedef int (*set_string_t)(owl_variable *, const char *); 15 typedef int (*set_int_t)(owl_variable *, int); 16 typedef int (*set_bool_t)(owl_variable *, bool); 17 18 typedef int (*validate_string_t)(const owl_variable *, const char *); 19 typedef int (*validate_int_t)(const owl_variable *, int); 20 typedef int (*validate_bool_t)(const owl_variable *, bool); 21 22 static void owl_variable_dict_newvar_bool_full(owl_vardict *vd, 23 const char *name, 24 bool default_val, 25 const char *summary, 26 const char *description, 27 validate_bool_t validate_fn, 28 set_bool_t set_fn, 29 get_bool_t get_fn); 30 31 static void owl_variable_dict_newvar_string_full(owl_vardict *vd, 32 const char *name, 33 const char *default_val, 34 const char *summary, 35 const char *description, 36 const char *validsettings, 37 validate_string_t validate_fn, 38 set_string_t set_fn, 39 get_string_t get_fn); 40 41 static void owl_variable_dict_newvar_int_full(owl_vardict *vd, 42 const char *name, 43 int default_val, 44 const char *summary, 45 const char *description, 46 const char *validsettings, 47 validate_int_t validate_fn, 48 set_int_t set_fn, 49 get_int_t get_fn); 50 51 static void owl_variable_dict_newvar_enum_full(owl_vardict *vd, 52 const char *name, 53 int default_val, 54 const char *summary, 55 const char *description, 56 const char *validsettings, 57 validate_int_t validate_fn, 58 set_int_t set_fn, 59 get_int_t get_fn); 60 61 #define OWLVAR_BOOL(name, default, summary, description) \ 62 owl_variable_dict_newvar_bool(vd, name, default, summary, description) 63 64 #define OWLVAR_BOOL_FULL(name, default, summary, description, validate, set, get) \ 65 owl_variable_dict_newvar_bool_full(vd, name, default, summary, description, \ 66 validate, set, get) 67 68 #define OWLVAR_INT(name, default, summary, description) \ 69 owl_variable_dict_newvar_int(vd, name, default, summary, description) 3 4 #define OWLVAR_BOOL(name,default,summary,description) \ 5 { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \ 6 NULL, NULL, NULL, NULL, NULL, NULL } 7 8 #define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \ 9 { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \ 10 validate, set, NULL, get, NULL, NULL } 11 12 #define OWLVAR_INT(name,default,summary,description) \ 13 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, "<int>", g_strdup(summary), g_strdup(description), NULL, \ 14 NULL, NULL, NULL, NULL, NULL, NULL } 70 15 71 16 #define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \ 72 owl_variable_dict_newvar_int_full(vd, name, default, summary, description, \ 73 validset, validate, set, get) 74 75 #define OWLVAR_PATH(name, default, summary, description) \ 76 owl_variable_dict_newvar_path(vd, name, default, summary, description) 77 78 #define OWLVAR_STRING(name, default, summary, description) \ 79 owl_variable_dict_newvar_string(vd, name, default, summary, description) 80 81 #define OWLVAR_STRING_FULL(name, default, validset, summary, description, validate, set, get) \ 82 owl_variable_dict_newvar_string_full(vd, name, default, summary, description, \ 83 validset, validate, set, get) 17 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \ 18 validate, set, NULL, get, NULL, NULL } 19 20 #define OWLVAR_PATH(name,default,summary,description) \ 21 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<path>", g_strdup(summary), g_strdup(description), NULL, \ 22 NULL, NULL, NULL, NULL, NULL, NULL } 23 24 #define OWLVAR_STRING(name,default,summary,description) \ 25 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<string>", g_strdup(summary), g_strdup(description), NULL, \ 26 NULL, NULL, NULL, NULL, NULL, NULL } 27 28 #define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \ 29 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, validset, g_strdup(summary), g_strdup(description), NULL, \ 30 validate, set, NULL, get, NULL, NULL } 84 31 85 32 /* enums are really integers, but where validset is a comma-separated 86 33 * list of strings which can be specified. The tokens, starting at 0, 87 34 * correspond to the values that may be specified. */ 88 #define OWLVAR_ENUM(name, default, summary, description, validset) \ 89 owl_variable_dict_newvar_enum(vd, name, default, summary, description, validset) 35 #define OWLVAR_ENUM(name,default,summary,description,validset) \ 36 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \ 37 owl_variable_enum_validate, \ 38 NULL, owl_variable_enum_set_fromstring, \ 39 NULL, owl_variable_enum_get_tostring, \ 40 NULL } 90 41 91 42 #define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \ 92 owl_variable_dict_newvar_enum_full(vd, name, default, summary, description, \ 93 validset, validate, set, get) 94 95 void owl_variable_add_defaults(owl_vardict *vd) 96 { 43 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \ 44 validate, \ 45 set, owl_variable_enum_set_fromstring, \ 46 get, owl_variable_enum_get_tostring, \ 47 NULL } 48 49 int owl_variable_add_defaults(owl_vardict *vd) 50 { 51 owl_variable variables_to_init[] = { 52 97 53 OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off", 98 54 "ring the terminal bell when personal messages are received", 99 55 "Can be set to 'on', 'off', or the name of a filter which\n" 100 "messages need to match in order to ring the bell") ;56 "messages need to match in order to ring the bell"), 101 57 102 58 OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1, 103 "enable / disable the terminal bell", "" ) ;59 "enable / disable the terminal bell", "" ), 104 60 105 61 OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG, … … 107 63 "If set to 'on', debugging messages are logged to the\n" 108 64 "file specified by the debugfile variable.\n", 109 NULL, owl_variable_debug_set, NULL) ;65 NULL, owl_variable_debug_set, NULL), 110 66 111 67 OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1, 112 "send a login message when BarnOwl starts", "" ) ;68 "send a login message when BarnOwl starts", "" ), 113 69 114 70 OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1, 115 "send a logout message when BarnOwl exits", "" ) ;71 "send a logout message when BarnOwl exits", "" ), 116 72 117 73 OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0, 118 "display received pings", "" ) ;74 "display received pings", "" ), 119 75 120 76 OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1, 121 "send pings", "" ) ;77 "send pings", "" ), 122 78 123 79 OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0, 124 "disable printing information in the separator bar", "" ) ;80 "disable printing information in the separator bar", "" ), 125 81 126 82 OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1, 127 "strip kerberos instance for reply", "") ;83 "strip kerberos instance for reply", ""), 128 84 129 85 OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1, 130 "strip leading and trailing newlines", "") ;86 "strip leading and trailing newlines", ""), 131 87 132 88 OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1, 133 "display outgoing messages", "" ) ;89 "display outgoing messages", "" ), 134 90 135 91 OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1, 136 "load logins from .anyone on startup", "" ) ;92 "load logins from .anyone on startup", "" ), 137 93 138 94 OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0, … … 141 97 "logged in the directory specified\n" 142 98 "by the 'logpath' variable. The filename in that\n" 143 "directory is derived from the sender of the message.\n" ) ;99 "directory is derived from the sender of the message.\n" ), 144 100 145 101 OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0, … … 149 105 "by the 'classlogpath' variable.\n" 150 106 "The filename in that directory is derived from\n" 151 "the name of the class to which the message was sent.\n" ) ;107 "the name of the class to which the message was sent.\n" ), 152 108 153 109 OWLVAR_ENUM( "loggingdirection" /* %OwlVarStub */, OWL_LOGGING_DIRECTION_BOTH, … … 158 114 "is selected both incoming and outgoing messages are\n" 159 115 "logged.", 160 "both,in,out") ;116 "both,in,out"), 161 117 162 118 OWLVAR_BOOL_FULL( "colorztext" /* %OwlVarStub */, 1, 163 119 "allow @color() in zephyrs to change color", 164 NULL, NULL, owl_variable_colorztext_set, NULL) ;120 NULL, NULL, owl_variable_colorztext_set, NULL), 165 121 166 122 OWLVAR_BOOL( "fancylines" /* %OwlVarStub */, 1, … … 168 124 "If turned off, dashes, pipes and pluses will be used\n" 169 125 "to draw lines on the screen. Useful when the terminal\n" 170 "is causing problems" ) ;126 "is causing problems" ), 171 127 172 128 OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1, 173 129 "Do automatic zcrypt processing", 174 "" ) ;130 "" ), 175 131 176 132 OWLVAR_BOOL_FULL( "pseudologins" /* %OwlVarStub */, 0, … … 180 136 "but sent no login message, or a user is not present that sent no\n" 181 137 "logout message, a pseudo login or logout message will be created\n", 182 NULL, owl_variable_pseudologins_set, NULL) ;138 NULL, owl_variable_pseudologins_set, NULL), 183 139 184 140 OWLVAR_BOOL( "ignorelogins" /* %OwlVarStub */, 0, … … 186 142 "When this is enabled, BarnOwl will print login and logout notifications\n" 187 143 "for AIM, zephyr, or other protocols. If disabled BarnOwl will not print\n" 188 "login or logout notifications.\n") ;144 "login or logout notifications.\n"), 189 145 190 146 OWLVAR_STRING( "logfilter" /* %OwlVarStub */, "", … … 195 151 "variables like logging, classlogging, loglogins, loggingdirection,\n" 196 152 "etc. If you want this variable to control all logging, make sure\n" 197 "all other logging variables are in their default state.\n") ;153 "all other logging variables are in their default state.\n"), 198 154 199 155 OWLVAR_BOOL( "loglogins" /* %OwlVarStub */, 0, … … 201 157 "When this is enabled, BarnOwl will log login and logout notifications\n" 202 158 "for AIM, zephyr, or other protocols. If disabled BarnOwl will not print\n" 203 "login or logout notifications.\n") ;159 "login or logout notifications.\n"), 204 160 205 161 OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1, … … 213 169 "in the editmulti keymap.\n", 214 170 "off,middle,on", 215 NULL, owl_variable_disable_ctrl_d_set, NULL) ;171 NULL, owl_variable_disable_ctrl_d_set, NULL), 216 172 217 173 OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people", 218 174 "path for logging personal zephyrs", 219 175 "Specifies a directory which must exist.\n" 220 "Files will be created in the directory for each sender.\n") ;176 "Files will be created in the directory for each sender.\n"), 221 177 222 178 OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class", 223 179 "path for logging class zephyrs", 224 180 "Specifies a directory which must exist.\n" 225 "Files will be created in the directory for each class.\n") ;181 "Files will be created in the directory for each class.\n"), 226 182 227 183 OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE, 228 184 "path for logging debug messages when debugging is enabled", 229 185 "This file will be logged to if 'debug' is set to 'on'.\n" 230 "BarnOwl will append a dot and the current process's pid to the filename.") ;186 "BarnOwl will append a dot and the current process's pid to the filename."), 231 187 232 188 OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL, … … 236 192 "See the documentation for 'zsig' for more information about\n" 237 193 "how the outgoing zsig is chosen." 238 ) ;194 ), 239 195 240 196 OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL, … … 242 198 "The named program will be run when BarnOwl receives new\n" 243 199 "messages. It will not be run again until the first\n" 244 "instance exits") ;200 "instance exits"), 245 201 246 202 OWLVAR_STRING( "zsender" /* %OwlVarStub */, "", … … 249 205 "zephyrs. If this is unset, it will use your Kerberos\n" 250 206 "principal. Note that customizing the sender name will\n" 251 "cause your zephyrs to be sent unauthenticated.") ;207 "cause your zephyrs to be sent unauthenticated."), 252 208 253 209 OWLVAR_STRING( "zsigfunc" /* %OwlVarStub */, "BarnOwl::default_zephyr_signature()", … … 256 212 "explicit zsig. The default setting implements the policy\n" 257 213 "described in the documentation for the 'zsig' variable.\n" 258 "See also BarnOwl::random_zephyr_signature().\n") ;214 "See also BarnOwl::random_zephyr_signature().\n"), 259 215 260 216 OWLVAR_STRING( "zsig" /* %OwlVarStub */, "", … … 263 219 "unset, 'zsigproc' will be run to generate a zsig. If that is\n" 264 220 "also unset, the 'zwrite-signature' zephyr variable will be\n" 265 "used instead.\n") ;221 "used instead.\n"), 266 222 267 223 OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "", … … 269 225 "The sepbar is the bar separating the top and bottom\n" 270 226 "of the BarnOwl screen. Any string specified here will\n" 271 "be displayed on the right of the sepbar\n") ;227 "be displayed on the right of the sepbar\n"), 272 228 273 229 OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0, 274 "turn zaway on or off", "" ) ;230 "turn zaway on or off", "" ), 275 231 276 232 OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 277 233 OWL_DEFAULT_ZAWAYMSG, 278 "zaway msg for responding to zephyrs when away", "" ) ;234 "zaway msg for responding to zephyrs when away", "" ), 279 235 280 236 OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 281 237 OWL_DEFAULT_ZAWAYMSG, 282 "default zaway message", "" ) ;238 "default zaway message", "" ), 283 239 284 240 OWLVAR_BOOL_FULL( "aaway" /* %OwlVarStub */, 0, 285 241 "Set AIM away status", 286 242 "", 287 NULL, owl_variable_aaway_set, NULL) ;243 NULL, owl_variable_aaway_set, NULL), 288 244 289 245 OWLVAR_STRING( "aaway_msg" /* %OwlVarStub */, 290 246 OWL_DEFAULT_AAWAYMSG, 291 "AIM away msg for responding when away", "" ) ;247 "AIM away msg for responding when away", "" ), 292 248 293 249 OWLVAR_STRING( "aaway_msg_default" /* %OwlVarStub */, 294 250 OWL_DEFAULT_AAWAYMSG, 295 "default AIM away message", "" ) ;251 "default AIM away message", "" ), 296 252 297 253 OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all", 298 254 "home view to switch to after 'X' and 'V'", 299 "SEE ALSO: view, filter\n" ) ;255 "SEE ALSO: view, filter\n" ), 300 256 301 257 OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none", 302 258 "filter on which to trigger alert actions", 303 "" ) ;259 "" ), 304 260 305 261 OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop", 306 262 "BarnOwl command to execute for alert actions", 307 "" ) ;263 "" ), 308 264 309 265 OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "<string>", "tty name for zephyr location", "", 310 NULL, owl_variable_tty_set, NULL) ;266 NULL, owl_variable_tty_set, NULL), 311 267 312 268 OWLVAR_STRING( "default_style" /* %OwlVarStub */, "default", … … 319 275 " perl - legacy perl interface\n" 320 276 "\nSEE ALSO: style, show styles, view -s <style>\n" 321 ) ;277 ), 322 278 323 279 … … 326 282 "This specifies the maximum number of columns for M-q to fill text\n" 327 283 "to. If set to 0, M-q will wrap to the width of the window, and\n" 328 "values less than 0 disable M-q entirely.\n") ;284 "values less than 0 disable M-q entirely.\n"), 329 285 330 286 OWLVAR_INT( "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70, … … 335 291 "\n" 336 292 "As a courtesy to recipients, it is recommended that outgoing\n" 337 "Zephyr messages be no wider than 70 columns.\n") ;293 "Zephyr messages be no wider than 70 columns.\n"), 338 294 339 295 OWLVAR_INT( "aim_ignorelogin_timer" /* %OwlVarStub */, 15, … … 342 298 "AIM login before allowing the receipt of AIM login notifications.\n" 343 299 "By default this is set to 15. If you would like to view login\n" 344 "notifications of buddies as soon as you login, set it to 0 instead.") ;300 "notifications of buddies as soon as you login, set it to 0 instead."), 345 301 346 302 … … 355 311 owl_variable_typewinsize_set, 356 312 NULL /* use default for get */ 357 ) ;313 ), 358 314 359 315 OWLVAR_INT( "typewindelta" /* %OwlVarStub */, 0, … … 365 321 "typewinsize to 1.\n\n" 366 322 "This works a lot better with a non-default scrollmode;\n" 367 "try :set scrollmode pagedcenter.\n") ;323 "try :set scrollmode pagedcenter.\n"), 368 324 369 325 OWLVAR_ENUM( "scrollmode" /* %OwlVarStub */, OWL_SCROLLMODE_NORMAL, … … 394 350 " the screen will be paged up or down and\n" 395 351 " the cursor will be near the center.\n", 396 "normal,top,neartop,center,paged,pagedcenter" ) ;352 "normal,top,neartop,center,paged,pagedcenter" ), 397 353 398 354 OWLVAR_BOOL( "narrow-related" /* %OwlVarStub:narrow_related */, 1, … … 402 358 "for Zephyr, this controls whether to narrow to e.g. class-help or\n" 403 359 "class-help.d alone, or to related-class-help, which includes\n" 404 "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" ) ;360 "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" ), 405 361 406 362 OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0, … … 409 365 "continue to follow the last message if this is set.\n" 410 366 "Note that this is currently risky as you might accidentally\n" 411 "delete a message right as it came in.\n" ) ;367 "delete a message right as it came in.\n" ), 412 368 413 369 OWLVAR_STRING_FULL( "default_exposure" /* %OwlVarStub */, "", … … 418 374 "~/.zephyr.vars.\n" 419 375 "See the description of exposure for the values this can be.", 420 NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get ) ;376 NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get ), 421 377 422 378 OWLVAR_STRING_FULL( "exposure" /* %OwlVarStub */, "", … … 474 430 " personal subscriptions will be entered for the\n" 475 431 " user.\n", 476 NULL, owl_variable_exposure_set, NULL /* use default for get */ ); 432 NULL, owl_variable_exposure_set, NULL /* use default for get */ ), 433 434 /* This MUST be last... */ 435 { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL, 436 NULL, NULL, NULL, NULL, NULL, NULL } 437 438 }; 439 440 int ret = owl_variable_dict_add_from_list(vd, variables_to_init); 441 owl_variable *var; 442 for (var = variables_to_init; var->name != NULL; var++) 443 owl_variable_cleanup(var); 444 return ret; 477 445 } 478 446 … … 484 452 /* commonly useful */ 485 453 486 int owl_variable_int_validate_gt0(const owl_variable *v, int newval) 487 { 488 return !(newval < 1); 489 } 490 491 int owl_variable_int_validate_positive(const owl_variable *v, int newval) 492 { 493 return !(newval < 0); 454 int owl_variable_int_validate_gt0(const owl_variable *v, const void *newval) 455 { 456 if (newval == NULL) return(0); 457 else if (*(const int*)newval < 1) return(0); 458 else return (1); 459 } 460 461 int owl_variable_int_validate_positive(const owl_variable *v, const void *newval) 462 { 463 if (newval == NULL) return(0); 464 else if (*(const int*)newval < 0) return(0); 465 else return (1); 494 466 } 495 467 496 468 /* typewinsize */ 497 int owl_variable_typewinsize_set(owl_variable *v, intnewval)469 int owl_variable_typewinsize_set(owl_variable *v, const void *newval) 498 470 { 499 471 int rv; … … 504 476 505 477 /* debug (cache value in g->debug) */ 506 int owl_variable_debug_set(owl_variable *v, bool newval) 507 { 508 g.debug = newval; 478 int owl_variable_debug_set(owl_variable *v, const void *newval) 479 { 480 if (newval && (*(const int*)newval == 1 || *(const int*)newval == 0)) { 481 g.debug = *(const int*)newval; 482 } 509 483 return owl_variable_bool_set_default(v, newval); 510 484 } 511 485 512 486 /* When 'aaway' is changed, need to notify the AIM server */ 513 int owl_variable_aaway_set(owl_variable *v, boolnewval)487 int owl_variable_aaway_set(owl_variable *v, const void *newval) 514 488 { 515 489 if (newval) { 516 owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g)); 517 } else { 518 owl_aim_set_awaymsg(""); 490 if (*(const int*)newval == 1) { 491 owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g)); 492 } else if (*(const int*)newval == 0) { 493 owl_aim_set_awaymsg(""); 494 } 519 495 } 520 496 return owl_variable_bool_set_default(v, newval); 521 497 } 522 498 523 int owl_variable_colorztext_set(owl_variable *v, boolnewval)499 int owl_variable_colorztext_set(owl_variable *v, const void *newval) 524 500 { 525 501 int ret = owl_variable_bool_set_default(v, newval); … … 534 510 } 535 511 536 int owl_variable_pseudologins_set(owl_variable *v, boolnewval)512 int owl_variable_pseudologins_set(owl_variable *v, const void *newval) 537 513 { 538 514 static guint timer = 0; 539 515 if (newval) { 540 owl_function_zephyr_buddy_check(0); 541 if (timer == 0) { 542 timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL); 543 } 544 } else { 545 if (timer != 0) { 546 g_source_remove(timer); 547 timer = 0; 516 if (*(const int*)newval == 1) { 517 owl_function_zephyr_buddy_check(0); 518 if (timer == 0) { 519 timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL); 520 } 521 } else { 522 if (timer != 0) { 523 g_source_remove(timer); 524 timer = 0; 525 } 548 526 } 549 527 } … … 553 531 /* note that changing the value of this will clobber 554 532 * any user setting of this */ 555 int owl_variable_disable_ctrl_d_set(owl_variable *v, intnewval)556 { 557 if ( !owl_context_is_startup(owl_global_get_context(&g))) {558 if ( newval == 2) {533 int owl_variable_disable_ctrl_d_set(owl_variable *v, const void *newval) 534 { 535 if (newval && !owl_context_is_startup(owl_global_get_context(&g))) { 536 if (*(const int*)newval == 2) { 559 537 owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char"); 560 } else if ( newval == 1) {538 } else if (*(const int*)newval == 1) { 561 539 owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete"); 562 540 } else { … … 564 542 } 565 543 } 566 return owl_variable_int_set_default(v, newval); 567 } 568 569 int owl_variable_tty_set(owl_variable *v, const char*newval)544 return owl_variable_int_set_default(v, newval); 545 } 546 547 int owl_variable_tty_set(owl_variable *v, const void *newval) 570 548 { 571 549 owl_zephyr_set_locationinfo(g_get_host_name(), newval); 572 return owl_variable_string_set_default(v, newval);573 } 574 575 int owl_variable_default_exposure_set(owl_variable *v, const char*newval)550 return(owl_variable_string_set_default(v, newval)); 551 } 552 553 int owl_variable_default_exposure_set(owl_variable *v, const void *newval) 576 554 { 577 555 return owl_zephyr_set_default_exposure(newval); 578 556 } 579 557 580 const char*owl_variable_default_exposure_get(const owl_variable *v)558 const void *owl_variable_default_exposure_get(const owl_variable *v) 581 559 { 582 560 return owl_zephyr_get_default_exposure(); 583 561 } 584 562 585 int owl_variable_exposure_set(owl_variable *v, const char*newval)563 int owl_variable_exposure_set(owl_variable *v, const void *newval) 586 564 { 587 565 int ret = owl_zephyr_set_exposure(newval); … … 595 573 /**************************************************************************/ 596 574 597 voidowl_variable_dict_setup(owl_vardict *vd) {575 int owl_variable_dict_setup(owl_vardict *vd) { 598 576 owl_dict_create(vd); 599 owl_variable_add_defaults(vd); 600 } 601 602 CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v, 603 GCallback fn, 604 GClosureMarshal marshal) { 605 GClosure *closure = g_cclosure_new_swap(fn, v, NULL); 606 g_closure_set_marshal(closure,marshal); 607 g_closure_ref(closure); 608 g_closure_sink(closure); 609 return closure; 577 return owl_variable_add_defaults(vd); 578 } 579 580 int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable *variables_to_init) 581 { 582 owl_variable *var, *cur; 583 for (var = variables_to_init; var->name != NULL; var++) { 584 cur = g_new(owl_variable, 1); 585 *cur = *var; 586 /* strdup all the strings so we can delete them consistently. */ 587 cur->name = g_strdup(var->name); 588 cur->summary = g_strdup(var->summary); 589 cur->description = g_strdup(var->description); 590 switch (cur->type) { 591 case OWL_VARIABLE_OTHER: 592 cur->set_fn(cur, cur->pval_default); 593 break; 594 case OWL_VARIABLE_STRING: 595 if (!cur->validate_fn) 596 cur->validate_fn = owl_variable_string_validate_default; 597 if (!cur->set_fn) 598 cur->set_fn = owl_variable_string_set_default; 599 if (!cur->set_fromstring_fn) 600 cur->set_fromstring_fn = owl_variable_string_set_fromstring_default; 601 if (!cur->get_fn) 602 cur->get_fn = owl_variable_get_default; 603 if (!cur->get_tostring_fn) 604 cur->get_tostring_fn = owl_variable_string_get_tostring_default; 605 if (!cur->delete_fn) 606 cur->delete_fn = owl_variable_delete_default; 607 cur->pval_default = g_strdup(var->pval_default); 608 cur->set_fn(cur, cur->pval_default); 609 break; 610 case OWL_VARIABLE_BOOL: 611 if (!cur->validate_fn) 612 cur->validate_fn = owl_variable_bool_validate_default; 613 if (!cur->set_fn) 614 cur->set_fn = owl_variable_bool_set_default; 615 if (!cur->set_fromstring_fn) 616 cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default; 617 if (!cur->get_fn) 618 cur->get_fn = owl_variable_get_default; 619 if (!cur->get_tostring_fn) 620 cur->get_tostring_fn = owl_variable_bool_get_tostring_default; 621 if (!cur->delete_fn) 622 cur->delete_fn = owl_variable_delete_default; 623 cur->val = g_new(int, 1); 624 cur->set_fn(cur, &cur->ival_default); 625 break; 626 case OWL_VARIABLE_INT: 627 if (!cur->validate_fn) 628 cur->validate_fn = owl_variable_int_validate_default; 629 if (!cur->set_fn) 630 cur->set_fn = owl_variable_int_set_default; 631 if (!cur->set_fromstring_fn) 632 cur->set_fromstring_fn = owl_variable_int_set_fromstring_default; 633 if (!cur->get_fn) 634 cur->get_fn = owl_variable_get_default; 635 if (!cur->get_tostring_fn) 636 cur->get_tostring_fn = owl_variable_int_get_tostring_default; 637 if (!cur->delete_fn) 638 cur->delete_fn = owl_variable_delete_default; 639 cur->val = g_new(int, 1); 640 cur->set_fn(cur, &cur->ival_default); 641 break; 642 default: 643 fprintf(stderr, "owl_variable_setup: invalid variable type\n"); 644 return(-2); 645 } 646 owl_dict_insert_element(vd, cur->name, cur, NULL); 647 } 648 return 0; 610 649 } 611 650 … … 615 654 } 616 655 617 static owl_variable *owl_variable_newvar(int type, const char *name, const char *summary, const char *description, const char *validsettings) { 618 owl_variable *var = g_new0(owl_variable, 1); 619 var->type = type;656 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description) 657 { 658 owl_variable * var = g_new0(owl_variable, 1); 620 659 var->name = g_strdup(name); 621 660 var->summary = g_strdup(summary); 622 661 var->description = g_strdup(description); 623 var->validsettings = g_strdup(validsettings);624 662 return var; 625 663 } 626 664 627 static void owl_variable_dict_newvar_int_full(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validsettings, validate_int_t validate_fn, set_int_t set_fn, get_int_t get_fn) 628 { 629 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary, 630 description, validsettings); 631 var->takes_on_off = false; 632 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default); 633 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default); 634 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_int_validate_default); 635 636 var->get_tostring_fn = owl_variable_make_closure( 637 var, G_CALLBACK(owl_variable_int_get_tostring_default), 638 g_cclosure_user_marshal_STRING__VOID); 639 var->set_fromstring_fn = owl_variable_make_closure( 640 var, G_CALLBACK(owl_variable_int_set_fromstring_default), 641 g_cclosure_user_marshal_INT__STRING); 642 643 g_value_init(&var->val, G_TYPE_INT); 644 owl_variable_set_int(var, default_val); 645 646 var->default_str = owl_variable_get_tostring(var); 647 owl_variable_dict_add_variable(vd, var); 648 } 649 650 void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description) { 651 owl_variable_dict_newvar_int_full(vd, name, default_val, summary, description, 652 "<int>", NULL, NULL, NULL); 653 } 654 655 static void owl_variable_dict_newvar_bool_full(owl_vardict *vd, const char *name, bool default_val, const char *summary, const char *description, validate_bool_t validate_fn, set_bool_t set_fn, get_bool_t get_fn) 656 { 657 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_BOOL, name, summary, 658 description, "on,off"); 659 var->takes_on_off = true; 660 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_bool_get_default); 661 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_bool_set_default); 662 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_bool_validate_default); 663 664 var->get_tostring_fn = owl_variable_make_closure( 665 var, G_CALLBACK(owl_variable_bool_get_tostring_default), 666 g_cclosure_user_marshal_STRING__VOID); 667 var->set_fromstring_fn = owl_variable_make_closure( 668 var, G_CALLBACK(owl_variable_bool_set_fromstring_default), 669 g_cclosure_user_marshal_INT__STRING); 670 671 g_value_init(&var->val, G_TYPE_BOOLEAN); 672 owl_variable_set_bool(var, default_val); 673 674 var->default_str = owl_variable_get_tostring(var); 675 owl_variable_dict_add_variable(vd, var); 676 } 677 678 void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, bool default_val, const char *summary, const char *description) { 679 owl_variable_dict_newvar_bool_full(vd, name, default_val, summary, description, 680 NULL, NULL, NULL); 681 } 682 683 static void owl_variable_dict_newvar_string_full(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description, const char *validsettings, validate_string_t validate_fn, set_string_t set_fn, get_string_t get_fn) 684 { 685 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_STRING, name, summary, 686 description, validsettings); 687 var->takes_on_off = false; 688 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_string_get_default); 689 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_string_set_default); 690 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_string_validate_default); 691 692 var->get_tostring_fn = owl_variable_make_closure( 693 var, G_CALLBACK(owl_variable_string_get_tostring_default), 694 g_cclosure_user_marshal_STRING__VOID); 695 var->set_fromstring_fn = owl_variable_make_closure( 696 var, G_CALLBACK(owl_variable_string_set_fromstring_default), 697 g_cclosure_user_marshal_INT__STRING); 698 699 g_value_init(&var->val, G_TYPE_STRING); 700 owl_variable_set_string(var, default_val); 701 702 var->default_str = owl_variable_get_tostring(var); 703 owl_variable_dict_add_variable(vd, var); 704 } 705 706 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) { 707 owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description, 708 "<string>", NULL, NULL, NULL); 709 } 710 711 void owl_variable_dict_newvar_path(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) { 712 owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description, 713 "<path>", NULL, NULL, NULL); 714 } 715 716 static void owl_variable_dict_newvar_enum_full(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validsettings, validate_int_t validate_fn, set_int_t set_fn, get_int_t get_fn) 717 { 718 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary, 719 description, validsettings); 720 var->takes_on_off = false; 721 var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default); 722 var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default); 723 var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_enum_validate); 724 725 var->get_tostring_fn = owl_variable_make_closure( 726 var, G_CALLBACK(owl_variable_enum_get_tostring), 727 g_cclosure_user_marshal_STRING__VOID); 728 var->set_fromstring_fn = owl_variable_make_closure( 729 var, G_CALLBACK(owl_variable_enum_set_fromstring), 730 g_cclosure_user_marshal_INT__STRING); 731 732 g_value_init(&var->val, G_TYPE_INT); 733 owl_variable_set_int(var, default_val); 734 735 var->default_str = owl_variable_get_tostring(var); 736 owl_variable_dict_add_variable(vd, var); 737 } 738 739 void owl_variable_dict_newvar_enum(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validset) { 740 owl_variable_dict_newvar_enum_full(vd, name, default_val, summary, description, 741 validset, NULL, NULL, NULL); 742 } 743 744 void owl_variable_dict_newvar_other(owl_vardict *vd, const char *name, const char *summary, const char *description, const char *validsettings, bool takes_on_off, GClosure *get_tostring_fn, GClosure *set_fromstring_fn) 745 { 746 owl_variable *var = owl_variable_newvar(OWL_VARIABLE_OTHER, name, summary, 747 description, validsettings); 748 var->takes_on_off = takes_on_off; 749 750 var->get_tostring_fn = g_closure_ref(get_tostring_fn); 751 g_closure_sink(get_tostring_fn); 752 753 var->set_fromstring_fn = g_closure_ref(set_fromstring_fn); 754 g_closure_sink(set_fromstring_fn); 755 756 var->default_str = owl_variable_get_tostring(var); 757 758 /* Note: this'll overwrite any existing variable of that name, even a C one, 759 but it's consistent with previous behavior and commands. */ 760 owl_variable_dict_add_variable(vd, var); 665 void owl_variable_update(owl_variable *var, const char *summary, const char *desc) { 666 g_free(var->summary); 667 var->summary = g_strdup(summary); 668 g_free(var->description); 669 var->description = g_strdup(desc); 670 } 671 672 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval) 673 { 674 owl_variable *old = owl_variable_get_var(vd, name); 675 if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) { 676 owl_variable_update(old, summ, desc); 677 g_free(old->pval_default); 678 old->pval_default = g_strdup(initval); 679 } else { 680 owl_variable * var = owl_variable_newvar(name, summ, desc); 681 var->type = OWL_VARIABLE_STRING; 682 var->validsettings = "<string>"; 683 var->pval_default = g_strdup(initval); 684 var->set_fn = owl_variable_string_set_default; 685 var->set_fromstring_fn = owl_variable_string_set_fromstring_default; 686 var->get_fn = owl_variable_get_default; 687 var->get_tostring_fn = owl_variable_string_get_tostring_default; 688 var->delete_fn = owl_variable_delete_default; 689 var->set_fn(var, initval); 690 owl_variable_dict_add_variable(vd, var); 691 } 692 } 693 694 void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval) 695 { 696 owl_variable *old = owl_variable_get_var(vd, name); 697 if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) { 698 owl_variable_update(old, summ, desc); 699 old->ival_default = initval; 700 } else { 701 owl_variable * var = owl_variable_newvar(name, summ, desc); 702 var->type = OWL_VARIABLE_INT; 703 var->validsettings = "<int>"; 704 var->ival_default = initval; 705 var->validate_fn = owl_variable_int_validate_default; 706 var->set_fn = owl_variable_int_set_default; 707 var->set_fromstring_fn = owl_variable_int_set_fromstring_default; 708 var->get_fn = owl_variable_get_default; 709 var->get_tostring_fn = owl_variable_int_get_tostring_default; 710 var->delete_fn = owl_variable_delete_default; 711 var->val = g_new(int, 1); 712 var->set_fn(var, &initval); 713 owl_variable_dict_add_variable(vd, var); 714 } 715 } 716 717 void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval) 718 { 719 owl_variable *old = owl_variable_get_var(vd, name); 720 if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) { 721 owl_variable_update(old, summ, desc); 722 old->ival_default = initval; 723 } else { 724 owl_variable * var = owl_variable_newvar(name, summ, desc); 725 var->type = OWL_VARIABLE_BOOL; 726 var->validsettings = "on,off"; 727 var->ival_default = initval; 728 var->validate_fn = owl_variable_bool_validate_default; 729 var->set_fn = owl_variable_bool_set_default; 730 var->set_fromstring_fn = owl_variable_bool_set_fromstring_default; 731 var->get_fn = owl_variable_get_default; 732 var->get_tostring_fn = owl_variable_bool_get_tostring_default; 733 var->delete_fn = owl_variable_delete_default; 734 var->val = g_new(int, 1); 735 var->set_fn(var, &initval); 736 owl_variable_dict_add_variable(vd, var); 737 } 761 738 } 762 739 … … 770 747 } 771 748 772 void owl_variable_delete(owl_variable *v) 773 { 749 void owl_variable_cleanup(owl_variable *v) 750 { 751 if (v->delete_fn) v->delete_fn(v); 774 752 g_free(v->name); 775 753 g_free(v->summary); 776 754 g_free(v->description); 777 g_free(v->default_str); 778 g_free(v->validsettings); 779 g_value_unset(&(v->val)); 780 g_closure_unref(v->get_tostring_fn); 781 g_closure_unref(v->set_fromstring_fn); 782 755 if (v->type == OWL_VARIABLE_STRING) 756 g_free(v->pval_default); 757 } 758 759 void owl_variable_delete(owl_variable *v) 760 { 761 owl_variable_cleanup(v); 783 762 g_free(v); 784 763 } … … 806 785 return v->type; 807 786 } 787 788 /* functions for getting and setting variable values */ 808 789 809 790 /* returns 0 on success, prints a status msg if msg is true */ 810 791 int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) { 811 792 char *tostring; 812 GValue values[] = {G_VALUE_INIT, G_VALUE_INIT}; 813 GValue return_box = G_VALUE_INIT; 814 int set_successfully; 815 816 g_value_init(&values[0], G_TYPE_POINTER); 817 g_value_set_pointer(&values[0], NULL); 818 g_value_init(&values[1], G_TYPE_STRING); 819 g_value_set_static_string(&values[1], value); 820 g_value_init(&return_box, G_TYPE_INT); 821 g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL); 822 823 set_successfully = g_value_get_int(&return_box); 824 if (0 != set_successfully) { 793 if (!v->set_fromstring_fn) { 794 if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v)); 795 return -1; 796 } 797 if (0 != v->set_fromstring_fn(v, value)) { 825 798 if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v), 826 799 owl_variable_get_validsettings(v)); 827 } else if (msg) { 828 tostring = owl_variable_get_tostring(v); 829 if (tostring) { 800 return -1; 801 } 802 if (msg) { 803 tostring = v->get_tostring_fn(v, v->get_fn(v)); 804 if (tostring) 830 805 owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring); 831 } else {806 else 832 807 owl_function_makemsg("%s = <null>", owl_variable_get_name(v)); 833 }834 808 g_free(tostring); 835 } 836 837 g_value_unset(&return_box); 838 g_value_unset(&values[1]); 839 g_value_unset(&values[0]); 840 return set_successfully; 809 } 810 return 0; 841 811 } 842 812 843 813 int owl_variable_set_string(owl_variable *v, const char *newval) 844 814 { 845 g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1); 846 847 set_string_t cb = (set_string_t) v->set_fn; 848 return cb(v, newval); 849 } 850 815 if (v->type != OWL_VARIABLE_STRING) return -1; 816 return v->set_fn(v, newval); 817 } 818 851 819 int owl_variable_set_int(owl_variable *v, int newval) 852 820 { 853 g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1); 854 855 set_int_t cb = (set_int_t) v->set_fn; 856 return cb(v, newval); 857 } 858 859 int owl_variable_set_bool(owl_variable *v, bool newval) { 860 g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1); 861 862 set_bool_t cb = (set_bool_t) v->set_fn; 863 return cb(v, newval); 864 } 865 821 if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1; 822 return v->set_fn(v, &newval); 823 } 824 866 825 int owl_variable_set_bool_on(owl_variable *v) 867 826 { 868 827 if (v->type != OWL_VARIABLE_BOOL) return -1; 869 return owl_variable_set_ bool(v, true);828 return owl_variable_set_int(v, true); 870 829 } 871 830 … … 873 832 { 874 833 if (v->type != OWL_VARIABLE_BOOL) return -1; 875 return owl_variable_set_ bool(v, false);834 return owl_variable_set_int(v, false); 876 835 } 877 836 878 837 CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v) 879 838 { 880 GValue instance = G_VALUE_INIT; 881 GValue tostring_box = G_VALUE_INIT; 882 char *ret = NULL; 883 884 g_value_init(&instance, G_TYPE_POINTER); 885 g_value_set_pointer(&instance, NULL); 886 g_value_init(&tostring_box, G_TYPE_STRING); 887 g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL); 888 889 ret = g_value_dup_string(&tostring_box); 890 891 g_value_unset(&tostring_box); 892 g_value_unset(&instance); 893 return ret; 894 } 895 896 const char *owl_variable_get_default_tostring(const owl_variable *v) 897 { 898 return v->default_str; 839 return v->get_tostring_fn(v, v->get_fn(v)); 840 } 841 842 CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v) 843 { 844 if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) { 845 return v->get_tostring_fn(v, &(v->ival_default)); 846 } else { 847 return v->get_tostring_fn(v, v->pval_default); 848 } 899 849 } 900 850 … … 904 854 } 905 855 856 /* returns a reference */ 857 const void *owl_variable_get(const owl_variable *v) 858 { 859 return v->get_fn(v); 860 } 861 906 862 const char *owl_variable_get_string(const owl_variable *v) 907 863 { 908 g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL); 909 910 get_string_t cb = (get_string_t) v->get_fn; 911 return cb(v); 864 if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) { 865 owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v)); 866 return NULL; 867 } 868 return owl_variable_get(v); 869 } 870 871 /* returns a reference */ 872 const void *owl_variable_get_other(const owl_variable *v) 873 { 874 if (owl_variable_get_type(v) != OWL_VARIABLE_OTHER) { 875 owl_function_error("Variable '%s' is not type other.", owl_variable_get_name(v)); 876 return NULL; 877 } 878 return owl_variable_get(v); 912 879 } 913 880 914 881 int owl_variable_get_int(const owl_variable *v) 915 882 { 916 g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0); 917 918 get_int_t cb = (get_int_t) v->get_fn; 919 return cb(v); 920 } 921 922 bool owl_variable_get_bool(const owl_variable *v) 923 { 924 g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE); 925 926 get_bool_t cb = (get_bool_t) v->get_fn; 927 return cb(v); 883 if (owl_variable_get_type(v) != OWL_VARIABLE_INT) { 884 owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v)); 885 return -1; 886 } 887 const int *pi = owl_variable_get(v); 888 if (!pi) return -1; 889 return *pi; 890 } 891 892 int owl_variable_get_bool(const owl_variable *v) 893 { 894 if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) { 895 owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v)); 896 return -1; 897 } 898 const int *pi = owl_variable_get(v); 899 if (!pi) return -1; 900 return *pi; 928 901 } 929 902 930 903 void owl_variable_describe(const owl_variable *v, owl_fmtext *fm) 931 904 { 932 c onst char *default_str= owl_variable_get_default_tostring(v);905 char *tostring = owl_variable_get_default_tostring(v); 933 906 char *default_buf; 934 907 935 if ( default_str)936 default_buf = g_strdup_printf("'%s'", default_str);908 if (tostring) 909 default_buf = g_strdup_printf("'%s'", tostring); 937 910 else 938 911 default_buf = g_strdup("<null>"); … … 941 914 owl_variable_get_summary(v), default_buf); 942 915 g_free(default_buf); 916 g_free(tostring); 943 917 } 944 918 945 919 void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) { 946 920 char *tostring; 947 const char *default_str;948 921 949 922 owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n"); … … 960 933 owl_fmtext_append_normal(fm, "\n\n"); 961 934 962 default_str = owl_variable_get_default_tostring(v); 935 936 tostring = owl_variable_get_default_tostring(v); 963 937 owl_fmtext_append_normal(fm, "Default: "); 964 owl_fmtext_append_normal(fm, ( default_str ? default_str: "<null>"));938 owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>")); 965 939 owl_fmtext_append_normal(fm, "\n\n"); 966 940 … … 974 948 owl_fmtext_append_normal(fm, "\n\n"); 975 949 } 950 g_free(tostring); 976 951 } 977 952 … … 985 960 /* default common functions */ 986 961 987 const char *owl_variable_string_get_default(const owl_variable *v) { 988 return g_value_get_string(&(v->val)); 989 } 990 991 int owl_variable_int_get_default(const owl_variable *v) { 992 return g_value_get_int(&(v->val)); 993 } 994 995 bool owl_variable_bool_get_default(const owl_variable *v) { 996 return g_value_get_boolean(&(v->val)); 962 const void *owl_variable_get_default(const owl_variable *v) { 963 return v->val; 964 } 965 966 void owl_variable_delete_default(owl_variable *v) 967 { 968 g_free(v->val); 997 969 } 998 970 999 971 /* default functions for booleans */ 1000 972 1001 int owl_variable_bool_validate_default(const owl_variable *v, bool newval) { 1002 return (newval == 1) || (newval == 0); 1003 } 1004 1005 int owl_variable_bool_set_default(owl_variable *v, bool newval) { 1006 if (!((validate_bool_t)v->validate_fn)(v, newval)) 1007 return -1; 1008 1009 g_value_set_boolean(&(v->val), newval); 973 int owl_variable_bool_validate_default(const owl_variable *v, const void *newval) { 974 if (newval == NULL) return(0); 975 else if (*(const int*)newval==1 || *(const int*)newval==0) return(1); 976 else return (0); 977 } 978 979 int owl_variable_bool_set_default(owl_variable *v, const void *newval) { 980 if (v->validate_fn) { 981 if (!v->validate_fn(v, newval)) return(-1); 982 } 983 *(int*)v->val = *(const int*)newval; 1010 984 return(0); 1011 985 } 1012 986 1013 int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) { 1014 bool i; 1015 if (!strcmp(newval, "on")) { 1016 i = true; 1017 } else if (!strcmp(newval, "off")) { 1018 i = false; 1019 } else { 1020 return(-1); 1021 } 1022 1023 return owl_variable_set_bool(v, i); 1024 } 1025 1026 CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy) 1027 { 1028 bool val = owl_variable_get_bool(v); 1029 if (val == 0) { 987 int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval) { 988 int i; 989 if (!strcmp(newval, "on")) i=1; 990 else if (!strcmp(newval, "off")) i=0; 991 else return(-1); 992 return (v->set_fn(v, &i)); 993 } 994 995 CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, const void *val) 996 { 997 if (val == NULL) { 998 return NULL; 999 } else if (*(const int*)val == 0) { 1030 1000 return g_strdup("off"); 1031 } else if ( val == 1) {1001 } else if (*(const int*)val == 1) { 1032 1002 return g_strdup("on"); 1033 1003 } else { … … 1038 1008 /* default functions for integers */ 1039 1009 1040 int owl_variable_int_validate_default(const owl_variable *v, int newval)1041 { 1042 return (1);1043 } 1044 1045 int owl_variable_int_set_default(owl_variable *v, intnewval) {1046 if ( !((validate_int_t)v->validate_fn)(v, newval))1047 return -1;1048 1049 g_value_set_int(&(v->val), newval);1010 int owl_variable_int_validate_default(const owl_variable *v, const void *newval) { 1011 if (newval == NULL) return(0); 1012 else return (1); 1013 } 1014 1015 int owl_variable_int_set_default(owl_variable *v, const void *newval) { 1016 if (v->validate_fn) { 1017 if (!v->validate_fn(v, newval)) return(-1); 1018 } 1019 *(int*)v->val = *(const int*)newval; 1050 1020 return(0); 1051 1021 } 1052 1022 1053 int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval , void *dummy) {1023 int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval) { 1054 1024 int i; 1055 1025 char *ep; 1056 1026 i = strtol(newval, &ep, 10); 1057 1027 if (*ep || ep==newval) return(-1); 1058 return owl_variable_set_int(v, i); 1059 } 1060 1061 CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy) 1062 { 1063 return g_strdup_printf("%d", owl_variable_get_int(v)); 1028 return (v->set_fn(v, &i)); 1029 } 1030 1031 CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, const void *val) 1032 { 1033 if (val == NULL) { 1034 return NULL; 1035 } else { 1036 return g_strdup_printf("%d", *(const int*)val); 1037 } 1064 1038 } 1065 1039 1066 1040 /* default functions for enums (a variant of integers) */ 1067 1041 1068 int owl_variable_enum_validate(const owl_variable *v, int newval) {1042 int owl_variable_enum_validate(const owl_variable *v, const void *newval) { 1069 1043 char **enums; 1070 1044 int nenums, val; 1045 if (newval == NULL) return(0); 1071 1046 enums = g_strsplit_set(v->validsettings, ",", 0); 1072 1047 nenums = g_strv_length(enums); 1073 1048 g_strfreev(enums); 1074 val = newval;1049 val = *(const int*)newval; 1075 1050 if (val < 0 || val >= nenums) { 1076 1051 return(0); … … 1079 1054 } 1080 1055 1081 int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval , void *dummy) {1056 int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval) { 1082 1057 char **enums; 1083 1058 int i, val=-1; … … 1091 1066 g_strfreev(enums); 1092 1067 if (val == -1) return(-1); 1093 return owl_variable_set_int(v, val);1094 } 1095 1096 CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy)1068 return (v->set_fn(v, &val)); 1069 } 1070 1071 CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, const void *val) 1097 1072 { 1098 1073 char **enums; … … 1100 1075 char *tostring; 1101 1076 1077 if (val == NULL) { 1078 return NULL; 1079 } 1102 1080 enums = g_strsplit_set(v->validsettings, ",", 0); 1103 1081 nenums = g_strv_length(enums); 1104 i = owl_variable_get_int(v);1082 i = *(const int*)val; 1105 1083 if (i<0 || i>=nenums) { 1106 1084 g_strfreev(enums); … … 1114 1092 /* default functions for stringeans */ 1115 1093 1116 int owl_variable_string_validate_default(const owl_variable *v, const char*newval) {1094 int owl_variable_string_validate_default(const struct _owl_variable *v, const void *newval) { 1117 1095 if (newval == NULL) return(0); 1118 1096 else return (1); 1119 1097 } 1120 1098 1121 int owl_variable_string_set_default(owl_variable *v, const char *newval) { 1122 if (!((validate_string_t)v->validate_fn)(v, newval)) 1123 return -1; 1124 1125 g_value_set_string(&(v->val), newval); 1099 int owl_variable_string_set_default(owl_variable *v, const void *newval) { 1100 if (v->validate_fn) { 1101 if (!v->validate_fn(v, newval)) return(-1); 1102 } 1103 g_free(v->val); 1104 v->val = g_strdup(newval); 1126 1105 return(0); 1127 1106 } 1128 1107 1129 int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 1130 { 1131 return owl_variable_set_string(v, newval); 1132 } 1133 1134 CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy) 1135 { 1136 return g_strdup(owl_variable_get_string(v)); 1137 } 1138 1108 int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval) { 1109 return (v->set_fn(v, newval)); 1110 } 1111 1112 CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, const void *val) 1113 { 1114 return g_strdup((const char*)val); 1115 } 1116
Note: See TracChangeset
for help on using the changeset viewer.