1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.5 |
static const char RCSid[] = "$Id: tty.c,v 2.4 2004/03/30 16:13:01 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* tty.c - i/o for terminal drivers. |
6 |
greg |
2.2 |
*/ |
7 |
|
|
|
8 |
greg |
2.3 |
#include "copyright.h" |
9 |
greg |
1.1 |
|
10 |
|
|
#include <stdio.h> |
11 |
|
|
#include <signal.h> |
12 |
|
|
#include <fcntl.h> |
13 |
|
|
#include <sys/ioctl.h> |
14 |
|
|
|
15 |
|
|
#include "driver.h" |
16 |
|
|
|
17 |
|
|
/* |
18 |
|
|
* Drivers using these routines must call getch() |
19 |
|
|
* for input from stdin. |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
struct sgttyb ttymode; /* original tty modes */ |
23 |
|
|
int ttyfd; /* tty file descriptor */ |
24 |
schorsch |
2.4 |
struct driver *ttydev; /* tty device */ |
25 |
greg |
1.1 |
|
26 |
schorsch |
2.4 |
static dr_cominf_t ttyin; |
27 |
|
|
static dr_getchf_t; |
28 |
|
|
static void ttydone(void); |
29 |
|
|
static void newinp(void); |
30 |
greg |
1.1 |
|
31 |
|
|
|
32 |
schorsch |
2.4 |
extern void /* XXX declare */ |
33 |
|
|
ttyset( /* set up raw tty device */ |
34 |
|
|
struct driver *dev, |
35 |
|
|
int fd |
36 |
|
|
) |
37 |
greg |
1.1 |
{ |
38 |
|
|
struct sgttyb flags; |
39 |
|
|
|
40 |
|
|
if (!isatty(fd)) |
41 |
|
|
return(-1); |
42 |
|
|
|
43 |
|
|
ttyfd = fd; |
44 |
|
|
ioctl(ttyfd, TIOCGETP, &ttymode); |
45 |
|
|
ioctl(ttyfd, TIOCGETP, &flags); |
46 |
|
|
flags.sg_flags |= RAW; /* also turns output */ |
47 |
|
|
flags.sg_flags &= ~ECHO; /* processing off */ |
48 |
|
|
ioctl(ttyfd, TIOCSETP, &flags); |
49 |
|
|
/* install input handler */ |
50 |
|
|
ttydev = dev; |
51 |
|
|
ttydev->comin = ttyin; |
52 |
|
|
ttydev->inpready = 0; |
53 |
|
|
signal(SIGIO, newinp); |
54 |
|
|
fcntl(ttyfd, F_SETFL, FASYNC); |
55 |
|
|
return(0); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
|
59 |
schorsch |
2.4 |
static void |
60 |
|
|
ttydone(void) /* restore tty modes */ |
61 |
greg |
1.1 |
{ |
62 |
|
|
fcntl(ttyfd, F_SETFL, 0); |
63 |
|
|
signal(SIGIO, SIG_DFL); |
64 |
|
|
ioctl(ttyfd, TIOCSETP, &ttymode); |
65 |
|
|
ttydev->comin = NULL; |
66 |
|
|
ttydev->inpready = 0; |
67 |
|
|
ttydev = NULL; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
|
71 |
schorsch |
2.4 |
static int |
72 |
|
|
getch(void) /* get a character in raw mode */ |
73 |
greg |
1.1 |
{ |
74 |
|
|
register int c; |
75 |
|
|
|
76 |
|
|
fcntl(ttyfd, F_SETFL, 0); /* allow read to block */ |
77 |
|
|
if (ttydev->inpready > 0) |
78 |
|
|
ttydev->inpready--; |
79 |
|
|
c = getchar(); |
80 |
|
|
fcntl(ttyfd, F_SETFL, FASYNC); |
81 |
|
|
return(c); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
|
85 |
schorsch |
2.4 |
static void |
86 |
|
|
ttyin( /* read a line in raw mode */ |
87 |
|
|
char *buf, |
88 |
|
|
char *prompt |
89 |
|
|
) |
90 |
greg |
1.1 |
{ |
91 |
|
|
|
92 |
greg |
1.3 |
if (prompt != NULL) |
93 |
|
|
(*ttydev->comout)(prompt); |
94 |
greg |
1.2 |
editline(buf, getch, ttydev->comout); |
95 |
greg |
1.1 |
} |
96 |
|
|
|
97 |
|
|
|
98 |
schorsch |
2.4 |
static void |
99 |
|
|
newinp(void) /* new input */ |
100 |
greg |
1.1 |
{ |
101 |
|
|
ttydev->inpready++; |
102 |
|
|
} |