1 
2 /*
3  * The functions in this file negotiate with the operating system for
4  * characters, and write characters in a barely buffered fashion on the display.
5  */
6 
7 module termio;
8 
9 version (Posix)
10 {
11 
12 import core.stdc.stdio;
13 import core.sys.posix.termios;
14 import core.sys.posix.sys.ioctl;
15 
16 import ed;
17 
18 extern (C) void cfmakeraw(in termios*);
19 
20 
21 termios  ostate;                 /* saved tty state */
22 termios  nstate;                 /* values for editor mode */
23 
24 
25 /*
26  * This function is called once to set up the terminal device streams.
27  * On VMS, it translates SYS$INPUT until it finds the terminal, then assigns
28  * a channel to it and sets it raw. On CPM it is a no-op.
29  */
30 void ttopen()
31 {
32         /* Adjust output channel        */
33         tcgetattr(1, &ostate);                       /* save old state */
34         tcgetattr(1, &nstate);                       /* get base of new state */
35 	cfmakeraw(&nstate);
36         tcsetattr(1, TCSADRAIN, &nstate);      /* set mode */
37 }
38 
39 /*
40  * This function gets called just before we go back home to the command
41  * interpreter. On VMS it puts the terminal back in a reasonable state.
42  * Another no-operation on CPM.
43  */
44 void ttclose()
45 {
46         tcsetattr(1, TCSADRAIN, &ostate);	// return to original mode
47 }
48 
49 /*
50  * Write a character to the display. On VMS, terminal output is buffered, and
51  * we just put the characters in the big array, after checking for overflow.
52  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
53  * MS-DOS (use the very very raw console output routine).
54  */
55 void ttputc(char c)
56 {
57         fputc(c, stdout);
58 }
59 
60 /*
61  * Flush terminal buffer. Does real work where the terminal output is buffered
62  * up. A no-operation on systems where byte at a time terminal I/O is done.
63  */
64 void ttflush()
65 {
66         fflush(stdout);
67 }
68 
69 /*
70  * Read a character from the terminal, performing no editing and doing no echo
71  * at all. More complex in VMS that almost anyplace else, which figures. Very
72  * simple on CPM, because the system can do exactly what you want.
73  */
74 int ttgetc()
75 {
76         return fgetc(stdin);
77 }
78 
79 /**************************
80  * Return TRUE if there are unread chars ready in the input.
81  */
82 
83 int ttkeysininput()
84 {
85 	int n;
86 	ioctl(0, FIONREAD, &n);
87         return n != 0;
88 }
89 
90 /**************************
91  * Return when there are unread chars ready in the input.
92  */
93 
94 void ttwaitkeys()
95 {
96     import core.sys.posix.sys.select;
97     fd_set readfds;
98     FD_ZERO(&readfds);
99     FD_SET(0, &readfds);
100     select(1, &readfds, null, null, null);
101 } 
102 
103 /******************************
104  */
105 
106 void ttyield()
107 {
108 }
109 
110 }