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