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