changeset 312:f0b7ff2df7e8

Ctrl-C does not terminate mcabber 1st Ctrl-C abort current completion 2 Ctrl-C in less than 2 seconds leave mcabber
author Mikael Berthe <mikael@lilotux.net>
date Thu, 14 Jul 2005 23:17:21 +0100
parents 1d6b16d9c577
children 1ceb68eb2fc1
files mcabber/src/main.c mcabber/src/screen.c mcabber/src/screen.h
diffstat 3 files changed, 95 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/main.c	Thu Jul 14 21:45:23 2005 +0100
+++ b/mcabber/src/main.c	Thu Jul 14 23:17:21 2005 +0100
@@ -44,61 +44,6 @@
 #include "harddefines.h"
 
 
-void sig_handler(int signum)
-{
-  if (signum == SIGCHLD) {
-    int status;
-    pid_t pid;
-    do {
-      pid = waitpid (WAIT_ANY, &status, WNOHANG);
-    } while (pid > 0);
-    //if (pid < 0)
-    //  ut_WriteLog("Error in waitpid: errno=%d\n", errno);
-    signal(SIGCHLD, sig_handler);
-  } else if (signum == SIGTERM) {
-    jb_disconnect();
-    scr_TerminateCurses();
-    printf("Killed by SIGTERM\nBye!\n");
-    exit(EXIT_SUCCESS);
-  } else {
-    ut_WriteLog("Caught signal: %d\n", signum);
-  }
-}
-
-void ask_password(void)
-{
-  char *password, *p;
-  size_t passsize = 128;
-  struct termios orig, new;
-  int nread;
-
-  /* Turn echoing off and fail if we can't. */
-  if (tcgetattr(fileno(stdin), &orig) != 0) return;
-  new = orig;
-  new.c_lflag &= ~ECHO;
-  if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return;
-
-  /* Read the password. */
-  password = NULL;
-  printf("Please enter password: ");
-  nread = getline(&password, &passsize, stdin);
-
-  /* Restore terminal. */
-  tcsetattr(fileno(stdin), TCSAFLUSH, &orig);
-  printf("\n");
-
-  if (nread == -1 || !password) return;
-
-  for (p = (char*)password; *p; p++)
-    ;
-  for ( ; p > (char*)password ; p--)
-    if (*p == '\n' || *p == '\r') *p = 0;
-
-  settings_set(SETTINGS_TYPE_OPTION, "password", password);
-  free(password);
-  return;
-}
-
 void mcabber_connect(void)
 {
   const char *username, *password, *resource, *servername;
@@ -147,6 +92,79 @@
   jb_reset_keepalive();
 }
 
+void mcabber_disconnect(const char *msg)
+{
+  jb_disconnect();
+  scr_TerminateCurses();
+  if (msg)
+    fprintf(stderr, "%s\n", msg);
+  printf("Bye!\n");
+  exit(EXIT_SUCCESS);
+}
+
+void sig_handler(int signum)
+{
+  if (signum == SIGCHLD) {
+    int status;
+    pid_t pid;
+    do {
+      pid = waitpid (WAIT_ANY, &status, WNOHANG);
+    } while (pid > 0);
+    //if (pid < 0)
+    //  ut_WriteLog("Error in waitpid: errno=%d\n", errno);
+    signal(SIGCHLD, sig_handler);
+  } else if (signum == SIGTERM) {
+    mcabber_disconnect("Killed by SIGTERM");
+  } else if (signum == SIGINT) {  // Ctrl-C
+    static time_t LastSigtermTime;
+    time_t now;
+    time(&now);
+    /* Terminate if 2 consecutive SIGTERMs */
+    if (now - LastSigtermTime < 2)
+      mcabber_disconnect("Killed by SIGINT");
+    LastSigtermTime = now;
+    signal(SIGINT, sig_handler);
+    scr_LogPrint("Hit Ctrl-C twice to leave mcabber");
+    scr_handle_sigint();
+  } else {
+    ut_WriteLog("Caught signal: %d\n", signum);
+  }
+}
+
+void ask_password(void)
+{
+  char *password, *p;
+  size_t passsize = 128;
+  struct termios orig, new;
+  int nread;
+
+  /* Turn echoing off and fail if we can't. */
+  if (tcgetattr(fileno(stdin), &orig) != 0) return;
+  new = orig;
+  new.c_lflag &= ~ECHO;
+  if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return;
+
+  /* Read the password. */
+  password = NULL;
+  printf("Please enter password: ");
+  nread = getline(&password, &passsize, stdin);
+
+  /* Restore terminal. */
+  tcsetattr(fileno(stdin), TCSAFLUSH, &orig);
+  printf("\n");
+
+  if (nread == -1 || !password) return;
+
+  for (p = (char*)password; *p; p++)
+    ;
+  for ( ; p > (char*)password ; p--)
+    if (*p == '\n' || *p == '\r') *p = 0;
+
+  settings_set(SETTINGS_TYPE_OPTION, "password", password);
+  free(password);
+  return;
+}
+
 void credits(void)
 {
   printf(MCABBER_VERSION "\n");
@@ -171,6 +189,7 @@
 
   ut_WriteLog("Setting signals handlers...\n");
   signal(SIGTERM, sig_handler);
+  signal(SIGINT,  sig_handler);
   signal(SIGCHLD, sig_handler);
 
   /* Parse command line options */
--- a/mcabber/src/screen.c	Thu Jul 14 21:45:23 2005 +0100
+++ b/mcabber/src/screen.c	Thu Jul 14 23:17:21 2005 +0100
@@ -1482,6 +1482,24 @@
   }
 }
 
+inline void refresh_inputline(void)
+{
+  mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset);
+  wclrtoeol(inputWnd);
+  if (*ptr_inputline)
+    wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset);
+}
+
+void scr_handle_sigint(void)
+{
+  scr_LogPrint("In screen. completion_started=%d", completion_started);
+  // Same as Ctrl-g, now
+  scr_cancel_current_completion();
+  scr_end_current_completion();
+  check_offset(-1);
+  refresh_inputline();
+}
+
 //  process_key(key)
 // Handle the pressed key, in the command line (bottom).
 int process_key(int key)
@@ -1647,14 +1665,8 @@
   }
   if (completion_started && key != 9 && key != KEY_RESIZE)
     scr_end_current_completion();
-  mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset);
-  wclrtoeol(inputWnd);
-  if (*ptr_inputline) {
-    wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset);
-  }
-  if (!update_roster) {
-    //update_panels();
+  refresh_inputline();
+  if (!update_roster)
     doupdate();
-  }
   return 0;
 }
--- a/mcabber/src/screen.h	Thu Jul 14 21:45:23 2005 +0100
+++ b/mcabber/src/screen.h	Thu Jul 14 23:17:21 2005 +0100
@@ -37,6 +37,7 @@
 inline int  scr_get_multimode();
 void scr_append_multiline(const char *line);
 inline const char *scr_get_multiline();
+void scr_handle_sigint(void);
 
 WINDOW *scr_GetInputWindow(void);