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