5
/* time interval configuration parameter support
7
/* #include <mail_conf.h>
9
/* int get_mail_conf_time(name, defval, min, max);
11
/* const char *defval;
15
/* void set_mail_conf_time(name, value)
19
/* void get_mail_conf_time_table(table)
20
/* CONFIG_TIME_TABLE *table;
21
/* AUXILIARY FUNCTIONS
22
/* int get_mail_conf_time2(name1, name2, defval, min, max);
29
/* This module implements configuration parameter support
30
/* for time interval values. The conversion routines understand
31
/* one-letter suffixes to specify an explicit time unit: s
32
/* (seconds), m (minutes), h (hours), d (days) or w (weeks).
33
/* Internally, time is represented in seconds.
35
/* get_mail_conf_time() looks up the named entry in the global
36
/* configuration dictionary. The default value is returned
37
/* when no value was found. \fIdef_unit\fR supplies the default
38
/* time unit for numbers numbers specified without explicit unit.
39
/* \fImin\fR is zero or specifies a lower limit on the integer
40
/* value or string length; \fImax\fR is zero or specifies an
41
/* upper limit on the integer value or string length.
43
/* set_mail_conf_time() updates the named entry in the global
44
/* configuration dictionary. This has no effect on values that
45
/* have been looked up earlier via the get_mail_conf_XXX() routines.
47
/* get_mail_conf_time_table() and get_mail_conf_time_fn_table() initialize
48
/* lists of variables, as directed by their table arguments. A table
49
/* must be terminated by a null entry.
51
/* Fatal errors: malformed numerical value, unknown time unit.
53
/* Values and defaults are given in any unit; upper and lower
54
/* bounds are given in seconds.
56
/* config(3) general configuration
57
/* mail_conf_str(3) string-valued configuration parameters
61
/* The Secure Mailer license must be distributed with this software.
64
/* IBM T.J. Watson Research
66
/* Yorktown Heights, NY 10598, USA
73
#include <stdio.h> /* sscanf() */
76
/* Utility library. */
81
#include <stringops.h>
85
#include "mail_conf.h"
88
#define HOUR (60 * MINUTE)
89
#define DAY (24 * HOUR)
90
#define WEEK (7 * DAY)
92
/* convert_mail_conf_time - look up and convert integer parameter value */
94
static int convert_mail_conf_time(const char *name, int *intval, int def_unit)
100
if ((strval = mail_conf_lookup_eval(name)) == 0)
103
switch (sscanf(strval, "%d%c%c", intval, &unit, &junk)) {
124
msg_fatal("parameter %s: bad time unit: %s", name, strval);
127
/* check_mail_conf_time - validate integer value */
129
static void check_mail_conf_time(const char *name, int intval, int min, int max)
131
if (min && intval < min)
132
msg_fatal("invalid %s: %d (min %d)", name, intval, min);
133
if (max && intval > max)
134
msg_fatal("invalid %s: %d (max %d)", name, intval, max);
137
/* get_def_time_unit - extract time unit from default value */
139
static int get_def_time_unit(const char *name, const char *defval)
143
for (cp = mail_conf_eval(defval); /* void */ ; cp++) {
145
msg_panic("parameter %s: missing time unit in default value: %s",
149
msg_panic("parameter %s: bad time unit in default value: %s",
156
/* get_mail_conf_time - evaluate integer-valued configuration variable */
158
int get_mail_conf_time(const char *name, const char *defval, int min, int max)
163
def_unit = get_def_time_unit(name, defval);
164
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
165
set_mail_conf_time(name, defval);
166
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
167
msg_panic("get_mail_conf_time: parameter not found: %s", name);
168
check_mail_conf_time(name, intval, min, max);
172
/* get_mail_conf_time2 - evaluate integer-valued configuration variable */
174
int get_mail_conf_time2(const char *name1, const char *name2,
175
const char *defval, int min, int max)
181
name = concatenate(name1, name2, (char *) 0);
182
def_unit = get_def_time_unit(name, defval);
183
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
184
set_mail_conf_time(name, defval);
185
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
186
msg_panic("get_mail_conf_time2: parameter not found: %s", name);
187
check_mail_conf_time(name, intval, min, max);
192
/* set_mail_conf_time - update integer-valued configuration dictionary entry */
194
void set_mail_conf_time(const char *name, const char *value)
196
mail_conf_update(name, value);
199
/* get_mail_conf_time_table - look up table of integers */
201
void get_mail_conf_time_table(CONFIG_TIME_TABLE *table)
203
while (table->name) {
204
table->target[0] = get_mail_conf_time(table->name, table->defval,
205
table->min, table->max);
213
* Stand-alone driver program for regression testing.
217
int main(int unused_argc, char **unused_argv)
224
static CONFIG_TIME_TABLE time_table[] = {
225
"seconds", "10s", &seconds, 0, 0,
226
"minutes", "10m", &minutes, 0, 0,
227
"hours", "10h", &hours, 0, 0,
228
"days", "10d", &days, 0, 0,
229
"weeks", "10w", &weeks, 0, 0,
233
get_mail_conf_time_table(time_table);
234
vstream_printf("10 seconds = %d\n", seconds);
235
vstream_printf("10 minutes = %d\n", minutes);
236
vstream_printf("10 hours = %d\n", hours);
237
vstream_printf("10 days = %d\n", days);
238
vstream_printf("10 weeks = %d\n", weeks);
239
vstream_fflush(VSTREAM_OUT);