Newer
Older
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
#include "sld.hh"
Options::Options() {
port = -1;
verbose = -1;
retrydelay = -1;
daemon = -1;
}
void Options::usage(const char *argv0) const {
std::cerr << "usage: "
<< argv0
<< " [-f conffile] [-d scriptdir] [-h] [-v] [-l login]"
<< "[-p pass] [-H host] [-P port] [-r] [-V]"
<< std::endl
<< " -f conffile : read and load conf file (def: /etc/sldrc)." << std::endl
<< " -d scriptdir : Scripts directory." << std::endl
<< " -h : Print this usage." << std::endl
<< " -v : Verbose mode." << std::endl
<< " -q : Quiet mode." << std::endl
<< " -d : Daemon mode." << std::endl
<< " -l host : Set login to host (def: check hostname)." << std::endl
<< " -p secret : Set pass to secret." << std::endl
<< " -H name : Set server host to name." << std::endl
<< " -P number : Set server port to number (def: 18136)." << std::endl
<< " -r val : Set retry delay to val in seconds (def: 60)." << std::endl
<< " -V : Print version and exit." << std::endl;
}
void Options::merge(const Options &opt) {
if (opt.server != "") server = opt.server;
if (opt.port >= 0) port = opt.port;
if (opt.login != "") login = opt.login;
if (opt.pass != "") pass = opt.pass;
if (opt.verbose >= 0) verbose = opt.verbose;
if (opt.conffile != "") conffile = opt.conffile;
if (opt.scriptdir != "") scriptdir = opt.scriptdir;
if (opt.retrydelay >= 0) retrydelay = opt.retrydelay;
if (opt.daemon >= 0) daemon = opt.daemon;
}
Options &Options::getoptions(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-h")) {
usage(*argv);
throw Error(ERR_USAGE);
}
else if (!strcmp(argv[i], "-v")) {
this->verbose = 1;
}
else if (!strcmp(argv[i], "-q")) {
this->verbose = 0;
}
else if (!strcmp(argv[i], "-d")) {
this->daemon = 1;
}
else if (!strcmp(argv[i], "-V")) {
std::cout << "sl daemon version : " << VERSION << std::endl;
exit(ERR_OK);
}
else if (!strcmp(argv[i], "-H")) {
if (++i >= argc)
throw Error(ERR_USAGE, "No enough argument for option -h");
this->server = string(argv[i]);
}
else if (!strcmp(argv[i], "-P")) {
if (++i >= argc)
throw Error(ERR_USAGE, "No enough argument for option -p");
char *endptr;
this->port = strtol(argv[i], &endptr, 10);
if (!(*argv[i] != '\0' && *endptr == '\0'))
throw Error(ERR_USAGE, "Unable to convert port to a number");
}
else if (!strcmp(argv[i], "-f")) {
if (++i >= argc)
throw Error(ERR_USAGE, "No enough argument for option -f");
this->conffile = string(argv[i]);
}
else if (!strcmp(argv[i], "-d")) {
if (++i >= argc)
throw Error(ERR_USAGE, "No enough argument for option -d");
this->scriptdir = string(argv[i]);
}
else if (!strcmp(argv[i], "-l")) {
if (++i >= argc)
throw Error(ERR_USAGE, "No enough argument for option -l");
this->login = string(argv[i]);
}
else if (!strcmp(argv[i], "-p")) {
if (++i >= argc)
throw Error(ERR_USAGE, "No enough argument for option -p");
this->pass = string(argv[i]);
}
else if (!strcmp(argv[i], "-r")) {
if (++i >= argc)
throw Error(ERR_USAGE, "No enough argument for option -r");
this->retrydelay = atoi(argv[i]);
}
else
throw Error(ERR_USAGE, (string) "Invalid options : " + argv[i]);
}
return *this;
}
Options Options::getoptions(const string file) {
ifstream fs;
char line[MAX_CONF_LINE_SIZE];
int i=0;
char name[11], value[MAX_CONF_LINE_SIZE - 10 + 1];
fs.open(file.c_str(), ifstream::in);
if (fs.fail())
throw Error(ERR_FILE, "Unable to open configuration file");
while (!fs.eof()) {
fs.getline(line, MAX_CONF_LINE_SIZE);
if (fs.eof())
break;
++i;
sscanf(line, "%10[^=]=%2039s\n", name, value); //TODO: fix magic number
if (!strcasecmp(name, "host"))
this->login = value;
else if (!strcasecmp(name, "pass"))
this->pass = value;
else if (!strcasecmp(name, "server"))
this->server = value;
else if (!strcasecmp(name, "port"))
this->port = atoi(value);
else if (!strcasecmp(name, "scriptdir"))
this->scriptdir = value;
else if (!strcasecmp(name, "verbose"))
this->verbose = atoi(value);
else if (!strcasecmp(name, "retrydelay"))
this->retrydelay = atoi(value);
else if (!strcasecmp(name, "daemon"))
this->daemon = atoi(value);
else
std::cerr << "Invalid line " << i << ": " << line << std::endl;
}
fs.close();
return *this;
}
ostream &operator<< (ostream &s, const Options &o) {
return s << "server: " << o.server << std::endl
<< "port: " << o.port << std::endl
<< "host login: " << o.login << std::endl
<< "host pass: " << o.pass << std::endl
<< "script directory: " << o.scriptdir << std::endl
<< "configuration file: " << o.conffile << std::endl
<< "retry delay: " << o.retrydelay << std::endl
<< "daemon mode: " << o.daemon << std::endl
<< "verbose level: " << o.verbose << std::endl;
}