1 /* $NetBSD: mail_conf_long.c,v 1.2 2020/03/18 19:05:16 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* mail_conf_long 3 6 /* SUMMARY 7 /* long integer-valued configuration parameter support 8 /* SYNOPSIS 9 /* #include <mail_conf.h> 10 /* 11 /* int get_mail_conf_long(name, defval, min, max); 12 /* const char *name; 13 /* long defval; 14 /* long min; 15 /* long max; 16 /* 17 /* int get_mail_conf_long_fn(name, defval, min, max); 18 /* const char *name; 19 /* long (*defval)(void); 20 /* long min; 21 /* long max; 22 /* 23 /* void set_mail_conf_long(name, value) 24 /* const char *name; 25 /* long value; 26 /* 27 /* void get_mail_conf_long_table(table) 28 /* const CONFIG_LONG_TABLE *table; 29 /* 30 /* void get_mail_conf_long_fn_table(table) 31 /* const CONFIG_LONG_TABLE *table; 32 /* AUXILIARY FUNCTIONS 33 /* int get_mail_conf_long2(name1, name2, defval, min, max); 34 /* const char *name1; 35 /* const char *name2; 36 /* long defval; 37 /* long min; 38 /* long max; 39 /* DESCRIPTION 40 /* This module implements configuration parameter support 41 /* for long integer values. 42 /* 43 /* get_mail_conf_long() 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 long 47 /* integer value; \fImax\fR is zero or specifies an upper limit 48 /* on the long integer value. 49 /* 50 /* get_mail_conf_long_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_long() 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_long_table() and get_mail_conf_long_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_long2() concatenates the two names and is otherwise 63 /* identical to get_mail_conf_long(). 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 /* Wietse Venema 80 /* Google, Inc. 81 /* 111 8th Avenue 82 /* New York, NY 10011, USA 83 /*--*/ 84 85 /* System library. */ 86 87 #include <sys_defs.h> 88 #include <stdlib.h> 89 #include <stdio.h> /* BUFSIZ */ 90 #include <errno.h> 91 92 /* Utility library. */ 93 94 #include <msg.h> 95 #include <mymalloc.h> 96 #include <dict.h> 97 #include <stringops.h> 98 99 /* Global library. */ 100 101 #include "mail_conf.h" 102 103 /* convert_mail_conf_long - look up and convert integer parameter value */ 104 105 static int convert_mail_conf_long(const char *name, long *longval) 106 { 107 const char *strval; 108 char *end; 109 110 if ((strval = mail_conf_lookup_eval(name)) != 0) { 111 errno = 0; 112 *longval = strtol(strval, &end, 10); 113 if (*strval == 0 || *end != 0 || errno == ERANGE) 114 msg_fatal("bad numerical configuration: %s = %s", name, strval); 115 return (1); 116 } 117 return (0); 118 } 119 120 /* check_mail_conf_long - validate integer value */ 121 122 static void check_mail_conf_long(const char *name, long longval, long min, long max) 123 { 124 if (min && longval < min) 125 msg_fatal("invalid %s parameter value %ld < %ld", name, longval, min); 126 if (max && longval > max) 127 msg_fatal("invalid %s parameter value %ld > %ld", name, longval, max); 128 } 129 130 /* get_mail_conf_long - evaluate integer-valued configuration variable */ 131 132 long get_mail_conf_long(const char *name, long defval, long min, long max) 133 { 134 long longval; 135 136 if (convert_mail_conf_long(name, &longval) == 0) 137 set_mail_conf_long(name, longval = defval); 138 check_mail_conf_long(name, longval, min, max); 139 return (longval); 140 } 141 142 /* get_mail_conf_long2 - evaluate integer-valued configuration variable */ 143 144 long get_mail_conf_long2(const char *name1, const char *name2, long defval, 145 long min, long max) 146 { 147 long longval; 148 char *name; 149 150 name = concatenate(name1, name2, (char *) 0); 151 if (convert_mail_conf_long(name, &longval) == 0) 152 set_mail_conf_long(name, longval = defval); 153 check_mail_conf_long(name, longval, min, max); 154 myfree(name); 155 return (longval); 156 } 157 158 /* get_mail_conf_long_fn - evaluate integer-valued configuration variable */ 159 160 typedef long (*stupid_indent_long) (void); 161 162 long get_mail_conf_long_fn(const char *name, stupid_indent_long defval, 163 long min, long max) 164 { 165 long longval; 166 167 if (convert_mail_conf_long(name, &longval) == 0) 168 set_mail_conf_long(name, longval = defval()); 169 check_mail_conf_long(name, longval, min, max); 170 return (longval); 171 } 172 173 /* set_mail_conf_long - update integer-valued configuration dictionary entry */ 174 175 void set_mail_conf_long(const char *name, long value) 176 { 177 const char myname[] = "set_mail_conf_long"; 178 char buf[BUFSIZ]; /* yeah! crappy code! */ 179 180 #ifndef NO_SNPRINTF 181 ssize_t ret; 182 183 ret = snprintf(buf, sizeof(buf), "%ld", value); 184 if (ret < 0) 185 msg_panic("%s: output error for %%ld", myname); 186 if (ret >= sizeof(buf)) 187 msg_panic("%s: output for %%ld exceeds space %ld", 188 myname, (long) sizeof(buf)); 189 #else 190 sprintf(buf, "%ld", value); /* yeah! more crappy code! */ 191 #endif 192 mail_conf_update(name, buf); 193 } 194 195 /* get_mail_conf_long_table - look up table of integers */ 196 197 void get_mail_conf_long_table(const CONFIG_LONG_TABLE *table) 198 { 199 while (table->name) { 200 table->target[0] = get_mail_conf_long(table->name, table->defval, 201 table->min, table->max); 202 table++; 203 } 204 } 205 206 /* get_mail_conf_long_fn_table - look up integers, defaults are functions */ 207 208 void get_mail_conf_long_fn_table(const CONFIG_LONG_FN_TABLE *table) 209 { 210 while (table->name) { 211 table->target[0] = get_mail_conf_long_fn(table->name, table->defval, 212 table->min, table->max); 213 table++; 214 } 215 } 216