source: variable.c @ 4479497

release-1.10release-1.8release-1.9
Last change on this file since 4479497 was 010a951, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Make owl_variable's get_tostring_fn return a newly allocated string The mess with buffer lengths and whatnot is obnoxious. Note that this does change semantics slightly: instead of (optionally) filling a buffer and giving a return code for good vs no-variable/invalid, we now return NULL for missing variables and always return a string if the variable exists. This also appears to be more accurate from the perspective of the calls anyway.
  • Property mode set to 100644
File size: 36.3 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
[c01e477]32#define OWLVAR_STRING_FULL(name,default,summary,description,validate,set,get) \
33        { name, OWL_VARIABLE_STRING, default, 0, "<string>", summary,description, NULL, \
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
[c01e477]268  OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "tty name for zephyr location", "",
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
372  /* This MUST be last... */
[aa2f33b3]373  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
374    NULL, NULL, NULL, NULL, NULL, NULL }
[7d4fbcd]375
376};
377
378/**************************************************************************/
379/*********************** SPECIFIC TO VARIABLES ****************************/
380/**************************************************************************/
381
382
383/* commonly useful */
384
[64735f0]385int owl_variable_int_validate_gt0(const owl_variable *v, const void *newval)
[4357be8]386{
[7d4fbcd]387  if (newval == NULL) return(0);
[defe4a3]388  else if (*(const int*)newval < 1) return(0);
[7d4fbcd]389  else return (1);
390}
391
[64735f0]392int owl_variable_int_validate_positive(const owl_variable *v, const void *newval)
[4357be8]393{
[7d4fbcd]394  if (newval == NULL) return(0);
[defe4a3]395  else if (*(const int*)newval < 0) return(0);
[7d4fbcd]396  else return (1);
397}
398
399/* typewinsize */
[e19eb97]400int owl_variable_typewinsize_set(owl_variable *v, const void *newval)
[4357be8]401{
[7d4fbcd]402  int rv;
403  rv = owl_variable_int_set_default(v, newval);
[f6fae8d]404  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
[7d4fbcd]405  return(rv);
406}
407
408/* debug (cache value in g->debug) */
[e19eb97]409int owl_variable_debug_set(owl_variable *v, const void *newval)
[4357be8]410{
[defe4a3]411  if (newval && (*(const int*)newval == 1 || *(const int*)newval == 0)) {
412    g.debug = *(const int*)newval;
[7d4fbcd]413  }
414  return owl_variable_bool_set_default(v, newval);
[4b660cc]415}
416
417/* When 'aaway' is changed, need to notify the AIM server */
[e19eb97]418int owl_variable_aaway_set(owl_variable *v, const void *newval)
[4357be8]419{
[4b660cc]420  if (newval) {
[defe4a3]421    if (*(const int*)newval == 1) {
[4b660cc]422      owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g));
[defe4a3]423    } else if (*(const int*)newval == 0) {
[4b660cc]424      owl_aim_set_awaymsg("");
425    }
426  }
427  return owl_variable_bool_set_default(v, newval);
[7d4fbcd]428}
429
[e19eb97]430int owl_variable_pseudologins_set(owl_variable *v, const void *newval)
[4357be8]431{
[3687413]432  static owl_timer *timer = NULL;
[4357be8]433  if (newval) {
[defe4a3]434    if (*(const int*)newval == 1) {
[4357be8]435      owl_function_zephyr_buddy_check(0);
[3687413]436      if (timer == NULL) {
[c6adf17]437        timer = owl_select_add_timer("owl_zephyr_buddycheck_timer",
438                                     180, 180, owl_zephyr_buddycheck_timer, NULL, NULL);
[3687413]439      }
440    } else {
441      if (timer != NULL) {
442        owl_select_remove_timer(timer);
443        timer = NULL;
444      }
[4357be8]445    }
446  }
447  return owl_variable_bool_set_default(v, newval);
448}
449
[7d4fbcd]450/* note that changing the value of this will clobber
451 * any user setting of this */
[e19eb97]452int owl_variable_disable_ctrl_d_set(owl_variable *v, const void *newval)
[4357be8]453{
[217a43e]454  if (newval && !owl_context_is_startup(owl_global_get_context(&g))) {
[defe4a3]455    if (*(const int*)newval == 2) {
[7d4fbcd]456      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
[defe4a3]457    } else if (*(const int*)newval == 1) {
[3b42640]458      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
[7d4fbcd]459    } else {
[3b42640]460      owl_function_command_norv("bindkey editmulti C-d command edit:done");
[7d4fbcd]461    }
462  } 
[217a43e]463  return owl_variable_int_set_default(v, newval); 
[7d4fbcd]464}
465
[e19eb97]466int owl_variable_tty_set(owl_variable *v, const void *newval)
[4357be8]467{
[f54b07d]468  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
[c01e477]469  return(owl_variable_string_set_default(v, newval));
470}
471
[7d4fbcd]472
473/**************************************************************************/
474/****************************** GENERAL ***********************************/
475/**************************************************************************/
476
477int owl_variable_dict_setup(owl_vardict *vd) {
[d536e72]478  owl_variable *var, *cur;
[7d4fbcd]479  if (owl_dict_create(vd)) return(-1);
[d536e72]480  for (var = variables_to_init; var->name != NULL; var++) {
[96828e4]481    cur = g_new(owl_variable, 1);
[66a8cd6]482    *cur = *var;
[7d7326c]483    /* strdup all the strings so we can delete them consistently. */
[d4927a7]484    cur->name = g_strdup(var->name);
485    cur->summary = g_strdup(var->summary);
486    cur->description = g_strdup(var->description);
[7d4fbcd]487    switch (cur->type) {
488    case OWL_VARIABLE_OTHER:
489      cur->set_fn(cur, cur->pval_default);
490      break;
491    case OWL_VARIABLE_STRING:
492      if (!cur->validate_fn) 
493        cur->validate_fn = owl_variable_string_validate_default;
494      if (!cur->set_fn) 
495        cur->set_fn = owl_variable_string_set_default;
496      if (!cur->set_fromstring_fn) 
497        cur->set_fromstring_fn = owl_variable_string_set_fromstring_default;
498      if (!cur->get_fn) 
499        cur->get_fn = owl_variable_get_default;
500      if (!cur->get_tostring_fn) 
501        cur->get_tostring_fn = owl_variable_string_get_tostring_default;     
[bbd74a9]502      if (!cur->delete_fn)
503        cur->delete_fn = owl_variable_delete_default;
[7d4fbcd]504      cur->set_fn(cur, cur->pval_default);
505      break;
506    case OWL_VARIABLE_BOOL:
507      if (!cur->validate_fn) 
508        cur->validate_fn = owl_variable_bool_validate_default;
509      if (!cur->set_fn) 
510        cur->set_fn = owl_variable_bool_set_default;
511      if (!cur->set_fromstring_fn) 
512        cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
513      if (!cur->get_fn) 
514        cur->get_fn = owl_variable_get_default;
515      if (!cur->get_tostring_fn) 
516        cur->get_tostring_fn = owl_variable_bool_get_tostring_default;     
[bbd74a9]517      if (!cur->delete_fn)
518        cur->delete_fn = owl_variable_delete_default;
[96828e4]519      cur->val = g_new(int, 1);
[7d4fbcd]520      cur->set_fn(cur, &cur->ival_default);
521      break;
522    case OWL_VARIABLE_INT:
523      if (!cur->validate_fn) 
524        cur->validate_fn = owl_variable_int_validate_default;
525      if (!cur->set_fn) 
526        cur->set_fn = owl_variable_int_set_default;
527      if (!cur->set_fromstring_fn) 
528        cur->set_fromstring_fn = owl_variable_int_set_fromstring_default;
529      if (!cur->get_fn) 
530        cur->get_fn = owl_variable_get_default;
531      if (!cur->get_tostring_fn) 
532        cur->get_tostring_fn = owl_variable_int_get_tostring_default;     
[bbd74a9]533      if (!cur->delete_fn)
534        cur->delete_fn = owl_variable_delete_default;
[96828e4]535      cur->val = g_new(int, 1);
[7d4fbcd]536      cur->set_fn(cur, &cur->ival_default);
537      break;
538    default:
539      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
540      return(-2);
541    }
[4d86e06]542    owl_dict_insert_element(vd, cur->name, cur, NULL);
[7d4fbcd]543  }
544  return 0;
545}
546
[a695a68]547void owl_variable_dict_add_variable(owl_vardict * vardict,
548                                    owl_variable * var) {
[3b0edaa]549  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
[a695a68]550}
551
[e19eb97]552owl_variable * owl_variable_newvar(const char *name, const char *summary, const char * description) {
[96828e4]553  owl_variable * var = g_new0(owl_variable, 1);
[d4927a7]554  var->name = g_strdup(name);
555  var->summary = g_strdup(summary);
556  var->description = g_strdup(description);
[a695a68]557  return var;
558}
559
[e19eb97]560void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
[ddbbcffa]561  if(var->summary) g_free(var->summary);
[d4927a7]562  var->summary = g_strdup(summary);
[ddbbcffa]563  if(var->description) g_free(var->description);
[d4927a7]564  var->description = g_strdup(desc);
[d536e72]565}
566
[e19eb97]567void owl_variable_dict_newvar_string(owl_vardict * vd, const char *name, const char *summ, const char * desc, const char * initval) {
[d536e72]568  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_STRING);
569  if(old) {
570    owl_variable_update(old, summ, desc);
[ddbbcffa]571    if(old->pval_default) g_free(old->pval_default);
[d4927a7]572    old->pval_default = g_strdup(initval);
[d536e72]573  } else {
574    owl_variable * var = owl_variable_newvar(name, summ, desc);
575    var->type = OWL_VARIABLE_STRING;
[d4927a7]576    var->pval_default = g_strdup(initval);
[d536e72]577    var->set_fn = owl_variable_string_set_default;
578    var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
579    var->get_fn = owl_variable_get_default;
580    var->get_tostring_fn = owl_variable_string_get_tostring_default;
[bbd74a9]581    var->delete_fn = owl_variable_delete_default;
[d536e72]582    var->set_fn(var, initval);
583    owl_variable_dict_add_variable(vd, var);
584  }
[a695a68]585}
586
[e19eb97]587void owl_variable_dict_newvar_int(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
[d536e72]588  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_INT);
589  if(old) {
590    owl_variable_update(old, summ, desc);
591    old->ival_default = initval;
592  } else {
593    owl_variable * var = owl_variable_newvar(name, summ, desc);
594    var->type = OWL_VARIABLE_INT;
595    var->ival_default = initval;
596    var->validate_fn = owl_variable_int_validate_default;
597    var->set_fn = owl_variable_int_set_default;
598    var->set_fromstring_fn = owl_variable_int_set_fromstring_default;
599    var->get_fn = owl_variable_get_default;
600    var->get_tostring_fn = owl_variable_int_get_tostring_default;
[bbd74a9]601    var->delete_fn = owl_variable_delete_default;
[96828e4]602    var->val = g_new(int, 1);
[d536e72]603    var->set_fn(var, &initval);
604    owl_variable_dict_add_variable(vd, var);
605  }
[a695a68]606}
607
[e19eb97]608void owl_variable_dict_newvar_bool(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
[d536e72]609  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_BOOL);
610  if(old) {
611    owl_variable_update(old, summ, desc);
612    old->ival_default = initval;
613  } else {
614    owl_variable * var = owl_variable_newvar(name, summ, desc);
615    var->type = OWL_VARIABLE_BOOL;
616    var->ival_default = initval;
617    var->validate_fn = owl_variable_bool_validate_default;
618    var->set_fn = owl_variable_bool_set_default;
619    var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
620    var->get_fn = owl_variable_get_default;
621    var->get_tostring_fn = owl_variable_bool_get_tostring_default;
[bbd74a9]622    var->delete_fn = owl_variable_delete_default;
[96828e4]623    var->val = g_new(int, 1);
[d536e72]624    var->set_fn(var, &initval);
625    owl_variable_dict_add_variable(vd, var);
626  }
[a695a68]627}
628
[0fef6eb]629void owl_variable_dict_cleanup(owl_vardict *d)
630{
[bf7aa1d]631  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
[7d4fbcd]632}
633
[0e57335]634/* free the list with owl_variable_dict_namelist_cleanup */
[e5c9b14a]635void owl_variable_dict_get_names(const owl_vardict *d, owl_list *l) {
[7d4fbcd]636  owl_dict_get_keys(d, l);
637}
638
[0e57335]639void owl_variable_dict_namelist_cleanup(owl_list *l)
640{
[ddbbcffa]641  owl_list_cleanup(l, g_free);
[7d4fbcd]642}
643
[3b0edaa]644void owl_variable_delete(owl_variable *v)
645{
[bbd74a9]646  if (v->delete_fn) v->delete_fn(v);
[ddbbcffa]647  g_free(v->name);
648  g_free(v->summary);
649  g_free(v->description);
650  g_free(v);
[7d4fbcd]651}
652
653
[64735f0]654const char *owl_variable_get_description(const owl_variable *v) {
[aa2f33b3]655  return v->description;
656}
657
[64735f0]658const char *owl_variable_get_summary(const owl_variable *v) {
[aa2f33b3]659  return v->summary;
[7d4fbcd]660}
661
[64735f0]662const char *owl_variable_get_validsettings(const owl_variable *v) {
[7d4fbcd]663  if (v->validsettings) {
664    return v->validsettings;
665  } else {
666    return "";
667  }
668}
669
670/* functions for getting and setting variable values */
671
672/* returns 0 on success, prints a status msg if msg is true */
[e19eb97]673int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) {
[7d4fbcd]674  owl_variable *v;
[010a951]675  char *tostring;
[7d4fbcd]676  if (!name) return(-1);
677  v = owl_dict_find_element(d, name);
678  if (v == NULL) {
[836ea3a3]679    if (msg) owl_function_error("Unknown variable %s", name);
[7d4fbcd]680    return -1;
681  }
682  if (!v->set_fromstring_fn) {
[836ea3a3]683    if (msg) owl_function_error("Variable %s is read-only", name);
[486688f]684    return -1;   
685  }
686  if (requirebool && v->type!=OWL_VARIABLE_BOOL) {
[836ea3a3]687    if (msg) owl_function_error("Variable %s is not a boolean", name);
[486688f]688    return -1;   
[7d4fbcd]689  }
690  if (0 != v->set_fromstring_fn(v, value)) {
[836ea3a3]691    if (msg) owl_function_error("Unable to set %s (must be %s)", name, 
[7d4fbcd]692                                  owl_variable_get_validsettings(v));
693    return -1;
694  }
695  if (msg && v->get_tostring_fn) {
[010a951]696    tostring = v->get_tostring_fn(v, v->val);
697    owl_function_makemsg("%s = '%s'", name, tostring);
698    g_free(tostring);
[7d4fbcd]699  }   
700  return 0;
701}
702 
[e19eb97]703int owl_variable_set_string(owl_vardict *d, const char *name, const char *newval) {
[7d4fbcd]704  owl_variable *v;
705  if (!name) return(-1);
706  v = owl_dict_find_element(d, name);
707  if (v == NULL || !v->set_fn) return(-1);
708  if (v->type!=OWL_VARIABLE_STRING) return(-1);
[ba6c8bd]709  return v->set_fn(v, newval);
[7d4fbcd]710}
711 
[e19eb97]712int owl_variable_set_int(owl_vardict *d, const char *name, int newval) {
[7d4fbcd]713  owl_variable *v;
714  if (!name) return(-1);
715  v = owl_dict_find_element(d, name);
716  if (v == NULL || !v->set_fn) return(-1);
717  if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1);
718  return v->set_fn(v, &newval);
719}
720 
[e19eb97]721int owl_variable_set_bool_on(owl_vardict *d, const char *name) {
[7d4fbcd]722  return owl_variable_set_int(d,name,1);
723}
724
[e19eb97]725int owl_variable_set_bool_off(owl_vardict *d, const char *name) {
[7d4fbcd]726  return owl_variable_set_int(d,name,0);
727}
728
[010a951]729char *owl_variable_get_tostring(const owl_vardict *d, const char *name) {
[7d4fbcd]730  owl_variable *v;
[010a951]731  if (!name) return NULL;
[7d4fbcd]732  v = owl_dict_find_element(d, name);
[010a951]733  if (v == NULL || !v->get_tostring_fn) return NULL;
734  return v->get_tostring_fn(v, v->val);
[7d4fbcd]735}
736
[010a951]737char *owl_variable_get_default_tostring(const owl_vardict *d, const char *name) {
[7d4fbcd]738  owl_variable *v;
[010a951]739  if (!name) return NULL;
[7d4fbcd]740  v = owl_dict_find_element(d, name);
[010a951]741  if (v == NULL || !v->get_tostring_fn) return NULL;
[7d4fbcd]742  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
[010a951]743    return v->get_tostring_fn(v, &(v->ival_default));
[7d4fbcd]744  } else {
[010a951]745    return v->get_tostring_fn(v, v->pval_default);
[7d4fbcd]746  }
747}
748
[e5c9b14a]749owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name, int require_type) {
[7d4fbcd]750  owl_variable *v;
751  if (!name) return(NULL);
752  v = owl_dict_find_element(d, name);
753  if (v == NULL || !v->get_fn || v->type != require_type) return(NULL);
[d536e72]754  return v;
755}
756
757/* returns a reference */
[e5c9b14a]758const void *owl_variable_get(const owl_vardict *d, const char *name, int require_type) {
[d536e72]759  owl_variable *v = owl_variable_get_var(d, name, require_type);
760  if(v == NULL) return NULL;
[7d4fbcd]761  return v->get_fn(v);
762}
763
764/* returns a reference */
[e5c9b14a]765const char *owl_variable_get_string(const owl_vardict *d, const char *name) {
[4d86e06]766  return owl_variable_get(d,name, OWL_VARIABLE_STRING);
[7d4fbcd]767}
768
769/* returns a reference */
[e5c9b14a]770const void *owl_variable_get_other(const owl_vardict *d, const char *name) {
[4d86e06]771  return owl_variable_get(d,name, OWL_VARIABLE_OTHER);
[7d4fbcd]772}
773
[e5c9b14a]774int owl_variable_get_int(const owl_vardict *d, const char *name) {
[defe4a3]775  const int *pi;
[4d86e06]776  pi = owl_variable_get(d,name,OWL_VARIABLE_INT);
[7d4fbcd]777  if (!pi) return(-1);
778  return(*pi);
779}
780
[e5c9b14a]781int owl_variable_get_bool(const owl_vardict *d, const char *name) {
[defe4a3]782  const int *pi;
[4d86e06]783  pi = owl_variable_get(d,name,OWL_VARIABLE_BOOL);
[7d4fbcd]784  if (!pi) return(-1);
785  return(*pi);
786}
787
[e5c9b14a]788void owl_variable_describe(const owl_vardict *d, const char *name, owl_fmtext *fm) {
[010a951]789  char *default_buf;
[7d4fbcd]790  owl_variable *v;
791
792  if (!name
793      || (v = owl_dict_find_element(d, name)) == NULL 
794      || !v->get_fn) {
[010a951]795    owl_fmtext_appendf_normal(fm, "     No such variable '%s'\n", name);
[7d4fbcd]796    return;
797  }
798  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
[010a951]799    default_buf = v->get_tostring_fn(v, &(v->ival_default));
[7d4fbcd]800  } else {
[010a951]801    default_buf = v->get_tostring_fn(v, v->pval_default);
[7d4fbcd]802  }
[010a951]803  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: '%s')\n",
804                            v->name,
805                            owl_variable_get_summary(v), default_buf);
806  g_free(default_buf);
[7d4fbcd]807}
808
[e5c9b14a]809void owl_variable_get_help(const owl_vardict *d, const char *name, owl_fmtext *fm) {
[010a951]810  char *tostring;
[7d4fbcd]811  owl_variable *v;
812
813  if (!name
814      || (v = owl_dict_find_element(d, name)) == NULL 
815      || !v->get_fn) {
816    owl_fmtext_append_normal(fm, "No such variable...\n");
817    return;
818  }
819
820  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
821  owl_fmtext_append_normal(fm, OWL_TABSTR);
822  owl_fmtext_append_normal(fm, name);
823  owl_fmtext_append_normal(fm, " - ");
[aa2f33b3]824  owl_fmtext_append_normal(fm, v->summary);
[7d4fbcd]825  owl_fmtext_append_normal(fm, "\n\n");
826
827  owl_fmtext_append_normal(fm, "Current:        ");
[010a951]828  tostring = owl_variable_get_tostring(d, name);
829  owl_fmtext_append_normal(fm, tostring);
830  g_free(tostring);
[7d4fbcd]831  owl_fmtext_append_normal(fm, "\n\n");
832
833
834  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
[010a951]835    tostring = v->get_tostring_fn(v, &(v->ival_default));
[7d4fbcd]836  } else {
[010a951]837    tostring = v->get_tostring_fn(v, v->pval_default);
[7d4fbcd]838  }
839  owl_fmtext_append_normal(fm, "Default:        ");
[010a951]840  owl_fmtext_append_normal(fm, tostring);
[7d4fbcd]841  owl_fmtext_append_normal(fm, "\n\n");
842
843  owl_fmtext_append_normal(fm, "Valid Settings: ");
844  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
845  owl_fmtext_append_normal(fm, "\n\n");
[aa2f33b3]846
847  if (v->description && *v->description) {
848    owl_fmtext_append_normal(fm, "Description:\n");
849    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
850    owl_fmtext_append_normal(fm, "\n\n");
851  }
[010a951]852  g_free(tostring);
[7d4fbcd]853}
854
855
856
857
858/**************************************************************************/
859/*********************** GENERAL TYPE-SPECIFIC ****************************/
860/**************************************************************************/
861
862/* default common functions */
863
[64735f0]864const void *owl_variable_get_default(const owl_variable *v) {
[7d4fbcd]865  return v->val;
866}
867
[bbd74a9]868void owl_variable_delete_default(owl_variable *v)
869{
[ddbbcffa]870  if (v->val) g_free(v->val);
[7d4fbcd]871}
872
873/* default functions for booleans */
874
[64735f0]875int owl_variable_bool_validate_default(const owl_variable *v, const void *newval) {
[7d4fbcd]876  if (newval == NULL) return(0);
[defe4a3]877  else if (*(const int*)newval==1 || *(const int*)newval==0) return(1);
[7d4fbcd]878  else return (0);
879}
880
[e19eb97]881int owl_variable_bool_set_default(owl_variable *v, const void *newval) {
[7d4fbcd]882  if (v->validate_fn) {
883    if (!v->validate_fn(v, newval)) return(-1);
884  }
[defe4a3]885  *(int*)v->val = *(const int*)newval;
[7d4fbcd]886  return(0);
887}
888
[e19eb97]889int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval) {
[7d4fbcd]890  int i;
891  if (!strcmp(newval, "on")) i=1;
892  else if (!strcmp(newval, "off")) i=0;
893  else return(-1);
894  return (v->set_fn(v, &i));
895}
896
[010a951]897char *owl_variable_bool_get_tostring_default(const owl_variable *v, const void *val) {
[7d4fbcd]898  if (val == NULL) {
[010a951]899    return g_strdup("<null>");
[defe4a3]900  } else if (*(const int*)val == 0) {
[010a951]901    return g_strdup("off");
[defe4a3]902  } else if (*(const int*)val == 1) {
[010a951]903    return g_strdup("on");
[7d4fbcd]904  } else {
[010a951]905    return g_strdup("<invalid>");
[7d4fbcd]906  }
907}
908
909/* default functions for integers */
910
[64735f0]911int owl_variable_int_validate_default(const owl_variable *v, const void *newval) {
[7d4fbcd]912  if (newval == NULL) return(0);
913  else return (1);
914}
915
[e19eb97]916int owl_variable_int_set_default(owl_variable *v, const void *newval) {
[7d4fbcd]917  if (v->validate_fn) {
918    if (!v->validate_fn(v, newval)) return(-1);
919  }
[defe4a3]920  *(int*)v->val = *(const int*)newval;
[7d4fbcd]921  return(0);
922}
923
[e19eb97]924int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval) {
[7d4fbcd]925  int i;
[99525be]926  char *ep;
927  i = strtol(newval, &ep, 10);
[7d4fbcd]928  if (*ep || ep==newval) return(-1);
929  return (v->set_fn(v, &i));
930}
931
[010a951]932char *owl_variable_int_get_tostring_default(const owl_variable *v, const void *val) {
[7d4fbcd]933  if (val == NULL) {
[010a951]934    return g_strdup("<null>");
[7d4fbcd]935  } else {
[010a951]936    return g_strdup_printf("%d", *(const int*)val);
[7d4fbcd]937  } 
938}
939
940/* default functions for enums (a variant of integers) */
941
[64735f0]942int owl_variable_enum_validate(const owl_variable *v, const void *newval) { 
[7d4fbcd]943  char **enums;
944  int nenums, val;
945  if (newval == NULL) return(0);
[d275eb2]946  enums = g_strsplit_set(v->validsettings, ",", 0);
947  nenums = g_strv_length(enums);
948  g_strfreev(enums);
[defe4a3]949  val = *(const int*)newval;
[7d4fbcd]950  if (val < 0 || val >= nenums) {
951    return(0);
952  }
953  return(1);
954}
955
[e19eb97]956int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval) {
[7d4fbcd]957  char **enums;
[d275eb2]958  int i, val=-1;
[7d4fbcd]959  if (newval == NULL) return(-1);
[d275eb2]960  enums = g_strsplit_set(v->validsettings, ",", 0);
961  for (i = 0; enums[i] != NULL; i++) {
[7d4fbcd]962    if (0==strcmp(newval, enums[i])) {
963      val = i;
964    }
965  }
[d275eb2]966  g_strfreev(enums);
[7d4fbcd]967  if (val == -1) return(-1);
968  return (v->set_fn(v, &val));
969}
970
[010a951]971char *owl_variable_enum_get_tostring(const owl_variable *v, const void *val) {
[7d4fbcd]972  char **enums;
973  int nenums, i;
[010a951]974  char *tostring;
[7d4fbcd]975
976  if (val == NULL) {
[010a951]977    return g_strdup("<null>");
[7d4fbcd]978  }
[d275eb2]979  enums = g_strsplit_set(v->validsettings, ",", 0);
980  nenums = g_strv_length(enums);
[defe4a3]981  i = *(const int*)val;
[7d4fbcd]982  if (i<0 || i>=nenums) {
[d275eb2]983    g_strfreev(enums);
[010a951]984    return g_strdup_printf("<invalid:%d>", i);
[7d4fbcd]985  }
[010a951]986  tostring = g_strdup(enums[i]);
[d275eb2]987  g_strfreev(enums);
[010a951]988  return tostring;
[7d4fbcd]989}
990
991/* default functions for stringeans */
992
[64735f0]993int owl_variable_string_validate_default(const struct _owl_variable *v, const void *newval) {
[7d4fbcd]994  if (newval == NULL) return(0);
995  else return (1);
996}
997
[e19eb97]998int owl_variable_string_set_default(owl_variable *v, const void *newval) {
[7d4fbcd]999  if (v->validate_fn) {
1000    if (!v->validate_fn(v, newval)) return(-1);
1001  }
[ddbbcffa]1002  if (v->val) g_free(v->val);
[d4927a7]1003  v->val = g_strdup(newval);
[7d4fbcd]1004  return(0);
1005}
1006
[e19eb97]1007int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval) {
[7d4fbcd]1008  return (v->set_fn(v, newval));
1009}
1010
[010a951]1011char *owl_variable_string_get_tostring_default(const owl_variable *v, const void *val) {
[7d4fbcd]1012  if (val == NULL) {
[010a951]1013    return g_strdup("<null>");
[7d4fbcd]1014  } else {
[010a951]1015    return g_strdup((const char*)val);
[7d4fbcd]1016  }
1017}
1018
Note: See TracBrowser for help on using the repository browser.