~rootmos/action

47cf241b4b03d728e8e4155418b09fdb5fe553b5 — Gustav Behm 5 months ago b19c837
Parse options
1 files changed, 59 insertions(+), 0 deletions(-)

M c-impl/action.c
M c-impl/action.c => c-impl/action.c +59 -0
@@ 5,6 5,62 @@
#define LIBR_IMPLEMENTATION
#include "r.h"

struct options {
    const char* action;

    enum {
        NOOP = 0,
        BIND,
        TRIGGER,
    } cmd;
};

static void print_usage(int fd, const char* prog)
{
    dprintf(fd, "usage: %s [OPTION]... ACTION\n", prog);
    dprintf(fd, "\n");
    dprintf(fd, "commands:\n");
    dprintf(fd, "  -b       bind action\n");
    dprintf(fd, "  -t       trigger action\n");
    dprintf(fd, "  -h       print this message\n");
}

static void parse_options(struct options* o, int argc, char* argv[])
{
    memset(o, 0, sizeof(*o));

    int res;
    while((res = getopt(argc, argv, "bth")) != -1) {
        switch(res) {
        case 't': o->cmd = TRIGGER; break;
        case 'b': o->cmd = BIND; break;
        case 'h':
        default:
            print_usage(res == 'h' ? 1 : 2, argv[0]);
            exit(res == 'h' ? 0 : 2);
        }
    }

    if(optind + 1 == argc) {
        o->action = argv[optind];
        debug("action: %s", o->action);
    } else {
        if(optind >= argc) {
            dprintf(2, "error: action not specified\n");
        } else {
            dprintf(2, "error: too many actions specified\n");
        }
        print_usage(2, argv[0]);
        exit(2);
    }

    if(o->cmd == NOOP) {
        dprintf(2, "error: command not specified\n");
        print_usage(2, argv[0]);
        exit(2);
    }
}

struct state {
    int running;



@@ 69,6 125,9 @@ static void signalfd_handle_event(struct state* st)

int main(int argc, char* argv[])
{
    struct options o;
    parse_options(&o, argc, argv);

    struct state st = {
        .running = 1,
    };