diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-08-05 22:10:56 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-08-05 22:10:56 -0400 |
commit | d0f1fb71d11a709c55b0ed56a9f35920c1282ec8 (patch) | |
tree | 32edb44c8f44e3cbe52043566c688de48b795730 /sway | |
parent | e07c77fbb78b1d57a19904f2f5a7309ddfc40955 (diff) |
Flesh out some command parsing
This implements the `set` command from i3
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands.c | 31 | ||||
-rw-r--r-- | sway/config.c | 22 | ||||
-rw-r--r-- | sway/config.h | 6 | ||||
-rw-r--r-- | sway/stringop.c | 2 |
4 files changed, 53 insertions, 8 deletions
diff --git a/sway/commands.c b/sway/commands.c index 14192c44..cb24f796 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -1,3 +1,4 @@ +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> @@ -5,16 +6,36 @@ #include "commands.h" int cmd_set(struct sway_config *config, int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "Invalid set command (expected 2 arguments, got %d)\n", argc); + return 1; + } + struct sway_variable *var = malloc(sizeof(struct sway_variable)); + var->name = malloc(strlen(argv[0]) + 1); + strcpy(var->name, argv[0]); + var->value = malloc(strlen(argv[1]) + 1); + strcpy(var->value, argv[1]); + list_add(config->symbols, var); + return 0; +} + +int cmd_bindsym(struct sway_config *config, int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "Invalid bindsym command (expected 2 arguments, got %d)\n", argc); + return 1; + } + // TODO: parse out keybindings return 0; } /* Keep alphabetized */ struct cmd_handler handlers[] = { + { "bindsym", cmd_bindsym } { "set", cmd_set } }; char **split_directive(char *line, int *argc) { - const char *delimiters = ","; + const char *delimiters = " "; *argc = 0; while (isspace(*line) && *line) ++line; @@ -94,5 +115,11 @@ int handle_command(struct sway_config *config, char *exec) { } int argc; char **argv = split_directive(exec + strlen(handler->command), &argc); - return handler->handle(config, argc, argv); + int ret = handler->handle(config, argc, argv); + int i; + for (i = 0; i < argc; ++i) { + free(argv[i]); + } + free(argv); + return ret; } diff --git a/sway/config.c b/sway/config.c index bb6533c2..7242be8d 100644 --- a/sway/config.c +++ b/sway/config.c @@ -11,8 +11,14 @@ struct sway_config *read_config(FILE *file) { struct sway_config *config = malloc(sizeof(struct sway_config)); config->symbols = create_list(); config->modes = create_list(); + config->current_mode = malloc(sizeof(struct sway_mode)); + config->current_mode->name = NULL; + config->current_mode->bindings = create_list(); + list_add(config->modes, config->current_mode); - int temp_braces = 0; // Temporary: skip all config sections with braces + bool success = true; + + int temp_depth = 0; // Temporary: skip all config sections with depth while (!feof(file)) { int _; @@ -22,19 +28,25 @@ struct sway_config *read_config(FILE *file) { if (!line[0]) { goto _continue; } - if (temp_braces && line[0] == '}') { - temp_braces--; + if (temp_depth && line[0] == '}') { + temp_depth--; goto _continue; } - handle_command(config, line); + if (!handle_command(config, line)) { + success = false; + } _continue: if (line && line[strlen(line) - 1] == '{') { - temp_braces++; + temp_depth++; } free(line); } + if (!success) { + exit(1); + } + return config; } diff --git a/sway/config.h b/sway/config.h index 2fe566f0..b2475871 100644 --- a/sway/config.h +++ b/sway/config.h @@ -5,6 +5,11 @@ #include <wlc/wlc.h> #include "list.h" +struct sway_variable { + char *name; + char *value; +}; + struct sway_binding { list_t *keys; struct wlc_modifiers modifiers; @@ -19,6 +24,7 @@ struct sway_mode { struct sway_config { list_t *symbols; list_t *modes; + struct sway_mode *current_mode; }; struct sway_config *read_config(FILE *file); diff --git a/sway/stringop.c b/sway/stringop.c index 0e7ad6a9..4b05e657 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -40,7 +40,7 @@ char *strip_comments(char *str) { } else if (str[i] == '\'' && !in_string) { in_character = !in_character; } else if (!in_character && !in_string) { - if (str[i] == '#') { + if (str[i] == '#' && i == 0) { str[i] = '\0'; break; } |