aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2023-12-02 11:38:44 -0900
committerDavid M. Syzdek <david@syzdek.net>2023-12-02 11:40:03 -0900
commita8592ea6a3aa6defb202184451ec97e71e5e5586 (patch)
tree3f070aab52451966f68e56f5bc674f88a273b232
parent6b1f95aa16fb73ad3689781a25127fcdd49faf88 (diff)
downloadcorset-a8592ea6a3aa6defb202184451ec97e71e5e5586.tar.gz
corset-a8592ea6a3aa6defb202184451ec97e71e5e5586.tar.bz2
corset-a8592ea6a3aa6defb202184451ec97e71e5e5586.tar.xz
corset-a8592ea6a3aa6defb202184451ec97e71e5e5586.zip
libcorset: refactoring default values
-rw-r--r--include/corset_module.h4
-rw-r--r--lib/libcorset/lmemory.c158
2 files changed, 72 insertions, 90 deletions
diff --git a/include/corset_module.h b/include/corset_module.h
index 3966b0f..d872820 100644
--- a/include/corset_module.h
+++ b/include/corset_module.h
@@ -98,6 +98,10 @@
98#define CORSET_API 0 98#define CORSET_API 0
99 99
100 100
101#define CORSET_NO 0
102#define CORSET_YES 1
103
104
101#define CORSET_MOD_SINGLE 0x0001 105#define CORSET_MOD_SINGLE 0x0001
102 106
103 107
diff --git a/lib/libcorset/lmemory.c b/lib/libcorset/lmemory.c
index b291c6f..d73bbe3 100644
--- a/lib/libcorset/lmemory.c
+++ b/lib/libcorset/lmemory.c
@@ -49,6 +49,22 @@
49#include "ldaemon.h" 49#include "ldaemon.h"
50 50
51 51
52///////////////////
53// //
54// Definitions //
55// //
56///////////////////
57// MARK: - Definitions
58
59#define CORSET_DFLT_CONFFILE (PKGCONFDIR "/corsetd.conf")
60#define CORSET_DFLT_FOREGROUND CORSET_NO
61#define CORSET_DFLT_MODDIR (PKGLIBDIR "/modules")
62#define CORSET_DFLT_PIDFILE (RUNSTATEDIR "/corsetd.pid")
63#define CORSET_DFLT_SOCKET (RUNSTATEDIR "/corsetd.sock")
64#define CORSET_DFLT_VERBOSE 0
65#define CORSET_DFLT_QUIET CORSET_NO
66
67
52///////////////// 68/////////////////
53// // 69// //
54// Variables // 70// Variables //
@@ -56,17 +72,6 @@
56///////////////// 72/////////////////
57// MARK: - Variables 73// MARK: - Variables
58 74
59static int default_cnf_init = 0;
60static corset_t default_cnf =
61{
62 .cnf_conffile = NULL,
63 .cnf_flags = 0,
64 .cnf_moddir = NULL,
65 .cnf_pidfile = NULL,
66 .cnf_sockfile = NULL,
67 .cnf_verbose = 0,
68};
69
70 75
71////////////////// 76//////////////////
72// // 77// //
@@ -77,7 +82,15 @@ static corset_t default_cnf =
77 82
78static int 83static int
79corset_defaults( 84corset_defaults(
80 corset_t * cnf ); 85 corset_t * ch );
86
87
88static void
89corset_set_param_flag(
90 corset_t * ch,
91 unsigned flag,
92 int dflt,
93 const int * valp );
81 94
82 95
83///////////////// 96/////////////////
@@ -89,52 +102,16 @@ corset_defaults(
89 102
90int 103int
91corset_defaults( 104corset_defaults(
92 corset_t * cnf ) 105 corset_t * ch )
93{ 106{
94 int rc; 107 int rc;
95 int ival; 108 if ((rc = corset_set_param(ch, CORSET_OPT_CONFFILE, NULL))) return(rc);
96 const char * str; 109 if ((rc = corset_set_param(ch, CORSET_OPT_FOREGROUND, NULL))) return(rc);
97 char buff[128]; 110 if ((rc = corset_set_param(ch, CORSET_OPT_MODDIR, NULL))) return(rc);
98 111 if ((rc = corset_set_param(ch, CORSET_OPT_PIDFILE, NULL))) return(rc);
99 default_cnf_init = 1; 112 if ((rc = corset_set_param(ch, CORSET_OPT_SOCKET, NULL))) return(rc);
100 cnf = ((cnf)) ? cnf : &default_cnf; 113 if ((rc = corset_set_param(ch, CORSET_OPT_VERBOSE, NULL))) return(rc);
101 114 if ((rc = corset_set_param(ch, CORSET_OPT_QUIET, NULL))) return(rc);
102 // copy flags
103 cnf->cnf_flags = default_cnf.cnf_flags;
104
105 // CORSET_OPT_CONFFILE
106 str = ((default_cnf.cnf_conffile)) ? default_cnf.cnf_conffile : buff;
107 if (str == buff)
108 snprintf(buff, sizeof(buff), "%s/%s.conf", PKGCONFDIR, corset_prog_name_ptr);
109 if ((rc = corset_set_param(cnf, CORSET_OPT_CONFFILE, str)) != 0)
110 return(rc);
111
112 // CORSET_OPT_MODDIR
113 str = ((default_cnf.cnf_moddir)) ? default_cnf.cnf_moddir : buff;
114 if (str == buff)
115 snprintf(buff, sizeof(buff), "%s/modules", PKGLIBDIR);
116 if ((rc = corset_set_param(cnf, CORSET_OPT_MODDIR, str)) != 0)
117 return(rc);
118
119 // CORSET_OPT_PIDFILE
120 str = ((default_cnf.cnf_pidfile)) ? default_cnf.cnf_pidfile : buff;
121 if (str == buff)
122 snprintf(buff, sizeof(buff), "%s/%s.pid", RUNSTATEDIR, corset_prog_name_ptr);
123 if ((rc = corset_set_param(cnf, CORSET_OPT_PIDFILE, str)) != 0)
124 return(rc);
125
126 // CORSET_OPT_SOCKET
127 str = ((default_cnf.cnf_sockfile)) ? default_cnf.cnf_sockfile : buff;
128 if (str == buff)
129 snprintf(buff, sizeof(buff), "%s/%s.sock", RUNSTATEDIR, corset_prog_name_ptr);
130 if ((rc = corset_set_param(cnf, CORSET_OPT_SOCKET, str)) != 0)
131 return(rc);
132
133 // CORSET_OPT_VERBOSE
134 ival = default_cnf.cnf_verbose;
135 if ((rc = corset_set_param(cnf, CORSET_OPT_VERBOSE, &ival)) != 0)
136 return(rc);
137
138 return(0); 115 return(0);
139} 116}
140 117
@@ -160,16 +137,8 @@ corset_get_param(
160 int option, 137 int option,
161 void * outvalue ) 138 void * outvalue )
162{ 139{
163 int rc;
164 char * str; 140 char * str;
165 141
166 cnf = ((cnf)) ? cnf : &default_cnf;
167
168 // initialize defaults
169 if (!(default_cnf_init))
170 if ((rc = corset_defaults(NULL)) != 0)
171 return(rc);
172
173 switch(option) 142 switch(option)
174 { 143 {
175 case CORSET_OPT_CONFFILE: 144 case CORSET_OPT_CONFFILE:
@@ -220,19 +189,14 @@ corset_t *
220corset_init( 189corset_init(
221 void ) 190 void )
222{ 191{
223 int rc;
224 corset_t * cnf; 192 corset_t * cnf;
225 193
226 // initialize defaults
227 if (!(default_cnf_init))
228 if ((rc = corset_defaults(NULL)) != 0)
229 return(NULL);
230
231 // initialize memory 194 // initialize memory
232 if ((cnf = malloc(sizeof(corset_t))) == NULL) 195 if ((cnf = malloc(sizeof(corset_t))) == NULL)
233 return(NULL); 196 return(NULL);
234 memset(cnf, 0, sizeof(corset_t)); 197 memset(cnf, 0, sizeof(corset_t));
235 if ((rc = corset_defaults(cnf)) != 0) 198
199 if ((corset_defaults(cnf)))
236 { 200 {
237 corset_destroy(cnf); 201 corset_destroy(cnf);
238 return(NULL); 202 return(NULL);
@@ -248,19 +212,12 @@ corset_set_param(
248 int option, 212 int option,
249 const void * invalue ) 213 const void * invalue )
250{ 214{
251 int rc;
252 char * str; 215 char * str;
253 216
254 cnf = ((cnf)) ? cnf : &default_cnf;
255
256 // initialize defaults
257 if (!(default_cnf_init))
258 if ((rc = corset_defaults(NULL)) != 0)
259 return(rc);
260
261 switch(option) 217 switch(option)
262 { 218 {
263 case CORSET_OPT_CONFFILE: 219 case CORSET_OPT_CONFFILE:
220 invalue = ((invalue)) ? invalue : CORSET_DFLT_CONFFILE;
264 if ((str = bindle_strdup(invalue)) == NULL) 221 if ((str = bindle_strdup(invalue)) == NULL)
265 return(-1); 222 return(-1);
266 if ((cnf->cnf_conffile)) 223 if ((cnf->cnf_conffile))
@@ -269,14 +226,11 @@ corset_set_param(
269 return(0); 226 return(0);
270 227
271 case CORSET_OPT_FOREGROUND: 228 case CORSET_OPT_FOREGROUND:
272 if ((invalue)) 229 corset_set_param_flag(cnf, CORSET_FLG_FOREGROUND, CORSET_DFLT_FOREGROUND, invalue);
273 cnf->cnf_flags |= CORSET_FLG_FOREGROUND;
274 if (!(invalue))
275 cnf->cnf_flags &= ~CORSET_FLG_FOREGROUND;
276 return(0); 230 return(0);
277 231
278 case CORSET_OPT_MODDIR: 232 case CORSET_OPT_MODDIR:
279 invalue = ((invalue)) ? invalue : default_cnf.cnf_moddir; 233 invalue = ((invalue)) ? invalue : CORSET_DFLT_MODDIR;
280 if ((str = bindle_strdup(invalue)) == NULL) 234 if ((str = bindle_strdup(invalue)) == NULL)
281 return(-1); 235 return(-1);
282 if ((cnf->cnf_moddir)) 236 if ((cnf->cnf_moddir))
@@ -285,6 +239,7 @@ corset_set_param(
285 return(0); 239 return(0);
286 240
287 case CORSET_OPT_PIDFILE: 241 case CORSET_OPT_PIDFILE:
242 invalue = ((invalue)) ? invalue : CORSET_DFLT_PIDFILE;
288 if ((str = bindle_strdup(invalue)) == NULL) 243 if ((str = bindle_strdup(invalue)) == NULL)
289 return(-1); 244 return(-1);
290 if ((cnf->cnf_pidfile)) 245 if ((cnf->cnf_pidfile))
@@ -293,13 +248,11 @@ corset_set_param(
293 return(0); 248 return(0);
294 249
295 case CORSET_OPT_QUIET: 250 case CORSET_OPT_QUIET:
296 if ((invalue)) 251 corset_set_param_flag(cnf, CORSET_FLG_QUIET, CORSET_DFLT_QUIET, invalue);
297 cnf->cnf_flags |= CORSET_FLG_QUIET;
298 if (!(invalue))
299 cnf->cnf_flags &= ~CORSET_FLG_QUIET;
300 return(0); 252 return(0);
301 253
302 case CORSET_OPT_SOCKET: 254 case CORSET_OPT_SOCKET:
255 invalue = ((invalue)) ? invalue : CORSET_DFLT_SOCKET;
303 if ((str = bindle_strdup(invalue)) == NULL) 256 if ((str = bindle_strdup(invalue)) == NULL)
304 return(-1); 257 return(-1);
305 if ((cnf->cnf_sockfile)) 258 if ((cnf->cnf_sockfile))
@@ -308,7 +261,7 @@ corset_set_param(
308 return(0); 261 return(0);
309 262
310 case CORSET_OPT_VERBOSE: 263 case CORSET_OPT_VERBOSE:
311 cnf->cnf_verbose = ((invalue)) ? *((const int *)invalue) : default_cnf.cnf_verbose; 264 cnf->cnf_verbose = ((invalue)) ? *((const int *)invalue) : CORSET_DFLT_VERBOSE;
312 return(0); 265 return(0);
313 266
314 default: 267 default:
@@ -319,4 +272,29 @@ corset_set_param(
319} 272}
320 273
321 274
275void
276corset_set_param_flag(
277 corset_t * ch,
278 unsigned flag,
279 int dflt,
280 const int * valp )
281{
282 int ival;
283
284 assert(ch != NULL);
285
286 if (!(valp))
287 {
288 ival = dflt;
289 valp = &ival;
290 };
291
292 if ((*valp))
293 ch->cnf_flags |= flag;
294 else
295 ch->cnf_flags &= ~flag;
296
297 return;
298}
299
322/* end of source file */ 300/* end of source file */