[hackers] [xssstate] Refactor xssstate.c
Hello all,
I'd like to propose a refactoring of xssstate.c. It simplifies the
code, removing the need for arg.h, and has strict error checking and
usage rules. Thoughts?
___
diff --git a/arg.h b/arg.h
deleted file mode 100644
index ba3fb3f..0000000
--- a/arg.h
+++ /dev/null
_AT_@ -1,48 +0,0 @@
-/*
- * 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][0] == '-'\
- && argv[0][1];\
- 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/xssstate.c b/xssstate.c
index f6e63a8..ad603e1 100644
--- a/xssstate.c
+++ b/xssstate.c
_AT_@ -1,106 +1,68 @@
-/*
- * See LICENSE file for copyright and license details.
- */
-
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
+/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
-#include <stdbool.h>
-#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <X11/extensions/scrnsaver.h>
-#include "arg.h"
-
-char *argv0;
-
-void
-die(const char *errstr, ...) {
+static void
+die(const char *fmt, ...)
+{
va_list ap;
- va_start(ap, errstr);
- vfprintf(stderr, errstr, ap);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
va_end(ap);
- exit(EXIT_FAILURE);
-}
+ fputc('\n', stderr);
-void
-usage(void)
-{
- die("usage: %s [-istv]\n", basename(argv0));
+ exit(1);
}
int
-main(int argc, char *argv[]) {
- XScreenSaverInfo *info;
+main(int argc, char *argv[])
+{
+ int base, errbase, idle, state;
Display *dpy;
- int base, errbase;
- Bool showstate, showtill, showidle;
-
- showstate = false;
- showtill = false;
- showidle = false;
-
- ARGBEGIN {
- case 'i':
- showidle = true;
- break;
- case 's':
- showstate = true;
- break;
- case 't':
- showtill = true;
- break;
- case 'v':
- die("xssstate-"VERSION", © 2008-2016 xssstate engineers"
- ", see LICENSE for details.\n");
- default:
- usage();
- } ARGEND;
-
- if (!showstate && !showtill && !showidle)
- usage();
-
- if (!(dpy = XOpenDisplay(0)))
- die("Cannot open display.\n");
+ XScreenSaverInfo *info;
+ if (argc != 2)
+ die("usage: xssstate [-i] [-s] [-t] [-v]");
+ if (!strcmp("-v", argv[1]))
+ die("xssstate-"VERSION);
+ idle = 0;
+ state = 0;
+ if (!strcmp("-i", argv[1])) {
+ idle = 1;
+ } else if (!strcmp("-s", argv[1])) {
+ state = 1;
+ } else if (strcmp("-t", argv[1])) {
+ die("usage: xssstate [-i] [-s] [-t] [-v]");
+ }
+ if (!(dpy = XOpenDisplay(NULL)))
+ die("xssstate: cannot open display");
if (!XScreenSaverQueryExtension(dpy, &base, &errbase))
- die("Screensaver extension not activated.\n");
-
- info = XScreenSaverAllocInfo();
- XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
-
- if (showstate) {
- switch(info->state) {
- case ScreenSaverOn:
- printf("on\n");
- break;
- case ScreenSaverOff:
- printf("off\n");
- break;
- case ScreenSaverDisabled:
- printf("disabled\n");
- break;
- }
- } else if (showtill) {
- switch(info->state) {
+ die("xssstate: screensaver extension not available");
+ if (!(info = XScreenSaverAllocInfo()))
+ die("xssstate: insufficient memory available");
+ if (!XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info))
+ die("xssstate: screensaver extension not supported");
+ if (idle) {
+ printf("%lu\n", info->idle);
+ } else {
+ switch (info->state) {
case ScreenSaverOn:
- printf("0\n");
+ state ? printf("on\n") : printf("0\n");
break;
case ScreenSaverOff:
- printf("%lu\n", info->til_or_since);
- break;
- case ScreenSaverDisabled:
- printf("-1\n");
+ state ? printf("off\n") :
+ printf("%lu\n", info->til_or_since);
break;
+ default:
+ state ? printf("disabled\n") : printf("-1\n");
}
- } else if (showidle) {
- printf("%lu\n", info->idle);
}
-
-
XCloseDisplay(dpy);
+ XFree(info);
return 0;
}
-
___
Russ
Received on Sun Sep 11 2016 - 22:17:09 CEST
This archive was generated by hypermail 2.3.0
: Sun Sep 11 2016 - 22:24:15 CEST