diff options
Diffstat (limited to 'sway/commands.c')
-rw-r--r-- | sway/commands.c | 67 |
1 files changed, 28 insertions, 39 deletions
diff --git a/sway/commands.c b/sway/commands.c index bdf9fe83..1203f63a 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -215,12 +215,9 @@ static void set_config_node(struct sway_node *node) { | |||
215 | } | 215 | } |
216 | } | 216 | } |
217 | 217 | ||
218 | struct cmd_results *execute_command(char *_exec, struct sway_seat *seat, | 218 | list_t *execute_command(char *_exec, struct sway_seat *seat, |
219 | struct sway_container *con) { | 219 | struct sway_container *con) { |
220 | // Even though this function will process multiple commands we will only | 220 | list_t *res_list = create_list(); |
221 | // return the last error, if any (for now). (Since we have access to an | ||
222 | // error string we could e.g. concatenate all errors there.) | ||
223 | struct cmd_results *results = NULL; | ||
224 | char *exec = strdup(_exec); | 221 | char *exec = strdup(_exec); |
225 | char *head = exec; | 222 | char *head = exec; |
226 | char *cmdlist; | 223 | char *cmdlist; |
@@ -254,8 +251,8 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat, | |||
254 | char *error = NULL; | 251 | char *error = NULL; |
255 | struct criteria *criteria = criteria_parse(head, &error); | 252 | struct criteria *criteria = criteria_parse(head, &error); |
256 | if (!criteria) { | 253 | if (!criteria) { |
257 | results = cmd_results_new(CMD_INVALID, head, | 254 | list_add(res_list, cmd_results_new(CMD_INVALID, head, |
258 | "%s", error); | 255 | "%s", error)); |
259 | free(error); | 256 | free(error); |
260 | goto cleanup; | 257 | goto cleanup; |
261 | } | 258 | } |
@@ -291,10 +288,8 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat, | |||
291 | } | 288 | } |
292 | struct cmd_handler *handler = find_handler(argv[0], NULL, 0); | 289 | struct cmd_handler *handler = find_handler(argv[0], NULL, 0); |
293 | if (!handler) { | 290 | if (!handler) { |
294 | if (results) { | 291 | list_add(res_list, cmd_results_new(CMD_INVALID, cmd, |
295 | free_cmd_results(results); | 292 | "Unknown/invalid command")); |
296 | } | ||
297 | results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command"); | ||
298 | free_argv(argc, argv); | 293 | free_argv(argc, argv); |
299 | goto cleanup; | 294 | goto cleanup; |
300 | } | 295 | } |
@@ -308,29 +303,21 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat, | |||
308 | if (!config->handler_context.using_criteria) { | 303 | if (!config->handler_context.using_criteria) { |
309 | set_config_node(node); | 304 | set_config_node(node); |
310 | struct cmd_results *res = handler->handle(argc-1, argv+1); | 305 | struct cmd_results *res = handler->handle(argc-1, argv+1); |
311 | if (res->status != CMD_SUCCESS) { | 306 | list_add(res_list, res); |
307 | if (res->status == CMD_INVALID) { | ||
312 | free_argv(argc, argv); | 308 | free_argv(argc, argv); |
313 | if (results) { | ||
314 | free_cmd_results(results); | ||
315 | } | ||
316 | results = res; | ||
317 | goto cleanup; | 309 | goto cleanup; |
318 | } | 310 | } |
319 | free_cmd_results(res); | ||
320 | } else { | 311 | } else { |
321 | for (int i = 0; i < views->length; ++i) { | 312 | for (int i = 0; i < views->length; ++i) { |
322 | struct sway_view *view = views->items[i]; | 313 | struct sway_view *view = views->items[i]; |
323 | set_config_node(&view->container->node); | 314 | set_config_node(&view->container->node); |
324 | struct cmd_results *res = handler->handle(argc-1, argv+1); | 315 | struct cmd_results *res = handler->handle(argc-1, argv+1); |
325 | if (res->status != CMD_SUCCESS) { | 316 | list_add(res_list, res); |
317 | if (res->status == CMD_INVALID) { | ||
326 | free_argv(argc, argv); | 318 | free_argv(argc, argv); |
327 | if (results) { | ||
328 | free_cmd_results(results); | ||
329 | } | ||
330 | results = res; | ||
331 | goto cleanup; | 319 | goto cleanup; |
332 | } | 320 | } |
333 | free_cmd_results(res); | ||
334 | } | 321 | } |
335 | } | 322 | } |
336 | free_argv(argc, argv); | 323 | free_argv(argc, argv); |
@@ -339,10 +326,7 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat, | |||
339 | cleanup: | 326 | cleanup: |
340 | free(exec); | 327 | free(exec); |
341 | list_free(views); | 328 | list_free(views); |
342 | if (!results) { | 329 | return res_list; |
343 | results = cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
344 | } | ||
345 | return results; | ||
346 | } | 330 | } |
347 | 331 | ||
348 | // this is like execute_command above, except: | 332 | // this is like execute_command above, except: |
@@ -575,20 +559,25 @@ void free_cmd_results(struct cmd_results *results) { | |||
575 | free(results); | 559 | free(results); |
576 | } | 560 | } |
577 | 561 | ||
578 | char *cmd_results_to_json(struct cmd_results *results) { | 562 | char *cmd_results_to_json(list_t *res_list) { |
579 | json_object *result_array = json_object_new_array(); | 563 | json_object *result_array = json_object_new_array(); |
580 | json_object *root = json_object_new_object(); | 564 | for (int i = 0; i < res_list->length; ++i) { |
581 | json_object_object_add(root, "success", | 565 | struct cmd_results *results = res_list->items[i]; |
582 | json_object_new_boolean(results->status == CMD_SUCCESS)); | 566 | json_object *root = json_object_new_object(); |
583 | if (results->input) { | 567 | json_object_object_add(root, "success", |
584 | json_object_object_add( | 568 | json_object_new_boolean(results->status == CMD_SUCCESS)); |
585 | root, "input", json_object_new_string(results->input)); | 569 | if (results->error) { |
586 | } | 570 | json_object_object_add(root, "parse_error", |
587 | if (results->error) { | 571 | json_object_new_boolean(results->status == CMD_INVALID)); |
588 | json_object_object_add( | 572 | json_object_object_add( |
589 | root, "error", json_object_new_string(results->error)); | 573 | root, "error", json_object_new_string(results->error)); |
574 | } | ||
575 | if (results->input) { | ||
576 | json_object_object_add( | ||
577 | root, "input", json_object_new_string(results->input)); | ||
578 | } | ||
579 | json_object_array_add(result_array, root); | ||
590 | } | 580 | } |
591 | json_object_array_add(result_array, root); | ||
592 | const char *json = json_object_to_json_string(result_array); | 581 | const char *json = json_object_to_json_string(result_array); |
593 | char *res = strdup(json); | 582 | char *res = strdup(json); |
594 | json_object_put(result_array); | 583 | json_object_put(result_array); |