forked from Openvario/sensord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline_parser.c
executable file
·168 lines (142 loc) · 4.61 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
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
160
161
162
163
164
165
166
167
168
/*
sensord - Sensor Interface for XCSoar Glide Computer - 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 "main.h"
#include "version.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "def.h"
extern int g_debug;
extern int g_log;
extern int g_foreground;
extern int g_secordcomp;
extern FILE *fp_console;
extern FILE *fp_sensordata;
extern FILE *fp_datalog;
extern FILE *fp_config;
void cmdline_parser(int argc, char **argv, t_io_mode *io_mode){
// locale variables
int c;
char datalog_filename[50];
char sensordata_filename[50];
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"\
" -r [filename] record measurement values to file\n"\
" -s second order temperature compensation for MS5611 enable"
" -p [filename] use values from file instead of measuring\n"\
"\n";
// check commandline arguments
while ((c = getopt (argc, argv, "vd::flhr:p:c:s")) != -1)
{
switch (c) {
case 'v':
//sprintf(s, "sensord V0.1 %s %s", __DATE__
printf("sensord V%c.%c RELEASE %c build: %s %s %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_GIT, __DATE__, __TIME__);
printf("sensord 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
// record sensordata for replay
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 g_foreground !!\n");
g_foreground = TRUE;
break;
case 's':
// enable second order compensation
printf("Second order compenstaion ENABLE");
g_secordcomp = TRUE;
break;
case 'r':
// record sensordata for replay
if (optarg == NULL)
{
printf("Missing option for -r\n");
printf("Exiting ...\n");
exit(EXIT_FAILURE);
}
io_mode->sensordata_to_file = TRUE;
strcpy(datalog_filename, optarg);
printf("!! RECORD DATA TO %s !!\n", datalog_filename);
// Open the fp to record file
fp_datalog = fopen(datalog_filename,"w+");
break;
case 'p':
// replay sensordata instead of measuring
if (optarg == NULL)
{
printf("Missing option for -p\n");
printf("Exiting ...\n");
exit(EXIT_FAILURE);
}
io_mode->sensordata_from_file = TRUE;
strcpy(sensordata_filename, optarg);
printf("!! REPLAY DATA FROM %s !!\n", sensordata_filename);
// Open the fp to replay file
fp_sensordata = fopen(sensordata_filename,"r");
if (fp_sensordata == NULL)
{
printf("Error opening file %s for replay !!\n", sensordata_filename);
printf("Exiting ...\n");
exit(EXIT_FAILURE);
}
break;
case '?':
printf("Unknow option %c\n", optopt);
printf("Usage: sensord [OPTION]\n%s",Usage);
printf("Exiting ...\n");
exit(EXIT_FAILURE);
break;
}
}
}