1/*++
2/* NAME
3/*	mail_conf_long 3
4/* SUMMARY
5/*	long integer-valued configuration parameter support
6/* SYNOPSIS
7/*	#include <mail_conf.h>
8/*
9/*	int	get_mail_conf_long(name, defval, min, max);
10/*	const char *name;
11/*	long	defval;
12/*	long	min;
13/*	long	max;
14/*
15/*	int	get_mail_conf_long_fn(name, defval, min, max);
16/*	const char *name;
17/*	long	(*defval)(void);
18/*	long	min;
19/*	long	max;
20/*
21/*	void	set_mail_conf_long(name, value)
22/*	const char *name;
23/*	long	value;
24/*
25/*	void	get_mail_conf_long_table(table)
26/*	const CONFIG_LONG_TABLE *table;
27/*
28/*	void	get_mail_conf_long_fn_table(table)
29/*	const CONFIG_LONG_TABLE *table;
30/* AUXILIARY FUNCTIONS
31/*	int	get_mail_conf_long2(name1, name2, defval, min, max);
32/*	const char *name1;
33/*	const char *name2;
34/*	long	defval;
35/*	long	min;
36/*	long	max;
37/* DESCRIPTION
38/*	This module implements configuration parameter support
39/*	for long integer values.
40/*
41/*	get_mail_conf_long() looks up the named entry in the global
42/*	configuration dictionary. The default value is returned
43/*	when no value was found.
44/*	\fImin\fR is zero or specifies a lower limit on the long
45/*	integer value; \fImax\fR is zero or specifies an upper limit
46/*	on the long integer value.
47/*
48/*	get_mail_conf_long_fn() is similar but specifies a function that
49/*	provides the default value. The function is called only
50/*	when the default value is needed.
51/*
52/*	set_mail_conf_long() updates the named entry in the global
53/*	configuration dictionary. This has no effect on values that
54/*	have been looked up earlier via the get_mail_conf_XXX() routines.
55/*
56/*	get_mail_conf_long_table() and get_mail_conf_long_fn_table() initialize
57/*	lists of variables, as directed by their table arguments. A table
58/*	must be terminated by a null entry.
59/*
60/*	get_mail_conf_long2() concatenates the two names and is otherwise
61/*	identical to get_mail_conf_long().
62/* DIAGNOSTICS
63/*	Fatal errors: malformed numerical value.
64/* SEE ALSO
65/*	config(3) general configuration
66/*	mail_conf_str(3) string-valued configuration parameters
67/* LICENSE
68/* .ad
69/* .fi
70/*	The Secure Mailer license must be distributed with this software.
71/* AUTHOR(S)
72/*	Wietse Venema
73/*	IBM T.J. Watson Research
74/*	P.O. Box 704
75/*	Yorktown Heights, NY 10598, USA
76/*--*/
77
78/* System library. */
79
80#include <sys_defs.h>
81#include <stdlib.h>
82#include <stdio.h>			/* BUFSIZ */
83#include <errno.h>
84
85/* Utility library. */
86
87#include <msg.h>
88#include <mymalloc.h>
89#include <dict.h>
90#include <stringops.h>
91
92/* Global library. */
93
94#include "mail_conf.h"
95
96/* convert_mail_conf_long - look up and convert integer parameter value */
97
98static int convert_mail_conf_long(const char *name, long *longval)
99{
100    const char *strval;
101    char   *end;
102
103    if ((strval = mail_conf_lookup_eval(name)) != 0) {
104	errno = 0;
105	*longval = strtol(strval, &end, 10);
106	if (*strval == 0 || *end != 0 || errno == ERANGE)
107	    msg_fatal("bad numerical configuration: %s = %s", name, strval);
108	return (1);
109    }
110    return (0);
111}
112
113/* check_mail_conf_long - validate integer value */
114
115static void check_mail_conf_long(const char *name, long longval, long min, long max)
116{
117    if (min && longval < min)
118	msg_fatal("invalid %s parameter value %ld < %ld", name, longval, min);
119    if (max && longval > max)
120	msg_fatal("invalid %s parameter value %ld > %ld", name, longval, max);
121}
122
123/* get_mail_conf_long - evaluate integer-valued configuration variable */
124
125long    get_mail_conf_long(const char *name, long defval, long min, long max)
126{
127    long    longval;
128
129    if (convert_mail_conf_long(name, &longval) == 0)
130	set_mail_conf_long(name, longval = defval);
131    check_mail_conf_long(name, longval, min, max);
132    return (longval);
133}
134
135/* get_mail_conf_long2 - evaluate integer-valued configuration variable */
136
137long    get_mail_conf_long2(const char *name1, const char *name2, long defval,
138			            long min, long max)
139{
140    long    longval;
141    char   *name;
142
143    name = concatenate(name1, name2, (char *) 0);
144    if (convert_mail_conf_long(name, &longval) == 0)
145	set_mail_conf_long(name, longval = defval);
146    check_mail_conf_long(name, longval, min, max);
147    myfree(name);
148    return (longval);
149}
150
151/* get_mail_conf_long_fn - evaluate integer-valued configuration variable */
152
153typedef long (*stupid_indent_long) (void);
154
155long    get_mail_conf_long_fn(const char *name, stupid_indent_long defval,
156			              long min, long max)
157{
158    long    longval;
159
160    if (convert_mail_conf_long(name, &longval) == 0)
161	set_mail_conf_long(name, longval = defval());
162    check_mail_conf_long(name, longval, min, max);
163    return (longval);
164}
165
166/* set_mail_conf_long - update integer-valued configuration dictionary entry */
167
168void    set_mail_conf_long(const char *name, long value)
169{
170    char    buf[BUFSIZ];		/* yeah! crappy code! */
171
172    sprintf(buf, "%ld", value);			/* yeah! more crappy code! */
173    mail_conf_update(name, buf);
174}
175
176/* get_mail_conf_long_table - look up table of integers */
177
178void    get_mail_conf_long_table(const CONFIG_LONG_TABLE *table)
179{
180    while (table->name) {
181	table->target[0] = get_mail_conf_long(table->name, table->defval,
182					      table->min, table->max);
183	table++;
184    }
185}
186
187/* get_mail_conf_long_fn_table - look up integers, defaults are functions */
188
189void    get_mail_conf_long_fn_table(const CONFIG_LONG_FN_TABLE *table)
190{
191    while (table->name) {
192	table->target[0] = get_mail_conf_long_fn(table->name, table->defval,
193						 table->min, table->max);
194	table++;
195    }
196}
197