1 |
/* Copyright (c) 1988 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* suncom.c - program to read and edit raw tty input. |
9 |
* |
10 |
* 10/5/88 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
#include <signal.h> |
15 |
#include <sys/ioctl.h> |
16 |
|
17 |
#define INITSTR "" /* TTY initialization string */ |
18 |
|
19 |
struct sgttyb ttymode; |
20 |
|
21 |
|
22 |
main(argc, argv) /* called with pid to signal and pipe fd */ |
23 |
int argc; |
24 |
char *argv[]; |
25 |
{ |
26 |
int sigpid; |
27 |
int outfd; |
28 |
|
29 |
if (argc != 3) |
30 |
exit(1); |
31 |
sigpid = atoi(argv[1]); |
32 |
outfd = atoi(argv[2]); |
33 |
|
34 |
fputs(INITSTR, stdout); |
35 |
fflush(stdout); |
36 |
/* reassign stdout */ |
37 |
if (outfd != fileno(stdout)) { |
38 |
dup2(outfd, fileno(stdout)); |
39 |
close(outfd); |
40 |
} |
41 |
/* set tty modes */ |
42 |
if (!isatty(0)) |
43 |
exit(1); |
44 |
ioctl(0, TIOCGETP, &ttymode); |
45 |
ttymode.sg_flags |= RAW; /* also turns output */ |
46 |
ttymode.sg_flags &= ~ECHO; /* processing off */ |
47 |
ioctl(0, TIOCSETP, &ttymode); |
48 |
/* read lines from input */ |
49 |
for ( ; ; ) { |
50 |
ungetc(getc(stdin), stdin); /* notify sigpid */ |
51 |
kill(sigpid, SIGIO); |
52 |
sendline(); |
53 |
} |
54 |
} |
55 |
|
56 |
|
57 |
int |
58 |
getch() /* get a character in raw mode */ |
59 |
{ |
60 |
return(getc(stdin)); |
61 |
} |
62 |
|
63 |
|
64 |
sends(s) /* send a string to stdout */ |
65 |
register char *s; |
66 |
{ |
67 |
do |
68 |
putc(*s, stdout); |
69 |
while (*s++); |
70 |
fflush(stdout); |
71 |
} |
72 |
|
73 |
|
74 |
sendline() /* read a line in raw mode */ |
75 |
{ |
76 |
char buf[256]; |
77 |
|
78 |
editline(buf, getch, sends); |
79 |
putc('\0', stdout); |
80 |
sends(buf); |
81 |
} |