Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / global / mail_conf_nint.c
blob369a7f4e8bccb38ce0ea983bc378df4b0ff4801e
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* mail_conf_nint 3
6 /* SUMMARY
7 /* integer-valued configuration parameter support
8 /* SYNOPSIS
9 /* #include <mail_conf.h>
11 /* int get_mail_conf_nint(name, defval, min, max);
12 /* const char *name;
13 /* const char *defval;
14 /* int min;
15 /* int max;
17 /* int get_mail_conf_nint_fn(name, defval, min, max);
18 /* const char *name;
19 /* char *(*defval)();
20 /* int min;
21 /* int max;
23 /* void set_mail_conf_nint(name, value)
24 /* const char *name;
25 /* const char *value;
27 /* void set_mail_conf_nint_int(name, value)
28 /* const char *name;
29 /* int value;
31 /* void get_mail_conf_nint_table(table)
32 /* const CONFIG_NINT_TABLE *table;
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}).
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.
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.
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.
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.
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 /*--*/
85 /* System library. */
87 #include <sys_defs.h>
88 #include <stdlib.h>
89 #include <stdio.h> /* sscanf() */
91 /* Utility library. */
93 #include <msg.h>
94 #include <mymalloc.h>
95 #include <dict.h>
96 #include <stringops.h>
98 /* Global library. */
100 #include "mail_conf.h"
102 /* convert_mail_conf_nint - look up and convert integer parameter value */
104 static int convert_mail_conf_nint(const char *name, int *intval)
106 const char *strval;
107 char junk;
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);
114 return (0);
117 /* check_mail_conf_nint - validate integer value */
119 static void check_mail_conf_nint(const char *name, int intval, int min, int max)
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);
127 /* get_mail_conf_nint - evaluate integer-valued configuration variable */
129 int get_mail_conf_nint(const char *name, const char *defval, int min, int max)
131 int intval;
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);
141 /* get_mail_conf_nint2 - evaluate integer-valued configuration variable */
143 int get_mail_conf_nint2(const char *name1, const char *name2, int defval,
144 int min, int max)
146 int intval;
147 char *name;
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);
159 /* get_mail_conf_nint_fn - evaluate integer-valued configuration variable */
161 typedef const char *(*stupid_indent_int) (void);
163 int get_mail_conf_nint_fn(const char *name, stupid_indent_int defval,
164 int min, int max)
166 int intval;
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);
176 /* set_mail_conf_nint - update integer-valued configuration dictionary entry */
178 void set_mail_conf_nint(const char *name, const char *value)
180 mail_conf_update(name, value);
183 /* set_mail_conf_nint_int - update integer-valued configuration dictionary entry */
185 void set_mail_conf_nint_int(const char *name, int value)
187 char buf[BUFSIZ]; /* yeah! crappy code! */
189 sprintf(buf, "%d", value); /* yeah! more crappy code! */
190 mail_conf_update(name, buf);
193 /* get_mail_conf_nint_table - look up table of integers */
195 void get_mail_conf_nint_table(const CONFIG_NINT_TABLE *table)
197 while (table->name) {
198 table->target[0] = get_mail_conf_nint(table->name, table->defval,
199 table->min, table->max);
200 table++;
204 /* get_mail_conf_nint_fn_table - look up integers, defaults are functions */
206 void get_mail_conf_nint_fn_table(const CONFIG_NINT_FN_TABLE *table)
208 while (table->name) {
209 table->target[0] = get_mail_conf_nint_fn(table->name, table->defval,
210 table->min, table->max);
211 table++;