diff options
author | 2015-08-05 22:10:56 -0400 | |
---|---|---|
committer | 2015-08-05 22:10:56 -0400 | |
commit | d0f1fb71d11a709c55b0ed56a9f35920c1282ec8 (patch) | |
tree | 32edb44c8f44e3cbe52043566c688de48b795730 | |
parent | Build out command subsystem (diff) | |
download | sway-d0f1fb71d11a709c55b0ed56a9f35920c1282ec8.tar.gz sway-d0f1fb71d11a709c55b0ed56a9f35920c1282ec8.tar.zst sway-d0f1fb71d11a709c55b0ed56a9f35920c1282ec8.zip |
Flesh out some command parsing
This implements the `set` command from i3
-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 @@ | |||
1 | #include <stdio.h> | ||
1 | #include <stdlib.h> | 2 | #include <stdlib.h> |
2 | #include <string.h> | 3 | #include <string.h> |
3 | #include <ctype.h> | 4 | #include <ctype.h> |
@@ -5,16 +6,36 @@ | |||
5 | #include "commands.h" | 6 | #include "commands.h" |
6 | 7 | ||
7 | int cmd_set(struct sway_config *config, int argc, char **argv) { | 8 | int cmd_set(struct sway_config *config, int argc, char **argv) { |
9 | if (argc != 2) { | ||
10 | fprintf(stderr, "Invalid set command (expected 2 arguments, got %d)\n", argc); | ||
11 | return 1; | ||
12 | } | ||
13 | struct sway_variable *var = malloc(sizeof(struct sway_variable)); | ||
14 | var->name = malloc(strlen(argv[0]) + 1); | ||
15 | strcpy(var->name, argv[0]); | ||
16 | var->value = malloc(strlen(argv[1]) + 1); | ||
17 | strcpy(var->value, argv[1]); | ||
18 | list_add(config->symbols, var); | ||
19 | return 0; | ||
20 | } | ||
21 | |||
22 | int cmd_bindsym(struct sway_config *config, int argc, char **argv) { | ||
23 | if (argc != 2) { | ||
24 | fprintf(stderr, "Invalid bindsym command (expected 2 arguments, got %d)\n", argc); | ||
25 | return 1; | ||
26 | } | ||
27 | // TODO: parse out keybindings | ||
8 | return 0; | 28 | return 0; |
9 | } | 29 | } |
10 | 30 | ||
11 | /* Keep alphabetized */ | 31 | /* Keep alphabetized */ |
12 | struct cmd_handler handlers[] = { | 32 | struct cmd_handler handlers[] = { |
33 | { "bindsym", cmd_bindsym } | ||
13 | { "set", cmd_set } | 34 | { "set", cmd_set } |
14 | }; | 35 | }; |
15 | 36 | ||
16 | char **split_directive(char *line, int *argc) { | 37 | char **split_directive(char *line, int *argc) { |
17 | const char *delimiters = ","; | 38 | const char *delimiters = " "; |
18 | *argc = 0; | 39 | *argc = 0; |
19 | while (isspace(*line) && *line) ++line; | 40 | while (isspace(*line) && *line) ++line; |
20 | 41 | ||
@@ -94,5 +115,11 @@ int handle_command(struct sway_config *config, char *exec) { | |||
94 | } | 115 | } |
95 | int argc; | 116 | int argc; |
96 | char **argv = split_directive(exec + strlen(handler->command), &argc); | 117 | char **argv = split_directive(exec + strlen(handler->command), &argc); |
97 | return handler->handle(config, argc, argv); | 118 | int ret = handler->handle(config, argc, argv); |
119 | int i; | ||
120 | for (i = 0; i < argc; ++i) { | ||
121 | free(argv[i]); | ||
122 | } | ||
123 | free(argv); | ||
124 | return ret; | ||
98 | } | 125 | } |
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) { | |||
11 | struct sway_config *config = malloc(sizeof(struct sway_config)); | 11 | struct sway_config *config = malloc(sizeof(struct sway_config)); |
12 | config->symbols = create_list(); | 12 | config->symbols = create_list(); |
13 | config->modes = create_list(); | 13 | config->modes = create_list(); |
14 | config->current_mode = malloc(sizeof(struct sway_mode)); | ||
15 | config->current_mode->name = NULL; | ||
16 | config->current_mode->bindings = create_list(); | ||
17 | list_add(config->modes, config->current_mode); | ||
14 | 18 | ||
15 | int temp_braces = 0; // Temporary: skip all config sections with braces | 19 | bool success = true; |
20 | |||
21 | int temp_depth = 0; // Temporary: skip all config sections with depth | ||
16 | 22 | ||
17 | while (!feof(file)) { | 23 | while (!feof(file)) { |
18 | int _; | 24 | int _; |
@@ -22,19 +28,25 @@ struct sway_config *read_config(FILE *file) { | |||
22 | if (!line[0]) { | 28 | if (!line[0]) { |
23 | goto _continue; | 29 | goto _continue; |
24 | } | 30 | } |
25 | if (temp_braces && line[0] == '}') { | 31 | if (temp_depth && line[0] == '}') { |
26 | temp_braces--; | 32 | temp_depth--; |
27 | goto _continue; | 33 | goto _continue; |
28 | } | 34 | } |
29 | 35 | ||
30 | handle_command(config, line); | 36 | if (!handle_command(config, line)) { |
37 | success = false; | ||
38 | } | ||
31 | 39 | ||
32 | _continue: | 40 | _continue: |
33 | if (line && line[strlen(line) - 1] == '{') { | 41 | if (line && line[strlen(line) - 1] == '{') { |
34 | temp_braces++; | 42 | temp_depth++; |
35 | } | 43 | } |
36 | free(line); | 44 | free(line); |
37 | } | 45 | } |
38 | 46 | ||
47 | if (!success) { | ||
48 | exit(1); | ||
49 | } | ||
50 | |||
39 | return config; | 51 | return config; |
40 | } | 52 | } |
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 @@ | |||
5 | #include <wlc/wlc.h> | 5 | #include <wlc/wlc.h> |
6 | #include "list.h" | 6 | #include "list.h" |
7 | 7 | ||
8 | struct sway_variable { | ||
9 | char *name; | ||
10 | char *value; | ||
11 | }; | ||
12 | |||
8 | struct sway_binding { | 13 | struct sway_binding { |
9 | list_t *keys; | 14 | list_t *keys; |
10 | struct wlc_modifiers modifiers; | 15 | struct wlc_modifiers modifiers; |
@@ -19,6 +24,7 @@ struct sway_mode { | |||
19 | struct sway_config { | 24 | struct sway_config { |
20 | list_t *symbols; | 25 | list_t *symbols; |
21 | list_t *modes; | 26 | list_t *modes; |
27 | struct sway_mode *current_mode; | ||
22 | }; | 28 | }; |
23 | 29 | ||
24 | struct sway_config *read_config(FILE *file); | 30 | 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) { | |||
40 | } else if (str[i] == '\'' && !in_string) { | 40 | } else if (str[i] == '\'' && !in_string) { |
41 | in_character = !in_character; | 41 | in_character = !in_character; |
42 | } else if (!in_character && !in_string) { | 42 | } else if (!in_character && !in_string) { |
43 | if (str[i] == '#') { | 43 | if (str[i] == '#' && i == 0) { |
44 | str[i] = '\0'; | 44 | str[i] = '\0'; |
45 | break; | 45 | break; |
46 | } | 46 | } |