/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. * Copyright 2004-2005 Cendio AB. * Copyright 2017 Peter Astrand for Cendio AB * Copyright 2011-2022 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 #endif #include #include #include #include #include #include #include #include #include #include #include using namespace rfb; 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(); const char* desc = current->getDescription(); fprintf(stderr," %-*s -", nameWidth, current->getName()); int column = strlen(current->getName()); if (column < nameWidth) column = nameWidth; column += 4; while (true) { const char* s = strchr(desc, ' '); int wordLen; if (s) wordLen = s-desc; else wordLen = strlen(desc); if (column + wordLen + 1 > width) { fprintf(stderr,"\n%*s",nameWidth+4,""); column = nameWidth+4; } fprintf(stderr," %.*s",wordLen,desc); column += wordLen + 1; desc += wordLen + 1; if (!s) break; } if (!def_str.empty()) { if (column + (int)def_str.size() + 11 > width) fprintf(stderr,"\n%*s",nameWidth+4,""); fprintf(stderr," (default=%s)\n",def_str.c_str()); } else { fprintf(stderr,"\n"); } } } bool Configuration::remove(const char* param) { std::list::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 --