source: variable.c @ 6794f72

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 6794f72 was 039213e, checked in by Erik Nygren <nygren@mit.edu>, 22 years ago
Changed default of maxwrapcols to 0 and made docs more clear.
  • Property mode set to 100644
File size: 23.8 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(    "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70,
123                 "maximum number of columns for M-q to fill text to (or unlimited if 0)" ),
124
125  OWLVAR_INT(    "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 0,
126                 "maximum number of columns for line-wrapping (or unlimited if 0)" ),
127
128  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
129                   OWL_TYPWIN_SIZE,
130                  "number of lines in the typing window", "int > 0",
131                   owl_variable_int_validate_gt0,
132                   owl_variable_typewinsize_set,
133                   NULL /* use default for get */
134                   ),
135
136  OWLVAR_ENUM( "webbrowser" /* %OwlVarStub */, OWL_WEBBROWSER_NETSCAPE,
137               "web browser to use to launch URLs",
138               "none,netscape,galeon" ),
139
140  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
141               "enable automatic following of the last zephyr" ),
142
143  /* This MUST be last... */
144  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
145
146};
147
148/**************************************************************************/
149/*********************** SPECIFIC TO VARIABLES ****************************/
150/**************************************************************************/
151
152
153/* commonly useful */
154
155int owl_variable_int_validate_gt0(owl_variable *v, void *newval) {
156  if (newval == NULL) return(0);
157  else if (*(int*)newval < 1) return(0);
158  else return (1);
159}
160
161int owl_variable_int_validate_positive(owl_variable *v, void *newval) {
162  if (newval == NULL) return(0);
163  else if (*(int*)newval < 0) return(0);
164  else return (1);
165}
166
167/* typewinsize */
168
169int owl_variable_typewinsize_set(owl_variable *v, void *newval) {
170  int rv;
171  rv = owl_variable_int_set_default(v, newval);
172  if (0 == rv) owl_function_resize();
173  return(rv);
174}
175
176/* debug (cache value in g->debug) */
177
178int owl_variable_debug_set(owl_variable *v, void *newval) {
179  if (newval && (*(int*)newval == 1 || *(int*)newval == 0)) {
180    g.debug = *(int*)newval;
181  }
182  return owl_variable_bool_set_default(v, newval);
183}
184
185/* note that changing the value of this will clobber
186 * any user setting of this */
187int owl_variable_disable_ctrl_d_set(owl_variable *v, void *newval) {
188  if (newval && !owl_context_is_startup(owl_global_get_context(&g))
189      && (*(int*)newval == 1 || *(int*)newval == 0)) {
190    if (*(int*)newval) {
191      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
192    } else {
193      owl_function_command_norv("bindkey editmulti C-d command editmulti:done");
194    }
195  } 
196  return owl_variable_bool_set_default(v, newval); 
197}
198
199
200/**************************************************************************/
201/****************************** GENERAL ***********************************/
202/**************************************************************************/
203
204int owl_variable_dict_setup(owl_vardict *vd) {
205  owl_variable *cur;
206  if (owl_dict_create(vd)) return(-1);
207  for (cur = variables_to_init; cur->name != NULL; cur++) {
208    switch (cur->type) {
209    case OWL_VARIABLE_OTHER:
210      cur->set_fn(cur, cur->pval_default);
211      break;
212    case OWL_VARIABLE_STRING:
213      if (!cur->validate_fn) 
214        cur->validate_fn = owl_variable_string_validate_default;
215      if (!cur->set_fn) 
216        cur->set_fn = owl_variable_string_set_default;
217      if (!cur->set_fromstring_fn) 
218        cur->set_fromstring_fn = owl_variable_string_set_fromstring_default;
219      if (!cur->get_fn) 
220        cur->get_fn = owl_variable_get_default;
221      if (!cur->get_tostring_fn) 
222        cur->get_tostring_fn = owl_variable_string_get_tostring_default;     
223      if (!cur->free_fn) 
224        cur->free_fn = owl_variable_free_default;
225      cur->set_fn(cur, cur->pval_default);
226      break;
227    case OWL_VARIABLE_BOOL:
228      if (!cur->validate_fn) 
229        cur->validate_fn = owl_variable_bool_validate_default;
230      if (!cur->set_fn) 
231        cur->set_fn = owl_variable_bool_set_default;
232      if (!cur->set_fromstring_fn) 
233        cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
234      if (!cur->get_fn) 
235        cur->get_fn = owl_variable_get_default;
236      if (!cur->get_tostring_fn) 
237        cur->get_tostring_fn = owl_variable_bool_get_tostring_default;     
238      if (!cur->free_fn) 
239        cur->free_fn = owl_variable_free_default;
240      cur->val = owl_malloc(sizeof(int));
241      cur->set_fn(cur, &cur->ival_default);
242      break;
243    case OWL_VARIABLE_INT:
244      if (!cur->validate_fn) 
245        cur->validate_fn = owl_variable_int_validate_default;
246      if (!cur->set_fn) 
247        cur->set_fn = owl_variable_int_set_default;
248      if (!cur->set_fromstring_fn) 
249        cur->set_fromstring_fn = owl_variable_int_set_fromstring_default;
250      if (!cur->get_fn) 
251        cur->get_fn = owl_variable_get_default;
252      if (!cur->get_tostring_fn) 
253        cur->get_tostring_fn = owl_variable_int_get_tostring_default;     
254      if (!cur->free_fn) 
255        cur->free_fn = owl_variable_free_default;
256      cur->val = owl_malloc(sizeof(int));
257      cur->set_fn(cur, &cur->ival_default);
258      break;
259    default:
260      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
261      return(-2);
262    }
263    owl_dict_insert_element(vd, cur->name, (void*)cur, NULL);
264  }
265  return 0;
266}
267
268void owl_variable_dict_free(owl_vardict *d) {
269  owl_dict_free_all(d, (void(*)(void*))owl_variable_free);
270}
271
272/* free the list with owl_variable_dict_namelist_free */
273void owl_variable_dict_get_names(owl_vardict *d, owl_list *l) {
274  owl_dict_get_keys(d, l);
275}
276
277void owl_variable_dict_namelist_free(owl_list *l) {
278  owl_list_free_all(l, owl_free);
279}
280
281void owl_variable_free(owl_variable *v) {
282  if (v->free_fn) v->free_fn(v);
283}
284
285
286char *owl_variable_get_docstring(owl_variable *v) {
287  return v->docstring;
288}
289
290char *owl_variable_get_validsettings(owl_variable *v) {
291  if (v->validsettings) {
292    return v->validsettings;
293  } else {
294    return "";
295  }
296}
297
298/* functions for getting and setting variable values */
299
300/* returns 0 on success, prints a status msg if msg is true */
301int owl_variable_set_fromstring(owl_vardict *d, char *name, char *value, int msg) {
302  owl_variable *v;
303  char buff2[1024];
304  if (!name) return(-1);
305  v = owl_dict_find_element(d, name);
306  if (v == NULL) {
307    if (msg) owl_function_makemsg("Unknown variable %s", name);
308    return -1;
309  }
310  if (!v->set_fromstring_fn) {
311    if (msg) owl_function_makemsg("Variable %s is read-only", name);
312    return -1;
313   
314  }
315  if (0 != v->set_fromstring_fn(v, value)) {
316    if (msg) owl_function_makemsg("Unable to set %s (must be %s)", name, 
317                                  owl_variable_get_validsettings(v));
318    return -1;
319  }
320  if (msg && v->get_tostring_fn) {
321    v->get_tostring_fn(v, buff2, 1024, v->val);
322    owl_function_makemsg("%s = '%s'", name, buff2);
323  }   
324  return 0;
325}
326 
327int owl_variable_set_string(owl_vardict *d, char *name, char *newval) {
328  owl_variable *v;
329  if (!name) return(-1);
330  v = owl_dict_find_element(d, name);
331  if (v == NULL || !v->set_fn) return(-1);
332  if (v->type!=OWL_VARIABLE_STRING) return(-1);
333  return v->set_fn(v, (void*)newval);
334}
335 
336int owl_variable_set_int(owl_vardict *d, char *name, int newval) {
337  owl_variable *v;
338  if (!name) return(-1);
339  v = owl_dict_find_element(d, name);
340  if (v == NULL || !v->set_fn) return(-1);
341  if (v->type!=OWL_VARIABLE_INT && v->type!=OWL_VARIABLE_BOOL) return(-1);
342  return v->set_fn(v, &newval);
343}
344 
345int owl_variable_set_bool_on(owl_vardict *d, char *name) {
346  return owl_variable_set_int(d,name,1);
347}
348
349int owl_variable_set_bool_off(owl_vardict *d, char *name) {
350  return owl_variable_set_int(d,name,0);
351}
352
353int owl_variable_get_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  return v->get_tostring_fn(v, buf, bufsize, v->val);
359}
360
361int owl_variable_get_default_tostring(owl_vardict *d, char *name, char *buf, int bufsize) {
362  owl_variable *v;
363  if (!name) return(-1);
364  v = owl_dict_find_element(d, name);
365  if (v == NULL || !v->get_tostring_fn) return(-1);
366  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
367    return v->get_tostring_fn(v, buf, bufsize, &(v->ival_default));
368  } else {
369    return v->get_tostring_fn(v, buf, bufsize, v->pval_default);
370  }
371}
372
373/* returns a reference */
374void *owl_variable_get(owl_vardict *d, char *name, int require_type) {
375  owl_variable *v;
376  if (!name) return(NULL);
377  v = owl_dict_find_element(d, name);
378  if (v == NULL || !v->get_fn || v->type != require_type) return(NULL);
379  return v->get_fn(v);
380}
381
382/* returns a reference */
383char *owl_variable_get_string(owl_vardict *d, char *name) {
384  return (char*)owl_variable_get(d,name, OWL_VARIABLE_STRING);
385}
386
387/* returns a reference */
388void *owl_variable_get_other(owl_vardict *d, char *name) {
389  return (char*)owl_variable_get(d,name, OWL_VARIABLE_OTHER);
390}
391
392int owl_variable_get_int(owl_vardict *d, char *name) {
393  int *pi;
394  pi = (int*)owl_variable_get(d,name,OWL_VARIABLE_INT);
395  if (!pi) return(-1);
396  return(*pi);
397}
398
399int owl_variable_get_bool(owl_vardict *d, char *name) {
400  int *pi;
401  pi = (int*)owl_variable_get(d,name,OWL_VARIABLE_BOOL);
402  if (!pi) return(-1);
403  return(*pi);
404}
405
406#define OWL_VARIABLE_HELP_FMT "     %-15s %-9s %-9s %s\n"
407
408/* appends to the end of the fmtext */
409void owl_variable_get_summaryheader(owl_fmtext *fm) {
410  char tmpbuff[512];
411  snprintf(tmpbuff, 512, 
412           OWL_VARIABLE_HELP_FMT OWL_VARIABLE_HELP_FMT,
413           "name",            "settings", "default", "meaning",
414           "---------------", "--------", "-------", "-------");
415  owl_fmtext_append_bold(fm, tmpbuff);
416}
417
418void owl_variable_get_summary(owl_vardict *d, char *name, owl_fmtext *fm) {
419  char defaultbuf[10];
420  char buf[1024];
421  int buflen = 1023;
422  owl_variable *v;
423
424  if (!name
425      || (v = owl_dict_find_element(d, name)) == NULL 
426      || !v->get_fn) {
427    snprintf(buf, buflen, "     No such variable '%s'\n", name);     
428    owl_fmtext_append_normal(fm, buf);
429    return;
430  }
431  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
432    v->get_tostring_fn(v, defaultbuf, 10, &(v->ival_default));
433  } else {
434    v->get_tostring_fn(v, defaultbuf, 10, v->pval_default);
435  }
436  snprintf(buf, buflen, OWL_VARIABLE_HELP_FMT, 
437                  v->name, 
438                  owl_variable_get_validsettings(v),
439                  defaultbuf,
440                  owl_variable_get_docstring(v));
441  owl_fmtext_append_normal(fm, buf);
442}
443
444void owl_variable_get_help(owl_vardict *d, char *name, owl_fmtext *fm) {
445  char buff[1024];
446  int bufflen = 1023;
447  owl_variable *v;
448
449  if (!name
450      || (v = owl_dict_find_element(d, name)) == NULL 
451      || !v->get_fn) {
452    owl_fmtext_append_normal(fm, "No such variable...\n");
453    return;
454  }
455
456  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
457  owl_fmtext_append_normal(fm, OWL_TABSTR);
458  owl_fmtext_append_normal(fm, name);
459  owl_fmtext_append_normal(fm, " - ");
460  owl_fmtext_append_normal(fm, v->docstring);
461  owl_fmtext_append_normal(fm, "\n\n");
462
463  owl_fmtext_append_normal(fm, "Current:        ");
464  owl_variable_get_tostring(d, name, buff, bufflen);
465  owl_fmtext_append_normal(fm, buff);
466  owl_fmtext_append_normal(fm, "\n\n");
467
468
469  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
470    v->get_tostring_fn(v, buff, bufflen, &(v->ival_default));
471  } else {
472    v->get_tostring_fn(v, buff, bufflen, v->pval_default);
473  }
474  owl_fmtext_append_normal(fm, "Default:        ");
475  owl_fmtext_append_normal(fm, buff);
476  owl_fmtext_append_normal(fm, "\n\n");
477
478  owl_fmtext_append_normal(fm, "Valid Settings: ");
479  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
480  owl_fmtext_append_normal(fm, "\n\n");
481}
482
483
484
485
486/**************************************************************************/
487/*********************** GENERAL TYPE-SPECIFIC ****************************/
488/**************************************************************************/
489
490/* default common functions */
491
492void *owl_variable_get_default(owl_variable *v) {
493  return v->val;
494}
495
496void owl_variable_free_default(owl_variable *v) {
497  if (v->val) owl_free(v->val);
498}
499
500/* default functions for booleans */
501
502int owl_variable_bool_validate_default(owl_variable *v, void *newval) {
503  if (newval == NULL) return(0);
504  else if (*(int*)newval==1 || *(int*)newval==0) return(1);
505  else return (0);
506}
507
508int owl_variable_bool_set_default(owl_variable *v, void *newval) {
509  if (v->validate_fn) {
510    if (!v->validate_fn(v, newval)) return(-1);
511  }
512  *(int*)v->val = *(int*)newval;
513  return(0);
514}
515
516int owl_variable_bool_set_fromstring_default(owl_variable *v, char *newval) {
517  int i;
518  if (!strcmp(newval, "on")) i=1;
519  else if (!strcmp(newval, "off")) i=0;
520  else return(-1);
521  return (v->set_fn(v, &i));
522}
523
524int owl_variable_bool_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
525  if (val == NULL) {
526    snprintf(buf, bufsize, "<null>");
527    return -1;
528  } else if (*(int*)val == 0) {
529    snprintf(buf, bufsize, "off");
530    return 0;
531  } else if (*(int*)val == 1) {
532    snprintf(buf, bufsize, "on");
533    return 0;
534  } else {
535    snprintf(buf, bufsize, "<invalid>");
536    return -1;
537  }
538}
539
540/* default functions for integers */
541
542int owl_variable_int_validate_default(owl_variable *v, void *newval) {
543  if (newval == NULL) return(0);
544  else return (1);
545}
546
547int owl_variable_int_set_default(owl_variable *v, void *newval) {
548  if (v->validate_fn) {
549    if (!v->validate_fn(v, newval)) return(-1);
550  }
551  *(int*)v->val = *(int*)newval;
552  return(0);
553}
554
555int owl_variable_int_set_fromstring_default(owl_variable *v, char *newval) {
556  int i;
557  char *ep = "x";
558  i = strtol(newval, &ep, 10);
559  if (*ep || ep==newval) return(-1);
560  return (v->set_fn(v, &i));
561}
562
563int owl_variable_int_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
564  if (val == NULL) {
565    snprintf(buf, bufsize, "<null>");
566    return -1;
567  } else {
568    snprintf(buf, bufsize, "%d", *(int*)val);
569    return 0;
570  } 
571}
572
573/* default functions for enums (a variant of integers) */
574
575int owl_variable_enum_validate(owl_variable *v, void *newval) { 
576  char **enums;
577  int nenums, val;
578  if (newval == NULL) return(0);
579  enums = atokenize(v->validsettings, ",", &nenums);
580  if (enums == NULL) return(0);
581  atokenize_free(enums, nenums);
582  val = *(int*)newval;
583  if (val < 0 || val >= nenums) {
584    return(0);
585  }
586  return(1);
587}
588
589int owl_variable_enum_set_fromstring(owl_variable *v, char *newval) {
590  char **enums;
591  int nenums, i, val=-1;
592  if (newval == NULL) return(-1);
593  enums = atokenize(v->validsettings, ",", &nenums);
594  if (enums == NULL) return(-1);
595  for (i=0; i<nenums; i++) {
596    if (0==strcmp(newval, enums[i])) {
597      val = i;
598    }
599  }
600  atokenize_free(enums, nenums);
601  if (val == -1) return(-1);
602  return (v->set_fn(v, &val));
603}
604
605int owl_variable_enum_get_tostring(owl_variable *v, char* buf, int bufsize, void *val) {
606  char **enums;
607  int nenums, i;
608
609  if (val == NULL) {
610    snprintf(buf, bufsize, "<null>");
611    return -1;
612  }
613  enums = atokenize(v->validsettings, ",", &nenums);
614  i = *(int*)val;
615  if (i<0 || i>=nenums) {
616    snprintf(buf, bufsize, "<invalid:%d>",i);
617    atokenize_free(enums, nenums);
618    return(-1);
619  }
620  snprintf(buf, bufsize, "%s", enums[i]);
621  return 0;
622}
623
624/* default functions for stringeans */
625
626int owl_variable_string_validate_default(struct _owl_variable *v, void *newval) {
627  if (newval == NULL) return(0);
628  else return (1);
629}
630
631int owl_variable_string_set_default(owl_variable *v, void *newval) {
632  if (v->validate_fn) {
633    if (!v->validate_fn(v, newval)) return(-1);
634  }
635  if (v->val) owl_free(v->val);
636  v->val = owl_strdup(newval);
637  return(0);
638}
639
640int owl_variable_string_set_fromstring_default(owl_variable *v, char *newval) {
641  return (v->set_fn(v, newval));
642}
643
644int owl_variable_string_get_tostring_default(owl_variable *v, char* buf, int bufsize, void *val) {
645  if (val == NULL) {
646    snprintf(buf, bufsize, "<null>");
647    return -1;
648  } else {
649    snprintf(buf, bufsize, "%s", (char*)val);
650    return 0;
651  }
652}
653
654
655
656/**************************************************************************/
657/************************* REGRESSION TESTS *******************************/
658/**************************************************************************/
659
660#ifdef OWL_INCLUDE_REG_TESTS
661
662#define FAIL_UNLESS(desc,pred) printf("\t%-4s: %s\n", (pred)?"ok":(numfailed++,"FAIL"), desc)
663
664int owl_variable_regtest(void) {
665  owl_vardict vd;
666  int numfailed=0;
667  char buf[1024];
668
669  printf("BEGIN testing owl_variable\n");
670  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
671
672  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"personalbell"));
673  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
674  FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"personalbell", buf, 1024));
675  FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
676  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"personalbell"));
677  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"personalbell"));
678  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"personalbell","off",0));
679  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"personalbell"));
680  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"personalbell","xxx",0));
681  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"personalbell"));
682
683
684  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
685  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
686  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
687
688  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
689  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
690  FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
691  FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
692  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
693  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
694  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
695  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
696  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0));
697  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
698  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0));
699  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0));
700  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
701
702  FAIL_UNLESS("get enum", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser"));
703  FAIL_UNLESS("get enum as string 1", 0==owl_variable_get_tostring(&vd,"webbrowser", buf, 1024));
704  FAIL_UNLESS("get enum as string 2", 0==strcmp(buf,"netscape"));
705  FAIL_UNLESS("set enum 1", 0==owl_variable_set_int(&vd,"webbrowser",OWL_WEBBROWSER_GALEON));
706  FAIL_UNLESS("get enum 2", OWL_WEBBROWSER_GALEON==owl_variable_get_int(&vd,"webbrowser"));
707  FAIL_UNLESS("set enum 1b", -1==owl_variable_set_int(&vd,"webbrowser",-3));
708  FAIL_UNLESS("set enum 1b", -1==owl_variable_set_int(&vd,"webbrowser",209));
709  FAIL_UNLESS("get enum 2b", OWL_WEBBROWSER_GALEON==owl_variable_get_int(&vd,"webbrowser"));
710  FAIL_UNLESS("set enum 3", 0==owl_variable_set_fromstring(&vd,"webbrowser","none",0));
711  FAIL_UNLESS("get enum 4", OWL_WEBBROWSER_NONE==owl_variable_get_int(&vd,"webbrowser"));
712  FAIL_UNLESS("set enum 5", 0==owl_variable_set_fromstring(&vd,"webbrowser","netscape",0));
713  FAIL_UNLESS("get enum 6", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser"));
714  FAIL_UNLESS("set enum 7", -1==owl_variable_set_fromstring(&vd,"webbrowser","xxx",0));
715  FAIL_UNLESS("set enum 8", -1==owl_variable_set_fromstring(&vd,"webbrowser","",0));
716  FAIL_UNLESS("set enum 9", -1==owl_variable_set_fromstring(&vd,"webbrowser","netscapey",0));
717  FAIL_UNLESS("get enum 10", OWL_WEBBROWSER_NETSCAPE==owl_variable_get_int(&vd,"webbrowser"));
718
719
720
721  owl_variable_dict_free(&vd);
722
723  if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n");
724  printf("END testing owl_variable (%d failures)\n", numfailed);
725  return(numfailed);
726}
727
728
729#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.