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
|
#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(void)
{
int exit_code;
signal(SIGALRM, SIG_IGN);
if(tty && need_tty_reset)
restore_tty (&in_orig);
#if 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);
signal (SIGHUP, SIG_IGN);
signal (SIGINT, SIG_IGN);
signal (SIGTERM, SIG_IGN);
signal (SIGALRM, SIG_IGN);
}
}
static void fail(void)
{
exit(1);
}
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;
atexit(cleanup_tty);
handlerIsSet = 1;
}
/*------ Restore original TTY settings on exit --------*/
signal (SIGHUP, (SIG_CAST) fail);
signal (SIGINT, (SIG_CAST) fail);
signal (SIGTERM, (SIG_CAST) fail);
signal (SIGALRM, (SIG_CAST) 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, const char *p1, const char *p2)
{
char ans[10];
if(!opentty(-1))
return 0;
while (1) {
fprintf(stderr, format, p1, p2);
fflush(stderr);
fflush(opentty(-1));
if (mtools_raw_tty) {
ans[0] = fgetc(opentty(1));
fputs("\n", stderr);
} else {
fgets(ans,9, opentty(0));
}
if (ans[0] == 'y' || ans[0] == 'Y')
return 0;
if (ans[0] == 'n' || ans[0] == 'N')
return -1;
}
}
|