URI:
       tbsdpty.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tbsdpty.c (2110B)
       ---
            1 #include <u.h>
            2 #include <sys/types.h>
            3 #include <sys/ioctl.h>
            4 #include <sys/stat.h>
            5 #include <errno.h>
            6 #include <grp.h>
            7 #include <termios.h>
            8 #ifdef HAS_SYS_TERMIOS
            9 #include <sys/termios.h>
           10 #endif
           11 #ifdef __linux__
           12 #include <pty.h>
           13 #endif
           14 #include <fcntl.h>
           15 #include <libc.h>
           16 #include <draw.h>
           17 #include "term.h"
           18 
           19 #define debug 0
           20 
           21 static char *abc =
           22         "abcdefghijklmnopqrstuvwxyz"
           23         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           24         "0123456789";
           25 static char *_123 =
           26         "0123456789"
           27         "abcdefghijklmnopqrstuvwxyz"
           28         "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
           29 
           30 int
           31 getpts(int fd[], char *slave)
           32 {
           33         char *a, *z;
           34         char pty[] = "/dev/ptyXX";
           35 
           36         for(a=abc; *a; a++)
           37         for(z=_123; *z; z++){
           38                 pty[8] = *a;
           39                 pty[9] = *z;
           40                 if((fd[1] = open(pty, ORDWR)) < 0){
           41                         if(errno == ENOENT)
           42                                 break;
           43                 }else{
           44                         fchmod(fd[1], 0620);
           45                         strcpy(slave, pty);
           46                         slave[5] = 't';
           47                         if((fd[0] = open(slave, ORDWR)) >= 0)
           48                                 return 0;
           49                         close(fd[1]);
           50                 }
           51         }
           52         sysfatal("no ptys");
           53         return 0;
           54 }
           55 
           56 int
           57 childpty(int fd[], char *slave)
           58 {
           59         int sfd;
           60 
           61         close(fd[1]);        /* drop master */
           62         setsid();
           63         sfd = open(slave, ORDWR);
           64         if(sfd < 0)
           65                 sysfatal("child open %s: %r\n", slave);
           66 #if !defined (__AIX__)
           67         if(ioctl(sfd, TIOCSCTTY, 0) < 0)
           68                 fprint(2, "ioctl TIOCSCTTY: %r\n");
           69 #endif
           70         return sfd;
           71 }
           72 
           73 struct winsize ows;
           74 
           75 void
           76 updatewinsize(int row, int col, int dx, int dy)
           77 {
           78         struct winsize ws;
           79 
           80         ws.ws_row = row;
           81         ws.ws_col = col;
           82         ws.ws_xpixel = dx;
           83 
           84         needdisplay(); // in case this is 'win' and not 9term
           85         // Leave "is this a hidpi display" in the low bit of the ypixel height for mc.
           86         dy &= ~1;
           87         if(display != nil && display->dpi >= DefaultDPI*3/2)
           88                 dy |= 1;
           89         ws.ws_ypixel = dy;
           90 
           91         if(ws.ws_row != ows.ws_row || ws.ws_col != ows.ws_col){
           92                 if(ioctl(rcfd, TIOCSWINSZ, &ws) < 0)
           93                         fprint(2, "ioctl: %r\n");
           94         }
           95         ows = ws;
           96 }
           97 
           98 static struct termios ttmode;
           99 
          100 int
          101 isecho(int fd)
          102 {
          103         if(tcgetattr(fd, &ttmode) < 0)
          104                 fprint(2, "tcgetattr: %r\n");
          105         if(debug) fprint(2, "israw %c%c\n",
          106                 ttmode.c_lflag&ICANON ? 'c' : '-',
          107                 ttmode.c_lflag&ECHO ? 'e' : '-');
          108         return ttmode.c_lflag&ECHO;
          109 }
          110 
          111 int
          112 getintr(int fd)
          113 {
          114         if(tcgetattr(fd, &ttmode) < 0)
          115                 return 0x7F;
          116         return ttmode.c_cc[VINTR];
          117 }