diff --git a/src/core/wee-command.c b/src/core/wee-command.c index b9fdf4ac3..386bd5e26 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -6076,7 +6076,16 @@ COMMAND_CALLBACK(upgrade) } } if (!ptr_binary && !quit) - ptr_binary = strdup (weechat_argv0); + { + ptr_binary = (weechat_argv0) ? strdup (weechat_argv0) : NULL; + if (!ptr_binary) + { + gui_chat_printf (NULL, + _("%sNo binary specified"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]); + return WEECHAT_RC_OK; + } + } if (!ptr_binary && !quit) { diff --git a/src/core/weechat.c b/src/core/weechat.c index cd474711f..65a65c64a 100644 --- a/src/core/weechat.c +++ b/src/core/weechat.c @@ -162,7 +162,7 @@ weechat_parse_args (int argc, char *argv[]) { int i; - weechat_argv0 = strdup (argv[0]); + weechat_argv0 = (argv && argv[0]) ? strdup (argv[0]) : NULL; weechat_upgrading = 0; weechat_home = NULL; weechat_server_cmd_line = 0; @@ -429,6 +429,42 @@ weechat_term_check () } } +/* + * Callback for system signal SIGHUP: quits WeeChat. + */ + +void +weechat_sighup () +{ + log_printf (_("Signal %s received, exiting WeeChat..."), "SIGHUP"); + (void) hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL); + weechat_quit = 1; +} + +/* + * Callback for system signal SIGQUIT: quits WeeChat. + */ + +void +weechat_sigquit () +{ + log_printf (_("Signal %s received, exiting WeeChat..."), "SIGQUIT"); + (void) hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL); + weechat_quit = 1; +} + +/* + * Callback for system signal SIGTERM: quits WeeChat. + */ + +void +weechat_sigterm () +{ + log_printf (_("Signal %s received, exiting WeeChat..."), "SIGTERM"); + (void) hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL); + weechat_quit = 1; +} + /* * Shutdowns WeeChat. */ @@ -455,11 +491,11 @@ weechat_shutdown (int return_code, int crash) } /* - * Entry point for WeeChat. + * Initializes WeeChat. */ -int -main (int argc, char *argv[]) +void +weechat_init (int argc, char *argv[], void (*gui_init_cb)()) { weechat_first_start_time = time (NULL); /* initialize start time */ gettimeofday (&weechat_current_start_timeval, NULL); @@ -478,15 +514,20 @@ main (int argc, char *argv[]) #endif utf8_init (); - util_catch_signal (SIGINT, SIG_IGN); /* ignore SIGINT signal */ - util_catch_signal (SIGQUIT, SIG_IGN); /* ignore SIGQUIT signal */ - util_catch_signal (SIGPIPE, SIG_IGN); /* ignore SIGPIPE signal */ - util_catch_signal (SIGSEGV, - &debug_sigsegv); /* crash dump for SIGSEGV signal */ + /* catch signals */ + util_catch_signal (SIGINT, SIG_IGN); /* signal ignored */ + util_catch_signal (SIGQUIT, SIG_IGN); /* signal ignored */ + util_catch_signal (SIGPIPE, SIG_IGN); /* signal ignored */ + util_catch_signal (SIGSEGV, &debug_sigsegv); /* crash dump */ + util_catch_signal (SIGHUP, &weechat_sighup); /* exit WeeChat */ + util_catch_signal (SIGQUIT, &weechat_sigquit); /* exit WeeChat */ + util_catch_signal (SIGTERM, &weechat_sigterm); /* exit WeeChat */ + hdata_init (); /* initialize hdata */ hook_init (); /* initialize hooks */ debug_init (); /* hook signals for debug */ - gui_main_pre_init (&argc, &argv); /* pre-initialize interface */ + gui_color_init (); /* initialize colors */ + gui_chat_init (); /* initialize chat */ command_init (); /* initialize WeeChat commands */ completion_init (); /* add core completion hooks */ gui_key_init (); /* init keys */ @@ -502,7 +543,10 @@ main (int argc, char *argv[]) secure_read (); /* read secured data options */ config_weechat_read (); /* read WeeChat options */ network_init_gnutls (); /* init GnuTLS */ - gui_main_init (); /* init WeeChat interface */ + + if (gui_init_cb) + (*gui_init_cb) (); /* init WeeChat interface */ + if (weechat_upgrading) { upgrade_weechat_load (); /* upgrade with session file */ @@ -519,15 +563,24 @@ main (int argc, char *argv[]) gui_layout_window_apply (gui_layout_current, -1); if (weechat_upgrading) upgrade_weechat_end (); /* remove .upgrade files + signal */ +} - gui_main_loop (); /* WeeChat main loop */ +/* + * Ends WeeChat. + */ +void +weechat_end (void (*gui_end_cb)(int clean_exit)) +{ gui_layout_store_on_exit (); /* store layout */ plugin_end (); /* end plugin interface(s) */ if (CONFIG_BOOLEAN(config_look_save_config_on_exit)) (void) config_weechat_write (); /* save WeeChat config file */ (void) secure_write (); /* save secured data */ - gui_main_end (1); /* shut down WeeChat GUI */ + + if (gui_end_cb) + (*gui_end_cb) (1); /* shut down WeeChat GUI */ + proxy_free_all (); /* free all proxies */ config_weechat_free (); /* free WeeChat options */ secure_free (); /* free secured data options */ @@ -538,6 +591,4 @@ main (int argc, char *argv[]) secure_end (); /* end secured data */ string_end (); /* end string */ weechat_shutdown (EXIT_SUCCESS, 0); /* quit WeeChat (oh no, why?) */ - - return EXIT_SUCCESS; /* make C compiler happy */ } diff --git a/src/core/weechat.h b/src/core/weechat.h index 8eee8079a..6107bd566 100644 --- a/src/core/weechat.h +++ b/src/core/weechat.h @@ -108,5 +108,7 @@ extern char *weechat_startup_commands; extern void weechat_term_check (); extern void weechat_shutdown (int return_code, int crash); +extern void weechat_init (int argc, char *argv[], void (*gui_init_cb)()); +extern void weechat_end (void (*gui_end_cb)(int clean_exit)); #endif /* WEECHAT_H */ diff --git a/src/gui/curses/CMakeLists.txt b/src/gui/curses/CMakeLists.txt index 61463d799..1466b2b60 100644 --- a/src/gui/curses/CMakeLists.txt +++ b/src/gui/curses/CMakeLists.txt @@ -19,7 +19,7 @@ # along with WeeChat. If not, see . # -set(WEECHAT_CURSES_SRC +set(LIB_GUI_CURSES_SRC gui-curses.h gui-curses-bar-window.c gui-curses-chat.c @@ -30,6 +30,9 @@ gui-curses-mouse.c gui-curses-term.c gui-curses-window.c) +set(WEECHAT_CURSES_MAIN_SRC +main.c) + set(EXECUTABLE weechat) find_package(Ncurses) @@ -70,12 +73,16 @@ list(APPEND EXTRA_LIBS "m") list(APPEND EXTRA_LIBS ${CURL_LIBRARIES}) -add_executable(${EXECUTABLE} ${WEECHAT_CURSES_SRC}) - include_directories(.. ../../core ../../plugins ${NCURSES_INCLUDE_PATH}) +add_library(weechat_gui_curses STATIC ${LIB_GUI_CURSES_SRC}) + +add_executable(${EXECUTABLE} ${WEECHAT_CURSES_MAIN_SRC}) + +add_dependencies(${EXECUTABLE} weechat_gui_curses) + # Because of a linker bug, we have to link 2 times with libweechat_core.a -target_link_libraries(${EXECUTABLE} ${STATIC_LIBS} ${EXTRA_LIBS} ${STATIC_LIBS}) +target_link_libraries(${EXECUTABLE} ${STATIC_LIBS} ${EXTRA_LIBS} ${STATIC_LIBS} weechat_gui_curses) install(TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin) diff --git a/src/gui/curses/gui-curses-color.c b/src/gui/curses/gui-curses-color.c index 4280a0493..5421aa612 100644 --- a/src/gui/curses/gui-curses-color.c +++ b/src/gui/curses/gui-curses-color.c @@ -1481,26 +1481,11 @@ gui_color_init_weechat () } /* - * Pre-initializes colors. + * Allocates GUI colors. */ void -gui_color_pre_init () -{ - int i; - - for (i = 0; i < GUI_COLOR_NUM_COLORS; i++) - { - gui_color[i] = NULL; - } -} - -/* - * Initializes GUI colors. - */ - -void -gui_color_init () +gui_color_alloc () { if (has_colors()) { diff --git a/src/gui/curses/gui-curses-main.c b/src/gui/curses/gui-curses-main.c index ee4fdbb22..ca72fade2 100644 --- a/src/gui/curses/gui-curses-main.c +++ b/src/gui/curses/gui-curses-main.c @@ -122,24 +122,6 @@ gui_main_get_password (const char *prompt1, const char *prompt2, endwin (); } -/* - * Pre-initializes GUI (called before gui_init). - */ - -void -gui_main_pre_init (int *argc, char **argv[]) -{ - /* make C compiler happy */ - (void) argc; - (void) argv; - - /* pre-init colors */ - gui_color_pre_init (); - - /* init some variables for chat area */ - gui_chat_init (); -} - /* * Initializes GUI. */ @@ -162,7 +144,7 @@ gui_main_init () nodelay (stdscr, TRUE); raw (); - gui_color_init (); + gui_color_alloc (); /* build prefixes according to configuration */ gui_chat_prefix_build (); @@ -246,45 +228,6 @@ gui_main_init () gui_window_set_bracketed_paste_mode (CONFIG_BOOLEAN(config_look_paste_bracketed)); } -/* - * Callback for system signal SIGQUIT: quits WeeChat. - */ - -void -gui_main_signal_sigquit () -{ - log_printf (_("Signal %s received, exiting WeeChat..."), - "SIGQUIT"); - (void) hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL); - weechat_quit = 1; -} - -/* - * Callback for system signal SIGTERM: quits WeeChat. - */ - -void -gui_main_signal_sigterm () -{ - log_printf (_("Signal %s received, exiting WeeChat..."), - "SIGTERM"); - (void) hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL); - weechat_quit = 1; -} - -/* - * Callback for system signal SIGHUP: quits WeeChat. - */ - -void -gui_main_signal_sighup () -{ - log_printf (_("Signal %s received, exiting WeeChat..."), - "SIGHUP"); - (void) hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL); - weechat_quit = 1; -} - /* * Callback for system signal SIGWINCH: refreshes screen. */ @@ -432,11 +375,6 @@ gui_main_loop () int max_fd; int ready; - /* catch SIGTERM/SIGQUIT/SIGHUP signals: quit program */ - util_catch_signal (SIGTERM, &gui_main_signal_sigterm); - util_catch_signal (SIGQUIT, &gui_main_signal_sigquit); - util_catch_signal (SIGHUP, &gui_main_signal_sighup); - /* catch SIGWINCH signal: redraw screen */ util_catch_signal (SIGWINCH, &gui_main_signal_sigwinch); diff --git a/src/gui/curses/gui-curses.h b/src/gui/curses/gui-curses.h index f8c0188ff..ae415bff5 100644 --- a/src/gui/curses/gui-curses.h +++ b/src/gui/curses/gui-curses.h @@ -31,6 +31,7 @@ #endif struct t_gui_buffer; +struct t_gui_line; struct t_gui_window; struct t_gui_bar_window; @@ -80,12 +81,15 @@ extern time_t gui_color_pairs_auto_reset_last; extern int gui_color_buffer_refresh_needed; extern int gui_window_current_emphasis; +/* main functions */ +extern void gui_main_init (); +extern void gui_main_loop (); + /* color functions */ extern int gui_color_get_extended_attrs (int color); extern int gui_color_get_pair (int fg, int bg); extern int gui_color_weechat_get_pair (int weechat_color); -extern void gui_color_pre_init (); -extern void gui_color_init (); +extern void gui_color_alloc (); /* chat functions */ extern void gui_chat_calculate_line_diff (struct t_gui_window *window, diff --git a/src/gui/curses/main.c b/src/gui/curses/main.c new file mode 100644 index 000000000..a234610f2 --- /dev/null +++ b/src/gui/curses/main.c @@ -0,0 +1,46 @@ +/* + * main.c - entry point for Curses GUI + * + * Copyright (C) 2003-2014 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "../../core/weechat.h" +#include "../gui-main.h" +#include "gui-curses.h" + + +/* + * Entry point for WeeChat (Curses GUI). + */ + +int +main (int argc, char *argv[]) +{ + weechat_init (argc, argv, &gui_main_init); + gui_main_loop (); + weechat_end (&gui_main_end); + + /* make C compiler happy (never executed) */ + return EXIT_SUCCESS; +} diff --git a/src/gui/gui-color.c b/src/gui/gui-color.c index 43e43a444..c1b9d94ca 100644 --- a/src/gui/gui-color.c +++ b/src/gui/gui-color.c @@ -1274,6 +1274,21 @@ gui_color_palette_free_structs () weelist_free (gui_color_list_with_alias); } +/* + * Initializes colors. + */ + +void +gui_color_init () +{ + int i; + + for (i = 0; i < GUI_COLOR_NUM_COLORS; i++) + { + gui_color[i] = NULL; + } +} + /* * Ends GUI colors. */ @@ -1286,6 +1301,7 @@ gui_color_end () for (i = 0; i < GUI_COLOR_NUM_COLORS; i++) { gui_color_free (gui_color[i]); + gui_color[i] = NULL; } gui_color_palette_free_structs (); gui_color_free_vars (); diff --git a/src/gui/gui-color.h b/src/gui/gui-color.h index fb4b86a09..578183200 100644 --- a/src/gui/gui-color.h +++ b/src/gui/gui-color.h @@ -187,6 +187,7 @@ extern int gui_color_palette_get_alias (const char *alias); extern struct t_gui_color_palette *gui_color_palette_get (int number); extern void gui_color_palette_add (int number, const char *value); extern void gui_color_palette_remove (int number); +extern void gui_color_init (); extern void gui_color_end (); /* color functions (GUI dependent) */ diff --git a/src/gui/gui-main.h b/src/gui/gui-main.h index f66e4dbd9..3b4ff8353 100644 --- a/src/gui/gui-main.h +++ b/src/gui/gui-main.h @@ -26,9 +26,6 @@ extern void gui_main_get_password (const char *prompt1, const char *prompt2, const char *prompt3, char *password, int size); extern void gui_main_debug_libs (); -extern void gui_main_loop (); -extern void gui_main_pre_init (int *argc, char **argv[]); -extern void gui_main_init (); extern void gui_main_end (int clean_exit); /* terminal functions (GUI dependent) */