aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-11-27 21:42:09 -0500
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-11-27 21:42:09 -0500
commit5c6f3d7266ab0c63015715f12c8e15eb144311a2 (patch)
tree8a30abddb8380fc03d6f8674c469570fbf493327 /sway/commands.c
parentMerge pull request #3175 from emersion/rename-gtk-primary-selection (diff)
downloadsway-5c6f3d7266ab0c63015715f12c8e15eb144311a2.tar.gz
sway-5c6f3d7266ab0c63015715f12c8e15eb144311a2.tar.zst
sway-5c6f3d7266ab0c63015715f12c8e15eb144311a2.zip
Change execute_command to return a list of results
This matches i3's behavior of returning a list of results that contain the result of each command that was executed. Additionally, the `parse_error` attribute has been added to the IPC JSON reply.
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c67
1 files changed, 28 insertions, 39 deletions
diff --git a/sway/commands.c b/sway/commands.c
index bffc18f6..34e66ce9 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
218struct cmd_results *execute_command(char *_exec, struct sway_seat *seat, 218list_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,
339cleanup: 326cleanup:
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:
@@ -574,20 +558,25 @@ void free_cmd_results(struct cmd_results *results) {
574 free(results); 558 free(results);
575} 559}
576 560
577char *cmd_results_to_json(struct cmd_results *results) { 561char *cmd_results_to_json(list_t *res_list) {
578 json_object *result_array = json_object_new_array(); 562 json_object *result_array = json_object_new_array();
579 json_object *root = json_object_new_object(); 563 for (int i = 0; i < res_list->length; ++i) {
580 json_object_object_add(root, "success", 564 struct cmd_results *results = res_list->items[i];
581 json_object_new_boolean(results->status == CMD_SUCCESS)); 565 json_object *root = json_object_new_object();
582 if (results->input) { 566 json_object_object_add(root, "success",
583 json_object_object_add( 567 json_object_new_boolean(results->status == CMD_SUCCESS));
584 root, "input", json_object_new_string(results->input)); 568 if (results->error) {
585 } 569 json_object_object_add(root, "parse_error",
586 if (results->error) { 570 json_object_new_boolean(results->status == CMD_INVALID));
587 json_object_object_add( 571 json_object_object_add(
588 root, "error", json_object_new_string(results->error)); 572 root, "error", json_object_new_string(results->error));
573 }
574 if (results->input) {
575 json_object_object_add(
576 root, "input", json_object_new_string(results->input));
577 }
578 json_object_array_add(result_array, root);
589 } 579 }
590 json_object_array_add(result_array, root);
591 const char *json = json_object_to_json_string(result_array); 580 const char *json = json_object_to_json_string(result_array);
592 char *res = strdup(json); 581 char *res = strdup(json);
593 json_object_put(result_array); 582 json_object_put(result_array);