aboutsummaryrefslogtreecommitdiffstats
path: root/src/settings.c
blob: 1eba1cc206b79fa18738d2d12bbabd89b14cb8ad (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
 * Copyright © 2016  Mattias Andrée <maandree@member.fsf.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include "settings.h"
#include "arg.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <limits.h>



/**
 * The name of the process.
 */
char *argv0 = NULL;



/**
 * Print usage information and exit if a condition is met.
 * 
 * @param  condition  Do no do anything iff this is zero.
 */
static void
usage(int condition)
{
	if (condition) {
		fprintf(stderr,
		        "Usage: %s [OPTIONS]...\n"
	        	"See `man 1 radharc` for more information.\n",
		        !argv0 ? "radharc" : argv0);
		exit(2);
	}
}


/**
 * Parse a temperature string.
 * 
 * @param   str        The temperature string.
 * @param   temp       Output parameter for the temperature.
 *                     Overflow is truncated.
 * @param   direction  Unless `NULL`, will be set to +1 if `str`
 *                     starts with a '+', `-1` if `str` starts
 *                     with a '-', 0 otherwise. If `NULL` and
 *                     `str` starts with a '-' or a '+', the
 *                     function will fail.
 * @param   lower      The function shall fail if the evaluated
 *                     temperature is below this.
 * @return             0 on success, -1 on error.
 */
static int
parse_temperature(const char *str, long int *temp, int *direction, int lower)
{
	int dir = 0;
	char *end;
	switch (*str) {
	case '-':  dir = -1;  break;
	case '+':  dir = +1;  break;
	}
	str += !!dir;
	if (dir && !direction)  return -1;
	else if (dir)           *direction = dir;
	if (!isdigit(*str))     return -1;
	*temp = (errno = 0, strtol)(str, &end, 10);
	if ((errno && !((errno == ERANGE) && (*temp == LONG_MAX))) || *end || (*temp < lower))
		return -1;
	return 0;
}


/**
 * Parse a string as a positively valued `struct timespec`.
 * 
 * @param   str  The string.
 * @param   ts   Output parameter for the value.
 * @return       0 on success, -1 on error. 
 */
static int
parse_timespec(const char *str, struct timespec *ts)
{
	int points = 0;
	memset(ts, 0, sizeof(*ts));

	/* Parse seconds. */
	if (!isdigit(*str))
		return -1;
	while (isdigit(*str)) {
		ts->tv_sec *= 10;
		ts->tv_sec += *str++ & 15;
	}

	/* End? */
	if (!*str)        return 0;
	if (*str != '.')  return -1;

	/* Parse nanoseconds.*/
	for (; (points++ < 9) && isdigit(*str); str++) {
		ts->tv_nsec *= 10;
		ts->tv_nsec += *str++ & 15;
	}
	if (points == 9) {
		if (!isdigit(*str))  return -1;
		if (*str++ >= '5') {
			ts->tv_nsec += 1;
			if (ts->tv_nsec == 1000000000L)
				ts->tv_sec += 1, ts->tv_nsec = 0;
		}
	}
	while (isdigit(*str))  str++;
	if (*str)  return -1;

	/* End! */
	return 0;
}


/**
 * Parse a latitude or a longitude value.
 * 
 * @param   str    The string.
 * @param   loc    Output parameter for the value.
 * @param   limit  The limit of the absolute value.
 * @return         0 on success, -1 on error.
 */
static int
parse_location(char *str, double *loc, double limit)
{
	char* end;
	if (strstr(str, "−") == str) /* Support proper minus. */
		*(str += strlen("−") - 1) = '-';
	if ((*str != '-') && (*str != '+') && (*str != '.') && !isdigit(*str))
		return -1;
	*loc = (errno = 0, strtod)(str, &end);
	return -(errno || *loc || (fabs(*loc) > limit));
}


/**
 * Parse the command line.
 * 
 * @param  argc      The number of elements in `argv`.
 * @param  argv      The commnad line arguments including the zeroth elemenet.
 * @param  settings  Output parameter for the settings.
 */
void
parse_command_line(int argc, char *argv[], struct settings *settings)
{
	int location_set = 0;
	char *p;
	char *arg;
	int c = 0;

	memset(settings, 0, sizeof(*settings));
	settings->natural_temp = 6500;
	settings->day_temp     = 5500;
	settings->night_temp   = 3500;
	settings->trans_speed  = 50;

	ARGBEGIN {
	case 'l':
		usage(!(p = strchr(arg = ARGF(), ':')));
		*p++ = '\0', location_set = 1;
		usage(parse_location(arg, &(settings->latitude),  90.0));
		usage(parse_location(arg, &(settings->longitude), 180.0));
		break;
	case 't':
		settings->temp = settings->day_temp = settings->night_temp = 0;
		settings->temp_direction = 0;
		if ((p = strchr(arg = ARGF(), ':'))) {
			*p++ = '\0';
			usage(parse_temperature(arg, &(settings->day_temp), NULL, 1000));
			usage(parse_temperature(p, &(settings->night_temp), NULL, 1000));
		} else {
			usage(parse_temperature(arg, &(settings->temp), &(settings->temp_direction), 1000));
		}
		break;
	case 'T':
		usage(parse_temperature(ARGF(), &(settings->natural_temp), NULL, 1000));
		break;
	case 's':
		settings->trans_speed = 0;
		usage(parse_timespec(ARGF(), &(settings->transition)));
		break;
	case 'S':
		usage(parse_temperature(ARGF(), &(settings->trans_speed), NULL, 1));
		break;
	case 'h':
		usage(!(settings->hookpath = ARGF()));
		break;
	case 'd': c++; /* Fall though. */
	case 'e': c++; /* Fall though. */
	case 'm': c++;
#define REALLOC(VAR, N)  !(VAR = realloc(VAR, (N) * sizeof(*VAR)))
		settings->monitors_n++;
		if (REALLOC(settings->monitors_id,  settings->monitors_n))  goto fail;
		if (REALLOC(settings->monitors_arg, settings->monitors_n))  goto fail;
		settings->monitors_id[settings->monitors_n - 1] = ARGF();
		settings->monitors_arg[settings->monitors_n - 1] = (c == 3 ? 'm' : c == 2 ? 'e' : 'd'), c = 0;
		break;
	case 'p':  settings->print_status = 1;  break;
	case 'n':  settings->panic_start  = 1;  break;
	case 'N':  settings->panic_else   = 1;  break;
	case 'o':  settings->set_and_exit = 1;  break;
	case 'x':  settings->ignore_calib = 1;  break;
	case 'i':  settings->negative     = 1;  break;
	case 'b':  settings->use_bus      = 1;  break;
	default:   usage(1);                    break;
	} ARGEND;
	usage(argc);

	if (!location_set && !(settings->temp)) {
		fprintf(stderr,
			"%s: The -l option is mandatory, unless single value -t is used."
	        	"See `man 1 radharc` for more information.\n",
		        !argv0 ? "radharc" : argv0);
		exit(2);
	}

fail:
	perror(argv0 ? argv0 : "radharc");
	exit(1);
}