source: variable.c @ e778351

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