You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.cxx 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. // -=- Configuration.cxx
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22. #include <assert.h>
  23. #ifdef WIN32
  24. #define strcasecmp _stricmp
  25. #define strncasecmp _strnicmp
  26. #endif
  27. #include <rfb/util.h>
  28. #include <rfb/Configuration.h>
  29. #include <rfb/LogWriter.h>
  30. #include <rfb/Exception.h>
  31. #ifdef WIN32
  32. // Under Win32, we use these routines from several threads,
  33. // so we must provide suitable locking.
  34. #include <rfb/Threading.h>
  35. static rfb::Mutex configLock;
  36. #endif
  37. #include <rdr/HexOutStream.h>
  38. #include <rdr/HexInStream.h>
  39. using namespace rfb;
  40. static LogWriter vlog("Config");
  41. // -=- Configuration
  42. VoidParameter* Configuration::head = 0;
  43. bool Configuration::setParam(const char* n, const char* v, bool immutable) {
  44. return setParam(n, strlen(n), v, immutable);
  45. }
  46. bool Configuration::setParam(const char* name, int len,
  47. const char* val, bool immutable)
  48. {
  49. VoidParameter* current = head;
  50. while (current) {
  51. if ((int)strlen(current->getName()) == len &&
  52. strncasecmp(current->getName(), name, len) == 0)
  53. {
  54. bool b = current->setParam(val);
  55. if (b && immutable)
  56. current->setImmutable();
  57. return b;
  58. }
  59. current = current->_next;
  60. }
  61. return false;
  62. }
  63. bool Configuration::setParam(const char* config, bool immutable) {
  64. bool hyphen = false;
  65. if (config[0] == '-') {
  66. hyphen = true;
  67. config++;
  68. if (config[0] == '-') config++; // allow gnu-style --<option>
  69. }
  70. const char* equal = strchr(config, '=');
  71. if (equal) {
  72. return setParam(config, equal-config, equal+1, immutable);
  73. } else if (hyphen) {
  74. VoidParameter* current = head;
  75. while (current) {
  76. if (strcasecmp(current->getName(), config) == 0) {
  77. bool b = current->setParam();
  78. if (b && immutable)
  79. current->setImmutable();
  80. return b;
  81. }
  82. current = current->_next;
  83. }
  84. }
  85. return false;
  86. }
  87. VoidParameter* Configuration::getParam(const char* param)
  88. {
  89. VoidParameter* current = head;
  90. while (current) {
  91. if (strcasecmp(current->getName(), param) == 0)
  92. return current;
  93. current = current->_next;
  94. }
  95. return 0;
  96. }
  97. void Configuration::listParams(int width, int nameWidth) {
  98. VoidParameter* current = head;
  99. while (current) {
  100. char* def_str = current->getDefaultStr();
  101. const char* desc = current->getDescription();
  102. fprintf(stderr," %-*s -", nameWidth, current->getName());
  103. int column = strlen(current->getName());
  104. if (column < nameWidth) column = nameWidth;
  105. column += 4;
  106. while (true) {
  107. const char* s = strchr(desc, ' ');
  108. int wordLen;
  109. if (s) wordLen = s-desc;
  110. else wordLen = strlen(desc);
  111. if (column + wordLen + 1 > width) {
  112. fprintf(stderr,"\n%*s",nameWidth+4,"");
  113. column = nameWidth+4;
  114. }
  115. fprintf(stderr," %.*s",wordLen,desc);
  116. column += wordLen + 1;
  117. desc += wordLen + 1;
  118. if (!s) break;
  119. }
  120. if (def_str) {
  121. if (column + (int)strlen(def_str) + 11 > width)
  122. fprintf(stderr,"\n%*s",nameWidth+4,"");
  123. fprintf(stderr," (default=%s)\n",def_str);
  124. strFree(def_str);
  125. } else {
  126. fprintf(stderr,"\n");
  127. }
  128. current = current->_next;
  129. }
  130. }
  131. // -=- VoidParameter
  132. VoidParameter::VoidParameter(const char* name_, const char* desc_)
  133. : immutable(false), name(name_), description(desc_) {
  134. _next = Configuration::head;
  135. Configuration::head = this;
  136. }
  137. VoidParameter::~VoidParameter() {
  138. }
  139. const char*
  140. VoidParameter::getName() const {
  141. return name;
  142. }
  143. const char*
  144. VoidParameter::getDescription() const {
  145. return description;
  146. }
  147. bool VoidParameter::setParam() {
  148. return false;
  149. }
  150. bool VoidParameter::isBool() const {
  151. return false;
  152. }
  153. void
  154. VoidParameter::setImmutable() {
  155. vlog.debug("set immutable %s", getName());
  156. immutable = true;
  157. }
  158. // -=- AliasParameter
  159. AliasParameter::AliasParameter(const char* name_, const char* desc_,
  160. VoidParameter* param_)
  161. : VoidParameter(name_, desc_), param(param_) {
  162. }
  163. bool
  164. AliasParameter::setParam(const char* v) {
  165. return param->setParam(v);
  166. }
  167. bool AliasParameter::setParam() {
  168. return param->setParam();
  169. }
  170. char*
  171. AliasParameter::getDefaultStr() const {
  172. return 0;
  173. }
  174. char* AliasParameter::getValueStr() const {
  175. return param->getValueStr();
  176. }
  177. bool AliasParameter::isBool() const {
  178. return param->isBool();
  179. }
  180. // -=- BoolParameter
  181. BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v)
  182. : VoidParameter(name_, desc_), value(v), def_value(v) {
  183. }
  184. bool
  185. BoolParameter::setParam(const char* v) {
  186. if (immutable) return true;
  187. if (*v == 0 || strcasecmp(v, "1") == 0 || strcasecmp(v, "on") == 0
  188. || strcasecmp(v, "true") == 0 || strcasecmp(v, "yes") == 0)
  189. value = 1;
  190. else if (strcasecmp(v, "0") == 0 || strcasecmp(v, "off") == 0
  191. || strcasecmp(v, "false") == 0 || strcasecmp(v, "no") == 0)
  192. value = 0;
  193. else {
  194. vlog.error("Bool parameter %s: invalid value '%s'", getName(), v);
  195. return false;
  196. }
  197. vlog.debug("set %s(Bool) to %s(%d)", getName(), v, value);
  198. return true;
  199. }
  200. bool BoolParameter::setParam() {
  201. setParam(true);
  202. return true;
  203. }
  204. void BoolParameter::setParam(bool b) {
  205. if (immutable) return;
  206. value = b;
  207. vlog.debug("set %s(Bool) to %d", getName(), value);
  208. }
  209. char*
  210. BoolParameter::getDefaultStr() const {
  211. char* result = new char[8];
  212. sprintf(result, "%d", (int)def_value);
  213. return result;
  214. }
  215. char* BoolParameter::getValueStr() const {
  216. char* result = new char[8];
  217. sprintf(result, "%d", (int)value);
  218. return result;
  219. }
  220. bool BoolParameter::isBool() const {
  221. return true;
  222. }
  223. BoolParameter::operator bool() const {
  224. return value;
  225. }
  226. // -=- IntParameter
  227. IntParameter::IntParameter(const char* name_, const char* desc_, int v)
  228. : VoidParameter(name_, desc_), value(v), def_value(v) {
  229. }
  230. bool
  231. IntParameter::setParam(const char* v) {
  232. if (immutable) return true;
  233. vlog.debug("set %s(Int) to %s", getName(), v);
  234. value = atoi(v);
  235. return true;
  236. }
  237. bool
  238. IntParameter::setParam(int v) {
  239. if (immutable) return true;
  240. vlog.debug("set %s(Int) to %d", getName(), v);
  241. value = v;
  242. return true;
  243. }
  244. char*
  245. IntParameter::getDefaultStr() const {
  246. char* result = new char[16];
  247. sprintf(result, "%d", def_value);
  248. return result;
  249. }
  250. char* IntParameter::getValueStr() const {
  251. char* result = new char[16];
  252. sprintf(result, "%d", value);
  253. return result;
  254. }
  255. IntParameter::operator int() const {
  256. return value;
  257. }
  258. // -=- StringParameter
  259. StringParameter::StringParameter(const char* name_, const char* desc_,
  260. const char* v)
  261. : VoidParameter(name_, desc_), value(strDup(v)), def_value(v)
  262. {
  263. if (!v) {
  264. fprintf(stderr,"Default value <null> for %s not allowed\n",name_);
  265. throw rfb::Exception("Default value <null> not allowed");
  266. }
  267. }
  268. StringParameter::~StringParameter() {
  269. strFree(value);
  270. }
  271. bool StringParameter::setParam(const char* v) {
  272. #ifdef WIN32
  273. Lock l(configLock);
  274. #endif
  275. if (immutable) return true;
  276. if (!v)
  277. throw rfb::Exception("setParam(<null>) not allowed");
  278. vlog.debug("set %s(String) to %s", getName(), v);
  279. strFree(value);
  280. value = strDup(v);
  281. return value != 0;
  282. }
  283. char* StringParameter::getDefaultStr() const {
  284. return strDup(def_value);
  285. }
  286. char* StringParameter::getValueStr() const {
  287. #ifdef WIN32
  288. Lock l(configLock);
  289. #endif
  290. return strDup(value);
  291. }
  292. // -=- BinaryParameter
  293. BinaryParameter::BinaryParameter(const char* name_, const char* desc_, const void* v, int l)
  294. : VoidParameter(name_, desc_), value(0), length(0), def_value((char*)v), def_length(l) {
  295. if (l) {
  296. value = new char[l];
  297. length = l;
  298. memcpy(value, v, l);
  299. }
  300. }
  301. BinaryParameter::~BinaryParameter() {
  302. if (value)
  303. delete [] value;
  304. }
  305. bool BinaryParameter::setParam(const char* v) {
  306. #ifdef WIN32
  307. Lock l(configLock);
  308. #endif
  309. if (immutable) return true;
  310. vlog.debug("set %s(Binary) to %s", getName(), v);
  311. return rdr::HexInStream::hexStrToBin(v, &value, &length);
  312. }
  313. void BinaryParameter::setParam(const void* v, int len) {
  314. #ifdef WIN32
  315. Lock l(configLock);
  316. #endif
  317. if (immutable) return;
  318. vlog.debug("set %s(Binary)", getName());
  319. delete [] value; value = 0;
  320. if (len) {
  321. value = new char[len];
  322. length = len;
  323. memcpy(value, v, len);
  324. }
  325. }
  326. char* BinaryParameter::getDefaultStr() const {
  327. return rdr::HexOutStream::binToHexStr(def_value, def_length);
  328. }
  329. char* BinaryParameter::getValueStr() const {
  330. #ifdef WIN32
  331. Lock l(configLock);
  332. #endif
  333. return rdr::HexOutStream::binToHexStr(value, length);
  334. }
  335. void BinaryParameter::getData(void** data_, int* length_) const {
  336. #ifdef WIN32
  337. Lock l(configLock);
  338. #endif
  339. if (length_) *length_ = length;
  340. if (data_) {
  341. *data_ = new char[length];
  342. memcpy(*data_, value, length);
  343. }
  344. }