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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
* Copyright 2004-2005 Cendio AB.
* Copyright 2017 Peter Astrand <astrand@cendio.se> for Cendio AB
* Copyright 2011-2025 Pierre Ossman for Cendio AB
*
* This 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 2 of the License, or
* (at your option) any later version.
*
* This software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
// -=- Configuration.cxx
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <algorithm>
#include <stdexcept>
#include <core/Configuration.h>
#include <core/LogWriter.h>
#include <core/string.h>
#include <rdr/HexOutStream.h>
#include <rdr/HexInStream.h>
using namespace core;
static LogWriter vlog("Config");
// -=- The Global Configuration object
Configuration* Configuration::global_ = nullptr;
Configuration* Configuration::global() {
if (!global_)
global_ = new Configuration();
return global_;
}
// -=- Configuration implementation
bool Configuration::set(const char* paramName, const char* val,
bool immutable)
{
for (VoidParameter* current: params) {
if (strcasecmp(current->getName(), paramName) == 0) {
bool b = current->setParam(val);
if (b && immutable)
current->setImmutable();
return b;
}
}
return false;
}
VoidParameter* Configuration::get(const char* param)
{
for (VoidParameter* current: params) {
if (strcasecmp(current->getName(), param) == 0)
return current;
}
return nullptr;
}
void Configuration::list(int width, int nameWidth) {
for (VoidParameter* current: params) {
std::string def_str = current->getDefaultStr();
std::string desc_str = current->getDescription();
if (!def_str.empty())
desc_str += " (default=" + def_str + ")";
const char* desc = desc_str.c_str();
fprintf(stderr," %-*s -", nameWidth, current->getName());
int column = strlen(current->getName());
if (column < nameWidth) column = nameWidth;
column += 4;
while (true) {
if (desc[0] == '\0')
break;
int wordLen = strcspn(desc, " \f\n\r\t\v,");
if (wordLen == 0) {
desc++;
continue;
}
if (desc[wordLen] == ',')
wordLen++;
if (column + wordLen + 1 > width) {
fprintf(stderr,"\n%*s",nameWidth+4,"");
column = nameWidth+4;
}
fprintf(stderr, " ");
column++;
fprintf(stderr, "%.*s", wordLen, desc);
column += wordLen;
desc += wordLen;
}
fprintf(stderr,"\n");
}
}
bool Configuration::remove(const char* param) {
std::list<VoidParameter*>::iterator iter;
iter = std::find_if(params.begin(), params.end(),
[param](VoidParameter* p) {
return strcasecmp(p->getName(), param) == 0;
});
if (iter == params.end())
return false;
params.erase(iter);
return true;
}
int Configuration::handleArg(int argc, char* argv[], int index)
{
std::string param, val;
const char* equal = strchr(argv[index], '=');
if (equal == argv[index])
return 0;
if (equal) {
param.assign(argv[index], equal-argv[index]);
val.assign(equal+1);
} else {
param.assign(argv[index]);
}
if ((param.length() > 0) && (param[0] == '-')) {
// allow gnu-style --<option>
if ((param.length() > 1) && (param[1] == '-'))
param = param.substr(2);
else
param = param.substr(1);
} else {
// All command line arguments need either an initial '-', or an '='
if (!equal)
return 0;
}
if (equal)
return set(param.c_str(), val.c_str()) ? 1 : 0;
for (VoidParameter* current: params) {
if (strcasecmp(current->getName(), param.c_str()) != 0)
continue;
// We need to resolve an ambiguity for booleans
if (dynamic_cast<BoolParameter*>(current) != nullptr) {
if (index+1 < argc) {
// FIXME: Should not duplicate the list of values here
if ((strcasecmp(argv[index+1], "0") == 0) ||
(strcasecmp(argv[index+1], "1") == 0) ||
(strcasecmp(argv[index+1], "on") == 0) ||
(strcasecmp(argv[index+1], "off") == 0) ||
(strcasecmp(argv[index+1], "true") == 0) ||
(strcasecmp(argv[index+1], "false") == 0) ||
(strcasecmp(argv[index+1], "yes") == 0) ||
(strcasecmp(argv[index+1], "no") == 0)) {
return current->setParam(argv[index+1]) ? 2 : 0;
}
}
}
if (current->setParam())
return 1;
if (index+1 >= argc)
return 0;
return current->setParam(argv[index+1]) ? 2 : 0;
}
return 0;
}
// -=- VoidParameter
VoidParameter::VoidParameter(const char* name_, const char* desc_)
: immutable(false), name(name_), description(desc_)
{
Configuration *conf;
conf = Configuration::global();
conf->params.push_back(this);
conf->params.sort([](const VoidParameter* a, const VoidParameter* b) {
return strcasecmp(a->getName(), b->getName()) < 0;
});
}
VoidParameter::~VoidParameter() {
Configuration *conf;
conf = Configuration::global();
conf->params.remove(this);
}
const char*
VoidParameter::getName() const {
return name;
}
const char*
VoidParameter::getDescription() const {
return description;
}
bool VoidParameter::setParam() {
return false;
}
bool VoidParameter::isDefault() const {
return getDefaultStr() == getValueStr();
}
void
VoidParameter::setImmutable() {
vlog.debug("Set immutable %s", getName());
immutable = true;
}
// -=- AliasParameter
AliasParameter::AliasParameter(const char* name_, const char* desc_,
VoidParameter* param_)
: VoidParameter(name_, desc_), param(param_) {
}
bool
AliasParameter::setParam(const char* v) {
return param->setParam(v);
}
bool AliasParameter::setParam() {
return param->setParam();
}
std::string AliasParameter::getDefaultStr() const {
return "";
}
std::string AliasParameter::getValueStr() const {
return param->getValueStr();
}
void
AliasParameter::setImmutable() {
vlog.debug("Set immutable %s (Alias)", getName());
param->setImmutable();
}
// -=- BoolParameter
BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v)
: VoidParameter(name_, desc_), value(v), def_value(v) {
}
bool
BoolParameter::setParam(const char* v) {
if (immutable) return true;
if (*v == 0 || strcasecmp(v, "1") == 0 || strcasecmp(v, "on") == 0
|| strcasecmp(v, "true") == 0 || strcasecmp(v, "yes") == 0)
setParam(true);
else if (strcasecmp(v, "0") == 0 || strcasecmp(v, "off") == 0
|| strcasecmp(v, "false") == 0 || strcasecmp(v, "no") == 0)
setParam(false);
else {
vlog.error("Bool parameter %s: Invalid value '%s'", getName(), v);
return false;
}
return true;
}
bool BoolParameter::setParam() {
setParam(true);
return true;
}
void BoolParameter::setParam(bool b) {
if (immutable) return;
value = b;
vlog.debug("Set %s(Bool) to %s", getName(), getValueStr().c_str());
}
std::string BoolParameter::getDefaultStr() const {
return def_value ? "on" : "off";
}
std::string BoolParameter::getValueStr() const {
return value ? "on" : "off";
}
BoolParameter::operator bool() const {
return value;
}
// -=- IntParameter
IntParameter::IntParameter(const char* name_, const char* desc_, int v,
int minValue_, int maxValue_)
: VoidParameter(name_, desc_), value(v), def_value(v),
minValue(minValue_), maxValue(maxValue_)
{
if (v < minValue || v > maxValue) {
vlog.error("Invalid default value %d for %s", v, getName());
throw std::invalid_argument("Invalid default value");
}
}
bool
IntParameter::setParam(const char* v) {
char* end;
long n;
if (immutable) return true;
n = strtol(v, &end, 0);
if ((*end != 0) || (n < INT_MIN) || (n > INT_MAX)) {
vlog.error("Int parameter %s: Invalid value '%s'", getName(), v);
return false;
}
return setParam(n);
}
bool
IntParameter::setParam(int v) {
if (immutable) return true;
if (v < minValue || v > maxValue) {
vlog.error("Int parameter %s: Invalid value '%d'", getName(), v);
return false;
}
vlog.debug("Set %s(Int) to %d", getName(), v);
value = v;
return true;
}
std::string IntParameter::getDefaultStr() const {
char result[16];
sprintf(result, "%d", def_value);
return result;
}
std::string IntParameter::getValueStr() const {
char result[16];
sprintf(result, "%d", value);
return result;
}
IntParameter::operator int() const {
return value;
}
// -=- StringParameter
StringParameter::StringParameter(const char* name_, const char* desc_,
const char* v)
: VoidParameter(name_, desc_), value(v?v:""), def_value(v?v:"")
{
if (!v) {
vlog.error("Default value <null> for %s not allowed",name_);
throw std::invalid_argument("Default value <null> not allowed");
}
}
bool StringParameter::setParam(const char* v) {
if (immutable) return true;
if (!v)
throw std::invalid_argument("setParam(<null>) not allowed");
vlog.debug("Set %s(String) to %s", getName(), v);
value = v;
return true;
}
std::string StringParameter::getDefaultStr() const {
return def_value;
}
std::string StringParameter::getValueStr() const {
return value;
}
StringParameter::operator const char *() const {
return value.c_str();
}
// -=- EnumParameter
EnumParameter::EnumParameter(const char* name_, const char* desc_,
const std::set<const char*>& enums_,
const char* v)
: VoidParameter(name_, desc_), value(v?v:""), def_value(v?v:"")
{
if (!v) {
vlog.error("Default value <null> for %s not allowed", name_);
throw std::invalid_argument("Default value <null> not allowed");
}
for (const char* e: enums_) {
if (!e) {
vlog.error("Enumeration <null> for %s not allowed", name_);
throw std::invalid_argument("Enumeration <null> not allowed");
}
enums.insert(e);
}
if (std::find(enums.begin(), enums.end(), def_value) == enums.end()) {
vlog.error("Default value %s for %s is not in list of valid values",
def_value.c_str(), name_);
throw std::invalid_argument("Default value is not in list of valid values");
}
}
bool EnumParameter::setParam(const char* v)
{
std::set<std::string>::const_iterator iter;
if (immutable) return true;
if (!v)
throw std::invalid_argument("setParam(<null>) not allowed");
iter = std::find_if(enums.begin(), enums.end(),
[v](const std::string& e) {
return strcasecmp(e.c_str(), v) == 0;
});
if (iter == enums.end()) {
vlog.error("Enum parameter %s: Invalid value '%s'", getName(), v);
return false;
}
vlog.debug("Set %s(Enum) to %s", getName(), iter->c_str());
value = *iter;
return true;
}
std::string EnumParameter::getDefaultStr() const
{
return def_value;
}
std::string EnumParameter::getValueStr() const
{
return value;
}
bool EnumParameter::operator==(const char* other) const
{
return strcasecmp(value.c_str(), other) == 0;
}
bool EnumParameter::operator==(const std::string& other) const
{
return *this == other.c_str();
}
bool EnumParameter::operator!=(const char* other) const
{
return strcasecmp(value.c_str(), other) != 0;
}
bool EnumParameter::operator!=(const std::string& other) const
{
return *this != other.c_str();
}
// -=- BinaryParameter
BinaryParameter::BinaryParameter(const char* name_, const char* desc_,
const uint8_t* v, size_t l)
: VoidParameter(name_, desc_),
value(nullptr), length(0), def_value(nullptr), def_length(0) {
if (l) {
assert(v);
value = new uint8_t[l];
length = l;
memcpy(value, v, l);
def_value = new uint8_t[l];
def_length = l;
memcpy(def_value, v, l);
}
}
BinaryParameter::~BinaryParameter() {
delete [] value;
delete [] def_value;
}
bool BinaryParameter::setParam(const char* v) {
if (immutable) return true;
std::vector<uint8_t> newValue = hexToBin(v, strlen(v));
if (newValue.empty() && strlen(v) > 0)
return false;
setParam(newValue.data(), newValue.size());
return true;
}
void BinaryParameter::setParam(const uint8_t* v, size_t len) {
if (immutable) return;
vlog.debug("Set %s(Binary)", getName());
delete [] value;
value = nullptr;
length = 0;
if (len) {
assert(v);
value = new uint8_t[len];
length = len;
memcpy(value, v, len);
}
}
std::string BinaryParameter::getDefaultStr() const {
return binToHex(def_value, def_length);
}
std::string BinaryParameter::getValueStr() const {
return binToHex(value, length);
}
std::vector<uint8_t> BinaryParameter::getData() const {
std::vector<uint8_t> out(length);
memcpy(out.data(), value, length);
return out;
}
// -=- ListParameter template
template<typename ValueType>
ListParameter<ValueType>::ListParameter(const char* name_,
const char* desc_,
const ListType& v)
: VoidParameter(name_, desc_), value(v), def_value(v)
{
}
template<typename ValueType>
bool ListParameter<ValueType>::setParam(const char* v)
{
std::vector<std::string> entries;
ListType new_value;
if (immutable)
return true;
// setParam({}) ends up as setParam(nullptr)
if (v != nullptr)
entries = split(v, ',');
for (std::string& entry : entries) {
ValueType e;
entry.erase(0, entry.find_first_not_of(" \f\n\r\t\v"));
entry.erase(entry.find_last_not_of(" \f\n\r\t\v")+1);
if (!decodeEntry(entry.c_str(), &e)) {
vlog.error("List parameter %s: Invalid value '%s'",
getName(), entry.c_str());
return false;
}
new_value.push_back(e);
}
return setParam(new_value);
}
template<typename ValueType>
bool ListParameter<ValueType>::setParam(const ListType& v)
{
ListType vnorm;
if (immutable)
return true;
for (const ValueType& entry : v) {
if (!validateEntry(entry)) {
vlog.error("List parameter %s: Invalid value '%s'", getName(),
encodeEntry(entry).c_str());
return false;
}
vnorm.push_back(normaliseEntry(entry));
}
value = vnorm;
vlog.debug("set %s(List) to %s", getName(), getValueStr().c_str());
return true;
}
template<typename ValueType>
std::string ListParameter<ValueType>::getDefaultStr() const
{
std::string result;
for (ValueType entry : def_value) {
// FIXME: Might want to add a space here as well for readability,
// but this would sacrifice backward compatibility
if (!result.empty())
result += ',';
result += encodeEntry(entry);
}
return result;
}
template<typename ValueType>
std::string ListParameter<ValueType>::getValueStr() const
{
std::string result;
for (ValueType entry : value) {
// FIXME: Might want to add a space here as well for readability,
// but this would sacrifice backward compatibility
if (!result.empty())
result += ',';
result += encodeEntry(entry);
}
return result;
}
template<typename ValueType>
typename ListParameter<ValueType>::const_iterator ListParameter<ValueType>::begin() const
{
return value.begin();
}
template<typename ValueType>
typename ListParameter<ValueType>::const_iterator ListParameter<ValueType>::end() const
{
return value.end();
}
template<typename ValueType>
bool ListParameter<ValueType>::validateEntry(const ValueType& /*entry*/) const
{
return true;
}
template<typename ValueType>
ValueType ListParameter<ValueType>::normaliseEntry(const ValueType& entry) const
{
return entry;
}
// -=- IntListParameter
template class core::ListParameter<int>;
IntListParameter::IntListParameter(const char* name_, const char* desc_,
const ListType& v,
int minValue_, int maxValue_)
: ListParameter<int>(name_, desc_, v),
minValue(minValue_), maxValue(maxValue_)
{
for (int entry : v) {
if (!validateEntry(entry)) {
vlog.error("Invalid default value %d for %s", entry, getName());
throw std::invalid_argument("Invalid default value");
}
}
}
bool IntListParameter::decodeEntry(const char* entry, int* out) const
{
long n;
char *end;
assert(entry);
assert(out);
if (entry[0] == '\0')
return false;
n = strtol(entry, &end, 0);
if ((*end != 0) || (n < INT_MIN) || (n > INT_MAX))
return false;
*out = n;
return true;
}
std::string IntListParameter::encodeEntry(const int& entry) const
{
char valstr[16];
sprintf(valstr, "%d", entry);
return valstr;
}
bool IntListParameter::validateEntry(const int& entry) const
{
return (entry >= minValue) && (entry <= maxValue);
}
// -=- StringListParameter
template class core::ListParameter<std::string>;
StringListParameter::StringListParameter(const char* name_,
const char* desc_,
const std::list<const char*>& v_)
: ListParameter<std::string>(name_, desc_, {})
{
for (const char* v: v_) {
if (!v) {
vlog.error("Default value <null> for %s not allowed", name_);
throw std::invalid_argument("Default value <null> not allowed");
}
value.push_back(v);
def_value.push_back(v);
}
}
StringListParameter::const_iterator StringListParameter::begin() const
{
return ListParameter<std::string>::begin();
}
StringListParameter::const_iterator StringListParameter::end() const
{
return ListParameter<std::string>::end();
}
bool StringListParameter::decodeEntry(const char* entry, std::string* out) const
{
*out = entry;
return true;
}
std::string StringListParameter::encodeEntry(const std::string& entry) const
{
return entry;
}
// -=- EnumListEntry
EnumListEntry::EnumListEntry(const std::string& v)
: value(v)
{
}
std::string EnumListEntry::getValueStr() const
{
return value;
}
bool EnumListEntry::operator==(const char* other) const
{
return strcasecmp(value.c_str(), other) == 0;
}
bool EnumListEntry::operator==(const std::string& other) const
{
return *this == other.c_str();
}
bool EnumListEntry::operator!=(const char* other) const
{
return strcasecmp(value.c_str(), other) != 0;
}
bool EnumListEntry::operator!=(const std::string& other) const
{
return *this != other.c_str();
}
// -=- EnumListParameter
EnumListParameter::EnumListParameter(const char* name_,
const char* desc_,
const std::set<const char*>& enums_,
const std::list<const char*>& v_)
: ListParameter<std::string>(name_, desc_, {})
{
for (const char* v: v_) {
if (!v) {
vlog.error("Default value <null> for %s not allowed", name_);
throw std::invalid_argument("Default value <null> not allowed");
}
value.push_back(v);
def_value.push_back(v);
}
for (const char* e: enums_) {
if (!e) {
vlog.error("Enumeration <null> for %s not allowed", name_);
throw std::invalid_argument("Enumeration <null> not allowed");
}
enums.insert(e);
}
for (const std::string& def_entry : def_value) {
if (std::find(enums.begin(), enums.end(), def_entry) == enums.end()) {
vlog.error("Default value %s for %s is not in list of valid values",
def_entry.c_str(), name_);
throw std::invalid_argument("Default value is not in list of valid values");
}
}
}
EnumListParameter::const_iterator EnumListParameter::begin() const
{
return ListParameter<std::string>::begin();
}
EnumListParameter::const_iterator EnumListParameter::end() const
{
return ListParameter<std::string>::end();
}
bool EnumListParameter::decodeEntry(const char* entry, std::string* out) const
{
*out = entry;
return true;
}
std::string EnumListParameter::encodeEntry(const std::string& entry) const
{
return entry;
}
bool EnumListParameter::validateEntry(const std::string& entry) const
{
for (const std::string& e : enums) {
if (strcasecmp(e.c_str(), entry.c_str()) == 0)
return true;
}
return false;
}
std::string EnumListParameter::normaliseEntry(const std::string& entry) const
{
for (const std::string& e : enums) {
if (strcasecmp(e.c_str(), entry.c_str()) == 0)
return e;
}
throw std::logic_error("Entry is not in list of valid values");
}
|