From 5ec88a365e2ebf89731a8c210c6afa417ab6bb2d Mon Sep 17 00:00:00 2001 From: Scoopta Date: Fri, 24 Jan 2020 23:36:56 -0800 Subject: [PATCH] Added --define --- inc/config.h | 2 ++ man/wofi.1 | 3 ++ src/config.c | 90 +++++++++++++++++++++++++++++----------------------- src/main.c | 31 +++++++++++++++++- 4 files changed, 85 insertions(+), 41 deletions(-) diff --git a/inc/config.h b/inc/config.h index 3b49a78..16a5f82 100644 --- a/inc/config.h +++ b/inc/config.h @@ -25,6 +25,8 @@ #include #include +void config_put(struct map* map, char* line); + void config_load(struct map* map, const char* config); char* config_get(struct map* config, const char* key, char* def_opt); diff --git a/man/wofi.1 b/man/wofi.1 index 595ca87..735451f 100644 --- a/man/wofi.1 +++ b/man/wofi.1 @@ -91,6 +91,9 @@ Specifies the location. See \fBwofi\fR(7) for more information, default is cente .TP .B \-a, \-\-no\-actions Disables multiple actions for modes that support it. +.TP +.B \-D, \-\-define=\fIKEY=VALUE\fR +Sets a config option .SH CONFIGURATION Wofi has 3 main files used for configuration. All files are completely optional. diff --git a/src/config.c b/src/config.c index 6f7eb3c..197114a 100644 --- a/src/config.c +++ b/src/config.c @@ -17,51 +17,61 @@ #include +void config_put(struct map* map, char* line) { + line = strdup(line); + char* hash = strchr(line, '#'); + if(hash != NULL) { + if(hash == line || *(hash - 1) != '\\') { + *hash = 0; + } + } + char* backslash = strchr(line, '\\'); + size_t backslash_count = 0; + while(backslash != NULL) { + ++backslash_count; + backslash = strchr(backslash + 1, '\\'); + } + size_t line_size = strlen(line); + char* new_line = calloc(1, line_size + 1 - backslash_count); + size_t new_line_count = 0; + for(size_t count = 0; count < line_size; ++count) { + if(line[count] == '\\') { + continue; + } + new_line[new_line_count++] = line[count]; + } + free(line); + line = new_line; + char* equals = strchr(line, '='); + if(equals == NULL) { + free(line); + return; + } + *equals = 0; + char* key = equals - 1; + while(*key == ' ') { + --key; + } + *(key + 1) = 0; + char* value = equals + 1; + while(*value == ' ') { + ++value; + } + size_t len = strlen(value); + char* end = value + len - 1; + if(*end == '\n' || *end == ' ') { + *end = 0; + } + map_put(map, line, value); + free(line); +} + void config_load(struct map* map, const char* config) { FILE* file = fopen(config, "r"); char* line = NULL; size_t size = 0; while(getline(&line, &size, file) != -1) { - char* hash = strchr(line, '#'); - if(hash != NULL) { - if(hash == line || *(hash - 1) != '\\') { - *hash = 0; - } - } - char* backslash = strchr(line, '\\'); - size_t backslash_count = 0; - while(backslash != NULL) { - ++backslash_count; - backslash = strchr(backslash + 1, '\\'); - } - char* new_line = calloc(1, size - backslash_count); - size_t line_size = strlen(line); - size_t new_line_count = 0; - for(size_t count = 0; count < line_size; ++count) { - if(line[count] == '\\') { - continue; - } - new_line[new_line_count++] = line[count]; - } - free(line); - line = new_line; - char* equals = strchr(line, '='); - if(equals == NULL) { - continue; - } - *equals = 0; - char* key = equals - 1; - while(*key == ' ') { - --key; - } - *(key + 1) = 0; - char* value = equals + 1; - while(*value == ' ') { - ++value; - } - size_t len = strlen(value); - *(value + len - 1) = 0; - map_put(map, line, value); + config_put(map, line); } free(line); fclose(file); diff --git a/src/main.c b/src/main.c index dff1b00..bb1337a 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,11 @@ static char* config_path; static char* stylesheet; static char* color_path; +struct option_node { + char* option; + struct wl_list link; +}; + static char* get_exec_name(char* path) { char* slash = strrchr(path, '/'); uint64_t offset; @@ -74,6 +79,7 @@ static void print_usage(char** argv) { printf("--version\t-v\tPrints the version and then exits\n"); printf("--location\t-l\tSets the location\n"); printf("--no-actions\t-a\tDisables multiple actions for modes that support it\n"); + printf("--define\t-D\tSets a config option\n"); exit(0); } @@ -339,6 +345,12 @@ int main(int argc, char** argv) { .flag = NULL, .val = 'a' }, + { + .name = "define", + .has_arg = required_argument, + .flag = NULL, + .val = 'D' + }, { .name = NULL, .has_arg = 0, @@ -369,8 +381,13 @@ int main(int argc, char** argv) { char* parse_search = NULL; char* location = NULL; char* no_actions = NULL; + + struct wl_list options; + wl_list_init(&options); + struct option_node* node; + int opt; - while((opt = getopt_long(argc, argv, "hfc:s:C:dS:W:H:p:x:y:nImk:t:P::ebM:iqvl:a", opts, NULL)) != -1) { + while((opt = getopt_long(argc, argv, "hfc:s:C:dS:W:H:p:x:y:nImk:t:P::ebM:iqvl:aD:", opts, NULL)) != -1) { switch(opt) { case 'h': print_usage(argv); @@ -456,6 +473,11 @@ int main(int argc, char** argv) { case 'a': no_actions = "true"; break; + case 'D': + node = malloc(sizeof(struct option_node)); + node->option = optarg; + wl_list_insert(&options, &node->link); + break; } } @@ -531,6 +553,13 @@ int main(int argc, char** argv) { free(COLORS_LOCATION); + struct option_node* tmp; + wl_list_for_each_safe(node, tmp, &options, link) { + config_put(config, node->option); + wl_list_remove(&node->link); + free(node); + } + if(map_get(config, "show") != NULL) { map_put(config, "mode", map_get(config, "show")); }