1 |
greg |
1.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 <sys/ioctl.h> |
15 |
|
|
|
16 |
|
|
#define INITSTR "" /* TTY initialization string */ |
17 |
|
|
|
18 |
|
|
struct sgttyb ttymode; |
19 |
|
|
|
20 |
|
|
|
21 |
|
|
main(argc, argv) /* called with pid to signal and pipe fd */ |
22 |
|
|
int argc; |
23 |
|
|
char *argv[]; |
24 |
|
|
{ |
25 |
|
|
int outfd; |
26 |
|
|
|
27 |
greg |
1.3 |
if (argc != 2) |
28 |
greg |
1.1 |
exit(1); |
29 |
greg |
1.3 |
outfd = atoi(argv[1]); |
30 |
greg |
1.1 |
|
31 |
|
|
fputs(INITSTR, stdout); |
32 |
|
|
fflush(stdout); |
33 |
|
|
/* reassign stdout */ |
34 |
|
|
if (outfd != fileno(stdout)) { |
35 |
|
|
dup2(outfd, fileno(stdout)); |
36 |
|
|
close(outfd); |
37 |
|
|
} |
38 |
|
|
/* set tty modes */ |
39 |
|
|
if (!isatty(0)) |
40 |
|
|
exit(1); |
41 |
|
|
ioctl(0, TIOCGETP, &ttymode); |
42 |
|
|
ttymode.sg_flags |= RAW; /* also turns output */ |
43 |
|
|
ttymode.sg_flags &= ~ECHO; /* processing off */ |
44 |
|
|
ioctl(0, TIOCSETP, &ttymode); |
45 |
|
|
/* read lines from input */ |
46 |
|
|
for ( ; ; ) { |
47 |
greg |
1.3 |
ungetc(getc(stdin), stdin); /* notify caller */ |
48 |
|
|
putc(' ', stdout); |
49 |
|
|
fflush(stdout); |
50 |
greg |
1.1 |
sendline(); |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
int |
56 |
|
|
getch() /* get a character in raw mode */ |
57 |
|
|
{ |
58 |
|
|
return(getc(stdin)); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
sends(s) /* send a string to stdout */ |
63 |
|
|
register char *s; |
64 |
|
|
{ |
65 |
|
|
do |
66 |
|
|
putc(*s, stdout); |
67 |
|
|
while (*s++); |
68 |
|
|
fflush(stdout); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
sendline() /* read a line in raw mode */ |
73 |
|
|
{ |
74 |
|
|
char buf[256]; |
75 |
|
|
|
76 |
greg |
1.2 |
editline(buf, getch, sends); |
77 |
greg |
1.1 |
putc('\0', stdout); |
78 |
|
|
sends(buf); |
79 |
|
|
} |