summaryrefslogtreecommitdiff
path: root/unix.c
blob: 3ac06c7596b60534ea78c90b0901c2ab5d8d2dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
  Terminal I/O routines for SEDT (a la UNIX)
*/
#include <stdio.h>
#include <stdlib.h>
#if defined(ULTRIX)
#include <sys/ioctl.h>
#include <sgtty.h>
#else
#include <sys/ioctl.h>
#if defined(CONVEX)
#include <sys/termios.h>
#else
#if defined(LINUX)
#include <termios.h>
#else
#include <termio.h>
#endif
#endif
#if defined(SGI)
#include <sys/ttold.h>
#endif
#endif

int     tty;
#if defined(ULTRIX)
struct	sgttyb	restore, special;
#else
#if defined(CONVEX) | defined(LINUX)
struct  termios  restore, special;
#else
struct  termio  restore, special;
#endif
#endif

UInit()
{
    /*char    *ttyname();*/

/*
  Open up the terminal device user is on.  Get the characteristics
  of this terminal, make a copy, and set for no post-processing,
  no echo, no canonical input processing, and process each character
  when typed rather than await a new-line.
*/
    if((tty = open(ttyname(fileno(stdin)), 2)) <= 0)
    {
    	perror("Can't attach the terminal");
    	exit(0);
    }
#if defined(ULTRIX)
    ioctl(tty, TIOCGETP, &restore);
#else
#if defined(LINUX)
    tcgetattr(tty, &restore);
#else
    ioctl(tty, TCGETA, &restore);
#endif
#endif

    special = restore;
    
#if defined(ULTRIX)
    special.sg_flags &= ~(ECHO | CRMOD);
    special.sg_flags |= CBREAK;
    ioctl(tty, TIOCSETP, &special);
#else
    special.c_iflag &= ~ICRNL;
    special.c_oflag &= ~OPOST;
    special.c_lflag &= ~(ECHO | ICANON | ISIG);
    special.c_cc[VEOF] = 1;
#if defined(LINUX)
    tcsetattr(tty, TCSANOW, &special);
#else
    ioctl(tty, TCSETA, &special);
#endif
#endif

}

UEnd()
{

/*
  Restore terminal attributes and close.
*/
#if defined(ULTRIX)
    ioctl(tty, TIOCSETP, &restore);
#else
#if defined(LINUX)
    tcsetattr(tty, TCSANOW, &restore);
#else
    ioctl(tty, TCSETA, &restore);
#endif
#endif
    close(tty);
}

USysenv()
{

/*
  Restore terminal attributes prior to Spawning a command.
*/
#if defined(ULTRIX)
    ioctl(tty, TIOCSETP, &restore);
#else
#if defined(LINUX)
    tcsetattr(tty, TCSANOW, &restore);
#else
    ioctl(tty, TCSETA, &restore);
#endif
#endif
    close(tty);
}

USedtenv()
{

/*
  Set terminal attributes back to Sedt specifications.
*/
    if((tty = open(ttyname(fileno(stdin)), 2)) <= 0)
    {
    	perror("Can't attach the terminal");
    	exit(0);
    }
#if defined(ULTRIX)
    ioctl(tty, TIOCSETP, &special);
#else

#if defined(LINUX)
    tcsetattr(tty, TCSANOW, &special);
#else
    ioctl(tty, TCSETA, &special);
#endif

#endif
}

UGetrm()
{
unsigned
    char    key;

    if((read(tty, &key, 1)) < 1)
    {
    	perror("Can't read from terminal");
    	exit(0);
    }
    return((int)key);
}

UPutrm(string, length)
char    *string;
int     length;
{
    write(tty, string, length);
}

#if defined(SUN)
char _toupper(C)
char C;
{return toupper(C);
}

char _tolower(C)
char C;
{return tolower(C);
}
#endif