1#!/usr/bin/perl -w 2 3use File::Copy; 4 5my $output_format = 'u'; 6 7sub reg_get_value($) 8{ 9 # it is believed that the registry moves keys around 10 # depending on OS version, this will de-mangle that 11 my $key = shift; 12 my $fhandle; 13 my $value; 14 15 open ($fhandle, "/proc/registry/$key") || return; 16 # reg keys have 0x00 0x5c at the end 17 $value = (split /\0/, <$fhandle>)[0]; 18 close ($fhandle); 19 20 if ( defined $value ) { 21 chomp ($value); 22 $value =~ s|\r\n||; 23# print "Value '$value' at '$key'\n"; 24 } 25 26 return $value; 27} 28 29sub reg_find_key($) 30{ 31 # it is believed that the registry moves keys around 32 # depending on OS version, this will de-mangle that 33 my $key = shift; 34 $key =~ s| |\\ |; 35 $key = `cd /proc/registry/ ; ls $key`; 36 37 return $key; 38} 39 40sub print_syntax() 41{ 42 print "oowintool [option] ...\n"; 43 print " encoding options\n"; 44 print " -w - windows form\n"; 45 print " -u - unix form (default)\n"; 46 print " commands:\n"; 47 print " --msvc-ver - dump version of MSVC eg. 6.0\n"; 48 print " --msvc-copy-dlls <dest> - copy msvc[pr]??.dlls into <dest>/msvcp??/\n"; 49 print " --msvc-productdir - dump productdir\n"; 50 print " --msvs-productdir - dump productdir\n"; 51 print " --dotnetsdk-dir - dump .Net SDK path\n"; 52 print " --csc-compilerdir - dump .Net SDK compiler path\n"; 53 print " --psdk-home - dump psdk install dir\n"; 54 print " --jdk-home - dump the jdk install dir\n"; 55 print " --nsis-dir - dump NSIS path\n"; 56 print " --help - this message\n"; 57} 58 59sub cygpath($$$) 60{ 61 my ($path, $input_format, $format) = @_; 62 63 return $path if ( ! defined $path ); 64 # Strip trailing path separators 65 if ($input_format eq 'u') { 66 $path =~ s|/*\s*$||; 67 } else { 68 $path =~ s|\\*\s*$||; 69 } 70 71 # 'Unterminated quoted string errors' from 'ash' when 72 # forking cygpath so - reimplement cygpath in perl [ gack ] 73 if ($format eq 'u' && $input_format eq 'w') { 74 $path =~ s|\\|/|g; 75 $path =~ s|([a-zA-Z]):/|/cygdrive/$1/|g; 76 } 77 elsif ($format eq 'w' && $input_format eq 'u') { 78 $path =~ s|/cygdrive/([a-zA-Z])/|/$1/|g; 79 $path =~ s|/|\\|g; 80 } 81 82 return $path; 83} 84 85sub print_path($$) 86{ 87 my ($path, $unix) = @_; 88 89 $path = cygpath ($path, $unix, $output_format); 90 91 print $path; 92} 93 94sub print_psdk_home() 95{ 96 my ($value, $key); 97 $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SDKs/Windows/v6.1/InstallationFolder'); 98 if (!defined $value) 99 { 100 $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SDKs/Windows/CurrentInstallFolder'); 101 } 102 if (!defined $value) 103 { 104 $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MicrosoftSDK/Directories/Install Dir'); 105 } 106 if (!defined $value) 107 { 108 $key = reg_find_key ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MicrosoftSDK/InstalledSDKs/*/Install Dir'); 109 $value = reg_get_value ($key); 110 } 111 if (!defined $value) 112 { 113 my $dir = cygpath (find_msvc()->{'product_dir'}, 'w', $output_format); 114 $value = `/bin/find "$dir" -iname platformsdk | head -n 1`; 115 } 116 117 defined $value || die "psdk not found"; 118 119 print cygpath ($value, 'w', $output_format); 120} 121 122my %msvc_net_2003 = ( 123 'ver' => '7.1', 124 'key' => 'Microsoft/VisualStudio/7.1/Setup/VC/ProductDir', 125 'dll_path' => '../Visual Studio .NET Professional 2003 - English', 126 'dll_suffix' => '71' 127); 128my %msvs_net_2003 = ( 129 'ver' => '7.1', 130 'key' => 'Microsoft/VisualStudio/7.1/Setup/VS/ProductDir', 131 'dll_path' => 'Visual Studio .NET Professional 2003 - English', 132 'dll_suffix' => '71' 133); 134my %msvs_net_2003_ea = ( 135 'ver' => '7.1', 136 'key' => 'Microsoft/VisualStudio/7.1/Setup/VS/ProductDir', 137 'dll_path' => 'Visual Studio .NET Enterprise Architect 2003 - English', # testme ... 138 'dll_suffix' => '71' 139); 140my %msvs_express_2005 = ( 141 'ver' => '8.0', 142 'key' => 'Microsoft/VCExpress/8.0/Setup/VS/ProductDir', 143 'dll_path' => '../SDK/v2.0/Bin', 144 'dll_suffix' => '80' 145); 146my %msvc_express_2005 = ( 147 'ver' => '8.0', 148 'key' => 'Microsoft/VCExpress/8.0/Setup/VC/ProductDir', 149 'dll_path' => '../SDK/v2.0/Bin', 150 'dll_suffix' => '80' 151); 152my %msvs_2005 = ( 153 'ver' => '8.0', 154 'key' => 'Microsoft/VisualStudio/8.0/Setup/VS/ProductDir', 155 'dll_path' => 'Visual Studio .NET Professional 2005 - English', 156 'dll_suffix' => '80' 157); 158my %msvc_2005 = ( 159 'ver' => '8.0', 160 'key' => 'Microsoft/VisualStudio/8.0/Setup/VC/ProductDir', 161 'dll_path' => '../SDK/v2.0/Bin', 162 'dll_suffix' => '80' 163); 164my %msvs_2008 = ( 165 'ver' => '9.0', 166 'key' => 'Microsoft/VisualStudio/9.0/Setup/VS/ProductDir', 167 'dll_path' => 'VC/redist/x86/Microsoft.VC90.CRT', 168 'dll_suffix' => '90' 169); 170my %msvc_2008 = ( 171 'ver' => '9.0', 172 'key' => 'Microsoft/VisualStudio/9.0/Setup/VC/ProductDir', 173 'dll_path' => 'redist/x86/Microsoft.VC90.CRT', 174 'dll_suffix' => '90' 175); 176my %msvs_express_2008 = ( 177 'ver' => '9.0', 178 'key' => 'Microsoft/VCExpress/9.0/Setup/VS/ProductDir', 179 'dll_path' => 'VC/redist/x86/Microsoft.VC90.CRT', 180 'dll_suffix' => '90' 181); 182my %msvc_express_2008 = ( 183 'ver' => '9.0', 184 'key' => 'Microsoft/VCExpress/9.0/Setup/VC/ProductDir', 185 'dll_path' => 'redist/x86/Microsoft.VC90.CRT', 186 'dll_suffix' => '90' 187); 188 189sub find_msvs() 190{ 191 my @ms_versions = ( \%msvs_2008, \%msvs_express_2008, \%msvs_2005, \%msvs_express_2005, \%msvs_net_2003_ea, \%msvs_net_2003 ); 192 193 for $ver (@ms_versions) 194 { 195 my $install = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/" . $ver->{'key'}); 196 if (defined $install && $install ne '') { 197 $ver->{'product_dir'} = $install; 198 return $ver; 199 } 200 } 201 die "Can't find MS Visual Studio / VC++"; 202} 203 204sub find_msvc() 205{ 206 my @ms_versions = ( \%msvc_2008, \%msvc_express_2008, \%msvc_2005, \%msvc_express_2005, \%msvc_net_2003 ); 207 208 for $ver (@ms_versions) 209 { 210 my $install = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/" . $ver->{'key'}); 211 if (defined $install && $install ne '') { 212 $ver->{'product_dir'} = $install; 213 return $ver; 214 } 215 } 216 die "Can't find MS Visual Studio / VC++"; 217} 218 219sub print_msvc_ver() 220{ 221 my $ver = find_msvc(); 222 print $ver->{'ver'}; 223} 224 225sub print_msvc_product_dir() 226{ 227 my $ver = find_msvc(); 228 print cygpath ($ver->{'product_dir'}, 'w', $output_format); 229} 230 231sub print_msvs_productdir() 232{ 233 my $ver = find_msvs(); 234 print cygpath ($ver->{'product_dir'}, 'w', $output_format); 235} 236 237sub print_csc_compiler_dir() 238{ 239 my $dir = cygpath (reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/InstallRoot"), 'w', $output_format); 240 my $csc_exe = `/bin/find "$dir" -iname csc.exe | grep "v2\." | head -n 1`; 241 print `dirname $csc_exe`; 242} 243 244sub print_dotnetsdk_dir() 245{ 246 my $dir = 247 reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/sdkInstallRootv1.1") || 248 reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/sdkInstallRootv2.0"); 249 print cygpath ($dir, 'w', $output_format); 250} 251 252sub print_jdk_dir() 253{ 254 my $dir = 255 reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.5/JavaHome") || 256 reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.4/JavaHome") || 257 reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.3/JavaHome"); 258 print cygpath($dir, 'w', $output_format); 259} 260 261sub print_nsis_dir() 262{ 263 my $dir = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/NSIS/@"); 264 print cygpath ($dir, 'w', $output_format) if defined $dir; 265} 266 267sub copy_dll($$$) 268{ 269 my ($src, $fname, $dest) = @_; 270 271 -f "$src/$fname" || die "can't find $src"; 272 -d $dest || die "no directory $dest"; 273 274 print STDERR "Copying $src/$fname to $dest\n"; 275 copy ("$src/$fname", $dest) || die "copy failed: $!"; 276 chmod (0755, "$dest/$fname") || die "failed to set dll executable: $!"; 277} 278 279sub msvc_find_version($) 280{ 281 my $checkpath = shift; 282 my $ver = find_msvc(); 283 my $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' . 284 $ver->{$checkpath}); 285 -d $srcdir && return $ver; 286 $ver = find_msvs(); 287 $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' . 288 $ver->{$checkpath}); 289 -d $srcdir && return $ver; 290 return undef; 291} 292 293sub msvc_copy_dlls($) 294{ 295 my $dest = shift; 296 my $ver = msvc_find_version('dll_path'); 297 defined $ver || return; 298 my $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' . 299 $ver->{'dll_path'}); 300 301 copy_dll ($srcdir, "msvcp" . $ver->{'dll_suffix'} . ".dll", 302 $dest . $ver->{'dll_suffix'}); 303 copy_dll ($srcdir, "msvcr" . $ver->{'dll_suffix'} . ".dll", 304 $dest . $ver->{'dll_suffix'}); 305 if ($ver->{'dll_suffix'} >= 90) { 306 copy_dll ($srcdir, "msvcm" . $ver->{'dll_suffix'} . ".dll", 307 $dest . $ver->{'dll_suffix'}); 308 copy_dll ($srcdir, "Microsoft.VC90.CRT.manifest", $dest . $ver->{'dll_suffix'}); 309 } 310} 311 312if (!@ARGV) { 313 print_syntax(); 314 exit 1; 315} 316 317my @commands = (); 318my $opt; 319while (@ARGV) { 320 $opt = shift @ARGV; 321 322 if ($opt eq '-w' || $opt eq '-u') { 323 $output_format = substr($opt, 1, 1); 324 } else { 325 push @commands, $opt; 326 } 327} 328 329while (@commands) { 330 $opt = shift @commands; 331 332 if (0) { 333 } elsif ($opt eq '--msvc-ver') { 334 print_msvc_ver(); 335 } elsif ($opt eq '--msvc-copy-dlls') { 336 my $dest = shift @commands; 337 defined $dest || die "copy-dlls requires a destination directory"; 338 msvc_copy_dlls( $dest ); 339 } elsif ($opt eq '--msvs-productdir') { 340 print_msvs_productdir(); 341 } elsif ($opt eq '--msvc-productdir') { 342 print_msvc_product_dir(); 343 } elsif ($opt eq '--dotnetsdk-dir') { 344 print_dotnetsdk_dir(); 345 } elsif ($opt eq '--csc-compilerdir') { 346 print_csc_compiler_dir(); 347 } elsif ($opt eq '--psdk-home') { 348 print_psdk_home(); 349 } elsif ($opt eq '--jdk-home') { 350 print_jdk_dir(); 351 } elsif ($opt eq '--nsis-dir') { 352 print_nsis_dir(); 353 } elsif ($opt eq '--help' || $opt eq '/?') { 354 print_syntax(); 355 } else { 356 print "Unknown option '$opt'\n"; 357 print_syntax(); 358 exit 1; 359 } 360} 361 362