xref: /original-bsd/usr.bin/telnet/tn3270.c (revision 6761bafc)
1 void
2 tn3270_init()
3 {
4 #if	defined(TN3270)
5     Sent3270TerminalType = 0;
6     Ifrontp = Ibackp = Ibuf;
7     init_ctlr();		/* Initialize some things */
8     init_keyboard();
9     init_screen();
10     init_system();
11 #endif	/* defined(TN3270) */
12 }
13 
14 #if	defined(TN3270)
15 
16 /*
17  * DataToNetwork - queue up some data to go to network.  If "done" is set,
18  * then when last byte is queued, we add on an IAC EOR sequence (so,
19  * don't call us with "done" until you want that done...)
20  *
21  * We actually do send all the data to the network buffer, since our
22  * only client needs for us to do that.
23  */
24 
25 int
26 DataToNetwork(buffer, count, done)
27 register char	*buffer;	/* where the data is */
28 register int	count;		/* how much to send */
29 int		done;		/* is this the last of a logical block */
30 {
31     register int c;
32     int origCount;
33     fd_set o;
34 
35     origCount = count;
36     FD_ZERO(&o);
37 
38     while (count) {
39 	if ((netobuf+sizeof netobuf - nfrontp) < 6) {
40 	    netflush();
41 	    while ((netobuf+sizeof netobuf - nfrontp) < 6) {
42 		FD_SET(net, &o);
43 		(void) select(net+1, (fd_set *) 0, &o, (fd_set *) 0,
44 						(struct timeval *) 0);
45 		netflush();
46 	    }
47 	}
48 	c = *buffer++;
49 	count--;
50 	if (c == IAC) {
51 	    *nfrontp++ = IAC;
52 	    *nfrontp++ = IAC;
53 	} else {
54 	    *nfrontp++ = c;
55 	}
56     }
57 
58     if (done && !count) {
59 	*nfrontp++ = IAC;
60 	*nfrontp++ = EOR;
61 	netflush();		/* try to move along as quickly as ... */
62     }
63     return(origCount - count);
64 }
65 
66 
67 #if	defined(unix)
68 static void
69 inputAvailable()
70 {
71     HaveInput = 1;
72 }
73 #endif	/* defined(unix) */
74 
75 void
76 outputPurge()
77 {
78     ttyflush(1);
79 }
80 
81 
82 /*
83  * The following routines are places where the various tn3270
84  * routines make calls into telnet.c.
85  */
86 
87 /* TtyChars() - returns the number of characters in the TTY buffer */
88 TtyChars()
89 {
90     return(tfrontp-tbackp);
91 }
92 
93 /* DataToTerminal - queue up some data to go to terminal. */
94 
95 int
96 DataToTerminal(buffer, count)
97 register char	*buffer;		/* where the data is */
98 register int	count;			/* how much to send */
99 {
100     int origCount;
101 #if	defined(unix)
102     fd_set	o;
103 
104     FD_ZERO(&o);
105 #endif	/* defined(unix) */
106     origCount = count;
107 
108     while (count) {
109 	if (tfrontp >= ttyobuf+sizeof ttyobuf) {
110 	    ttyflush(0);
111 	    while (tfrontp >= ttyobuf+sizeof ttyobuf) {
112 #if	defined(unix)
113 		FD_SET(tout, &o);
114 		(void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
115 						(struct timeval *) 0);
116 #endif	/* defined(unix) */
117 		ttyflush(0);
118 	    }
119 	}
120 	*tfrontp++ = *buffer++;
121 	count--;
122     }
123     return(origCount - count);
124 }
125 
126 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
127  *			Note that we consider the buffer to run all the
128  *			way to the kernel (thus the select).
129  */
130 
131 void
132 EmptyTerminal()
133 {
134 #if	defined(unix)
135     fd_set	o;
136 
137     FD_ZERO(&o);
138 #endif	/* defined(unix) */
139 
140     if (tfrontp == tbackp) {
141 #if	defined(unix)
142 	FD_SET(tout, &o);
143 	(void) select(tout+1, (int *) 0, &o, (int *) 0,
144 			(struct timeval *) 0);	/* wait for TTLOWAT */
145 #endif	/* defined(unix) */
146     } else {
147 	while (tfrontp != tbackp) {
148 	    ttyflush(0);
149 #if	defined(unix)
150 	    FD_SET(tout, &o);
151 	    (void) select(tout+1, (int *) 0, &o, (int *) 0,
152 				(struct timeval *) 0);	/* wait for TTLOWAT */
153 #endif	/* defined(unix) */
154 	}
155     }
156 }
157 
158 
159 /*
160  * Push3270 - Try to send data along the 3270 output (to screen) direction.
161  */
162 
163 static int
164 Push3270()
165 {
166     int save = scc;
167 
168     if (scc) {
169 	if (Ifrontp+scc > Ibuf+sizeof Ibuf) {
170 	    if (Ibackp != Ibuf) {
171 		memcpy(Ibuf, Ibackp, Ifrontp-Ibackp);
172 		Ifrontp -= (Ibackp-Ibuf);
173 		Ibackp = Ibuf;
174 	    }
175 	}
176 	if (Ifrontp+scc < Ibuf+sizeof Ibuf) {
177 	    telrcv();
178 	}
179     }
180     return save != scc;
181 }
182 
183 
184 /*
185  * Finish3270 - get the last dregs of 3270 data out to the terminal
186  *		before quitting.
187  */
188 
189 static void
190 Finish3270()
191 {
192     while (Push3270() || !DoTerminalOutput()) {
193 #if	defined(unix)
194 	HaveInput = 0;
195 #endif	/* defined(unix) */
196 	;
197     }
198 }
199 
200 
201 /* StringToTerminal - output a null terminated string to the terminal */
202 
203 void
204 StringToTerminal(s)
205 char *s;
206 {
207     int count;
208 
209     count = strlen(s);
210     if (count) {
211 	(void) DataToTerminal(s, count);	/* we know it always goes... */
212     }
213 }
214 
215 
216 #if	((!defined(NOT43)) || defined(PUTCHAR))
217 /* _putchar - output a single character to the terminal.  This name is so that
218  *	curses(3x) can call us to send out data.
219  */
220 
221 void
222 _putchar(c)
223 char c;
224 {
225     if (tfrontp >= ttyobuf+sizeof ttyobuf) {
226 	(void) DataToTerminal(&c, 1);
227     } else {
228 	*tfrontp++ = c;		/* optimize if possible. */
229     }
230 }
231 #endif	/* ((!defined(NOT43)) || defined(PUTCHAR)) */
232 
233 #endif	/* defined(TN3270) */
234