summaryrefslogtreecommitdiffstats
path: root/src/global/mail_conf_long.c
blob: c702000f20236ae1dba264166bdb58325404568c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*++
/* NAME
/*	mail_conf_long 3
/* SUMMARY
/*	long integer-valued configuration parameter support
/* SYNOPSIS
/*	#include <mail_conf.h>
/*
/*	int	get_mail_conf_long(name, defval, min, max);
/*	const char *name;
/*	long	defval;
/*	long	min;
/*	long	max;
/*
/*	int	get_mail_conf_long_fn(name, defval, min, max);
/*	const char *name;
/*	long	(*defval)(void);
/*	long	min;
/*	long	max;
/*
/*	void	set_mail_conf_long(name, value)
/*	const char *name;
/*	long	value;
/*
/*	void	get_mail_conf_long_table(table)
/*	const CONFIG_LONG_TABLE *table;
/*
/*	void	get_mail_conf_long_fn_table(table)
/*	const CONFIG_LONG_TABLE *table;
/* AUXILIARY FUNCTIONS
/*	int	get_mail_conf_long2(name1, name2, defval, min, max);
/*	const char *name1;
/*	const char *name2;
/*	long	defval;
/*	long	min;
/*	long	max;
/* DESCRIPTION
/*	This module implements configuration parameter support
/*	for long integer values.
/*
/*	get_mail_conf_long() looks up the named entry in the global
/*	configuration dictionary. The default value is returned
/*	when no value was found.
/*	\fImin\fR is zero or specifies a lower limit on the long
/*	integer value; \fImax\fR is zero or specifies an upper limit
/*	on the long integer value.
/*
/*	get_mail_conf_long_fn() is similar but specifies a function that
/*	provides the default value. The function is called only
/*	when the default value is needed.
/*
/*	set_mail_conf_long() updates the named entry in the global
/*	configuration dictionary. This has no effect on values that
/*	have been looked up earlier via the get_mail_conf_XXX() routines.
/*
/*	get_mail_conf_long_table() and get_mail_conf_long_fn_table() initialize
/*	lists of variables, as directed by their table arguments. A table
/*	must be terminated by a null entry.
/*
/*	get_mail_conf_long2() concatenates the two names and is otherwise
/*	identical to get_mail_conf_long().
/* DIAGNOSTICS
/*	Fatal errors: malformed numerical value.
/* SEE ALSO
/*	config(3) general configuration
/*	mail_conf_str(3) string-valued configuration parameters
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <stdlib.h>
#include <stdio.h>			/* BUFSIZ */
#include <errno.h>

/* Utility library. */

#include <msg.h>
#include <mymalloc.h>
#include <dict.h>
#include <stringops.h>

/* Global library. */

#include "mail_conf.h"

/* convert_mail_conf_long - look up and convert integer parameter value */

static int convert_mail_conf_long(const char *name, long *longval)
{
    const char *strval;
    char   *end;

    if ((strval = mail_conf_lookup_eval(name)) != 0) {
	errno = 0;
	*longval = strtol(strval, &end, 10);
	if (*strval == 0 || *end != 0 || errno == ERANGE)
	    msg_fatal("bad numerical configuration: %s = %s", name, strval);
	return (1);
    }
    return (0);
}

/* check_mail_conf_long - validate integer value */

static void check_mail_conf_long(const char *name, long longval, long min, long max)
{
    if (min && longval < min)
	msg_fatal("invalid %s parameter value %ld < %ld", name, longval, min);
    if (max && longval > max)
	msg_fatal("invalid %s parameter value %ld > %ld", name, longval, max);
}

/* get_mail_conf_long - evaluate integer-valued configuration variable */

long    get_mail_conf_long(const char *name, long defval, long min, long max)
{
    long    longval;

    if (convert_mail_conf_long(name, &longval) == 0)
	set_mail_conf_long(name, longval = defval);
    check_mail_conf_long(name, longval, min, max);
    return (longval);
}

/* get_mail_conf_long2 - evaluate integer-valued configuration variable */

long    get_mail_conf_long2(const char *name1, const char *name2, long defval,
			            long min, long max)
{
    long    longval;
    char   *name;

    name = concatenate(name1, name2, (char *) 0);
    if (convert_mail_conf_long(name, &longval) == 0)
	set_mail_conf_long(name, longval = defval);
    check_mail_conf_long(name, longval, min, max);
    myfree(name);
    return (longval);
}

/* get_mail_conf_long_fn - evaluate integer-valued configuration variable */

typedef long (*stupid_indent_long) (void);

long    get_mail_conf_long_fn(const char *name, stupid_indent_long defval,
			              long min, long max)
{
    long    longval;

    if (convert_mail_conf_long(name, &longval) == 0)
	set_mail_conf_long(name, longval = defval());
    check_mail_conf_long(name, longval, min, max);
    return (longval);
}

/* set_mail_conf_long - update integer-valued configuration dictionary entry */

void    set_mail_conf_long(const char *name, long value)
{
    const char myname[] = "set_mail_conf_long";
    char    buf[BUFSIZ];		/* yeah! crappy code! */

#ifndef NO_SNPRINTF
    ssize_t ret;

    ret = snprintf(buf, sizeof(buf), "%ld", value);
    if (ret < 0)
	msg_panic("%s: output error for %%ld", myname);
    if (ret >= sizeof(buf))
	msg_panic("%s: output for %%ld exceeds space %ld",
		  myname, (long) sizeof(buf));
#else
    sprintf(buf, "%ld", value);			/* yeah! more crappy code! */
#endif
    mail_conf_update(name, buf);
}

/* get_mail_conf_long_table - look up table of integers */

void    get_mail_conf_long_table(const CONFIG_LONG_TABLE *table)
{
    while (table->name) {
	table->target[0] = get_mail_conf_long(table->name, table->defval,
					      table->min, table->max);
	table++;
    }
}

/* get_mail_conf_long_fn_table - look up integers, defaults are functions */

void    get_mail_conf_long_fn_table(const CONFIG_LONG_FN_TABLE *table)
{
    while (table->name) {
	table->target[0] = get_mail_conf_long_fn(table->name, table->defval,
						 table->min, table->max);
	table++;
    }
}