| 1 | package Psh::OS::Win; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | $Psh::OS::PATH_SEPARATOR=';'; | ||
| 5 | $Psh::OS::FILE_SEPARATOR='\\'; | ||
| 6 | |||
| 7 | $Psh::history_file = "psh_history"; | ||
| 8 | |||
| 9 | require Psh::Util; | ||
| 10 | |||
| 11 | eval { | ||
| 12 | use Win32; | ||
| 13 | use Win32::TieRegistry 0.20; | ||
| 14 | use Win32::Process; | ||
| 15 | use Win32::Console; | ||
| 16 | use Win32::NetAdmin; | ||
| 17 | }; | ||
| 18 | |||
| 19 | if ($@) { | ||
| 20 | Psh::Util::print_error_i18n('no_libwin32'); | ||
| 21 | die "\n"; | ||
| 22 | } | ||
| 23 | |||
| 24 | my $console= new Win32::Console(); | ||
| 25 | my @user_cache=(); | ||
| 26 | |||
| 27 | # | ||
| 28 | # For documentation see Psh::OS::Unix | ||
| 29 | # | ||
| 30 | |||
| 31 | |||
| 32 | sub set_window_title { | ||
| 33 | my $title=shift; | ||
| 34 | $console->Title($title); | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub reinstall_resize_handler { | ||
| 39 | # actually we have no 'handlers' here but instead simply do it | ||
| 40 | my ($cols,$rows)=$console->Size(); | ||
| 41 | $ENV{COLUMNS}=$cols; | ||
| 42 | $ENV{ROWS}=$rows; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub get_hostname { | ||
| 46 | my $name_from_reg = $Registry->{"HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName\\ComputerName"}; | ||
| 47 | return $name_from_reg if $name_from_reg; | ||
| 48 | return 'localhost'; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub get_known_hosts { | ||
| 52 | my $hosts_file = "$ENV{windir}\\HOSTS"; | ||
| 53 | if (open(F_KNOWNHOST,"< $hosts_file")) { | ||
| 54 | my $hosts_text = join('', <F_KNOWNHOST>); | ||
| 55 | close(F_KNOWNHOST); | ||
| 56 | return Psh::Util::parse_hosts_file($hosts_text); | ||
| 57 | } else { | ||
| 58 | return ("localhost"); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | # | ||
| 63 | # void display_pod(text) | ||
| 64 | # | ||
| 65 | sub display_pod { | ||
| 66 | my $tmp= Psh::OS::tmpnam(); | ||
| 67 | my $text= shift; | ||
| 68 | |||
| 69 | open( TMP,">$tmp"); | ||
| 70 | print TMP $text; | ||
| 71 | close(TMP); | ||
| 72 | |||
| 73 | eval { | ||
| 74 | require Pod::Text; | ||
| 75 | Pod::Text::pod2text($tmp,*STDOUT); | ||
| 76 | }; | ||
| 77 | print $text if $@; | ||
| 78 | |||
| 79 | unlink($tmp); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub inc_shlvl { | ||
| 83 | if (! $ENV{SHLVL}) { | ||
| 84 | $Psh::login_shell = 1; | ||
| 85 | $ENV{SHLVL} = 1; | ||
| 86 | } else { | ||
| 87 | $Psh::login_shell = 0; | ||
| 88 | $ENV{SHLVL}++; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub execute_complex_command { | ||
| 93 | my @array= @{shift()}; | ||
| 94 | my $fgflag= shift @array; | ||
| 95 | my @return_val; | ||
| 96 | my $pgrp_leader=0; | ||
| 97 | my $success= 0; | ||
| 98 | my $pid; | ||
| 99 | my $string=''; | ||
| 100 | my @tmp; | ||
| 101 | |||
| 102 | if($#array) { | ||
| 103 | Psh::Util::print_error("No piping yet.\n"); | ||
| 104 | return (); | ||
| 105 | } | ||
| 106 | |||
| 107 | my $obj; | ||
| 108 | for( my $i=0; $i<@array; $i++) { | ||
| 109 | my ($strategy, $how, $options, $words, $text, $opt)= @{$array[$i]}; | ||
| 110 | local $Psh::current_options=$opt; | ||
| 111 | my $line= join(' ',@$words); | ||
| 112 | my ($eval_thingie,$bgflag); | ||
| 113 | ($success,$eval_thingie,$words,$bgflag,@return_val)= $strategy->execute( \$line, $words, $how, 0); | ||
| 114 | |||
| 115 | my @tmp; | ||
| 116 | |||
| 117 | if( defined($eval_thingie)) { | ||
| 118 | ($obj,$success,@tmp)= _fork_process($eval_thingie,$fgflag,$text,undef,$words); | ||
| 119 | } | ||
| 120 | if( @return_val < 1 || | ||
| 121 | !defined($return_val[0])) { | ||
| 122 | @return_val= @tmp; | ||
| 123 | } | ||
| 124 | $string=$text; | ||
| 125 | } | ||
| 126 | if ($obj) { | ||
| 127 | my $pid=$obj->GetProcessID(); | ||
| 128 | my $job=Psh::Joblist::create_job($pid,$string,$obj); | ||
| 129 | if( $fgflag) { | ||
| 130 | _wait_for_system($obj, 1); | ||
| 131 | } else { | ||
| 132 | my $visindex= Psh::Joblist::get_job_number($pid); | ||
| 133 | Psh::Util::print_out("[$visindex] Background $pid $string\n"); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | return ($success,@return_val); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _fork_process { | ||
| 140 | local( $Psh::code, $Psh::fgflag, $Psh::string, $Psh::options, | ||
| 141 | $Psh::words) = @_; | ||
| 142 | local $Psh::pid; | ||
| 143 | |||
| 144 | # TODO: perhaps we should use Win32::Process? | ||
| 145 | # hmm - won't help alot :-( - warp | ||
| 146 | # print_error_i18n('no_jobcontrol') unless $Psh::fgflag; | ||
| 147 | |||
| 148 | if( ref($Psh::code) eq 'CODE') { | ||
| 149 | return (0,&{$Psh::code}); | ||
| 150 | } else { | ||
| 151 | if ($Psh::words) { | ||
| 152 | my $obj; | ||
| 153 | Win32::Process::Create($obj, | ||
| 154 | @$Psh::words->[0], | ||
| 155 | $Psh::string, | ||
| 156 | 0, | ||
| 157 | NORMAL_PRIORITY_CLASS, | ||
| 158 | "."); | ||
| 159 | return ($obj,0); | ||
| 160 | # We are passing around objects instead of pid because | ||
| 161 | # Win32::Process currently only allows me to create objects, | ||
| 162 | # not look them up via pid | ||
| 163 | } else { | ||
| 164 | return (0,system($Psh::code)); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _wait_for_system { | ||
| 170 | my ($obj, $quiet)=@_; | ||
| 171 | |||
| 172 | return unless $obj; | ||
| 173 | $obj->Wait(INFINITE); | ||
| 174 | _handle_wait_status($obj,$quiet) | ||
| 175 | } | ||
| 176 | |||
| 177 | sub _handle_wait_status { | ||
| 178 | my ($obj,$quiet)=@_; | ||
| 179 | |||
| 180 | return '' unless $obj; | ||
| 181 | my $pid= $obj->GetProcessID(); | ||
| 182 | my $job= Psh::Joblist::get_job($obj->GetProcessID()); | ||
| 183 | my $command = $job->{call}; | ||
| 184 | my $visindex= Psh::Joblist::get_job_number($pid); | ||
| 185 | my $verb=''; | ||
| 186 | |||
| 187 | my $tmp= Psh::Locale::get_text('done'); | ||
| 188 | Psh::Util::print_out("[$visindex] \u$tmp $pid $command\n") unless $quiet; | ||
| 189 | Psh::Joblist::delete_job($pid); | ||
| 190 | return ''; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub fork_process { | ||
| 194 | _fork_process(@_); | ||
| 195 | return undef; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub get_all_users { | ||
| 199 | unless (@user_cache) { | ||
| 200 | Win32::NetAdmin::GetUsers("",FILTER_NORMAL_ACCOUNT,\@user_cache); | ||
| 201 | } | ||
| 202 | return @user_cache; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | sub has_job_control { return 1; } | ||
| 207 | |||
| 208 | sub resume_job { | ||
| 209 | my $job= shift; | ||
| 210 | $job->{assoc_obj}->Resume(); | ||
| 211 | } | ||
| 212 | |||
| 213 | # | ||
| 214 | # void restart_job(bool FOREGROUND, int JOB_INDEX) | ||
| 215 | # | ||
| 216 | sub restart_job | ||
| 217 | { | ||
| 218 | my ($fg_flag, $job_to_start) = @_; | ||
| 219 | |||
| 220 | my $job= Psh::Joblist::find_job($job_to_start); | ||
| 221 | |||
| 222 | if(defined($job)) { | ||
| 223 | my $pid = $job->{pid}; | ||
| 224 | my $command = $job->{call}; | ||
| 225 | |||
| 226 | if ($command) { | ||
| 227 | my $tmp=Psh::Locale::get_text('restart'); | ||
| 228 | my $verb = "\u$tmp"; | ||
| 229 | my $qRunning = $job->{running}; | ||
| 230 | if ($fg_flag) { | ||
| 231 | my $tmp= Psh::Locale::get_text('foreground'); | ||
| 232 | $verb = "\u$tmp"; | ||
| 233 | } elsif ($qRunning) { | ||
| 234 | # bg request, and it's already running: | ||
| 235 | return; | ||
| 236 | } | ||
| 237 | my $visindex = Psh::Joblist::get_job_number($pid); | ||
| 238 | Psh::Util::print_out("[$visindex] $verb $pid $command\n"); | ||
| 239 | |||
| 240 | if($fg_flag) { | ||
| 241 | eval { _wait_for_system($job->{assoc_obj}, 0); }; | ||
| 242 | } elsif( !$qRunning) { | ||
| 243 | $job->continue; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | sub get_home_dir { | ||
| 250 | my $user= shift; | ||
| 251 | my $home; | ||
| 252 | if (!$user) { | ||
| 253 | $home=$ENV{HOME}||$ENV{USERPROFILE}||$ENV{HOMEDRIVE}.$ENV{HOMEPATH}; | ||
| 254 | } else { | ||
| 255 | # There is a UserGetAttributes function in Win32::NetAdmin but | ||
| 256 | # it will only work if you're admin | ||
| 257 | # I'v searched my registry but did not find something usable | ||
| 258 | } | ||
| 259 | return $home||"\\"; | ||
| 260 | } # we really should return something (profile?) | ||
| 261 | |||
| 262 | |||
| 263 | sub get_rc_files { | ||
| 264 | my @rc=(); | ||
| 265 | |||
| 266 | push @rc, "\\etc\\pshrc" if -r "\\etc\\pshrc"; | ||
| 267 | push @rc, "$ENV{WINDIR}\\pshrc" if -r "$ENV{WINDIR}\\pshrc"; | ||
| 268 | my $home= Psh::OS::get_home_dir(); | ||
| 269 | if ($home) { push @rc, catfile($home,'pshrc') }; | ||
| 270 | return @rc; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub remove_readline_handler {1} | ||
| 274 | |||
| 275 | sub is_path_absolute { | ||
| 276 | my $path= shift; | ||
| 277 | |||
| 278 | return substr($path,0,1) eq "\\" || | ||
| 279 | $path=~ /^[a-zA-Z]\:\\/; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub get_path_extension { | ||
| 283 | my $extsep = $Psh::OS::PATH_SEPARATOR || ';'; | ||
| 284 | my $pathext = $ENV{PATHEXT} || $Registry->{"HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment\\PATHEXT"} || ".cmd;.bat;.exe;.com"; # Environment has precedence over LOCAL_MACHINE registry | ||
| 285 | return split("$extsep",$pathext); | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | # Simply doing backtick eval - mainly for Prompt evaluation | ||
| 290 | sub backtick { | ||
| 291 | return `@_`; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub abs_path { | ||
| 295 | my $dir= shift; | ||
| 296 | if (defined &Win32::GetFullPathName) { | ||
| 297 | my $tmp= Win32::GetFullPathName($dir); | ||
| 298 | $tmp=~tr:\\:/:; # otherwise prompt code etc messes up | ||
| 299 | return $tmp; | ||
| 300 | } | ||
| 301 | undef; | ||
| 302 | } | ||
| 303 | |||
| 304 | sub getcwd_psh { | ||
| 305 | my $tmp; | ||
| 306 | if (defined &Win32::GetCwd) { | ||
| 307 | $tmp= Win32::GetCwd(); | ||
| 308 | $tmp=~tr:\\:/:; | ||
| 309 | } | ||
| 310 | return $tmp||Psh::OS::fb_getcwd(); | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | sub get_editor { | ||
| 315 | my $suggestion= shift; | ||
| 316 | return $suggestion||$ENV{VISUAL}||$ENV{EDITOR}||'edit'; | ||
| 317 | } | ||
| 318 | |||
| 319 | |||
| 320 | # From File::Spec | ||
| 321 | |||
| 322 | |||
| 323 | sub canonpath { | ||
| 324 | my ($path) = @_; | ||
| 325 | $path =~ s/^([a-z]:)/\u$1/s; | ||
| 326 | $path =~ s|/|\\|g; | ||
| 327 | $path =~ s|([^\\])\\+|$1\\|g; # xx////xx -> xx/xx | ||
| 328 | $path =~ s|(\\\.)+\\|\\|g; # xx/././xx -> xx/xx | ||
| 329 | $path =~ s|^(\.\\)+||s unless $path eq ".\\"; # ./xx -> xx | ||
| 330 | $path =~ s|\\\Z(?!\n)|| | ||
| 331 | unless $path =~ m#^([A-Z]:)?\\\Z(?!\n)#s; # xx/ -> xx | ||
| 332 | return $path; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub catfile { | ||
| 336 | my $file = pop @_; | ||
| 337 | return $file unless @_; | ||
| 338 | my $dir = catdir(@_); | ||
| 339 | $dir .= "\\" unless substr($dir,-1) eq "\\"; | ||
| 340 | return $dir.$file; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub catdir { | ||
| 344 | my @args = @_; | ||
| 345 | foreach (@args) { | ||
| 346 | # append a slash to each argument unless it has one there | ||
| 347 | $_ .= "/" if $_ eq '' || substr($_,-1) ne "/"; | ||
| 348 | } | ||
| 349 | return canonpath(join('', @args)); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub file_name_is_absolute { | ||
| 353 | my $file= shift; | ||
| 354 | return scalar($file =~ m{^([a-z]:)?[\\/]}is); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub rootdir { | ||
| 358 | "\\"; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub splitdir { | ||
| 362 | my ($directories) = @_ ; | ||
| 363 | |||
| 364 | if ( $directories !~ m|[\\/]\Z(?!\n)| ) { | ||
| 365 | return split( m|[\\/]|, $directories ); | ||
| 366 | } | ||
| 367 | else { | ||
| 368 | my( @directories )= split( m|[\\/]|, "${directories}dummy" ) ; | ||
| 369 | $directories[ $#directories ]= '' ; | ||
| 370 | return @directories ; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | sub splitpath { | ||
| 375 | my ($path, $nofile) = @_; | ||
| 376 | my ($volume,$directory,$file) = ('','',''); | ||
| 377 | if ( $nofile ) { | ||
| 378 | $path =~ | ||
| 379 | m{^( (?:[a-zA-Z]:|(?:\\\\|//)[^\\/]+[\\/][^\\/]+)? ) | ||
| 380 | (.*) | ||
| 381 | }xs; | ||
| 382 | $volume = $1; | ||
| 383 | $directory = $2; | ||
| 384 | } | ||
| 385 | else { | ||
| 386 | $path =~ | ||
| 387 | m{^ ( (?: [a-zA-Z]: | | ||
| 388 | (?:\\\\|//)[^\\/]+[\\/][^\\/]+ | ||
| 389 | )? | ||
| 390 | ) | ||
| 391 | ( (?:.*[\\\\/](?:\.\.?\Z(?!\n))?)? ) | ||
| 392 | (.*) | ||
| 393 | }xs; | ||
| 394 | $volume = $1; | ||
| 395 | $directory = $2; | ||
| 396 | $file = $3; | ||
| 397 | } | ||
| 398 | |||
| 399 | return ($volume,$directory,$file); | ||
| 400 | } | ||
| 401 | sub rel2abs { | ||
| 402 | my ($path,$base ) = @_; | ||
| 403 | |||
| 404 | if ( ! file_name_is_absolute( $path ) ) { | ||
| 405 | if ( !defined( $base ) || $base eq '' ) { | ||
| 406 | $base = Psh::OS::getcwd_psh() ; | ||
| 407 | } | ||
| 408 | elsif ( ! file_name_is_absolute( $base ) ) { | ||
| 409 | $base = rel2abs( $base ) ; | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | $base = canonpath( $base ) ; | ||
| 413 | } | ||
| 414 | |||
| 415 | my ( $path_directories, $path_file ) = | ||
| 416 | (splitpath( $path, 1 ))[1,2] ; | ||
| 417 | |||
| 418 | my ( $base_volume, $base_directories ) = | ||
| 419 | splitpath( $base, 1 ) ; | ||
| 420 | |||
| 421 | $path = catpath( | ||
| 422 | $base_volume, | ||
| 423 | catdir( $base_directories, $path_directories ), | ||
| 424 | $path_file | ||
| 425 | ) ; | ||
| 426 | } | ||
| 427 | |||
| 428 | return canonpath( $path ) ; | ||
| 429 | } | ||
| 430 | |||
| 431 | 1; | ||
| 432 | |||
| 433 | __END__ |