2 * bsd_ioctl.c - Deal with 2.11BSD ioctl system calls.
4 * $Revision: 1.13 $ $Date: 1999/12/27 04:38:29 $
12 #include <linux/sockios.h> /* FIOSETOWN */
15 /* Structures and defines required by this file */
17 /* First the list of ioctls handled so far */
18 #define TR_FIOCLEX 0x20006601 /* 0x2 is void */
19 #define TR_TIOCGETP 0x40067408 /* 0x4 is a read */
20 #define TR_TIOCSETP 0x40067409 /* 0x8 is a write */
21 #define TR_TIOCSETN 0x8006740a
22 #define TR_TIOCSETC 0x80067411
23 #define TR_TIOCGETD 0x40027400
24 #define TR_TIOCSETD 0x80027401
25 #define TR_TIOCGETC 0x40067412
26 #define TR_TIOCGLTC 0x40067474
27 #define TR_TIOCSLTC 0x80067475
28 #define TR_TIOCGWINSZ 0x40087468
29 #define TR_TIOCSWINSZ 0x40027467
30 #define TR_FIOSETOWN 0x8002667b
31 #define TR_TIOCMGET 0x4002746a
32 #define TR_TIOCGPGRP 0x40027477
33 #define TR_TIOCSPGRP 0x80027476
37 int8_t sg_ispeed; /* input speed */
38 int8_t sg_ospeed; /* output speed */
39 int8_t sg_erase; /* erase character */
40 int8_t sg_kill; /* kill character */
41 int16_t sg_flags; /* mode flags */
45 int8_t t_intrc; /* interrupt */
46 int8_t t_quitc; /* quit */
47 int8_t t_startc; /* start output */
48 int8_t t_stopc; /* stop output */
49 int8_t t_eofc; /* end-of-file */
50 int8_t t_brkc; /* input delimiter (like nl) */
54 int8_t t_suspc; /* stop process signal */
55 int8_t t_dsuspc; /* delayed stop process signal */
56 int8_t t_rprntc; /* reprint line */
57 int8_t t_flushc; /* flush output (toggles) */
58 int8_t t_werasc; /* word erase */
59 int8_t t_lnextc; /* literal next character */
65 #define TR_TANDEM 0x00000001 /* send stopc on out q full */
66 #define TR_CBREAK 0x00000002 /* half-cooked mode */
68 #define TR_ECHO 0x00000008 /* echo input */
69 #define TR_CRMOD 0x00000010 /* map \r to \r\n on output */
70 #define TR_RAW 0x00000020 /* no i/o processing */
71 #define TR_ODDP 0x00000040 /* get/send odd parity */
72 #define TR_EVENP 0x00000080 /* get/send even parity */
73 #define TR_ANYP 0x000000c0 /* get any parity/send none */
74 /* 0x100 (old NLDELAY) */
76 #define TR_XTABS 0x00000400 /* expand tabs on output */
78 /* Values for sg_ispeed and sg_ospeed */
98 /* Variables, functions and the actual code of this file */
100 extern arglist *A; /* Pointer to various arguments on stack */
102 /* Forward prototypes */
109 static int trap_gettermios(u_int16_t fd, u_int32_t type, u_int16_t ucnt);
110 static int trap_settermios(u_int16_t fd, u_int32_t type, u_int16_t ucnt);
119 u_int8_t size, letter, value;
122 struct winsize *winptr;
124 larg1 = (sarg2 << 16) | uarg3;
128 fprintf(dbg_file, "val %d ", uarg1);
131 fprintf(dbg_file, "FIOCLEX ");
134 fprintf(dbg_file, "TIOCGETP ");
137 fprintf(dbg_file, "TIOCSETP ");
140 fprintf(dbg_file, "TIOCSETN ");
143 fprintf(dbg_file, "TIOCSETC ");
146 fprintf(dbg_file, "TIOCGETD ");
149 fprintf(dbg_file, "TIOCSETD ");
152 fprintf(dbg_file, "TIOCGETC ");
155 fprintf(dbg_file, "TIOCGLTC ");
158 fprintf(dbg_file, "TIOCSLTC ");
161 fprintf(dbg_file, "TIOCGWINSZ ");
164 fprintf(dbg_file, "TIOCSWINSZ ");
167 fprintf(dbg_file, "FIOSETOWN ");
170 fprintf(dbg_file, "TIOCMGET ");
173 fprintf(dbg_file, "TIOCGPGRP ");
176 fprintf(dbg_file, "TIOCSPGRP ");
179 fprintf(dbg_file, "0x%lx ", larg1);
181 value = larg1 & 0xff;
182 letter = (larg1 >> 8) & 0xff;
183 size = (larg1 >> 16) & 0xff;
185 fprintf(dbg_file, "('%c' ", letter);
186 fprintf(dbg_file, "size %d ", size);
187 fprintf(dbg_file, "val %d) ", value);
190 fprintf(dbg_file, "ptr %d ", uarg4);
199 i = trap_gettermios(uarg1, larg1, uarg4);
206 i = trap_settermios(uarg1, larg1, uarg4);
210 i = ioctl(uarg1, FIOCLEX, NULL);
213 shortptr = (int16_t *) & dspace[uarg4];
214 i = ioctl(uarg1, TIOCGETD, &val);
219 shortptr = (int16_t *) & dspace[uarg4];
220 i = ioctl(uarg1, TIOCGPGRP, &val);
225 shortptr = (int16_t *) & dspace[uarg4];
227 i = ioctl(uarg1, TIOCSPGRP, &val);
230 shortptr = (int16_t *) & dspace[uarg4];
232 i = ioctl(uarg1, TIOCSETD, &val);
235 shortptr = (int16_t *) & dspace[uarg4];
237 /* Something wrong here, wonder why! */
238 TrapDebug((dbg_file, "fd %d val %d ", uarg1, val));
239 i = ioctl(uarg1, FIOSETOWN, &val);
242 shortptr = (int16_t *) & dspace[uarg4];
243 i = ioctl(uarg1, TIOCMGET, &val);
249 /* 2.11BSD and POSIX winsize the same! */
250 winptr = (struct winsize *) &dspace[uarg4];
251 i = ioctl(uarg1, TIOCGWINSZ, winptr);
254 winptr = (struct winsize *) &dspace[uarg4];
255 i = ioctl(uarg1, TIOCSWINSZ, winptr);
265 /* Convert from termios to old sgttyb structure */
266 static void to_sgttyb(struct tr_sgttyb *sgtb, struct termios *tios)
268 switch (tios->c_ispeed) {
270 sgtb->sg_ispeed = TR_B0;
273 sgtb->sg_ispeed = TR_B50;
276 sgtb->sg_ispeed = TR_B75;
279 sgtb->sg_ispeed = TR_B110;
282 sgtb->sg_ispeed = TR_B134;
285 sgtb->sg_ispeed = TR_B150;
288 sgtb->sg_ispeed = TR_B200;
291 sgtb->sg_ispeed = TR_B300;
294 sgtb->sg_ispeed = TR_B600;
297 sgtb->sg_ispeed = TR_B1200;
300 sgtb->sg_ispeed = TR_B1800;
303 sgtb->sg_ispeed = TR_B2400;
306 sgtb->sg_ispeed = TR_B4800;
309 sgtb->sg_ispeed = TR_B9600;
312 sgtb->sg_ispeed = TR_EXTA;
315 sgtb->sg_ispeed = TR_EXTB;
318 sgtb->sg_ispeed = TR_B0;
321 switch (tios->c_ospeed) {
323 sgtb->sg_ospeed = TR_B0;
326 sgtb->sg_ospeed = TR_B50;
329 sgtb->sg_ospeed = TR_B75;
332 sgtb->sg_ospeed = TR_B110;
335 sgtb->sg_ospeed = TR_B134;
338 sgtb->sg_ospeed = TR_B150;
341 sgtb->sg_ospeed = TR_B200;
344 sgtb->sg_ospeed = TR_B300;
347 sgtb->sg_ospeed = TR_B600;
350 sgtb->sg_ospeed = TR_B1200;
353 sgtb->sg_ospeed = TR_B1800;
356 sgtb->sg_ospeed = TR_B2400;
359 sgtb->sg_ospeed = TR_B4800;
362 sgtb->sg_ospeed = TR_B9600;
365 sgtb->sg_ospeed = TR_EXTA;
368 sgtb->sg_ospeed = TR_EXTB;
371 sgtb->sg_ospeed = TR_B0;
374 sgtb->sg_erase = tios->c_cc[VERASE];
375 sgtb->sg_kill = tios->c_cc[VKILL];
377 if (tios->c_oflag & OXTABS)
378 sgtb->sg_flags |= TR_XTABS;
379 if (tios->c_cflag & PARENB) {
380 if (tios->c_cflag & PARODD)
381 sgtb->sg_flags |= TR_ODDP;
383 sgtb->sg_flags |= TR_EVENP;
385 sgtb->sg_flags |= TR_ANYP;
386 if (tios->c_oflag & ONLCR)
387 sgtb->sg_flags |= TR_CRMOD;
388 if (tios->c_lflag & ECHO)
389 sgtb->sg_flags |= TR_ECHO;
390 if (!(tios->c_lflag & ICANON)) {
391 if (!(tios->c_lflag & ECHO))
392 sgtb->sg_flags |= TR_CBREAK;
394 sgtb->sg_flags |= TR_RAW;
398 /* Convert from old sgttyb to termios structure */
399 static void to_termios(struct tr_sgttyb *sgtb, struct termios *tios)
401 TrapDebug((dbg_file, "\n\tto_termios: sgtty flags are 0x%x ",
404 switch (sgtb->sg_ispeed) {
409 tios->c_ispeed = B50;
412 tios->c_ispeed = B75;
415 tios->c_ispeed = B110;
418 tios->c_ispeed = B134;
421 tios->c_ispeed = B150;
424 tios->c_ispeed = B200;
427 tios->c_ispeed = B300;
430 tios->c_ispeed = B600;
433 tios->c_ispeed = B1200;
436 tios->c_ispeed = B1800;
439 tios->c_ispeed = B2400;
442 tios->c_ispeed = B4800;
445 tios->c_ispeed = B9600;
448 tios->c_ispeed = B19200;
451 tios->c_ispeed = B38400;
457 switch (sgtb->sg_ospeed) {
462 tios->c_ospeed = B50;
465 tios->c_ospeed = B75;
468 tios->c_ospeed = B110;
471 tios->c_ospeed = B134;
474 tios->c_ospeed = B150;
477 tios->c_ospeed = B200;
480 tios->c_ospeed = B300;
483 tios->c_ospeed = B600;
486 tios->c_ospeed = B1200;
489 tios->c_ospeed = B1800;
492 tios->c_ospeed = B2400;
495 tios->c_ospeed = B4800;
498 tios->c_ospeed = B9600;
501 tios->c_ospeed = B19200;
504 tios->c_ospeed = B38400;
510 tios->c_cc[VERASE] = sgtb->sg_erase;
511 tios->c_cc[VKILL] = sgtb->sg_kill;
513 /* Initially turn off any flags we might set */
514 tios->c_oflag &= ~(OXTABS | ONLCR);
515 tios->c_cflag &= ~(PARENB | PARODD);
516 tios->c_lflag &= ~(ECHO);
518 if (sgtb->sg_flags & TR_XTABS)
519 tios->c_oflag |= OXTABS;
520 if (sgtb->sg_flags & TR_ODDP) {
521 tios->c_cflag |= PARENB;
522 tios->c_cflag &= ~PARODD;
524 if (sgtb->sg_flags & TR_EVENP)
525 tios->c_cflag |= PARENB | PARODD;
526 if (sgtb->sg_flags & TR_ANYP)
527 tios->c_cflag &= ~PARENB;
528 if (sgtb->sg_flags & TR_CRMOD)
529 tios->c_oflag |= ONLCR;
530 if (sgtb->sg_flags & TR_ECHO)
531 tios->c_lflag |= ECHO;
533 if (sgtb->sg_flags & TR_RAW) {
535 ~(ECHO | ICANON | IEXTEN | ISIG | BRKINT | ICRNL | INPCK |
537 tios->c_cflag &= ~(CSIZE | PARENB);
538 tios->c_cflag |= CS8;
539 tios->c_oflag &= ~(OPOST);
540 tios->c_cc[VMIN] = 1;
541 tios->c_cc[VTIME] = 0;
544 if (sgtb->sg_flags & TR_CBREAK) {
545 TrapDebug((dbg_file, "\n\tto_termios: setting cbreak I hope "));
546 tios->c_lflag &= ~(ECHO | ICANON);
547 tios->c_cc[VMIN] = 1;
548 tios->c_cc[VTIME] = 0;
550 TrapDebug((dbg_file, "\n\tto_termios: iflag is 0x%x",
551 (int) tios->c_iflag));
552 TrapDebug((dbg_file, "\n\tto_termios: oflag is 0x%x",
553 (int) tios->c_oflag));
554 TrapDebug((dbg_file, "\n\tto_termios: lflag is 0x%x ",
555 (int) tios->c_lflag));
558 /* Convert from termios to old [l]tchars structures */
559 static void to_tchars(struct tr_tchars *tc, struct tr_ltchars *ltc,
560 struct termios *tios)
563 tc->t_intrc = tios->c_cc[VINTR];
564 tc->t_quitc = tios->c_cc[VQUIT];
565 tc->t_startc = tios->c_cc[VSTART];
566 tc->t_stopc = tios->c_cc[VSTOP];
567 tc->t_eofc = tios->c_cc[VEOF];
568 tc->t_brkc = tios->c_cc[VEOL];
571 ltc->t_suspc = tios->c_cc[VSUSP];
572 ltc->t_dsuspc = tios->c_cc[VDSUSP];
573 ltc->t_rprntc = tios->c_cc[VREPRINT];
574 ltc->t_flushc = tios->c_cc[VDISCARD];
575 ltc->t_werasc = tios->c_cc[VERASE];
576 ltc->t_lnextc = tios->c_cc[VLNEXT];
580 /* Convert from old [l]tchars to termios structures */
581 static void tc_to_tchars(struct tr_tchars *tc, struct tr_ltchars *ltc,
582 struct termios *tios)
585 tios->c_cc[VINTR] = tc->t_intrc;
586 tios->c_cc[VQUIT] = tc->t_quitc;
587 tios->c_cc[VSTART] = tc->t_startc;
588 tios->c_cc[VSTOP] = tc->t_stopc;
589 tios->c_cc[VEOF] = tc->t_eofc;
590 tios->c_cc[VEOL] = tc->t_brkc;
593 tios->c_cc[VSUSP] = ltc->t_suspc;
594 tios->c_cc[VDSUSP] = ltc->t_dsuspc;
595 tios->c_cc[VREPRINT] = ltc->t_rprntc;
596 tios->c_cc[VDISCARD] = ltc->t_flushc;
597 tios->c_cc[VERASE] = ltc->t_werasc;
598 tios->c_cc[VLNEXT] = ltc->t_lnextc;
602 /* Handle most get ioctls that deal with termios stuff */
603 static int trap_gettermios(u_int16_t fd, u_int32_t type, u_int16_t ucnt)
606 struct tr_sgttyb *sgtb;
607 struct tr_tchars *tc;
608 struct tr_ltchars *ltc;
613 i = tcgetattr(fd, &tios);
620 sgtb = (struct tr_sgttyb *) &dspace[ucnt];
621 to_sgttyb(sgtb, &tios);
624 tc = (struct tr_tchars *) &dspace[ucnt];
625 to_tchars(tc, NULL, &tios);
628 ltc = (struct tr_ltchars *) &dspace[ucnt];
629 to_tchars(NULL, ltc, &tios);
632 /* Unknown type, should never get here */
636 /* Handle most set ioctls that deal with termios stuff */
637 static int trap_settermios(u_int16_t fd, u_int32_t type, u_int16_t ucnt)
640 struct tr_sgttyb *sgtb;
641 struct tr_tchars *tc;
642 struct tr_ltchars *ltc;
647 i = tcgetattr(fd, &tios);
652 sgtb = (struct tr_sgttyb *) &dspace[ucnt];
653 to_termios(sgtb, &tios);
654 i = tcsetattr(fd, TCSANOW, &tios);
657 sgtb = (struct tr_sgttyb *) &dspace[ucnt];
658 to_termios(sgtb, &tios);
659 i = tcsetattr(fd, TCSADRAIN, &tios);
662 tc = (struct tr_tchars *) &dspace[ucnt];
663 tc_to_tchars(tc, NULL, &tios);
664 i = tcsetattr(fd, TCSANOW, &tios);
667 ltc = (struct tr_ltchars *) &dspace[ucnt];
668 tc_to_tchars(NULL, ltc, &tios);
669 i = tcsetattr(fd, TCSANOW, &tios);
672 /* Unknown type, should never get here */