7 /* time interval configuration parameter support
9 /* #include <mail_conf.h>
11 /* int get_mail_conf_time(name, defval, min, max);
13 /* const char *defval;
17 /* void set_mail_conf_time(name, value)
21 /* void set_mail_conf_time_int(name, value)
25 /* void get_mail_conf_time_table(table)
26 /* const CONFIG_TIME_TABLE *table;
27 /* AUXILIARY FUNCTIONS
28 /* int get_mail_conf_time2(name1, name2, defval, def_unit, min, max);
36 /* This module implements configuration parameter support
37 /* for time interval values. The conversion routines understand
38 /* one-letter suffixes to specify an explicit time unit: s
39 /* (seconds), m (minutes), h (hours), d (days) or w (weeks).
40 /* Internally, time is represented in seconds.
42 /* get_mail_conf_time() looks up the named entry in the global
43 /* configuration dictionary. The default value is returned
44 /* when no value was found. \fIdef_unit\fR supplies the default
45 /* time unit for numbers numbers specified without explicit unit.
46 /* \fImin\fR is zero or specifies a lower limit on the integer
47 /* value or string length; \fImax\fR is zero or specifies an
48 /* upper limit on the integer value or string length.
50 /* set_mail_conf_time() updates the named entry in the global
51 /* configuration dictionary. This has no effect on values that
52 /* have been looked up earlier via the get_mail_conf_XXX() routines.
54 /* get_mail_conf_time_table() and get_mail_conf_time_fn_table() initialize
55 /* lists of variables, as directed by their table arguments. A table
56 /* must be terminated by a null entry.
58 /* Fatal errors: malformed numerical value, unknown time unit.
60 /* Values and defaults are given in any unit; upper and lower
61 /* bounds are given in seconds.
63 /* config(3) general configuration
64 /* mail_conf_str(3) string-valued configuration parameters
68 /* The Secure Mailer license must be distributed with this software.
71 /* IBM T.J. Watson Research
73 /* Yorktown Heights, NY 10598, USA
80 #include <stdio.h> /* sscanf() */
83 /* Utility library. */
88 #include <stringops.h>
92 #include "conv_time.h"
93 #include "mail_conf.h"
95 /* convert_mail_conf_time - look up and convert integer parameter value */
97 static int convert_mail_conf_time(const char *name
, int *intval
, int def_unit
)
101 if ((strval
= mail_conf_lookup_eval(name
)) == 0)
103 if (conv_time(strval
, intval
, def_unit
) == 0)
104 msg_fatal("parameter %s: bad time value or unit: %s", name
, strval
);
108 /* check_mail_conf_time - validate integer value */
110 static void check_mail_conf_time(const char *name
, int intval
, int min
, int max
)
112 if (min
&& intval
< min
)
113 msg_fatal("invalid %s: %d (min %d)", name
, intval
, min
);
114 if (max
&& intval
> max
)
115 msg_fatal("invalid %s: %d (max %d)", name
, intval
, max
);
118 /* get_def_time_unit - extract time unit from default value */
120 static int get_def_time_unit(const char *name
, const char *defval
)
124 for (cp
= mail_conf_eval(defval
); /* void */ ; cp
++) {
126 msg_panic("parameter %s: missing time unit in default value: %s",
130 msg_panic("parameter %s: bad time unit in default value: %s",
137 /* get_mail_conf_time - evaluate integer-valued configuration variable */
139 int get_mail_conf_time(const char *name
, const char *defval
, int min
, int max
)
144 def_unit
= get_def_time_unit(name
, defval
);
145 if (convert_mail_conf_time(name
, &intval
, def_unit
) == 0)
146 set_mail_conf_time(name
, defval
);
147 if (convert_mail_conf_time(name
, &intval
, def_unit
) == 0)
148 msg_panic("get_mail_conf_time: parameter not found: %s", name
);
149 check_mail_conf_time(name
, intval
, min
, max
);
153 /* get_mail_conf_time2 - evaluate integer-valued configuration variable */
155 int get_mail_conf_time2(const char *name1
, const char *name2
,
156 int defval
, int def_unit
, int min
, int max
)
161 name
= concatenate(name1
, name2
, (char *) 0);
162 if (convert_mail_conf_time(name
, &intval
, def_unit
) == 0)
163 set_mail_conf_time_int(name
, defval
);
164 if (convert_mail_conf_time(name
, &intval
, def_unit
) == 0)
165 msg_panic("get_mail_conf_time2: parameter not found: %s", name
);
166 check_mail_conf_time(name
, intval
, min
, max
);
171 /* set_mail_conf_time - update integer-valued configuration dictionary entry */
173 void set_mail_conf_time(const char *name
, const char *value
)
175 mail_conf_update(name
, value
);
178 /* set_mail_conf_time_int - update integer-valued configuration dictionary entry */
180 void set_mail_conf_time_int(const char *name
, int value
)
182 char buf
[BUFSIZ
]; /* yeah! crappy code! */
184 sprintf(buf
, "%ds", value
); /* yeah! more crappy code! */
185 mail_conf_update(name
, buf
);
188 /* get_mail_conf_time_table - look up table of integers */
190 void get_mail_conf_time_table(const CONFIG_TIME_TABLE
*table
)
192 while (table
->name
) {
193 table
->target
[0] = get_mail_conf_time(table
->name
, table
->defval
,
194 table
->min
, table
->max
);
202 * Stand-alone driver program for regression testing.
206 int main(int unused_argc
, char **unused_argv
)
213 static const CONFIG_TIME_TABLE time_table
[] = {
214 "seconds", "10s", &seconds
, 0, 0,
215 "minutes", "10m", &minutes
, 0, 0,
216 "hours", "10h", &hours
, 0, 0,
217 "days", "10d", &days
, 0, 0,
218 "weeks", "10w", &weeks
, 0, 0,
222 get_mail_conf_time_table(time_table
);
223 vstream_printf("10 seconds = %d\n", seconds
);
224 vstream_printf("10 minutes = %d\n", minutes
);
225 vstream_printf("10 hours = %d\n", hours
);
226 vstream_printf("10 days = %d\n", days
);
227 vstream_printf("10 weeks = %d\n", weeks
);
228 vstream_fflush(VSTREAM_OUT
);