@@ 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,
};