xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/mail_conf_long.c (revision 567219e1d7461bff1b180e494a9674a287b057a7)
1 /*	$NetBSD: mail_conf_long.c,v 1.1.1.2 2011/03/02 19:32:15 tron 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 
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_long - look up and convert integer parameter value */
99 
100 static int convert_mail_conf_long(const char *name, long *longval)
101 {
102     const char *strval;
103     char   *end;
104 
105     if ((strval = mail_conf_lookup_eval(name)) != 0) {
106 	errno = 0;
107 	*longval = strtol(strval, &end, 10);
108 	if (*strval == 0 || *end != 0 || errno == ERANGE)
109 	    msg_fatal("bad numerical configuration: %s = %s", name, strval);
110 	return (1);
111     }
112     return (0);
113 }
114 
115 /* check_mail_conf_long - validate integer value */
116 
117 static void check_mail_conf_long(const char *name, long longval, long min, long max)
118 {
119     if (min && longval < min)
120 	msg_fatal("invalid %s parameter value %ld < %ld", name, longval, min);
121     if (max && longval > max)
122 	msg_fatal("invalid %s parameter value %ld > %ld", name, longval, max);
123 }
124 
125 /* get_mail_conf_long - evaluate integer-valued configuration variable */
126 
127 long    get_mail_conf_long(const char *name, long defval, long min, long max)
128 {
129     long    longval;
130 
131     if (convert_mail_conf_long(name, &longval) == 0)
132 	set_mail_conf_long(name, longval = defval);
133     check_mail_conf_long(name, longval, min, max);
134     return (longval);
135 }
136 
137 /* get_mail_conf_long2 - evaluate integer-valued configuration variable */
138 
139 long    get_mail_conf_long2(const char *name1, const char *name2, long defval,
140 			            long min, long max)
141 {
142     long    longval;
143     char   *name;
144 
145     name = concatenate(name1, name2, (char *) 0);
146     if (convert_mail_conf_long(name, &longval) == 0)
147 	set_mail_conf_long(name, longval = defval);
148     check_mail_conf_long(name, longval, min, max);
149     myfree(name);
150     return (longval);
151 }
152 
153 /* get_mail_conf_long_fn - evaluate integer-valued configuration variable */
154 
155 typedef long (*stupid_indent_long) (void);
156 
157 long    get_mail_conf_long_fn(const char *name, stupid_indent_long defval,
158 			              long min, long max)
159 {
160     long    longval;
161 
162     if (convert_mail_conf_long(name, &longval) == 0)
163 	set_mail_conf_long(name, longval = defval());
164     check_mail_conf_long(name, longval, min, max);
165     return (longval);
166 }
167 
168 /* set_mail_conf_long - update integer-valued configuration dictionary entry */
169 
170 void    set_mail_conf_long(const char *name, long value)
171 {
172     char    buf[BUFSIZ];		/* yeah! crappy code! */
173 
174     sprintf(buf, "%ld", value);			/* yeah! more crappy code! */
175     mail_conf_update(name, buf);
176 }
177 
178 /* get_mail_conf_long_table - look up table of integers */
179 
180 void    get_mail_conf_long_table(const CONFIG_LONG_TABLE *table)
181 {
182     while (table->name) {
183 	table->target[0] = get_mail_conf_long(table->name, table->defval,
184 					      table->min, table->max);
185 	table++;
186     }
187 }
188 
189 /* get_mail_conf_long_fn_table - look up integers, defaults are functions */
190 
191 void    get_mail_conf_long_fn_table(const CONFIG_LONG_FN_TABLE *table)
192 {
193     while (table->name) {
194 	table->target[0] = get_mail_conf_long_fn(table->name, table->defval,
195 						 table->min, table->max);
196 	table++;
197     }
198 }
199