From de727ff75340c040a04e70348d51c21855b2e49f Mon Sep 17 00:00:00 2001 From: "Jeffrey E. Bedard" Date: Thu, 8 Jun 2017 03:00:59 -0400 Subject: [PATCH] Moved usage to a separate data file, to be read from the installation directory or from the current directory. --- Makefile | 8 +++++++- main.c | 49 +++++++++++++++++++++++++++---------------------- usage.txt | 7 +++++++ 3 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 usage.txt diff --git a/Makefile b/Makefile index aa32104..ce1aa10 100644 --- a/Makefile +++ b/Makefile @@ -17,16 +17,21 @@ LDFLAGS+=-L/usr/X11R7/lib LDFLAGS+=-Wl,-R/usr/X11R7/lib LDFLAGS+=-Wl,-R/usr/X11R6/lib LDFLAGS+=-lX11 -lXxf86vm + PROG=batwarn + prefix=${DESTDIR}${PREFIX} bindir=${prefix}/bin docdir=${prefix}/share/doc/${PROG} datadir=${prefix}/share/${PROG} -all: ${PROG} + OBJS=batwarn.o gamma.o main.o util.o version.o CFLAGS+=-std=c99 CFLAGS+=-D_XOPEN_SOURCE=700 +CFLAGS+=-DBATWARN_USAGE=\"${docdir}/usage.txt\" CFLAGS+=${debug_flags} + +all: ${PROG} ${PROG}: ${OBJS} gam sgam ${CC} -o ${PROG} ${OBJS} ${LDFLAGS} gamobj=gam.o gamma.o @@ -46,6 +51,7 @@ install: install -s sgam ${bindir} install -m 0644 LICENSE ${docdir} install -m 0644 README.md ${docdir} + install -m 0644 usage.txt ${docdir} cppcheck: cppcheck --enable=all --inconclusive --std=c11 . \ -DDEBUG 2> cppcheck.log diff --git a/main.c b/main.c index df3bcfe..f20b4f5 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ // batwarn - (C) 2015-2017 Jeffrey E. Bedard #include "batwarn.h" +#include #include #include #include @@ -10,28 +11,30 @@ static void exit_cb(void) { batwarn_set_gamma(BATWARN_GAMMA_NORMAL); } -__attribute__((noreturn)) -static void usage(char * arg0, const char * optstr, - int sz, const int8_t ec) +static void print_version(void) { - { - uint8_t l = 0; - while (arg0[++l]); - write(1, arg0, l); + int s; + const char * v = batwarn_get_version(&s); + write(1, v, s); +} +_Noreturn void usage(const int ec) +{ + int fd = open(BATWARN_USAGE, O_RDONLY); + /* The following is useful for uninstalled binaries, such as on + * systems on which you do not have root access, and for viewing + * usage prior to installation. */ + if (fd < 0) // fail to the current directory: + fd = open("usage.txt", O_RDONLY); + if (fd >= 0) { + /* Note: Adjust this size upwards if usage.txt + no longer fits in 512 bytes. */ + enum { SZ = 512 }; + char buf[SZ]; + // Write how much we read. + write(1, buf, read(fd, buf, SZ)); + // No need to close since exit will do it for us... } - write(1, " -", 2); - write(1, optstr, sz); - write(1, "\n", 1); - const char helptext[] = - "-d\t\tDo not fork a daemon; run in the foreground.\n" - "-h\t\tShow this usage information.\n" - "-H\t\tEnable hibernation at critical battery level.\n" - "-p PERCENT\tSet the warning percent for gamma change.\n" - "-s\t\tEnable suspend at critical battery level.\n" - "Copyright 2017, Jeffrey E. Bedard \n"; - write(1, helptext, sizeof(helptext)); - const char * version = batwarn_get_version(&sz); - write(1, version, sz); + print_version(); exit(ec); } static uint8_t parse_argv(int argc, char ** argv, uint8_t flags) @@ -53,9 +56,11 @@ static uint8_t parse_argv(int argc, char ** argv, uint8_t flags) flags |= BATWARN_SUSPEND; break; case 'h': // help - usage(*argv, optstr, sizeof(optstr), 0); + usage(0); + //usage(*argv, optstr, sizeof(optstr), 0); default: // usage - usage(*argv, optstr, sizeof(optstr), 1); + usage(1); + ///usage(*argv, optstr, sizeof(optstr), 1); } return flags; } diff --git a/usage.txt b/usage.txt new file mode 100644 index 0000000..194264f --- /dev/null +++ b/usage.txt @@ -0,0 +1,7 @@ +batwarn -dhHp:s +-d Do not fork a daemon; run in the foreground. +-h Show this usage information. +-H Enable hibernation at critical battery level. +-p PERCENT Set the warning percent for gamma change. +-s Enable suspend at critical battery level. +Copyright 2017, Jeffrey E. Bedard