| 1 | package LibTmux::Codec; | ||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use v5.44; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use LibTmux::X; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | sub format ($class, @tokens) { | ||||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 6 | 1 | for (@tokens) { | |||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | LibTmux::X::InvalidArgument->throw(message => 'invalid format token') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 8 | unless /\A[\@A-Za-z_][A-Za-z_0-9]*\z/; | ||||||||||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||||||||
| 10 | 1 | my $modifiers = | |||||||||||||||||||||||||||||||||
| 11 | 's|\\\\|\\\\\\\\|;s|' . "\t" . '|\\\\t|;s|' . "\n" . '|\\\\n|:'; | ||||||||||||||||||||||||||||||||||
| 12 | 1 | return join "\t", map { '#{' . $modifiers . $_ . '}' } @tokens; | |||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | sub decode ($class, $bytes, $count) { | ||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 16 | 15 | ▶ | LibTmux::X::Codec->throw(message => 'expected byte stream') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 17 | if utf8::is_utf8($bytes); | ||||||||||||||||||||||||||||||||||
| 18 | 15 | ▶ | return [] if $bytes eq ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 19 | 14 | ▶ | LibTmux::X::Codec->throw(message => 'missing final record newline') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 20 | unless $bytes =~ /\n\z/; | ||||||||||||||||||||||||||||||||||
| 21 | 13 | my @rows; | |||||||||||||||||||||||||||||||||
| 22 | 13 | my @frames = split /\n/, $bytes, -1; | |||||||||||||||||||||||||||||||||
| 23 | 13 | pop @frames; | |||||||||||||||||||||||||||||||||
| 24 | 13 | for my $row (@frames) { | |||||||||||||||||||||||||||||||||
| 25 | 14 | ▶ | my @fields = length($row) ? split(/\t/, $row, -1) : (''); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 26 | 14 | ▶ | LibTmux::X::Codec->throw(message => 'incorrect field count') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 27 | unless @fields == $count; | ||||||||||||||||||||||||||||||||||
| 28 | 13 | $_ = _unescape($_) for @fields; | |||||||||||||||||||||||||||||||||
| 29 | 8 | push @rows, \@fields; | |||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||
| 31 | 7 | return \@rows; | |||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 22 | ▶ | sub _unescape ($field, $extra = '') { | ||||||||||||||||||||||||||||||||
|
Subroutine
_unescape: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||
| 35 | 22 | my %escapes = ( | |||||||||||||||||||||||||||||||||
| 36 | '\\' => '\\', | ||||||||||||||||||||||||||||||||||
| 37 | n => "\n", | ||||||||||||||||||||||||||||||||||
| 38 | t => "\t", | ||||||||||||||||||||||||||||||||||
| 39 | a => "\a", | ||||||||||||||||||||||||||||||||||
| 40 | b => "\b", | ||||||||||||||||||||||||||||||||||
| 41 | f => "\f", | ||||||||||||||||||||||||||||||||||
| 42 | v => "\x0b", | ||||||||||||||||||||||||||||||||||
| 43 | r => "\r", | ||||||||||||||||||||||||||||||||||
| 44 | '$' => '$' | ||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||
| 46 | 22 | @escapes{split //, $extra} = split //, $extra; | |||||||||||||||||||||||||||||||||
| 47 | 22 | my $out = ''; | |||||||||||||||||||||||||||||||||
| 48 | 22 | ▶ | while (length $field) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 49 | 37 | ▶ | if ($field =~ s/\A([^\\]+)//s) { $out .= $1; next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | # Older tmux print paths add vis escapes after format expansion. | ||||||||||||||||||||||||||||||||||
| 52 | 20 | ▶ | if ($field =~ s/\A\\([0-3][0-7]{2})//) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 53 | 1 | $out .= chr(oct($1)); | |||||||||||||||||||||||||||||||||
| 54 | 1 | next; | |||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||
| 56 | LibTmux::X::Codec->throw( | ||||||||||||||||||||||||||||||||||
| 57 | message => 'unknown or incomplete field escape') | ||||||||||||||||||||||||||||||||||
| 58 | 19 | ▶ | unless $field =~ s/\A\\(.)//s && exists $escapes{$1}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field =~ s/\A\\(.)//su and exists $escapes{$1}
MC/DC100%2/2
$field =~ s/\A\\(.)//su and exists $escapes{$1}
$field =~ s/\A\\(.)//su
exists $escapes{$1}
Truth table100%3/3
A and B
A= $field =~ s/\A\\(.)//suB= exists $escapes{$1}
| |||||||||||||||||||||||||||||||||||
| 59 | 12 | $out .= $escapes{$1}; | |||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||
| 61 | 15 | return $out; | |||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | sub _quoted_value ($text, $option) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_quoted_value: called CC=12 SCAR=28.3
| |||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | # A command value starts with a verb and unquoted argument separator. | ||||||||||||||||||||||||||||||||||
| 67 | # String options are one args_escape token, quoted when they contain spaces. | ||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | return $text if $option && $text =~ /\A[A-Za-z][A-Za-z0-9_-]* /; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$option and $text =~ /\A[A-Za-z][A-Za-z0-9_-]* /u
MC/DC0%0/2
$option and $text =~ /\A[A-Za-z][A-Za-z0-9_-]* /u
$option
$text =~ /\A[A-Za-z][A-Za-z0-9_-]* /u
Truth table66%2/3
A and B
A= $optionB= $text =~ /\A[A-Za-z][A-Za-z0-9_-]* /u
| |||||||||||||||||||||||||||||||||||
| 69 | 5 | ▶ | if ($option && length($text)) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$option and length $text
MC/DC50%1/2
$option and length $text
$option
length $text
Truth table66%2/3
A and B
A= $optionB= length $text
| |||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | LibTmux::X::Codec->throw(message => 'malformed quoted option value') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 71 | unless $text =~ | ||||||||||||||||||||||||||||||||||
| 72 | /\A(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|((?:\\.|[^\x09-\x0d \\"'])+))\z/s; | ||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $text = $1 // $2 // $3; | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
($1 // $2) // $3
$1 // $2
MC/DC0%0/3
($1 // $2) // $3
$1
$2
$3
Truth table0%0/4
A // B // C
A= $1B= $2C= $3
| |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||
| 75 | elsif (!$option) { | ||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | LibTmux::X::Codec->throw( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 77 | message => 'malformed quoted environment value') | ||||||||||||||||||||||||||||||||||
| 78 | unless $text =~ /\A(?:\\.|[^"\\])*\z/s; | ||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | # Both native encoders escape these dollars. tmux 3.4 adds one more | ||||||||||||||||||||||||||||||||||
| 82 | # slash while printing; an even run identifies that additional layer. | ||||||||||||||||||||||||||||||||||
| 83 | 2 | $text =~ s!(\\+)\$([A-Za-z_{])! | |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | ('\\' x (length($1) % 2 ? length($1) : length($1) - 1)) . '$' . $2!ge; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | return _unescape($text, $option ? q{"'~#;{}%} : q{"`}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | sub _option_value ($class, $bytes, $name) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_option_value: called CC=9 SCAR=23.7
| |||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | return '' if $bytes eq ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | LibTmux::X::Codec->throw(message => 'missing option record newline') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | unless $bytes =~ s/\n\z//; | ||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | if ($name =~ /\A\@/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | # The unquoted name can itself contain spaces, newlines or vis escapes. | ||||||||||||||||||||||||||||||||||
| 95 | # Its value is the final complete args_escape token, not a second word. | ||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | LibTmux::X::Codec->throw(message => 'unexpected user option record') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | unless $bytes =~ | ||||||||||||||||||||||||||||||||||
| 98 | /\A\@.* ("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|(?:\\.|[^\x09-\x0d \\"'])+)\z/s; | ||||||||||||||||||||||||||||||||||
| 99 | 1 | return _quoted_value($1, true); | |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | 2 | my @values; | |||||||||||||||||||||||||||||||||
| 102 | 2 | pos($bytes) = 0; | |||||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | while (pos($bytes) < length($bytes)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | LibTmux::X::Codec->throw(message => 'malformed option record') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 105 | unless $bytes =~ | ||||||||||||||||||||||||||||||||||
| 106 | /\G([a-z][a-z0-9-]*)(?:\[[^\]]+\])?\*?(?: ([^\n]*))?(?:\n|\z)/gc; | ||||||||||||||||||||||||||||||||||
| 107 | 2 | my ($resolved, $value) = ($1, $2); | |||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | # These OPTIONS_TABLE_KEY values use key_string_lookup_key, not | ||||||||||||||||||||||||||||||||||
| 111 | # args_escape. Use the resolved name even for abbreviated requests. | ||||||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | push @values, $resolved =~ /\A(?:backspace|prefix|prefix2)\z/ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | ? $value | ||||||||||||||||||||||||||||||||||
| 114 | : _quoted_value($value, true); | ||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | 0 | return join "\n", @values; | |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | sub _environment_value ($class, $bytes, $name) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_environment_value: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||||||||
| 120 | 3 | ▶ | return undef if $bytes eq '' || $bytes eq "unset $name;\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$bytes eq "" or $bytes eq "unset $name;\n"
MC/DC0%0/2
$bytes eq "" or $bytes eq "unset $name;\n"
$bytes eq ""
$bytes eq "unset $name;\n"
Truth table33%1/3
A or B
A= $bytes eq ""B= $bytes eq "unset $name;\n"
| |||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | LibTmux::X::Codec->throw(message => 'malformed environment record') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 122 | unless $bytes =~ /\A\Q$name\E="(.*)"; export \Q$name\E;\n\z/s; | ||||||||||||||||||||||||||||||||||
| 123 | 2 | return _quoted_value($1, false); | |||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | LibTmux::Codec - Reversible single-lookup tmux format records | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | Backslash, tab, and newline are escaped by tmux substitution modifiers. | ||||||||||||||||||||||||||||||||||
| 133 | Decoding composes those escapes with older tmux print escapes in one pass, | ||||||||||||||||||||||||||||||||||
| 134 | returning octets. Use this decoder only with L</format> output from a UTF-8 | ||||||||||||||||||||||||||||||||||
| 135 | command client; raw command output has a separate contract. An empty stream | ||||||||||||||||||||||||||||||||||
| 136 | has no rows; one newline is one empty single-field record. Malformed streams | ||||||||||||||||||||||||||||||||||
| 137 | throw L<LibTmux::X::Codec|LibTmux::X/Codec>. | ||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | Private readers for L<LibTmux::Server/get_option> and | ||||||||||||||||||||||||||||||||||
| 140 | L<LibTmux::Server/get_environment> decode tmux's native quoted values without | ||||||||||||||||||||||||||||||||||
| 141 | evaluating shell text. They do not change L<LibTmux::Server/cmd> output. | ||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | =head2 format | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | C<LibTmux::Codec-E<gt>format(@tokens)> returns one tmux format string in token | ||||||||||||||||||||||||||||||||||
| 148 | order without I/O. Tokens must be registered-style names, including user option | ||||||||||||||||||||||||||||||||||
| 149 | names beginning with C<@>; invalid names throw | ||||||||||||||||||||||||||||||||||
| 150 | L<LibTmux::X::InvalidArgument|LibTmux::X/InvalidArgument>. No input is mutated. | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | =head2 decode | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | C<LibTmux::Codec-E<gt>decode($bytes, $field_count)> returns an arrayref of row | ||||||||||||||||||||||||||||||||||
| 155 | arrayrefs in stream order, including empty fields and duplicate rows. It | ||||||||||||||||||||||||||||||||||
| 156 | performs no I/O and does not mutate the input. An empty byte stream returns | ||||||||||||||||||||||||||||||||||
| 157 | C<[]>; malformed framing throws L<LibTmux::X::Codec|LibTmux::X/Codec>. | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||||||||