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