Debian Codegraph @

telnetd/term.c

raw
1 /*
2 Copyright (C) 2002-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #include <telnetd.h>
22
23 #include <fcntl.h>
24
25 typedef struct termios TERMDESC;
26 #define _term_getattr tcgetattr
27 #define _term_setattr(fd, tp) tcsetattr (fd, TCSANOW, tp)
28
29 TERMDESC termbuf, termbuf2;
30
31 #ifdef IOCTL_INTERFACE
32
33 void
34 _term_setattr (int fd, TERMDESC *tp)
35 {
36 ioctl (fd, TIOCSETN, (char *) &tp->sg);
37 ioctl (fd, TIOCSETC, (char *) &tp->tc);
38 ioctl (fd, TIOCSLTC, (char *) &tp->ltc);
39 ioctl (fd, TIOCLSET, &tp->lflags);
40 }
41
42 void
43 term_send_eof (void)
44 {
45 /*nothing */
46 }
47
48 int
49 term_change_eof (void)
50 {
51 return 0;
52 }
53
54 int
55 tty_linemode (void)
56 {
57 return termbuf.state & TS_EXTPROC;
58 }
59
60 void
61 tty_setlinemode (int on)
62 {
63 # ifdef TIOCEXT
64 set_termbuf ();
65 ioctl (pty, TIOCEXT, (char *) &on);
66 init_termbuf ();
67 # else/* !TIOCEXT */
68 # ifdef EXTPROC
69 if (on)
70 termbuf.c_lflag |= EXTPROC;
71 else
72 termbuf.c_lflag &= ~EXTPROC;
73 # endif
74 # endif/* TIOCEXT */
75 }
76
77 int
78 tty_isecho (void)
79 {
80 return termbuf.sg.sg_flags & ECHO;
81 }
82
83 int
84 tty_flowmode (void)
85 {
86 return ((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0;
87 }
88
89 int
90 tty_restartany (void)
91 {
92 # ifdef DECCTQ
93 return (termbuf.lflags & DECCTQ) ? 0 : 1;
94 # else
95 return -1;
96 # endif
97 }
98
99 void
100 tty_setecho (int on)
101 {
102 if (on)
103 termbuf.sg.sg_flags |= ECHO | CRMOD;
104 else
105 termbuf.sg.sg_flags &= ~(ECHO | CRMOD);
106 }
107
108 int
109 tty_israw (void)
110 {
111 return termbuf.sg.sg_flags & RAW;
112 }
113
114 # if defined AUTHENTICATION && defined NO_LOGIN_F && defined LOGIN_R
115 int
116 tty_setraw (int on)
117 {
118 if (on)
119 termbuf.sg.sg_flags |= RAW;
120 else
121 termbuf.sg.sg_flags &= ~RAW;
122 }
123 # endif
124
125 void
126 tty_binaryin (int on)
127 {
128 if (on)
129 termbuf.lflags |= LPASS8;
130 else
131 termbuf.lflags &= ~LPASS8;
132 }
133
134 void
135 tty_binaryout (int on)
136 {
137 if (on)
138 termbuf.lflags |= LLITOUT;
139 else
140 termbuf.lflags &= ~LLITOUT;
141 }
142
143 int
144 tty_isbinaryin (void)
145 {
146 return termbuf.lflags & LPASS8;
147 }
148
149 int
150 tty_isbinaryout (void)
151 {
152 return termbuf.lflags & LLITOUT;
153 }
154
155 int
156 tty_isediting (void)
157 {
158 return !(termbuf.sg.sg_flags & (CBREAK | RAW));
159 }
160
161 int
162 tty_istrapsig (void)
163 {
164 return !(termbuf.sg.sg_flags & RAW);
165 }
166
167 void
168 tty_setedit (int on)
169 {
170 if (on)
171 termbuf.sg.sg_flags &= ~CBREAK;
172 else
173 termbuf.sg.sg_flags |= CBREAK;
174 }
175
176 void
177 tty_setsig (int on)
178 {
179 }
180
181 int
182 tty_issofttab (void)
183 {
184 return termbuf.sg.sg_flags & XTABS;
185 }
186
187 void
188 tty_setsofttab (int on)
189 {
190 if (on)
191 termbuf.sg.sg_flags |= XTABS;
192 else
193 termbuf.sg.sg_flags &= ~XTABS;
194 }
195
196 int
197 tty_islitecho (void)
198 {
199 return !(termbuf.lflags & LCTLECH);
200 }
201
202 void
203 tty_setlitecho (int on)
204 {
205 if (on)
206 termbuf.lflags &= ~LCTLECH;
207 else
208 termbuf.lflags |= LCTLECH;
209 }
210
211 int
212 tty_iscrnl (void)
213 {
214 return termbuf.sg.sg_flags & CRMOD;
215 }
216
217 #else /* !IOCTL_INTERFACE */
218
219 # define termdesc_eofc c_cc[VEOF]
220 # define termdesc_erase c_cc[VERASE]
221 # define termdesc_kill c_cc[VKILL]
222 # define termdesc_ip c_cc[VINTR]
223 # define termdesc_abort c_cc[VQUIT]
224 # ifdef VSTART
225 # define termdesc_xon c_cc[VSTART]
226 # endif
227 # ifdef VSTOP
228 # define termdesc_xoff c_cc[VSTOP]
229 # endif
230
231 # if !defined VDISCARD && defined VFLUSHO
232 # define VDISCARD VFLUSHO
233 # endif
234 # ifdef VDISCARD
235 # define termdesc_ao c_cc[VDISCARD]
236 # endif
237
238 # ifdef VSUSP
239 # define termdesc_susp c_cc[VSUSP]
240 # endif
241 # ifdef VWERASE
242 # define termdesc_ew c_cc[VWERASE]
243 # endif
244 # ifdef VREPRINT
245 # define termdesc_rp c_cc[VREPRINT]
246 # endif
247 # ifdef VLNEXT
248 # define termdesc_lnext c_cc[VLNEXT]
249 # endif
250 # ifdef VEOL
251 # define termdesc_forw1 c_cc[VEOL]
252 # endif
253 # ifdef VEOL2
254 # define termdesc_forw2 c_cc[VEOL2]
255 # endif
256 # ifdef VSTATUS
257 # define termdesc_status c_cc[VSTATUS]
258 # endif
259
260 # if VEOF == VMIN
261 static cc_t oldeofc = '\004';
262 # endif
263
264 void
265 term_send_eof (void)
266 {
267 # if VEOF == VMIN
268 if (!tty_isediting ())
269 pty_output_byte (oldeofc);
270 # endif
271 }
272
273 int
274 term_change_eof (void)
275 {
276 # if VEOF == VMIN
277 if (!tty_isediting ())
278 return 1;
279 if (slctab[SLC_EOF].sptr)
280 oldeofc = *slctab[SLC_EOF].sptr;
281 # endif
282
283 return 0;
284 }
285
286 int
287 tty_linemode (void)
288 {
289 # ifdef EXTPROC
290 return (termbuf.c_lflag & EXTPROC);
291 # else
292 return 0; /* Can't ever set it either. */
293 # endif/* EXTPROC */
294 }
295
296 void
297 tty_setlinemode (int on)
298 {
299 # ifdef TIOCEXT
300 set_termbuf ();
301 ioctl (pty, TIOCEXT, (char *) &on);
302 init_termbuf ();
303 # else/* !TIOCEXT */
304 # ifdef EXTPROC
305 if (on)
306 termbuf.c_lflag |= EXTPROC;
307 else
308 termbuf.c_lflag &= ~EXTPROC;
309 # else
310 /* !EXTPROC */
311 (void) on; /* Silence warnings. */
312 # endif
313 # endif/* TIOCEXT */
314 }
315
316 int
317 tty_isecho (void)
318 {
319 return termbuf.c_lflag & ECHO;
320 }
321
322 int
323 tty_flowmode (void)
324 {
325 return (termbuf.c_iflag & IXON) ? 1 : 0;
326 }
327
328 int
329 tty_restartany (void)
330 {
331 return (termbuf.c_iflag & IXANY) ? 1 : 0;
332 }
333
334 void
335 tty_setecho (int on)
336 {
337 if (on)
338 termbuf.c_lflag |= ECHO;
339 else
340 termbuf.c_lflag &= ~ECHO;
341 }
342
343 int
344 tty_israw (void)
345 {
346 return !(termbuf.c_lflag & ICANON);
347 }
348
349 # if defined AUTHENTICATION && defined NO_LOGIN_F && defined LOGIN_R
350 int
351 tty_setraw (int on)
352 {
353 if (on)
354 termbuf.c_lflag &= ~ICANON;
355 else
356 termbuf.c_lflag |= ICANON;
357 }
358 # endif
359
360 void
361 tty_binaryin (int on)
362 {
363 if (on)
364 termbuf.c_iflag &= ~ISTRIP;
365 else
366 termbuf.c_iflag |= ISTRIP;
367 }
368
369 void
370 tty_binaryout (int on)
371 {
372 if (on)
373 {
374 termbuf.c_cflag &= ~(CSIZE | PARENB);
375 termbuf.c_cflag |= CS8;
376 termbuf.c_oflag &= ~OPOST;
377 }
378 else
379 {
380 termbuf.c_cflag &= ~CSIZE;
381 termbuf.c_cflag |= CS7 | PARENB;
382 termbuf.c_oflag |= OPOST;
383 }
384 }
385
386 int
387 tty_isbinaryin (void)
388 {
389 return !(termbuf.c_iflag & ISTRIP);
390 }
391
392 int
393 tty_isbinaryout (void)
394 {
395 return !(termbuf.c_oflag & OPOST);
396 }
397
398 int
399 tty_isediting (void)
400 {
401 return termbuf.c_lflag & ICANON;
402 }
403
404 int
405 tty_istrapsig (void)
406 {
407 return termbuf.c_lflag & ISIG;
408 }
409
410 void
411 tty_setedit (int on)
412 {
413 if (on)
414 termbuf.c_lflag |= ICANON;
415 else
416 termbuf.c_lflag &= ~ICANON;
417 }
418
419 void
420 tty_setsig (int on)
421 {
422 if (on)
423 termbuf.c_lflag |= ISIG;
424 else
425 termbuf.c_lflag &= ~ISIG;
426 }
427
428 int
429 tty_issofttab (void)
430 {
431 # ifdef OXTABS
432 return termbuf.c_oflag & OXTABS;
433 # endif
434 # ifdef TABDLY
435 return (termbuf.c_oflag & TABDLY) == TAB3;
436 # endif
437 }
438
439 void
440 tty_setsofttab (int on)
441 {
442 if (on)
443 {
444 # ifdef OXTABS
445 termbuf.c_oflag |= OXTABS;
446 # endif
447 # ifdef TABDLY
448 termbuf.c_oflag &= ~TABDLY;
449 termbuf.c_oflag |= TAB3;
450 # endif
451 }
452 else
453 {
454 # ifdef OXTABS
455 termbuf.c_oflag &= ~OXTABS;
456 # endif
457 # ifdef TABDLY
458 termbuf.c_oflag &= ~TABDLY;
459 termbuf.c_oflag |= TAB0;
460 # endif
461 }
462 }
463
464 int
465 tty_islitecho (void)
466 {
467 # ifdef ECHOCTL
468 return !(termbuf.c_lflag & ECHOCTL);
469 # endif
470 # ifdef TCTLECH
471 return !(termbuf.c_lflag & TCTLECH);
472 # endif
473 # if !defined ECHOCTL && !defined TCTLECH
474 return 0; /* assumes ctl chars are echoed '^x' */
475 # endif
476 }
477
478 void
479 tty_setlitecho (int on)
480 {
481 # ifdef ECHOCTL
482 if (on)
483 termbuf.c_lflag &= ~ECHOCTL;
484 else
485 termbuf.c_lflag |= ECHOCTL;
486 # endif
487 # ifdef TCTLECH
488 if (on)
489 termbuf.c_lflag &= ~TCTLECH;
490 else
491 termbuf.c_lflag |= TCTLECH;
492 # endif
493 }
494
495 int
496 tty_iscrnl (void)
497 {
498 return termbuf.c_iflag & ICRNL;
499 }
500
501 #endif /* !IOCTL_INTERFACE */
502
503 void
504 init_termbuf (void)
505 {
506 #if !defined SOLARIS10 && !defined SOLARIS
507 _term_getattr (pty, &termbuf);
508 #else /* SOLARIS || SOLARIS10 */
509 /* On Solaris the master PTY is not able to report terminal
510 * settings about the slave TTY, since it is only the slave
511 * that is working with an designated line discipline.
512 * Therefore we must determine the slave descriptor, which
513 * exists only if ptsname() returns a non-empty string.
514 * This happens for the parent process. The child process
515 * sees an empty name, so undergoes minimal processing.
516 */
517 int tty = pty;
518 char *name = ptsname (pty);
519
520 if (name)
521 /* Minimal access means read only! */
522 tty = open (name, O_RDONLY | O_NONBLOCK);
523
524 _term_getattr (tty, &termbuf);
525
526 if (name)
527 close (tty);
528 #endif /* SOLARIS || SOLARIS10 */
529
530 termbuf2 = termbuf;
531 }
532
533 #if defined TIOCPKT_IOCTL
534 /*FIXME: Hardly needed?
535 * Built by OpenSolaris and BSD, though. */
536 void
537 copy_termbuf (void)
538 {
539 size_t len = 0;
540 char *cp = (char *) &termbuf;
541
542 while (pty_input_level () > 0)
543 {
544 if (len >= sizeof (termbuf))
545 break;
546 *cp++ = pty_get_char (0);
547 len++;
548 }
549 termbuf2 = termbuf;
550 }
551 #endif
552
553 void
554 set_termbuf (void)
555 {
556 if (memcmp (&termbuf, &termbuf2, sizeof (termbuf)))
557 #if !defined SOLARIS10 && !defined SOLARIS
558 _term_setattr (pty, &termbuf);
559 #else /* SOLARIS || SOLARIS10 */
560 {
561 /* Same reason as with _term_getattr. */
562 int tty = pty;
563 char *name = ptsname (pty);
564
565 if (name)
566 tty = open (name, O_RDWR | O_NONBLOCK | O_NOCTTY);
567
568 _term_setattr (tty, &termbuf);
569
570 if (name)
571 close (tty);
572 }
573 #endif /* SOLARIS || SOLARIS10 */
574 }
575
576 /* spcset(func, valp, valpp)
577
578 This function takes various special characters (func), and
579 sets *valp to the current value of that character, and
580 *valpp to point to where in the "termbuf" structure that
581 value is kept.
582
583 It returns the SLC_ level of support for this function. */
584
585 #define setval(a, b) *valp = termbuf.a ; \
586 *valpp = &termbuf.a ; \
587 return b;
588 #define defval(a) *valp = ((cc_t)a); \
589 *valpp = (cc_t *)0; \
590 return SLC_DEFAULT;
591
592 int
593 spcset (int func, cc_t *valp, cc_t **valpp)
594 {
595 switch (func)
596 {
597 case SLC_EOF:
598 setval (termdesc_eofc, SLC_VARIABLE);
599
600 case SLC_EC:
601 setval (termdesc_erase, SLC_VARIABLE);
602
603 case SLC_EL:
604 setval (termdesc_kill, SLC_VARIABLE);
605
606 case SLC_IP:
607 setval (termdesc_ip, SLC_VARIABLE | SLC_FLUSHIN | SLC_FLUSHOUT);
608
609 case SLC_ABORT:
610 setval (termdesc_abort, SLC_VARIABLE | SLC_FLUSHIN | SLC_FLUSHOUT);
611
612 case SLC_XON:
613 #ifdef termdesc_xon
614 setval (termdesc_xon, SLC_VARIABLE);
615 #else
616 defval (0x13);
617 #endif
618
619 case SLC_XOFF:
620 #ifdef termdesc_xoff
621 setval (termdesc_xoff, SLC_VARIABLE);
622 #else
623 defval (0x11);
624 #endif
625
626 case SLC_EW:
627 #ifdef termdesc_ew
628 setval (termdesc_ew, SLC_VARIABLE);
629 #else
630 defval (0);
631 #endif
632
633 case SLC_RP:
634 #ifdef termdesc_rp
635 setval (termdesc_rp, SLC_VARIABLE);
636 #else
637 defval (0);
638 #endif
639
640 case SLC_LNEXT:
641 #ifdef termdesc_lnext
642 setval (termdesc_lnext, SLC_VARIABLE);
643 #else
644 defval (0);
645 #endif
646
647 case SLC_AO:
648 #ifdef termdesc_ao
649 setval (termdesc_ao, SLC_VARIABLE | SLC_FLUSHOUT);
650 #else
651 defval (0);
652 #endif
653
654 case SLC_SUSP:
655 #ifdef termdesc_susp
656 setval (termdesc_susp, SLC_VARIABLE | SLC_FLUSHIN);
657 #else
658 defval (0);
659 #endif
660
661 #ifdef termdesc_forw1
662 case SLC_FORW1:
663 setval (termdesc_forw1, SLC_VARIABLE);
664 #endif
665
666 #ifdef termdesc_forw2
667 case SLC_FORW2:
668 setval (termdesc_forw2, SLC_VARIABLE);
669 #endif
670
671 case SLC_AYT:
672 #ifdef termdesc_status
673 setval (termdesc_status, SLC_VARIABLE);
674 #else
675 defval (0);
676 #endif
677
678 case SLC_BRK:
679 case SLC_SYNCH:
680 case SLC_EOR:
681 defval (0);
682
683 default:
684 *valp = 0;
685 *valpp = 0;
686 return SLC_NOSUPPORT;
687 }
688 }
689
690 #if B4800 != 4800
691 # define DECODE_BAUD
692 #endif
693
694 #ifdef DECODE_BAUD
695
696 /*
697 * A table of available terminal speeds
698 */
699 struct termspeeds
700 {
701 int speed;
702 int value;
703 } termspeeds[] = {
704 {0, B0},
705 {50, B50},
706 {75, B75},
707 {110, B110},
708 {134, B134},
709 {150, B150},
710 {200, B200},
711 {300, B300},
712 {600, B600},
713 {1200, B1200},
714 {1800, B1800},
715 {2400, B2400},
716 {4800, B4800},
717 # ifdef B7200
718 {7200, B7200},
719 # endif
720 {9600, B9600},
721 # ifdef B14400
722 {14400, B14400},
723 # endif
724 # ifdef B19200
725 {19200, B19200},
726 # endif
727 # ifdef B28800
728 {28800, B28800},
729 # endif
730 # ifdef B38400
731 {38400, B38400},
732 # endif
733 # ifdef B57600
734 {57600, B57600},
735 # endif
736 # ifdef B115200
737 {115200, B115200},
738 # endif
739 # ifdef B230400
740 {230400, B230400},
741 # endif
742 {-1, 0}
743 };
744 #endif /* DECODE_BAUD */
745
746 void
747 tty_tspeed (int val)
748 {
749 #ifdef DECODE_BAUD
750 struct termspeeds *tp;
751
752 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
753 ;
754 if (tp->speed == -1) /* back up to last valid value */
755 --tp;
756 val = tp->value;
757 #endif
758 cfsetospeed (&termbuf, val);
759 }
760
761 void
762 tty_rspeed (int val)
763 {
764 #ifdef DECODE_BAUD
765 struct termspeeds *tp;
766
767 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
768 ;
769 if (tp->speed == -1) /* back up to last valid value */
770 --tp;
771 val = tp->value;
772 #endif
773 cfsetispeed (&termbuf, val);
774 }