| 1 | package AnyEvent::Subprocess::Job::Delegate::Pty; | ||||||||||
| 2 | 48 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 48 | $AnyEvent::Subprocess::Job::Delegate::Pty::VERSION = '1.102912'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: give the child a pseudo-terminal | ||||||||||
| 6 | 48 | ▶ | use IO::Pty; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 48 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 48 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 48 | ▶ | use POSIX qw(dup2); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 47 | with 'AnyEvent::Subprocess::Job::Delegate'; | |||||||||
| 13 | |||||||||||
| 14 | 47 | has 'want_leftovers' => ( | |||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | isa => 'Bool', | ||||||||||
| 17 | default => 0, | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | 47 | has 'pty' => ( | |||||||||
| 21 | traits => ['NoClone'], | ||||||||||
| 22 | is => 'ro', | ||||||||||
| 23 | isa => 'IO::Pty', | ||||||||||
| 24 | lazy_build => 1, | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | 47 | has 'slave_pty' => ( | |||||||||
| 28 | traits => ['NoClone'], | ||||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | lazy_build => 1, | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 47 | has 'handle' => ( | |||||||||
| 34 | traits => ['NoClone'], | ||||||||||
| 35 | is => 'ro', | ||||||||||
| 36 | isa => 'AnyEvent::Subprocess::Handle', | ||||||||||
| 37 | lazy_build => 1, | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | has 'handle_class' => ( | ||||||||||
| 41 | is => 'ro', | ||||||||||
| 42 | isa => 'ClassName', | ||||||||||
| 43 | required => 1, | ||||||||||
| 44 | 8 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 8 | require AnyEvent::Subprocess::Handle; | |||||||||
| 46 | 8 | return 'AnyEvent::Subprocess::Handle'; | |||||||||
| 47 | }, | ||||||||||
| 48 | 47 | ); | |||||||||
| 49 | |||||||||||
| 50 | 4 | ▶ | sub __build_handle { | ||||||||
|
Subroutine
__build_handle: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 4 | my ($self, $fh, @rest) = @_; | |||||||||
| 52 | 4 | return $self->handle_class->new( fh => $fh, @rest ); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | has 'run_delegate_class' => ( | ||||||||||
| 56 | is => 'ro', | ||||||||||
| 57 | isa => 'ClassName', | ||||||||||
| 58 | required => 1, | ||||||||||
| 59 | 8 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 8 | require AnyEvent::Subprocess::Running::Delegate::Handle; | |||||||||
| 61 | 8 | return 'AnyEvent::Subprocess::Running::Delegate::Handle'; | |||||||||
| 62 | }, | ||||||||||
| 63 | 47 | ); | |||||||||
| 64 | |||||||||||
| 65 | has 'redirect_handles' => ( | ||||||||||
| 66 | is => 'ro', | ||||||||||
| 67 | isa => 'ArrayRef[GlobRef]', | ||||||||||
| 68 | auto_deref => 1, | ||||||||||
| 69 | required => 1, | ||||||||||
| 70 | 8 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 8 | return [\*STDIN, \*STDOUT], | |||||||||
| 72 | }, | ||||||||||
| 73 | 47 | ); | |||||||||
| 74 | |||||||||||
| 75 | 4 | ▶ | sub _build_pty { | ||||||||
|
Subroutine
_build_pty: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 4 | return IO::Pty->new; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 2 | ▶ | sub _build_slave_pty { | ||||||||
|
Subroutine
_build_slave_pty: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 2 | my $self = shift; | |||||||||
| 81 | 2 | return $self->pty->slave; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 4 | ▶ | sub _build_handle { | ||||||||
|
Subroutine
_build_handle: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 4 | my $self = shift; | |||||||||
| 86 | |||||||||||
| 87 | 4 | return $self->__build_handle( | |||||||||
| 88 | $self->pty, | ||||||||||
| 89 | _direction => 'rw', | ||||||||||
| 90 | _name => 'parent pty handle: '. $self->name, | ||||||||||
| 91 | ); | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 4 | ▶ | sub build_run_delegates { | ||||||||
|
Subroutine
build_run_delegates: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 95 | 4 | my $self = shift; | |||||||||
| 96 | 4 | return $self->run_delegate_class->new( | |||||||||
| 97 | name => $self->name, | ||||||||||
| 98 | direction => 'rw', | ||||||||||
| 99 | handle => $self->handle, | ||||||||||
| 100 | want_leftovers => $self->want_leftovers, | ||||||||||
| 101 | ); | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 2 | ▶ | sub parent_finalize_hook { | ||||||||
|
Subroutine
parent_finalize_hook: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 105 | 2 | my $self = shift; | |||||||||
| 106 | 2 | $self->pty->close_slave; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 2 | ▶ | sub child_setup_hook { | ||||||||
|
Subroutine
child_setup_hook: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 110 | 2 | my $self = shift; | |||||||||
| 111 | |||||||||||
| 112 | 2 | $self->pty->make_slave_controlling_terminal; | |||||||||
| 113 | |||||||||||
| 114 | 2 | $self->handle->do_not_want; | |||||||||
| 115 | |||||||||||
| 116 | 2 | AnyEvent::Util::fh_nonblocking $self->slave_pty, 0; | |||||||||
| 117 | |||||||||||
| 118 | 2 | for my $fh ($self->redirect_handles){ | |||||||||
| 119 | 4 | ▶ | dup2( fileno($self->slave_pty), fileno($fh) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | or confess "Can't dup2 $fh to slave pty: $!"; | ||||||||||
| 121 | } | ||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 2 | ▶ | sub build_code_args {} | ||||||||
|
Subroutine
build_code_args: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 125 | 1 | ▶ | sub child_finalize_hook {} | ||||||||
|
Subroutine
child_finalize_hook: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 126 | 4 | ▶ | sub parent_setup_hook {} | ||||||||
|
Subroutine
parent_setup_hook: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 127 | 1 | ▶ | sub receive_child_result {} | ||||||||
|
Subroutine
receive_child_result: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 128 | 0 | ▶ | sub receive_child_error {} | ||||||||
|
Subroutine
receive_child_error: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 129 | |||||||||||
| 130 | 47 | __PACKAGE__->meta->make_immutable; | |||||||||
| 131 | |||||||||||
| 132 | 47 | 1; | |||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | =pod | ||||||||||
| 137 | |||||||||||
| 138 | =head1 NAME | ||||||||||
| 139 | |||||||||||
| 140 | AnyEvent::Subprocess::Job::Delegate::Pty - give the child a pseudo-terminal | ||||||||||
| 141 | |||||||||||
| 142 | =head1 VERSION | ||||||||||
| 143 | |||||||||||
| 144 | version 1.102912 | ||||||||||
| 145 | |||||||||||
| 146 | =head1 DESCRIPTION | ||||||||||
| 147 | |||||||||||
| 148 | You can have more than one of these, but the last one will become the | ||||||||||
| 149 | controlling tty. | ||||||||||
| 150 | |||||||||||
| 151 | =head1 INITARGS | ||||||||||
| 152 | |||||||||||
| 153 | =head2 redirect_handles | ||||||||||
| 154 | |||||||||||
| 155 | A list of filehandles that will be connected to this Pty in the child. | ||||||||||
| 156 | Defaults to stdout and stderr. | ||||||||||
| 157 | |||||||||||
| 158 | =head1 METHODS | ||||||||||
| 159 | |||||||||||
| 160 | =head2 pty | ||||||||||
| 161 | |||||||||||
| 162 | Returns the L<IO::Pty|IO::Pty> object. You can use this object to set | ||||||||||
| 163 | the child's window size, etc. | ||||||||||
| 164 | |||||||||||
| 165 | =head2 handle | ||||||||||
| 166 | |||||||||||
| 167 | The handle that you can read/write to communicate with the child. | ||||||||||
| 168 | Note that writing can be confusing; because the Pty emulates a | ||||||||||
| 169 | terminal, and terminals echo input, you will get back things that you | ||||||||||
| 170 | write. You can disable this behavior by changing the terminal | ||||||||||
| 171 | parameters in the child process. | ||||||||||
| 172 | |||||||||||
| 173 | (If you don't know what "raw mode" and "cooked mode" are, you should | ||||||||||
| 174 | read up on UNIX terminals. You might really want a pipe, not a pseudo | ||||||||||
| 175 | terminal.) | ||||||||||
| 176 | |||||||||||
| 177 | =head1 AUTHOR | ||||||||||
| 178 | |||||||||||
| 179 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||
| 180 | |||||||||||
| 181 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 182 | |||||||||||
| 183 | This software is copyright (c) 2011 by Jonathan Rockway. | ||||||||||
| 184 | |||||||||||
| 185 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 186 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 187 | |||||||||||
| 188 | =cut | ||||||||||
| 189 | |||||||||||
| 190 | |||||||||||
| 191 | __END__ | ||||||||||