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

  SLC 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

  SLC 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 SLC; 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 "slc.hh"
#include "options.hh"

#include <getopt.h>
#include <stdlib.h>
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
/*!
** Constructor
*/
Seblu's avatar
Seblu committed
Options::Options() {
  port = 18136;
  history_size = 100;
  history_file = (string) getenv("HOME") + "/.slc_history";
  log_size = 500;
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
/*!
** Print usage on @param out. The program name is @param name.
*/
Seblu's avatar
Seblu committed
void Options::usage(const char *name, ostream &out) const {
  out << "usage: " << name
Seblu's avatar
Seblu committed
      << " [-h|--help] [-l|--login name] [-V|--version] server [port]" << std::endl;
Seblu's avatar
Seblu committed
  out << "-h | --help        : Print this help." << std::endl;
Seblu's avatar
Seblu committed
  out << "-V | --version     : Print version and exit." << std::endl;
Seblu's avatar
Seblu committed
  out << "-l | --login name  : Define login to name." << 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
*/
Seblu's avatar
Seblu committed
Options &Options::load(int argc, char *argv[]) {
  struct option lopt[] = {
    {"help", 0, 0, 'h'},
Seblu's avatar
Seblu committed
    {"version", 0, 0, 'V'},
Seblu's avatar
Seblu committed
    {"login", 0, 0, 'l'},
    {0, 0, 0, 0},
  };
  int option;

Seblu's avatar
Seblu committed
  while ((option = getopt_long(argc, argv, "Vhl:", lopt, 0)) != -1) {
Seblu's avatar
Seblu committed
    switch (option) {
    case 'h':
      usage(*argv, std::cout);
      exit(ERR_NO);
Seblu's avatar
Seblu committed
    case 'V':
      std::cout << "Version: " << VERSION << std::endl;
      exit(ERR_NO);
Seblu's avatar
Seblu committed
    case 'l':
      printf("login %s\n", optarg);
      login = optarg;
      break;
    case '?':
      usage(*argv, std::cerr);
      exit(ERR_USAGE);
    }
  }

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

  server = argv[optind++];

  if (argv[optind] != 0) {
    port = atoi(argv[optind++]);
  }

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

  return *this;
}

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

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

  // Get history path
  if ((senv = getenv("SLC_HISTORY_FILE")) != 0 && *senv != 0)
    history_file = senv;

  // Get history size
  if ((senv = getenv("SLC_HISTORY_SIZE")) != 0 && *senv != 0)
    history_size = atoi(senv);
Seblu's avatar
Seblu committed

 // Get log size
  if ((senv = getenv("SLC_LOG_SIZE")) != 0 && *senv != 0)
    log_size = atoi(senv);

  return *this;
}