1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* tty.c - i/o for terminal drivers. |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
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 |
|
24 |
int ttyfd; /* tty file descriptor */ |
25 |
|
26 |
struct driver *ttydev; /* tty device */ |
27 |
|
28 |
|
29 |
ttyset(dev, fd) /* set up raw tty device */ |
30 |
struct driver *dev; |
31 |
int fd; |
32 |
{ |
33 |
int ttyin(), newinp(); |
34 |
struct sgttyb flags; |
35 |
|
36 |
if (!isatty(fd)) |
37 |
return(-1); |
38 |
|
39 |
ttyfd = fd; |
40 |
ioctl(ttyfd, TIOCGETP, &ttymode); |
41 |
ioctl(ttyfd, TIOCGETP, &flags); |
42 |
flags.sg_flags |= RAW; /* also turns output */ |
43 |
flags.sg_flags &= ~ECHO; /* processing off */ |
44 |
ioctl(ttyfd, TIOCSETP, &flags); |
45 |
/* install input handler */ |
46 |
ttydev = dev; |
47 |
ttydev->comin = ttyin; |
48 |
ttydev->inpready = 0; |
49 |
signal(SIGIO, newinp); |
50 |
fcntl(ttyfd, F_SETFL, FASYNC); |
51 |
return(0); |
52 |
} |
53 |
|
54 |
|
55 |
ttydone() /* restore tty modes */ |
56 |
{ |
57 |
fcntl(ttyfd, F_SETFL, 0); |
58 |
signal(SIGIO, SIG_DFL); |
59 |
ioctl(ttyfd, TIOCSETP, &ttymode); |
60 |
ttydev->comin = NULL; |
61 |
ttydev->inpready = 0; |
62 |
ttydev = NULL; |
63 |
} |
64 |
|
65 |
|
66 |
int |
67 |
getch() /* get a character in raw mode */ |
68 |
{ |
69 |
register int c; |
70 |
|
71 |
fcntl(ttyfd, F_SETFL, 0); /* allow read to block */ |
72 |
if (ttydev->inpready > 0) |
73 |
ttydev->inpready--; |
74 |
c = getchar(); |
75 |
fcntl(ttyfd, F_SETFL, FASYNC); |
76 |
return(c); |
77 |
} |
78 |
|
79 |
|
80 |
static |
81 |
ttyin(buf, prompt) /* read a line in raw mode */ |
82 |
char *buf, *prompt; |
83 |
{ |
84 |
int getch(); |
85 |
|
86 |
if (prompt != NULL) |
87 |
(*ttydev->comout)(prompt); |
88 |
editline(buf, getch, ttydev->comout); |
89 |
} |
90 |
|
91 |
|
92 |
static |
93 |
newinp() /* new input */ |
94 |
{ |
95 |
ttydev->inpready++; |
96 |
} |