source: variable.c @ 8190ce8

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