| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: tty.c,v 2.3 2003/02/25 02:47:23 greg Exp $";
|
| 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 |
int ttyfd; /* tty file descriptor */
|
| 24 |
struct driver *ttydev; /* tty device */
|
| 25 |
|
| 26 |
static dr_cominf_t ttyin;
|
| 27 |
static dr_getchf_t;
|
| 28 |
static void ttydone(void);
|
| 29 |
static void newinp(void);
|
| 30 |
|
| 31 |
|
| 32 |
extern void /* XXX declare */
|
| 33 |
ttyset( /* set up raw tty device */
|
| 34 |
struct driver *dev,
|
| 35 |
int fd
|
| 36 |
)
|
| 37 |
{
|
| 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 |
static void
|
| 60 |
ttydone(void) /* restore tty modes */
|
| 61 |
{
|
| 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 |
static int
|
| 72 |
getch(void) /* get a character in raw mode */
|
| 73 |
{
|
| 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 |
static void
|
| 86 |
ttyin( /* read a line in raw mode */
|
| 87 |
char *buf,
|
| 88 |
char *prompt
|
| 89 |
)
|
| 90 |
{
|
| 91 |
|
| 92 |
if (prompt != NULL)
|
| 93 |
(*ttydev->comout)(prompt);
|
| 94 |
editline(buf, getch, ttydev->comout);
|
| 95 |
}
|
| 96 |
|
| 97 |
|
| 98 |
static void
|
| 99 |
newinp(void) /* new input */
|
| 100 |
{
|
| 101 |
ttydev->inpready++;
|
| 102 |
}
|