Skip to content
option.cc 4.29 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
  This file is part of SLS.
Seblu's avatar
Seblu committed
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
Seblu's avatar
Seblu committed

  SLS is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
Seblu's avatar
Seblu committed
  the Free Software Foundation; version 2 of the License.
Seblu's avatar
Seblu committed

  SLS 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 SLS; if not, write to the Free Software
Seblu's avatar
Seblu committed
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Seblu's avatar
Seblu committed
*/

Seblu's avatar
Seblu committed
#include "sls.hh"
#include "option.hh"

#include <getopt.h>
#include <stdlib.h>

/*!
** Constructor
*/
Option::Option() {
  verbose = false;
Seblu's avatar
Seblu committed
  database = "/var/db/sls.db";
Seblu's avatar
Seblu committed
  port = 18136;
  maxconn = 200;
Seblu's avatar
Seblu committed
  daemon = true;
Seblu's avatar
Seblu committed
  logfile = "/var/log/sls";
Seblu's avatar
Seblu committed
  pidfile = "/var/lock/sls.pid";
}

/*!
** Print usage on @param out. The program name is @param name.
*/
void Option::usage(const char *name, ostream &out) const {
  out << "usage: " << name
Seblu's avatar
Seblu committed
      << " [-h|--help] [-d|--database file] [-v|--verbose] [-p port] [-t max-thread]" << std::endl
Seblu's avatar
Seblu committed
      << "-h | --help           : Print this help." << std::endl
Seblu's avatar
Seblu committed
      << "-d | --database file  : Define database (def: /var/db/sls.db)." << std::endl
Seblu's avatar
Seblu committed
      << "-v | --verbose        : Set verbose mode." << std::endl
      << "-V | --version        : Print version and exit." << std::endl
Seblu's avatar
Seblu committed
      << "-p | --port p         : sls listen on port p (def: 18136)." << std::endl
      << "-m | --max-conn p     : Maximun number of connection (def: 200)." << std::endl
Seblu's avatar
Seblu committed
      << "-P | --pid-file       : Path to pid file (def: /var/lock/sls.pid)." << std::endl
      << "-l | --log-file       : Path to log file (def: /var/log/sls)." << std::endl
      << "-D | --debug          : Run with verbose and not in daemon mode." << std::endl;
Seblu's avatar
Seblu committed
}

/*!
** Parse argline arguments and fill options accordingly
**
** @param argc argc of your program
** @param argv argv of your program
**
** @return
*/
Option &Option::load(int argc, char *argv[]) {
  struct option lopt[] = {
    {"help", 0, 0, 'h'},
    {"database", 0, 0, 'd'},
    {"verbose", 0, 0, 'v'},
    {"version", 0, 0, 'V'},
    {"port", 0, 0, 'p'},
    {"max-conn", 0, 0, 'm'},
Seblu's avatar
Seblu committed
    {"pid-file", 0, 0, 'P'},
    {"log-file", 0, 0, 'l'},
    {"debug", 0, 0, 'D'},
    {0, 0, 0, 0},
  };
  int option;

  while ((option = getopt_long(argc, argv, "hvnVDd:p:P:l:m:", lopt, 0)) != -1) {
Seblu's avatar
Seblu committed
    switch (option) {
    case 'h':
      usage(*argv, std::cout);
      exit(ERR_NO);
Seblu's avatar
Seblu committed
      break;
    case 'p':
      port = atoi(optarg);
      break;
    case 'm':
      maxconn = atoi(optarg);
Seblu's avatar
Seblu committed
      break;
Seblu's avatar
Seblu committed
    case 'd':
Seblu's avatar
Seblu committed
      database = optarg;
Seblu's avatar
Seblu committed
      break;
    case 'P':
      pidfile = optarg;
      break;
    case 'l':
      logfile = optarg;
      break;
    case 'V':
      std::cerr << "Version: " << VERSION << std::endl;
Seblu's avatar
Seblu committed
      exit(ERR_NO);
    case 'D':
      daemon = false;
    case 'v':
      verbose = true;
      break;
    case '?':
      usage(*argv, std::cerr);
      exit(ERR_USAGE);
    }
  }

  if (argv[optind] != 0) {
    usage(*argv, std::cerr);
    exit(ERR_USAGE);
  }

  return *this;
}

Option &Option::loadenv() {
  const char *senv;

  // Get log file path
  if ((senv = getenv("SLS_LOG_FILE")) != 0 && *senv != 0)
    logfile = senv;

  // Get pid file path
  if ((senv = getenv("SLS_PID_FILE")) != 0 && *senv != 0)
    pidfile = senv;

  // Get DB file path
  if ((senv = getenv("SLS_DB_FILE")) != 0 && *senv != 0)
    database = senv;
Seblu's avatar
Seblu committed

  // Get port
  if ((senv = getenv("SLS_PORT")) != 0 && *senv != 0)
    port = atoi(senv);
Seblu's avatar
Seblu committed

  // Get max connection
  if ((senv = getenv("SLS_MAX_CONN")) != 0 && *senv != 0)
    maxconn = atoi(senv);
Seblu's avatar
Seblu committed

  // Get debug state
  if ((senv = getenv("SLS_DEBUG")) != 0) {
    daemon = false;
    verbose = true;
  }

  // Get verbose state
  if ((senv = getenv("SLS_VERBOSE")) != 0)
    verbose = true;
Seblu's avatar
Seblu committed

  return *this;
}

void Option::print(ostream &s) const {
  s << "Database file: " << database << std::endl
    << "Log file: " << logfile << std::endl
    << "Pid file: " << pidfile << std::endl
    << "TCP port: " << port << std::endl
    << "Max conn: " << maxconn << std::endl
    << "Debug mode: " << !daemon << std::endl;
}