aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-11-22 21:20:41 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2017-11-22 21:20:41 -0500
commitd7d21bb0f895248cafefc3d12e4aed033a8e5d17 (patch)
treef1cf994ea711080c15fed5210054b491ba3c1d05 /sway/commands.c
parentAdd views to tree and render them (diff)
downloadsway-d7d21bb0f895248cafefc3d12e4aed033a8e5d17.tar.gz
sway-d7d21bb0f895248cafefc3d12e4aed033a8e5d17.tar.zst
sway-d7d21bb0f895248cafefc3d12e4aed033a8e5d17.zip
Add initial command subsystem (untested)
Need to spin up the IPC server to test this
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c247
1 files changed, 247 insertions, 0 deletions
diff --git a/sway/commands.c b/sway/commands.c
new file mode 100644
index 00000000..f8c2fd99
--- /dev/null
+++ b/sway/commands.c
@@ -0,0 +1,247 @@
1#define _XOPEN_SOURCE 500
2#include <ctype.h>
3#include <stdarg.h>
4#include <stdlib.h>
5#include <string.h>
6#include <strings.h>
7#include <stdio.h>
8#include <json-c/json.h>
9#include "sway/commands.h"
10#include "stringop.h"
11#include "log.h"
12
13struct cmd_handler {
14 char *command;
15 sway_cmd *handle;
16};
17
18// Returns error object, or NULL if check succeeds.
19struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
20 struct cmd_results *error = NULL;
21 switch (type) {
22 case EXPECTED_MORE_THAN:
23 if (argc > val) {
24 return NULL;
25 }
26 error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
27 "(expected more than %d argument%s, got %d)",
28 name, val, (char*[2]){"s", ""}[argc==1], argc);
29 break;
30 case EXPECTED_AT_LEAST:
31 if (argc >= val) {
32 return NULL;
33 }
34 error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
35 "(expected at least %d argument%s, got %d)",
36 name, val, (char*[2]){"s", ""}[argc==1], argc);
37 break;
38 case EXPECTED_LESS_THAN:
39 if (argc < val) {
40 return NULL;
41 };
42 error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
43 "(expected less than %d argument%s, got %d)",
44 name, val, (char*[2]){"s", ""}[argc==1], argc);
45 break;
46 case EXPECTED_EQUAL_TO:
47 if (argc == val) {
48 return NULL;
49 };
50 error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
51 "(expected %d arguments, got %d)", name, val, argc);
52 break;
53 }
54 return error;
55}
56
57/**
58 * Check and add color to buffer.
59 *
60 * return error object, or NULL if color is valid.
61 */
62struct cmd_results *add_color(const char *name, char *buffer, const char *color) {
63 int len = strlen(color);
64 if (len != 7 && len != 9) {
65 return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
66 }
67
68 if (color[0] != '#') {
69 return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
70 }
71
72 int i;
73 for (i = 1; i < len; ++i) {
74 if (!isxdigit(color[i])) {
75 return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
76 }
77 }
78
79 // copy color to buffer
80 strncpy(buffer, color, len);
81 // add default alpha channel if color was defined without it
82 if (len == 7) {
83 buffer[7] = 'f';
84 buffer[8] = 'f';
85 }
86 buffer[9] = '\0';
87
88 return NULL;
89}
90
91/* Keep alphabetized */
92static struct cmd_handler handlers[] = {
93 { "exit", cmd_exit },
94};
95
96static int handler_compare(const void *_a, const void *_b) {
97 const struct cmd_handler *a = _a;
98 const struct cmd_handler *b = _b;
99 return strcasecmp(a->command, b->command);
100}
101
102static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
103 struct cmd_handler d = { .command=line };
104 struct cmd_handler *res = NULL;
105 sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT);
106 /* TODO
107 if (block == CMD_BLOCK_BAR) {
108 res = bsearch(&d, bar_handlers,
109 sizeof(bar_handlers) / sizeof(struct cmd_handler),
110 sizeof(struct cmd_handler), handler_compare);
111 } else if (block == CMD_BLOCK_BAR_COLORS){
112 res = bsearch(&d, bar_colors_handlers,
113 sizeof(bar_colors_handlers) / sizeof(struct cmd_handler),
114 sizeof(struct cmd_handler), handler_compare);
115 } else if (block == CMD_BLOCK_INPUT) {
116 res = bsearch(&d, input_handlers,
117 sizeof(input_handlers) / sizeof(struct cmd_handler),
118 sizeof(struct cmd_handler), handler_compare);
119 } else if (block == CMD_BLOCK_IPC) {
120 res = bsearch(&d, ipc_handlers,
121 sizeof(ipc_handlers) / sizeof(struct cmd_handler),
122 sizeof(struct cmd_handler), handler_compare);
123 } else if (block == CMD_BLOCK_IPC_EVENTS) {
124 res = bsearch(&d, ipc_event_handlers,
125 sizeof(ipc_event_handlers) / sizeof(struct cmd_handler),
126 sizeof(struct cmd_handler), handler_compare);
127 } else {
128 */
129 res = bsearch(&d, handlers,
130 sizeof(handlers) / sizeof(struct cmd_handler),
131 sizeof(struct cmd_handler), handler_compare);
132 //}
133 return res;
134}
135
136struct cmd_results *handle_command(char *_exec) {
137 // Even though this function will process multiple commands we will only
138 // return the last error, if any (for now). (Since we have access to an
139 // error string we could e.g. concatonate all errors there.)
140 struct cmd_results *results = NULL;
141 char *exec = strdup(_exec);
142 char *head = exec;
143 char *cmdlist;
144 char *cmd;
145
146 head = exec;
147 do {
148 // Split command list
149 cmdlist = argsep(&head, ";");
150 cmdlist += strspn(cmdlist, whitespace);
151 do {
152 // Split commands
153 cmd = argsep(&cmdlist, ",");
154 cmd += strspn(cmd, whitespace);
155 if (strcmp(cmd, "") == 0) {
156 sway_log(L_INFO, "Ignoring empty command.");
157 continue;
158 }
159 sway_log(L_INFO, "Handling command '%s'", cmd);
160 //TODO better handling of argv
161 int argc;
162 char **argv = split_args(cmd, &argc);
163 if (strcmp(argv[0], "exec") != 0) {
164 int i;
165 for (i = 1; i < argc; ++i) {
166 if (*argv[i] == '\"' || *argv[i] == '\'') {
167 strip_quotes(argv[i]);
168 }
169 }
170 }
171 struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END);
172 if (!handler) {
173 if (results) {
174 free_cmd_results(results);
175 }
176 results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
177 free_argv(argc, argv);
178 goto cleanup;
179 }
180 free_argv(argc, argv);
181 } while(cmdlist);
182 } while(head);
183cleanup:
184 free(exec);
185 if (!results) {
186 results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
187 }
188 return results;
189}
190
191struct cmd_results *cmd_results_new(enum cmd_status status,
192 const char *input, const char *format, ...) {
193 struct cmd_results *results = malloc(sizeof(struct cmd_results));
194 if (!results) {
195 sway_log(L_ERROR, "Unable to allocate command results");
196 return NULL;
197 }
198 results->status = status;
199 if (input) {
200 results->input = strdup(input); // input is the command name
201 } else {
202 results->input = NULL;
203 }
204 if (format) {
205 char *error = malloc(256);
206 va_list args;
207 va_start(args, format);
208 if (error) {
209 vsnprintf(error, 256, format, args);
210 }
211 va_end(args);
212 results->error = error;
213 } else {
214 results->error = NULL;
215 }
216 return results;
217}
218
219void free_cmd_results(struct cmd_results *results) {
220 if (results->input) {
221 free(results->input);
222 }
223 if (results->error) {
224 free(results->error);
225 }
226 free(results);
227}
228
229const char *cmd_results_to_json(struct cmd_results *results) {
230 json_object *result_array = json_object_new_array();
231 json_object *root = json_object_new_object();
232 json_object_object_add(root, "success",
233 json_object_new_boolean(results->status == CMD_SUCCESS));
234 if (results->input) {
235 json_object_object_add(
236 root, "input", json_object_new_string(results->input));
237 }
238 if (results->error) {
239 json_object_object_add(
240 root, "error", json_object_new_string(results->error));
241 }
242 json_object_array_add(result_array, root);
243 const char *json = json_object_to_json_string(result_array);
244 free(result_array);
245 free(root);
246 return json;
247}