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