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