Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / global / mail_conf_str.c
blob4baea45416850546d18726a0e160e6824f80e97a
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* mail_conf_str 3
6 /* SUMMARY
7 /* string-valued global configuration parameter support
8 /* SYNOPSIS
9 /* #include <mail_conf.h>
11 /* char *get_mail_conf_str(name, defval, min, max)
12 /* const char *name;
13 /* const char *defval;
14 /* int min;
15 /* int max;
17 /* char *get_mail_conf_str_fn(name, defval, min, max)
18 /* const char *name;
19 /* const char *(*defval)(void);
20 /* int min;
21 /* int max;
23 /* void set_mail_conf_str(name, value)
24 /* const char *name;
25 /* const char *value;
27 /* void get_mail_conf_str_table(table)
28 /* const CONFIG_STR_TABLE *table;
30 /* void get_mail_conf_str_fn_table(table)
31 /* const CONFIG_STR_TABLE *table;
32 /* AUXILIARY FUNCTIONS
33 /* char *get_mail_conf_str2(name, suffix, defval, min, max)
34 /* const char *name;
35 /* const char *suffix;
36 /* const char *defval;
37 /* int min;
38 /* int max;
39 /* DESCRIPTION
40 /* This module implements support for string-valued global
41 /* configuration parameters.
43 /* get_mail_conf_str() looks up the named entry in the global
44 /* configuration dictionary. The default value is returned when
45 /* no value was found. String results should be passed to myfree()
46 /* when no longer needed. \fImin\fR is zero or specifies a lower
47 /* bound on the string length; \fImax\fR is zero or specifies an
48 /* upper limit on the string length.
50 /* get_mail_conf_str_fn() is similar but specifies a function that
51 /* provides the default value. The function is called only when
52 /* the default value is used.
54 /* set_mail_conf_str() updates the named entry in the global
55 /* configuration dictionary. This has no effect on values that
56 /* have been looked up earlier via the get_mail_conf_XXX() routines.
58 /* get_mail_conf_str_table() and get_mail_conf_str_fn_table() read
59 /* lists of variables, as directed by their table arguments. A table
60 /* must be terminated by a null entry.
62 /* get_mail_conf_str2() concatenates the two names and is otherwise
63 /* identical to get_mail_conf_str().
64 /* DIAGNOSTICS
65 /* Fatal errors: bad string length.
66 /* SEE ALSO
67 /* config(3) generic config parameter support
68 /* LICENSE
69 /* .ad
70 /* .fi
71 /* The Secure Mailer license must be distributed with this software.
72 /* AUTHOR(S)
73 /* Wietse Venema
74 /* IBM T.J. Watson Research
75 /* P.O. Box 704
76 /* Yorktown Heights, NY 10598, USA
77 /*--*/
79 /* System library. */
81 #include <sys_defs.h>
82 #include <stdlib.h>
83 #include <string.h>
85 /* Utility library. */
87 #include <msg.h>
88 #include <mymalloc.h>
89 #include <stringops.h>
91 /* Global library. */
93 #include "mail_conf.h"
95 /* check_mail_conf_str - validate string length */
97 static void check_mail_conf_str(const char *name, const char *strval,
98 int min, int max)
100 ssize_t len = strlen(strval);
102 if (min && len < min)
103 msg_fatal("bad string length %ld < %d: %s = %s",
104 (long) len, min, name, strval);
105 if (max && len > max)
106 msg_fatal("bad string length %ld > %d: %s = %s",
107 (long) len, max, name, strval);
110 /* get_mail_conf_str - evaluate string-valued configuration variable */
112 char *get_mail_conf_str(const char *name, const char *defval,
113 int min, int max)
115 const char *strval;
117 if ((strval = mail_conf_lookup_eval(name)) == 0) {
118 strval = mail_conf_eval(defval);
119 mail_conf_update(name, strval);
121 check_mail_conf_str(name, strval, min, max);
122 return (mystrdup(strval));
125 /* get_mail_conf_str2 - evaluate string-valued configuration variable */
127 char *get_mail_conf_str2(const char *name1, const char *name2,
128 const char *defval,
129 int min, int max)
131 const char *strval;
132 char *name;
134 name = concatenate(name1, name2, (char *) 0);
135 if ((strval = mail_conf_lookup_eval(name)) == 0) {
136 strval = mail_conf_eval(defval);
137 mail_conf_update(name, strval);
139 check_mail_conf_str(name, strval, min, max);
140 myfree(name);
141 return (mystrdup(strval));
144 /* get_mail_conf_str_fn - evaluate string-valued configuration variable */
146 typedef const char *(*stupid_indent_str) (void);
148 char *get_mail_conf_str_fn(const char *name, stupid_indent_str defval,
149 int min, int max)
151 const char *strval;
153 if ((strval = mail_conf_lookup_eval(name)) == 0) {
154 strval = mail_conf_eval(defval());
155 mail_conf_update(name, strval);
157 check_mail_conf_str(name, strval, min, max);
158 return (mystrdup(strval));
161 /* set_mail_conf_str - update string-valued configuration dictionary entry */
163 void set_mail_conf_str(const char *name, const char *value)
165 mail_conf_update(name, value);
168 /* get_mail_conf_str_table - look up table of strings */
170 void get_mail_conf_str_table(const CONFIG_STR_TABLE *table)
172 while (table->name) {
173 if (table->target[0])
174 myfree(table->target[0]);
175 table->target[0] = get_mail_conf_str(table->name, table->defval,
176 table->min, table->max);
177 table++;
181 /* get_mail_conf_str_fn_table - look up strings, defaults are functions */
183 void get_mail_conf_str_fn_table(const CONFIG_STR_FN_TABLE *table)
185 while (table->name) {
186 if (table->target[0])
187 myfree(table->target[0]);
188 table->target[0] = get_mail_conf_str_fn(table->name, table->defval,
189 table->min, table->max);
190 table++;