commit fced537a700b48906b97c66828ef38822b7b9b4d
parent 905d33fff042b399e005c54dfda48690a9283850
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 23 Jun 2023 00:32:32 +0200
Add MIT license and cleanup
Diffstat:
A | LICENSE | | | 21 | +++++++++++++++++++++ |
M | Makefile | | | 30 | ++++++++++++++++++++---------- |
M | sigwmsl.c | | | 34 | ++++++++++++++++++++-------------- |
M | wmsl.c | | | 118 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
4 files changed, 127 insertions(+), 76 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2023 Louis Burda
+
+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.
diff --git a/Makefile b/Makefile
@@ -1,18 +1,19 @@
-CFLAGS = -I.
+PREFIX ?= /usr/local
+BINDIR ?= /bin
+
+CFLAGS = -I . -Wunused-function -Wunused-variable -Wconversion
LDLIBS = -lX11 -lm
-DESTDIR ?=
-BINDIR ?= usr/bin
-.PHONY: all install uninstall
+ifeq ($(DEBUG),1)
+CFLAGS += -Og -g
+else
+CFLAGS += -O2
+endif
all: wmsl sigwmsl
-install:
- install -m 755 sigwmsl wmsl "$(DESTDIR)/$(BINDIR)"
-
-uninstall:
- rm "$(DESTDIR)/$(BINDIR)/wmsl"
- rm "$(DESTDIR)/$(BINDIR)/sigwmsl"
+clean:
+ rm -f wmsl sigwmsl
blocks.h: blocks.def.h
cp blocks.def.h blocks.h
@@ -21,3 +22,12 @@ wmsl: wmsl.c blocks.h
$(CC) -o $@ $< $(CFLAGS) $(LDLIBS)
sigwmsl: sigwmsl.c
+
+install:
+ install -m 755 sigwmsl wmsl -t "$(DESTDIR)$(PREFIX)$(BINDIR)"
+
+uninstall:
+ rm "$(DESTDIR)$(PREFIX)$(BINDIR)/wmsl"
+ rm "$(DESTDIR)$(PREFIX)$(BINDIR)/sigwmsl"
+
+.PHONY: all clean install uninstall
diff --git a/sigwmsl.c b/sigwmsl.c
@@ -1,20 +1,27 @@
+#include <asm-generic/errno-base.h>
+#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
+#include <string.h>
#include <stdlib.h>
-#include <errno.h>
-
-void die(const char *errstr, ...);
-const char* pid_file = "/tmp/.wmsl.pid";
+static const char* pid_file = "/tmp/.wmsl.pid";
-void
-die(const char *errstr, ...)
+static void
+die(const char *fmt, ...)
{
va_list ap;
- va_start(ap, errstr);
- vprintf(errstr, ap);
+ va_start(ap, fmt);
+ fputs("sigwmsl: ", stderr);
+ vprintf(fmt, ap);
+ if (*fmt && fmt[strlen(fmt)-1] == ':') {
+ fputc(' ', stderr);
+ perror(NULL);
+ } else {
+ fputc('\n', stderr);
+ }
va_end(ap);
exit(1);
@@ -23,15 +30,15 @@ die(const char *errstr, ...)
int
main(int argc, const char **argv)
{
- FILE *f;
char buffer[64];
pid_t pid;
sigval_t sigval;
int block_id;
+ FILE *f;
if (argc != 2) {
- printf("USAGE: sigwmsl <block-id>\n");
- return EXIT_SUCCESS;
+ fprintf(stderr, "Usage: sigwmsl <block-id>\n");
+ return 1;
}
block_id = atoi(argv[1]);
@@ -48,10 +55,9 @@ main(int argc, const char **argv)
sigval.sival_int = block_id;
if (sigqueue(pid, SIGALRM, sigval) == -1) {
- switch (errno) {
- case EINVAL:
+ if (errno == EINVAL) {
die("sigwmsl: invalid signal value\n");
- case ESRCH:
+ } else if (errno == ESRCH) {
die("sigwmsl: wmsl is not running\n");
}
}
diff --git a/wmsl.c b/wmsl.c
@@ -31,7 +31,7 @@ struct block {
char output[OUTPUTMAX];
};
-static const char *usage = "USAGE: wmsl [-h] [-v]\n";
+static const char *usage = "Usage: wmsl [-h] [-v]\n";
static const char *pid_file = "/tmp/.wmsl.pid";
static char status_text[STATUSMAX];
@@ -44,40 +44,34 @@ static pid_t pid;
#include "blocks.h"
-void
-die(const char *fmtstr, ...)
+static void
+__attribute__((format(printf,1,2)))
+die(const char *fmt, ...)
{
va_list ap;
- va_start(ap, fmtstr);
- vfprintf(stderr, fmtstr, ap);
+ va_start(ap, fmt);
+ fputs("wmsl: ", stderr);
+ vfprintf(stderr, fmt, ap);
+ if (*fmt && fmt[strlen(fmt)-1] == ':') {
+ fputc(' ', stderr);
+ perror(NULL);
+ } else {
+ fputc('\n', stderr);
+ }
va_end(ap);
exit(EXIT_FAILURE);
}
-void
-set_status_var(const char *str)
-{
- XStoreName(dpy, root, str);
- XFlush(dpy);
-}
-
-void
-clean_status(int sig)
-{
- set_status_var("");
- exit(0);
-}
-
-void
-debug(const char *fmtstr, ...)
+static void
+__attribute__((format(printf,1,2)))
+debug(const char *fmt, ...)
{
char buf[12];
struct tm *tm;
time_t sec;
va_list ap;
- int len;
if (!verbose)
return;
@@ -88,15 +82,30 @@ debug(const char *fmtstr, ...)
strftime(buf, sizeof(buf), "[%T] ", tm);
fprintf(stderr, "%s", buf);
- va_start(ap, fmtstr);
- vfprintf(stderr, fmtstr, ap);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
va_end(ap);
}
-void
+static void
+update_status(const char *str)
+{
+ XStoreName(dpy, root, str);
+ XFlush(dpy);
+}
+
+static void
+clean_status(int sig)
+{
+ update_status("");
+ exit(0);
+}
+
+static void
signal_handler(int sig, siginfo_t *info, void *context)
{
- int i, block_id;
+ int block_id;
+ size_t i;
block_id = info->si_value.sival_int;
if (!block_id || info->si_pid == pid) return;
@@ -111,7 +120,7 @@ signal_handler(int sig, siginfo_t *info, void *context)
}
}
-void
+static void
concat_status(const char *text, size_t len)
{
if (status_len + len + 1 > STATUSMAX)
@@ -123,7 +132,7 @@ concat_status(const char *text, size_t len)
status_text[status_len] = '\0';
}
-int
+static int
run_block(const char *cmd, pid_t *pid)
{
int output[2];
@@ -151,17 +160,18 @@ run_block(const char *cmd, pid_t *pid)
return output[0];
}
-bool
-read_block(int fd, char *buf, ssize_t size)
+static bool
+read_block(int fd, char *buf, size_t size)
{
const uint64_t timeout_ns = 1000000000;
struct timeval timeout;
struct timespec start, stop;
char tmp[256];
fd_set fdset;
- int ret, nread;
+ ssize_t nread;
uint64_t ns;
char *tok;
+ int ret;
clock_gettime(CLOCK_REALTIME, &start);
@@ -171,15 +181,15 @@ read_block(int fd, char *buf, ssize_t size)
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
- timeout.tv_sec = (timeout_ns - ns) / 1000000000;
- timeout.tv_usec = (timeout_ns - ns) / 1000 % 1000000000;
+ timeout.tv_sec = (time_t) (timeout_ns - ns) / 1000000000;
+ timeout.tv_usec = (time_t) (timeout_ns - ns) / 1000 % 1000000000;
ret = select(fd + 1, &fdset, NULL, NULL, &timeout);
clock_gettime(CLOCK_REALTIME, &stop);
- ns = (stop.tv_sec - start.tv_sec) * 1000000000;
- ns += stop.tv_nsec - start.tv_nsec;
+ ns = (uint64_t) ((stop.tv_sec - start.tv_sec) * 1000000000
+ + (stop.tv_nsec - start.tv_nsec));
if (ret < 0 && errno == EINTR) continue;
if (ret <= 0) break;
@@ -188,11 +198,11 @@ read_block(int fd, char *buf, ssize_t size)
nread = read(fd, buf, size);
if (nread <= 0) break;
- tok = memchr(buf, '\n', nread);
+ tok = memchr(buf, '\n', (size_t) nread);
if (tok) *tok = '\0';
buf += nread;
- size -= nread;
+ size -= (size_t) nread;
} else {
nread = read(fd, tmp, sizeof(tmp));
if (nread <= 0) break;
@@ -202,12 +212,14 @@ read_block(int fd, char *buf, ssize_t size)
return tok != NULL;
}
-void
+static void
update_blocks(void)
{
- int i, fd, output_len, update;
+ size_t i, output_len;
char tmp_output[OUTPUTMAX];
pid_t child;
+ bool update;
+ int fd;
debug("checking block output..\n");
@@ -219,7 +231,7 @@ update_blocks(void)
for (status_len = i = 0; i < ARRSIZE(blocks); i++) {
if ((blocks[i].flags & READY) ||
blocks[i].sleep_max && blocks[i].sleep_left <= 0.2) {
- blocks[i].sleep_left = blocks[i].sleep_max;
+ blocks[i].sleep_left = (float) blocks[i].sleep_max;
blocks[i].flags = IDLE;
/* get command output */
@@ -228,10 +240,10 @@ update_blocks(void)
memset(tmp_output, 0, sizeof(tmp_output));
if (read_block(fd, tmp_output, sizeof(tmp_output)-1)) {
output_len = strlen(tmp_output);
- debug("%i: %s\n", i, tmp_output);
+ debug("%lu: %s\n", i, tmp_output);
} else {
output_len = 0;
- debug("%i: NO OUTPUT\n", i);
+ debug("%lu: NO OUTPUT\n", i);
}
if (strcmp(tmp_output, blocks[i].output)) {
@@ -257,10 +269,10 @@ update_blocks(void)
concat_status(suffix, ARRSIZE(suffix) - 1);
- set_status_var(status_text);
+ update_status(status_text);
}
-float
+static float
get_elapsed(void)
{
static struct timespec last = { .tv_nsec = -1 };
@@ -273,17 +285,18 @@ get_elapsed(void)
if (last.tv_nsec == -1)
diff = 0;
else
- diff = cur.tv_sec - last.tv_sec
- + (cur.tv_nsec - last.tv_nsec) / 1000000000.f;
+ diff = (float) (cur.tv_sec - last.tv_sec)
+ + (float) (cur.tv_nsec - last.tv_nsec) / 1000000000.f;
last = cur;
return diff;
}
-void
+static void
sleep_till_next(void)
{
- int i, minsleep;
+ unsigned int minsleep;
+ size_t i;
last_sleep = 0; /* reset last_sleep in case we return early */
for (i = 0, minsleep = 0; i < ARRSIZE(blocks); i++) {
@@ -291,7 +304,7 @@ sleep_till_next(void)
blocks[i].sleep_max && blocks[i].sleep_left <= 0)
return;
if (!minsleep || blocks[i].sleep_max && blocks[i].sleep_left < minsleep)
- minsleep = ceil(blocks[i].sleep_left);
+ minsleep = (unsigned int) ceil(blocks[i].sleep_left);
}
if (minsleep < 1)
@@ -310,7 +323,7 @@ int
main(int argc, const char **argv)
{
struct sigaction sa;
- int i, screen, sleep;
+ int i, screen;
FILE *f;
for (i = 1; i < argc; i++) {
@@ -320,10 +333,11 @@ main(int argc, const char **argv)
verbose = 1;
break;
case 'h':
- printf("%s", usage);
+ fputs(usage, stdout);
return 0;
default:
- die("%s", usage);
+ fputs(usage, stderr);
+ return 1;
}
}