]> git.saurik.com Git - apple/network_cmds.git/blob - telnet.tproj/tn3270.c
0e44ea907521120d0e91e2b10c08f66a1d356b13
[apple/network_cmds.git] / telnet.tproj / tn3270.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1988, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56 #ifndef lint
57 static char sccsid[] = "@(#)tn3270.c 8.2 (Berkeley) 5/30/95";
58 #endif /* not lint */
59
60 #include <sys/types.h>
61 #include <arpa/telnet.h>
62
63 #include "general.h"
64
65 #include "defines.h"
66 #include "ring.h"
67 #include "externs.h"
68 #include "fdset.h"
69
70 #if defined(TN3270)
71
72 #include "../ctlr/screen.h"
73 #include "../general/globals.h"
74
75 #include "../sys_curses/telextrn.h"
76 #include "../ctlr/externs.h"
77
78 #if defined(unix) || defined(__APPLE__)
79 int
80 HaveInput, /* There is input available to scan */
81 cursesdata, /* Do we dump curses data? */
82 sigiocount; /* Number of times we got a SIGIO */
83
84 char tline[200];
85 char *transcom = 0; /* transparent mode command (default: none) */
86 #endif /* defined(unix) || defined(__APPLE__) */
87
88 char Ibuf[8*BUFSIZ], *Ifrontp, *Ibackp;
89
90 static char sb_terminal[] = { IAC, SB,
91 TELOPT_TTYPE, TELQUAL_IS,
92 'I', 'B', 'M', '-', '3', '2', '7', '8', '-', '2',
93 IAC, SE };
94 #define SBTERMMODEL 13
95
96 static int
97 Sent3270TerminalType; /* Have we said we are a 3270? */
98
99 #endif /* defined(TN3270) */
100
101
102 void
103 init_3270()
104 {
105 #if defined(TN3270)
106 #if defined(unix) || defined(__APPLE__)
107 HaveInput = 0;
108 sigiocount = 0;
109 #endif /* defined(unix) || defined(__APPLE__) */
110 Sent3270TerminalType = 0;
111 Ifrontp = Ibackp = Ibuf;
112 init_ctlr(); /* Initialize some things */
113 init_keyboard();
114 init_screen();
115 init_system();
116 #endif /* defined(TN3270) */
117 }
118
119 \f
120 #if defined(TN3270)
121
122 /*
123 * DataToNetwork - queue up some data to go to network. If "done" is set,
124 * then when last byte is queued, we add on an IAC EOR sequence (so,
125 * don't call us with "done" until you want that done...)
126 *
127 * We actually do send all the data to the network buffer, since our
128 * only client needs for us to do that.
129 */
130
131 int
132 DataToNetwork(buffer, count, done)
133 register char *buffer; /* where the data is */
134 register int count; /* how much to send */
135 int done; /* is this the last of a logical block */
136 {
137 register int loop, c;
138 int origCount;
139
140 origCount = count;
141
142 while (count) {
143 /* If not enough room for EORs, IACs, etc., wait */
144 if (NETROOM() < 6) {
145 fd_set o;
146
147 FD_ZERO(&o);
148 netflush();
149 while (NETROOM() < 6) {
150 FD_SET(net, &o);
151 (void) select(net+1, (fd_set *) 0, &o, (fd_set *) 0,
152 (struct timeval *) 0);
153 netflush();
154 }
155 }
156 c = ring_empty_count(&netoring);
157 if (c > count) {
158 c = count;
159 }
160 loop = c;
161 while (loop) {
162 if (((unsigned char)*buffer) == IAC) {
163 break;
164 }
165 buffer++;
166 loop--;
167 }
168 if ((c = c-loop)) {
169 ring_supply_data(&netoring, buffer-c, c);
170 count -= c;
171 }
172 if (loop) {
173 NET2ADD(IAC, IAC);
174 count--;
175 buffer++;
176 }
177 }
178
179 if (done) {
180 NET2ADD(IAC, EOR);
181 netflush(); /* try to move along as quickly as ... */
182 }
183 return(origCount - count);
184 }
185
186
187 #if defined(unix) || defined(__APPLE__)
188 void
189 inputAvailable(signo)
190 int signo;
191 {
192 HaveInput = 1;
193 sigiocount++;
194 }
195 #endif /* defined(unix) || defined(__APPLE__) */
196
197 void
198 outputPurge()
199 {
200 (void) ttyflush(1);
201 }
202
203
204 /*
205 * The following routines are places where the various tn3270
206 * routines make calls into telnet.c.
207 */
208
209 /*
210 * DataToTerminal - queue up some data to go to terminal.
211 *
212 * Note: there are people who call us and depend on our processing
213 * *all* the data at one time (thus the select).
214 */
215
216 int
217 DataToTerminal(buffer, count)
218 register char *buffer; /* where the data is */
219 register int count; /* how much to send */
220 {
221 register int c;
222 int origCount;
223
224 origCount = count;
225
226 while (count) {
227 if (TTYROOM() == 0) {
228 #if defined(unix) || defined(__APPLE__)
229 fd_set o;
230
231 FD_ZERO(&o);
232 #endif /* defined(unix) || defined(__APPLE__) */
233 (void) ttyflush(0);
234 while (TTYROOM() == 0) {
235 #if defined(unix) || defined(__APPLE__)
236 FD_SET(tout, &o);
237 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
238 (struct timeval *) 0);
239 #endif /* defined(unix) || defined(__APPLE__) */
240 (void) ttyflush(0);
241 }
242 }
243 c = TTYROOM();
244 if (c > count) {
245 c = count;
246 }
247 ring_supply_data(&ttyoring, buffer, c);
248 count -= c;
249 buffer += c;
250 }
251 return(origCount);
252 }
253 \f
254
255 /*
256 * Push3270 - Try to send data along the 3270 output (to screen) direction.
257 */
258
259 int
260 Push3270()
261 {
262 int save = ring_full_count(&netiring);
263
264 if (save) {
265 if (Ifrontp+save > Ibuf+sizeof Ibuf) {
266 if (Ibackp != Ibuf) {
267 memmove(Ibuf, Ibackp, Ifrontp-Ibackp);
268 Ifrontp -= (Ibackp-Ibuf);
269 Ibackp = Ibuf;
270 }
271 }
272 if (Ifrontp+save < Ibuf+sizeof Ibuf) {
273 (void)telrcv();
274 }
275 }
276 return save != ring_full_count(&netiring);
277 }
278
279
280 /*
281 * Finish3270 - get the last dregs of 3270 data out to the terminal
282 * before quitting.
283 */
284
285 void
286 Finish3270()
287 {
288 while (Push3270() || !DoTerminalOutput()) {
289 #if defined(unix) || defined(__APPLE__)
290 HaveInput = 0;
291 #endif /* defined(unix) || defined(__APPLE__) */
292 ;
293 }
294 }
295
296
297 /* StringToTerminal - output a null terminated string to the terminal */
298
299 void
300 StringToTerminal(s)
301 char *s;
302 {
303 int count;
304
305 count = strlen(s);
306 if (count) {
307 (void) DataToTerminal(s, count); /* we know it always goes... */
308 }
309 }
310
311
312 #if ((!defined(NOT43)) || defined(PUTCHAR))
313 /* _putchar - output a single character to the terminal. This name is so that
314 * curses(3x) can call us to send out data.
315 */
316
317 void
318 _putchar(c)
319 char c;
320 {
321 #if defined(sun) /* SunOS 4.0 bug */
322 c &= 0x7f;
323 #endif /* defined(sun) */
324 if (cursesdata) {
325 Dump('>', &c, 1);
326 }
327 if (!TTYROOM()) {
328 (void) DataToTerminal(&c, 1);
329 } else {
330 TTYADD(c);
331 }
332 }
333 #endif /* ((!defined(NOT43)) || defined(PUTCHAR)) */
334
335 void
336 SetIn3270()
337 {
338 if (Sent3270TerminalType && my_want_state_is_will(TELOPT_BINARY)
339 && my_want_state_is_do(TELOPT_BINARY) && !donebinarytoggle) {
340 if (!In3270) {
341 In3270 = 1;
342 Init3270(); /* Initialize 3270 functions */
343 /* initialize terminal key mapping */
344 InitTerminal(); /* Start terminal going */
345 setconnmode(0);
346 }
347 } else {
348 if (In3270) {
349 StopScreen(1);
350 In3270 = 0;
351 Stop3270(); /* Tell 3270 we aren't here anymore */
352 setconnmode(0);
353 }
354 }
355 }
356
357 /*
358 * tn3270_ttype()
359 *
360 * Send a response to a terminal type negotiation.
361 *
362 * Return '0' if no more responses to send; '1' if a response sent.
363 */
364
365 int
366 tn3270_ttype()
367 {
368 /*
369 * Try to send a 3270 type terminal name. Decide which one based
370 * on the format of our screen, and (in the future) color
371 * capaiblities.
372 */
373 InitTerminal(); /* Sets MaxNumberColumns, MaxNumberLines */
374 if ((MaxNumberLines >= 24) && (MaxNumberColumns >= 80)) {
375 Sent3270TerminalType = 1;
376 if ((MaxNumberLines >= 27) && (MaxNumberColumns >= 132)) {
377 MaxNumberLines = 27;
378 MaxNumberColumns = 132;
379 sb_terminal[SBTERMMODEL] = '5';
380 } else if (MaxNumberLines >= 43) {
381 MaxNumberLines = 43;
382 MaxNumberColumns = 80;
383 sb_terminal[SBTERMMODEL] = '4';
384 } else if (MaxNumberLines >= 32) {
385 MaxNumberLines = 32;
386 MaxNumberColumns = 80;
387 sb_terminal[SBTERMMODEL] = '3';
388 } else {
389 MaxNumberLines = 24;
390 MaxNumberColumns = 80;
391 sb_terminal[SBTERMMODEL] = '2';
392 }
393 NumberLines = 24; /* before we start out... */
394 NumberColumns = 80;
395 ScreenSize = NumberLines*NumberColumns;
396 if ((MaxNumberLines*MaxNumberColumns) > MAXSCREENSIZE) {
397 ExitString("Programming error: MAXSCREENSIZE too small.\n",
398 1);
399 /*NOTREACHED*/
400 }
401 printsub('>', sb_terminal+2, sizeof sb_terminal-2);
402 ring_supply_data(&netoring, sb_terminal, sizeof sb_terminal);
403 return 1;
404 } else {
405 return 0;
406 }
407 }
408
409 #if defined(unix) || defined(__APPLE__)
410 int
411 settranscom(argc, argv)
412 int argc;
413 char *argv[];
414 {
415 int i;
416
417 if (argc == 1 && transcom) {
418 transcom = 0;
419 }
420 if (argc == 1) {
421 return 1;
422 }
423 transcom = tline;
424 (void) strcpy(transcom, argv[1]);
425 for (i = 2; i < argc; ++i) {
426 (void) strcat(transcom, " ");
427 (void) strcat(transcom, argv[i]);
428 }
429 return 1;
430 }
431 #endif /* defined(unix) || defined(__APPLE__) */
432
433 #endif /* defined(TN3270) */