source: variable.c @ 217a43e

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