-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.cpp
136 lines (113 loc) · 3.71 KB
/
params.cpp
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
/***************************************************************************
* Copyright (C) 2006 by Dmitriy Gorbenko *
* *
* 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. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/times.h>
#include <time.h>
#include <getopt.h>
#include "define.h"
#include "str.h"
#include "tools.h"
#include "message.h"
#include "params.h"
using namespace std;
struct params_def startup_params;
void print_help()
{
message("Usage: %s [OPTIONS]\n"\
"OPTIONS:\n"\
"-h, --help This help\n"
"-s, --silence Do not print to stdout\n"
"-l, --log_file Set log file\n"
"-f, --config_file Set config file\n"
"-d, --daemon Become daemon after starting\n"
"\n"
"Default config file is: /etc/%s\n"
"Default log file is: /var/log/%s\n"
"\n"
, PROGRAM, PROGRAM, PROGRAM);
};
void reset_params()
{
startup_params.alternative_config = FALSE;
startup_params.alternative_log = FALSE;
startup_params.view_help = FALSE;
startup_params.become_daemon = FALSE;
startup_params.silence = FALSE;
startup_params.config_file = NULL;
startup_params.log_file = NULL;
};
unsigned int parse_params(int counter, char **values)
{
int c;
static struct option long_options[] = {
{ "help", 0, 0, 'h' },
{ "log_file", 1, 0, 'l' },
{ "config_file", 1, 0, 'f' },
{ "daemon", 0, 0, 'd' },
{ "silence", 0, 0, 's' },
{ 0 , 0 , 0 , 0}
};
while (1) {
int option_index = 0;
c = getopt_long (counter, values,"hf:l:ds", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
message("\nParameter %s", long_options[option_index].name);
if(optarg)
fprintf(stdout,"\n with argument %s",optarg);
fprintf(stdout,"\n");
break;
case 'h':
print_help();
startup_params.view_help = TRUE;
break;
case 'f':
startup_params.alternative_config = TRUE;
startup_params.config_file = safe_strdup(optarg);
break;
case 'l':
startup_params.alternative_log = TRUE;
startup_params.log_file = safe_strdup(optarg);
break;
case 'd':
startup_params.become_daemon = TRUE;
break;
case 's':
startup_params.silence = TRUE;
break;
case ':': // missing parameter
message("Missing parameter. Try ` --help' for more options.\n\n");
return FALSE;
break;
case '?': // unknown option
message("Unknown option. Try ` --help' for more options.\n\n");
return FALSE;
break;
}
}
return TRUE;
};