| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| #include <sys/ioctl.h> |
| 8 |
|
| #include <sys/stat.h> |
| 9 |
|
| #if defined(__DARWIN__) || defined(__FreeBSD__) || defined(__OpenBSD__) |
| 10 |
|
| #include <sys/ttycom.h> |
| 11 |
|
| #endif |
| 12 |
|
| |
| 13 |
|
| #include <fcntl.h> |
| 14 |
|
| #include <limits.h> |
| 15 |
|
| #include <stdio.h> |
| 16 |
|
| #include <string.h> |
| 17 |
|
| #include <unistd.h> |
| 18 |
|
| |
| 19 |
|
| typedef SV * Term_TtyWrite; |
| 20 |
|
| |
| 21 |
|
| MODULE = Term::TtyWrite PACKAGE = Term::TtyWrite |
| 22 |
|
| |
| 23 |
|
| void |
| 24 |
|
| DESTROY(obj) |
| 25 |
|
| Term_TtyWrite obj |
| 26 |
|
| |
| 27 |
|
| CODE: |
| 28 |
|
| SV **svp; |
| 29 |
1 |
▶ | if ((svp = hv_fetchs((HV*)obj, "fd", FALSE))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((svp = hv_fetchs((HV*)obj, "fd", FALSE))) { |
1 |
0 |
1 |
|
| 30 |
1 |
▶ | if (SvOK(*svp) && SvIOK(*svp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvOK(*svp) && SvIOK(*svp)) |
1 |
0 |
1 |
| if (SvOK(*svp) && SvIOK(*svp)) |
1 |
0 |
1 |
|
| 31 |
1 |
| close((int) SvIV(*svp)); |
| 32 |
|
| } |
| 33 |
|
| |
| 34 |
|
| Term_TtyWrite |
| 35 |
|
| new(...) |
| 36 |
|
| INIT: |
| 37 |
|
| char *classname, *devname; |
| 38 |
|
| int fd, i; |
| 39 |
|
| STRLEN len; |
| 40 |
|
| |
| 41 |
3 |
▶ | if ( sv_isobject(ST(0)) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( sv_isobject(ST(0)) ) { |
0 |
3 |
3 |
|
| 42 |
0 |
▶ | classname = HvNAME(SvSTASH(SvRV(ST(0)))); |
Branch: 6 branches
| Branch | True | False | Total |
| classname = HvNAME(SvSTASH(SvRV(ST(0)))); |
0 |
0 |
0 |
| classname = HvNAME(SvSTASH(SvRV(ST(0)))); |
0 |
0 |
0 |
| classname = HvNAME(SvSTASH(SvRV(ST(0)))); |
0 |
0 |
0 |
| classname = HvNAME(SvSTASH(SvRV(ST(0)))); |
0 |
0 |
0 |
| classname = HvNAME(SvSTASH(SvRV(ST(0)))); |
0 |
0 |
0 |
| classname = HvNAME(SvSTASH(SvRV(ST(0)))); |
0 |
0 |
0 |
|
| 43 |
|
| } else { |
| 44 |
6 |
| classname = (char *)SvPV_nolen(ST(0)); |
| 45 |
|
| } |
| 46 |
|
| |
| 47 |
|
| CODE: |
| 48 |
3 |
| RETVAL = (Term_TtyWrite)newHV(); |
| 49 |
|
| |
| 50 |
3 |
▶ | if (items != 2 || !SvPOK(ST(1))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (items != 2 || !SvPOK(ST(1))) |
2 |
1 |
3 |
| if (items != 2 || !SvPOK(ST(1))) |
0 |
2 |
2 |
|
| 51 |
1 |
| Perl_croak(aTHX_ "Usage: Term::TtyWrite->new(\"/dev/sometty\")\n"); |
| 52 |
|
| |
| 53 |
2 |
| devname = SvPV(ST(1),len); |
| 54 |
20 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
19 |
1 |
20 |
|
| 55 |
19 |
▶ | if (devname[i] == '\0') |
Branch: 1 branch
| Branch | True | False | Total |
| if (devname[i] == '\0') |
1 |
18 |
19 |
|
| 56 |
1 |
| Perl_croak(aTHX_ "invalid device name\n"); |
| 57 |
|
| } |
| 58 |
1 |
▶ | if ((fd = open(devname, O_WRONLY)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((fd = open(devname, O_WRONLY)) < 0) |
0 |
1 |
1 |
|
| 59 |
0 |
| Perl_croak(aTHX_ "could not open '%s': %s", devname, strerror(errno)); |
| 60 |
|
| |
| 61 |
1 |
| hv_stores((HV *)RETVAL, "fd", newSViv(fd) ); |
| 62 |
|
| |
| 63 |
|
| OUTPUT: |
| 64 |
|
| RETVAL |
| 65 |
|
| |
| 66 |
|
| void |
| 67 |
|
| write(obj, ...) |
| 68 |
|
| Term_TtyWrite obj |
| 69 |
|
| |
| 70 |
|
| INIT: |
| 71 |
1 |
▶ | if (items != 2 || !SvPOK(ST(1))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (items != 2 || !SvPOK(ST(1))) |
0 |
1 |
1 |
| if (items != 2 || !SvPOK(ST(1))) |
0 |
0 |
0 |
|
| 72 |
1 |
| Perl_croak(aTHX_ "Usage: $obj->write(\"some data\")"); |
| 73 |
|
| |
| 74 |
|
| CODE: |
| 75 |
|
| char *str; |
| 76 |
|
| int fd; |
| 77 |
|
| STRLEN len; |
| 78 |
|
| SV **svp; |
| 79 |
0 |
▶ | if ((svp = hv_fetchs((HV*)obj, "fd", FALSE))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((svp = hv_fetchs((HV*)obj, "fd", FALSE))) { |
0 |
0 |
0 |
|
| 80 |
0 |
▶ | if (SvOK(*svp) && SvIOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvOK(*svp) && SvIOK(*svp)) { |
0 |
0 |
0 |
| if (SvOK(*svp) && SvIOK(*svp)) { |
0 |
0 |
0 |
|
| 81 |
0 |
| fd = (int) SvIV(*svp); |
| 82 |
0 |
| str = SvPV(ST(1),len); |
| 83 |
0 |
▶ | while(len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(len-- > 0) { |
0 |
0 |
0 |
|
| 84 |
0 |
| ioctl(fd, TIOCSTI, str++); |
| 85 |
|
| } |
| 86 |
|
| } else { |
| 87 |
0 |
| Perl_croak(aTHX_ "fd unexpectedly is not set"); |
| 88 |
|
| } |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| void |
| 92 |
|
| write_delay(obj, ...) |
| 93 |
|
| Term_TtyWrite obj |
| 94 |
|
| |
| 95 |
|
| INIT: |
| 96 |
1 |
▶ | if (items != 3 || !SvPOK(ST(1)) || !SvNIOK(ST(2))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (items != 3 || !SvPOK(ST(1)) || !SvNIOK(ST(2))) |
0 |
1 |
1 |
| if (items != 3 || !SvPOK(ST(1)) || !SvNIOK(ST(2))) |
0 |
0 |
0 |
| if (items != 3 || !SvPOK(ST(1)) || !SvNIOK(ST(2))) |
0 |
0 |
0 |
|
| 97 |
1 |
| Perl_croak(aTHX_ "Usage: $obj->write_delay(\"some data\", 250)"); |
| 98 |
|
| |
| 99 |
|
| CODE: |
| 100 |
|
| char *str; |
| 101 |
|
| int fd; |
| 102 |
|
| IV delayms; |
| 103 |
|
| STRLEN len; |
| 104 |
|
| SV **svp; |
| 105 |
|
| useconds_t delay; |
| 106 |
|
| |
| 107 |
0 |
▶ | if ((svp = hv_fetchs((HV*)obj, "fd", FALSE))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((svp = hv_fetchs((HV*)obj, "fd", FALSE))) { |
0 |
0 |
0 |
|
| 108 |
0 |
▶ | if (SvOK(*svp) && SvIOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvOK(*svp) && SvIOK(*svp)) { |
0 |
0 |
0 |
| if (SvOK(*svp) && SvIOK(*svp)) { |
0 |
0 |
0 |
|
| 109 |
0 |
| fd = (int) SvIV(*svp); |
| 110 |
0 |
| str = SvPV(ST(1),len); |
| 111 |
0 |
| delayms = SvIV(ST(2)); |
| 112 |
0 |
▶ | if (delayms > UINT_MAX / 1000) delayms = UINT_MAX / 1000; |
Branch: 1 branch
| Branch | True | False | Total |
| if (delayms > UINT_MAX / 1000) delayms = UINT_MAX / 1000; |
0 |
0 |
0 |
|
| 113 |
0 |
| delay = delayms * 1000; |
| 114 |
0 |
▶ | while(len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(len-- > 0) { |
0 |
0 |
0 |
|
| 115 |
0 |
| ioctl(fd, TIOCSTI, str++); |
| 116 |
0 |
| usleep(delay); |
| 117 |
|
| } |
| 118 |
|
| } else { |
| 119 |
0 |
| Perl_croak(aTHX_ "fd unexpectedly is not set"); |
| 120 |
|
| } |
| 121 |
|
| } |