source: variable.c @ 7d4fbcd

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