source: variable.c @ 4c0ac34

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 4c0ac34 was c6adf17, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Track names along with timers, add :show timers This will help people with BarnOwls eating CPU to diagnose timer leaks.
  • Property mode set to 100644
File size: 36.7 KB
Line 
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
8#define OWLVAR_BOOL(name,default,summary,description) \
9        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary,description, NULL, \
10        NULL, NULL, NULL, NULL, NULL, NULL }
11
12#define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \
13        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary,description, NULL, \
14        validate, set, NULL, get, NULL, NULL }
15
16#define OWLVAR_INT(name,default,summary,description) \
17        { name, OWL_VARIABLE_INT, NULL, default, "<int>", summary,description, NULL, \
18        NULL, NULL, NULL, NULL, NULL, NULL }
19
20#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
21        { name, OWL_VARIABLE_INT, NULL, default, validset, summary,description, NULL, \
22        validate, set, NULL, get, NULL, NULL }
23
24#define OWLVAR_PATH(name,default,summary,description) \
25        { name, OWL_VARIABLE_STRING, default, 0, "<path>", summary,description,  NULL, \
26        NULL, NULL, NULL, NULL, NULL, NULL }
27
28#define OWLVAR_STRING(name,default,summary,description) \
29        { name, OWL_VARIABLE_STRING, default, 0, "<string>", summary,description, NULL, \
30        NULL, NULL, NULL, NULL, NULL, NULL }
31
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
36/* enums are really integers, but where validset is a comma-separated
37 * list of strings which can be specified.  The tokens, starting at 0,
38 * correspond to the values that may be specified. */
39#define OWLVAR_ENUM(name,default,summary,description,validset) \
40        { name, OWL_VARIABLE_INT, NULL, default, validset, summary,description, NULL, \
41        owl_variable_enum_validate, \
42        NULL, owl_variable_enum_set_fromstring, \
43        NULL, owl_variable_enum_get_tostring, \
44        NULL }
45
46#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
47        { name, OWL_VARIABLE_INT, NULL, default, validset, summary,description, NULL, \
48        validate, \
49        set, owl_variable_enum_set_fromstring, \
50        get, owl_variable_enum_get_tostring, \
51        NULL }
52
53static owl_variable variables_to_init[] = {
54
55  OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off",
56                 "ring the terminal bell when personal messages are received",
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"),
59
60  OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1,
61               "enable / disable the terminal bell", "" ),
62
63  OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG,
64                    "whether debugging is enabled",
65                    "If set to 'on', debugging messages are logged to the\n"
66                    "file specified by the debugfile variable.\n",
67                    NULL, owl_variable_debug_set, NULL),
68
69  OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1,
70               "send a login message when owl starts", "" ),
71
72  OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1,
73               "send a logout message when owl exits", "" ),
74
75  OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0,
76               "display received pings", "" ),
77
78  OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1,
79               "send pings", "" ),
80
81  OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0,
82               "disable printing information in the separator bar", "" ),
83
84  OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1,
85               "strip kerberos instance for reply", ""),
86
87  OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1,
88               "strip leading and trailing newlines", ""),
89
90  OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1,
91               "display outgoing messages", "" ),
92
93  OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1,
94               "load logins from .anyone on startup", "" ),
95
96  OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0,
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" ),
102
103  OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0,
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" ),
110
111  OWLVAR_ENUM( "loggingdirection" /* %OwlVarStub */, OWL_LOGGING_DIRECTION_BOTH,
112               "specifies which kind of messages should be logged",
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
120  OWLVAR_BOOL( "colorztext" /* %OwlVarStub */, 1,
121               "allow @color() in zephyrs to change color",
122               "Note that only messages received after this variable\n"
123               "is set will be affected." ),
124
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
131  OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1,
132               "Do automatic zcrypt processing",
133               "" ),
134
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"
140                    "logout message, a pseudo login or logout message will be created\n",
141                    NULL, owl_variable_pseudologins_set, NULL),
142
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"
146               "for AIM, zephyr, or other protocols.  If disabled Owl will not print\n"
147               "login or logout notifications.\n"),
148
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"
153                 "This is a completely separate mechanism from the other logging\n"
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
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
164  OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1,
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",
174                    NULL, owl_variable_disable_ctrl_d_set, NULL),
175
176  OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people",
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"),
180
181  OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class",
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"),
185
186  OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE,
187               "path for logging debug messages when debugging is enabled",
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."),
190 
191  OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL,
192               "name of a program to run that will generate zsigs",
193               "This program should produce a zsig on stdout when run.\n"
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               ),
198
199  OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL,
200               "name of a program to run when new messages are present",
201               "The named program will be run when owl receives new\n"
202               "messages.  It will not be run again until the first\n"
203               "instance exits"),
204
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
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"
216                 "described in the documentation for the 'zsig' variable.\n"
217                 "See also BarnOwl::random_zephyr_signature().\n"),
218
219  OWLVAR_STRING( "zsig" /* %OwlVarStub */, "",
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"),
225
226  OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "",
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"),
231
232  OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0,
233               "turn zaway on or off", "" ),
234
235  OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 
236                 OWL_DEFAULT_ZAWAYMSG,
237                 "zaway msg for responding to zephyrs when away", "" ),
238
239  OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 
240                 OWL_DEFAULT_ZAWAYMSG,
241                 "default zaway message", "" ),
242
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
256  OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all",
257                 "home view to switch to after 'X' and 'V'", 
258                 "SEE ALSO: view, filter\n" ),
259
260  OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none",
261                 "filter on which to trigger alert actions",
262                 "" ),
263
264  OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop",
265                 "owl command to execute for alert actions",
266                 "" ),
267
268  OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "tty name for zephyr location", "",
269                      NULL, owl_variable_tty_set, NULL),
270
271  OWLVAR_STRING( "default_style" /* %OwlVarStub */, "__unspecified__",
272                 "name of the default formatting style",
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
282
283  OWLVAR_INT(    "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70,
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"),
288
289  OWLVAR_INT(    "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70,
290                 "maximum number of columns for line-wrapping",
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"),
297
298  OWLVAR_INT( "aim_ignorelogin_timer" /* %OwlVarStub */, 15,
299              "number of seconds after AIM login to ignore login messages",
300              "This specifies the number of seconds to wait after an\n"
301              "AIM login before allowing the receipt of AIM login notifications.\n"
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."),
304
305             
306  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
307                   OWL_TYPWIN_SIZE,
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",
313                   owl_variable_int_validate_gt0,
314                   owl_variable_typewinsize_set,
315                   NULL /* use default for get */
316                   ),
317
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
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
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" ),
364
365  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
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" ),
371
372  /* This MUST be last... */
373  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
374    NULL, NULL, NULL, NULL, NULL, NULL }
375
376};
377
378/**************************************************************************/
379/*********************** SPECIFIC TO VARIABLES ****************************/
380/**************************************************************************/
381
382
383/* commonly useful */
384
385int owl_variable_int_validate_gt0(const owl_variable *v, const void *newval)
386{
387  if (newval == NULL) return(0);
388  else if (*(const int*)newval < 1) return(0);
389  else return (1);
390}
391
392int owl_variable_int_validate_positive(const owl_variable *v, const void *newval)
393{
394  if (newval == NULL) return(0);
395  else if (*(const int*)newval < 0) return(0);
396  else return (1);
397}
398
399/* typewinsize */
400int owl_variable_typewinsize_set(owl_variable *v, const void *newval)
401{
402  int rv;
403  rv = owl_variable_int_set_default(v, newval);
404  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
405  return(rv);
406}
407
408/* debug (cache value in g->debug) */
409int owl_variable_debug_set(owl_variable *v, const void *newval)
410{
411  if (newval && (*(const int*)newval == 1 || *(const int*)newval == 0)) {
412    g.debug = *(const int*)newval;
413  }
414  return owl_variable_bool_set_default(v, newval);
415}
416
417/* When 'aaway' is changed, need to notify the AIM server */
418int owl_variable_aaway_set(owl_variable *v, const void *newval)
419{
420  if (newval) {
421    if (*(const int*)newval == 1) {
422      owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g));
423    } else if (*(const int*)newval == 0) {
424      owl_aim_set_awaymsg("");
425    }
426  }
427  return owl_variable_bool_set_default(v, newval);
428}
429
430int owl_variable_pseudologins_set(owl_variable *v, const void *newval)
431{
432  static owl_timer *timer = NULL;
433  if (newval) {
434    if (*(const int*)newval == 1) {
435      owl_function_zephyr_buddy_check(0);
436      if (timer == NULL) {
437        timer = owl_select_add_timer("owl_zephyr_buddycheck_timer",
438                                     180, 180, owl_zephyr_buddycheck_timer, NULL, NULL);
439      }
440    } else {
441      if (timer != NULL) {
442        owl_select_remove_timer(timer);
443        timer = NULL;
444      }
445    }
446  }
447  return owl_variable_bool_set_default(v, newval);
448}
449
450/* note that changing the value of this will clobber
451 * any user setting of this */
452int owl_variable_disable_ctrl_d_set(owl_variable *v, const void *newval)
453{
454  if (newval && !owl_context_is_startup(owl_global_get_context(&g))) {
455    if (*(const int*)newval == 2) {
456      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
457    } else if (*(const int*)newval == 1) {
458      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
459    } else {
460      owl_function_command_norv("bindkey editmulti C-d command edit:done");
461    }
462  } 
463  return owl_variable_int_set_default(v, newval); 
464}
465
466int owl_variable_tty_set(owl_variable *v, const void *newval)
467{
468  owl_zephyr_set_locationinfo(owl_global_get_hostname(&g), newval);
469  return(owl_variable_string_set_default(v, newval));
470}
471
472
473/**************************************************************************/
474/****************************** GENERAL ***********************************/
475/**************************************************************************/
476
477int owl_variable_dict_setup(owl_vardict *vd) {
478  owl_variable *var, *cur;
479  if (owl_dict_create(vd)) return(-1);
480  for (var = variables_to_init; var->name != NULL; var++) {
481    cur = owl_malloc(sizeof(owl_variable));
482    *cur = *var;
483    switch (cur->type) {
484    case OWL_VARIABLE_OTHER:
485      cur->set_fn(cur, cur->pval_default);
486      break;
487    case OWL_VARIABLE_STRING:
488      if (!cur->validate_fn) 
489        cur->validate_fn = owl_variable_string_validate_default;
490      if (!cur->set_fn) 
491        cur->set_fn = owl_variable_string_set_default;
492      if (!cur->set_fromstring_fn) 
493        cur->set_fromstring_fn = owl_variable_string_set_fromstring_default;
494      if (!cur->get_fn) 
495        cur->get_fn = owl_variable_get_default;
496      if (!cur->get_tostring_fn) 
497        cur->get_tostring_fn = owl_variable_string_get_tostring_default;     
498      if (!cur->delete_fn)
499        cur->delete_fn = owl_variable_delete_default;
500      cur->set_fn(cur, cur->pval_default);
501      break;
502    case OWL_VARIABLE_BOOL:
503      if (!cur->validate_fn) 
504        cur->validate_fn = owl_variable_bool_validate_default;
505      if (!cur->set_fn) 
506        cur->set_fn = owl_variable_bool_set_default;
507      if (!cur->set_fromstring_fn) 
508        cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
509      if (!cur->get_fn) 
510        cur->get_fn = owl_variable_get_default;
511      if (!cur->get_tostring_fn) 
512        cur->get_tostring_fn = owl_variable_bool_get_tostring_default;     
513      if (!cur->delete_fn)
514        cur->delete_fn = owl_variable_delete_default;
515      cur->val = owl_malloc(sizeof(int));
516      cur->set_fn(cur, &cur->ival_default);
517      break;
518    case OWL_VARIABLE_INT:
519      if (!cur->validate_fn) 
520        cur->validate_fn = owl_variable_int_validate_default;
521      if (!cur->set_fn) 
522        cur->set_fn = owl_variable_int_set_default;
523      if (!cur->set_fromstring_fn) 
524        cur->set_fromstring_fn = owl_variable_int_set_fromstring_default;
525      if (!cur->get_fn) 
526        cur->get_fn = owl_variable_get_default;
527      if (!cur->get_tostring_fn) 
528        cur->get_tostring_fn = owl_variable_int_get_tostring_default;     
529      if (!cur->delete_fn)
530        cur->delete_fn = owl_variable_delete_default;
531      cur->val = owl_malloc(sizeof(int));
532      cur->set_fn(cur, &cur->ival_default);
533      break;
534    default:
535      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
536      return(-2);
537    }
538    owl_dict_insert_element(vd, cur->name, cur, NULL);
539  }
540  return 0;
541}
542
543void owl_variable_dict_add_variable(owl_vardict * vardict,
544                                    owl_variable * var) {
545  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
546}
547
548owl_variable * owl_variable_newvar(const char *name, const char *summary, const char * description) {
549  owl_variable * var = owl_malloc(sizeof(owl_variable));
550  memset(var, 0, sizeof(owl_variable));
551  var->name = owl_strdup(name);
552  var->summary = owl_strdup(summary);
553  var->description = owl_strdup(description);
554  return var;
555}
556
557void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
558  if(var->summary) owl_free(var->summary);
559  var->summary = owl_strdup(summary);
560  if(var->description) owl_free(var->description);
561  var->description = owl_strdup(desc);
562}
563
564void owl_variable_dict_newvar_string(owl_vardict * vd, const char *name, const char *summ, const char * desc, const char * initval) {
565  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_STRING);
566  if(old) {
567    owl_variable_update(old, summ, desc);
568    if(old->pval_default) owl_free(old->pval_default);
569    old->pval_default = owl_strdup(initval);
570  } else {
571    owl_variable * var = owl_variable_newvar(name, summ, desc);
572    var->type = OWL_VARIABLE_STRING;
573    var->pval_default = owl_strdup(initval);
574    var->set_fn = owl_variable_string_set_default;
575    var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
576    var->get_fn = owl_variable_get_default;
577    var->get_tostring_fn = owl_variable_string_get_tostring_default;
578    var->delete_fn = owl_variable_delete_default;
579    var->set_fn(var, initval);
580    owl_variable_dict_add_variable(vd, var);
581  }
582}
583
584void owl_variable_dict_newvar_int(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
585  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_INT);
586  if(old) {
587    owl_variable_update(old, summ, desc);
588    old->ival_default = initval;
589  } else {
590    owl_variable * var = owl_variable_newvar(name, summ, desc);
591    var->type = OWL_VARIABLE_INT;
592    var->ival_default = initval;
593    var->validate_fn = owl_variable_int_validate_default;
594    var->set_fn = owl_variable_int_set_default;
595    var->set_fromstring_fn = owl_variable_int_set_fromstring_default;
596    var->get_fn = owl_variable_get_default;
597    var->get_tostring_fn = owl_variable_int_get_tostring_default;
598    var->delete_fn = owl_variable_delete_default;
599    var->val = owl_malloc(sizeof(int));
600    var->set_fn(var, &initval);
601    owl_variable_dict_add_variable(vd, var);
602  }
603}
604
605void owl_variable_dict_newvar_bool(owl_vardict * vd, const char *name, const char *summ, const char * desc, int initval) {
606  owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_BOOL);
607  if(old) {
608    owl_variable_update(old, summ, desc);
609    old->ival_default = initval;
610  } else {
611    owl_variable * var = owl_variable_newvar(name, summ, desc);
612    var->type = OWL_VARIABLE_BOOL;
613    var->ival_default = initval;
614    var->validate_fn = owl_variable_bool_validate_default;
615    var->set_fn = owl_variable_bool_set_default;
616    var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
617    var->get_fn = owl_variable_get_default;
618    var->get_tostring_fn = owl_variable_bool_get_tostring_default;
619    var->delete_fn = owl_variable_delete_default;
620    var->val = owl_malloc(sizeof(int));
621    var->set_fn(var, &initval);
622    owl_variable_dict_add_variable(vd, var);
623  }
624}
625
626void owl_variable_dict_cleanup(owl_vardict *d)
627{
628  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
629}
630
631/* free the list with owl_variable_dict_namelist_cleanup */
632void owl_variable_dict_get_names(const owl_vardict *d, owl_list *l) {
633  owl_dict_get_keys(d, l);
634}
635
636void owl_variable_dict_namelist_cleanup(owl_list *l)
637{
638  owl_list_cleanup(l, owl_free);
639}
640
641void owl_variable_delete(owl_variable *v)
642{
643  if (v->delete_fn) v->delete_fn(v);
644  owl_free(v);
645}
646
647
648const char *owl_variable_get_description(const owl_variable *v) {
649  return v->description;
650}
651
652const char *owl_variable_get_summary(const owl_variable *v) {
653  return v->summary;
654}
655
656const char *owl_variable_get_validsettings(const owl_variable *v) {
657  if (v->validsettings) {
658    return v->validsettings;
659  } else {
660    return "";
661  }
662}
663
664/* functions for getting and setting variable values */
665
666/* returns 0 on success, prints a status msg if msg is true */
667int owl_variable_set_fromstring(owl_vardict *d, const char *name, const char *value, int msg, int requirebool) {
668  owl_variable *v;
669  char buff2[1024];
670  if (!name) return(-1);
671  v = owl_dict_find_element(d, name);
672  if (v == NULL) {
673    if (msg) owl_function_error("Unknown variable %s", name);
674    return -1;
675  }
676  if (!v->set_fromstring_fn) {
677    if (msg) owl_function_error("Variable %s is read-only", name);
678    return -1;   
679  }
680  if (requirebool && v->type!=OWL_VARIABLE_BOOL) {
681    if (msg) owl_function_error("Variable %s is not a boolean", name);
682    return -1;   
683  }
684  if (0 != v->set_fromstring_fn(v, value)) {
685    if (msg) owl_function_error("Unable to set %s (must be %s)", name, 
686                                  owl_variable_get_validsettings(v));
687    return -1;
688  }
689  if (msg && v->get_tostring_fn) {
690    v->get_tostring_fn(v, buff2, 1024, v->val);
691    owl_function_makemsg("%s = '%s'", name, buff2);
692  }   
693  return 0;
694}
695 
696int owl_variable_set_string(owl_vardict *d, const char *name, const char *newval) {
697  owl_variable *v;
698  if (!name) return(-1);
699  v = owl_dict_find_element(d, name);
700  if (v == NULL || !v->set_fn) return(-1);
701  if (v->type!=OWL_VARIABLE_STRING) return(-1);
702  return v->set_fn(v, newval);
703}
704 
705int owl_variable_set_int(owl_vardict *d, const char *name, int newval) {
706  owl_variable *v;
707  if (!name) return(-1);
708  v = owl_dict_find_element(d, name);
709  if (v == NULL || !v->set_fn) return(-1);
710  if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1);
711  return v->set_fn(v, &newval);
712}
713 
714int owl_variable_set_bool_on(owl_vardict *d, const char *name) {
715  return owl_variable_set_int(d,name,1);
716}
717
718int owl_variable_set_bool_off(owl_vardict *d, const char *name) {
719  return owl_variable_set_int(d,name,0);
720}
721
722int owl_variable_get_tostring(const owl_vardict *d, const char *name, char *buf, int bufsize) {
723  owl_variable *v;
724  if (!name) return(-1);
725  v = owl_dict_find_element(d, name);
726  if (v == NULL || !v->get_tostring_fn) return(-1);
727  return v->get_tostring_fn(v, buf, bufsize, v->val);
728}
729
730int owl_variable_get_default_tostring(const owl_vardict *d, const char *name, char *buf, int bufsize) {
731  owl_variable *v;
732  if (!name) return(-1);
733  v = owl_dict_find_element(d, name);
734  if (v == NULL || !v->get_tostring_fn) return(-1);
735  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
736    return v->get_tostring_fn(v, buf, bufsize, &(v->ival_default));
737  } else {
738    return v->get_tostring_fn(v, buf, bufsize, v->pval_default);
739  }
740}
741
742owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name, int require_type) {
743  owl_variable *v;
744  if (!name) return(NULL);
745  v = owl_dict_find_element(d, name);
746  if (v == NULL || !v->get_fn || v->type != require_type) return(NULL);
747  return v;
748}
749
750/* returns a reference */
751const void *owl_variable_get(const owl_vardict *d, const char *name, int require_type) {
752  owl_variable *v = owl_variable_get_var(d, name, require_type);
753  if(v == NULL) return NULL;
754  return v->get_fn(v);
755}
756
757/* returns a reference */
758const char *owl_variable_get_string(const owl_vardict *d, const char *name) {
759  return owl_variable_get(d,name, OWL_VARIABLE_STRING);
760}
761
762/* returns a reference */
763const void *owl_variable_get_other(const owl_vardict *d, const char *name) {
764  return owl_variable_get(d,name, OWL_VARIABLE_OTHER);
765}
766
767int owl_variable_get_int(const owl_vardict *d, const char *name) {
768  const int *pi;
769  pi = owl_variable_get(d,name,OWL_VARIABLE_INT);
770  if (!pi) return(-1);
771  return(*pi);
772}
773
774int owl_variable_get_bool(const owl_vardict *d, const char *name) {
775  const int *pi;
776  pi = owl_variable_get(d,name,OWL_VARIABLE_BOOL);
777  if (!pi) return(-1);
778  return(*pi);
779}
780
781void owl_variable_describe(const owl_vardict *d, const char *name, owl_fmtext *fm) {
782  char defaultbuf[50];
783  char buf[1024];
784  int buflen = 1023;
785  owl_variable *v;
786
787  if (!name
788      || (v = owl_dict_find_element(d, name)) == NULL 
789      || !v->get_fn) {
790    snprintf(buf, buflen, "     No such variable '%s'\n", name);     
791    owl_fmtext_append_normal(fm, buf);
792    return;
793  }
794  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
795    v->get_tostring_fn(v, defaultbuf, 50, &(v->ival_default));
796  } else {
797    v->get_tostring_fn(v, defaultbuf, 50, v->pval_default);
798  }
799  snprintf(buf, buflen, OWL_TABSTR "%-20s - %s (default: '%s')\n", 
800                  v->name, 
801                  owl_variable_get_summary(v), defaultbuf);
802  owl_fmtext_append_normal(fm, buf);
803}
804
805void owl_variable_get_help(const owl_vardict *d, const char *name, owl_fmtext *fm) {
806  char buff[1024];
807  int bufflen = 1023;
808  owl_variable *v;
809
810  if (!name
811      || (v = owl_dict_find_element(d, name)) == NULL 
812      || !v->get_fn) {
813    owl_fmtext_append_normal(fm, "No such variable...\n");
814    return;
815  }
816
817  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
818  owl_fmtext_append_normal(fm, OWL_TABSTR);
819  owl_fmtext_append_normal(fm, name);
820  owl_fmtext_append_normal(fm, " - ");
821  owl_fmtext_append_normal(fm, v->summary);
822  owl_fmtext_append_normal(fm, "\n\n");
823
824  owl_fmtext_append_normal(fm, "Current:        ");
825  owl_variable_get_tostring(d, name, buff, bufflen);
826  owl_fmtext_append_normal(fm, buff);
827  owl_fmtext_append_normal(fm, "\n\n");
828
829
830  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
831    v->get_tostring_fn(v, buff, bufflen, &(v->ival_default));
832  } else {
833    v->get_tostring_fn(v, buff, bufflen, v->pval_default);
834  }
835  owl_fmtext_append_normal(fm, "Default:        ");
836  owl_fmtext_append_normal(fm, buff);
837  owl_fmtext_append_normal(fm, "\n\n");
838
839  owl_fmtext_append_normal(fm, "Valid Settings: ");
840  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
841  owl_fmtext_append_normal(fm, "\n\n");
842
843  if (v->description && *v->description) {
844    owl_fmtext_append_normal(fm, "Description:\n");
845    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
846    owl_fmtext_append_normal(fm, "\n\n");
847  }
848}
849
850
851
852
853/**************************************************************************/
854/*********************** GENERAL TYPE-SPECIFIC ****************************/
855/**************************************************************************/
856
857/* default common functions */
858
859const void *owl_variable_get_default(const owl_variable *v) {
860  return v->val;
861}
862
863void owl_variable_delete_default(owl_variable *v)
864{
865  if (v->val) owl_free(v->val);
866}
867
868/* default functions for booleans */
869
870int owl_variable_bool_validate_default(const owl_variable *v, const void *newval) {
871  if (newval == NULL) return(0);
872  else if (*(const int*)newval==1 || *(const int*)newval==0) return(1);
873  else return (0);
874}
875
876int owl_variable_bool_set_default(owl_variable *v, const void *newval) {
877  if (v->validate_fn) {
878    if (!v->validate_fn(v, newval)) return(-1);
879  }
880  *(int*)v->val = *(const int*)newval;
881  return(0);
882}
883
884int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval) {
885  int i;
886  if (!strcmp(newval, "on")) i=1;
887  else if (!strcmp(newval, "off")) i=0;
888  else return(-1);
889  return (v->set_fn(v, &i));
890}
891
892int owl_variable_bool_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
893  if (val == NULL) {
894    snprintf(buf, bufsize, "<null>");
895    return -1;
896  } else if (*(const int*)val == 0) {
897    snprintf(buf, bufsize, "off");
898    return 0;
899  } else if (*(const int*)val == 1) {
900    snprintf(buf, bufsize, "on");
901    return 0;
902  } else {
903    snprintf(buf, bufsize, "<invalid>");
904    return -1;
905  }
906}
907
908/* default functions for integers */
909
910int owl_variable_int_validate_default(const owl_variable *v, const void *newval) {
911  if (newval == NULL) return(0);
912  else return (1);
913}
914
915int owl_variable_int_set_default(owl_variable *v, const void *newval) {
916  if (v->validate_fn) {
917    if (!v->validate_fn(v, newval)) return(-1);
918  }
919  *(int*)v->val = *(const int*)newval;
920  return(0);
921}
922
923int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval) {
924  int i;
925  char *ep;
926  i = strtol(newval, &ep, 10);
927  if (*ep || ep==newval) return(-1);
928  return (v->set_fn(v, &i));
929}
930
931int owl_variable_int_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
932  if (val == NULL) {
933    snprintf(buf, bufsize, "<null>");
934    return -1;
935  } else {
936    snprintf(buf, bufsize, "%d", *(const int*)val);
937    return 0;
938  } 
939}
940
941/* default functions for enums (a variant of integers) */
942
943int owl_variable_enum_validate(const owl_variable *v, const void *newval) { 
944  char **enums;
945  int nenums, val;
946  if (newval == NULL) return(0);
947  enums = atokenize(v->validsettings, ",", &nenums);
948  if (enums == NULL) return(0);
949  atokenize_delete(enums, nenums);
950  val = *(const int*)newval;
951  if (val < 0 || val >= nenums) {
952    return(0);
953  }
954  return(1);
955}
956
957int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval) {
958  char **enums;
959  int nenums, i, val=-1;
960  if (newval == NULL) return(-1);
961  enums = atokenize(v->validsettings, ",", &nenums);
962  if (enums == NULL) return(-1);
963  for (i=0; i<nenums; i++) {
964    if (0==strcmp(newval, enums[i])) {
965      val = i;
966    }
967  }
968  atokenize_delete(enums, nenums);
969  if (val == -1) return(-1);
970  return (v->set_fn(v, &val));
971}
972
973int owl_variable_enum_get_tostring(const owl_variable *v, char* buf, int bufsize, const void *val) {
974  char **enums;
975  int nenums, i;
976
977  if (val == NULL) {
978    snprintf(buf, bufsize, "<null>");
979    return -1;
980  }
981  enums = atokenize(v->validsettings, ",", &nenums);
982  i = *(const int*)val;
983  if (i<0 || i>=nenums) {
984    snprintf(buf, bufsize, "<invalid:%d>",i);
985    atokenize_delete(enums, nenums);
986    return(-1);
987  }
988  snprintf(buf, bufsize, "%s", enums[i]);
989  atokenize_delete(enums, nenums);
990  return 0;
991}
992
993/* default functions for stringeans */
994
995int owl_variable_string_validate_default(const struct _owl_variable *v, const void *newval) {
996  if (newval == NULL) return(0);
997  else return (1);
998}
999
1000int owl_variable_string_set_default(owl_variable *v, const void *newval) {
1001  if (v->validate_fn) {
1002    if (!v->validate_fn(v, newval)) return(-1);
1003  }
1004  if (v->val) owl_free(v->val);
1005  v->val = owl_strdup(newval);
1006  return(0);
1007}
1008
1009int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval) {
1010  return (v->set_fn(v, newval));
1011}
1012
1013int owl_variable_string_get_tostring_default(const owl_variable *v, char* buf, int bufsize, const void *val) {
1014  if (val == NULL) {
1015    snprintf(buf, bufsize, "<null>");
1016    return -1;
1017  } else {
1018    snprintf(buf, bufsize, "%s", (const char*)val);
1019    return 0;
1020  }
1021}
1022
Note: See TracBrowser for help on using the repository browser.