diff --git a/src/config.c b/src/config.c index cdd5991..79e516d 100644 --- a/src/config.c +++ b/src/config.c @@ -877,10 +877,10 @@ void config_free() }; } -void config_generate() +void config_generate(const char *config_dir) { struct dirent *ent; - DIR *dh = opendir(CONFIG_DIR); + DIR *dh = opendir(config_dir); if (!dh) { perror("opendir"); @@ -895,7 +895,7 @@ void config_generate() continue; - sprintf(path, "%s/%s", CONFIG_DIR, ent->d_name); + sprintf(path, "%s/%s", config_dir, ent->d_name); cfg = calloc(1, sizeof(struct keyboard_config)); diff --git a/src/main.c b/src/main.c index b6e15aa..3efd9d8 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,7 @@ #include #include #include +#include #include "keys.h" #include "config.h" @@ -1125,11 +1126,11 @@ static void cleanup() udev_monitor_unref(udevmon); } -static void lock() +static void lock(const char *lock_file) { int fd; - if ((fd = open(LOCK_FILE, O_CREAT | O_RDWR, 0600)) == -1) { + if ((fd = open(lock_file, O_CREAT | O_RDWR, 0600)) == -1) { perror("flock open"); exit(1); } @@ -1150,12 +1151,12 @@ static void exit_signal_handler(int sig) exit(0); } -static void daemonize() +static void daemonize(const char *log_file) { - int fd = open(LOG_FILE, O_APPEND | O_WRONLY); + int fd = open(log_file, O_APPEND | O_WRONLY); info("Daemonizing."); - info("Log output will be stored in %s", LOG_FILE); + info("Log output will be stored in %s", log_file); if (fork()) exit(0); @@ -1178,57 +1179,79 @@ int main(int argc, char *argv[]) dbg("Debug mode enabled."); dbg2("Verbose debugging enabled."); - if (argc > 1) { - if (!strcmp(argv[1], "-v")) { - fprintf(stderr, "keyd version: %s (%s)\n", VERSION, - GIT_COMMIT_HASH); - return 0; - } else if (!strcmp(argv[1], "-m")) { - return monitor_loop(); - } else if (!strcmp(argv[1], "-l")) { - size_t i; - - for (i = 0; i < KEY_MAX; i++) - if (keycode_table[i].name) { - const struct keycode_table_ent *ent - = &keycode_table[i]; - printf("%s\n", ent->name); - if (ent->alt_name) - printf("%s\n", - ent->alt_name); - if (ent->shifted_name) - printf("%s\n", - ent->shifted_name); - } - return 0; - } else { - if (strcmp(argv[1], "-h") - && strcmp(argv[1], "--help")) + char *config_dir = CONFIG_DIR; + char *log_file = LOG_FILE; + char *lock_file = LOCK_FILE; + { + static struct option longopts[] = { + {"help", no_argument, NULL, 'h'}, + {"config-dir", required_argument, NULL, 0}, + {"log-file", required_argument, NULL, 1}, + {"lock-file", required_argument, NULL, 2}, + {NULL, 0, NULL, 0} + }; + int longindex = 0; + int opt; + while ((opt = getopt_long(argc, argv, "vmlhd", longopts, &longindex)) != -1) + switch (opt) { + case 'v': + fprintf(stderr, "keyd version: %s (%s)\n", VERSION, GIT_COMMIT_HASH); + return 0; + case 'm': + return monitor_loop(); + case 'l': + size_t i; + + for (i = 0; i < KEY_MAX; i++) + if (keycode_table[i].name) { + const struct keycode_table_ent *ent = &keycode_table[i]; + printf("%s\n", ent->name); + if (ent->alt_name) + printf("%s\n", ent->alt_name); + if (ent->shifted_name) + printf("%s\n", ent->shifted_name); + } + return 0; + case 'h': fprintf(stderr, - "%s is not a valid option.\n", - argv[1]); - - fprintf(stderr, - "Usage: %s [-m] [-l] [-d]\n\nOptions:\n" - "\t-m monitor mode\n" "\t-l list keys\n" - "\t-d fork and start as a daemon\n" - "\t-v print version\n" - "\t-h print this help message\n", argv[0]); - - return 0; - } + "Usage: %s [-m] [-l] [-d]\n\nOptions:\n" + "\t-m monitor mode\n" + "\t-l list keys\n" + "\t-d fork and start as a daemon\n" + "\t--config-dir \n" + "\t--log-file \n" + "\t--lock-file \n" + "\t-v print version\n" + "\t-h print this help message\n", + argv[0]); + return 0; + case 'd': + daemonize(log_file); + break; + case 0: + config_dir = strdup(optarg); + fprintf(stderr, "Using config directory \"%s\"\n", config_dir); + break; + case 1: + log_file = strdup(optarg); + fprintf(stderr, "Using log file \"%s\"\n", log_file); + break; + case 2: + lock_file = strdup(optarg); + fprintf(stderr, "Using lock file \"%s\"\n", lock_file); + break; + case '?': + return 1; + } } - lock(); + lock(lock_file); signal(SIGINT, exit_signal_handler); signal(SIGTERM, exit_signal_handler); - if (argc > 1 && !strcmp(argv[1], "-d")) - daemonize(); - info("Starting keyd v%s (%s).", VERSION, GIT_COMMIT_HASH); - config_generate(); + config_generate(config_dir); vkbd = create_virtual_keyboard(); vptr = create_virtual_pointer();