comparison mcabber/src/main.c @ 789:1fa93cc6f5e3

Correctly restore term settings when interrupting the password query
author Mikael Berthe <mikael@lilotux.net>
date Thu, 06 Apr 2006 22:36:46 +0200
parents d7f3b58db522
children d8e0a1ce3e8a
comparison
equal deleted inserted replaced
788:7f10b84ba7fc 789:1fa93cc6f5e3
44 #ifdef ENABLE_HGCSET 44 #ifdef ENABLE_HGCSET
45 # include "hgcset.h" 45 # include "hgcset.h"
46 #endif 46 #endif
47 47
48 48
49 static struct termios *backup_termios;
50
49 char *mcabber_version(void) 51 char *mcabber_version(void)
50 { 52 {
51 char *ver; 53 char *ver;
52 #ifdef HGCSET 54 #ifdef HGCSET
53 ver = g_strdup_printf("%s (%s)", PACKAGE_VERSION, HGCSET); 55 ver = g_strdup_printf("%s (%s)", PACKAGE_VERSION, HGCSET);
122 124
123 void mcabber_disconnect(const char *msg) 125 void mcabber_disconnect(const char *msg)
124 { 126 {
125 jb_disconnect(); 127 jb_disconnect();
126 scr_TerminateCurses(); 128 scr_TerminateCurses();
129
130 // Restore term settings, if needed.
131 if (backup_termios)
132 tcsetattr(fileno(stdin), TCSAFLUSH, backup_termios);
133
127 if (msg) 134 if (msg)
128 fprintf(stderr, "%s\n", msg); 135 fprintf(stderr, "%s\n", msg);
129 printf("Bye!\n"); 136 printf("Bye!\n");
130 exit(EXIT_SUCCESS); 137 exit(EXIT_SUCCESS);
131 } 138 }
165 172
166 password = g_new0(char, passsize); 173 password = g_new0(char, passsize);
167 174
168 /* Turn echoing off and fail if we can't. */ 175 /* Turn echoing off and fail if we can't. */
169 if (tcgetattr(fileno(stdin), &orig) != 0) return; 176 if (tcgetattr(fileno(stdin), &orig) != 0) return;
177 backup_termios = &orig;
178
170 new = orig; 179 new = orig;
171 new.c_lflag &= ~ECHO; 180 new.c_lflag &= ~ECHO;
172 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return; 181 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return;
173 182
174 /* Read the password. */ 183 /* Read the password. */
176 fgets(password, passsize, stdin); 185 fgets(password, passsize, stdin);
177 186
178 /* Restore terminal. */ 187 /* Restore terminal. */
179 tcsetattr(fileno(stdin), TCSAFLUSH, &orig); 188 tcsetattr(fileno(stdin), TCSAFLUSH, &orig);
180 printf("\n"); 189 printf("\n");
190 backup_termios = NULL;
181 191
182 for (p = (char*)password; *p; p++) 192 for (p = (char*)password; *p; p++)
183 ; 193 ;
184 for ( ; p > (char*)password ; p--) 194 for ( ; p > (char*)password ; p--)
185 if (*p == '\n' || *p == '\r') *p = 0; 195 if (*p == '\n' || *p == '\r') *p = 0;