home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / __tioctl.c < prev    next >
C/C++ Source or Header  |  1996-10-01  |  7KB  |  300 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __tioctl.c,v 1.1.1.1 1994/04/04 04:30:13 amiga Exp
  20.  *
  21.  *  __tioctl.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:13  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.5  1993/11/05  21:51:08  mw
  26.  *  seems I got oldstyle tty handling oposite way..
  27.  *
  28.  *  Revision 1.4  1992/08/09  20:39:01  amiga
  29.  *  add a cast to get rid of a warning
  30.  *
  31.  *  Revision 1.3  1992/07/04  19:07:25  mwild
  32.  *  send DISK_INFO packet with synchronous port, async delivery seems to be
  33.  *  broken with CNC:
  34.  *
  35.  * Revision 1.2  1992/06/08  02:36:00  mwild
  36.  * fix TIOCGWINSZ, row/column was off by one
  37.  *
  38.  * Revision 1.1  1992/05/14  19:55:40  mwild
  39.  * Initial revision
  40.  *
  41.  */
  42.  
  43. #define _KERNEL
  44. #include "ixemul.h"
  45. #include "kprintf.h"
  46. #include <string.h>
  47. #include <sgtty.h>
  48.  
  49. #define OEXTB 15
  50. #undef B0    
  51. #undef B50    
  52. #undef B75    
  53. #undef B110    
  54. #undef B134    
  55. #undef B150    
  56. #undef B200    
  57. #undef B300    
  58. #undef B600    
  59. #undef B1200    
  60. #undef B1800    
  61. #undef B2400    
  62. #undef B4800    
  63. #undef B9600    
  64. #undef EXTA    
  65. #undef EXTB    
  66. #include <sys/termios.h>
  67. #include <ctype.h>
  68. #include <devices/conunit.h>
  69. #include <intuition/intuition.h>
  70.  
  71. /* IOCTLs on "interactive" files */
  72.  
  73. int
  74. __tioctl(struct file *f, unsigned int cmd, unsigned int inout, 
  75.      unsigned int arglen, unsigned int arg)
  76. {
  77.   int omask, result, err = 0;
  78.  
  79.   omask = syscall (SYS_sigsetmask, ~0);
  80.   __get_file (f);
  81.   result = -1;
  82.  
  83.   if (!f->f_fh->fh_Port)
  84.     {
  85.       err = ENOTTY;
  86.     }
  87.   else switch (cmd)
  88.     {
  89.     case TIOCGETA:
  90.       {
  91.         struct termios *t = (struct termios *)arg;
  92.         unsigned char *cp;
  93.  
  94.         t->c_iflag = IGNBRK | IGNPAR | IXON;
  95.     if (f->f_ttyflags & IXTTY_ICRNL)
  96.       t->c_iflag |= ICRNL;
  97.     if (f->f_ttyflags & IXTTY_INLCR)
  98.       t->c_iflag |= INLCR;
  99.         t->c_oflag = 0;
  100.     if (f->f_ttyflags & IXTTY_OPOST)
  101.       t->c_oflag |= OPOST;
  102.     if (f->f_ttyflags & IXTTY_ONLCR)
  103.       t->c_oflag |= ONLCR;
  104.         t->c_cflag = CS8|CLOCAL;
  105.         t->c_ispeed=
  106.         t->c_ospeed= EXTB;
  107.     /* Conman does ECHOCTL, Commo doesn't.. I use Conman:-)) */
  108.         t->c_lflag = ECHOCTL | ((f->f_ttyflags & IXTTY_RAW) ? 0 : ICANON | ECHO);
  109.     cp = t->c_cc;
  110.     cp[VSTART] = 'q' & 31;
  111.     cp[VSTOP] = 's' & 31;
  112.     cp[VSUSP] = 0; /* sneef.. would that be nice... */
  113.     cp[VDSUSP] = 0;
  114.     cp[VREPRINT] = 0;
  115.     cp[VDISCARD] = 'x' & 31;
  116.     cp[VWERASE] = 0;
  117.     cp[VLNEXT] = 0;
  118.     cp[VSTATUS] = 0;
  119.     cp[VINTR] = 3;
  120.     cp[VQUIT] = 0;
  121.     cp[VERASE] = 8;
  122.     cp[VKILL] = 'x' & 31;
  123.     cp[VEOF] = '\\' & 31;
  124.     cp[VEOL] = 10;
  125.     cp[VEOL2] = 0;
  126.     result = 0;
  127.     break;
  128.       }
  129.  
  130.     case TIOCSETA:
  131.     case TIOCSETAW:
  132.     case TIOCSETAF:
  133.       {
  134.         struct termios *t = (struct termios *)arg;
  135.         int makeraw;
  136.         
  137.         makeraw = (t->c_lflag & (ICANON | ECHO)) != (ICANON | ECHO);
  138.     /* the only thing that counts so far.. if ICANON is disabled,        
  139.      * we disable ECHO too, no matter what the user wanted, and 
  140.      * send a RAW-packet.. */
  141.     if (!makeraw && (f->f_ttyflags & IXTTY_RAW))
  142.           {
  143.         f->f_ttyflags &= ~IXTTY_RAW;
  144.         SendPacket1(f, __srwport, ACTION_SCREEN_MODE, 0);
  145.         __wait_sync_packet(&f->f_sp);
  146.       }
  147.     else if (makeraw && !(f->f_ttyflags & IXTTY_RAW))
  148.       {
  149.         f->f_ttyflags |= IXTTY_RAW;
  150.         SendPacket1(f, __srwport, ACTION_SCREEN_MODE, -1);
  151.         __wait_sync_packet(&f->f_sp);
  152.       }
  153.     if (t->c_iflag & ICRNL)
  154.       f->f_ttyflags |= IXTTY_ICRNL;
  155.     else
  156.       f->f_ttyflags &= ~IXTTY_ICRNL;
  157.     if (t->c_iflag & INLCR)
  158.       f->f_ttyflags |= IXTTY_INLCR;
  159.     else
  160.       f->f_ttyflags &= ~IXTTY_INLCR;
  161.     result = 0;
  162.     break;
  163.       }
  164.  
  165.     case TIOCGETP:
  166.       {
  167.     struct sgttyb *s = (struct sgttyb *)arg;
  168.     s->sg_erase = 8;
  169.     s->sg_kill = 'x' & 31;
  170.     s->sg_flags = ODDP|EVENP|ANYP|
  171.               ((f->f_ttyflags & IXTTY_RAW) ? (CBREAK|RAW) : (ECHO|CRMOD));
  172.     s->sg_ispeed =
  173.     s->sg_ospeed = OEXTB;
  174.     result = 0;
  175.     break;
  176.       }
  177.  
  178.     case TIOCSETN:
  179.     case TIOCSETP:
  180.       {
  181.     struct sgttyb *s = (struct sgttyb *)arg;
  182.  
  183.     if (!(s->sg_flags & (RAW|CBREAK)))
  184.           {
  185.             f->f_ttyflags &= ~IXTTY_RAW;
  186.         SendPacket1(f, __srwport, ACTION_SCREEN_MODE, 0);
  187.         __wait_sync_packet(&f->f_sp);
  188.       }
  189.         else if ((s->sg_flags & (RAW|CBREAK)))
  190.       {
  191.         f->f_ttyflags |= IXTTY_RAW;
  192.         SendPacket1(f, __srwport, ACTION_SCREEN_MODE, -1);
  193.         __wait_sync_packet(&f->f_sp);
  194.       }
  195.     result = 0;
  196.     break;
  197.       }
  198.  
  199.     case TIOCGWINSZ:
  200.       {
  201.     struct winsize *ws = (struct winsize *) arg;
  202.     struct ConUnit *cu;
  203.     struct IOStdReq *ios;
  204.     struct InfoData *info;
  205.     struct Window *w;
  206.  
  207.     err = EINVAL;    /* default */
  208.     
  209.     info = alloca (sizeof (struct InfoData) + 2);
  210.     info = LONG_ALIGN (info);
  211.     bzero (info, sizeof (struct InfoData));
  212.     
  213.     LastResult (f) = 0; LastError (f) = 0;
  214.     SendPacket1 (f, __srwport, ACTION_DISK_INFO, CTOBPTR (info));
  215.     __wait_sync_packet (&f->f_sp);
  216.     if (LastResult (f) != -1)
  217.           break;
  218.  
  219.     w = (struct Window *) info->id_VolumeNode;
  220.     if (! w) 
  221.       break;
  222.     /* this information is from DevCon notes, not from the Bantam book */
  223.     ios = (struct IOStdReq *) info->id_InUse;
  224.     if (! ios || ((int)ios & 1)) 
  225.       break;
  226.     cu = (struct ConUnit *) ios->io_Unit;
  227.     if (!cu) 
  228.       break;
  229.  
  230.     /* paranoid check.. */
  231.     if (cu->cu_Window != w)
  232.       break;
  233.  
  234.         ws->ws_xpixel = w->Width - w->BorderLeft - w->BorderRight;
  235.         ws->ws_ypixel = w->Height - w->BorderTop - w->BorderBottom;
  236.  
  237.     ws->ws_col = cu->cu_XMax + 1;    /* Thanks Rob! those values are off */
  238.     ws->ws_row = cu->cu_YMax + 1;    /* by one! */
  239.  
  240.     result = 0;
  241.     break;
  242.       }
  243.  
  244.     case TIOCSPGRP:
  245.       {
  246.     int *pgrp = (int *)arg;
  247.  
  248.         if (u.u_session)
  249.           u.u_session->pgrp = *pgrp;
  250.     result = 0;
  251.         break;
  252.       }
  253.  
  254.     case TIOCGPGRP:
  255.       {
  256.     int *pgrp = (int *)arg;
  257.  
  258.         *pgrp = (u.u_session ? u.u_session->pgrp : syscall(SYS_getpgrp));
  259.     result = 0;
  260.         break;
  261.       }
  262.  
  263.     case TIOCOUTQ:
  264.       {
  265.     int *count = (int *)arg;
  266.  
  267.     *count = 0;
  268.     result = 0;
  269.     break;
  270.       }
  271.  
  272.     case TIOCPKT:
  273.       {
  274.         int *on = (int *)arg;
  275.  
  276.     if (*on)
  277.             f->f_ttyflags |= IXTTY_PKT;
  278.     else
  279.             f->f_ttyflags &= ~IXTTY_PKT;
  280.     result = 0;
  281.         break;
  282.       }
  283.  
  284.     case TIOCSWINSZ:
  285.       /* should I really try to resize the window ?? */
  286.  
  287.     default:
  288.       /* this is no error, but nevertheless we don't take any actions.. */      
  289.       result = 0;
  290.       break;
  291.     }
  292.  
  293.     LastResult(f) = 0;
  294.     __release_file (f);
  295.     syscall (SYS_sigsetmask, omask);
  296.     errno = err;
  297.     KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  298.     return result;
  299. }
  300.