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
|
/*
* Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
*
* This program 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.
*/
#include "OptionAgent.h"
#include "Environ.h"
#include "Log.h"
#include "Path.h"
#include "FsPath.h"
#include "ScriptAgent.h"
#include "StringTool.h"
#include "HelpException.h"
#include "LogicException.h"
#include "ScriptException.h"
#include "OptionParams.h"
#include "StringMsg.h"
#include "UnknownMsgException.h"
#include "minmax.h"
#include <cstdlib> //getenv
#include <stdlib.h> //getenv
#include <string.h> //strlen
#include <locale.h> //setlocale
#ifndef LC_MESSAGES
#define LC_MESSAGES LC_CTYPE
#endif
//NOTE: SYSTEM_DATA_DIR is set to "$(datadir)/games/@PACKAGE@"
#ifndef SYSTEM_DATA_DIR
#define SYSTEM_DATA_DIR ""
#endif
//NOTE: userdir = $HOME + USER_DATA_DIR
#ifndef USER_DATA_DIR
#define USER_DATA_DIR ".fillets-ng"
#endif
const char *OptionAgent::CONFIG_FILE = "script/options.lua";
//-----------------------------------------------------------------
/**
* Set user and sytem dir
* and process "script/options.lua" - this will set user and system paths
* and process "script/init.lua".
*/
void
OptionAgent::own_init()
{
m_environ = new Environ();
prepareVersion();
prepareDataPaths();
prepareLang();
}
//-----------------------------------------------------------------
/**
* Save user config.
* Delete left messages.
*/
void
OptionAgent::own_shutdown()
{
delete m_environ;
}
//-----------------------------------------------------------------
/**
* Set program version.
*/
void
OptionAgent::prepareVersion()
{
#ifdef VERSION
setParam("version", VERSION);
#else
setParam("version", "0.0.1");
#endif
#ifdef PACKAGE
setParam("package", PACKAGE);
#else
setParam("package", "A game");
#endif
}
//-----------------------------------------------------------------
/**
* Set user and sytem dir options.
* Userdir="$HOME/.fillets-ng" or ""
*/
void
OptionAgent::prepareDataPaths()
{
registerWatcher("systemdir");
registerWatcher("userdir");
OptionAgent::agent()->setParam("systemdir", SYSTEM_DATA_DIR);
std::string userdir = "";
const char *home = getenv("HOME");
if (home) {
userdir = FsPath::join(home, USER_DATA_DIR);
OptionAgent::agent()->setParam("userdir", userdir);
}
else {
readUserConfig();
}
}
//-----------------------------------------------------------------
/**
* Prepare user lang option.
* For 2-letter lang codes
* see http://www.w3.org/WAI/ER/IG/ert/iso639.htm
*/
void
OptionAgent::prepareLang()
{
setlocale(LC_ALL, "");
//NOTE: '.' will be decimal point for Lua
setlocale(LC_NUMERIC, "C");
if (getParam("lang").empty()) {
char *form = setlocale(LC_MESSAGES, NULL);
if (form) {
int size = min(5, strlen(form));
if (size >= 2) {
setParam("lang", std::string(form, size));
}
}
}
}
//-----------------------------------------------------------------
/**
* Parse command line options.
* Format: $0 [name=value ...]
*
* @throws LogicException when format is wrong
*/
void
OptionAgent::parseCmdOpt(int argc, char *argv[], const OptionParams ¶ms)
{
if (argc >= 1) {
setParam("program", argv[0]);
}
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
parseDashOpt(argv[i], params);
}
else {
parseParamOpt(argv[i], params);
}
}
}
//-----------------------------------------------------------------
/**
* Supported options are '-h', '-v'.
* @throws HelpException when only help is need
* @throws LogicException when used option is unknown
*/
void
OptionAgent::parseDashOpt(const std::string &arg,
const OptionParams ¶ms)
{
if ("-h" == arg || "--help" == arg) {
throw HelpException(ExInfo(getHelpInfo(params)));
}
else if ("-v" == arg || "--version" == arg) {
throw HelpException(ExInfo(getVersionInfo()));
}
else if ("-c" == arg || "--config" == arg) {
throw HelpException(ExInfo(params.getConfig(m_environ)));
}
else {
throw LogicException(ExInfo("unknown option")
.addInfo("arg", arg)
.addInfo("use",
getParam("program") + " --help"));
}
}
//-----------------------------------------------------------------
void
OptionAgent::parseParamOpt(const std::string &arg,
const OptionParams ¶ms)
{
std::string name;
std::string value;
if (splitOpt(arg, &name, &value)) {
params.checkValidity(name, value);
setParam(name, value);
}
else {
throw LogicException(ExInfo("unknown option")
.addInfo("arg", arg)
.addInfo("use",
getParam("program") + " --help"));
}
}
//-----------------------------------------------------------------
/**
* Split "name=value".
* @return true for success
*/
bool
OptionAgent::splitOpt(const std::string &option,
std::string *out_name, std::string *out_value)
{
bool result = false;
std::string::size_type pos = option.find('=');
if (pos != std::string::npos) {
if (pos + 1 < option.size()) {
*out_name = option.substr(0, pos);
*out_value = option.substr(pos + 1, std::string::npos);
result = true;
}
}
return result;
}
//-----------------------------------------------------------------
/**
* Set param.
* Notice watchers.
* When watcher is not available, it will be removed.
*
* @param name param name
* @param value param value
*/
void
OptionAgent::setParam(const std::string &name, const std::string &value)
{
m_environ->setParam(name, value);
}
//-----------------------------------------------------------------
/**
* Store this integer value like string param.
*/
void
OptionAgent::setParam(const std::string &name, long value)
{
m_environ->setParam(name, value);
}
//-----------------------------------------------------------------
/**
* Return value.
* Implicit value is "".
*
* @param name param name
* @param implicit default value = ""
* @return value or implicit value
*/
std::string
OptionAgent::getParam(const std::string &name,
const std::string &implicit) const
{
return m_environ->getParam(name, implicit);
}
//-----------------------------------------------------------------
/**
* Returns number value.
* Implicit value is zero.
*
* @param name param name
* @param implicit default value = 0
* @return number or implicit
*/
int
OptionAgent::getAsInt(const std::string &name,
int implicit) const
{
return m_environ->getAsInt(name, implicit);
}
//-----------------------------------------------------------------
/**
* Returns boolean value.
* Implicit value is false.
*
* @param name param name
* @param implicit default value = false
* @return stored boolean value or implicit value
*/
bool
OptionAgent::getAsBool(const std::string &name,
bool implicit) const
{
return m_environ->getAsBool(name, implicit);
}
//-----------------------------------------------------------------
/**
* Set param also on disk.
* Preserve all current params in options file.
*/
void
OptionAgent::setPersistent(const std::string &name, const std::string &value)
{
//NOTE: path must be created before change of environ
Path config = Path::dataWritePath(CONFIG_FILE);
Environ *swap_env = m_environ;
m_environ = new Environ();
try {
if (config.exists()) {
ScriptAgent::agent()->scriptInclude(config);
}
}
catch (ScriptException &e) {
LOG_WARNING(e.info());
}
setParam(name, value);
m_environ->store(config);
delete m_environ;
m_environ = swap_env;
setParam(name, value);
}
//-----------------------------------------------------------------
void
OptionAgent::setPersistent(const std::string &name, long value)
{
setPersistent(name, StringTool::toString(value));
}
//-----------------------------------------------------------------
/**
* Set value when it is empty.
*/
void
OptionAgent::setDefault(const std::string &name, const std::string &value)
{
m_environ->setParam(name, m_environ->getParam(name, value));
}
//-----------------------------------------------------------------
void
OptionAgent::setDefault(const std::string &name, int value)
{
m_environ->setParam(name, m_environ->getAsInt(name, value));
}
//-----------------------------------------------------------------
void
OptionAgent::addWatcher(const std::string &name, BaseMsg *msg)
{
m_environ->addWatcher(name, msg);
}
//-----------------------------------------------------------------
void
OptionAgent::removeWatchers(const std::string &listenerName)
{
m_environ->removeWatchers(listenerName);
}
//-----------------------------------------------------------------
/**
* Get help text.
*/
std::string
OptionAgent::getHelpInfo(const OptionParams ¶ms) const
{
std::string help = "Usage: "
+ getParam("program") + " [options] [name=value ...]\n";
help += " -h, --help Show this help\n";
help += " -v, --version Show version\n";
help += " -c, --config Show config\n";
help += "\n";
help += "Config variables:\n";
help += params.getHelp(m_environ);
return help;
}
//-----------------------------------------------------------------
std::string
OptionAgent::getVersionInfo() const
{
return getParam("package") + " " + getParam("version");
}
//-----------------------------------------------------------------
/**
* Handle incoming message.
* Messages:
* - param_changed(systemdir) ... reread system options
* - param_changed(userdir) ... reread user options
*
* @throws UnknownMsgException
*/
void
OptionAgent::receiveString(const StringMsg *msg)
{
if (msg->equalsName("param_changed")) {
std::string param = msg->getValue();
if ("systemdir" == param) {
readSystemConfig();
}
else if ("userdir" == param) {
readUserConfig();
}
else {
throw UnknownMsgException(msg);
}
}
else {
throw UnknownMsgException(msg);
}
}
//-----------------------------------------------------------------
void
OptionAgent::readSystemConfig()
{
try {
Path systemConfig = Path::dataSystemPath(CONFIG_FILE);
if (systemConfig.exists()) {
ScriptAgent::agent()->scriptInclude(systemConfig);
}
}
catch (ScriptException &e) {
LOG_WARNING(e.info());
}
}
//-----------------------------------------------------------------
void
OptionAgent::readUserConfig()
{
try {
Path userConfig = Path::dataUserPath(CONFIG_FILE);
if (userConfig.exists()) {
ScriptAgent::agent()->scriptInclude(userConfig);
}
}
catch (ScriptException &e) {
LOG_WARNING(e.info());
}
}
|