| 1 | /* You may distribute under the terms of either the GNU General Public License |
| 2 | * or the Artistic License (the same terms as Perl itself) |
| 3 | * |
| 4 | * (C) Paul Evans, 2016 -- [email protected] |
| 5 | */ |
| 6 | |
| 7 | #include "EXTERN.h" |
| 8 | #include "perl.h" |
| 9 | #include "XSUB.h" |
| 10 | |
| 11 | /* Until GNU libc supports termios2, we'll have to go the direct approach |
| 12 | * |
| 13 | * Because of this there's some extra mess we have to account for. Namely, |
| 14 | * that <asm/termios.h> wants to provide two structs already known by this |
| 15 | * point (because "perl.h" brought them in). gcc will complain. But since we |
| 16 | * don't care about the new definitions of these new structs, we'll just throw |
| 17 | * them away. |
| 18 | */ |
| 19 | |
| 20 | #define winsize ASM_TERMIOS_winsize |
| 21 | #define termio ASM_TERMIOS_termio |
| 22 | # include <asm/termios.h> |
| 23 | # include <asm/ioctls.h> |
| 24 | #undef winsize |
| 25 | #undef termio |
| 26 | |
| 27 | typedef struct termios2 *Linux__Termios2; |
| 28 | |
| 29 | MODULE = Linux::Termios2 PACKAGE = Linux::Termios2 |
| 30 | |
| 31 | Linux::Termios2 |
| 32 | new(package) |
| 33 | char *package |
| 34 | CODE: |
| 35 | Newx(RETVAL, 1, struct termios2); |
| 36 | OUTPUT: |
| 37 | RETVAL |
| 38 | |
| 39 | void |
| 40 | DESTROY(self) |
| 41 | Linux::Termios2 self |
| 42 | CODE: |
| 43 | Safefree(self); |
| 44 | |
| 45 | void |
| 46 | getattr(self, fd) |
| 47 | Linux::Termios2 self |
| 48 | int fd |
| 49 | PPCODE: |
| 50 | if(ioctl(fd, TCGETS2, self)) |
| 51 | XSRETURN_UNDEF; |
| 52 | |
| 53 | ST(0) = &PL_sv_yes; |
| 54 | XSRETURN(1); |
| 55 | |
| 56 | void |
| 57 | setattr(self, fd, act) |
| 58 | Linux::Termios2 self |
| 59 | int fd |
| 60 | int act |
| 61 | PPCODE: |
| 62 | { |
| 63 | int ctl; |
| 64 | switch(act) { |
| 65 | case TCSANOW: ctl = TCSETS2; break; |
| 66 | case TCSADRAIN: ctl = TCSETSW2; break; |
| 67 | case TCSAFLUSH: ctl = TCSETSF2; break; |
| 68 | default: |
| 69 | SETERRNO(EINVAL, LIB_INVARG); |
| 70 | XSRETURN_UNDEF; |
| 71 | } |
| 72 | if(ioctl(fd, ctl, self)) |
| 73 | XSRETURN_UNDEF; |
| 74 | |
| 75 | ST(0) = &PL_sv_yes; |
| 76 | XSRETURN(1); |
| 77 | } |
| 78 | |
| 79 | int |
| 80 | getcflag(self) |
| 81 | Linux::Termios2 self |
| 82 | ALIAS: |
| 83 | getcflag = 0 |
| 84 | getiflag = 1 |
| 85 | getlflag = 2 |
| 86 | getoflag = 3 |
| 87 | getispeed = 4 |
| 88 | getospeed = 5 |
| 89 | CODE: |
| 90 | switch(ix) { |
| 91 | case 0: RETVAL = self->c_cflag; break; |
| 92 | case 1: RETVAL = self->c_iflag; break; |
| 93 | case 2: RETVAL = self->c_lflag; break; |
| 94 | case 3: RETVAL = self->c_oflag; break; |
| 95 | case 4: RETVAL = self->c_ispeed; break; |
| 96 | case 5: RETVAL = self->c_ospeed; break; |
| 97 | } |
| 98 | OUTPUT: |
| 99 | RETVAL |
| 100 | |
| 101 | void |
| 102 | setcflag(self, val) |
| 103 | Linux::Termios2 self |
| 104 | int val |
| 105 | ALIAS: |
| 106 | setcflag = 0 |
| 107 | setiflag = 1 |
| 108 | setlflag = 2 |
| 109 | setoflag = 3 |
| 110 | CODE: |
| 111 | switch(ix) { |
| 112 | case 0: self->c_cflag = val; break; |
| 113 | case 1: self->c_iflag = val; break; |
| 114 | case 2: self->c_lflag = val; break; |
| 115 | case 3: self->c_oflag = val; break; |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | setispeed(self, val) |
| 120 | Linux::Termios2 self |
| 121 | int val |
| 122 | ALIAS: |
| 123 | setispeed = 0 |
| 124 | setospeed = 1 |
| 125 | CODE: |
| 126 | switch(ix) { |
| 127 | case 0: self->c_ispeed = val; break; |
| 128 | case 1: self->c_ospeed = val; break; |
| 129 | } |
| 130 | self->c_cflag &= ~CBAUD; |
| 131 | self->c_cflag |= BOTHER; |