Newer
Older
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER (<contact@seblu.net> )
SLS 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.
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
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 US
*/
#include "sls.hh"
#include "option.hh"
#include <getopt.h>
#include <stdlib.h>
/*!
** Constructor
*/
Option::Option() {
verbose = false;
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
port = 18136;
daemon = true;
closefd = true;
logfile = "/var/log/syslog/sls";
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
<< " [-h|--help] [-d|--database file] [-v|--verbose] [-p port]" << std::endl
<< "-h | --help : Print this help." << std::endl
<< "-d | --database dir : Define database directory." << std::endl
<< "-v | --verbose : Set verbose mode." << std::endl
<< "-V | --version : Print version and exit." << std::endl
<< "-p | --port p : sls listen on port p." << std::endl
<< "-P | --pid-file : Path to pid file." << std::endl
<< "-l | --log-file : Path to log file." << std::endl
<< "-D | --debug : Run with verbose and not in daemon mode." << std::endl
<< "-n | --no-close : Don't close stdin,stdout and stderr." << std::endl;
}
/*!
** 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'},
{"pid-file", 0, 0, 'P'},
{"log-file", 0, 0, 'l'},
{"debug", 0, 0, 'D'},
{"no-close", 0, 0, 'n'},
{0, 0, 0, 0},
};
int option;
while ((option = getopt_long(argc, argv, "hvnVDd:p:P:l:", lopt, 0)) != -1) {
switch (option) {
case 'h':
usage(*argv, std::cout);
break;
case 'p':
port = atoi(optarg);
break;
case 'd':
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
break;
case 'P':
pidfile = optarg;
break;
case 'l':
logfile = optarg;
break;
case 'V':
std::cerr << "version : " << VERSION << std::endl;
exit(ERR_NO);
case 'D':
daemon = false;
case 'v':
verbose = true;
break;
case 'n':
closefd = false;
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 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);
// // Get log size
// if ((senv = getenv("SLC_LOG_SIZE")) != 0 && *senv != 0)
// log_size = atoi(senv);
return *this;
}