blob: fc682103935384925dc64799f9a7f66ef9385825 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Configuration.cxx
20
21#include <stdlib.h>
22#include <ctype.h>
23#include <string.h>
24#include <assert.h>
25#ifdef WIN32
26#define strcasecmp _stricmp
27#define strncasecmp _strnicmp
28#endif
29
30#include <rfb/util.h>
31#include <rfb/Configuration.h>
32#include <rfb/LogWriter.h>
33#include <rfb/Exception.h>
34
35#ifdef WIN32
36
37// Under Win32, we use these routines from several threads,
38// so we must provide suitable locking.
39#include <rfb/Threading.h>
40
41static rfb::Mutex configLock;
42
43#endif
44
45#include <rdr/HexOutStream.h>
46#include <rdr/HexInStream.h>
47
48using namespace rfb;
49
50static LogWriter vlog("Config");
51
52
53// -=- Configuration
54
55VoidParameter* Configuration::head = 0;
56
57bool Configuration::setParam(const char* n, const char* v, bool immutable) {
58 return setParam(n, strlen(n), v, immutable);
59}
60
61bool Configuration::setParam(const char* name, int len,
62 const char* val, bool immutable)
63{
64 VoidParameter* current = head;
65 while (current) {
66 if ((int)strlen(current->getName()) == len &&
67 strncasecmp(current->getName(), name, len) == 0)
68 {
69 bool b = current->setParam(val);
Peter Åstrand55855d52005-01-03 12:01:45 +000070 current->setHasBeenSet();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000071 if (b && immutable)
72 current->setImmutable();
73 return b;
74 }
75 current = current->_next;
76 }
77 return false;
78}
79
80bool Configuration::setParam(const char* config, bool immutable) {
81 bool hyphen = false;
82 if (config[0] == '-') {
83 hyphen = true;
84 config++;
85 if (config[0] == '-') config++; // allow gnu-style --<option>
86 }
87 const char* equal = strchr(config, '=');
88 if (equal) {
89 return setParam(config, equal-config, equal+1, immutable);
90 } else if (hyphen) {
91 VoidParameter* current = head;
92 while (current) {
93 if (strcasecmp(current->getName(), config) == 0) {
94 bool b = current->setParam();
Peter Åstrand55855d52005-01-03 12:01:45 +000095 current->setHasBeenSet();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000096 if (b && immutable)
97 current->setImmutable();
98 return b;
99 }
100 current = current->_next;
101 }
102 }
103 return false;
104}
105
106VoidParameter* Configuration::getParam(const char* param)
107{
108 VoidParameter* current = head;
109 while (current) {
110 if (strcasecmp(current->getName(), param) == 0)
111 return current;
112 current = current->_next;
113 }
114 return 0;
115}
116
117void Configuration::listParams(int width, int nameWidth) {
118 VoidParameter* current = head;
119 while (current) {
120 char* def_str = current->getDefaultStr();
121 const char* desc = current->getDescription();
122 fprintf(stderr," %-*s -", nameWidth, current->getName());
123 int column = strlen(current->getName());
124 if (column < nameWidth) column = nameWidth;
125 column += 4;
126 while (true) {
127 const char* s = strchr(desc, ' ');
128 int wordLen;
129 if (s) wordLen = s-desc;
130 else wordLen = strlen(desc);
131
132 if (column + wordLen + 1 > width) {
133 fprintf(stderr,"\n%*s",nameWidth+4,"");
134 column = nameWidth+4;
135 }
136 fprintf(stderr," %.*s",wordLen,desc);
137 column += wordLen + 1;
138 desc += wordLen + 1;
139 if (!s) break;
140 }
141
142 if (def_str) {
143 if (column + (int)strlen(def_str) + 11 > width)
144 fprintf(stderr,"\n%*s",nameWidth+4,"");
145 fprintf(stderr," (default=%s)\n",def_str);
146 strFree(def_str);
147 } else {
148 fprintf(stderr,"\n");
149 }
150 current = current->_next;
151 }
152}
153
154// -=- VoidParameter
155
156VoidParameter::VoidParameter(const char* name_, const char* desc_)
Peter Åstrand55855d52005-01-03 12:01:45 +0000157 : immutable(false), _hasBeenSet(false), name(name_), description(desc_) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000158 _next = Configuration::head;
159 Configuration::head = this;
160}
161
162VoidParameter::~VoidParameter() {
163}
164
165const char*
166VoidParameter::getName() const {
167 return name;
168}
169
170const char*
171VoidParameter::getDescription() const {
172 return description;
173}
174
175bool VoidParameter::setParam() {
176 return false;
177}
178
179bool VoidParameter::isBool() const {
180 return false;
181}
182
183void
184VoidParameter::setImmutable() {
185 vlog.debug("set immutable %s", getName());
186 immutable = true;
187}
188
Peter Åstrand55855d52005-01-03 12:01:45 +0000189void
190VoidParameter::setHasBeenSet() {
191 _hasBeenSet = true;
192}
193
194bool
195VoidParameter::hasBeenSet() {
196 return _hasBeenSet;
197}
198
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000199// -=- AliasParameter
200
201AliasParameter::AliasParameter(const char* name_, const char* desc_,
202 VoidParameter* param_)
203 : VoidParameter(name_, desc_), param(param_) {
204}
205
206bool
207AliasParameter::setParam(const char* v) {
208 return param->setParam(v);
209}
210
211bool AliasParameter::setParam() {
212 return param->setParam();
213}
214
215char*
216AliasParameter::getDefaultStr() const {
217 return 0;
218}
219
220char* AliasParameter::getValueStr() const {
221 return param->getValueStr();
222}
223
224bool AliasParameter::isBool() const {
225 return param->isBool();
226}
227
228// -=- BoolParameter
229
230BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v)
231: VoidParameter(name_, desc_), value(v), def_value(v) {
232}
233
234bool
235BoolParameter::setParam(const char* v) {
236 if (immutable) return true;
237
238 if (*v == 0 || strcasecmp(v, "1") == 0 || strcasecmp(v, "on") == 0
239 || strcasecmp(v, "true") == 0 || strcasecmp(v, "yes") == 0)
240 value = 1;
241 else if (strcasecmp(v, "0") == 0 || strcasecmp(v, "off") == 0
242 || strcasecmp(v, "false") == 0 || strcasecmp(v, "no") == 0)
243 value = 0;
244 else {
245 vlog.error("Bool parameter %s: invalid value '%s'", getName(), v);
246 return false;
247 }
248
249 vlog.debug("set %s(Bool) to %s(%d)", getName(), v, value);
250 return true;
251}
252
253bool BoolParameter::setParam() {
254 setParam(true);
255 return true;
256}
257
258void BoolParameter::setParam(bool b) {
259 if (immutable) return;
260 value = b;
261 vlog.debug("set %s(Bool) to %d", getName(), value);
262}
263
264char*
265BoolParameter::getDefaultStr() const {
266 char* result = new char[8];
267 sprintf(result, "%d", (int)def_value);
268 return result;
269}
270
271char* BoolParameter::getValueStr() const {
272 char* result = new char[8];
273 sprintf(result, "%d", (int)value);
274 return result;
275}
276
277bool BoolParameter::isBool() const {
278 return true;
279}
280
281BoolParameter::operator bool() const {
282 return value;
283}
284
285// -=- IntParameter
286
287IntParameter::IntParameter(const char* name_, const char* desc_, int v)
288: VoidParameter(name_, desc_), value(v), def_value(v) {
289}
290
291bool
292IntParameter::setParam(const char* v) {
293 if (immutable) return true;
294 vlog.debug("set %s(Int) to %s", getName(), v);
295 value = atoi(v);
296 return true;
297}
298
299bool
300IntParameter::setParam(int v) {
301 if (immutable) return true;
302 vlog.debug("set %s(Int) to %d", getName(), v);
303 value = v;
304 return true;
305}
306
307char*
308IntParameter::getDefaultStr() const {
309 char* result = new char[16];
310 sprintf(result, "%d", def_value);
311 return result;
312}
313
314char* IntParameter::getValueStr() const {
315 char* result = new char[16];
316 sprintf(result, "%d", value);
317 return result;
318}
319
320IntParameter::operator int() const {
321 return value;
322}
323
324// -=- StringParameter
325
326StringParameter::StringParameter(const char* name_, const char* desc_,
327 const char* v)
328 : VoidParameter(name_, desc_), value(strDup(v)), def_value(v)
329{
330 if (!v) {
331 fprintf(stderr,"Default value <null> for %s not allowed\n",name_);
332 throw rfb::Exception("Default value <null> not allowed");
333 }
334}
335
336StringParameter::~StringParameter() {
337 strFree(value);
338}
339
340bool StringParameter::setParam(const char* v) {
341#ifdef WIN32
342 Lock l(configLock);
343#endif
344 if (immutable) return true;
345 if (!v)
346 throw rfb::Exception("setParam(<null>) not allowed");
347 vlog.debug("set %s(String) to %s", getName(), v);
348 strFree(value);
349 value = strDup(v);
350 return value != 0;
351}
352
353char* StringParameter::getDefaultStr() const {
354 return strDup(def_value);
355}
356
357char* StringParameter::getValueStr() const {
358#ifdef WIN32
359 Lock l(configLock);
360#endif
361 return strDup(value);
362}
363
364// -=- BinaryParameter
365
366BinaryParameter::BinaryParameter(const char* name_, const char* desc_, const void* v, int l)
367: VoidParameter(name_, desc_), value(0), length(0), def_value((char*)v), def_length(l) {
368 if (l) {
369 value = new char[l];
370 length = l;
371 memcpy(value, v, l);
372 }
373}
374BinaryParameter::~BinaryParameter() {
375 if (value)
376 delete [] value;
377}
378
379bool BinaryParameter::setParam(const char* v) {
380#ifdef WIN32
381 Lock l(configLock);
382#endif
383 if (immutable) return true;
384 vlog.debug("set %s(Binary) to %s", getName(), v);
385 return rdr::HexInStream::hexStrToBin(v, &value, &length);
386}
387
388void BinaryParameter::setParam(const void* v, int len) {
389#ifdef WIN32
390 Lock l(configLock);
391#endif
392 if (immutable) return;
393 vlog.debug("set %s(Binary)", getName());
394 delete [] value; value = 0;
395 if (len) {
396 value = new char[len];
397 length = len;
398 memcpy(value, v, len);
399 }
400}
401
402char* BinaryParameter::getDefaultStr() const {
403 return rdr::HexOutStream::binToHexStr(def_value, def_length);
404}
405
406char* BinaryParameter::getValueStr() const {
407#ifdef WIN32
408 Lock l(configLock);
409#endif
410 return rdr::HexOutStream::binToHexStr(value, length);
411}
412
413void BinaryParameter::getData(void** data_, int* length_) const {
414#ifdef WIN32
415 Lock l(configLock);
416#endif
417 if (length_) *length_ = length;
418 if (data_) {
419 *data_ = new char[length];
420 memcpy(*data_, value, length);
421 }
422}