aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2022-09-25 00:20:45 +0200
committerThomas Voss <thomasvoss@live.com> 2022-09-25 00:20:45 +0200
commit7f3c35a90e9bd7e0c8e34efc1d8ca992350919ce (patch)
tree2f96f35d6694a392af8d052d044cf628b7ac2c44
parent00ab2a237a9685944ce8ba7330ac07e84439a50d (diff)
Automatically adjust input to [0, MAX]
-rw-r--r--main.c66
1 files changed, 41 insertions, 25 deletions
diff --git a/main.c b/main.c
index 3a839a2..586ed00 100644
--- a/main.c
+++ b/main.c
@@ -24,6 +24,9 @@
#include <lux.h>
+#define die(...) err(EXIT_FAILURE, __VA_ARGS__);
+#define diex(...) errx(EXIT_FAILURE, __VA_ARGS__);
+
static int parseint(char *);
static double parsedouble(char *);
static void usage(char *);
@@ -31,7 +34,8 @@ static void usage(char *);
int
main(int argc, char **argv)
{
- int opt;
+ int br, max, opt;
+ double pr;
struct luxdisp disp;
luxinit(&disp);
@@ -39,42 +43,54 @@ main(int argc, char **argv)
goto Gflag;
else while ((opt = getopt(argc, argv, ":d:D:gGi:I:s:S:")) != -1) {
switch (opt) {
- case 'g':;
- int br;
+ case 'g':
if ((br = luxget(&disp)) == -1)
- err(EXIT_FAILURE, "luxget");
+ die("luxget");
printf("%d\n", br);
break;
case 'G':
-Gflag:;
- double pr;
+Gflag:
if ((pr = luxgetp(&disp)) == -1)
- err(EXIT_FAILURE, "luxgetp");
+ die("luxgetp");
printf("%f\n", pr);
break;
case 'd':
- if (luxdec(&disp, parseint(optarg)) == -1)
- err(EXIT_FAILURE, "luxdec");
- break;
case 'i':
- if (luxinc(&disp, parseint(optarg)) == -1)
- err(EXIT_FAILURE, "luxinc");
- break;
case 's':
- if (luxset(&disp, parseint(optarg)) == -1)
- err(EXIT_FAILURE, "luxset");
+ br = parseint(optarg);
+ if (opt == 'd')
+ br = -br;
+ if (opt != 's') {
+ int tmp = luxget(&disp);
+ if (tmp == -1)
+ die("luxget");
+ br += luxget(&disp);
+ }
+ if (br > (max = luxmax(&disp)))
+ br = max;
+ else if (br < 0)
+ br = 0;
+ if (luxset(&disp, br) == -1)
+ die("luxset");
break;
case 'D':
- if (luxdecp(&disp, parsedouble(optarg)) == -1)
- err(EXIT_FAILURE, "luxdecp");
- break;
case 'I':
- if (luxincp(&disp, parsedouble(optarg)) == -1)
- err(EXIT_FAILURE, "luxincp");
- break;
case 'S':
- if (luxsetp(&disp, parsedouble(optarg)) == -1)
- err(EXIT_FAILURE, "luxsetp");
+ pr = parsedouble(optarg);
+ if (opt == 'D')
+ pr = -pr;
+ if (opt != 'S') {
+ double tmp = luxgetp(&disp);
+ if (tmp == -1)
+ die("luxgetp");
+ pr += luxgetp(&disp);
+ }
+ if (pr > 100)
+ pr = 100;
+ else if (pr < 0)
+ pr = 0;
+ if (luxsetp(&disp, pr) == -1)
+ die("luxset");
break;
default:
usage(argv[0]);
@@ -92,7 +108,7 @@ parseint(char *s)
long n = strtol(optarg, &endptr, 0);
if (*s != '\0' && *endptr == '\0')
return (int) n;
- errx(EXIT_FAILURE, "parseint: invalid integer '%s'", s);
+ diex("parseint: invalid integer '%s'", s);
}
double
@@ -102,7 +118,7 @@ parsedouble(char *s)
double n = strtod(optarg, &endptr);
if (*s != '\0' && *endptr == '\0')
return n;
- errx(EXIT_FAILURE, "parsedouble: invalid double '%s'", s);
+ diex("parsedouble: invalid double '%s'", s);
}
void