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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Unix SMB/CIFS implementation.
* Copyright (C) Jelmer Vernooij 2005
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "lib/util/dlinklist.h"
#include "param/param.h"
#include "system/filesys.h"
struct param_section *param_get_section(struct param_context *ctx, const char *name)
{
struct param_section *sect;
if (name == NULL)
name = GLOBAL_NAME;
for (sect = ctx->sections; sect; sect = sect->next) {
if (!strcasecmp_m(sect->name, name))
return sect;
}
return NULL;
}
struct param_opt *param_section_get(struct param_section *section,
const char *name)
{
struct param_opt *p;
for (p = section->parameters; p; p = p->next) {
if (strcasecmp_m(p->key, name) == 0)
return p;
}
return NULL;
}
struct param_opt *param_get (struct param_context *ctx, const char *section_name, const char *name)
{
struct param_section *section = param_get_section(ctx, section_name);
if (section == NULL)
return NULL;
return param_section_get(section, name);
}
/* Look up parameter. If it is not found, add it */
static struct param_opt *param_get_add(struct param_context *ctx, const char *section_name, const char *name)
{
struct param_section *section;
struct param_opt *p;
section = param_get_section(ctx, section_name);
if (section == NULL) {
section = talloc_zero(ctx, struct param_section);
if (section == NULL)
return NULL;
section->name = talloc_strdup(section, section_name);
DLIST_ADD(ctx->sections, section);
}
p = param_section_get(section, name);
if (p == NULL) {
p = talloc_zero(section, struct param_opt);
if (p == NULL)
return NULL;
p->key = talloc_strdup(p, name);
DLIST_ADD(section->parameters, p);
}
return p;
}
const char *param_get_string(struct param_context *ctx, const char *section, const char *param)
{
struct param_opt *p = param_get(ctx, section, param);
if (p == NULL)
return NULL;
return p->value;
}
int param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value)
{
struct param_opt *p = param_get_add(ctx, section, param);
if (p == NULL)
return -1;
p->value = talloc_strdup(p, value);
return 0;
}
const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param,
const char *separator)
{
struct param_opt *p = param_get(ctx, section, param);
if (p == NULL)
return NULL;
if (separator == NULL)
separator = LIST_SEP;
return str_list_make(ctx, p->value, separator);
}
int param_set_string_list(struct param_context *ctx, const char *section, const char *param, const char **list)
{
struct param_opt *p = param_get_add(ctx, section, param);
p->value = str_list_join(p, list, ' ');
return 0;
}
int param_get_int(struct param_context *ctx, const char *section, const char *param, int default_v)
{
const char *value = param_get_string(ctx, section, param);
if (value)
return strtol(value, NULL, 0);
return default_v;
}
void param_set_int(struct param_context *ctx, const char *section, const char *param, int value)
{
struct param_opt *p = param_get_add(ctx, section, param);
if (!p)
return;
p->value = talloc_asprintf(p, "%d", value);
}
unsigned long param_get_ulong(struct param_context *ctx, const char *section, const char *param, unsigned long default_v)
{
const char *value = param_get_string(ctx, section, param);
if (value)
return strtoul(value, NULL, 0);
return default_v;
}
void param_set_ulong(struct param_context *ctx, const char *section, const char *name, unsigned long value)
{
struct param_opt *p = param_get_add(ctx, section, name);
if (!p)
return;
p->value = talloc_asprintf(p, "%lu", value);
}
static bool param_sfunc (const char *name, void *_ctx)
{
struct param_context *ctx = (struct param_context *)_ctx;
struct param_section *section = param_get_section(ctx, name);
if (section == NULL) {
section = talloc_zero(ctx, struct param_section);
if (section == NULL)
return false;
section->name = talloc_strdup(section, name);
DLIST_ADD(ctx->sections, section);
}
/* Make sure this section is on top of the list for param_pfunc */
DLIST_PROMOTE(ctx->sections, section);
return true;
}
static bool param_pfunc (const char *name, const char *value, void *_ctx)
{
struct param_context *ctx = (struct param_context *)_ctx;
struct param_opt *p = param_section_get(ctx->sections, name);
if (!p) {
p = talloc_zero(ctx->sections, struct param_opt);
if (p == NULL)
return False;
p->key = talloc_strdup(p, name);
p->value = talloc_strdup(p, value);
DLIST_ADD(ctx->sections->parameters, p);
} else { /* Replace current value */
talloc_free(p->value);
p->value = talloc_strdup(p, value);
}
return True;
}
struct param_context *param_init(TALLOC_CTX *mem_ctx)
{
return talloc_zero(mem_ctx, struct param_context);
}
int param_read(struct param_context *ctx, const char *fn)
{
ctx->sections = talloc_zero(ctx, struct param_section);
if (ctx->sections == NULL)
return -1;
ctx->sections->name = talloc_strdup(ctx->sections, "global");
if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
return -1;
}
return 0;
}
int param_write(struct param_context *ctx, const char *fn)
{
int file;
struct param_section *section;
if (fn == NULL || ctx == NULL)
return -1;
file = open(fn, O_WRONLY|O_CREAT, 0755);
if (file == -1)
return -1;
for (section = ctx->sections; section; section = section->next) {
struct param_opt *param;
fdprintf(file, "[%s]\n", section->name);
for (param = section->parameters; param; param = param->next) {
fdprintf(file, "\t%s = %s\n", param->key, param->value);
}
fdprintf(file, "\n");
}
close(file);
return 0;
}
|