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