From e5d3cf2538f6fcb6a6f5271736c5e5b3dea4e7b1 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 29 Mar 2014 11:34:02 +0100 Subject: [PATCH] gui: Reorder functions. As a preparation for the removal of do_select() we need various functions of gui.c to be in a different order. Specifically, input_post_select() will soon call handle_command(), so the latter function will need to be declared before the former. Patches which move functions around are generally hard to read, so this commit performs pure code movement only. However, as exec_and_display_cmd() still calls do_select() we introduce a temporary forward declaration for do_select(). It will be removed in the next patch. --- gui.c | 208 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 105 insertions(+), 103 deletions(-) diff --git a/gui.c b/gui.c index 70981e89..6eea455f 100644 --- a/gui.c +++ b/gui.c @@ -1033,6 +1033,111 @@ static int command_post_select(int mode, fd_set *rfds) return 1; } +static int do_select(int mode); + +/* read from command pipe and print data to bot window */ +static void exec_and_display_cmd(const char *cmd) +{ + int ret, fds[3] = {0, 1, 1}; + + outputf(COLOR_COMMAND, "%s", cmd); + ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds); + if (ret < 0) + return; + ret = mark_fd_nonblocking(fds[1]); + if (ret < 0) + goto fail; + ret = mark_fd_nonblocking(fds[2]); + if (ret < 0) + goto fail; + command_fds[0] = fds[1]; + command_fds[1] = fds[2]; + if (do_select(COMMAND_MODE) >= 0) + PARA_INFO_LOG("command complete\n"); + else + PARA_NOTICE_LOG("command aborted\n"); + print_in_bar(COLOR_MSG, " "); + return; +fail: + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); + close(command_fds[0]); + close(command_fds[1]); +} + +static void para_cmd(char *cmd) +{ + char *c; + + print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n"); + c = make_message(BINDIR "/para_client -- %s", cmd); + exec_and_display_cmd(c); + free(c); +} + +static void display_cmd(char *cmd) +{ + print_in_bar(COLOR_MSG, "executing display command, hit any key to abort"); + exec_and_display_cmd(cmd); +} + +/* + * shutdown curses and stat pipe before executing external commands + */ +static void external_cmd(char *cmd) +{ + int fds[3] = {-1, -1, -1}; + + if (cmd_pid) + return; + shutdown_curses(); + if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0) + return; + do_select(EXTERNAL_MODE); + init_curses(); +} + +static void handle_command(int c) +{ + int i; + + /* first check user-defined key bindings */ + for (i = 0; i < conf.key_map_given; ++i) { + char *tmp, *handler, *arg; + + tmp = para_strdup(conf.key_map_arg[i]); + if (!split_key_map(tmp, &handler, &arg)) { + free(tmp); + return; + } + if (strcmp(tmp, km_keyname(c))) { + free(tmp); + continue; + } + if (*handler == 'd') + display_cmd(arg); + else if (*handler == 'x') + external_cmd(arg); + else if (*handler == 'p') + para_cmd(arg); + else if (*handler == 'i') { + int num = find_cmd_byname(arg); + if (num >= 0) + command_list[num].handler(); + } + free(tmp); + return; + } + /* not found, check internal key bindings */ + for (i = 0; command_list[i].handler; i++) { + if (!strcmp(km_keyname(c), command_list[i].key)) { + command_list[i].handler(); + return; + } + } + print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help", + km_keyname(c)); +} + static void input_pre_select(int mode, fd_set *rfds, int *max_fileno) { if (mode == GETCH_MODE || mode == COMMAND_MODE) @@ -1116,67 +1221,6 @@ check_return: goto repeat; } -/* read from command pipe and print data to bot window */ -static void exec_and_display_cmd(const char *cmd) -{ - int ret, fds[3] = {0, 1, 1}; - - outputf(COLOR_COMMAND, "%s", cmd); - ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds); - if (ret < 0) - return; - ret = mark_fd_nonblocking(fds[1]); - if (ret < 0) - goto fail; - ret = mark_fd_nonblocking(fds[2]); - if (ret < 0) - goto fail; - command_fds[0] = fds[1]; - command_fds[1] = fds[2]; - if (do_select(COMMAND_MODE) >= 0) - PARA_INFO_LOG("command complete\n"); - else - PARA_NOTICE_LOG("command aborted\n"); - print_in_bar(COLOR_MSG, " "); - return; -fail: - PARA_ERROR_LOG("%s\n", para_strerror(-ret)); - close(command_fds[0]); - close(command_fds[1]); -} - -static void para_cmd(char *cmd) -{ - char *c; - - print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n"); - c = make_message(BINDIR "/para_client -- %s", cmd); - exec_and_display_cmd(c); - free(c); -} - -static void display_cmd(char *cmd) -{ - print_in_bar(COLOR_MSG, "executing display command, hit any key to abort"); - exec_and_display_cmd(cmd); -} - -/* - * shutdown curses and stat pipe before executing external commands - */ -static void external_cmd(char *cmd) -{ - int fds[3] = {-1, -1, -1}; - - if (cmd_pid) - return; - shutdown_curses(); - if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0) - return; - do_select(EXTERNAL_MODE); - init_curses(); -} - static void print_scroll_msg(void) { unsigned lines_total, filled = ringbuffer_filled(bot_win_rb); @@ -1436,48 +1480,6 @@ static void com_prev_theme(void) com_refresh(); } -static void handle_command(int c) -{ - int i; - - /* first check user-defined key bindings */ - for (i = 0; i < conf.key_map_given; ++i) { - char *tmp, *handler, *arg; - - tmp = para_strdup(conf.key_map_arg[i]); - if (!split_key_map(tmp, &handler, &arg)) { - free(tmp); - return; - } - if (strcmp(tmp, km_keyname(c))) { - free(tmp); - continue; - } - if (*handler == 'd') - display_cmd(arg); - else if (*handler == 'x') - external_cmd(arg); - else if (*handler == 'p') - para_cmd(arg); - else if (*handler == 'i') { - int num = find_cmd_byname(arg); - if (num >= 0) - command_list[num].handler(); - } - free(tmp); - return; - } - /* not found, check internal key bindings */ - for (i = 0; command_list[i].handler; i++) { - if (!strcmp(km_keyname(c), command_list[i].key)) { - command_list[i].handler(); - return; - } - } - print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help", - km_keyname(c)); -} - __noreturn static void print_help_and_die(void) { struct ggo_help h = DEFINE_GGO_HELP(gui); -- 2.39.5