Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* 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 | |
| 41 | static rfb::Mutex configLock; |
| 42 | |
| 43 | #endif |
| 44 | |
| 45 | #include <rdr/HexOutStream.h> |
| 46 | #include <rdr/HexInStream.h> |
| 47 | |
| 48 | using namespace rfb; |
| 49 | |
| 50 | static LogWriter vlog("Config"); |
| 51 | |
| 52 | |
| 53 | // -=- Configuration |
| 54 | |
| 55 | VoidParameter* Configuration::head = 0; |
| 56 | |
| 57 | bool Configuration::setParam(const char* n, const char* v, bool immutable) { |
| 58 | return setParam(n, strlen(n), v, immutable); |
| 59 | } |
| 60 | |
| 61 | bool 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 Åstrand | 55855d5 | 2005-01-03 12:01:45 +0000 | [diff] [blame] | 70 | current->setHasBeenSet(); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 71 | if (b && immutable) |
| 72 | current->setImmutable(); |
| 73 | return b; |
| 74 | } |
| 75 | current = current->_next; |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool 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 Åstrand | 55855d5 | 2005-01-03 12:01:45 +0000 | [diff] [blame] | 95 | current->setHasBeenSet(); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 96 | if (b && immutable) |
| 97 | current->setImmutable(); |
| 98 | return b; |
| 99 | } |
| 100 | current = current->_next; |
| 101 | } |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | VoidParameter* 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 | |
| 117 | void 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 | |
| 156 | VoidParameter::VoidParameter(const char* name_, const char* desc_) |
Peter Åstrand | 55855d5 | 2005-01-03 12:01:45 +0000 | [diff] [blame] | 157 | : immutable(false), _hasBeenSet(false), name(name_), description(desc_) { |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 158 | _next = Configuration::head; |
| 159 | Configuration::head = this; |
| 160 | } |
| 161 | |
| 162 | VoidParameter::~VoidParameter() { |
| 163 | } |
| 164 | |
| 165 | const char* |
| 166 | VoidParameter::getName() const { |
| 167 | return name; |
| 168 | } |
| 169 | |
| 170 | const char* |
| 171 | VoidParameter::getDescription() const { |
| 172 | return description; |
| 173 | } |
| 174 | |
| 175 | bool VoidParameter::setParam() { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | bool VoidParameter::isBool() const { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | VoidParameter::setImmutable() { |
| 185 | vlog.debug("set immutable %s", getName()); |
| 186 | immutable = true; |
| 187 | } |
| 188 | |
Peter Åstrand | 55855d5 | 2005-01-03 12:01:45 +0000 | [diff] [blame] | 189 | void |
| 190 | VoidParameter::setHasBeenSet() { |
| 191 | _hasBeenSet = true; |
| 192 | } |
| 193 | |
| 194 | bool |
| 195 | VoidParameter::hasBeenSet() { |
| 196 | return _hasBeenSet; |
| 197 | } |
| 198 | |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 199 | // -=- AliasParameter |
| 200 | |
| 201 | AliasParameter::AliasParameter(const char* name_, const char* desc_, |
| 202 | VoidParameter* param_) |
| 203 | : VoidParameter(name_, desc_), param(param_) { |
| 204 | } |
| 205 | |
| 206 | bool |
| 207 | AliasParameter::setParam(const char* v) { |
| 208 | return param->setParam(v); |
| 209 | } |
| 210 | |
| 211 | bool AliasParameter::setParam() { |
| 212 | return param->setParam(); |
| 213 | } |
| 214 | |
| 215 | char* |
| 216 | AliasParameter::getDefaultStr() const { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | char* AliasParameter::getValueStr() const { |
| 221 | return param->getValueStr(); |
| 222 | } |
| 223 | |
| 224 | bool AliasParameter::isBool() const { |
| 225 | return param->isBool(); |
| 226 | } |
| 227 | |
| 228 | // -=- BoolParameter |
| 229 | |
| 230 | BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v) |
| 231 | : VoidParameter(name_, desc_), value(v), def_value(v) { |
| 232 | } |
| 233 | |
| 234 | bool |
| 235 | BoolParameter::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 | |
| 253 | bool BoolParameter::setParam() { |
| 254 | setParam(true); |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | void BoolParameter::setParam(bool b) { |
| 259 | if (immutable) return; |
| 260 | value = b; |
| 261 | vlog.debug("set %s(Bool) to %d", getName(), value); |
| 262 | } |
| 263 | |
| 264 | char* |
| 265 | BoolParameter::getDefaultStr() const { |
| 266 | char* result = new char[8]; |
| 267 | sprintf(result, "%d", (int)def_value); |
| 268 | return result; |
| 269 | } |
| 270 | |
| 271 | char* BoolParameter::getValueStr() const { |
| 272 | char* result = new char[8]; |
| 273 | sprintf(result, "%d", (int)value); |
| 274 | return result; |
| 275 | } |
| 276 | |
| 277 | bool BoolParameter::isBool() const { |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | BoolParameter::operator bool() const { |
| 282 | return value; |
| 283 | } |
| 284 | |
| 285 | // -=- IntParameter |
| 286 | |
| 287 | IntParameter::IntParameter(const char* name_, const char* desc_, int v) |
| 288 | : VoidParameter(name_, desc_), value(v), def_value(v) { |
| 289 | } |
| 290 | |
| 291 | bool |
| 292 | IntParameter::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 | |
| 299 | bool |
| 300 | IntParameter::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 | |
| 307 | char* |
| 308 | IntParameter::getDefaultStr() const { |
| 309 | char* result = new char[16]; |
| 310 | sprintf(result, "%d", def_value); |
| 311 | return result; |
| 312 | } |
| 313 | |
| 314 | char* IntParameter::getValueStr() const { |
| 315 | char* result = new char[16]; |
| 316 | sprintf(result, "%d", value); |
| 317 | return result; |
| 318 | } |
| 319 | |
| 320 | IntParameter::operator int() const { |
| 321 | return value; |
| 322 | } |
| 323 | |
| 324 | // -=- StringParameter |
| 325 | |
| 326 | StringParameter::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 | |
| 336 | StringParameter::~StringParameter() { |
| 337 | strFree(value); |
| 338 | } |
| 339 | |
| 340 | bool 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 | |
| 353 | char* StringParameter::getDefaultStr() const { |
| 354 | return strDup(def_value); |
| 355 | } |
| 356 | |
| 357 | char* StringParameter::getValueStr() const { |
| 358 | #ifdef WIN32 |
| 359 | Lock l(configLock); |
| 360 | #endif |
| 361 | return strDup(value); |
| 362 | } |
| 363 | |
| 364 | // -=- BinaryParameter |
| 365 | |
| 366 | BinaryParameter::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 | } |
| 374 | BinaryParameter::~BinaryParameter() { |
| 375 | if (value) |
| 376 | delete [] value; |
| 377 | } |
| 378 | |
| 379 | bool 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 | |
| 388 | void 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 | |
| 402 | char* BinaryParameter::getDefaultStr() const { |
| 403 | return rdr::HexOutStream::binToHexStr(def_value, def_length); |
| 404 | } |
| 405 | |
| 406 | char* BinaryParameter::getValueStr() const { |
| 407 | #ifdef WIN32 |
| 408 | Lock l(configLock); |
| 409 | #endif |
| 410 | return rdr::HexOutStream::binToHexStr(value, length); |
| 411 | } |
| 412 | |
| 413 | void 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 | } |