diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-01-05 09:40:47 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-05 09:40:47 -0500 |
commit | fdc92e745498a4e0707a51c3fd3390d462e9ae59 (patch) | |
tree | 40cdc706c8b1439d630c6b514817c8e6f0b4e4fd /sway | |
parent | Merge pull request #1552 from martinetd/cleanup (diff) | |
parent | config: add 'set' command (diff) | |
download | sway-fdc92e745498a4e0707a51c3fd3390d462e9ae59.tar.gz sway-fdc92e745498a4e0707a51c3fd3390d462e9ae59.tar.zst sway-fdc92e745498a4e0707a51c3fd3390d462e9ae59.zip |
Merge pull request #1554 from martinetd/cmd_set
config: add 'set' command
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands.c | 3 | ||||
-rw-r--r-- | sway/commands/set.c | 71 | ||||
-rw-r--r-- | sway/config.c | 7 | ||||
-rw-r--r-- | sway/meson.build | 1 |
4 files changed, 80 insertions, 2 deletions
diff --git a/sway/commands.c b/sway/commands.c index c1c6dc5d..f01329db 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -138,6 +138,7 @@ static struct cmd_handler handlers[] = { | |||
138 | { "input", cmd_input }, | 138 | { "input", cmd_input }, |
139 | { "output", cmd_output }, | 139 | { "output", cmd_output }, |
140 | { "seat", cmd_seat }, | 140 | { "seat", cmd_seat }, |
141 | { "set", cmd_set }, | ||
141 | }; | 142 | }; |
142 | 143 | ||
143 | static int handler_compare(const void *_a, const void *_b) { | 144 | static int handler_compare(const void *_a, const void *_b) { |
@@ -290,7 +291,7 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) { | |||
290 | int i; | 291 | int i; |
291 | // Var replacement, for all but first argument of set | 292 | // Var replacement, for all but first argument of set |
292 | // TODO commands | 293 | // TODO commands |
293 | for (i = /*handler->handle == cmd_set ? 2 :*/ 1; i < argc; ++i) { | 294 | for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) { |
294 | argv[i] = do_var_replacement(argv[i]); | 295 | argv[i] = do_var_replacement(argv[i]); |
295 | unescape_string(argv[i]); | 296 | unescape_string(argv[i]); |
296 | } | 297 | } |
diff --git a/sway/commands/set.c b/sway/commands/set.c new file mode 100644 index 00000000..dcd928ba --- /dev/null +++ b/sway/commands/set.c | |||
@@ -0,0 +1,71 @@ | |||
1 | #define _XOPEN_SOURCE 700 | ||
2 | #include <stdio.h> | ||
3 | #include <string.h> | ||
4 | #include <strings.h> | ||
5 | #include "sway/commands.h" | ||
6 | #include "sway/config.h" | ||
7 | #include "list.h" | ||
8 | #include "log.h" | ||
9 | #include "stringop.h" | ||
10 | |||
11 | // sort in order of longest->shortest | ||
12 | static int compare_set_qsort(const void *_l, const void *_r) { | ||
13 | struct sway_variable const *l = *(void **)_l; | ||
14 | struct sway_variable const *r = *(void **)_r; | ||
15 | return strlen(r->name) - strlen(l->name); | ||
16 | } | ||
17 | |||
18 | void free_sway_variable(struct sway_variable *var) { | ||
19 | if (!var) { | ||
20 | return; | ||
21 | } | ||
22 | free(var->name); | ||
23 | free(var->value); | ||
24 | free(var); | ||
25 | } | ||
26 | |||
27 | struct cmd_results *cmd_set(int argc, char **argv) { | ||
28 | char *tmp; | ||
29 | struct cmd_results *error = NULL; | ||
30 | if (!config->reading) return cmd_results_new(CMD_FAILURE, "set", "Can only be used in config file."); | ||
31 | if ((error = checkarg(argc, "set", EXPECTED_AT_LEAST, 2))) { | ||
32 | return error; | ||
33 | } | ||
34 | |||
35 | if (argv[0][0] != '$') { | ||
36 | sway_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]); | ||
37 | |||
38 | size_t size = snprintf(NULL, 0, "$%s", argv[0]); | ||
39 | tmp = malloc(size + 1); | ||
40 | if (!tmp) { | ||
41 | return cmd_results_new(CMD_FAILURE, "set", "Not possible to create variable $'%s'", argv[0]); | ||
42 | } | ||
43 | snprintf(tmp, size+1, "$%s", argv[0]); | ||
44 | |||
45 | argv[0] = tmp; | ||
46 | } | ||
47 | |||
48 | struct sway_variable *var = NULL; | ||
49 | // Find old variable if it exists | ||
50 | int i; | ||
51 | for (i = 0; i < config->symbols->length; ++i) { | ||
52 | var = config->symbols->items[i]; | ||
53 | if (strcmp(var->name, argv[0]) == 0) { | ||
54 | break; | ||
55 | } | ||
56 | var = NULL; | ||
57 | } | ||
58 | if (var) { | ||
59 | free(var->value); | ||
60 | } else { | ||
61 | var = malloc(sizeof(struct sway_variable)); | ||
62 | if (!var) { | ||
63 | return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable"); | ||
64 | } | ||
65 | var->name = strdup(argv[0]); | ||
66 | list_add(config->symbols, var); | ||
67 | list_qsort(config->symbols, compare_set_qsort); | ||
68 | } | ||
69 | var->value = join_args(argv + 1, argc - 1); | ||
70 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
71 | } | ||
diff --git a/sway/config.c b/sway/config.c index 627ed94f..e0a93e19 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -61,7 +61,12 @@ void free_config(struct sway_config *config) { | |||
61 | } | 61 | } |
62 | 62 | ||
63 | // TODO: handle all currently unhandled lists as we add implementations | 63 | // TODO: handle all currently unhandled lists as we add implementations |
64 | list_free(config->symbols); | 64 | if (config->symbols) { |
65 | for (i = 0; i < config->symbols->length; i++) { | ||
66 | free_sway_variable(config->symbols->items[i]); | ||
67 | } | ||
68 | list_free(config->symbols); | ||
69 | } | ||
65 | if (config->modes) { | 70 | if (config->modes) { |
66 | for (i = 0; i < config->modes->length; i++) { | 71 | for (i = 0; i < config->modes->length; i++) { |
67 | free_mode(config->modes->items[i]); | 72 | free_mode(config->modes->items[i]); |
diff --git a/sway/meson.build b/sway/meson.build index 01d5ef36..30ec166b 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -15,6 +15,7 @@ sway_sources = files( | |||
15 | 'commands/seat.c', | 15 | 'commands/seat.c', |
16 | 'commands/seat/attach.c', | 16 | 'commands/seat/attach.c', |
17 | 'commands/seat/fallback.c', | 17 | 'commands/seat/fallback.c', |
18 | 'commands/set.c', | ||
18 | 'commands/input/accel_profile.c', | 19 | 'commands/input/accel_profile.c', |
19 | 'commands/input/click_method.c', | 20 | 'commands/input/click_method.c', |
20 | 'commands/input/drag_lock.c', | 21 | 'commands/input/drag_lock.c', |