1 | #
|
---|
2 | (*
|
---|
3 | (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
---|
4 | See the copyright notice in the ACK home directory, in the file "Copyright".
|
---|
5 | *)
|
---|
6 |
|
---|
7 | (*$R-*)
|
---|
8 | IMPLEMENTATION MODULE Terminal;
|
---|
9 | (*
|
---|
10 | Module: Input/Output to/from terminals
|
---|
11 | Author: Ceriel J.H. Jacobs
|
---|
12 | Version: $Header: /cvsup/minix/src/lib/ack/libm2/Terminal.mod,v 1.1 2005/10/10 15:27:46 beng Exp $
|
---|
13 |
|
---|
14 | Implementation for Unix.
|
---|
15 | *)
|
---|
16 | FROM SYSTEM IMPORT ADR;
|
---|
17 | #ifdef __USG
|
---|
18 | FROM Unix IMPORT read, write, open, fcntl;
|
---|
19 | #else
|
---|
20 | FROM Unix IMPORT read, write, open, ioctl;
|
---|
21 | #endif
|
---|
22 | VAR fildes: INTEGER;
|
---|
23 | unreadch: CHAR;
|
---|
24 | unread: BOOLEAN;
|
---|
25 | tty: ARRAY[0..8] OF CHAR;
|
---|
26 |
|
---|
27 | PROCEDURE Read(VAR ch: CHAR);
|
---|
28 | BEGIN
|
---|
29 | IF unread THEN
|
---|
30 | ch := unreadch;
|
---|
31 | unread := FALSE
|
---|
32 | ELSE
|
---|
33 | IF read(fildes, ADR(ch), 1) < 0 THEN
|
---|
34 | ;
|
---|
35 | END;
|
---|
36 | END;
|
---|
37 | unreadch := ch;
|
---|
38 | END Read;
|
---|
39 |
|
---|
40 | PROCEDURE BusyRead(VAR ch: CHAR);
|
---|
41 | VAR l: INTEGER;
|
---|
42 | BEGIN
|
---|
43 | IF unread THEN
|
---|
44 | ch := unreadch;
|
---|
45 | unread := FALSE
|
---|
46 | ELSE
|
---|
47 | #ifdef __USG
|
---|
48 | l := fcntl(fildes, (*FGETFL*) 3, 0);
|
---|
49 | IF fcntl(fildes,
|
---|
50 | (* FSETFL *) 4,
|
---|
51 | l + (*ONDELAY*) 2) < 0 THEN
|
---|
52 | ;
|
---|
53 | END;
|
---|
54 | IF read(fildes, ADR(ch), 1) = 0 THEN
|
---|
55 | ch := 0C;
|
---|
56 | ELSE
|
---|
57 | unreadch := ch;
|
---|
58 | END;
|
---|
59 | IF fcntl(fildes, (*FSETFL*)4, l) < 0 THEN
|
---|
60 | ;
|
---|
61 | END;
|
---|
62 | #else
|
---|
63 | #ifdef __BSD4_2
|
---|
64 | IF ioctl(fildes, INTEGER(ORD('f')*256+127+4*65536+40000000H), ADR(l)) < 0 THEN
|
---|
65 | #else
|
---|
66 | IF ioctl(fildes, INTEGER(ORD('f')*256+127), ADR(l)) < 0 THEN
|
---|
67 | #endif
|
---|
68 | ;
|
---|
69 | END;
|
---|
70 |
|
---|
71 | IF l = 0 THEN
|
---|
72 | ch := 0C;
|
---|
73 | ELSE
|
---|
74 | IF read(fildes, ADR(ch), 1) < 0 THEN
|
---|
75 | ;
|
---|
76 | END;
|
---|
77 | unreadch := ch;
|
---|
78 | END;
|
---|
79 | #endif
|
---|
80 | END;
|
---|
81 | END BusyRead;
|
---|
82 |
|
---|
83 | PROCEDURE ReadAgain;
|
---|
84 | BEGIN
|
---|
85 | unread := TRUE;
|
---|
86 | END ReadAgain;
|
---|
87 |
|
---|
88 | PROCEDURE Write(ch: CHAR);
|
---|
89 | BEGIN
|
---|
90 | IF write(fildes, ADR(ch), 1) < 0 THEN
|
---|
91 | ;
|
---|
92 | END;
|
---|
93 | END Write;
|
---|
94 |
|
---|
95 | PROCEDURE WriteLn;
|
---|
96 | BEGIN
|
---|
97 | Write(12C);
|
---|
98 | END WriteLn;
|
---|
99 |
|
---|
100 | PROCEDURE WriteString(s: ARRAY OF CHAR);
|
---|
101 | VAR i: CARDINAL;
|
---|
102 | BEGIN
|
---|
103 | i := 0;
|
---|
104 | WHILE (i <= HIGH(s)) & (s[i] # 0C) DO
|
---|
105 | Write(s[i]);
|
---|
106 | INC(i)
|
---|
107 | END
|
---|
108 | END WriteString;
|
---|
109 |
|
---|
110 | BEGIN
|
---|
111 | tty := "/dev/tty";
|
---|
112 | fildes := open(ADR(tty), 2);
|
---|
113 | unread := FALSE;
|
---|
114 | END Terminal.
|
---|