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