source: variable.c @ 1aee7d9

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 1aee7d9 was 1aee7d9, checked in by Erik Nygren <nygren@mit.edu>, 22 years ago
* Added RCS Id strings to all files. * 'show keymaps' shows details of all keymaps after summary list.
  • Property mode set to 100644
File size: 23.5 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
8static const char fileIdent[] = "$Id$";
9
10#define OWLVAR_BOOL(name,default,docstring) \
11        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", docstring, NULL, \
12        NULL, NULL, NULL, NULL, NULL }
13
14#define OWLVAR_BOOL_FULL(name,default,docstring,validate,set,get) \
15        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", docstring, NULL, \
16        validate, set, NULL, get, NULL }
17
18#define OWLVAR_INT(name,default,docstring) \
19        { name, OWL_VARIABLE_INT, NULL, default, "<int>", docstring, NULL, \
20        NULL, NULL, NULL, NULL, NULL, NULL }
21
22#define OWLVAR_INT_FULL(name,default,docstring,validset,validate,set,get) \
23        { name, OWL_VARIABLE_INT, NULL, default, validset, docstring, NULL, \
24        validate, set, NULL, get, NULL, NULL }
25
26#define OWLVAR_PATH(name,default,docstring) \
27        { name, OWL_VARIABLE_STRING, default, 0, "<path>", docstring,  NULL, \
28        NULL, NULL, NULL, NULL, NULL, NULL }
29
30#define OWLVAR_STRING(name,default,docstring) \
31        { name, OWL_VARIABLE_STRING, default, 0, "<string>", docstring, NULL, \
32        NULL, NULL, NULL, NULL, NULL, NULL }
33
34/* enums are really integers, but where validset is a comma-separated
35 * list of strings which can be specified.  The tokens, starting at 0,
36 * correspond to the values that may be specified. */
37#define OWLVAR_ENUM(name,default,docstring,validset) \
38        { name, OWL_VARIABLE_INT, NULL, default, validset, docstring, NULL, \
39        owl_variable_enum_validate, \
40        NULL, owl_variable_enum_set_fromstring, \
41        NULL, owl_variable_enum_get_tostring, \
42        NULL }
43
44static owl_variable variables_to_init[] = {
45
46  OWLVAR_BOOL( "personalbell" /* %OwlVarStub */, 0,
47               "ring the terminal bell when personal messages are received" ),
48
49  OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1,
50               "enable / disable the terminal bell" ),
51
52  OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG,
53                    "whether debugging is enabled",
54                    NULL, owl_variable_debug_set, NULL),
55
56  OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1,
57               "send a login message when owl starts" ),
58
59  OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1,
60               "send a logout message when owl exits" ),
61
62  OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0,
63               "display received pings" ),
64
65  OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1,
66               "send pings" ),
67
68  OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1,
69               "display outgoing messages" ),
70
71  OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1,
72               "load logins from .anyone on startup" ),
73
74  OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0,
75               "turn personal logging on or off" ),
76
77  OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0,
78               "turn class logging on or off" ),
79
80  OWLVAR_BOOL_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 0,
81                    "don't send zephyrs on C-d",
82                    NULL, owl_variable_disable_ctrl_d_set, NULL),
83
84  OWLVAR_BOOL( "_burningears" /* %OwlVarStub:burningears */, 0,
85               "[NOT YET IMPLEMENTED] beep on messages matching patterns" ),
86
87  OWLVAR_BOOL( "_summarymode" /* %OwlVarStub:summarymode */, 0,
88               "[NOT YET IMPLEMENTED]" ),
89
90  OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people",
91               "path for logging personal zephyrs" ),
92
93  OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class",
94               "path for logging class zephyrs" ),
95
96  OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE,
97               "path for logging debug messages when debugging is enabled" ),
98 
99  OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsig_exec */, NULL,
100               "name of a program to run that will generate zsigs" ),
101
102  OWLVAR_STRING( "zsig" /* %OwlVarStub */, "",
103                 "zephyr signature" ),
104
105  OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "",
106                 "string to append to the end of the sepbar" ),
107
108  OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0,
109               "turn zaway on or off" ),
110
111  OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 
112                 OWL_DEFAULT_ZAWAYMSG,
113                 "zaway msg for responding to zephyrs when away" ),
114
115  OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 
116                 OWL_DEFAULT_ZAWAYMSG,
117                 "default zaway message" ),
118
119  OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all",
120                 "home view to switch to after 'X'" ),
121
122  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
123                   OWL_TYPWIN_SIZE,
124                  "number of lines in the typing window", "int > 0",
125                   owl_variable_int_validate_gt0,
126                   owl_variable_typewinsize_set,
127                   NULL /* use default for get */
128                   ),
129
130  OWLVAR_ENUM( "webbrowser" /* %OwlVarStub */, OWL_WEBBROWSER_NETSCAPE,
131               "web browser to use to launch URLs",
132               "none,netscape,galeon" ),
133
134  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
135               "enable automatic following of the last zephyr" ),
136
137  /* This MUST be last... */
138  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
139
140};
141
142/**************************************************************************/
143/*********************** SPECIFIC TO VARIABLES ****************************/
144/**************************************************************************/
145
146
147/* commonly useful */
148
149int owl_variable_int_validate_gt0(owl_variable *v, void *newval) {
150  if (newval == NULL) return(0);
151  else if (*(int*)newval < 1) return(0);
152  else return (1);
153}
154
155int owl_variable_int_validate_positive(owl_variable *v, void *newval) {
156  if (newval == NULL) return(0);
157  else if (*(int*)newval < 0) return(0);
158  else return (1);
159}
160
161/* typewinsize */
162
163int owl_variable_typewinsize_set(owl_variable *v, void *newval) {
164  int rv;
165  rv = owl_variable_int_set_default(v, newval);
166  if (0 == rv) owl_function_resize();
167  return(rv);
168}
169
170/* debug (cache value in g->debug) */
171
172int owl_variable_debug_set(owl_variable *v, void *newval) {
173  if (newval && (*(int*)newval == 1 || *(int*)newval == 0)) {
174    g.debug = *(int*)newval;
175  }
176  return owl_variable_bool_set_default(v, newval);
177}
178
179/* note that changing the value of this will clobber
180 * any user setting of this */
181int owl_variable_disable_ctrl_d_set(owl_variable *v, void *newval) {
182  if (newval && !owl_context_is_startup(owl_global_get_context(&g))
183      && (*(int*)newval == 1 || *(int*)newval == 0)) {
184    if (*(int*)newval) {
185      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
186    } else {
187      owl_function_command_norv("bindkey editmulti C-d command editmulti:done");
188    }
189  } 
190  return owl_variable_bool_set_default(v, newval); 
191}
192
193
194/**************************************************************************/
195/****************************** GENERAL ***********************************/
196/**************************************************************************/
197
198int owl_variable_dict_setup(owl_vardict *vd) {
199  owl_variable *cur;
200  if (owl_dict_create(vd)) return(-1);
201  for (cur = variables_to_init; cur->name != NULL; cur++) {
202    switch (cur->type) {
203    case OWL_VARIABLE_OTHER:
204      cur->set_fn(cur, cur->pval_default);
205      break;
206    case OWL_VARIABLE_STRING:
207      if (!cur->validate_fn) 
208        cur->validate_fn = owl_variable_string_validate_default;
209      if (!cur->set_fn) 
210        cur->set_fn = owl_variable_string_set_default;
211      if (!cur->set_fromstring_fn) 
212        cur->set_fromstring_fn = owl_variable_string_set_fromstring_default;
213      if (!cur->get_fn) 
214        cur->get_fn = owl_variable_get_default;
215      if (!cur->get_tostring_fn) 
216        cur->get_tostring_fn = owl_variable_string_get_tostring_default;     
217      if (!cur->free_fn) 
218        cur->free_fn = owl_variable_free_default;
219      cur->set_fn(cur, cur->pval_default);
220      break;
221    case OWL_VARIABLE_BOOL:
222      if (!cur->validate_fn) 
223        cur->validate_fn = owl_variable_bool_validate_default;
224      if (!cur->set_fn) 
225        cur->set_fn = owl_variable_bool_set_default;
226      if (!cur->set_fromstring_fn) 
227        cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
228      if (!cur->get_fn) 
229        cur->get_fn = owl_variable_get_default;
230      if (!cur->get_tostring_fn) 
231        cur->get_tostring_fn = owl_variable_bool_get_tostring_default;     
232      if (!cur->free_fn) 
233        cur->free_fn = owl_variable_free_default;
234      cur->val = owl_malloc(sizeof(int));
235      cur->set_fn(cur, &cur->ival_default);
236      break;
237    case OWL_VARIABLE_INT:
238      if (!cur->validate_fn) 
239        cur->validate_fn = owl_variable_int_validate_default;
240      if (!cur->set_fn) 
241        cur->set_fn = owl_variable_int_set_default;
242      if (!cur->set_fromstring_fn) 
243        cur->set_fromstring_fn = owl_variable_int_set_fromstring_default;
244      if (!cur->get_fn) 
245        cur->get_fn = owl_variable_get_default;
246      if (!cur->get_tostring_fn) 
247        cur->get_tostring_fn = owl_variable_int_get_tostring_default;     
248      if (!cur->free_fn) 
249        cur->free_fn = owl_variable_free_default;
250      cur->val = owl_malloc(sizeof(int));
251      cur->set_fn(cur, &cur->ival_default);
252      break;
253    default:
254      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
255      return(-2);
256    }
257    owl_dict_insert_element(vd, cur->name, (void*)cur, NULL);
258  }
259  return 0;
260}
261
262void owl_variable_dict_free(owl_vardict *d) {
263  owl_dict_free_all(d, (void(*)(void*))owl_variable_free);
264}
265
266/* free the list with owl_variable_dict_namelist_free */
267void owl_variable_dict_get_names(owl_vardict *d, owl_list *l) {
268  owl_dict_get_keys(d, l);
269}
270
271void owl_variable_dict_namelist_free(owl_list *l) {
272  owl_list_free_all(l, owl_free);
273}
274
275void owl_variable_free(owl_variable *v) {
276  if (v->free_fn) v->free_fn(v);
277}
278
279
280char *owl_variable_get_docstring(owl_variable *v) {
281  return v->docstring;
282}
283
284char *owl_variable_get_validsettings(owl_variable *v) {
285  if (v->validsettings) {
286    return v->validsettings;
287  } else {
288    return "";
289  }
290}
291
292/* functions for getting and setting variable values */
293
294/* returns 0 on success, prints a status msg if msg is true */
295int owl_variable_set_fromstring(owl_vardict *d, char *name, char *value, int msg) {
296  owl_variable *v;
297  char buff2[1024];
298  if (!name) return(-1);
299  v = owl_dict_find_element(d, name);
300  if (v == NULL) {
301    if (msg) owl_function_makemsg("Unknown variable %s", name);
302    return -1;
303  }
304  if (!v->set_fromstring_fn) {
305    if (msg) owl_function_makemsg("Variable %s is read-only", name);
306    return -1;
307   
308  }
309  if (0 != v->set_fromstring_fn(v, value)) {
310    if (msg) owl_function_makemsg("Unable to set %s (must be %s)", name, 
311                                  owl_variable_get_validsettings(v));
312    return -1;
313  }
314  if (msg && v->get_tostring_fn) {
315    v->get_tostring_fn(v, buff2, 1024, v->val);
316    owl_function_makemsg("%s = '%s'", name, buff2);
317  }   
318  return 0;
319}
320 
321int owl_variable_set_string(owl_vardict *d, char *name, char *newval) {
322  owl_variable *v;
323  if (!name) return(-1);
324  v = owl_dict_find_element(d, name);
325  if (v == NULL || !v->set_fn) return(-1);
326  if (v->type!=OWL_VARIABLE_STRING) return(-1);
327  return v->set_fn(v, (void*)newval);
328}
329 
330int owl_variable_set_int(owl_vardict *d, char *name, int newval) {
331  owl_variable *v;
332  if (!name) return(-1);
333  v = owl_dict_find_element(d, name);
334  if (v == NULL || !v->set_fn) return(-1);
335  if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1);
336  return v->set_fn(v, &newval);
337}
338 
339int owl_variable_set_bool_on(owl_vardict *d, char *name) {
340  return owl_variable_set_int(d,name,1);
341}
342
343int owl_variable_set_bool_off(owl_vardict *d, char *name) {
344  return owl_variable_set_int(d,name,0);
345}
346
347int owl_variable_get_tostring(owl_vardict *d, char *name, char *buf, int bufsize) {
348  owl_variable *v;
349  if (!name) return(-1);
350  v = owl_dict_find_element(d, name);
351  if (v == NULL || !v->get_tostring_fn) return(-1);
352  return v->get_tostring_fn(v, buf, bufsize, v->val);
353}
354
355int owl_variable_get_default_tostring(owl_vardict *d, char *name, char *buf, int bufsize) {
356  owl_variable *v;
357  if (!name) return(-1);
358  v = owl_dict_find_element(d, name);
359  if (v == NULL || !v->get_tostring_fn) return(-1);
360  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
361    return v->get_tostring_fn(v, buf, bufsize, &(v->ival_default));
362  } else {
363    return v->get_tostring_fn(v, buf, bufsize, v->pval_default);
364  }
365}
366
367/* returns a reference */
368void *owl_variable_get(owl_vardict *d, char *name, int require_type) {
369  owl_variable *v;
370  if (!name) return(NULL);
371  v = owl_dict_find_element(d, name);
372  if (v == NULL || !v->get_fn || v->type != require_type) return(NULL);
373  return v->get_fn(v);
374}
375
376/* returns a reference */
377char *owl_variable_get_string(owl_vardict *d, char *name) {
378  return (char*)owl_variable_get(d,name, OWL_VARIABLE_STRING);
379}
380
381/* returns a reference */
382void *owl_variable_get_other(owl_vardict *d, char *name) {
383  return (char*)owl_variable_get(d,name, OWL_VARIABLE_OTHER);
384}
385
386int owl_variable_get_int(owl_vardict *d, char *name) {
387  int *pi;
388  pi = (int*)owl_variable_get(d,name,OWL_VARIABLE_INT);
389  if (!pi) return(-1);
390  return(*pi);
391}
392
393int owl_variable_get_bool(owl_vardict *d, char *name) {
394  int *pi;
395  pi = (int*)owl_variable_get(d,name,OWL_VARIABLE_BOOL);
396  if (!pi) return(-1);
397  return(*pi);
398}
399
400#define OWL_VARIABLE_HELP_FMT "     %-15s %-9s %-9s %s\n"
401
402/* appends to the end of the fmtext */
403void owl_variable_get_summaryheader(owl_fmtext *fm) {
404  char tmpbuff[512];
405  snprintf(tmpbuff, 512, 
406           OWL_VARIABLE_HELP_FMT OWL_VARIABLE_HELP_FMT,
407           "name",            "settings", "default", "meaning",
408           "---------------", "--------", "-------", "-------");
409  owl_fmtext_append_bold(fm, tmpbuff);
410}
411
412void owl_variable_get_summary(owl_vardict *d, char *name, owl_fmtext *fm) {
413  char defaultbuf[10];
414  char buf[1024];
415  int buflen = 1023;
416  owl_variable *v;
417
418  if (!name
419      || (v = owl_dict_find_element(d, name)) == NULL 
420      || !v->get_fn) {
421    snprintf(buf, buflen, "     No such variable '%s'\n", name);     
422    owl_fmtext_append_normal(fm, buf);
423    return;
424  }
425  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
426    v->get_tostring_fn(v, defaultbuf, 10, &(v->ival_default));
427  } else {
428    v->get_tostring_fn(v, defaultbuf, 10, v->pval_default);
429  }
430  snprintf(buf, buflen, OWL_VARIABLE_HELP_FMT, 
431                  v->name, 
432                  owl_variable_get_validsettings(v),
433                  defaultbuf,
434                  owl_variable_get_docstring(v));
435  owl_fmtext_append_normal(fm, buf);
436}
437
438void owl_variable_get_help(owl_vardict *d, char *name, owl_fmtext *fm) {
439  char buff[1024];
440  int bufflen = 1023;
441  owl_variable *v;
442
443  if (!name
444      || (v = owl_dict_find_element(d, name)) == NULL 
445      || !v->get_fn) {
446    owl_fmtext_append_normal(fm, "No such variable...\n");
447    return;
448  }
449
450  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
451  owl_fmtext_append_normal(fm, OWL_TABSTR);
452  owl_fmtext_append_normal(fm, name);
453  owl_fmtext_append_normal(fm, " - ");
454  owl_fmtext_append_normal(fm, v->docstring);
455  owl_fmtext_append_normal(fm, "\n\n");
456
457  owl_fmtext_append_normal(fm, "Current:        ");
458  owl_variable_get_tostring(d, name, buff, bufflen);
459  owl_fmtext_append_normal(fm, buff);
460  owl_fmtext_append_normal(fm, "\n\n");
461
462
463  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
464    v->get_tostring_fn(v, buff, bufflen, &(v->ival_default));
465  } else {
466    v->get_tostring_fn(v, buff, bufflen, v->pval_default);
467  }
468  owl_fmtext_append_normal(fm, "Default:        ");
469  owl_fmtext_append_normal(fm, buff);
470  owl_fmtext_append_normal(fm, "\n\n");
471
472  owl_fmtext_append_normal(fm, "Valid Settings: ");
473  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
474  owl_fmtext_append_normal(fm, "\n\n");
475}
476
477
478
479
480/**************************************************************************/
481/*********************** GENERAL TYPE-SPECIFIC ****************************/
482/**************************************************************************/
483
484/* default common functions */
485
486void *owl_variable_get_default(owl_variable *v) {
487  return v->val;
488}
489
490void owl_variable_free_default(owl_variable *v) {
491  if (v->val) owl_free(v->val);
492}
493
494/* default functions for booleans */
495
496int owl_variable_bool_validate_default(owl_variable *v, void *newval) {
497  if (newval == NULL) return(0);
498  else if (*(int*)newval==1 || *(int*)newval==0) return(1);
499  else return (0);
500}
501
502int owl_variable_bool_set_default(owl_variable *v, void *newval) {
503  if (v->validate_fn) {
504    if (!v->validate_fn(v, newval)) return(-1);
505  }
506  *(int*)v->val = *(int*)newval;
507  return(0);
508}
509
510int owl_variable_bool_set_fromstring_default(owl_variable *v, char *newval) {
511  int i;
512  if (!strcmp(newval, "on")) i=1;
513  else if (!strcmp(newval, "off")) i=0;
514  else return(-1);
515  return (v->set_fn(v, &i));
516}
517
518int owl_variable_bool_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
519  if (val == NULL) {
520    snprintf(buf, bufsize, "<null>");
521    return -1;
522  } else if (*(int*)val == 0) {
523    snprintf(buf, bufsize, "off");
524    return 0;
525  } else if (*(int*)val == 1) {
526    snprintf(buf, bufsize, "on");
527    return 0;
528  } else {
529    snprintf(buf, bufsize, "<invalid>");
530    return -1;
531  }
532}
533
534/* default functions for integers */
535
536int owl_variable_int_validate_default(owl_variable *v, void *newval) {
537  if (newval == NULL) return(0);
538  else return (1);
539}
540
541int owl_variable_int_set_default(owl_variable *v, void *newval) {
542  if (v->validate_fn) {
543    if (!v->validate_fn(v, newval)) return(-1);
544  }
545  *(int*)v->val = *(int*)newval;
546  return(0);
547}
548
549int owl_variable_int_set_fromstring_default(owl_variable *v, char *newval) {
550  int i;
551  char *ep = "x";
552  i = strtol(newval, &ep, 10);
553  if (*ep || ep==newval) return(-1);
554  return (v->set_fn(v, &i));
555}
556
557int owl_variable_int_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
558  if (val == NULL) {
559    snprintf(buf, bufsize, "<null>");
560    return -1;
561  } else {
562    snprintf(buf, bufsize, "%d", *(int*)val);
563    return 0;
564  } 
565}
566
567/* default functions for enums (a variant of integers) */
568
569int owl_variable_enum_validate(owl_variable *v, void *newval) { 
570  char **enums;
571  int nenums, val;
572  if (newval == NULL) return(0);
573  enums = atokenize(v->validsettings, ",", &nenums);
574  if (enums == NULL) return(0);
575  atokenize_free(enums, nenums);
576  val = *(int*)newval;
577  if (val < 0 || val >= nenums) {
578    return(0);
579  }
580  return(1);
581}
582
583int owl_variable_enum_set_fromstring(owl_variable *v, char *newval) {
584  char **enums;
585  int nenums, i, val=-1;
586  if (newval == NULL) return(-1);
587  enums = atokenize(v->validsettings, ",", &nenums);
588  if (enums == NULL) return(-1);
589  for (i=0; i<nenums; i++) {
590    if (0==strcmp(newval, enums[i])) {
591      val = i;
592    }
593  }
594  atokenize_free(enums, nenums);
595  if (val == -1) return(-1);
596  return (v->set_fn(v, &val));
597}
598
599int owl_variable_enum_get_tostring(owl_variable *v, char* buf, int bufsize, void *val) {
600  char **enums;
601  int nenums, i;
602
603  if (val == NULL) {
604    snprintf(buf, bufsize, "<null>");
605    return -1;
606  }
607  enums = atokenize(v->validsettings, ",", &nenums);
608  i = *(int*)val;
609  if (i<0 || i>=nenums) {
610    snprintf(buf, bufsize, "<invalid:%d>",i);
611    atokenize_free(enums, nenums);
612    return(-1);
613  }
614  snprintf(buf, bufsize, "%s", enums[i]);
615  return 0;
616}
617
618/* default functions for stringeans */
619
620int owl_variable_string_validate_default(struct _owl_variable *v, void *newval) {
621  if (newval == NULL) return(0);
622  else return (1);
623}
624
625int owl_variable_string_set_default(owl_variable *v, void *newval) {
626  if (v->validate_fn) {
627    if (!v->validate_fn(v, newval)) return(-1);
628  }
629  if (v->val) owl_free(v->val);
630  v->val = owl_strdup(newval);
631  return(0);
632}
633
634int owl_variable_string_set_fromstring_default(owl_variable *v, char *newval) {
635  return (v->set_fn(v, newval));
636}
637
638int owl_variable_string_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
639  if (val == NULL) {
640    snprintf(buf, bufsize, "<null>");
641    return -1;
642  } else {
643    snprintf(buf, bufsize, "%s", (char*)val);
644    return 0;
645  }
646}
647
648
649
650/**************************************************************************/
651/************************* REGRESSION TESTS *******************************/
652/**************************************************************************/
653
654#ifdef OWL_INCLUDE_REG_TESTS
655
656#define FAIL_UNLESS(desc,pred) printf("\t%-4s: %s\n", (pred)?"ok":(numfailed++,"FAIL"), desc)
657
658int owl_variable_regtest(void) {
659  owl_vardict vd;
660  int numfailed=0;
661  char buf[1024];
662
663  printf("BEGIN testing owl_variable\n");
664  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
665
666  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"personalbell"));
667  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
668  FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"personalbell", buf, 1024));
669  FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
670  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"personalbell"));
671  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"personalbell"));
672  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"personalbell","off",0));
673  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"personalbell"));
674  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"personalbell","xxx",0));
675  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"personalbell"));
676
677
678  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
679  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
680  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
681
682  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
683  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
684  FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
685  FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
686  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
687  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
688  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
689  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
690  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0));
691  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
692  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0));
693  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0));
694  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
695
696  FAIL_UNLESS("get enum", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser"));
697  FAIL_UNLESS("get enum as string 1", 0==owl_variable_get_tostring(&vd,"webbrowser", buf, 1024));
698  FAIL_UNLESS("get enum as string 2", 0==strcmp(buf,"netscape"));
699  FAIL_UNLESS("set enum 1", 0==owl_variable_set_int(&vd,"webbrowser",OWL_WEBBROWSER_GALEON));
700  FAIL_UNLESS("get enum 2", OWL_WEBBROWSER_GALEON==owl_variable_get_int(&vd,"webbrowser"));
701  FAIL_UNLESS("set enum 1b", -1==owl_variable_set_int(&vd,"webbrowser",-3));
702  FAIL_UNLESS("set enum 1b", -1==owl_variable_set_int(&vd,"webbrowser",209));
703  FAIL_UNLESS("get enum 2b", OWL_WEBBROWSER_GALEON==owl_variable_get_int(&vd,"webbrowser"));
704  FAIL_UNLESS("set enum 3", 0==owl_variable_set_fromstring(&vd,"webbrowser","none",0));
705  FAIL_UNLESS("get enum 4", OWL_WEBBROWSER_NONE==owl_variable_get_int(&vd,"webbrowser"));
706  FAIL_UNLESS("set enum 5", 0==owl_variable_set_fromstring(&vd,"webbrowser","netscape",0));
707  FAIL_UNLESS("get enum 6", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser"));
708  FAIL_UNLESS("set enum 7", -1==owl_variable_set_fromstring(&vd,"webbrowser","xxx",0));
709  FAIL_UNLESS("set enum 8", -1==owl_variable_set_fromstring(&vd,"webbrowser","",0));
710  FAIL_UNLESS("set enum 9", -1==owl_variable_set_fromstring(&vd,"webbrowser","netscapey",0));
711  FAIL_UNLESS("get enum 10", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser"));
712
713
714
715  owl_variable_dict_free(&vd);
716
717  if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n");
718  printf("END testing owl_variable (%d failures)\n", numfailed);
719  return(numfailed);
720}
721
722
723#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.