1 /* $NetBSD: mail_conf_int.c,v 1.1.1.2 2011/03/02 19:32:15 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* mail_conf_int 3 6 /* SUMMARY 7 /* integer-valued configuration parameter support 8 /* SYNOPSIS 9 /* #include <mail_conf.h> 10 /* 11 /* int get_mail_conf_int(name, defval, min, max); 12 /* const char *name; 13 /* int defval; 14 /* int min; 15 /* int max; 16 /* 17 /* int get_mail_conf_int_fn(name, defval, min, max); 18 /* const char *name; 19 /* int (*defval)(); 20 /* int min; 21 /* int max; 22 /* 23 /* void set_mail_conf_int(name, value) 24 /* const char *name; 25 /* int value; 26 /* 27 /* void get_mail_conf_int_table(table) 28 /* const CONFIG_INT_TABLE *table; 29 /* 30 /* void get_mail_conf_int_fn_table(table) 31 /* const CONFIG_INT_TABLE *table; 32 /* AUXILIARY FUNCTIONS 33 /* int get_mail_conf_int2(name1, name2, defval, min, max); 34 /* const char *name1; 35 /* const char *name2; 36 /* int defval; 37 /* int min; 38 /* int max; 39 /* DESCRIPTION 40 /* This module implements configuration parameter support 41 /* for integer values. 42 /* 43 /* get_mail_conf_int() looks up the named entry in the global 44 /* configuration dictionary. The default value is returned 45 /* when no value was found. 46 /* \fImin\fR is zero or specifies a lower limit on the integer 47 /* value or string length; \fImax\fR is zero or specifies an 48 /* upper limit on the integer value or string length. 49 /* 50 /* get_mail_conf_int_fn() is similar but specifies a function that 51 /* provides the default value. The function is called only 52 /* when the default value is needed. 53 /* 54 /* set_mail_conf_int() updates the named entry in the global 55 /* configuration dictionary. This has no effect on values that 56 /* have been looked up earlier via the get_mail_conf_XXX() routines. 57 /* 58 /* get_mail_conf_int_table() and get_mail_conf_int_fn_table() initialize 59 /* lists of variables, as directed by their table arguments. A table 60 /* must be terminated by a null entry. 61 /* 62 /* get_mail_conf_int2() concatenates the two names and is otherwise 63 /* identical to get_mail_conf_int(). 64 /* DIAGNOSTICS 65 /* Fatal errors: malformed numerical value. 66 /* SEE ALSO 67 /* config(3) general configuration 68 /* mail_conf_str(3) string-valued configuration parameters 69 /* LICENSE 70 /* .ad 71 /* .fi 72 /* The Secure Mailer license must be distributed with this software. 73 /* AUTHOR(S) 74 /* Wietse Venema 75 /* IBM T.J. Watson Research 76 /* P.O. Box 704 77 /* Yorktown Heights, NY 10598, USA 78 /*--*/ 79 80 /* System library. */ 81 82 #include <sys_defs.h> 83 #include <stdlib.h> 84 #include <stdio.h> /* BUFSIZ */ 85 #include <errno.h> 86 87 /* Utility library. */ 88 89 #include <msg.h> 90 #include <mymalloc.h> 91 #include <dict.h> 92 #include <stringops.h> 93 94 /* Global library. */ 95 96 #include "mail_conf.h" 97 98 /* convert_mail_conf_int - look up and convert integer parameter value */ 99 100 static int convert_mail_conf_int(const char *name, int *intval) 101 { 102 const char *strval; 103 char *end; 104 long longval; 105 106 if ((strval = mail_conf_lookup_eval(name)) != 0) { 107 errno = 0; 108 *intval = longval = strtol(strval, &end, 10); 109 if (*strval == 0 || *end != 0 || errno == ERANGE || longval != *intval) 110 msg_fatal("bad numerical configuration: %s = %s", name, strval); 111 return (1); 112 } 113 return (0); 114 } 115 116 /* check_mail_conf_int - validate integer value */ 117 118 static void check_mail_conf_int(const char *name, int intval, int min, int max) 119 { 120 if (min && intval < min) 121 msg_fatal("invalid %s parameter value %d < %d", name, intval, min); 122 if (max && intval > max) 123 msg_fatal("invalid %s parameter value %d > %d", name, intval, max); 124 } 125 126 /* get_mail_conf_int - evaluate integer-valued configuration variable */ 127 128 int get_mail_conf_int(const char *name, int defval, int min, int max) 129 { 130 int intval; 131 132 if (convert_mail_conf_int(name, &intval) == 0) 133 set_mail_conf_int(name, intval = defval); 134 check_mail_conf_int(name, intval, min, max); 135 return (intval); 136 } 137 138 /* get_mail_conf_int2 - evaluate integer-valued configuration variable */ 139 140 int get_mail_conf_int2(const char *name1, const char *name2, int defval, 141 int min, int max) 142 { 143 int intval; 144 char *name; 145 146 name = concatenate(name1, name2, (char *) 0); 147 if (convert_mail_conf_int(name, &intval) == 0) 148 set_mail_conf_int(name, intval = defval); 149 check_mail_conf_int(name, intval, min, max); 150 myfree(name); 151 return (intval); 152 } 153 154 /* get_mail_conf_int_fn - evaluate integer-valued configuration variable */ 155 156 typedef int (*stupid_indent_int) (void); 157 158 int get_mail_conf_int_fn(const char *name, stupid_indent_int defval, 159 int min, int max) 160 { 161 int intval; 162 163 if (convert_mail_conf_int(name, &intval) == 0) 164 set_mail_conf_int(name, intval = defval()); 165 check_mail_conf_int(name, intval, min, max); 166 return (intval); 167 } 168 169 /* set_mail_conf_int - update integer-valued configuration dictionary entry */ 170 171 void set_mail_conf_int(const char *name, int value) 172 { 173 char buf[BUFSIZ]; /* yeah! crappy code! */ 174 175 sprintf(buf, "%d", value); /* yeah! more crappy code! */ 176 mail_conf_update(name, buf); 177 } 178 179 /* get_mail_conf_int_table - look up table of integers */ 180 181 void get_mail_conf_int_table(const CONFIG_INT_TABLE *table) 182 { 183 while (table->name) { 184 table->target[0] = get_mail_conf_int(table->name, table->defval, 185 table->min, table->max); 186 table++; 187 } 188 } 189 190 /* get_mail_conf_int_fn_table - look up integers, defaults are functions */ 191 192 void get_mail_conf_int_fn_table(const CONFIG_INT_FN_TABLE *table) 193 { 194 while (table->name) { 195 table->target[0] = get_mail_conf_int_fn(table->name, table->defval, 196 table->min, table->max); 197 table++; 198 } 199 } 200