#!perl.exe use diagnostics; #use Env; use English; use strict; use vars qw( %env_vars %var_select $filename ); use warnings; use File::Basename; use IO::Handle; use IO::File; use Tk; use TK::FileDialog; use XML::Parser; use Win32::AdminMisc; =head1 NAME RestoreWin32EnvVars - Script for restoring the environment variables in Win32 =head1 SYNOPSIS RestoreWin32EnvVars filename =head1 DESCRIPTION This script will restore the environment variables on win32 platforms from an XML file. It should work on Win 2K and WinNT 4.0. It has not been tested on Win 9X and Win ME platforms. The script brings up a screen that lets you select the variables to restore. =head1 MODULES USED Win32::AdminMisc File::Basename IO::Handle IO::File Tk TK::FileDialog XML::Writer =cut my $DEBUG = 0; # set to 0 to turn off debugging, 1 - to turn on debugging my $OVERWRITE = 2; # 0 - do not overwrite, 1 - overwrite, 2 - prompt my( $def, $response, $old_value, $proceed ); #turn on autoflush (required so that the print gets to the screen #before the read) STDOUT->autoflush( 1 ); #Win32::AdminMisc::GetEnvVar( \%env_vars, ENV_SYSTEM ); SetupScreen(); # print %env_vars; #confirm before proceeding # print( "\nDone ... \n"); exit 0; sub SetEnvVars { my $var; foreach $var ( sort keys %env_vars ) { if( $var_select{ $var } == 1 ) { print( "setting $var to $env_vars{$var} ...\n"); Win32::AdminMisc::SetEnvVar( $var, $env_vars{$var}, ENV_SYSTEM ); } } exit; } my( $mw, $f, $t, $var, $l_var, $l_def, $c, $e, $bLoad, $bApply, $bBrowse, $bSelect, $bDeselect, $l, $fd ); my @sys_env_vars = ( "ComSpec", "Number_of_Processors", "OS", "PROCESSOR_ARCHITECTURE", "PROCESSOR_IDENTIFIER", "PROCESSOR_LEVEL", "PROCESSOR_REVISION", "Template", "windir", "Os2Lib" ); sub SetupScreen { $mw = MainWindow->new(); $mw->title( "System Environment Variables" ); $filename = "env_vars.xml"; $fd =$mw->FileDialog( -Create => 0, -Title => "Files", -FPat => '*.xml', -SelDir => 0, -Horiz => 1, -Grab => 0, -Path => ".", -File => $filename ); $f = $mw->Frame()->pack( -expand => 1, -fill => 'x' ); $l = $f->Label( -text => "File Name: " ) -> grid( $e = $f->Entry( -textvariable => \$filename), $bBrowse = $f->Button( -text => "Browse", -command => sub { my $old_fname = $filename; $filename = $fd->Show(); if( !defined( $filename ) ) { $filename = $old_fname; }} ), $bLoad = $f->Button( -text => "Load", -command => \&ReadEnvVars ), $bSelect = $f->Button( -text => "Select All", -command => \&SelectAll ), $bDeselect = $f->Button( -text => "Deselect All", -command => \&DeselectAll ), $bApply = $f->Button( -text => "Apply", -command => \&SetEnvVars ) ); $t = $mw->Scrolled( "Text", -wrap => 'none', -scrollbars => 'osoe' ) -> pack( -expand => 1, -fill => 'both' ); $t->configure( -state => 'disabled' ); $mw->configure( -height => 20, -width => 150 ); MainLoop; } sub SelectAll { foreach $var ( sort keys %env_vars ) { if( scalar( grep( /\b$var\b/i, @sys_env_vars ) ) < 1 ) { $var_select{ $var } = 1; } else { $var_select{ $var } = 0; } } } sub DeselectAll { foreach $var ( sort keys %env_vars ) { $var_select{ $var } = 0; } } sub ReadEnvVars { my $p = XML::Parser->new( Style => 'Stream' ); $p->parsefile( $filename ); # print "\n %env_vars \n"; SelectAll(); $t->configure( -state => 'normal' ); foreach $var ( sort keys %env_vars ) { print "displaying $var => $env_vars{ $var } \n" if $DEBUG; $c = $t->Checkbutton( -variable => \$var_select{ $var }, -cursor => 'arrow' ); $t->windowCreate( 'end', -window => $c ); $t->insert( 'end', " " ); $l_var = $t->Label( -relief => 'sunken', -width => 35, -text => $var ); $t->windowCreate('end', -window => $l_var ); $t->insert( 'end', " " ); $l_def = $t->Scrolled( "Label", -relief => 'sunken', # -width => 250, -text => "$env_vars{$var}", -anchor => 'w', -justify => 'left' , -wraplength => 300 ); $t->windowCreate('end', -window => $l_def ); $t->insert( 'end', "\n" ); #sleep( 1 ); } $t->configure( -state => 'disabled' ); } my( $var_name, $var_def, @tag_stack ); sub StartTag { my( $p, $tag ) = @_; push @tag_stack, $tag; $var_def = ""; # print "Found tag $tag \n"; if( $tag eq "var" ) { $var_name = $_{name}; # print "Found var name: $var_name \n"; } } sub EndTag { my $tag = pop @tag_stack; if( $tag eq "var" ) { print "Inserting $var_name => $var_def \n" if $DEBUG; $env_vars{ $var_name } = $var_def; } } sub Text { $var_def .= $_; }