#include <sys_defs.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <msg.h>
#include <mymalloc.h>
#include <dict.h>
#include <stringops.h>
#include "conv_time.h"
#include "mail_conf.h"
static int convert_mail_conf_time(const char *name, int *intval, int def_unit)
{
const char *strval;
if ((strval = mail_conf_lookup_eval(name)) == 0)
return (0);
if (conv_time(strval, intval, def_unit) == 0)
msg_fatal("parameter %s: bad time value or unit: %s", name, strval);
return (1);
}
static void check_mail_conf_time(const char *name, int intval, int min, int max)
{
if (min && intval < min)
msg_fatal("invalid %s: %d (min %d)", name, intval, min);
if (max && intval > max)
msg_fatal("invalid %s: %d (max %d)", name, intval, max);
}
static int get_def_time_unit(const char *name, const char *defval)
{
const char *cp;
for (cp = mail_conf_eval(defval); ; cp++) {
if (*cp == 0)
msg_panic("parameter %s: missing time unit in default value: %s",
name, defval);
if (ISALPHA(*cp)) {
if (cp[1] != 0)
msg_panic("parameter %s: bad time unit in default value: %s",
name, defval);
return (*cp);
}
}
}
int get_mail_conf_time(const char *name, const char *defval, int min, int max)
{
int intval;
int def_unit;
def_unit = get_def_time_unit(name, defval);
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
set_mail_conf_time(name, defval);
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
msg_panic("get_mail_conf_time: parameter not found: %s", name);
check_mail_conf_time(name, intval, min, max);
return (intval);
}
int get_mail_conf_time2(const char *name1, const char *name2,
int defval, int def_unit, int min, int max)
{
int intval;
char *name;
name = concatenate(name1, name2, (char *) 0);
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
set_mail_conf_time_int(name, defval);
if (convert_mail_conf_time(name, &intval, def_unit) == 0)
msg_panic("get_mail_conf_time2: parameter not found: %s", name);
check_mail_conf_time(name, intval, min, max);
myfree(name);
return (intval);
}
void set_mail_conf_time(const char *name, const char *value)
{
mail_conf_update(name, value);
}
void set_mail_conf_time_int(const char *name, int value)
{
char buf[BUFSIZ];
snprintf(buf, sizeof buf, "%ds", value);
mail_conf_update(name, buf);
}
void get_mail_conf_time_table(const CONFIG_TIME_TABLE *table)
{
while (table->name) {
table->target[0] = get_mail_conf_time(table->name, table->defval,
table->min, table->max);
table++;
}
}
#ifdef TEST
#include <vstream.h>
int main(int unused_argc, char **unused_argv)
{
static int seconds;
static int minutes;
static int hours;
static int days;
static int weeks;
static const CONFIG_TIME_TABLE time_table[] = {
"seconds", "10s", &seconds, 0, 0,
"minutes", "10m", &minutes, 0, 0,
"hours", "10h", &hours, 0, 0,
"days", "10d", &days, 0, 0,
"weeks", "10w", &weeks, 0, 0,
0,
};
get_mail_conf_time_table(time_table);
vstream_printf("10 seconds = %d\n", seconds);
vstream_printf("10 minutes = %d\n", minutes);
vstream_printf("10 hours = %d\n", hours);
vstream_printf("10 days = %d\n", days);
vstream_printf("10 weeks = %d\n", weeks);
vstream_fflush(VSTREAM_OUT);
return (0);
}
#endif