-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcmdline_parser.c
108 lines (90 loc) · 2.87 KB
/
cmdline_parser.c
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
/*
variod - - http://www.openvario.org/
Copyright (C) 2014 The openvario project
A detailed list of copyright holders can be found in the file "AUTHORS"
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 3
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, see <http://www.gnu.org/licenses/>.
*/
#include "cmdline_parser.h"
#include "version.h"
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
bool g_foreground = false;
FILE *fp_config=NULL;
void cmdline_parser(int argc, char **argv){
// locale variables
int c;
char config_filename[50];
const char* Usage = "\n"\
" -v print version information\n"\
" -f don't daemonize, stay in foreground\n"\
" -c [filename] use config file [filename]\n"\
" -d[n] set debug level. n can be [1..2]. default=1\n"\
"\n";
// check commandline arguments
while ((c = getopt (argc, argv, "vc:d::f")) != -1)
{
switch (c) {
case 'v':
printf("variod V%c.%c RELEASE %c build: %s %s %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_GIT, __DATE__, __TIME__);
printf("variod Copyright (C) 2014 see AUTHORS on www.openvario.org\n");
printf("This program comes with ABSOLUTELY NO WARRANTY;\n");
printf("This is free software, and you are welcome to redistribute it under certain conditions;\n");
break;
case 'c':
// use config file
if (optarg == NULL)
{
printf("Missing option for -c\n");
printf("Exiting ...\n");
exit(EXIT_FAILURE);
}
else
{
strcpy(config_filename, optarg);
printf("!! Using config file %s !!\n", config_filename);
// Open the fp to config file
fp_config = fopen(config_filename,"r");
//check if config file opened ok ...
if( fp_config == NULL)
{
printf("Error opening config file: %s\n", config_filename);
printf("Exiting ...\n");
exit(EXIT_FAILURE);
}
}
break;
case 'd':
if (optarg == NULL)
{
g_debug = 1;
}
else
g_debug = atoi(optarg);
printf("!! DEBUG LEVEL %d !!\n",g_debug);
break;
case 'f':
// don't daemonize
printf("!! STAY in foreground !!\n");
g_foreground = true;
break;
case '?':
printf("Unknow option %c\n", optopt);
printf("Usage: variod [OPTION]\n%s",Usage);
printf("Exiting ...\n");
exit(EXIT_FAILURE);
break;
}
}
}