source: variable.c

Last change on this file was 4fd3c04, checked in by Anders Kaseorg <andersk@mit.edu>, 6 years ago
Remove AIM support This code has received almost no security attention, and anyway, AIM is shutting down on December 15, 2017. https://aimemories.tumblr.com/post/166091776077/aimemories Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 42.6 KB
RevLine 
[7d4fbcd]1#include "owl.h"
[f271129]2#include <stdio.h>
[06e04a9]3#include "gmarshal_funcs.h"
4
[353719a]5/* TODO(davidben): When we can require 2.30 and up, remove this. */
6#ifndef G_VALUE_INIT
7#define G_VALUE_INIT { 0, { { 0 } } }
8#endif
9
10typedef const char *(*get_string_t)(const owl_variable *);
11typedef int (*get_int_t)(const owl_variable *);
[4584d1f]12typedef bool (*get_bool_t)(const owl_variable *);
[353719a]13
14typedef int (*set_string_t)(owl_variable *, const char *);
15typedef int (*set_int_t)(owl_variable *, int);
[4584d1f]16typedef int (*set_bool_t)(owl_variable *, bool);
17
18typedef int (*validate_string_t)(const owl_variable *, const char *);
19typedef int (*validate_int_t)(const owl_variable *, int);
20typedef int (*validate_bool_t)(const owl_variable *, bool);
21
22static 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
31static 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
41static 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
51static 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)
[7d4fbcd]70
[aa2f33b3]71#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
[4584d1f]72        owl_variable_dict_newvar_int_full(vd, name, default, summary, description, \
73                                          validset, validate, set, get)
[7d4fbcd]74
[4584d1f]75#define OWLVAR_PATH(name, default, summary, description) \
76        owl_variable_dict_newvar_path(vd, name, default, summary, description)
[7d4fbcd]77
[4584d1f]78#define OWLVAR_STRING(name, default, summary, description) \
79        owl_variable_dict_newvar_string(vd, name, default, summary, description)
[7d4fbcd]80
[4584d1f]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)
[c01e477]84
[7d4fbcd]85/* enums are really integers, but where validset is a comma-separated
86 * list of strings which can be specified.  The tokens, starting at 0,
87 * correspond to the values that may be specified. */
[4584d1f]88#define OWLVAR_ENUM(name, default, summary, description, validset) \
89        owl_variable_dict_newvar_enum(vd, name, default, summary, description, validset)
[7d4fbcd]90
[aa2f33b3]91#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
[4584d1f]92        owl_variable_dict_newvar_enum_full(vd, name, default, summary, description, \
93                                           validset, validate, set, get)
[217a43e]94
[6a8b519]95void owl_variable_add_defaults(owl_vardict *vd)
[9e86f6f]96{
[5d365f6]97  OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off",
[213a3eb]98                 "ring the terminal bell when personal messages are received",
[5d365f6]99                 "Can be set to 'on', 'off', or the name of a filter which\n"
[4584d1f]100                 "messages need to match in order to ring the bell");
[7d4fbcd]101
102  OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1,
[4584d1f]103               "enable / disable the terminal bell", "" );
[7d4fbcd]104
105  OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG,
106                    "whether debugging is enabled",
[aa2f33b3]107                    "If set to 'on', debugging messages are logged to the\n"
108                    "file specified by the debugfile variable.\n",
[4584d1f]109                    NULL, owl_variable_debug_set, NULL);
[7d4fbcd]110
111  OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1,
[4584d1f]112               "send a login message when BarnOwl starts", "" );
[7d4fbcd]113
114  OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1,
[4584d1f]115               "send a logout message when BarnOwl exits", "" );
[7d4fbcd]116
117  OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0,
[4584d1f]118               "display received pings", "" );
[7d4fbcd]119
120  OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1,
[4584d1f]121               "send pings", "" );
[7d4fbcd]122
[73624b4]123  OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0,
[4584d1f]124               "disable printing information in the separator bar", "" );
[73624b4]125
[f9c43ae]126  OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1,
[4584d1f]127               "strip kerberos instance for reply", "");
[f9c43ae]128
[7e3e00a]129  OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1,
[4584d1f]130               "strip leading and trailing newlines", "");
[7e3e00a]131
[7d4fbcd]132  OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1,
[4584d1f]133               "display outgoing messages", "" );
[7d4fbcd]134
135  OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1,
[4584d1f]136               "load logins from .anyone on startup", "" );
[7d4fbcd]137
[d126a19]138  OWLVAR_BOOL_FULL( "colorztext" /* %OwlVarStub */, 1,
139                    "allow @color() in zephyrs to change color",
[a95b564]140                    "", NULL, owl_variable_colorztext_set, NULL);
[e1c4636]141
[c15bbfb]142  OWLVAR_BOOL( "fancylines" /* %OwlVarStub */, 1,
143               "Use 'nice' line drawing on the terminal.",
144               "If turned off, dashes, pipes and pluses will be used\n"
145               "to draw lines on the screen.  Useful when the terminal\n"
[4584d1f]146               "is causing problems" );
[c15bbfb]147
[d309eb3]148  OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1,
149               "Do automatic zcrypt processing",
[4584d1f]150               "" );
[d309eb3]151
[4357be8]152  OWLVAR_BOOL_FULL( "pseudologins" /* %OwlVarStub */, 0,
153                    "Enable zephyr pseudo logins",
[a16d7e5]154                    "When this is enabled, BarnOwl will periodically check the zephyr\n"
[4357be8]155                    "location of users in your .anyone file.  If a user is present\n"
156                    "but sent no login message, or a user is not present that sent no\n"
[d544237]157                    "logout message, a pseudo login or logout message will be created\n",
[4584d1f]158                    NULL, owl_variable_pseudologins_set, NULL);
[5a95b69]159
[280ddc6]160  OWLVAR_BOOL( "ignorelogins" /* %OwlVarStub */, 0,
161               "Enable printing of login notifications",
[a16d7e5]162               "When this is enabled, BarnOwl will print login and logout notifications\n"
[4fd3c04]163               "for zephyr or other protocols.  If disabled BarnOwl will not print\n"
[4584d1f]164               "login or logout notifications.\n");
[280ddc6]165
[217a43e]166  OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1,
[aa2f33b3]167                    "don't send zephyrs on C-d",
[40de7394]168                    "If set to 'on', C-d won't send a zephyr from the edit\n"
169                    "window.  If set to 'off', C-d will always send a zephyr\n"
[aa2f33b3]170                    "being composed in the edit window.  If set to 'middle',\n"
171                    "C-d will only ever send a zephyr if the cursor is at\n"
172                    "the end of the message being composed.\n\n"
173                    "Note that this works by changing the C-d keybinding\n"
174                    "in the editmulti keymap.\n",
175                    "off,middle,on",
[4584d1f]176                    NULL, owl_variable_disable_ctrl_d_set, NULL);
[7d4fbcd]177
178  OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE,
[aa2f33b3]179               "path for logging debug messages when debugging is enabled",
[26ad412]180               "This file will be logged to if 'debug' is set to 'on'.\n"
[4584d1f]181               "BarnOwl will append a dot and the current process's pid to the filename.");
[7d4fbcd]182 
[ced25d1]183  OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL,
[aa2f33b3]184               "name of a program to run that will generate zsigs",
185               "This program should produce a zsig on stdout when run.\n"
[81a96af]186               "Note that it is important that this program not block.\n\n"
187               "See the documentation for 'zsig' for more information about\n"
188               "how the outgoing zsig is chosen."
[4584d1f]189               );
[7d4fbcd]190
[700c712]191  OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL,
192               "name of a program to run when new messages are present",
[a16d7e5]193               "The named program will be run when BarnOwl receives new\n"
[700c712]194               "messages.  It will not be run again until the first\n"
[4584d1f]195               "instance exits");
[700c712]196
[247cbc9]197  OWLVAR_STRING( "zsender" /* %OwlVarStub */, "",
198             "zephyr sender name",
199         "Allows you to customize the outgoing username in\n"
200         "zephyrs.  If this is unset, it will use your Kerberos\n"
201         "principal. Note that customizing the sender name will\n"
[4584d1f]202         "cause your zephyrs to be sent unauthenticated.");
[247cbc9]203
[de3f641]204  OWLVAR_STRING( "zsigfunc" /* %OwlVarStub */, "BarnOwl::default_zephyr_signature()",
205                 "zsig perl function",
206                 "Called every time you start a zephyrgram without an\n"
207                 "explicit zsig.  The default setting implements the policy\n"
[b120bd3]208                 "described in the documentation for the 'zsig' variable.\n"
[4584d1f]209                 "See also BarnOwl::random_zephyr_signature().\n");
[de3f641]210
[7d4fbcd]211  OWLVAR_STRING( "zsig" /* %OwlVarStub */, "",
[81a96af]212                 "zephyr signature",
213                 "The zsig to get on outgoing messages. If this variable is\n"
214                 "unset, 'zsigproc' will be run to generate a zsig. If that is\n"
215                 "also unset, the 'zwrite-signature' zephyr variable will be\n"
[4584d1f]216                 "used instead.\n");
[7d4fbcd]217
218  OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "",
[aa2f33b3]219                 "string to append to the end of the sepbar",
220                 "The sepbar is the bar separating the top and bottom\n"
[a16d7e5]221                 "of the BarnOwl screen.  Any string specified here will\n"
[4584d1f]222                 "be displayed on the right of the sepbar\n");
[7d4fbcd]223
224  OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0,
[4584d1f]225               "turn zaway on or off", "" );
[7d4fbcd]226
227  OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 
228                 OWL_DEFAULT_ZAWAYMSG,
[4584d1f]229                 "zaway msg for responding to zephyrs when away", "" );
[7d4fbcd]230
231  OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 
232                 OWL_DEFAULT_ZAWAYMSG,
[4584d1f]233                 "default zaway message", "" );
[7d4fbcd]234
235  OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all",
[aa2f33b3]236                 "home view to switch to after 'X' and 'V'", 
[4584d1f]237                 "SEE ALSO: view, filter\n" );
[7d4fbcd]238
[ecd5dc5]239  OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none",
240                 "filter on which to trigger alert actions",
[4584d1f]241                 "" );
[ecd5dc5]242
243  OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop",
[a16d7e5]244                 "BarnOwl command to execute for alert actions",
[4584d1f]245                 "" );
[ecd5dc5]246
[f203cad]247  OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "<string>", "tty name for zephyr location", "",
[4584d1f]248                      NULL, owl_variable_tty_set, NULL);
[bd3f232]249
[bc14adc]250  OWLVAR_STRING( "default_style" /* %OwlVarStub */, "default",
[c3ab155]251                 "name of the default formatting style",
[f1e629d]252                 "This sets the default message formatting style.\n"
253                 "Styles may be created with the 'style' command.\n"
254                 "Some built-in styles include:\n"
[a16d7e5]255                 "   default  - the default BarnOwl formatting\n"
[f1e629d]256                 "   oneline  - one line per-message\n"
257                 "   perl     - legacy perl interface\n"
258                 "\nSEE ALSO: style, show styles, view -s <style>\n"
[4584d1f]259                 );
[f1e629d]260
[c3ab155]261
[d36f2cb]262  OWLVAR_INT(    "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70,
[4d9e4254]263                 "maximum number of columns for M-q (edit:fill-paragraph) to fill text to",
264                 "This specifies the maximum number of columns for M-q to fill text\n"
265                 "to.  If set to 0, M-q will wrap to the width of the window, and\n"
[4584d1f]266                 "values less than 0 disable M-q entirely.\n");
[d36f2cb]267
[a4bbd80]268  OWLVAR_INT(    "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70,
[aa2f33b3]269                 "maximum number of columns for line-wrapping",
[4d9e4254]270                 "This specifies the maximum number of columns for\n"
271                 "auto-line-wrapping.  If set to 0, text will be wrapped at the\n"
272                 "window width. Values less than 0 disable automatic wrapping.\n"
273                 "\n"
274                 "As a courtesy to recipients, it is recommended that outgoing\n"
[4584d1f]275                 "Zephyr messages be no wider than 70 columns.\n");
[d36f2cb]276
[7d4fbcd]277  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
278                   OWL_TYPWIN_SIZE,
[aa2f33b3]279                  "number of lines in the typing window", 
280                   "This specifies the height of the window at the\n"
281                   "bottom of the screen where commands are entered\n"
282                   "and where messages are composed.\n",
283                   "int > 0",
[7d4fbcd]284                   owl_variable_int_validate_gt0,
285                   owl_variable_typewinsize_set,
286                   NULL /* use default for get */
[4584d1f]287                   );
[7d4fbcd]288
[da466e0]289  OWLVAR_INT( "typewindelta" /* %OwlVarStub */, 0,
290                  "number of lines to add to the typing window when in use",
291                   "On small screens you may want the typing window to\n"
292                   "auto-hide when not entering a command or message.\n"
293                   "This variable is the number of lines to add to the\n"
294           "typing window when it is in use; you can then set\n"
295           "typewinsize to 1.\n\n"
296           "This works a lot better with a non-default scrollmode;\n"
[4584d1f]297           "try :set scrollmode pagedcenter.\n");
[da466e0]298
[aa2f33b3]299  OWLVAR_ENUM( "scrollmode" /* %OwlVarStub */, OWL_SCROLLMODE_NORMAL,
300               "how to scroll up and down",
301               "This controls how the screen is scrolled as the\n"
302               "cursor moves between messages being displayed.\n"
303               "The following modes are supported:\n\n"
[a16d7e5]304               "   normal      - This is the BarnOwl default.  Scrolling happens\n"
[aa2f33b3]305               "                 when it needs to, and an attempt is made to\n"
306               "                 keep the current message roughly near\n"
307               "                 the middle of the screen.\n"
308               "   top         - The current message will always be the\n"
309               "                 the top message displayed.\n"
310               "   neartop     - The current message will be one down\n"
311               "                 from the top message displayed,\n"
312               "                 where possible.\n"
313               "   center      - An attempt is made to keep the current\n"
314               "                 message near the center of the screen.\n"
315               "   paged       - The top message displayed only changes\n"
316               "                 when user moves the cursor to the top\n"
317               "                 or bottom of the screen.  When it moves,\n"
318               "                 the screen will be paged up or down and\n"
319               "                 the cursor will be near the top or\n"
320               "                 the bottom.\n"
321               "   pagedcenter - The top message displayed only changes\n"
322               "                 when user moves the cursor to the top\n"
323               "                 or bottom of the screen.  When it moves,\n"
324               "                 the screen will be paged up or down and\n"
325               "                 the cursor will be near the center.\n",
[4584d1f]326               "normal,top,neartop,center,paged,pagedcenter" );
[aa2f33b3]327
[66e409c]328  OWLVAR_BOOL( "narrow-related" /* %OwlVarStub:narrow_related */, 1,
329               "Make smartnarrow use broader filters",
[8258ea5]330               "Causes smartfilter to narrow to messages \"related\" to \n"
[66e409c]331               "the current message, as well as ones to the same place.\n\n"
332               "for Zephyr, this controls whether to narrow to e.g. class-help or\n"
333               "class-help.d alone, or to related-class-help, which includes\n"
[4584d1f]334               "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" );
[ecd5dc5]335
[7d4fbcd]336  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
[aa2f33b3]337               "enable automatic following of the last zephyr",
338               "If the cursor is at the last message, it will\n"
339               "continue to follow the last message if this is set.\n"
340               "Note that this is currently risky as you might accidentally\n"
[4584d1f]341               "delete a message right as it came in.\n" );
[7d4fbcd]342
[f203cad]343  OWLVAR_STRING_FULL( "default_exposure" /* %OwlVarStub */, "",
344                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
345                      "controls the persistent value for exposure",
346                      "The default exposure level corresponds to the Zephyr exposure value\n"
347                      "in ~/.zephyr.vars.  Defaults to realm-visible if there is no value in\n"
348                      "~/.zephyr.vars.\n"
349                      "See the description of exposure for the values this can be.",
[4584d1f]350                      NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get );
[f203cad]351
352  OWLVAR_STRING_FULL( "exposure" /* %OwlVarStub */, "",
353                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
354                      "controls who can zlocate you",
355                      "The exposure level, defaulting to the value of default_exposure,\n"
356                      "can be one of the following (from least exposure to widest exposure,\n"
357                      "as listed in zctl(1)):\n"
358                      "\n"
359                      "   none            - This completely disables Zephyr for the user. \n"
360                      "                     The user is not registered with Zephyr.  No user\n"
361                      "                     location information is retained by Zephyr.  No\n"
362                      "                     login or logout announcements will be sent.  No\n"
363                      "                     subscriptions will be entered for the user, and\n"
364                      "                     no notices will be displayed by zwgc(1).\n"
365                      "   opstaff         - The user is registered with Zephyr.  No login or\n"
366                      "                     logout announcements will be sent, and location\n"
367                      "                     information will only be visible to Operations\n"
368                      "                     staff.  Default subscriptions and any additional\n"
369                      "                     personal subscriptions will be entered for the\n"
370                      "                     user.\n"
371                      "   realm-visible   - The user is registered with Zephyr.  User\n"
372                      "                     location information is retained by Zephyr and\n"
373                      "                     made available only to users within the user’s\n"
374                      "                     Kerberos realm.  No login or logout\n"
375                      "                     announcements will be sent.  This is the system\n"
376                      "                     default.  Default subscriptions and any\n"
377                      "                     additional personal subscriptions will be\n"
378                      "                     entered for the user.\n"
379                      "   realm-announced - The user is registered with Zephyr.  User\n"
380                      "                     location information is retained by Zephyr and\n"
381                      "                     made available only to users authenticated\n"
382                      "                     within the user’s Kerberos realm.  Login and\n"
383                      "                     logout announcements will be sent, but only to\n"
384                      "                     users within the user’s Kerberos realm who have\n"
385                      "                     explicitly requested such via subscriptions. \n"
386                      "                     Default subscriptions and any additional\n"
387                      "                     personal subscriptions will be entered for the\n"
388                      "                     user.\n"
389                      "   net-visible     - The user is registered with Zephyr.  User\n"
390                      "                     location information is retained by Zephyr and\n"
391                      "                     made available to any authenticated user who\n"
392                      "                     requests such.  Login and logout announcements\n"
393                      "                     will be sent only to users within the user’s\n"
394                      "                     Kerberos realm who have explicitly requested\n"
395                      "                     such via subscriptions.  Default subscriptions\n"
396                      "                     and any additional personal subscriptions will\n"
397                      "                     be entered for the user.\n"
398                      "   net-announced   - The user is registered with Zephyr.  User\n"
399                      "                     location information is retained by Zephyr and\n"
400                      "                     made available to any authenticated user who\n"
401                      "                     requests such.  Login and logout announcements\n"
402                      "                     will be sent to any user has requested such. \n"
403                      "                     Default subscriptions and any additional\n"
404                      "                     personal subscriptions will be entered for the\n"
405                      "                     user.\n",
[4584d1f]406                      NULL, owl_variable_exposure_set, NULL /* use default for get */ );
[9e86f6f]407}
[7d4fbcd]408
409/**************************************************************************/
410/*********************** SPECIFIC TO VARIABLES ****************************/
411/**************************************************************************/
412
413
414/* commonly useful */
415
[353719a]416int owl_variable_int_validate_gt0(const owl_variable *v, int newval)
[4357be8]417{
[06e04a9]418  return !(newval < 1);
[7d4fbcd]419}
420
[353719a]421int owl_variable_int_validate_positive(const owl_variable *v, int newval)
[4357be8]422{
[06e04a9]423  return !(newval < 0);
[7d4fbcd]424}
425
426/* typewinsize */
[353719a]427int owl_variable_typewinsize_set(owl_variable *v, int newval)
[4357be8]428{
[7d4fbcd]429  int rv;
[353719a]430  rv = owl_variable_int_set_default(v, newval);
[f6fae8d]431  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
[7d4fbcd]432  return(rv);
433}
434
435/* debug (cache value in g->debug) */
[4584d1f]436int owl_variable_debug_set(owl_variable *v, bool newval)
[4357be8]437{
[4584d1f]438  g.debug = newval;
[353719a]439  return owl_variable_bool_set_default(v, newval);
[4b660cc]440}
441
[4584d1f]442int owl_variable_colorztext_set(owl_variable *v, bool newval)
[d126a19]443{
[353719a]444  int ret = owl_variable_bool_set_default(v, newval);
[d126a19]445  /* flush the format cache so that we see the update, but only if we're done initializing BarnOwl */
446  if (owl_global_get_msglist(&g) != NULL)
447    owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
448  if (owl_global_get_mainwin(&g) != NULL) {
449    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
450    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
451  }
452  return ret;
453}
454
[4584d1f]455int owl_variable_pseudologins_set(owl_variable *v, bool newval)
[4357be8]456{
[72146c7]457  static guint timer = 0;
[4584d1f]458  if (newval) {
[06e04a9]459    owl_function_zephyr_buddy_check(0);
460    if (timer == 0) {
461      timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL);
462    }
463  } else {
464    if (timer != 0) {
465      g_source_remove(timer);
466      timer = 0;
[4357be8]467    }
468  }
[353719a]469  return owl_variable_bool_set_default(v, newval);
[4357be8]470}
471
[7d4fbcd]472/* note that changing the value of this will clobber
473 * any user setting of this */
[353719a]474int owl_variable_disable_ctrl_d_set(owl_variable *v, int newval)
[4357be8]475{
[06e04a9]476  if (!owl_context_is_startup(owl_global_get_context(&g))) {
477    if (newval == 2) {
[7d4fbcd]478      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
[06e04a9]479    } else if (newval == 1) {
[3b42640]480      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
[7d4fbcd]481    } else {
[3b42640]482      owl_function_command_norv("bindkey editmulti C-d command edit:done");
[7d4fbcd]483    }
484  } 
[353719a]485  return owl_variable_int_set_default(v, newval);
[7d4fbcd]486}
487
[353719a]488int owl_variable_tty_set(owl_variable *v, const char *newval)
[4357be8]489{
[f54b07d]490  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
[353719a]491  return owl_variable_string_set_default(v, newval);
[c01e477]492}
493
[353719a]494int owl_variable_default_exposure_set(owl_variable *v, const char *newval)
[f203cad]495{
496  return owl_zephyr_set_default_exposure(newval);
497}
498
[353719a]499const char *owl_variable_default_exposure_get(const owl_variable *v)
[f203cad]500{
501  return owl_zephyr_get_default_exposure();
502}
503
[353719a]504int owl_variable_exposure_set(owl_variable *v, const char *newval)
[f203cad]505{
506  int ret = owl_zephyr_set_exposure(newval);
507  if (ret != 0)
508    return ret;
[353719a]509  return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval));
[f203cad]510}
[7d4fbcd]511
512/**************************************************************************/
513/****************************** GENERAL ***********************************/
514/**************************************************************************/
515
[6a8b519]516void owl_variable_dict_setup(owl_vardict *vd) {
[4c7c21f]517  owl_dict_create(vd);
[6a8b519]518  owl_variable_add_defaults(vd);
[9e86f6f]519}
520
[06e04a9]521CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v,
[353719a]522                                               GCallback fn,
523                                               GClosureMarshal marshal) {
524  GClosure *closure = g_cclosure_new_swap(fn, v, NULL);
[06e04a9]525  g_closure_set_marshal(closure,marshal);
526  g_closure_ref(closure);
527  g_closure_sink(closure);
528  return closure;
529}
530
[a695a68]531void owl_variable_dict_add_variable(owl_vardict * vardict,
532                                    owl_variable * var) {
[9d4dfdc]533  char *oldvalue = NULL;
534  owl_variable *oldvar = owl_variable_get_var(vardict, var->name);
535  /* Save the old value as a string. */
536  if (oldvar) {
537    oldvalue = owl_variable_get_tostring(oldvar);
538  }
[3b0edaa]539  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
[9d4dfdc]540  /* Restore the old value. */
541  if (oldvalue) {
542    owl_variable_set_fromstring(var, oldvalue, 0);
543    g_free(oldvalue);
544  }
[a695a68]545}
546
[4584d1f]547static owl_variable *owl_variable_newvar(int type, const char *name, const char *summary, const char *description, const char *validsettings) {
[7dcef03]548  owl_variable *var = g_slice_new0(owl_variable);
[4584d1f]549  var->type = type;
[d4927a7]550  var->name = g_strdup(name);
551  var->summary = g_strdup(summary);
552  var->description = g_strdup(description);
[69f74c2]553  var->validsettings = g_strdup(validsettings);
[4584d1f]554  return var;
555}
556
557static 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)
558{
559  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary,
560                                          description, validsettings);
561  var->takes_on_off = false;
562  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default);
563  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default);
564  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_int_validate_default);
565
566  var->get_tostring_fn = owl_variable_make_closure(
567      var, G_CALLBACK(owl_variable_int_get_tostring_default),
568      g_cclosure_user_marshal_STRING__VOID);
569  var->set_fromstring_fn = owl_variable_make_closure(
570      var, G_CALLBACK(owl_variable_int_set_fromstring_default),
571      g_cclosure_user_marshal_INT__STRING);
572
573  g_value_init(&var->val, G_TYPE_INT);
574  owl_variable_set_int(var, default_val);
575
576  var->default_str = owl_variable_get_tostring(var);
577  owl_variable_dict_add_variable(vd, var);
578}
579
580void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description) {
581  owl_variable_dict_newvar_int_full(vd, name, default_val, summary, description,
582                                    "<int>", NULL, NULL, NULL);
583}
584
585static 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)
586{
587  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_BOOL, name, summary,
588                                          description, "on,off");
589  var->takes_on_off = true;
590  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_bool_get_default);
591  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_bool_set_default);
592  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_bool_validate_default);
593
594  var->get_tostring_fn = owl_variable_make_closure(
595      var, G_CALLBACK(owl_variable_bool_get_tostring_default),
596      g_cclosure_user_marshal_STRING__VOID);
597  var->set_fromstring_fn = owl_variable_make_closure(
598      var, G_CALLBACK(owl_variable_bool_set_fromstring_default),
599      g_cclosure_user_marshal_INT__STRING);
600
601  g_value_init(&var->val, G_TYPE_BOOLEAN);
602  owl_variable_set_bool(var, default_val);
603
604  var->default_str = owl_variable_get_tostring(var);
605  owl_variable_dict_add_variable(vd, var);
606}
607
608void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, bool default_val, const char *summary, const char *description) {
609  owl_variable_dict_newvar_bool_full(vd, name, default_val, summary, description,
610                                     NULL, NULL, NULL);
611}
612
613static 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)
614{
615  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_STRING, name, summary,
616                                          description, validsettings);
617  var->takes_on_off = false;
618  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_string_get_default);
619  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_string_set_default);
620  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_string_validate_default);
621
622  var->get_tostring_fn = owl_variable_make_closure(
623      var, G_CALLBACK(owl_variable_string_get_tostring_default),
624      g_cclosure_user_marshal_STRING__VOID);
625  var->set_fromstring_fn = owl_variable_make_closure(
626      var, G_CALLBACK(owl_variable_string_set_fromstring_default),
627      g_cclosure_user_marshal_INT__STRING);
628
629  g_value_init(&var->val, G_TYPE_STRING);
630  owl_variable_set_string(var, default_val);
631
632  var->default_str = owl_variable_get_tostring(var);
633  owl_variable_dict_add_variable(vd, var);
634}
635
636void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) {
637  owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description,
638                                       "<string>", NULL, NULL, NULL);
639}
640
641void owl_variable_dict_newvar_path(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) {
642  owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description,
643                                       "<path>", NULL, NULL, NULL);
644}
645
646static 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)
647{
648  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary,
649                                          description, validsettings);
650  var->takes_on_off = false;
651  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default);
652  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default);
653  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_enum_validate);
654
655  var->get_tostring_fn = owl_variable_make_closure(
656      var, G_CALLBACK(owl_variable_enum_get_tostring),
657      g_cclosure_user_marshal_STRING__VOID);
658  var->set_fromstring_fn = owl_variable_make_closure(
659      var, G_CALLBACK(owl_variable_enum_set_fromstring),
660      g_cclosure_user_marshal_INT__STRING);
661
662  g_value_init(&var->val, G_TYPE_INT);
663  owl_variable_set_int(var, default_val);
664
665  var->default_str = owl_variable_get_tostring(var);
666  owl_variable_dict_add_variable(vd, var);
667}
668
669void owl_variable_dict_newvar_enum(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validset) {
670  owl_variable_dict_newvar_enum_full(vd, name, default_val, summary, description,
671                                     validset, NULL, NULL, NULL);
672}
673
674void 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)
675{
676  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_OTHER, name, summary,
677                                          description, validsettings);
[3b9ca71]678  var->takes_on_off = takes_on_off;
[353719a]679
[69f74c2]680  var->get_tostring_fn = g_closure_ref(get_tostring_fn);
681  g_closure_sink(get_tostring_fn);
[a695a68]682
[69f74c2]683  var->set_fromstring_fn = g_closure_ref(set_fromstring_fn);
684  g_closure_sink(set_fromstring_fn);
[06e04a9]685
[69f74c2]686  var->default_str = owl_variable_get_tostring(var);
[353719a]687
[69f74c2]688  /* Note: this'll overwrite any existing variable of that name, even a C one,
689     but it's consistent with previous behavior and commands. */
690  owl_variable_dict_add_variable(vd, var);
[a695a68]691}
692
[0fef6eb]693void owl_variable_dict_cleanup(owl_vardict *d)
694{
[bf7aa1d]695  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
[7d4fbcd]696}
697
[ce68f23]698CALLER_OWN GPtrArray *owl_variable_dict_get_names(const owl_vardict *d) {
699  return owl_dict_get_keys(d);
[7d4fbcd]700}
701
[f41956d]702void owl_variable_delete(owl_variable *v)
[3b0edaa]703{
[06e04a9]704  g_free(v->name);
705  g_free(v->summary);
706  g_free(v->description);
[353719a]707  g_free(v->default_str);
[5001a3d]708  g_free(v->validsettings);
[f20daff]709  if (v->type != OWL_VARIABLE_OTHER)
710    g_value_unset(&(v->val));
[06e04a9]711  g_closure_unref(v->get_tostring_fn);
712  g_closure_unref(v->set_fromstring_fn);
[9efc154]713
[7dcef03]714  g_slice_free(owl_variable, v);
[7d4fbcd]715}
716
717
[ca54fd6]718const char *owl_variable_get_name(const owl_variable *v)
719{
720  return v->name;
721}
722
[64735f0]723const char *owl_variable_get_description(const owl_variable *v) {
[aa2f33b3]724  return v->description;
725}
726
[64735f0]727const char *owl_variable_get_summary(const owl_variable *v) {
[aa2f33b3]728  return v->summary;
[7d4fbcd]729}
730
[64735f0]731const char *owl_variable_get_validsettings(const owl_variable *v) {
[fa981f3]732  return v->validsettings;
[7d4fbcd]733}
734
[81e93c3]735bool owl_variable_takes_on_off(const owl_variable *v) {
736  return v->takes_on_off;
737}
738
[ca54fd6]739int owl_variable_get_type(const owl_variable *v)
740{
741  return v->type;
742}
743
[7d4fbcd]744/* returns 0 on success, prints a status msg if msg is true */
[ca54fd6]745int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
[010a951]746  char *tostring;
[353719a]747  GValue values[] = {G_VALUE_INIT, G_VALUE_INIT};
748  GValue return_box = G_VALUE_INIT;
[f0a7c09]749  int set_successfully;
[63d3e4e]750
[353719a]751  g_value_init(&values[0], G_TYPE_POINTER);
752  g_value_set_pointer(&values[0], NULL);
[63d3e4e]753  g_value_init(&values[1], G_TYPE_STRING);
754  g_value_set_static_string(&values[1], value);
[353719a]755  g_value_init(&return_box, G_TYPE_INT);
[06e04a9]756  g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL);
[63d3e4e]757
[06e04a9]758  set_successfully = g_value_get_int(&return_box);
759  if (0 != set_successfully) {
[ca54fd6]760    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
761                                owl_variable_get_validsettings(v));
[897fc1c]762  } else if (msg) {
[353719a]763    tostring = owl_variable_get_tostring(v);
[06e04a9]764    if (tostring) {
[ca749a9]765      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
[06e04a9]766    } else {
[ca749a9]767      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
[06e04a9]768    }
[010a951]769    g_free(tostring);
[06e04a9]770  }
[63d3e4e]771
772  g_value_unset(&return_box);
773  g_value_unset(&values[1]);
774  g_value_unset(&values[0]);
[06e04a9]775  return set_successfully;
776}
[7d4fbcd]777 
[ca54fd6]778int owl_variable_set_string(owl_variable *v, const char *newval)
779{
[353719a]780  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1);
781
782  set_string_t cb = (set_string_t) v->set_fn;
783  return cb(v, newval);
[7d4fbcd]784}
[06e04a9]785
[ca54fd6]786int owl_variable_set_int(owl_variable *v, int newval)
787{
[353719a]788  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1);
789
790  set_int_t cb = (set_int_t) v->set_fn;
791  return cb(v, newval);
[7d4fbcd]792}
[06e04a9]793
[4584d1f]794int owl_variable_set_bool(owl_variable *v, bool newval) {
[353719a]795  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1);
796
797  set_bool_t cb = (set_bool_t) v->set_fn;
798  return cb(v, newval);
[06e04a9]799}
800
[ca54fd6]801int owl_variable_set_bool_on(owl_variable *v)
802{
803  if (v->type != OWL_VARIABLE_BOOL) return -1;
[06e04a9]804  return owl_variable_set_bool(v, true);
[7d4fbcd]805}
806
[ca54fd6]807int owl_variable_set_bool_off(owl_variable *v)
808{
809  if (v->type != OWL_VARIABLE_BOOL) return -1;
[06e04a9]810  return owl_variable_set_bool(v, false);
[7d4fbcd]811}
812
[ca54fd6]813CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
[d427f08]814{
[353719a]815  GValue instance = G_VALUE_INIT;
816  GValue tostring_box = G_VALUE_INIT;
[06e04a9]817  char *ret = NULL;
[353719a]818
819  g_value_init(&instance, G_TYPE_POINTER);
820  g_value_set_pointer(&instance, NULL);
821  g_value_init(&tostring_box, G_TYPE_STRING);
822  g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL);
823
824  ret = g_value_dup_string(&tostring_box);
825
[63d3e4e]826  g_value_unset(&tostring_box);
827  g_value_unset(&instance);
[06e04a9]828  return ret;
[7d4fbcd]829}
830
[353719a]831const char *owl_variable_get_default_tostring(const owl_variable *v)
[bc1d648]832{
[353719a]833  return v->default_str;
[d536e72]834}
835
[353719a]836owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
[bc1d648]837{
[353719a]838  return owl_dict_find_element(d, name);
[7d4fbcd]839}
840
[ca54fd6]841const char *owl_variable_get_string(const owl_variable *v)
842{
[353719a]843  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL);
844
845  get_string_t cb = (get_string_t) v->get_fn;
846  return cb(v);
[7d4fbcd]847}
848
[ca54fd6]849int owl_variable_get_int(const owl_variable *v)
850{
[353719a]851  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0);
852
853  get_int_t cb = (get_int_t) v->get_fn;
854  return cb(v);
[7d4fbcd]855}
856
[4584d1f]857bool owl_variable_get_bool(const owl_variable *v)
[ca54fd6]858{
[353719a]859  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE);
860
861  get_bool_t cb = (get_bool_t) v->get_fn;
862  return cb(v);
[7d4fbcd]863}
864
[ca54fd6]865void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
866{
[353719a]867  const char *default_str = owl_variable_get_default_tostring(v);
[ca749a9]868  char *default_buf;
869
[353719a]870  if (default_str)
871    default_buf = g_strdup_printf("'%s'", default_str);
[ca749a9]872  else
873    default_buf = g_strdup("<null>");
874  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
875                            owl_variable_get_name(v),
876                            owl_variable_get_summary(v), default_buf);
[010a951]877  g_free(default_buf);
[7d4fbcd]878}
879
[ca54fd6]880void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
[010a951]881  char *tostring;
[353719a]882  const char *default_str;
[7d4fbcd]883
884  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
885  owl_fmtext_append_normal(fm, OWL_TABSTR);
[ca54fd6]886  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
[7d4fbcd]887  owl_fmtext_append_normal(fm, " - ");
[ca54fd6]888  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
[7d4fbcd]889  owl_fmtext_append_normal(fm, "\n\n");
890
891  owl_fmtext_append_normal(fm, "Current:        ");
[ca54fd6]892  tostring = owl_variable_get_tostring(v);
[ca749a9]893  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
[010a951]894  g_free(tostring);
[7d4fbcd]895  owl_fmtext_append_normal(fm, "\n\n");
896
[353719a]897  default_str = owl_variable_get_default_tostring(v);
[7d4fbcd]898  owl_fmtext_append_normal(fm, "Default:        ");
[353719a]899  owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>"));
[7d4fbcd]900  owl_fmtext_append_normal(fm, "\n\n");
901
902  owl_fmtext_append_normal(fm, "Valid Settings: ");
903  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
904  owl_fmtext_append_normal(fm, "\n\n");
[aa2f33b3]905
906  if (v->description && *v->description) {
907    owl_fmtext_append_normal(fm, "Description:\n");
908    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
909    owl_fmtext_append_normal(fm, "\n\n");
910  }
[7d4fbcd]911}
912
913
914
915
916/**************************************************************************/
917/*********************** GENERAL TYPE-SPECIFIC ****************************/
918/**************************************************************************/
919
920/* default common functions */
921
[353719a]922const char *owl_variable_string_get_default(const owl_variable *v) {
[06e04a9]923  return g_value_get_string(&(v->val));
[7d4fbcd]924}
925
[353719a]926int owl_variable_int_get_default(const owl_variable *v) {
[06e04a9]927  return g_value_get_int(&(v->val));
928}
929
[4584d1f]930bool owl_variable_bool_get_default(const owl_variable *v) {
[06e04a9]931  return g_value_get_boolean(&(v->val));
932}
933
[7d4fbcd]934/* default functions for booleans */
935
[4584d1f]936int owl_variable_bool_validate_default(const owl_variable *v, bool newval) {
[06e04a9]937  return (newval == 1) || (newval == 0);
938}
939
[353719a]940int owl_variable_bool_set_default(owl_variable *v, bool newval) {
941  if (!((validate_bool_t)v->validate_fn)(v, newval))
942    return -1;
[7d4fbcd]943
[06e04a9]944  g_value_set_boolean(&(v->val), newval);
[7d4fbcd]945  return(0);
946}
947
[06e04a9]948int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
[4584d1f]949  bool i;
[06e04a9]950  if (!strcmp(newval, "on")) {
[4584d1f]951    i = true;
[06e04a9]952  } else if (!strcmp(newval, "off")) {
[4584d1f]953    i = false;
[06e04a9]954  } else {
955    return(-1);
956  }
[353719a]957
958  return owl_variable_set_bool(v, i);
[7d4fbcd]959}
960
[353719a]961CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy)
[d427f08]962{
[94b9ee0]963  return g_strdup(owl_variable_get_bool(v) ? "on" : "off");
[7d4fbcd]964}
965
966/* default functions for integers */
967
[353719a]968int owl_variable_int_validate_default(const owl_variable *v, int newval)
[06e04a9]969{
970  return (1);
[7d4fbcd]971}
972
[353719a]973int owl_variable_int_set_default(owl_variable *v, int newval) {
974  if (!((validate_int_t)v->validate_fn)(v, newval))
975    return -1;
976
[06e04a9]977  g_value_set_int(&(v->val), newval);
[7d4fbcd]978  return(0);
979}
980
[06e04a9]981int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
[7d4fbcd]982  int i;
[99525be]983  char *ep;
984  i = strtol(newval, &ep, 10);
[7d4fbcd]985  if (*ep || ep==newval) return(-1);
[353719a]986  return owl_variable_set_int(v, i);
[7d4fbcd]987}
988
[353719a]989CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy)
[d427f08]990{
[353719a]991  return g_strdup_printf("%d", owl_variable_get_int(v));
[7d4fbcd]992}
993
994/* default functions for enums (a variant of integers) */
995
[353719a]996int owl_variable_enum_validate(const owl_variable *v, int newval) {
[7d4fbcd]997  char **enums;
998  int nenums, val;
[d275eb2]999  enums = g_strsplit_set(v->validsettings, ",", 0);
1000  nenums = g_strv_length(enums);
1001  g_strfreev(enums);
[06e04a9]1002  val = newval;
[7d4fbcd]1003  if (val < 0 || val >= nenums) {
1004    return(0);
1005  }
1006  return(1);
1007}
1008
[06e04a9]1009int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval, void *dummy) {
[7d4fbcd]1010  char **enums;
[d275eb2]1011  int i, val=-1;
[7d4fbcd]1012  if (newval == NULL) return(-1);
[d275eb2]1013  enums = g_strsplit_set(v->validsettings, ",", 0);
1014  for (i = 0; enums[i] != NULL; i++) {
[7d4fbcd]1015    if (0==strcmp(newval, enums[i])) {
1016      val = i;
1017    }
1018  }
[d275eb2]1019  g_strfreev(enums);
[7d4fbcd]1020  if (val == -1) return(-1);
[353719a]1021  return owl_variable_set_int(v, val);
[7d4fbcd]1022}
1023
[353719a]1024CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy)
[d427f08]1025{
[7d4fbcd]1026  char **enums;
1027  int nenums, i;
[010a951]1028  char *tostring;
[7d4fbcd]1029
[d275eb2]1030  enums = g_strsplit_set(v->validsettings, ",", 0);
1031  nenums = g_strv_length(enums);
[353719a]1032  i = owl_variable_get_int(v);
[7d4fbcd]1033  if (i<0 || i>=nenums) {
[d275eb2]1034    g_strfreev(enums);
[010a951]1035    return g_strdup_printf("<invalid:%d>", i);
[7d4fbcd]1036  }
[010a951]1037  tostring = g_strdup(enums[i]);
[d275eb2]1038  g_strfreev(enums);
[010a951]1039  return tostring;
[7d4fbcd]1040}
1041
1042/* default functions for stringeans */
1043
[353719a]1044int owl_variable_string_validate_default(const owl_variable *v, const char *newval) {
[7d4fbcd]1045  if (newval == NULL) return(0);
1046  else return (1);
1047}
1048
[353719a]1049int owl_variable_string_set_default(owl_variable *v, const char *newval) {
1050  if (!((validate_string_t)v->validate_fn)(v, newval))
1051    return -1;
[06e04a9]1052
1053  g_value_set_string(&(v->val), newval);
[7d4fbcd]1054  return(0);
1055}
1056
[06e04a9]1057int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 
1058{
[353719a]1059  return owl_variable_set_string(v, newval);
[7d4fbcd]1060}
1061
[353719a]1062CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy)
[d427f08]1063{
[353719a]1064  return g_strdup(owl_variable_get_string(v));
[7d4fbcd]1065}
1066
Note: See TracBrowser for help on using the repository browser.