aboutsummaryrefslogtreecommitdiff
path: root/lux.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-11-13 09:22:03 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-11-13 09:22:35 +0100
commited9b3f73897665d3bf15ad0ee41a2608e2f3fe5b (patch)
tree6b27745cc6c3516e6df803140a313983fd25698f /lux.c
parent8edfed66f26ef9686b5a5ee0e288ed9f5cddfdfb (diff)
Rename main.c to lux.c
Diffstat (limited to 'lux.c')
-rw-r--r--lux.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/lux.c b/lux.c
new file mode 100644
index 0000000..f6fc33a
--- /dev/null
+++ b/lux.c
@@ -0,0 +1,129 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define _POSIX_C_SOURCE 2
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#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 *);
+
+int
+main(int argc, char **argv)
+{
+ int br, max, opt;
+ double pr;
+ lux_t disp;
+
+ luxinit(&disp);
+ if (argc == 1)
+ goto Gflag;
+ else while ((opt = getopt(argc, argv, ":d:D:gGi:I:s:S:")) != -1) {
+ switch (opt) {
+ case 'g':
+ if ((br = luxget(&disp)) == -1)
+ die("luxget");
+ printf("%d\n", br);
+ break;
+ case 'G':
+Gflag:
+ if ((pr = luxgetp(&disp)) == -1)
+ die("luxgetp");
+ printf("%f\n", pr);
+ break;
+ case 'd':
+ case 'i':
+ case 's':
+ 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':
+ case 'I':
+ case 'S':
+ 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]);
+ }
+ }
+
+ luxfree(&disp);
+ return EXIT_SUCCESS;
+}
+
+int
+parseint(char *s)
+{
+ char *endptr;
+ long n = strtol(optarg, &endptr, 0);
+ if (*s != '\0' && *endptr == '\0')
+ return (int) n;
+ diex("parseint: invalid integer '%s'", s);
+}
+
+double
+parsedouble(char *s)
+{
+ char *endptr;
+ double n = strtod(optarg, &endptr);
+ if (*s != '\0' && *endptr == '\0')
+ return n;
+ diex("parsedouble: invalid double '%s'", s);
+}
+
+void
+usage(char *s)
+{
+ fprintf(stderr, "Usage: %s [-gG | -dDiIsS val]\n", s);
+ exit(EXIT_FAILURE);
+}