blib/lib/IO/Termios.pm

filter: Statementstmt 54.9% 138 / 251 Branchbran 11.9% 10 / 84 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 51.8% 28 / 54 Podpod 100% 12 / 12 totaltotal 45.8% 188 / 410 SCAR 61.0
CC
47
Cov
43%
open
30.0
set_mode
30.0
set_modem
20.0
CRAP 447.5
↑ Summary Build »
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, 2008-2018 -- leonerd@leonerd.org.uk
5  
6 package IO::Termios;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use 5.010; # //
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use base qw( IO::Handle );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.08';
16  
17 2 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Fcntl qw( O_RDWR );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use POSIX qw( TCSANOW );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use IO::Tty;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 use IO::Tty::Constant qw(
23 TIOCMGET TIOCMSET TIOCMBIC TIOCMBIS
24 TIOCM_DTR TIOCM_DSR TIOCM_RTS TIOCM_CTS TIOCM_CD TIOCM_RI
25 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Linux can support finer-grained control of baud rates if we let it
28 2 ▶use constant HAVE_LINUX_TERMIOS2 => eval { require Linux::Termios2; };
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head1 NAME
31  
32 C<IO::Termios> - supply F<termios(3)> methods to C<IO::Handle> objects
33  
34 =head1 SYNOPSIS
35  
36 use IO::Termios;
37  
38 my $term = IO::Termios->open( "/dev/ttyS0", "9600,8,n,1" )
39 or die "Cannot open ttyS0 - $!";
40  
41 $term->print( "Hello world\n" ); # Still an IO::Handle
42  
43 while( <$term> ) {
44 print "A line from ttyS0: $_";
45 }
46  
47 =head1 DESCRIPTION
48  
49 This class extends the generic C<IO::Handle> object class by providing methods
50 which access the system's terminal control C<termios(3)> operations. These
51 methods are primarily of interest when dealing with TTY devices, including
52 serial ports.
53  
54 The flag-setting methods will apply to any TTY device, such as a pseudo-tty,
55 and are useful for controlling such flags as the C<ECHO> flag, to disable
56 local echo.
57  
58 my $stdin = IO::Termios->new( \*STDIN );
59 $stdin->setflag_echo( 0 );
60  
61 When dealing with a serial port the line mode method is useful for setting the
62 basic serial parameters such as baud rate, and the modem line control methods
63 can be used to access the hardware handshaking lines.
64  
65 my $ttyS0 = IO::Termios->open( "/dev/ttyS0" );
66 $ttyS0->set_mode( "19200,8,n,1" );
67 $ttyS0->set_modem({ dsr => 1, cts => 1 });
68  
69 =head2 Upgrading STDIN/STDOUT/STDERR
70  
71 If you pass the C<-upgrade> option at C<import> time, any of STDIN, STDOUT or
72 STDERR that are found to be TTY wrappers are automatically upgraded into
73 C<IO::Termios> instances.
74  
75 use IO::Termios -upgrade;
76  
77 STDIN->setflag_echo(0);
78  
79 =head2 Arbitrary Baud Rates on Linux
80  
81 F<Linux> supports a non-POSIX extension to the usual C<termios> interface,
82 which allows arbitrary baud rates to be set. C<IO::Termios> can automatically
83 make use of this ability if the L<Linux::Termios2> module is installed. If so,
84 this will be used automatically and transparently, to allow the C<set*baud>
85 methods to set any rate allowed by the kernel/driver. If not, then only the
86 POSIX-compatible rates may be used.
87  
88 =cut
89  
90 sub import
91 {
92 2 ▶ my $pkg = shift;
Subroutine import: called CC=5 SCAR=22.5
93 2 my @symbols = @_;
94 2 my $caller = caller;
95  
96 2 my $upgrade;
97 2 ▶ @symbols = grep { $_ eq "-upgrade" ? ( $upgrade++, 0 ) : 1 } @symbols;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '-upgrade' ? : 0 0 0
98  
99 2 ▶ if( $upgrade ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upgrade) 0 2 2
100 0 foreach my $fh ( *STDIN{IO}, *STDOUT{IO}, *STDERR{IO} ) {
101 0 ▶ IO::Termios::Attrs->new->getattr( $fh->fileno ) or next;
Branch: 1 branch
BranchTrueFalseTotal
"IO::Termios::Attrs"->new->getattr($fh->fileno) or next 0 0 0
102  
103 0 bless $fh, __PACKAGE__;
104 }
105 }
106 }
107  
108 =head1 CONSTRUCTORS
109  
110 =cut
111  
112 =head2 new
113  
114 $term = IO::Termios->new()
115  
116 Construct a new C<IO::Termios> object around the terminal for the program.
117 This is found by checking if any of C<STDIN>, C<STDOUT> or C<STDERR> are a
118 terminal. The first one that's found is used. An error occurs if no terminal
119 can be found by this method.
120  
121 =head2 new (handle)
122  
123 $term = IO::Termios->new( $handle )
124  
125 Construct a new C<IO::Termios> object around the given filehandle.
126  
127 =cut
128  
129 sub new
130 {
131 1 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=26.2
Pod documented
132 1 my ( $handle ) = @_;
133  
134 1 ▶ if( not $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle) 0 1 1
135 # Try to find a terminal - STDIN, STDOUT, STDERR are good candidates
136 0 ▶ return $class->SUPER::new_from_fd( fileno STDIN, "w+" ) if -t STDIN;
Branch: 1 branch
BranchTrueFalseTotal
if -t STDIN 0 0 0
137 0 ▶ return $class->SUPER::new_from_fd( fileno STDOUT, "w+" ) if -t STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
if -t STDOUT 0 0 0
138 0 ▶ return $class->SUPER::new_from_fd( fileno STDERR, "w+" ) if -t STDERR;
Branch: 1 branch
BranchTrueFalseTotal
if -t STDERR 0 0 0
139  
140 0 die "TODO: Need to find a terminal\n";
141 }
142  
143 1 ▶ croak '$handle is not a filehandle' unless defined fileno $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined fileno $handle 0 1 1
144  
145 1 my $self = $class->SUPER::new_from_fd( $handle, "w+" );
146  
147 1 return $self;
148 }
149  
150 =head2 open
151  
152 $term = IO::Termios->open( $path, $modestr, $flags )
153  
154 Open the given path, and return a new C<IO::Termios> object around the
155 filehandle. If the C<open> call fails, C<undef> is returned.
156  
157 If C<$modestr> is provided, the constructor will pass it to the C<set_mode>
158 method before returning.
159  
160 If C<$flags> is provided, it will be passed on to the underlying C<sysopen()>
161 call used to open the filehandle. It should contain a bitwise-or combination
162 of C<O_*> flags from the L<Fcntl> module - for example C<O_NOCTTY> or
163 C<O_NDELAY>. The value C<O_RDWR> will be added to this; the caller does not
164 need to specify it directly. For example:
165  
166 use Fcntl qw( O_NOCTTY O_NDELAY );
167  
168 $term = IO::Termios->open( "/dev/ttyS0", O_NOCTTY|O_NDELAY );
169 $term->setflag_clocal( 1 );
170 $term->blocking( 1 );
171  
172 =cut
173  
174 sub open
175 {
176 0 ▶ my $class = shift;
Subroutine open: not called CC=5 SCAR=34.0
Pod documented
177 0 my ( $path, $modestr, $flags ) = @_;
178  
179 0 ▶ $flags //= 0;
Condition0%0/2
$flags //= 0
l!l
00
MC/DC0%0/1
$flags //= 0
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
180  
181 0 ▶ sysopen my $tty, $path, O_RDWR | $flags, or return undef;
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $tty, $path, 2 | $flags or return undef 0 0 0
182 0 ▶ my $self = $class->new( $tty ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $self = $class->new($tty) or return undef 0 0 0
183  
184 0 ▶ $self->set_mode( $modestr ) if defined $modestr;
Branch: 1 branch
BranchTrueFalseTotal
if defined $modestr 0 0 0
185  
186 0 return $self;
187 }
188  
189 =head1 METHODS
190  
191 =cut
192  
193 =head2 getattr
194  
195 $attrs = $term->getattr
196  
197 Makes a C<tcgetattr()> call on the underlying filehandle, and returns a
198 C<IO::Termios::Attrs> object.
199  
200 If the C<tcgetattr()> call fails, C<undef> is returned.
201  
202 =cut
203  
204 sub getattr
205 {
206 8 ▶ my $self = shift;
Subroutine getattr: called CC=2 SCAR=7.0
Pod documented
207  
208 8 my $attrs = IO::Termios::Attrs->new;
209 8 ▶ $attrs->getattr( $self->fileno ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$attrs->getattr($self->fileno) or return undef 0 8 8
210  
211 8 return $attrs;
212 }
213  
214 =head2 setattr
215  
216 $term->setattr( $attrs )
217  
218 Makes a C<tcsetattr()> call on the underlying file handle, setting attributes
219 from the given C<IO::Termios::Attrs> object.
220  
221 If the C<tcsetattr()> call fails, C<undef> is returned. Otherwise, a true
222 value is returned.
223  
224 =cut
225  
226 sub setattr
227 {
228 4 ▶ my $self = shift;
Subroutine setattr: called CC=1 SCAR=0.0
Pod documented
229 4 my ( $attrs ) = @_;
230  
231 4 return $attrs->setattr( $self->fileno, TCSANOW );
232 }
233  
234 =head2 set_mode
235  
236 =head2 get_mode
237  
238 $term->set_mode( $modestr )
239  
240 $modestr = $term->get_mode
241  
242 Accessor for the derived "mode string", which is a comma-joined concatenation
243 of the baud rate, character size, parity mode, and stop size in a format such
244 as
245  
246 19200,8,n,1
247  
248 When setting the mode string, trailing components may be omitted meaning their
249 value will not be affected.
250  
251 =cut
252  
253 sub set_mode
254 {
255 0 ▶ my $self = shift;
Subroutine set_mode: not called CC=5 SCAR=34.0
Pod documented
256 0 my ( $modestr ) = @_;
257  
258 0 my ( $baud, $csize, $parity, $stop ) = split m/,/, $modestr;
259  
260 0 my $attrs = $self->getattr;
261  
262 0 ▶ $attrs->setbaud ( $baud ) if defined $baud;
Branch: 1 branch
BranchTrueFalseTotal
if defined $baud 0 0 0
263 0 ▶ $attrs->setcsize ( $csize ) if defined $csize;
Branch: 1 branch
BranchTrueFalseTotal
if defined $csize 0 0 0
264 0 ▶ $attrs->setparity( $parity ) if defined $parity;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parity 0 0 0
265 0 ▶ $attrs->setstop ( $stop ) if defined $stop;
Branch: 1 branch
BranchTrueFalseTotal
if defined $stop 0 0 0
266  
267 0 $self->setattr( $attrs );
268 }
269  
270 sub get_mode
271 {
272 0 ▶ my $self = shift;
Subroutine get_mode: not called CC=1 SCAR=6.9
Pod documented
273  
274 0 my $attrs = $self->getattr;
275 0 return join ",",
276 $attrs->getibaud,
277 $attrs->getcsize,
278 $attrs->getparity,
279 $attrs->getstop;
280 }
281  
282 =head2 tiocmget
283  
284 =head2 tiocmset
285  
286 $bits = $term->tiocmget
287  
288 $term->tiocmset( $bits )
289  
290 Accessor for the modem line control bits. Takes or returns a bitmask of
291 values.
292  
293 =cut
294  
295 sub tiocmget
296 {
297 0 ▶ my $self = shift;
Subroutine tiocmget: not called CC=2 SCAR=17.9
Pod documented
298  
299 0 my $bitstr = pack "i!", 0;
300 0 ▶ ioctl( $self, TIOCMGET, $bitstr ) or
Branch: 1 branch
BranchTrueFalseTotal
ioctl $self, 21525, $bitstr or croak("Cannot ioctl(TIOCMGET) - $!") 0 0 0
301 croak "Cannot ioctl(TIOCMGET) - $!";
302  
303 0 return unpack "i!", $bitstr;
304 }
305  
306 sub tiocmset
307 {
308 0 ▶ my $self = shift;
Subroutine tiocmset: not called CC=2 SCAR=17.9
Pod documented
309 0 my ( $bits ) = @_;
310  
311 0 my $bitstr = pack "i!", $bits;
312 0 ▶ ioctl( $self, TIOCMSET, $bitstr )
Branch: 1 branch
BranchTrueFalseTotal
ioctl $self, 21528, $bitstr or croak("Cannot ioctl(TIOCMSET) - $!") 0 0 0
313 or croak "Cannot ioctl(TIOCMSET) - $!";
314 }
315  
316 =head2 tiocmbic
317  
318 =head2 tiocmbis
319  
320 $term->tiocmbic( $bits )
321  
322 $term->tiocmbis( $bits )
323  
324 Bitwise mutator methods for the modem line control bits. C<tiocmbic> will
325 clear just the bits provided and leave the others unchanged; C<tiocmbis> will
326 set them.
327  
328 =cut
329  
330 sub tiocmbic
331 {
332 0 ▶ my $self = shift;
Subroutine tiocmbic: not called CC=2 SCAR=17.9
Pod documented
333 0 my ( $bits ) = @_;
334  
335 0 my $bitstr = pack "i!", $bits;
336 0 ▶ ioctl( $self, TIOCMBIC, $bitstr )
Branch: 1 branch
BranchTrueFalseTotal
ioctl $self, 21527, $bitstr or croak("Cannot ioctl(TIOCMBIC) - $!") 0 0 0
337 or croak "Cannot ioctl(TIOCMBIC) - $!";
338 }
339  
340 sub tiocmbis
341 {
342 0 ▶ my $self = shift;
Subroutine tiocmbis: not called CC=2 SCAR=17.9
Pod documented
343 0 my ( $bits ) = @_;
344  
345 0 my $bitstr = pack "i!", $bits;
346 0 ▶ ioctl( $self, TIOCMBIS, $bitstr )
Branch: 1 branch
BranchTrueFalseTotal
ioctl $self, 21526, $bitstr or croak("Cannot ioctl(TIOCMBIS) - $!") 0 0 0
347 or croak "Cannot ioctl(TIOCMBIS) - $!";
348 }
349  
350 2 my %_bit2modem;
351 my %_modem2bit;
352 2 foreach (qw( dtr dsr rts cts cd ri )) {
353 12 my $bit = IO::Tty::Constant->${\"TIOCM_\U$_"};
354 12 $_bit2modem{$bit} = $_;
355 12 $_modem2bit{$_} = $bit;
356  
357 my $getmodem = sub {
358 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
359 0 return !!($self->tiocmget & $bit);
360 12 };
361 my $setmodem = sub {
362 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
363 0 my ( $set ) = @_;
364 0 ▶ $set ? $self->tiocmbis( $bit )
Branch: 1 branch
BranchTrueFalseTotal
$set ? : 0 0 0
365 : $self->tiocmbic( $bit );
366 12 };
367  
368 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
369 12 *{"getmodem_$_"} = $getmodem;
370 12 *{"setmodem_$_"} = $setmodem;
371 }
372  
373 =head2 get_modem
374  
375 $flags = $term->get_modem
376  
377 Returns a hash reference containing named flags corresponding to the modem
378 line control bits. Any bit that is set will yield a key in the returned hash
379 of the same name. The bit names are
380  
381 dtr dsr rts cts cd ri
382  
383 =cut
384  
385 sub get_modem
386 {
387 0 ▶ my $self = shift;
Subroutine get_modem: not called CC=2 SCAR=17.9
Pod documented
388 0 my $bits = $self->tiocmget;
389  
390 return +{
391 0 ▶ map { $bits & $_modem2bit{$_} ? ( $_ => 1 ) : () } keys %_modem2bit
Branch: 1 branch
BranchTrueFalseTotal
$bits & $_modem2bit{$_} ? : 0 0 0
392 };
393 }
394  
395 =head2 set_modem
396  
397 $term->set_modem( $flags )
398  
399 Changes the modem line control bit flags as given by the hash reference. Each
400 bit to be changed should be represented by a key in the C<$flags> hash of the
401 names given above. False values will be cleared, true values will be set.
402 Other flags will not be altered.
403  
404 =cut
405  
406 sub set_modem
407 {
408 0 ▶ my $self = shift;
Subroutine set_modem: not called CC=4 SCAR=30.0
Pod documented
409 0 my ( $flags ) = @_;
410  
411 0 my $bits = $self->tiocmget;
412 0 foreach ( keys %$flags ) {
413 0 ▶ my $bit = $_modem2bit{$_} or croak "Unrecognised modem line control bit $_";
Branch: 1 branch
BranchTrueFalseTotal
my $bit = $_modem2bit{$_} or croak("Unrecognised modem line control bit $_") 0 0 0
414  
415 0 ▶ $flags->{$_} ? ( $bits |= $bit )
Branch: 1 branch
BranchTrueFalseTotal
$flags->{$_} ? : 0 0 0
416 : ( $bits &= ~$bit );
417 }
418  
419 0 $self->tiocmset( $bits );
420 }
421  
422 =head2 getmodem_BIT
423  
424 =head2 setmodem_BIT
425  
426 $set = $term->getmodem_BIT
427  
428 $term->setmodem_BIT( $set )
429  
430 Accessor methods for each of the modem line control bits. A set of methods
431 exists for each of the named modem control bits given above.
432  
433 =head1 FLAG-ACCESSOR METHODS
434  
435 Theses methods are implemented in terms of the lower level methods, but
436 provide an interface which is more abstract, and easier to re-implement on
437 other non-POSIX systems. These should be used in preference to the lower ones.
438  
439 For efficiency, when getting or setting a large number of flags, it may be
440 more efficient to call C<getattr>, then operate on the returned object,
441 before possibly passing it to C<setattr>. The returned C<IO::Termios::Attrs>
442 object supports the same methods as documented here.
443  
444 The following two sections of code are therefore equivalent, though the latter
445 is more efficient as it only calls C<setattr> once.
446  
447 $term->setbaud( 38400 );
448 $term->setcsize( 8 );
449 $term->setparity( 'n' );
450 $term->setstop( 1 );
451  
452 Z<>
453  
454 my $attrs = $term->getattr;
455 $attrs->setbaud( 38400 );
456 $attrs->setcsize( 8 );
457 $attrs->setparity( 'n' );
458 $attrs->setstop( 1 );
459 $term->setattr( $attrs );
460  
461 However, a convenient shortcut method is provided for the common case of
462 setting the baud rate, character size, parity and stop size all at the same
463 time. This is C<set_mode>:
464  
465 $term->set_mode( "38400,8,n,1" );
466  
467 =cut
468  
469 =head2 getibaud
470  
471 =head2 getobaud
472  
473 =head2 setibaud
474  
475 =head2 setobaud
476  
477 =head2 setbaud
478  
479 $baud = $term->getibaud
480  
481 $baud = $term->getobaud
482  
483 $term->setibaud( $baud )
484  
485 $term->setobaud( $baud )
486  
487 $term->setbaud( $baud )
488  
489 Convenience accessors for the C<ispeed> and C<ospeed>. C<$baud> is an integer
490 directly giving the line rate, instead of one of the C<BI<nnn>> constants.
491  
492 =head2 getcsize
493  
494 =head2 setcsize
495  
496 $bits = $term->getcsize
497  
498 $term->setcsize( $bits )
499  
500 Convenience accessor for the C<CSIZE> bits of C<c_cflag>. C<$bits> is an
501 integer 5 to 8.
502  
503 =head2 getparity
504  
505 =head2 setparity
506  
507 $parity = $term->getparity
508  
509 $term->setparity( $parity )
510  
511 Convenience accessor for the C<PARENB> and C<PARODD> bits of C<c_cflag>.
512 C<$parity> is C<n>, C<o> or C<e>.
513  
514 =head2 getstop
515  
516 =head2 setstop
517  
518 $stop = $term->getstop
519  
520 $term->setstop( $stop )
521  
522 Convenience accessor for the C<CSTOPB> bit of C<c_cflag>. C<$stop> is 1 or 2.
523  
524 =head2 cfmakeraw
525  
526 $term->cfmakeraw
527  
528 I<Since version 0.07.>
529  
530 Adjusts several bit flags to put the terminal into a "raw" mode. Input is
531 available a character at a time, echo is disabled, and all special processing
532 of input and output characters is disabled.
533  
534 =cut
535  
536 2 foreach my $name (qw( ibaud obaud csize parity stop )) {
537 10 my $getmethod = "get$name";
538 10 my $setmethod = "set$name";
539  
540 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
541 *$getmethod = sub {
542 0 ▶ my ( $self ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
543 0 ▶ my $attrs = $self->getattr or croak "Cannot getattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
my $attrs = $self->getattr or croak("Cannot getattr - $!") 0 0 0
544 0 return $attrs->$getmethod;
545 10 };
546 *$setmethod = sub {
547 0 ▶ my ( $self, $val ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
548 0 ▶ my $attrs = $self->getattr or croak "Cannot getattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
my $attrs = $self->getattr or croak("Cannot getattr - $!") 0 0 0
549 0 $attrs->$setmethod( $val );
550 0 ▶ $self->setattr( $attrs ) or croak "Cannot setattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->setattr($attrs) or croak("Cannot setattr - $!") 0 0 0
551 10 };
552 }
553  
554 2 foreach my $method (qw( setbaud cfmakeraw )) {
555 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
556 *$method = sub {
557 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
558 0 ▶ my $attrs = $self->getattr or croak "Cannot getattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
my $attrs = $self->getattr or croak("Cannot getattr - $!") 0 0 0
559 0 $attrs->$method( @_ );
560 0 ▶ $self->setattr( $attrs ) or croak "Cannot setattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->setattr($attrs) or croak("Cannot setattr - $!") 0 0 0
561 4 };
562 }
563  
564 =head2 getflag_opost
565  
566 =head2 setflag_opost
567  
568 $mode = $term->getflag_opost
569  
570 $term->setflag_opost( $mode )
571  
572 I<Since version 0.07.>
573  
574 Accessor for the C<OPOST> bit of the C<c_oflag>. This enables system-specific
575 post-processing on output.
576  
577 =head2 getflag_cread
578  
579 =head2 setflag_cread
580  
581 $mode = $term->getflag_cread
582  
583 $term->setflag_cread( $mode )
584  
585 Accessor for the C<CREAD> bit of the C<c_cflag>. This enables the receiver.
586  
587 =head2 getflag_hupcl
588  
589 =head2 setflag_hupcl
590  
591 $mode = $term->getflag_hupcl
592  
593 $term->setflag_hupcl( $mode )
594  
595 Accessor for the C<HUPCL> bit of the C<c_cflag>. This lowers the modem control
596 lines after the last process closes the device.
597  
598 =head2 getflag_clocal
599  
600 =head2 setflag_clocal
601  
602 $mode = $term->getflag_clocal
603  
604 $term->setflag_clocal( $mode )
605  
606 Accessor for the C<CLOCAL> bit of the C<c_cflag>. This controls whether local
607 mode is enabled; which if set, ignores modem control lines.
608  
609 =cut
610  
611 =head2 getflag_icanon
612  
613 =head2 setflag_icanon
614  
615 $mode = $term->getflag_icanon
616  
617 $term->setflag_icanon( $mode )
618  
619 Accessor for the C<ICANON> bit of C<c_lflag>. This is called "canonical" mode
620 and controls whether the terminal's line-editing feature will be used to
621 return a whole line (if true), or if individual bytes from keystrokes will be
622 returned as they are available (if false).
623  
624 =cut
625  
626 =head2 getflag_echo
627  
628 =head2 setflag_echo
629  
630 $mode = $term->getflag_echo
631  
632 $term->setflag_echo( $mode )
633  
634 Accessor for the C<ECHO> bit of C<c_lflag>. This controls whether input
635 characters are echoed back to the terminal.
636  
637 =cut
638  
639 2 my @flags = (
640 # oflag
641 [ opost => qw( OPOST o ) ],
642 # cflag
643 [ cread => qw( CREAD c ) ],
644 [ clocal => qw( CLOCAL c ) ],
645 [ hupcl => qw( HUPCL c ) ],
646 # lflag
647 [ icanon => qw( ICANON l ) ],
648 [ echo => qw( ECHO l ) ],
649 );
650  
651 2 foreach ( @flags ) {
652 12 my ( $name ) = @$_;
653  
654 12 my $getmethod = "getflag_$name";
655 12 my $setmethod = "setflag_$name";
656  
657 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
658 *$getmethod = sub {
659 4 ▶ my ( $self ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
660 4 ▶ my $attrs = $self->getattr or croak "Cannot getattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
my $attrs = $self->getattr or croak("Cannot getattr - $!") 0 4 4
661 4 return $attrs->$getmethod;
662 12 };
663 *$setmethod = sub {
664 4 ▶ my ( $self, $set ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.4
665 4 ▶ my $attrs = $self->getattr or croak "Cannot getattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
my $attrs = $self->getattr or croak("Cannot getattr - $!") 0 4 4
666 4 $attrs->$setmethod( $set );
667 4 ▶ $self->setattr( $attrs ) or croak "Cannot setattr - $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->setattr($attrs) or croak("Cannot setattr - $!") 0 4 4
668 12 };
669 }
670  
671 package # hide from CPAN
672 IO::Termios::Attrs;
673  
674 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
675 2 use POSIX qw(
676 CSIZE CS5 CS6 CS7 CS8 PARENB PARODD CSTOPB
677 IGNBRK BRKINT PARMRK ISTRIP INLCR IGNCR ICRNL IXON OPOST ECHO ECHONL ICANON ISIG IEXTEN
678 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
679 # IO::Tty has more B<\d> constants than POSIX has
680 2 ▶use IO::Tty;
Subroutine BEGIN: called CC=1 SCAR=0.0
681  
682 # Simple XS-implemented classes tend not to respect subclassing
683 sub new
684 {
685 8 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
686 8 my $self = $class->SUPER::new;
687 8 bless $self, $class;
688 8 return $self;
689 }
690  
691 2 ▶if( IO::Termios::HAVE_LINUX_TERMIOS2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&IO::Termios::HAVE_LINUX_TERMIOS2()) { } 0 2 2
692 0 our @ISA = qw( Linux::Termios2 );
693  
694 # baud is directly applicable
695 0 *getibaud = __PACKAGE__->can( 'getispeed' );
696 0 *getobaud = __PACKAGE__->can( 'getospeed' );
697  
698 0 *setibaud = __PACKAGE__->can( 'setispeed' );
699 0 *setobaud = __PACKAGE__->can( 'setospeed' );
700 }
701 else {
702 2 our @ISA = qw( POSIX::Termios );
703  
704 # baud needs converting to/from the speed_t constants
705  
706 2 my %_speed2baud = map { IO::Tty::Constant->${\"B$_"} => $_ }
707 qw( 0 50 75 110 134 150 200 300 600 1200 2400 4800 9600 19200 38400 57600 115200 230400 );
708 2 my %_baud2speed = reverse %_speed2baud;
709  
710 0 ▶ *getibaud = sub { $_speed2baud{ $_[0]->getispeed } };
Subroutine __ANON__: not called CC=1 SCAR=1.2
711 0 ▶ *getobaud = sub { $_speed2baud{ $_[0]->getospeed } };
Subroutine __ANON__: not called CC=1 SCAR=1.2
712  
713 *setibaud = sub {
714 0 ▶ $_[0]->setispeed( $_baud2speed{$_[1]} // die "Unrecognised baud rate" );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_baud2speed{$_[1]} // die("Unrecognised baud rate")
l!l
00
MC/DC0%0/1
$_baud2speed{$_[1]} // die("Unrecognised baud rate")
$_baud2speed{$_[1]}
Truth table0%0/2
A
A= $_baud2speed{$_[1]}
Aresult
11
00
715 2 };
716 *setobaud = sub {
717 0 ▶ $_[0]->setospeed( $_baud2speed{$_[1]} // die "Unrecognised baud rate" );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_baud2speed{$_[1]} // die("Unrecognised baud rate")
l!l
00
MC/DC0%0/1
$_baud2speed{$_[1]} // die("Unrecognised baud rate")
$_baud2speed{$_[1]}
Truth table0%0/2
A
A= $_baud2speed{$_[1]}
Aresult
11
00
718 2 };
719  
720 }
721  
722 sub setbaud
723 {
724 0 ▶ $_[0]->setibaud( $_[1] ); $_[0]->setobaud( $_[1] );
Subroutine setbaud: not called CC=1 SCAR=6.9
725 }
726  
727 2 foreach ( @flags ) {
728 12 my ( $name, $const, $member ) = @$_;
729  
730 12 $const = POSIX->$const();
731  
732 12 my $getmethod = "getflag_$name";
733 12 my $getflag = "get${member}flag";
734  
735 12 my $setmethod = "setflag_$name";
736 12 my $setflag = "set${member}flag";
737  
738 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
739 *$getmethod = sub {
740 4 ▶ my ( $self ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
741 4 $self->$getflag & $const
742 12 };
743 *$setmethod = sub {
744 4 ▶ my ( $self, $set ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
745 4 ▶ $set ? $self->$setflag( $self->$getflag | $const )
Branch: 1 branch
BranchTrueFalseTotal
$set ? : 2 2 4
746 : $self->$setflag( $self->$getflag & ~$const );
747 12 };
748 }
749  
750 sub getcsize
751 {
752 0 ▶ my $self = shift;
Subroutine getcsize: not called CC=1 SCAR=6.9
753 0 my $cflag = $self->getcflag;
754 return {
755 CS5, 5,
756 CS6, 6,
757 CS7, 7,
758 CS8, 8,
759 0 }->{ $cflag & CSIZE };
760 }
761  
762 sub setcsize
763 {
764 0 ▶ my $self = shift;
Subroutine setcsize: not called CC=1 SCAR=6.9
765 0 my ( $bits ) = @_;
766 0 my $cflag = $self->getcflag;
767  
768 0 $cflag &= ~CSIZE;
769 $cflag |= {
770 5, CS5,
771 6, CS6,
772 7, CS7,
773 8, CS8,
774 0 }->{ $bits };
775  
776 0 $self->setcflag( $cflag );
777 }
778  
779 sub getparity
780 {
781 0 ▶ my $self = shift;
Subroutine getparity: not called CC=3 SCAR=24.8
782 0 my $cflag = $self->getcflag;
783 0 ▶ return 'n' unless $cflag & PARENB;
Branch: 1 branch
BranchTrueFalseTotal
unless $cflag & 256 0 0 0
784 0 ▶ return 'o' if $cflag & PARODD;
Branch: 1 branch
BranchTrueFalseTotal
if $cflag & 512 0 0 0
785 0 return 'e';
786 }
787  
788 sub setparity
789 {
790 0 ▶ my $self = shift;
Subroutine setparity: not called CC=4 SCAR=30.0
791 0 my ( $parity ) = @_;
792 0 my $cflag = $self->getcflag;
793  
794 0 ▶ $parity eq 'n' ? $cflag &= ~PARENB :
Branch: 3 branches
BranchTrueFalseTotal
$parity eq 'n' ? : 0 0 0
$parity eq 'o' ? : 0 0 0
$parity eq 'e' ? : 0 0 0
795 $parity eq 'o' ? $cflag |= PARENB|PARODD :
796 $parity eq 'e' ? ($cflag |= PARENB) &= ~PARODD :
797 croak "Unrecognised parity '$parity'";
798  
799 0 $self->setcflag( $cflag );
800 }
801  
802 sub getstop
803 {
804 0 ▶ my $self = shift;
Subroutine getstop: not called CC=2 SCAR=17.9
805 0 ▶ return 2 if $self->getcflag & CSTOPB;
Branch: 1 branch
BranchTrueFalseTotal
if $self->getcflag & 64 0 0 0
806 0 return 1;
807 }
808  
809 sub setstop
810 {
811 0 ▶ my $self = shift;
Subroutine setstop: not called CC=3 SCAR=24.8
812 0 my ( $stop ) = @_;
813 0 my $cflag = $self->getcflag;
814  
815 0 ▶ $stop == 1 ? $cflag &= ~CSTOPB :
Branch: 2 branches
BranchTrueFalseTotal
$stop == 1 ? : 0 0 0
$stop == 2 ? : 0 0 0
816 $stop == 2 ? $cflag |= CSTOPB :
817 croak "Unrecognised stop '$stop'";
818  
819 0 $self->setcflag( $cflag );
820 }
821  
822 sub cfmakeraw
823 {
824 0 ▶ my $self = shift;
Subroutine cfmakeraw: not called CC=1 SCAR=6.9
825  
826 # Coped from bit manipulations in termios(3)
827 0 $self->setiflag( $self->getiflag & ~( IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON ) );
828 0 $self->setoflag( $self->getoflag & ~( OPOST ) );
829 0 $self->setlflag( $self->getlflag & ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN ) );
830 0 $self->setcflag( $self->getcflag & ~( CSIZE | PARENB ) | CS8 );
831 }
832  
833 =head1 TODO
834  
835 =over 4
836  
837 =item *
838  
839 Adding more getflag_*/setflag_* convenience wrappers
840  
841 =back
842  
843 =head1 SEE ALSO
844  
845 =over 4
846  
847 =item *
848  
849 L<IO::Tty> - Import Tty control constants
850  
851 =back
852  
853 =head1 AUTHOR
854  
855 Paul Evans <leonerd@leonerd.org.uk>
856  
857 =cut
858  
859 2 0x55AA;
↑ Summary Build »