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