~ubuntu-branches/ubuntu/vivid/postfix/vivid-proposed

« back to all changes in this revision

Viewing changes to src/global/mail_conf_time.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-02-27 09:33:07 UTC
  • Revision ID: james.westby@ubuntu.com-20050227093307-cn789t27ibnlh6tf
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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    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);
 
23
/*      const char *name1;
 
24
/*      const char *name2;
 
25
/*      int     def_unit;
 
26
/*      int     min;
 
27
/*      int     max;
 
28
/* DESCRIPTION
 
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.
 
34
/*
 
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.
 
42
/*
 
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.
 
46
/*
 
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.
 
50
/* DIAGNOSTICS
 
51
/*      Fatal errors: malformed numerical value, unknown time unit.
 
52
/* BUGS
 
53
/*      Values and defaults are given in any unit; upper and lower
 
54
/*      bounds are given in seconds.
 
55
/* SEE ALSO
 
56
/*      config(3) general configuration
 
57
/*      mail_conf_str(3) string-valued configuration parameters
 
58
/* LICENSE
 
59
/* .ad
 
60
/* .fi
 
61
/*      The Secure Mailer license must be distributed with this software.
 
62
/* AUTHOR(S)
 
63
/*      Wietse Venema
 
64
/*      IBM T.J. Watson Research
 
65
/*      P.O. Box 704
 
66
/*      Yorktown Heights, NY 10598, USA
 
67
/*--*/
 
68
 
 
69
/* System library. */
 
70
 
 
71
#include <sys_defs.h>
 
72
#include <stdlib.h>
 
73
#include <stdio.h>                      /* sscanf() */
 
74
#include <ctype.h>
 
75
 
 
76
/* Utility library. */
 
77
 
 
78
#include <msg.h>
 
79
#include <mymalloc.h>
 
80
#include <dict.h>
 
81
#include <stringops.h>
 
82
 
 
83
/* Global library. */
 
84
 
 
85
#include "mail_conf.h"
 
86
 
 
87
#define MINUTE  (60)
 
88
#define HOUR    (60 * MINUTE)
 
89
#define DAY     (24 * HOUR)
 
90
#define WEEK    (7 * DAY)
 
91
 
 
92
/* convert_mail_conf_time - look up and convert integer parameter value */
 
93
 
 
94
static int convert_mail_conf_time(const char *name, int *intval, int def_unit)
 
95
{
 
96
    const char *strval;
 
97
    char    unit;
 
98
    char    junk;
 
99
 
 
100
    if ((strval = mail_conf_lookup_eval(name)) == 0)
 
101
        return (0);
 
102
 
 
103
    switch (sscanf(strval, "%d%c%c", intval, &unit, &junk)) {
 
104
    case 1:
 
105
        unit = def_unit;
 
106
    case 2:
 
107
        switch (unit) {
 
108
        case 'w':
 
109
            *intval *= WEEK;
 
110
            return (1);
 
111
        case 'd':
 
112
            *intval *= DAY;
 
113
            return (1);
 
114
        case 'h':
 
115
            *intval *= HOUR;
 
116
            return (1);
 
117
        case 'm':
 
118
            *intval *= MINUTE;
 
119
            return (1);
 
120
        case 's':
 
121
            return (1);
 
122
        }
 
123
    }
 
124
    msg_fatal("parameter %s: bad time unit: %s", name, strval);
 
125
}
 
126
 
 
127
/* check_mail_conf_time - validate integer value */
 
128
 
 
129
static void check_mail_conf_time(const char *name, int intval, int min, int max)
 
130
{
 
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);
 
135
}
 
136
 
 
137
/* get_def_time_unit - extract time unit from default value */
 
138
 
 
139
static int get_def_time_unit(const char *name, const char *defval)
 
140
{
 
141
    const char *cp;
 
142
 
 
143
    for (cp = mail_conf_eval(defval); /* void */ ; cp++) {
 
144
        if (*cp == 0)
 
145
            msg_panic("parameter %s: missing time unit in default value: %s",
 
146
                      name, defval);
 
147
        if (ISALPHA(*cp)) {
 
148
            if (cp[1] != 0)
 
149
                msg_panic("parameter %s: bad time unit in default value: %s",
 
150
                          name, defval);
 
151
            return (*cp);
 
152
        }
 
153
    }
 
154
}
 
155
 
 
156
/* get_mail_conf_time - evaluate integer-valued configuration variable */
 
157
 
 
158
int     get_mail_conf_time(const char *name, const char *defval, int min, int max)
 
159
{
 
160
    int     intval;
 
161
    int     def_unit;
 
162
 
 
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);
 
169
    return (intval);
 
170
}
 
171
 
 
172
/* get_mail_conf_time2 - evaluate integer-valued configuration variable */
 
173
 
 
174
int     get_mail_conf_time2(const char *name1, const char *name2,
 
175
                                    const char *defval, int min, int max)
 
176
{
 
177
    int     intval;
 
178
    char   *name;
 
179
    int     def_unit;
 
180
 
 
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);
 
188
    myfree(name);
 
189
    return (intval);
 
190
}
 
191
 
 
192
/* set_mail_conf_time - update integer-valued configuration dictionary entry */
 
193
 
 
194
void    set_mail_conf_time(const char *name, const char *value)
 
195
{
 
196
    mail_conf_update(name, value);
 
197
}
 
198
 
 
199
/* get_mail_conf_time_table - look up table of integers */
 
200
 
 
201
void    get_mail_conf_time_table(CONFIG_TIME_TABLE *table)
 
202
{
 
203
    while (table->name) {
 
204
        table->target[0] = get_mail_conf_time(table->name, table->defval,
 
205
                                              table->min, table->max);
 
206
        table++;
 
207
    }
 
208
}
 
209
 
 
210
#ifdef TEST
 
211
 
 
212
 /*
 
213
  * Stand-alone driver program for regression testing.
 
214
  */
 
215
#include <vstream.h>
 
216
 
 
217
int     main(int unused_argc, char **unused_argv)
 
218
{
 
219
    static int seconds;
 
220
    static int minutes;
 
221
    static int hours;
 
222
    static int days;
 
223
    static int weeks;
 
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,
 
230
        0,
 
231
    };
 
232
 
 
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);
 
240
}
 
241
 
 
242
#endif