diff options
author | Drew DeVault <ddevault@linode.com> | 2015-08-05 17:30:40 -0400 |
---|---|---|
committer | Drew DeVault <ddevault@linode.com> | 2015-08-05 17:30:47 -0400 |
commit | e07c77fbb78b1d57a19904f2f5a7309ddfc40955 (patch) | |
tree | 8d01247340d324cf197706b4f1484c00ef0b1e0c | |
parent | MIT license (diff) | |
download | sway-e07c77fbb78b1d57a19904f2f5a7309ddfc40955.tar.gz sway-e07c77fbb78b1d57a19904f2f5a7309ddfc40955.tar.zst sway-e07c77fbb78b1d57a19904f2f5a7309ddfc40955.zip |
Build out command subsystem
Everyone loves code stolen from your own projects
-rw-r--r-- | sway/commands.c | 98 | ||||
-rw-r--r-- | sway/commands.h | 13 | ||||
-rw-r--r-- | sway/config.c | 15 | ||||
-rw-r--r-- | sway/main.c | 1 |
4 files changed, 125 insertions, 2 deletions
diff --git a/sway/commands.c b/sway/commands.c new file mode 100644 index 00000000..14192c44 --- /dev/null +++ b/sway/commands.c | |||
@@ -0,0 +1,98 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <string.h> | ||
3 | #include <ctype.h> | ||
4 | #include "stringop.h" | ||
5 | #include "commands.h" | ||
6 | |||
7 | int cmd_set(struct sway_config *config, int argc, char **argv) { | ||
8 | return 0; | ||
9 | } | ||
10 | |||
11 | /* Keep alphabetized */ | ||
12 | struct cmd_handler handlers[] = { | ||
13 | { "set", cmd_set } | ||
14 | }; | ||
15 | |||
16 | char **split_directive(char *line, int *argc) { | ||
17 | const char *delimiters = ","; | ||
18 | *argc = 0; | ||
19 | while (isspace(*line) && *line) ++line; | ||
20 | |||
21 | int capacity = 10; | ||
22 | char **parts = malloc(sizeof(char *) * capacity); | ||
23 | |||
24 | if (!*line) return parts; | ||
25 | |||
26 | int in_string = 0, in_character = 0; | ||
27 | int i, j, _; | ||
28 | for (i = 0, j = 0; line[i]; ++i) { | ||
29 | if (line[i] == '\\') { | ||
30 | ++i; | ||
31 | } else if (line[i] == '"' && !in_character) { | ||
32 | in_string = !in_string; | ||
33 | } else if (line[i] == '\'' && !in_string) { | ||
34 | in_character = !in_character; | ||
35 | } else if (!in_character && !in_string) { | ||
36 | if (strchr(delimiters, line[i]) != NULL) { | ||
37 | char *item = malloc(i - j + 1); | ||
38 | strncpy(item, line + j, i - j); | ||
39 | item[i - j] = '\0'; | ||
40 | item = strip_whitespace(item, &_); | ||
41 | if (item[0] == '\0') { | ||
42 | free(item); | ||
43 | } else { | ||
44 | if (*argc == capacity) { | ||
45 | capacity *= 2; | ||
46 | parts = realloc(parts, sizeof(char *) * capacity); | ||
47 | } | ||
48 | parts[*argc] = item; | ||
49 | j = i + 1; | ||
50 | ++*argc; | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | char *item = malloc(i - j + 1); | ||
56 | strncpy(item, line + j, i - j); | ||
57 | item[i - j] = '\0'; | ||
58 | item = strip_whitespace(item, &_); | ||
59 | if (*argc == capacity) { | ||
60 | capacity++; | ||
61 | parts = realloc(parts, sizeof(char *) * capacity); | ||
62 | } | ||
63 | parts[*argc] = item; | ||
64 | ++*argc; | ||
65 | return parts; | ||
66 | } | ||
67 | |||
68 | int handler_compare(const void *_a, const void *_b) { | ||
69 | const struct cmd_handler *a = _a; | ||
70 | const struct cmd_handler *b = _b; | ||
71 | return strcasecmp(a->command, b->command); | ||
72 | } | ||
73 | |||
74 | struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) { | ||
75 | struct cmd_handler d = { .command=line }; | ||
76 | struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare); | ||
77 | return res; | ||
78 | } | ||
79 | |||
80 | int handle_command(struct sway_config *config, char *exec) { | ||
81 | char *ptr, *cmd; | ||
82 | if ((ptr = strchr(exec, ' ')) == NULL) { | ||
83 | cmd = malloc(strlen(exec) + 1); | ||
84 | strcpy(exec, cmd); | ||
85 | } else { | ||
86 | int index = ptr - exec; | ||
87 | cmd = malloc(index + 1); | ||
88 | strncpy(cmd, exec, index); | ||
89 | cmd[index] = '\0'; | ||
90 | } | ||
91 | struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd); | ||
92 | if (handler == NULL) { | ||
93 | return 1; | ||
94 | } | ||
95 | int argc; | ||
96 | char **argv = split_directive(exec + strlen(handler->command), &argc); | ||
97 | return handler->handle(config, argc, argv); | ||
98 | } | ||
diff --git a/sway/commands.h b/sway/commands.h new file mode 100644 index 00000000..f99a5201 --- /dev/null +++ b/sway/commands.h | |||
@@ -0,0 +1,13 @@ | |||
1 | #ifndef _SWAY_COMMANDS_H | ||
2 | #define _SWAY_COMMANDS_H | ||
3 | #include <stdbool.h> | ||
4 | #include "config.h" | ||
5 | |||
6 | struct cmd_handler { | ||
7 | char *command; | ||
8 | int (*handle)(struct sway_config *config, int argc, char **argv); | ||
9 | }; | ||
10 | |||
11 | int handle_command(struct sway_config *config, char *command); | ||
12 | |||
13 | #endif | ||
diff --git a/sway/config.c b/sway/config.c index 3ad1bcf9..bb6533c2 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -1,8 +1,10 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdbool.h> | ||
2 | #include <stdlib.h> | 3 | #include <stdlib.h> |
3 | #include "readline.h" | 4 | #include "readline.h" |
4 | #include "stringop.h" | 5 | #include "stringop.h" |
5 | #include "list.h" | 6 | #include "list.h" |
7 | #include "commands.h" | ||
6 | #include "config.h" | 8 | #include "config.h" |
7 | 9 | ||
8 | struct sway_config *read_config(FILE *file) { | 10 | struct sway_config *read_config(FILE *file) { |
@@ -10,6 +12,8 @@ struct sway_config *read_config(FILE *file) { | |||
10 | config->symbols = create_list(); | 12 | config->symbols = create_list(); |
11 | config->modes = create_list(); | 13 | config->modes = create_list(); |
12 | 14 | ||
15 | int temp_braces = 0; // Temporary: skip all config sections with braces | ||
16 | |||
13 | while (!feof(file)) { | 17 | while (!feof(file)) { |
14 | int _; | 18 | int _; |
15 | char *line = read_line(file); | 19 | char *line = read_line(file); |
@@ -18,8 +22,17 @@ struct sway_config *read_config(FILE *file) { | |||
18 | if (!line[0]) { | 22 | if (!line[0]) { |
19 | goto _continue; | 23 | goto _continue; |
20 | } | 24 | } |
21 | printf("Parsing config line %s\n", line); | 25 | if (temp_braces && line[0] == '}') { |
26 | temp_braces--; | ||
27 | goto _continue; | ||
28 | } | ||
29 | |||
30 | handle_command(config, line); | ||
31 | |||
22 | _continue: | 32 | _continue: |
33 | if (line && line[strlen(line) - 1] == '{') { | ||
34 | temp_braces++; | ||
35 | } | ||
23 | free(line); | 36 | free(line); |
24 | } | 37 | } |
25 | 38 | ||
diff --git a/sway/main.c b/sway/main.c index ab687b17..1058c613 100644 --- a/sway/main.c +++ b/sway/main.c | |||
@@ -30,7 +30,6 @@ int main(int argc, char **argv) { | |||
30 | return 0; | 30 | return 0; |
31 | } | 31 | } |
32 | return 0; | 32 | return 0; |
33 | // TODO: | ||
34 | 33 | ||
35 | static struct wlc_interface interface = { }; | 34 | static struct wlc_interface interface = { }; |
36 | if (!wlc_init(&interface, argc, argv)) { | 35 | if (!wlc_init(&interface, argc, argv)) { |