1 /*        $NetBSD: mail_conf_bool.c,v 1.1.1.1 2009/06/23 10:08:46 tron Exp $    */
2 
3 /*++
4 /* NAME
5 /*        mail_conf_bool 3
6 /* SUMMARY
7 /*        boolean-valued configuration parameter support
8 /* SYNOPSIS
9 /*        #include <mail_conf.h>
10 /*
11 /*        int       get_mail_conf_bool(name, defval)
12 /*        const char *name;
13 /*        int       defval;
14 /*
15 /*        int       get_mail_conf_bool_fn(name, defval)
16 /*        const char *name;
17 /*        int       (*defval)();
18 /*
19 /*        void      set_mail_conf_bool(name, value)
20 /*        const char *name;
21 /*        int       value;
22 /*
23 /*        void      get_mail_conf_bool_table(table)
24 /*        const CONFIG_BOOL_TABLE *table;
25 /*
26 /*        void      get_mail_conf_bool_fn_table(table)
27 /*        const CONFIG_BOOL_TABLE *table;
28 /* DESCRIPTION
29 /*        This module implements configuration parameter support for
30 /*        boolean values. The internal representation is zero (false)
31 /*        and non-zero (true). The external representation is "no"
32 /*        (false) and "yes" (true). The conversion from external
33 /*        representation is case insensitive.
34 /*
35 /*        get_mail_conf_bool() looks up the named entry in the global
36 /*        configuration dictionary. The specified default value is
37 /*        returned when no value was found.
38 /*
39 /*        get_mail_conf_bool_fn() is similar but specifies a function that
40 /*        provides the default value. The function is called only
41 /*        when the default value is needed.
42 /*
43 /*        set_mail_conf_bool() updates the named entry in the global
44 /*        configuration dictionary. This has no effect on values that
45 /*        have been looked up earlier via the get_mail_conf_XXX() routines.
46 /*
47 /*        get_mail_conf_bool_table() and get_mail_conf_int_fn_table() initialize
48 /*        lists of variables, as directed by their table arguments. A table
49 /*        must be terminated by a null entry.
50 /* DIAGNOSTICS
51 /*        Fatal errors: malformed boolean value.
52 /* SEE ALSO
53 /*        config(3) general configuration
54 /*        mail_conf_str(3) string-valued configuration parameters
55 /*        mail_conf_int(3) integer-valued configuration parameters
56 /* LICENSE
57 /* .ad
58 /* .fi
59 /*        The Secure Mailer license must be distributed with this software.
60 /* AUTHOR(S)
61 /*        Wietse Venema
62 /*        IBM T.J. Watson Research
63 /*        P.O. Box 704
64 /*        Yorktown Heights, NY 10598, USA
65 /*--*/
66 
67 /* System library. */
68 
69 #include <sys_defs.h>
70 #include <stdlib.h>
71 #include <string.h>
72 
73 #ifdef STRCASECMP_IN_STRINGS_H
74 #include <strings.h>
75 #endif
76 
77 /* Utility library. */
78 
79 #include <msg.h>
80 #include <dict.h>
81 
82 /* Global library. */
83 
84 #include "mail_conf.h"
85 
86 /* convert_mail_conf_bool - look up and convert boolean parameter value */
87 
convert_mail_conf_bool(const char * name,int * intval)88 static int convert_mail_conf_bool(const char *name, int *intval)
89 {
90     const char *strval;
91 
92     if ((strval = mail_conf_lookup_eval(name)) == 0) {
93           return (0);
94     } else {
95           if (strcasecmp(strval, CONFIG_BOOL_YES) == 0) {
96               *intval = 1;
97           } else if (strcasecmp(strval, CONFIG_BOOL_NO) == 0) {
98               *intval = 0;
99           } else {
100               msg_fatal("bad boolean configuration: %s = %s", name, strval);
101           }
102           return (1);
103     }
104 }
105 
106 /* get_mail_conf_bool - evaluate boolean-valued configuration variable */
107 
get_mail_conf_bool(const char * name,int defval)108 int     get_mail_conf_bool(const char *name, int defval)
109 {
110     int     intval;
111 
112     if (convert_mail_conf_bool(name, &intval) == 0)
113           set_mail_conf_bool(name, intval = defval);
114     return (intval);
115 }
116 
117 /* get_mail_conf_bool_fn - evaluate boolean-valued configuration variable */
118 
119 typedef int (*stupid_indent_int) (void);
120 
get_mail_conf_bool_fn(const char * name,stupid_indent_int defval)121 int     get_mail_conf_bool_fn(const char *name, stupid_indent_int defval)
122 {
123     int     intval;
124 
125     if (convert_mail_conf_bool(name, &intval) == 0)
126           set_mail_conf_bool(name, intval = defval());
127     return (intval);
128 }
129 
130 /* set_mail_conf_bool - update boolean-valued configuration dictionary entry */
131 
set_mail_conf_bool(const char * name,int value)132 void    set_mail_conf_bool(const char *name, int value)
133 {
134     mail_conf_update(name, value ? CONFIG_BOOL_YES : CONFIG_BOOL_NO);
135 }
136 
137 /* get_mail_conf_bool_table - look up table of booleans */
138 
get_mail_conf_bool_table(const CONFIG_BOOL_TABLE * table)139 void    get_mail_conf_bool_table(const CONFIG_BOOL_TABLE *table)
140 {
141     while (table->name) {
142           table->target[0] = get_mail_conf_bool(table->name, table->defval);
143           table++;
144     }
145 }
146 
147 /* get_mail_conf_bool_fn_table - look up booleans, defaults are functions */
148 
get_mail_conf_bool_fn_table(const CONFIG_BOOL_FN_TABLE * table)149 void    get_mail_conf_bool_fn_table(const CONFIG_BOOL_FN_TABLE *table)
150 {
151     while (table->name) {
152           table->target[0] = get_mail_conf_bool_fn(table->name, table->defval);
153           table++;
154     }
155 }
156