~ubuntu-branches/debian/jessie/debfoster/jessie

« back to all changes in this revision

Viewing changes to src/asktty.c

  • Committer: Bazaar Package Importer
  • Author(s): Ivo Timmermans
  • Date: 2002-01-17 23:08:39 UTC
  • Revision ID: james.westby@ubuntu.com-20020117230839-s22xl5hew1z4s3r5
Tags: upstream-2.5
ImportĀ upstreamĀ versionĀ 2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdarg.h>
 
3
#include <string.h>
 
4
#include <ctype.h>
 
5
#include <stdlib.h>
 
6
#include <unistd.h>
 
7
#include <signal.h>
 
8
#include <termios.h>
 
9
#include <sys/ioctl.h>
 
10
#include <sys/fcntl.h>
 
11
 
 
12
#include "config.h"
 
13
#include "intl.h"
 
14
#include "asktty.h"
 
15
 
 
16
#define READBUF 4096
 
17
 
 
18
static struct termios *tty = NULL;
 
19
static void (*old_sighandler)(int) = NULL;
 
20
 
 
21
void tty_restore(void) {
 
22
        if(tty)
 
23
                tcsetattr(STDIN_FILENO, TCSANOW, tty);
 
24
}
 
25
 
 
26
static void sighandler(int sig) {
 
27
        tty_restore();
 
28
        printf(_("Interrupted\n"));
 
29
        if(old_sighandler)
 
30
                old_sighandler(sig);
 
31
        exit(128+sig);
 
32
}
 
33
 
 
34
void tty_init(void) {
 
35
        static struct termios t;
 
36
        void (*old_sig)(int);
 
37
 
 
38
        if(!tcgetattr(STDIN_FILENO, &t)) {
 
39
                tty = &t;
 
40
                old_sig = signal(SIGINT, sighandler);
 
41
                if(old_sig != SIG_ERR)
 
42
                        old_sighandler = old_sig;
 
43
        }
 
44
}
 
45
 
 
46
int tty_ask(char *choices, char *fmt, ...) {
 
47
        va_list ap;
 
48
        struct termios tmp;
 
49
        int r;
 
50
        unsigned char c[READBUF];
 
51
        char *choice;
 
52
 
 
53
        for(;;) {
 
54
                va_start(ap, fmt);
 
55
                vfprintf(stdout, fmt, ap);
 
56
                va_end(ap);
 
57
                fflush(stdout);
 
58
 
 
59
                if(tty) {
 
60
                        tmp = *tty;
 
61
                        tmp.c_lflag &= ~ICANON;
 
62
                        tmp.c_iflag &= ~ICRNL;
 
63
                        tmp.c_lflag &= ~ECHO;
 
64
                        tmp.c_cc[VTIME] = 0;
 
65
                        tmp.c_cc[VMIN] = 1;
 
66
                        tcsetattr(STDIN_FILENO, TCSANOW, &tmp);
 
67
                }
 
68
 
 
69
                r = read(STDIN_FILENO, c, READBUF);
 
70
 
 
71
                tty_restore();
 
72
 
 
73
                if(r < 0) {
 
74
                        printf(_("Interrupted\n"));
 
75
                        return -1;
 
76
                }
 
77
 
 
78
                if(!r || strchr(" \r", *c))
 
79
                        *c = choices[0];
 
80
                else
 
81
                        *c = tolower(*c);
 
82
 
 
83
                if(choice = strchr(choices, *c)) {
 
84
                        printf("%c\n", toupper(*c));
 
85
                        fflush(stdout);
 
86
                        return choice - choices;
 
87
                }
 
88
 
 
89
                printf("\a\r\033[K");
 
90
        }
 
91
        return -1;
 
92
}