1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/* Copyright 1997,2001,2002,2007-2009 Alain Knaff.
* This file is part of mtools.
*
* Mtools 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.
*
* Mtools 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 Mtools. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include "sysincludes.h"
#include "mtools.h"
static FILE *tty=NULL;
static int notty=0;
static int ttyfd=-1;
#ifdef USE_RAWTERM
int mtools_raw_tty = 1;
#else
int mtools_raw_tty = 0;
#endif
#ifdef USE_RAWTERM
# if defined TCSANOW && defined HAVE_TCSETATTR
/* we have tcsetattr & tcgetattr. Good */
typedef struct termios Terminal;
# define stty(a,b) (void)tcsetattr(a,TCSANOW,b)
# define gtty(a,b) (void)tcgetattr(a,b)
# define USE_TCIFLUSH
# elif defined TCSETS && defined TCGETS
typedef struct termios Terminal;
# define stty(a,b) (void)ioctl(a,TCSETS,(char *)b)
# define gtty(a,b) (void)ioctl(a,TCGETS,(char *)b)
# define USE_TCIFLUSH
# elif defined TCSETA && defined TCGETA
typedef struct termio Terminal;
# define stty(a,b) (void)ioctl(a,TCSETA,(char *)b)
# define gtty(a,b) (void)ioctl(a,TCGETA,(char *)b)
# define USE_TCIFLUSH
# elif defined(HAVE_SGTTY_H) && defined(TIOCSETP) && defined(TIOCGETP)
typedef struct sgttyb Terminal;
# define stty(a,b) (void)ioctl(a,TIOCSETP,(char *)b)
# define gtty(a,b) (void)ioctl(a,TIOCGETP,(char *)b)
# define USE_SGTTY
# define discard_input(a) /**/
# else
/* no way to use raw terminal */
# warning Cannot use raw terminal code (disabled)
# undef USE_RAWTERM
# endif
#endif
#ifdef USE_TCIFLUSH
# if defined TCIFLUSH && defined HAVE_TCFLUSH
# define discard_input(a) tcflush(a,TCIFLUSH)
# else
# define discard_input(a) /**/
# endif
#endif
#ifdef USE_RAWTERM
static int tty_mode = -1; /* 1 for raw, 0 for cooked, -1 for initial */
static int need_tty_reset = 0;
static int handlerIsSet = 0;
#define restore_tty(a) stty(STDIN,a)
#define STDIN ttyfd
#define FAIL (-1)
#define DONE 0
static Terminal in_orig;
/*--------------- Signal Handler routines -------------*/
static void tty_time_out(int dummy)
{
int exit_code;
signal(SIGALRM, SIG_IGN);
if(tty && need_tty_reset)
restore_tty (&in_orig);
#ifdef future
if (fail_on_timeout)
exit_code=SHFAIL;
else {
if (default_choice && mode_defined) {
if (yes_no) {
if ('Y' == default_choice)
exit_code=0;
else
exit_code=1;
} else
exit_code=default_choice-minc+1;
} else
exit_code=DONE;
}
#else
exit_code = DONE;
#endif
exit(exit_code);
}
static void cleanup_tty(void)
{
if(tty && need_tty_reset) {
restore_tty (&in_orig);
setup_signal();
}
}
static void set_raw_tty(int mode)
{
Terminal in_raw;
if(mode != tty_mode && mode != -1) {
if(!handlerIsSet) {
/* Determine existing TTY settings */
gtty (STDIN, &in_orig);
need_tty_reset = 1;
/* Restore original TTY settings on exit */
atexit(cleanup_tty);
handlerIsSet = 1;
}
setup_signal();
signal (SIGALRM, tty_time_out);
/* Change STDIN settings to raw */
gtty (STDIN, &in_raw);
if(mode) {
#ifdef USE_SGTTY
in_raw.sg_flags |= CBREAK;
#else
in_raw.c_lflag &= ~ICANON;
in_raw.c_cc[VMIN]=1;
in_raw.c_cc[VTIME]=0;
#endif
stty (STDIN, &in_raw);
} else {
#ifdef USE_SGTTY
in_raw.sg_flags &= ~CBREAK;
#else
in_raw.c_lflag |= ICANON;
#endif
stty (STDIN, &in_raw);
}
tty_mode = mode;
discard_input(STDIN);
}
}
#endif
FILE *opentty(int mode)
{
if(notty)
return NULL;
if (tty == NULL) {
ttyfd = open("/dev/tty", O_RDONLY);
if(ttyfd >= 0) {
tty = fdopen(ttyfd, "r");
}
}
if (tty == NULL){
if ( !isatty(0) ){
notty = 1;
return NULL;
}
ttyfd = 0;
tty = stdin;
}
#ifdef USE_RAWTERM
if(mtools_raw_tty)
set_raw_tty(mode);
#endif
return tty;
}
int ask_confirmation(const char *format, ...)
{
char ans[10];
va_list ap;
if(!opentty(-1))
return 0;
while (1) {
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fflush(stderr);
fflush(opentty(-1));
if (mtools_raw_tty) {
ans[0] = fgetc(opentty(1));
fputs("\n", stderr);
} else {
if(fgets(ans,9, opentty(0)) == NULL)
/* Treat end-of-file as no */
ans[0] = 'n';
}
if (ans[0] == 'y' || ans[0] == 'Y')
return 0;
if (ans[0] == 'n' || ans[0] == 'N')
return -1;
}
}
|