diff options
author | Dominique Martinet <asmadeus@codewreck.org> | 2017-12-29 15:31:04 +0100 |
---|---|---|
committer | Dominique Martinet <asmadeus@codewreck.org> | 2018-01-05 15:36:20 +0100 |
commit | c83900593daace2ef85174163edf2748179e28f2 (patch) | |
tree | 40cdc706c8b1439d630c6b514817c8e6f0b4e4fd /sway/commands/set.c | |
parent | Merge pull request #1552 from martinetd/cleanup (diff) | |
download | sway-c83900593daace2ef85174163edf2748179e28f2.tar.gz sway-c83900593daace2ef85174163edf2748179e28f2.tar.zst sway-c83900593daace2ef85174163edf2748179e28f2.zip |
config: add 'set' command
Diffstat (limited to 'sway/commands/set.c')
-rw-r--r-- | sway/commands/set.c | 71 |
1 files changed, 71 insertions, 0 deletions
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 | } | ||