tAdd TTY backend - spoon - dwm status utility (2f30 fork)
 (HTM) git clone git://src.adamsgaard.dk/spoon
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit a4bf7bc838fe7c71237f8de392148f8fe6328d39
 (DIR) parent 2f7dab475bd35e18fb96a63c7f061a5050bfad1d
 (HTM) Author: sin <sin@2f30.org>
       Date:   Thu, 15 Mar 2018 11:06:45 +0000
       
       Add TTY backend
       
       This makes spoon more usable in general.  There is some added overhead
       in this approach because the external program will have to fork + exec
       spoon but at least a single invocation will gather statistics from all
       modules.
       
       It can now be used easily in the tmux status bar.
       
       Diffstat:
         M Makefile                            |       2 +-
         A arg.h                               |      65 +++++++++++++++++++++++++++++++
         M spoon.c                             |      53 +++++++++++++++++++++++++++++--
       
       3 files changed, 116 insertions(+), 4 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       t@@ -5,7 +5,7 @@ SRC = spoon.c batt.c wifi.c cpu.c count.c temp.c date.c load.c file.c key.c\
        OBJ = spoon.o batt.o wifi.o cpu.o count.o temp.o date.o load.o file.o key.o\
              netspeed.o strlcpy.o strlcat.o stub.o brightness.o
        BIN = spoon
       -DISTFILES = $(SRC) types.h util.h config.def.h Makefile LICENSE configure
       +DISTFILES = $(SRC) arg.h types.h util.h config.def.h Makefile LICENSE configure
        
        include config.mk
        
 (DIR) diff --git a/arg.h b/arg.h
       t@@ -0,0 +1,65 @@
       +/*
       + * 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_)
       +
       +/* Handles obsolete -NUM syntax */
       +#define ARGNUM                                case '0':\
       +                                        case '1':\
       +                                        case '2':\
       +                                        case '3':\
       +                                        case '4':\
       +                                        case '5':\
       +                                        case '6':\
       +                                        case '7':\
       +                                        case '8':\
       +                                        case '9'
       +
       +#define ARGEND                        }\
       +                        }
       +
       +#define ARGC()                argc_
       +
       +#define ARGNUMF()        (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
       +
       +#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])))
       +
       +#define LNGARG()        &argv[0][0]
       +
       +#endif
 (DIR) diff --git a/spoon.c b/spoon.c
       t@@ -1,11 +1,13 @@
        #include <err.h>
        #include <limits.h>
        #include <stdio.h>
       +#include <stdlib.h>
        #include <unistd.h>
        
        #include <X11/keysym.h>
        #include <X11/Xlib.h>
        
       +#include "arg.h"
        #include "util.h"
        
        int battread(void *, char *, size_t);
       t@@ -33,6 +35,10 @@ struct ent {
        #include "types.h"
        #include "config.h"
        
       +char *argv0;
       +int single;
       +int tty;
       +
        int
        dummyread(void *arg, char *buf, size_t len)
        {
       t@@ -59,7 +65,7 @@ entcat(char *line, size_t len)
        }
        
        void
       -loop(void)
       +xloop(void)
        {
                char line[LINE_MAX];
                Display *dpy;
       t@@ -71,13 +77,54 @@ loop(void)
                        entcat(line, sizeof(line));
                        XStoreName(dpy, DefaultRootWindow(dpy), line);
                        XSync(dpy, False);
       +                if (single)
       +                        break;
                        sleep(delay);
                }
        }
        
       +void
       +ttyloop(void)
       +{
       +        char line[LINE_MAX];
       +
       +        for (;;) {
       +                entcat(line, sizeof(line));
       +                puts(line);
       +                if (single)
       +                        break;
       +                sleep(delay);
       +        }
       +}
       +
       +void
       +usage(void)
       +{
       +        fprintf(stderr, "%s: [-st]\n", argv0);
       +        fprintf(stderr, "  -s        One shot mode\n");
       +        fprintf(stderr, "  -t        Use TTY backend\n");
       +        exit(1);
       +}
       +
        int
       -main(void)
       +main(int argc, char *argv[])
        {
       -        loop();
       +        char *end;
       +
       +        ARGBEGIN {
       +        case 's':
       +                single = 1;
       +                break;
       +        case 't':
       +                tty = 1;
       +                break;
       +        default:
       +                usage();
       +        } ARGEND
       +
       +        if (tty)
       +                ttyloop();
       +        else
       +                xloop();
                return 0;
        }