source: variable.c @ bdc53e0

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