ctemp

Set color temperature
git clone git://git.akobets.xyz/ctemp
Log | Files | Refs | README | LICENSE

commit 54fb5cec01bba5ff251aba19363a293c20c328b3
parent 7ad3206172bacc79bb10242204bbf315ac4c9140
Author: Artem Kobets <artem@akobets.xyz>
Date:   Mon, 24 Feb 2020 19:28:46 +0200

getopt -> arg.h; add default interface

Diffstat:
MMakefile | 17++++++++++-------
Aarg.h | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig.def.h | 2++
Mconfig.mk | 10+++++-----
Mctemp.c | 65++++++++++++++++++++++++++++++++++++++---------------------------
5 files changed, 104 insertions(+), 39 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,22 +1,25 @@ include config.mk -SRC =\ - colors.c\ - if-drm.c\ - if-quartz.c\ - if-x.c\ - ctemp.c\ +SRC = \ + colors.c \ + if-drm.c \ + if-quartz.c \ + if-x.c \ + ctemp.c \ util.c OBJ = $(SRC:.c=.o) all: ctemp +config.h: + cp config.def.h config.h + .c.o: $(CC) -o $@ -c $< $(CFLAGS) $(OBJ): config.mk ctemp.h util.h -ctemp: $(OBJ) +ctemp: config.h $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) clean: diff --git a/arg.h b/arg.h @@ -0,0 +1,49 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/config.def.h b/config.def.h @@ -0,0 +1,2 @@ +/* use this interface if -i flag is not specified */ +#define DEFAULT_INTERFACE "x" diff --git a/config.mk b/config.mk @@ -25,12 +25,12 @@ X_FLAGS = -DX_SUPPORT INCS = $(DRM_INCS) $(X_INCS) $(QUARTZ_INCS) LIBS = $(DRM_LIBS) $(X_LIBS) $(QUARTZ_INCS) -CPPFLAGS = -DVERSION=\"$(VERSION)\"\ - -D_POSIX_C_SOURCE=200809L\ - -D_XOPEN_SOURCE=700\ +CPPFLAGS = -DVERSION=\"$(VERSION)\" \ + -D_POSIX_C_SOURCE=200809L \ + -D_XOPEN_SOURCE=700 \ $(DRM_FLAGS) $(X_FLAGS) $(QUARTZ_FLAGS) -CFLAGS = -std=c99 -pedantic -Wall -Os $(INCS) $(CPPFLAGS) -LDFLAGS = $(LIBS) +CFLAGS = -std=c99 -pedantic -Wall -Os $(INCS) $(CPPFLAGS) +LDFLAGS = $(LIBS) CC = cc diff --git a/ctemp.c b/ctemp.c @@ -2,20 +2,24 @@ #error "No supported interface configured" #endif -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> -#include <unistd.h> +#include "arg.h" #include "ctemp.h" #include "util.h" #define MAX_INTERFACE_STR_LEN 1024 -static void usage(const char* name, const Interface **interfaces); +static void usage(const Interface **interfaces); + +#include "config.h" + +char *argv0; static void -usage(const char* name, const Interface **interfaces) +usage(const Interface **interfaces) { char ifStr[MAX_INTERFACE_STR_LEN]; unsigned int i; @@ -29,10 +33,12 @@ usage(const char* name, const Interface **interfaces) die( "usage: %s -i interface [temperature]\n" - " interface - one of (%s)\n" + " interface - one of [%s]" + " (default - %s)\n" " temperature - %i-%i, with interval %i, rounded down" " (default - %i)\n", - name, ifStr, TEMP_MIN, TEMP_MAX, TEMP_INTERVAL, TEMP_DEFAULT + argv0, ifStr, DEFAULT_INTERFACE, + TEMP_MIN, TEMP_MAX, TEMP_INTERVAL, TEMP_DEFAULT ); } @@ -51,42 +57,47 @@ main(int argc, char **argv) { NULL }; SetGammaFunc set_gamma; - int opt; + char *if_name; unsigned int i; int temp; set_gamma = NULL; - while ((opt = getopt(argc, argv, "hi:v")) != -1) { - switch (opt) { - case 'h': - usage(argv[0], interfaces); - break; - case 'i': - for (i = 0; interfaces[i] != NULL; i++) { - if (strcmp(optarg, interfaces[i]->name) == 0) - set_gamma = interfaces[i]->set_gamma; + ARGBEGIN { + case 'i': { + if_name = EARGF(usage(interfaces)); + for (i = 0; interfaces[i] != NULL; i++) { + if (strcmp(if_name, interfaces[i]->name) == 0) + set_gamma = interfaces[i]->set_gamma; + } + break; + } + case 'v': + puts("ctemp "VERSION); + return 0; + default: + usage(interfaces); + break; + } ARGEND + + if (set_gamma == NULL) { + for (i = 0; interfaces[i] != NULL; i++) { + if (strcmp(DEFAULT_INTERFACE, interfaces[i]->name) == 0) { + set_gamma = interfaces[i]->set_gamma; } - break; - case 'v': - puts("ctemp "VERSION); - exit(EXIT_SUCCESS); - break; - default: - usage(argv[0], interfaces); } } if (set_gamma == NULL) - usage(argv[0], interfaces); + usage(interfaces); temp = 0; - if (optind == argc) + if (argc == 0) temp = TEMP_DEFAULT; else { - temp = atoi(argv[optind]); + temp = atoi(argv[0]); if (temp < TEMP_MIN || temp > TEMP_MAX) - usage(argv[0], interfaces); + usage(interfaces); } set_gamma(temp);