Postfix3.3.1
mail_conf_str.c
[詳解]
1 /*++
2 /* NAME
3 /* mail_conf_str 3
4 /* SUMMARY
5 /* string-valued global configuration parameter support
6 /* SYNOPSIS
7 /* #include <mail_conf.h>
8 /*
9 /* char *get_mail_conf_str(name, defval, min, max)
10 /* const char *name;
11 /* const char *defval;
12 /* int min;
13 /* int max;
14 /*
15 /* char *get_mail_conf_str_fn(name, defval, min, max)
16 /* const char *name;
17 /* const char *(*defval)(void);
18 /* int min;
19 /* int max;
20 /*
21 /* void set_mail_conf_str(name, value)
22 /* const char *name;
23 /* const char *value;
24 /*
25 /* void get_mail_conf_str_table(table)
26 /* const CONFIG_STR_TABLE *table;
27 /*
28 /* void get_mail_conf_str_fn_table(table)
29 /* const CONFIG_STR_TABLE *table;
30 /* AUXILIARY FUNCTIONS
31 /* char *get_mail_conf_str2(name, suffix, defval, min, max)
32 /* const char *name;
33 /* const char *suffix;
34 /* const char *defval;
35 /* int min;
36 /* int max;
37 /*
38 /* void check_mail_conf_str(name, strval, min, max)
39 /* const char *name;
40 /* const char *strval;
41 /* int min;
42 /* int max;
43 /* DESCRIPTION
44 /* This module implements support for string-valued global
45 /* configuration parameters.
46 /*
47 /* get_mail_conf_str() looks up the named entry in the global
48 /* configuration dictionary. The default value is returned when
49 /* no value was found. String results should be passed to myfree()
50 /* when no longer needed. \fImin\fR is zero or specifies a lower
51 /* bound on the string length; \fImax\fR is zero or specifies an
52 /* upper limit on the string length.
53 /*
54 /* get_mail_conf_str_fn() is similar but specifies a function that
55 /* provides the default value. The function is called only when
56 /* the default value is used.
57 /*
58 /* set_mail_conf_str() updates the named entry in the global
59 /* configuration dictionary. This has no effect on values that
60 /* have been looked up earlier via the get_mail_conf_XXX() routines.
61 /*
62 /* get_mail_conf_str_table() and get_mail_conf_str_fn_table() read
63 /* lists of variables, as directed by their table arguments. A table
64 /* must be terminated by a null entry.
65 /*
66 /* get_mail_conf_str2() concatenates the two names and is otherwise
67 /* identical to get_mail_conf_str().
68 /*
69 /* check_mail_conf_str() exits with a fatal run-time error
70 /* when the string does not meet its length requirements.
71 /* DIAGNOSTICS
72 /* Fatal errors: bad string length.
73 /* SEE ALSO
74 /* config(3) generic config parameter support
75 /* LICENSE
76 /* .ad
77 /* .fi
78 /* The Secure Mailer license must be distributed with this software.
79 /* AUTHOR(S)
80 /* Wietse Venema
81 /* IBM T.J. Watson Research
82 /* P.O. Box 704
83 /* Yorktown Heights, NY 10598, USA
84 /*--*/
85 
86 /* System library. */
87 
88 #include <sys_defs.h>
89 #include <stdlib.h>
90 #include <string.h>
91 
92 /* Utility library. */
93 
94 #include <msg.h>
95 #include <mymalloc.h>
96 #include <stringops.h>
97 
98 /* Global library. */
99 
100 #include "mail_conf.h"
101 
102 /* check_mail_conf_str - validate string length */
103 
104 void check_mail_conf_str(const char *name, const char *strval,
105  int min, int max)
106 {
107  ssize_t len = strlen(strval);
108 
109  if (min && len < min)
110  msg_fatal("bad string length %ld < %d: %s = %s",
111  (long) len, min, name, strval);
112  if (max && len > max)
113  msg_fatal("bad string length %ld > %d: %s = %s",
114  (long) len, max, name, strval);
115 }
116 
117 /* get_mail_conf_str - evaluate string-valued configuration variable */
118 
119 char *get_mail_conf_str(const char *name, const char *defval,
120  int min, int max)
121 {
122  const char *strval;
123 
124  if ((strval = mail_conf_lookup_eval(name)) == 0) {
125  strval = mail_conf_eval(defval);
126  mail_conf_update(name, strval);
127  }
128  check_mail_conf_str(name, strval, min, max);
129  return (mystrdup(strval));
130 }
131 
132 /* get_mail_conf_str2 - evaluate string-valued configuration variable */
133 
134 char *get_mail_conf_str2(const char *name1, const char *name2,
135  const char *defval,
136  int min, int max)
137 {
138  const char *strval;
139  char *name;
140 
141  name = concatenate(name1, name2, (char *) 0);
142  if ((strval = mail_conf_lookup_eval(name)) == 0) {
143  strval = mail_conf_eval(defval);
144  mail_conf_update(name, strval);
145  }
146  check_mail_conf_str(name, strval, min, max);
147  myfree(name);
148  return (mystrdup(strval));
149 }
150 
151 /* get_mail_conf_str_fn - evaluate string-valued configuration variable */
152 
153 typedef const char *(*stupid_indent_str) (void);
154 
155 char *get_mail_conf_str_fn(const char *name, stupid_indent_str defval,
156  int min, int max)
157 {
158  const char *strval;
159 
160  if ((strval = mail_conf_lookup_eval(name)) == 0) {
161  strval = mail_conf_eval(defval());
162  mail_conf_update(name, strval);
163  }
164  check_mail_conf_str(name, strval, min, max);
165  return (mystrdup(strval));
166 }
167 
168 /* set_mail_conf_str - update string-valued configuration dictionary entry */
169 
170 void set_mail_conf_str(const char *name, const char *value)
171 {
172  mail_conf_update(name, value);
173 }
174 
175 /* get_mail_conf_str_table - look up table of strings */
176 
178 {
179  while (table->name) {
180  if (table->target[0])
181  myfree(table->target[0]);
182  table->target[0] = get_mail_conf_str(table->name, table->defval,
183  table->min, table->max);
184  table++;
185  }
186 }
187 
188 /* get_mail_conf_str_fn_table - look up strings, defaults are functions */
189 
191 {
192  while (table->name) {
193  if (table->target[0])
194  myfree(table->target[0]);
195  table->target[0] = get_mail_conf_str_fn(table->name, table->defval,
196  table->min, table->max);
197  table++;
198  }
199 }
void myfree(void *ptr)
Definition: mymalloc.c:207
char * mystrdup(const char *str)
Definition: mymalloc.c:225
const char * mail_conf_eval(const char *string)
Definition: mail_conf.c:237
const char * defval
Definition: mail_conf.h:102
char * get_mail_conf_str(const char *name, const char *defval, int min, int max)
char * get_mail_conf_str2(const char *name1, const char *name2, const char *defval, int min, int max)
const char *(* stupid_indent_str)(void)
void check_mail_conf_str(const char *name, const char *strval, int min, int max)
void set_mail_conf_str(const char *name, const char *value)
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
const char * mail_conf_lookup_eval(const char *name)
Definition: mail_conf.c:262
char * concatenate(const char *arg0,...)
Definition: concatenate.c:42
void get_mail_conf_str_fn_table(const CONFIG_STR_FN_TABLE *table)
void get_mail_conf_str_table(const CONFIG_STR_TABLE *table)
char * get_mail_conf_str_fn(const char *name, stupid_indent_str defval, int min, int max)
const char * name
Definition: mail_conf.h:101
char ** target
Definition: mail_conf.h:103
void mail_conf_update(const char *key, const char *value)
Definition: mail_conf.c:275
const char *(* defval)(void)
Definition: mail_conf.h:175
const char * name
Definition: mail_conf.h:174