| 1 | package Acme::Claude::Shell::Tools; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use 5.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | our @EXPORT_OK = qw(shell_tools); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Claude::Agent qw(tool); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Claude::Agent::CLI qw(menu status ask_yn prompt start_spinner stop_spinner); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use IO::Async::Process; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Cwd qw(abs_path getcwd); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use Term::ANSIColor qw(colored); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub shell_tools { | ||||||||||||||||||||||||||
|
Subroutine
shell_tools: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 1 | my ($session) = @_; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | return [ | ||||||||||||||||||||||||||||
| 22 | tool( | ||||||||||||||||||||||||||||
| 23 | 'execute_command', | ||||||||||||||||||||||||||||
| 24 | 'Execute a shell command and return its output. Use this for ALL shell operations including listing files, reading files, etc. The user will be prompted to approve each command.', | ||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | type => 'object', | ||||||||||||||||||||||||||||
| 27 | properties => { | ||||||||||||||||||||||||||||
| 28 | command => { | ||||||||||||||||||||||||||||
| 29 | type => 'string', | ||||||||||||||||||||||||||||
| 30 | description => 'The shell command to execute (e.g., "ls -la", "cat file.txt", "find . -name *.pl")', | ||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | working_dir => { | ||||||||||||||||||||||||||||
| 33 | type => 'string', | ||||||||||||||||||||||||||||
| 34 | description => 'Directory to run command in (optional, defaults to current directory)', | ||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | required => ['command'], | ||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 0 | my ($params, $loop) = @_; | |||||||||||||||||||||||||||
| 41 | 0 | return _execute_command($session, $params, $loop); | |||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | ), | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | tool( | ||||||||||||||||||||||||||||
| 46 | 'get_working_directory', | ||||||||||||||||||||||||||||
| 47 | 'Get the current working directory. This is safe and does not require user confirmation.', | ||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||
| 49 | type => 'object', | ||||||||||||||||||||||||||||
| 50 | properties => {}, | ||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 1 | my ($params, $loop) = @_; | |||||||||||||||||||||||||||
| 54 | 1 | my $future = $loop->new_future; | |||||||||||||||||||||||||||
| 55 | 1 | $future->done(_mcp_result(getcwd())); | |||||||||||||||||||||||||||
| 56 | 1 | return $future; | |||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | ), | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | tool( | ||||||||||||||||||||||||||||
| 61 | 'read_file', | ||||||||||||||||||||||||||||
| 62 | 'Read the contents of a file. Safe operation - does not require user confirmation. Use this instead of execute_command for reading files.', | ||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||
| 64 | type => 'object', | ||||||||||||||||||||||||||||
| 65 | properties => { | ||||||||||||||||||||||||||||
| 66 | path => { | ||||||||||||||||||||||||||||
| 67 | type => 'string', | ||||||||||||||||||||||||||||
| 68 | description => 'Path to the file to read', | ||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||
| 70 | lines => { | ||||||||||||||||||||||||||||
| 71 | type => 'integer', | ||||||||||||||||||||||||||||
| 72 | description => 'Number of lines to read from the beginning (optional)', | ||||||||||||||||||||||||||||
| 73 | }, | ||||||||||||||||||||||||||||
| 74 | tail => { | ||||||||||||||||||||||||||||
| 75 | type => 'integer', | ||||||||||||||||||||||||||||
| 76 | description => 'Number of lines to read from the end (optional)', | ||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||
| 79 | required => ['path'], | ||||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||||
| 81 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 3 | my ($params, $loop) = @_; | |||||||||||||||||||||||||||
| 83 | 3 | return _read_file_safe($session, $params, $loop); | |||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | ), | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | tool( | ||||||||||||||||||||||||||||
| 88 | 'list_directory', | ||||||||||||||||||||||||||||
| 89 | 'List the contents of a directory. Safe operation - does not require user confirmation. Use this instead of execute_command for listing files.', | ||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | type => 'object', | ||||||||||||||||||||||||||||
| 92 | properties => { | ||||||||||||||||||||||||||||
| 93 | path => { | ||||||||||||||||||||||||||||
| 94 | type => 'string', | ||||||||||||||||||||||||||||
| 95 | description => 'Path to the directory to list (defaults to current directory)', | ||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||
| 97 | pattern => { | ||||||||||||||||||||||||||||
| 98 | type => 'string', | ||||||||||||||||||||||||||||
| 99 | description => 'Glob pattern to filter files (e.g., "*.pl", "*.txt")', | ||||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||
| 101 | long_format => { | ||||||||||||||||||||||||||||
| 102 | type => 'boolean', | ||||||||||||||||||||||||||||
| 103 | description => 'Show detailed file information (size, date, permissions)', | ||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||
| 105 | show_hidden => { | ||||||||||||||||||||||||||||
| 106 | type => 'boolean', | ||||||||||||||||||||||||||||
| 107 | description => 'Include hidden files (starting with .)', | ||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||||||||||
| 110 | }, | ||||||||||||||||||||||||||||
| 111 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 2 | my ($params, $loop) = @_; | |||||||||||||||||||||||||||
| 113 | 2 | return _list_directory_safe($session, $params, $loop); | |||||||||||||||||||||||||||
| 114 | }, | ||||||||||||||||||||||||||||
| 115 | ), | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | tool( | ||||||||||||||||||||||||||||
| 118 | 'search_files', | ||||||||||||||||||||||||||||
| 119 | 'Search for files by name pattern or content. Safe operation - does not require user confirmation.', | ||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | type => 'object', | ||||||||||||||||||||||||||||
| 122 | properties => { | ||||||||||||||||||||||||||||
| 123 | pattern => { | ||||||||||||||||||||||||||||
| 124 | type => 'string', | ||||||||||||||||||||||||||||
| 125 | description => 'File name pattern to search for (e.g., "*.pm", "config*")', | ||||||||||||||||||||||||||||
| 126 | }, | ||||||||||||||||||||||||||||
| 127 | content => { | ||||||||||||||||||||||||||||
| 128 | type => 'string', | ||||||||||||||||||||||||||||
| 129 | description => 'Text pattern to search for within files (grep)', | ||||||||||||||||||||||||||||
| 130 | }, | ||||||||||||||||||||||||||||
| 131 | path => { | ||||||||||||||||||||||||||||
| 132 | type => 'string', | ||||||||||||||||||||||||||||
| 133 | description => 'Directory to search in (defaults to current directory)', | ||||||||||||||||||||||||||||
| 134 | }, | ||||||||||||||||||||||||||||
| 135 | max_depth => { | ||||||||||||||||||||||||||||
| 136 | type => 'integer', | ||||||||||||||||||||||||||||
| 137 | description => 'Maximum directory depth to search', | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||||||||
| 141 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | 2 | my ($params, $loop) = @_; | |||||||||||||||||||||||||||
| 143 | 2 | return _search_files_safe($session, $params, $loop); | |||||||||||||||||||||||||||
| 144 | }, | ||||||||||||||||||||||||||||
| 145 | ), | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | tool( | ||||||||||||||||||||||||||||
| 148 | 'get_system_info', | ||||||||||||||||||||||||||||
| 149 | 'Get system information including OS, disk space, and memory. Safe operation - does not require user confirmation.', | ||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | type => 'object', | ||||||||||||||||||||||||||||
| 152 | properties => { | ||||||||||||||||||||||||||||
| 153 | info_type => { | ||||||||||||||||||||||||||||
| 154 | type => 'string', | ||||||||||||||||||||||||||||
| 155 | description => 'Type of info: "all", "os", "disk", "memory", "processes" (defaults to "all")', | ||||||||||||||||||||||||||||
| 156 | enum => ['all', 'os', 'disk', 'memory', 'processes'], | ||||||||||||||||||||||||||||
| 157 | }, | ||||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | }, | ||||||||||||||||||||||||||||
| 160 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 2 | my ($params, $loop) = @_; | |||||||||||||||||||||||||||
| 162 | 2 | return _get_system_info($session, $params, $loop); | |||||||||||||||||||||||||||
| 163 | }, | ||||||||||||||||||||||||||||
| 164 | 1 | ), | |||||||||||||||||||||||||||
| 165 | ]; | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub _execute_command { | ||||||||||||||||||||||||||
|
Subroutine
_execute_command: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 169 | 0 | my ($session, $params, $loop) = @_; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | my $command = $params->{command}; | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | my $dir = $params->{working_dir} // $session->working_dir; | ||||||||||||||||||||||||||
|
Condition0%0/3
$params->{"working_dir"} // $session->working_dir
MC/DC0%0/2
$params->{"working_dir"} // $session->working_dir
$params->{"working_dir"}
$session->working_dir
Truth table0%0/3
A // B
A= $params->{"working_dir"}B= $session->working_dir
| |||||||||||||||||||||||||||||
| 173 | 0 | my $colorful = $session->colorful; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ($session->can('_spinner') && $session->_spinner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$session->can("_spinner") and $session->_spinner
MC/DC0%0/2
$session->can("_spinner") and $session->_spinner
$session->can("_spinner")
$session->_spinner
Truth table0%0/3
A and B
A= $session->can("_spinner")B= $session->_spinner
| |||||||||||||||||||||||||||||
| 176 | 0 | stop_spinner($session->_spinner); | |||||||||||||||||||||||||||
| 177 | 0 | $session->_spinner(undef); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my ($approved, $new_command) = _confirm_command($session, $command); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | unless ($approved) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | my $future = $loop->new_future; | |||||||||||||||||||||||||||
| 184 | 0 | $future->done(_mcp_result("User cancelled command", 1)); | |||||||||||||||||||||||||||
| 185 | 0 | return $future; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | $command = $new_command if defined $new_command; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | $session->_spinner(start_spinner("Executing...", $loop)); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | push @{$session->_history}, { | |||||||||||||||||||||||||||
| 195 | time => _timestamp(), | ||||||||||||||||||||||||||||
| 196 | command => $command, | ||||||||||||||||||||||||||||
| 197 | status => 'running', | ||||||||||||||||||||||||||||
| 198 | }; | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | my $future = $loop->new_future; | |||||||||||||||||||||||||||
| 201 | 0 | my $stdout = ''; | |||||||||||||||||||||||||||
| 202 | 0 | my $stderr = ''; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | my $process = IO::Async::Process->new( | ||||||||||||||||||||||||||||
| 205 | command => [ '/bin/sh', '-c', $command ], | ||||||||||||||||||||||||||||
| 206 | ($dir && -d $dir ? (setup => [ chdir => $dir ]) : ()), | ||||||||||||||||||||||||||||
| 207 | stdout => { | ||||||||||||||||||||||||||||
| 208 | into => \$stdout, | ||||||||||||||||||||||||||||
| 209 | }, | ||||||||||||||||||||||||||||
| 210 | stderr => { | ||||||||||||||||||||||||||||
| 211 | into => \$stderr, | ||||||||||||||||||||||||||||
| 212 | }, | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | on_finish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 214 | 0 | my ($self, $exitcode) = @_; | |||||||||||||||||||||||||||
| 215 | 0 | my $exit_status = $exitcode >> 8; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($exit_status != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | $session->_history->[-1]{status} = "exit $exit_status"; | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | my $output = $stderr || $stdout || "Command failed with exit code $exit_status"; | ||||||||||||||||||||||||||
|
Condition0%0/5
$stderr || $stdout || "Command failed with exit code $exit_status"
$stderr or $stdout
MC/DC0%0/2
$stderr || $stdout || "Command failed with exit code $exit_status"
$stderr
$stdout
Truth table0%0/3
A or B
A= $stderrB= $stdout
| |||||||||||||||||||||||||||||
| 220 | 0 | $future->done(_mcp_result($output)); | |||||||||||||||||||||||||||
| 221 | } else { | ||||||||||||||||||||||||||||
| 222 | 0 | $session->_history->[-1]{status} = 'success'; | |||||||||||||||||||||||||||
| 223 | 0 | ▶ | $future->done(_mcp_result($stdout // '')); | ||||||||||||||||||||||||||
|
Condition0%0/2
$stdout // ""
MC/DC0%0/1
$stdout // ""
$stdout
Truth table0%0/2
A
A= $stdout
| |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | }, | ||||||||||||||||||||||||||||
| 226 | 0 | ▶ | on_exception => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 227 | 0 | my ($self, $exception, $errno, $exitcode) = @_; | |||||||||||||||||||||||||||
| 228 | 0 | $session->_history->[-1]{status} = 'error'; | |||||||||||||||||||||||||||
| 229 | 0 | $future->done(_mcp_result("Error: $exception", 1)); | |||||||||||||||||||||||||||
| 230 | }, | ||||||||||||||||||||||||||||
| 231 | 0 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dir and -d $dir
MC/DC0%0/2
$dir and -d $dir
$dir
-d $dir
Truth table0%0/3
A and B
A= $dirB= -d $dir
| |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | $loop->add($process); | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | return $future; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 10 | ▶ | sub _mcp_result { | ||||||||||||||||||||||||||
|
Subroutine
_mcp_result: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 239 | 10 | my ($text, $is_error) = @_; | |||||||||||||||||||||||||||
| 240 | return { | ||||||||||||||||||||||||||||
| 241 | 10 | ▶ | content => [{ type => 'text', text => $text }], | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | is_error => $is_error ? 1 : 0, | ||||||||||||||||||||||||||||
| 243 | }; | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 9 | my @DANGEROUS_PATTERNS = ( | |||||||||||||||||||||||||||
| 247 | { pattern => qr/\brm\s+(-[rf]+|--recursive|--force)/i, | ||||||||||||||||||||||||||||
| 248 | reason => 'Recursive or forced file deletion' }, | ||||||||||||||||||||||||||||
| 249 | { pattern => qr/\bsudo\b/, | ||||||||||||||||||||||||||||
| 250 | reason => 'Superuser command' }, | ||||||||||||||||||||||||||||
| 251 | { pattern => qr/\bmkfs\b/, | ||||||||||||||||||||||||||||
| 252 | reason => 'Filesystem formatting' }, | ||||||||||||||||||||||||||||
| 253 | { pattern => qr/\bdd\b.*\bof=/, | ||||||||||||||||||||||||||||
| 254 | reason => 'Direct disk write' }, | ||||||||||||||||||||||||||||
| 255 | { pattern => qr/>\s*\/dev\//, | ||||||||||||||||||||||||||||
| 256 | reason => 'Writing to device file' }, | ||||||||||||||||||||||||||||
| 257 | { pattern => qr/\bchmod\s+(-R\s+)?777\b/, | ||||||||||||||||||||||||||||
| 258 | reason => 'World-writable permissions' }, | ||||||||||||||||||||||||||||
| 259 | { pattern => qr/\bchown\s+-R\b.*\//, | ||||||||||||||||||||||||||||
| 260 | reason => 'Recursive ownership change' }, | ||||||||||||||||||||||||||||
| 261 | { pattern => qr/\bkill\s+-9\b/, | ||||||||||||||||||||||||||||
| 262 | reason => 'Forceful process termination' }, | ||||||||||||||||||||||||||||
| 263 | { pattern => qr/\b(reboot|shutdown|halt|poweroff)\b/, | ||||||||||||||||||||||||||||
| 264 | reason => 'System shutdown/reboot' }, | ||||||||||||||||||||||||||||
| 265 | { pattern => qr/\bformat\b/, | ||||||||||||||||||||||||||||
| 266 | reason => 'Disk formatting' }, | ||||||||||||||||||||||||||||
| 267 | { pattern => qr/:\s*\(\s*\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;/, | ||||||||||||||||||||||||||||
| 268 | reason => 'Fork bomb detected' }, | ||||||||||||||||||||||||||||
| 269 | { pattern => qr/\bwget\b.*\|\s*(ba)?sh/i, | ||||||||||||||||||||||||||||
| 270 | reason => 'Piping remote script to shell' }, | ||||||||||||||||||||||||||||
| 271 | { pattern => qr/\bcurl\b.*\|\s*(ba)?sh/i, | ||||||||||||||||||||||||||||
| 272 | reason => 'Piping remote script to shell' }, | ||||||||||||||||||||||||||||
| 273 | ); | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | sub _check_dangerous { | ||||||||||||||||||||||||||
|
Subroutine
_check_dangerous: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 276 | 0 | my ($command) = @_; | |||||||||||||||||||||||||||
| 277 | 0 | for my $check (@DANGEROUS_PATTERNS) { | |||||||||||||||||||||||||||
| 278 | 0 | ▶ | if ($command =~ $check->{pattern}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 0 | return $check; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | 0 | return undef; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | sub _choose_action { | ||||||||||||||||||||||||||
|
Subroutine
_choose_action: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 286 | 0 | my ($colorful) = @_; | |||||||||||||||||||||||||||
| 287 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 0 | ▶ | return menu("Action", [ | ||||||||||||||||||||||||||
|
Condition0%0/2
menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}]) // "x"
MC/DC0%0/1
menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}]) // "x"
menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}])
Truth table0%0/2
A
A= menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}])
| |||||||||||||||||||||||||||||
| 289 | { key => 'a', label => 'Approve and run' }, | ||||||||||||||||||||||||||||
| 290 | { key => 'd', label => 'Dry-run (preview only)' }, | ||||||||||||||||||||||||||||
| 291 | { key => 'e', label => 'Edit command' }, | ||||||||||||||||||||||||||||
| 292 | { key => 'x', label => 'Cancel' }, | ||||||||||||||||||||||||||||
| 293 | ]) // 'x'; | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 0 | print "Action: [a]pprove, [d]ry-run, [e]dit, [x] cancel > "; | |||||||||||||||||||||||||||
| 296 | 0 | my $answer = <STDIN>; | |||||||||||||||||||||||||||
| 297 | 0 | ▶ | return 'x' unless defined $answer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | $answer =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 299 | 0 | ▶ | return $answer =~ /^[adex]$/i ? lc $answer : 'x'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 0 | ▶ | sub _confirm_command { | ||||||||||||||||||||||||||
|
Subroutine
_confirm_command: not called CC=28 SCAR=67.0
| |||||||||||||||||||||||||||||
| 303 | 0 | my ($session, $command) = @_; | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 0 | my $colorful = $session->colorful; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 0 | my $danger = _check_dangerous($command); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | if ($danger && $session->safe_mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$danger and $session->safe_mode
MC/DC0%0/2
$danger and $session->safe_mode
$danger
$session->safe_mode
Truth table0%0/3
A and B
A= $dangerB= $session->safe_mode
| |||||||||||||||||||||||||||||
| 312 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 0 | status('warning', "Potentially dangerous command detected!"); | |||||||||||||||||||||||||||
| 314 | 0 | print colored(['yellow'], " Reason: $danger->{reason}\n"); | |||||||||||||||||||||||||||
| 315 | } else { | ||||||||||||||||||||||||||||
| 316 | 0 | print "WARNING: Potentially dangerous command!\n"; | |||||||||||||||||||||||||||
| 317 | 0 | print " Reason: $danger->{reason}\n"; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 0 | status('info', "Command: $command"); | |||||||||||||||||||||||||||
| 324 | } else { | ||||||||||||||||||||||||||||
| 325 | 0 | print "Command: $command\n"; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 0 | my $choice = _choose_action($colorful); | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 0 | ▶ | if ($choice eq 'x') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 331 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | 0 | status('warning', "Command cancelled"); | |||||||||||||||||||||||||||
| 333 | } else { | ||||||||||||||||||||||||||||
| 334 | 0 | print "Cancelled.\n"; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | 0 | return (0, undef); | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | elsif ($choice eq 'd') { | ||||||||||||||||||||||||||||
| 339 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 0 | status('info', "[DRY-RUN] Would execute:"); | |||||||||||||||||||||||||||
| 341 | 0 | print colored(['cyan'], " $command\n\n"); | |||||||||||||||||||||||||||
| 342 | } else { | ||||||||||||||||||||||||||||
| 343 | 0 | print "[DRY-RUN] Would execute: $command\n"; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | 0 | return (0, undef); | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | elsif ($choice eq 'e') { | ||||||||||||||||||||||||||||
| 348 | 0 | my $new_cmd; | |||||||||||||||||||||||||||
| 349 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 0 | $new_cmd = prompt("Edit command:", $command); | |||||||||||||||||||||||||||
| 351 | } else { | ||||||||||||||||||||||||||||
| 352 | 0 | print "Edit command [$command]: "; | |||||||||||||||||||||||||||
| 353 | 0 | $new_cmd = <STDIN>; | |||||||||||||||||||||||||||
| 354 | 0 | ▶ | chomp $new_cmd if defined $new_cmd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 0 | ▶ | $new_cmd = $command unless length($new_cmd // ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$new_cmd // ""
MC/DC0%0/1
$new_cmd // ""
$new_cmd
Truth table0%0/2
A
A= $new_cmd
| |||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 0 | status('info', "Modified command:"); | |||||||||||||||||||||||||||
| 360 | 0 | print colored(['bold', 'white'], " $new_cmd\n\n"); | |||||||||||||||||||||||||||
| 361 | } else { | ||||||||||||||||||||||||||||
| 362 | 0 | print "Modified: $new_cmd\n"; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | if (_check_dangerous($new_cmd) && $session->safe_mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
_check_dangerous($new_cmd) and $session->safe_mode
MC/DC0%0/2
_check_dangerous($new_cmd) and $session->safe_mode
_check_dangerous($new_cmd)
$session->safe_mode
Truth table0%0/3
A and B
A= _check_dangerous($new_cmd)B= $session->safe_mode
| |||||||||||||||||||||||||||||
| 366 | 0 | my $confirmed; | |||||||||||||||||||||||||||
| 367 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 0 | $confirmed = ask_yn("Are you SURE you want to run this command?", 'n'); | |||||||||||||||||||||||||||
| 369 | } else { | ||||||||||||||||||||||||||||
| 370 | 0 | print "Are you SURE? (y/N): "; | |||||||||||||||||||||||||||
| 371 | 0 | my $ans = <STDIN>; | |||||||||||||||||||||||||||
| 372 | 0 | ▶ | chomp $ans if defined $ans; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 0 | ▶ | $confirmed = ($ans // '') =~ /^y/i; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ans // ""
MC/DC0%0/1
$ans // ""
$ans
Truth table0%0/2
A
A= $ans
| |||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | 0 | ▶ | return (0, undef) unless $confirmed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 0 | return (1, $new_cmd); | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 0 | ▶ | if ($danger && $session->safe_mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$danger and $session->safe_mode
MC/DC0%0/2
$danger and $session->safe_mode
$danger
$session->safe_mode
Truth table0%0/3
A and B
A= $dangerB= $session->safe_mode
| |||||||||||||||||||||||||||||
| 382 | 0 | my $confirmed; | |||||||||||||||||||||||||||
| 383 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | $confirmed = ask_yn("Are you SURE you want to run this dangerous command?", 'n'); | |||||||||||||||||||||||||||
| 385 | } else { | ||||||||||||||||||||||||||||
| 386 | 0 | print "Are you SURE? (y/N): "; | |||||||||||||||||||||||||||
| 387 | 0 | my $ans = <STDIN>; | |||||||||||||||||||||||||||
| 388 | 0 | ▶ | chomp $ans if defined $ans; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 0 | ▶ | $confirmed = ($ans // '') =~ /^y/i; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ans // ""
MC/DC0%0/1
$ans // ""
$ans
Truth table0%0/2
A
A= $ans
| |||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 0 | ▶ | unless ($confirmed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | if ($colorful) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 0 | status('warning', "Command cancelled"); | |||||||||||||||||||||||||||
| 395 | } else { | ||||||||||||||||||||||||||||
| 396 | 0 | print "Cancelled.\n"; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | 0 | return (0, undef); | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | return (1, undef); | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub _list_files { | ||||||||||||||||||||||||||
|
Subroutine
_list_files: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 406 | 0 | my ($session, $params, $loop) = @_; | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 0 | ▶ | my $path = $params->{path} // '.'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params->{"path"} // "."
MC/DC0%0/1
$params->{"path"} // "."
$params->{"path"}
Truth table0%0/2
A
A= $params->{"path"}
| |||||||||||||||||||||||||||||
| 409 | 0 | ▶ | my $pattern = $params->{pattern} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params->{"pattern"} // ""
MC/DC0%0/1
$params->{"pattern"} // ""
$params->{"pattern"}
Truth table0%0/2
A
A= $params->{"pattern"}
| |||||||||||||||||||||||||||||
| 410 | 0 | ▶ | my $long = $params->{long_format} // 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params->{"long_format"} // 1
MC/DC0%0/1
$params->{"long_format"} // 1
$params->{"long_format"}
Truth table0%0/2
A
A= $params->{"long_format"}
| |||||||||||||||||||||||||||||
| 411 | 0 | ▶ | my $hidden = $params->{hidden} // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params->{"hidden"} // 0
MC/DC0%0/1
$params->{"hidden"} // 0
$params->{"hidden"}
Truth table0%0/2
A
A= $params->{"hidden"}
| |||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 0 | my @opts; | |||||||||||||||||||||||||||
| 414 | 0 | ▶ | push @opts, '-l' if $long; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | 0 | ▶ | push @opts, '-a' if $hidden; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 0 | ▶ | my $target = $pattern ? "$path/$pattern" : $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | 0 | my $cmd = "ls @opts $target 2>/dev/null || ls @opts $path"; | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | return _execute_command($session, { command => $cmd }, $loop); | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | ▶ | sub _read_file { | ||||||||||||||||||||||||||
|
Subroutine
_read_file: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 424 | 0 | my ($session, $params, $loop) = @_; | |||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 0 | my $path = $params->{path}; | |||||||||||||||||||||||||||
| 427 | 0 | my $lines = $params->{lines}; | |||||||||||||||||||||||||||
| 428 | 0 | my $tail = $params->{tail}; | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 0 | my $cmd; | |||||||||||||||||||||||||||
| 431 | 0 | ▶ | if ($tail) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 432 | 0 | $cmd = "tail -n $tail " . _shell_quote($path); | |||||||||||||||||||||||||||
| 433 | } elsif ($lines) { | ||||||||||||||||||||||||||||
| 434 | 0 | $cmd = "head -n $lines " . _shell_quote($path); | |||||||||||||||||||||||||||
| 435 | } else { | ||||||||||||||||||||||||||||
| 436 | 0 | $cmd = "cat " . _shell_quote($path); | |||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 0 | return _execute_command($session, { command => $cmd }, $loop); | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | ▶ | sub _shell_quote { | ||||||||||||||||||||||||||
|
Subroutine
_shell_quote: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 443 | 0 | my ($str) = @_; | |||||||||||||||||||||||||||
| 444 | 0 | $str =~ s/'/'\\''/g; | |||||||||||||||||||||||||||
| 445 | 0 | return "'$str'"; | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 0 | ▶ | sub _timestamp { | ||||||||||||||||||||||||||
|
Subroutine
_timestamp: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 449 | 0 | my @t = localtime; | |||||||||||||||||||||||||||
| 450 | 0 | return sprintf("%04d-%02d-%02d %02d:%02d:%02d", | |||||||||||||||||||||||||||
| 451 | $t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0]); | ||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 3 | ▶ | sub _read_file_safe { | ||||||||||||||||||||||||||
|
Subroutine
_read_file_safe: called CC=14 SCAR=32.7
| |||||||||||||||||||||||||||||
| 455 | 3 | my ($session, $params, $loop) = @_; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 3 | my $path = $params->{path}; | |||||||||||||||||||||||||||
| 458 | 3 | my $lines = $params->{lines}; | |||||||||||||||||||||||||||
| 459 | 3 | my $tail_lines = $params->{tail}; | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 3 | my $future = $loop->new_future; | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 3 | my $full_path = $path; | |||||||||||||||||||||||||||
| 464 | 3 | ▶ | if ($path !~ m{^/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 465 | 0 | ▶ | $full_path = File::Spec->catfile($session->working_dir // getcwd(), $path); | ||||||||||||||||||||||||||
|
Condition0%0/3
$session->working_dir // getcwd()
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir
getcwd()
Truth table0%0/3
A // B
A= $session->working_dirB= getcwd()
| |||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 3 | ▶ | unless (-e $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 469 | 1 | $future->done(_mcp_result("Error: File not found: $path", 1)); | |||||||||||||||||||||||||||
| 470 | 1 | return $future; | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | 2 | ▶ | unless (-r $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 474 | 0 | $future->done(_mcp_result("Error: Permission denied: $path", 1)); | |||||||||||||||||||||||||||
| 475 | 0 | return $future; | |||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 2 | ▶ | if (-d $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 479 | 0 | $future->done(_mcp_result("Error: Path is a directory: $path", 1)); | |||||||||||||||||||||||||||
| 480 | 0 | return $future; | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 2 | my $content; | |||||||||||||||||||||||||||
| 484 | 2 | ▶ | if (open my $fh, '<', $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 485 | 2 | ▶ | if ($lines) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 486 | 1 | my @lines; | |||||||||||||||||||||||||||
| 487 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | 2 | push @lines, $line; | |||||||||||||||||||||||||||
| 489 | 2 | ▶ | last if @lines >= $lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||
| 491 | 1 | $content = join('', @lines); | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | elsif ($tail_lines) { | ||||||||||||||||||||||||||||
| 494 | 0 | my @buffer; | |||||||||||||||||||||||||||
| 495 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 496 | 0 | push @buffer, $line; | |||||||||||||||||||||||||||
| 497 | 0 | ▶ | shift @buffer if @buffer > $tail_lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | 0 | $content = join('', @buffer); | |||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | else { | ||||||||||||||||||||||||||||
| 502 | 1 | local $/; | |||||||||||||||||||||||||||
| 503 | 1 | $content = <$fh>; | |||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | 2 | close $fh; | |||||||||||||||||||||||||||
| 506 | 2 | ▶ | $future->done(_mcp_result($content // '')); | ||||||||||||||||||||||||||
|
Condition50%1/2
$content // ""
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
| |||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | else { | ||||||||||||||||||||||||||||
| 509 | 0 | $future->done(_mcp_result("Error reading file: $!", 1)); | |||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 2 | return $future; | |||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | 2 | ▶ | sub _list_directory_safe { | ||||||||||||||||||||||||||
|
Subroutine
_list_directory_safe: called CC=13 SCAR=35.8
| |||||||||||||||||||||||||||||
| 516 | 2 | my ($session, $params, $loop) = @_; | |||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | 2 | ▶ | my $path = $params->{path} // '.'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"path"} // "."
MC/DC0%0/1
$params->{"path"} // "."
$params->{"path"}
Truth table50%1/2
A
A= $params->{"path"}
| |||||||||||||||||||||||||||||
| 519 | 2 | my $pattern = $params->{pattern}; | |||||||||||||||||||||||||||
| 520 | 2 | my $long_format = $params->{long_format}; | |||||||||||||||||||||||||||
| 521 | 2 | my $show_hidden = $params->{show_hidden}; | |||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | 2 | my $future = $loop->new_future; | |||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 2 | my $full_path = $path; | |||||||||||||||||||||||||||
| 526 | 2 | ▶ | if ($path !~ m{^/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | 0 | ▶ | $full_path = File::Spec->catfile($session->working_dir // getcwd(), $path); | ||||||||||||||||||||||||||
|
Condition0%0/3
$session->working_dir // getcwd()
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir
getcwd()
Truth table0%0/3
A // B
A= $session->working_dirB= getcwd()
| |||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | 2 | ▶ | unless (-d $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | 0 | $future->done(_mcp_result("Error: Not a directory: $path", 1)); | |||||||||||||||||||||||||||
| 532 | 0 | return $future; | |||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | 2 | ▶ | unless (opendir my $dh, $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 536 | 0 | $future->done(_mcp_result("Error: Cannot open directory: $!", 1)); | |||||||||||||||||||||||||||
| 537 | 0 | return $future; | |||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||
| 539 | else { | ||||||||||||||||||||||||||||
| 540 | 2 | my @entries = readdir($dh); | |||||||||||||||||||||||||||
| 541 | 2 | closedir $dh; | |||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | 2 | ▶ | unless ($show_hidden) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | 2 | @entries = grep { !/^\./ } @entries; | |||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||
| 546 | else { | ||||||||||||||||||||||||||||
| 547 | 0 | ▶ | @entries = grep { $_ ne '.' && $_ ne '..' } @entries; | ||||||||||||||||||||||||||
|
Condition0%0/3
$_ ne '.' && $_ ne '..'
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.'
$_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
| |||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | 2 | ▶ | if ($pattern) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 551 | 1 | my $regex = _glob_to_regex($pattern); | |||||||||||||||||||||||||||
| 552 | 1 | @entries = grep { /$regex/ } @entries; | |||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 2 | @entries = sort @entries; | |||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 2 | my @output; | |||||||||||||||||||||||||||
| 558 | 2 | ▶ | if ($long_format) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | 0 | for my $entry (@entries) { | |||||||||||||||||||||||||||
| 560 | 0 | my $entry_path = File::Spec->catfile($full_path, $entry); | |||||||||||||||||||||||||||
| 561 | 0 | my @stat = stat($entry_path); | |||||||||||||||||||||||||||
| 562 | 0 | ▶ | if (@stat) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | 0 | my $size = $stat[7]; | |||||||||||||||||||||||||||
| 564 | 0 | my $mtime = $stat[9]; | |||||||||||||||||||||||||||
| 565 | 0 | my $mode = $stat[2]; | |||||||||||||||||||||||||||
| 566 | 0 | ▶ | my $type = -d $entry_path ? 'd' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 567 | 0 | my $perms = _format_perms($mode); | |||||||||||||||||||||||||||
| 568 | 0 | my $date = _format_date($mtime); | |||||||||||||||||||||||||||
| 569 | 0 | push @output, sprintf("%s%s %8d %s %s", | |||||||||||||||||||||||||||
| 570 | $type, $perms, $size, $date, $entry); | ||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||
| 572 | else { | ||||||||||||||||||||||||||||
| 573 | 0 | push @output, $entry; | |||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | else { | ||||||||||||||||||||||||||||
| 578 | 2 | @output = @entries; | |||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | 2 | $future->done(_mcp_result(join("\n", @output))); | |||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 2 | return $future; | |||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | 2 | ▶ | sub _search_files_safe { | ||||||||||||||||||||||||||
|
Subroutine
_search_files_safe: called CC=10 SCAR=26.6
| |||||||||||||||||||||||||||||
| 588 | 2 | my ($session, $params, $loop) = @_; | |||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | 2 | my $pattern = $params->{pattern}; | |||||||||||||||||||||||||||
| 591 | 2 | my $content = $params->{content}; | |||||||||||||||||||||||||||
| 592 | 2 | ▶ | my $path = $params->{path} // '.'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"path"} // "."
MC/DC0%0/1
$params->{"path"} // "."
$params->{"path"}
Truth table50%1/2
A
A= $params->{"path"}
| |||||||||||||||||||||||||||||
| 593 | 2 | my $max_depth = $params->{max_depth}; | |||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | 2 | my $future = $loop->new_future; | |||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | 2 | ▶ | unless ($pattern || $content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pattern or $content
MC/DC0%0/2
$pattern or $content
$pattern
$content
Truth table66%2/3
A or B
A= $patternB= $content
| |||||||||||||||||||||||||||||
| 598 | 0 | $future->done(_mcp_result("Error: Must specify 'pattern' (filename) or 'content' (text search)", 1)); | |||||||||||||||||||||||||||
| 599 | 0 | return $future; | |||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | 2 | my $full_path = $path; | |||||||||||||||||||||||||||
| 603 | 2 | ▶ | if ($path !~ m{^/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 604 | 0 | ▶ | $full_path = File::Spec->catfile($session->working_dir // getcwd(), $path); | ||||||||||||||||||||||||||
|
Condition0%0/3
$session->working_dir // getcwd()
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir
getcwd()
Truth table0%0/3
A // B
A= $session->working_dirB= getcwd()
| |||||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | 2 | ▶ | unless (-d $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 608 | 0 | $future->done(_mcp_result("Error: Not a directory: $path", 1)); | |||||||||||||||||||||||||||
| 609 | 0 | return $future; | |||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | 2 | my @results; | |||||||||||||||||||||||||||
| 613 | 2 | ▶ | my $regex = $pattern ? _glob_to_regex($pattern) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 614 | 2 | ▶ | my $content_re = $content ? qr/\Q$content\E/i : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | 2 | _search_recursive($full_path, $full_path, $regex, $content_re, $max_depth, 0, \@results); | |||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 2 | ▶ | if (@results) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 619 | 2 | $future->done(_mcp_result(join("\n", @results))); | |||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||
| 621 | else { | ||||||||||||||||||||||||||||
| 622 | 0 | $future->done(_mcp_result("No matches found")); | |||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | 2 | return $future; | |||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | 4 | ▶ | sub _search_recursive { | ||||||||||||||||||||||||||
|
Subroutine
_search_recursive: called CC=19 SCAR=30.1
| |||||||||||||||||||||||||||||
| 629 | 4 | my ($base, $dir, $name_re, $content_re, $max_depth, $depth, $results) = @_; | |||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | 4 | ▶ | return if defined $max_depth && $depth > $max_depth; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $max_depth and $depth > $max_depth
MC/DC0%0/2
defined $max_depth and $depth > $max_depth
defined $max_depth
$depth > $max_depth
Truth table33%1/3
A and B
A= defined $max_depthB= $depth > $max_depth
| |||||||||||||||||||||||||||||
| 632 | 4 | ▶ | return if @$results >= 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | 4 | ▶ | opendir my $dh, $dir or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 635 | 4 | my @entries = readdir($dh); | |||||||||||||||||||||||||||
| 636 | 4 | closedir $dh; | |||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | 4 | for my $entry (sort @entries) { | |||||||||||||||||||||||||||
| 639 | 16 | ▶ | next if $entry eq '.' || $entry eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$entry eq "." or $entry eq ".."
MC/DC100%2/2
$entry eq "." or $entry eq ".."
$entry eq "."
$entry eq ".."
Truth table100%3/3
A or B
A= $entry eq "."B= $entry eq ".."
| |||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | 8 | my $path = File::Spec->catfile($dir, $entry); | |||||||||||||||||||||||||||
| 642 | 8 | my $rel_path = File::Spec->abs2rel($path, $base); | |||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | 8 | ▶ | if (-d $path) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 645 | 2 | _search_recursive($base, $path, $name_re, $content_re, $max_depth, $depth + 1, $results); | |||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||
| 647 | elsif (-f $path) { | ||||||||||||||||||||||||||||
| 648 | 6 | ▶ | my $name_match = !$name_re || $entry =~ $name_re; | ||||||||||||||||||||||||||
|
Condition100%3/3
!$name_re || $entry =~ /$name_re/u
MC/DC100%2/2
!$name_re || $entry =~ /$name_re/u
!$name_re
$entry =~ /$name_re/u
Truth table100%3/3
not(A) || B
A= !$name_reB= $entry =~ /$name_re/u
| |||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | 6 | ▶ | if ($name_match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 651 | 5 | ▶ | if ($content_re) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 652 | 3 | ▶ | if (open my $fh, '<', $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 653 | 3 | my $line_num = 0; | |||||||||||||||||||||||||||
| 654 | 3 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 655 | 9 | $line_num++; | |||||||||||||||||||||||||||
| 656 | 9 | ▶ | if ($line =~ $content_re) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 657 | 1 | chomp $line; | |||||||||||||||||||||||||||
| 658 | 1 | ▶ | $line = substr($line, 0, 100) . '...' if length($line) > 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 659 | 1 | push @$results, "$rel_path:$line_num: $line"; | |||||||||||||||||||||||||||
| 660 | 1 | ▶ | last if @$results >= 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||
| 663 | 3 | close $fh; | |||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||
| 666 | else { | ||||||||||||||||||||||||||||
| 667 | 2 | push @$results, $rel_path; | |||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||
| 669 | } | ||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | 8 | ▶ | last if @$results >= 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | 2 | ▶ | sub _get_system_info { | ||||||||||||||||||||||||||
|
Subroutine
_get_system_info: called CC=20 SCAR=31.2
| |||||||||||||||||||||||||||||
| 677 | 2 | my ($session, $params, $loop) = @_; | |||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | 2 | ▶ | my $info_type = $params->{info_type} // 'all'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"info_type"} // "all"
MC/DC0%0/1
$params->{"info_type"} // "all"
$params->{"info_type"}
Truth table50%1/2
A
A= $params->{"info_type"}
| |||||||||||||||||||||||||||||
| 680 | 2 | my $future = $loop->new_future; | |||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | 2 | my @info; | |||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | 2 | ▶ | if ($info_type eq 'all' || $info_type eq 'os') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info_type eq "all" or $info_type eq "os"
MC/DC0%0/2
$info_type eq "all" or $info_type eq "os"
$info_type eq "all"
$info_type eq "os"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "os"
| |||||||||||||||||||||||||||||
| 685 | 2 | push @info, "=== OS Information ==="; | |||||||||||||||||||||||||||
| 686 | 2 | push @info, "System: $^O"; | |||||||||||||||||||||||||||
| 687 | 2 | push @info, "Perl: $^V"; | |||||||||||||||||||||||||||
| 688 | 2 | ▶ | if (open my $fh, '<', '/etc/os-release') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 689 | 2 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 690 | 26 | chomp; | |||||||||||||||||||||||||||
| 691 | 26 | ▶ | push @info, $_ if /^(NAME|VERSION|PRETTY_NAME)=/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 692 | } | ||||||||||||||||||||||||||||
| 693 | 2 | close $fh; | |||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||
| 695 | 2 | push @info, ""; | |||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | 2 | ▶ | if ($info_type eq 'all' || $info_type eq 'disk') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info_type eq "all" or $info_type eq "disk"
MC/DC50%1/2
$info_type eq "all" or $info_type eq "disk"
$info_type eq "all"
$info_type eq "disk"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "disk"
| |||||||||||||||||||||||||||||
| 699 | 1 | push @info, "=== Disk Usage ==="; | |||||||||||||||||||||||||||
| 700 | 1 | ▶ | my $cwd = $session->working_dir // getcwd(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$session->working_dir // getcwd()
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir
getcwd()
Truth table33%1/3
A // B
A= $session->working_dirB= getcwd()
| |||||||||||||||||||||||||||||
| 701 | 1 | push @info, "Working directory: $cwd"; | |||||||||||||||||||||||||||
| 702 | 1 | push @info, ""; | |||||||||||||||||||||||||||
| 703 | } | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | 2 | ▶ | if ($info_type eq 'all' || $info_type eq 'memory') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info_type eq "all" or $info_type eq "memory"
MC/DC50%1/2
$info_type eq "all" or $info_type eq "memory"
$info_type eq "all"
$info_type eq "memory"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "memory"
| |||||||||||||||||||||||||||||
| 706 | 1 | push @info, "=== Memory ==="; | |||||||||||||||||||||||||||
| 707 | 1 | ▶ | if ($^O eq 'darwin') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 708 | 0 | push @info, "(Memory info requires system command on macOS)"; | |||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||
| 710 | elsif (-r '/proc/meminfo') { | ||||||||||||||||||||||||||||
| 711 | 1 | ▶ | if (open my $fh, '<', '/proc/meminfo') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 712 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 713 | 54 | ▶ | push @info, $_ if /^(MemTotal|MemFree|MemAvailable|Buffers|Cached):/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||
| 715 | 1 | close $fh; | |||||||||||||||||||||||||||
| 716 | } | ||||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||
| 718 | 1 | push @info, ""; | |||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | 2 | ▶ | if ($info_type eq 'all' || $info_type eq 'processes') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info_type eq "all" or $info_type eq "processes"
MC/DC50%1/2
$info_type eq "all" or $info_type eq "processes"
$info_type eq "all"
$info_type eq "processes"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "processes"
| |||||||||||||||||||||||||||||
| 722 | 1 | push @info, "=== Current Process ==="; | |||||||||||||||||||||||||||
| 723 | 1 | push @info, "PID: $$"; | |||||||||||||||||||||||||||
| 724 | 1 | ▶ | push @info, "User: " . (getpwuid($<) // $<); | ||||||||||||||||||||||||||
|
Condition33%1/3
getpwuid $< // $<
MC/DC0%0/2
getpwuid $< // $<
getpwuid $<
$<
Truth table33%1/3
A // B
A= getpwuid $<B= $<
| |||||||||||||||||||||||||||||
| 725 | 1 | push @info, ""; | |||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||
| 728 | 2 | $future->done(_mcp_result(join("\n", @info))); | |||||||||||||||||||||||||||
| 729 | 2 | return $future; | |||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||
| 732 | 2 | ▶ | sub _glob_to_regex { | ||||||||||||||||||||||||||
|
Subroutine
_glob_to_regex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 733 | 2 | my ($glob) = @_; | |||||||||||||||||||||||||||
| 734 | 2 | $glob =~ s/\./\\./g; | |||||||||||||||||||||||||||
| 735 | 2 | $glob =~ s/\*/.*/g; | |||||||||||||||||||||||||||
| 736 | 2 | $glob =~ s/\?/./g; | |||||||||||||||||||||||||||
| 737 | 2 | return qr/^$glob$/i; | |||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | 0 | ▶ | sub _format_perms { | ||||||||||||||||||||||||||
|
Subroutine
_format_perms: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 741 | 0 | my ($mode) = @_; | |||||||||||||||||||||||||||
| 742 | 0 | my $perms = ''; | |||||||||||||||||||||||||||
| 743 | 0 | ▶ | $perms .= ($mode & 0400) ? 'r' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 744 | 0 | ▶ | $perms .= ($mode & 0200) ? 'w' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 745 | 0 | ▶ | $perms .= ($mode & 0100) ? 'x' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 746 | 0 | ▶ | $perms .= ($mode & 0040) ? 'r' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 747 | 0 | ▶ | $perms .= ($mode & 0020) ? 'w' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 748 | 0 | ▶ | $perms .= ($mode & 0010) ? 'x' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 749 | 0 | ▶ | $perms .= ($mode & 0004) ? 'r' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 750 | 0 | ▶ | $perms .= ($mode & 0002) ? 'w' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 751 | 0 | ▶ | $perms .= ($mode & 0001) ? 'x' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 752 | 0 | return $perms; | |||||||||||||||||||||||||||
| 753 | } | ||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||
| 755 | 0 | ▶ | sub _format_date { | ||||||||||||||||||||||||||
|
Subroutine
_format_date: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 756 | 0 | my ($time) = @_; | |||||||||||||||||||||||||||
| 757 | 0 | my @t = localtime($time); | |||||||||||||||||||||||||||
| 758 | 0 | return sprintf("%s %2d %02d:%02d", | |||||||||||||||||||||||||||
| 759 | (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$t[4]], | ||||||||||||||||||||||||||||
| 760 | $t[3], $t[2], $t[1]); | ||||||||||||||||||||||||||||
| 761 | } | ||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | 9 | 1; | |||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | __END__ | ||||||||||||||||||||||||||||