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