Postfix3.3.1
mail_conf_time.c
[詳解]
1 /*++
2 /* NAME
3 /* mail_conf_time 3
4 /* SUMMARY
5 /* time interval configuration parameter support
6 /* SYNOPSIS
7 /* #include <mail_conf.h>
8 /*
9 /* int get_mail_conf_time(name, defval, min, max);
10 /* const char *name;
11 /* const char *defval;
12 /* int min;
13 /* int max;
14 /*
15 /* void set_mail_conf_time(name, value)
16 /* const char *name;
17 /* const char *value;
18 /*
19 /* void set_mail_conf_time_int(name, value)
20 /* const char *name;
21 /* int value;
22 /*
23 /* void get_mail_conf_time_table(table)
24 /* const CONFIG_TIME_TABLE *table;
25 /* AUXILIARY FUNCTIONS
26 /* int get_mail_conf_time2(name1, name2, defval, def_unit, min, max);
27 /* const char *name1;
28 /* const char *name2;
29 /* int defval;
30 /* int def_unit;
31 /* int min;
32 /* int max;
33 /*
34 /* void check_mail_conf_time(name, intval, min, max)
35 /* const char *name;
36 /* int intval;
37 /* int min;
38 /* int max;
39 /* DESCRIPTION
40 /* This module implements configuration parameter support
41 /* for time interval values. The conversion routines understand
42 /* one-letter suffixes to specify an explicit time unit: s
43 /* (seconds), m (minutes), h (hours), d (days) or w (weeks).
44 /* Internally, time is represented in seconds.
45 /*
46 /* get_mail_conf_time() looks up the named entry in the global
47 /* configuration dictionary. The default value is returned
48 /* when no value was found. \fIdef_unit\fR supplies the default
49 /* time unit for numbers numbers specified without explicit unit.
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 /* set_mail_conf_time() 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_time_table() and get_mail_conf_time_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 /* check_mail_conf_time() terminates the program with a fatal
63 /* runtime error when the time does not meet its requirements.
64 /* DIAGNOSTICS
65 /* Fatal errors: malformed numerical value, unknown time unit.
66 /* BUGS
67 /* Values and defaults are given in any unit; upper and lower
68 /* bounds are given in seconds.
69 /* SEE ALSO
70 /* config(3) general configuration
71 /* mail_conf_str(3) string-valued configuration parameters
72 /* LICENSE
73 /* .ad
74 /* .fi
75 /* The Secure Mailer license must be distributed with this software.
76 /* AUTHOR(S)
77 /* Wietse Venema
78 /* IBM T.J. Watson Research
79 /* P.O. Box 704
80 /* Yorktown Heights, NY 10598, USA
81 /*
82 /* Wietse Venema
83 /* Google, Inc.
84 /* 111 8th Avenue
85 /* New York, NY 10011, USA
86 /*--*/
87 
88 /* System library. */
89 
90 #include <sys_defs.h>
91 #include <stdlib.h>
92 #include <stdio.h> /* BUFSIZ */
93 #include <ctype.h>
94 
95 /* Utility library. */
96 
97 #include <msg.h>
98 #include <mymalloc.h>
99 #include <dict.h>
100 #include <stringops.h>
101 
102 /* Global library. */
103 
104 #include "conv_time.h"
105 #include "mail_conf.h"
106 
107 /* convert_mail_conf_time - look up and convert integer parameter value */
108 
109 static int convert_mail_conf_time(const char *name, int *intval, int def_unit)
110 {
111  const char *strval;
112 
113  if ((strval = mail_conf_lookup_eval(name)) == 0)
114  return (0);
115  if (conv_time(strval, intval, def_unit) == 0)
116  msg_fatal("parameter %s: bad time value or unit: %s", name, strval);
117  return (1);
118 }
119 
120 /* check_mail_conf_time - validate integer value */
121 
122 void check_mail_conf_time(const char *name, int intval, int min, int max)
123 {
124  if (min && intval < min)
125  msg_fatal("invalid %s: %d (min %d)", name, intval, min);
126  if (max && intval > max)
127  msg_fatal("invalid %s: %d (max %d)", name, intval, max);
128 }
129 
130 /* get_def_time_unit - extract time unit from default value */
131 
132 static int get_def_time_unit(const char *name, const char *defval)
133 {
134  const char *cp;
135 
136  for (cp = mail_conf_eval(defval); /* void */ ; cp++) {
137  if (*cp == 0)
138  msg_panic("parameter %s: missing time unit in default value: %s",
139  name, defval);
140  if (ISALPHA(*cp)) {
141 #if 0
142  if (cp[1] != 0)
143  msg_panic("parameter %s: bad time unit in default value: %s",
144  name, defval);
145 #endif
146  return (*cp);
147  }
148  }
149 }
150 
151 /* get_mail_conf_time - evaluate integer-valued configuration variable */
152 
153 int get_mail_conf_time(const char *name, const char *defval, int min, int max)
154 {
155  int intval;
156  int def_unit;
157 
158  def_unit = get_def_time_unit(name, defval);
159  if (convert_mail_conf_time(name, &intval, def_unit) == 0)
160  set_mail_conf_time(name, defval);
161  if (convert_mail_conf_time(name, &intval, def_unit) == 0)
162  msg_panic("get_mail_conf_time: parameter not found: %s", name);
163  check_mail_conf_time(name, intval, min, max);
164  return (intval);
165 }
166 
167 /* get_mail_conf_time2 - evaluate integer-valued configuration variable */
168 
169 int get_mail_conf_time2(const char *name1, const char *name2,
170  int defval, int def_unit, int min, int max)
171 {
172  int intval;
173  char *name;
174 
175  name = concatenate(name1, name2, (char *) 0);
176  if (convert_mail_conf_time(name, &intval, def_unit) == 0)
177  set_mail_conf_time_int(name, defval);
178  if (convert_mail_conf_time(name, &intval, def_unit) == 0)
179  msg_panic("get_mail_conf_time2: parameter not found: %s", name);
180  check_mail_conf_time(name, intval, min, max);
181  myfree(name);
182  return (intval);
183 }
184 
185 /* set_mail_conf_time - update integer-valued configuration dictionary entry */
186 
187 void set_mail_conf_time(const char *name, const char *value)
188 {
189  mail_conf_update(name, value);
190 }
191 
192 /* set_mail_conf_time_int - update integer-valued configuration dictionary entry */
193 
194 void set_mail_conf_time_int(const char *name, int value)
195 {
196  char buf[BUFSIZ]; /* yeah! crappy code! */
197 
198  sprintf(buf, "%ds", value); /* yeah! more crappy code! */
199  mail_conf_update(name, buf);
200 }
201 
202 /* get_mail_conf_time_table - look up table of integers */
203 
205 {
206  while (table->name) {
207  table->target[0] = get_mail_conf_time(table->name, table->defval,
208  table->min, table->max);
209  table++;
210  }
211 }
212 
213 #ifdef TEST
214 
215  /*
216  * Stand-alone driver program for regression testing.
217  */
218 #include <vstream.h>
219 
220 int main(int unused_argc, char **unused_argv)
221 {
222  static int seconds;
223  static int minutes;
224  static int hours;
225  static int days;
226  static int weeks;
227  static const CONFIG_TIME_TABLE time_table[] = {
228  "seconds", "10s", &seconds, 0, 0,
229  "minutes", "10m", &minutes, 0, 0,
230  "hours", "10h", &hours, 0, 0,
231  "days", "10d", &days, 0, 0,
232  "weeks", "10w", &weeks, 0, 0,
233  0,
234  };
235 
236  get_mail_conf_time_table(time_table);
237  vstream_printf("10 seconds = %d\n", seconds);
238  vstream_printf("10 minutes = %d\n", minutes);
239  vstream_printf("10 hours = %d\n", hours);
240  vstream_printf("10 days = %d\n", days);
241  vstream_printf("10 weeks = %d\n", weeks);
243  return (0);
244 }
245 
246 #endif
int get_mail_conf_time2(const char *name1, const char *name2, int defval, int def_unit, int min, int max)
void myfree(void *ptr)
Definition: mymalloc.c:207
NORETURN msg_panic(const char *fmt,...)
Definition: msg.c:295
#define VSTREAM_OUT
Definition: vstream.h:67
int main(int argc, char **argv)
Definition: anvil.c:1010
const char * mail_conf_eval(const char *string)
Definition: mail_conf.c:237
int conv_time(const char *strval, int *timval, int def_unit)
Definition: conv_time.c:67
void check_mail_conf_time(const char *name, int intval, int min, int max)
const char * name
Definition: mail_conf.h:139
const char * defval
Definition: mail_conf.h:140
void set_mail_conf_time_int(const char *name, int value)
void get_mail_conf_time_table(const CONFIG_TIME_TABLE *table)
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
const char * mail_conf_lookup_eval(const char *name)
Definition: mail_conf.c:262
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
char * concatenate(const char *arg0,...)
Definition: concatenate.c:42
void mail_conf_update(const char *key, const char *value)
Definition: mail_conf.c:275
void set_mail_conf_time(const char *name, const char *value)
#define ISALPHA(c)
Definition: sys_defs.h:1746
int get_mail_conf_time(const char *name, const char *defval, int min, int max)