Added --define

This commit is contained in:
Scoopta 2020-01-24 23:36:56 -08:00
parent d57ae47057
commit 5ec88a365e
4 changed files with 85 additions and 41 deletions

View File

@ -25,6 +25,8 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
void config_put(struct map* map, char* line);
void config_load(struct map* map, const char* config); void config_load(struct map* map, const char* config);
char* config_get(struct map* config, const char* key, char* def_opt); char* config_get(struct map* config, const char* key, char* def_opt);

View File

@ -91,6 +91,9 @@ Specifies the location. See \fBwofi\fR(7) for more information, default is cente
.TP .TP
.B \-a, \-\-no\-actions .B \-a, \-\-no\-actions
Disables multiple actions for modes that support it. Disables multiple actions for modes that support it.
.TP
.B \-D, \-\-define=\fIKEY=VALUE\fR
Sets a config option
.SH CONFIGURATION .SH CONFIGURATION
Wofi has 3 main files used for configuration. All files are completely optional. Wofi has 3 main files used for configuration. All files are completely optional.

View File

@ -17,11 +17,8 @@
#include <config.h> #include <config.h>
void config_load(struct map* map, const char* config) { void config_put(struct map* map, char* line) {
FILE* file = fopen(config, "r"); line = strdup(line);
char* line = NULL;
size_t size = 0;
while(getline(&line, &size, file) != -1) {
char* hash = strchr(line, '#'); char* hash = strchr(line, '#');
if(hash != NULL) { if(hash != NULL) {
if(hash == line || *(hash - 1) != '\\') { if(hash == line || *(hash - 1) != '\\') {
@ -34,8 +31,8 @@ void config_load(struct map* map, const char* config) {
++backslash_count; ++backslash_count;
backslash = strchr(backslash + 1, '\\'); backslash = strchr(backslash + 1, '\\');
} }
char* new_line = calloc(1, size - backslash_count);
size_t line_size = strlen(line); size_t line_size = strlen(line);
char* new_line = calloc(1, line_size + 1 - backslash_count);
size_t new_line_count = 0; size_t new_line_count = 0;
for(size_t count = 0; count < line_size; ++count) { for(size_t count = 0; count < line_size; ++count) {
if(line[count] == '\\') { if(line[count] == '\\') {
@ -47,7 +44,8 @@ void config_load(struct map* map, const char* config) {
line = new_line; line = new_line;
char* equals = strchr(line, '='); char* equals = strchr(line, '=');
if(equals == NULL) { if(equals == NULL) {
continue; free(line);
return;
} }
*equals = 0; *equals = 0;
char* key = equals - 1; char* key = equals - 1;
@ -60,8 +58,20 @@ void config_load(struct map* map, const char* config) {
++value; ++value;
} }
size_t len = strlen(value); size_t len = strlen(value);
*(value + len - 1) = 0; char* end = value + len - 1;
if(*end == '\n' || *end == ' ') {
*end = 0;
}
map_put(map, line, value); 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) {
config_put(map, line);
} }
free(line); free(line);
fclose(file); fclose(file);

View File

@ -34,6 +34,11 @@ static char* config_path;
static char* stylesheet; static char* stylesheet;
static char* color_path; static char* color_path;
struct option_node {
char* option;
struct wl_list link;
};
static char* get_exec_name(char* path) { static char* get_exec_name(char* path) {
char* slash = strrchr(path, '/'); char* slash = strrchr(path, '/');
uint64_t offset; 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("--version\t-v\tPrints the version and then exits\n");
printf("--location\t-l\tSets the location\n"); printf("--location\t-l\tSets the location\n");
printf("--no-actions\t-a\tDisables multiple actions for modes that support it\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); exit(0);
} }
@ -339,6 +345,12 @@ int main(int argc, char** argv) {
.flag = NULL, .flag = NULL,
.val = 'a' .val = 'a'
}, },
{
.name = "define",
.has_arg = required_argument,
.flag = NULL,
.val = 'D'
},
{ {
.name = NULL, .name = NULL,
.has_arg = 0, .has_arg = 0,
@ -369,8 +381,13 @@ int main(int argc, char** argv) {
char* parse_search = NULL; char* parse_search = NULL;
char* location = NULL; char* location = NULL;
char* no_actions = NULL; char* no_actions = NULL;
struct wl_list options;
wl_list_init(&options);
struct option_node* node;
int opt; 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) { switch(opt) {
case 'h': case 'h':
print_usage(argv); print_usage(argv);
@ -456,6 +473,11 @@ int main(int argc, char** argv) {
case 'a': case 'a':
no_actions = "true"; no_actions = "true";
break; 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); 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) { if(map_get(config, "show") != NULL) {
map_put(config, "mode", map_get(config, "show")); map_put(config, "mode", map_get(config, "show"));
} }