xref: /aoo42x/main/solenv/bin/guw.pl (revision 7e90fac2)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#**************************************************************
5#
6#  Licensed to the Apache Software Foundation (ASF) under one
7#  or more contributor license agreements.  See the NOTICE file
8#  distributed with this work for additional information
9#  regarding copyright ownership.  The ASF licenses this file
10#  to you under the Apache License, Version 2.0 (the
11#  "License"); you may not use this file except in compliance
12#  with the License.  You may obtain a copy of the License at
13#
14#    http://www.apache.org/licenses/LICENSE-2.0
15#
16#  Unless required by applicable law or agreed to in writing,
17#  software distributed under the License is distributed on an
18#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19#  KIND, either express or implied.  See the License for the
20#  specific language governing permissions and limitations
21#  under the License.
22#
23#**************************************************************
24
25
26# Description: ??
27
28#---------------------------------------------------------------------------
29# external modules
30use Text::ParseWords;
31
32# global vars
33@params = ();
34
35# set debug mode here:
36#$debug="true";
37#$debug_light="true";
38
39#---------------------------------------------------------------------------
40# Define known parameter exceptions
41%knownpara = ( 'echo', [ '/TEST', 'QQQ', 'CCC', 'uno:' ],
42			   'cl', [ '-clr:', '-Z' ],
43			   'csc', [ '-target:' ],
44			   'lib', [ 'OUT:', 'EXTRACT:','out:', 'def:', 'machine:' ],
45			   'link', [ 'BASE:', 'DEBUG', 'DLL', 'LIBPATH', 'MACHINE:',
46						 'MAP', 'NODEFAULTLIB', 'OPT', 'PDB', 'RELEASE',
47						 'SUBSYSTEM', 'STACK', 'out:', 'map:', 'ENTRY:',
48						 'implib:', 'delayload:', 'def', 'COMMENT:' ],
49			   'regcomp', [ '-env:', 'vnd.sun.star.expand:' , 'vnd.openoffice.pymodule' ],
50			   'regmerge', [ '/UCR' ],
51			   'rc', [ '-D' ],
52			   'rsc', [ '-DOOO_' ] );
53
54#---------------------------------------------------------------------------
55# procedures
56
57
58#----------------------------------------------------------
59# Function name: myCygpath
60# Description:   Transform POSIX path to DOS path
61# Arguments:     1. Variable (string) with one token
62#                2. optional - if set remove spaces and shorten to 8.3
63#                   representation.
64# Return value:  Reformatted String
65#----------------------------------------------------------
66sub myCygpath {
67    my $posixpath = shift;
68    my $shortenpath = shift || '';
69
70    my $dospath;
71
72    if ( $posixpath =~ / / and $shortenpath ) {
73        chomp( $dospath = qx{cygpath -d "$posixpath"} );
74        # "cygpath -d" returns "" if the file doesn't exist.
75        if ($dospath eq "") {
76            $dospath = ".";
77            print(STDERR "Error: guw.pl: Path: $posixpath:\nhas a problem! Probably nonexistent filename with space.\n");
78            if ( (defined $debug_light) or (defined $debug) ) {
79                die "exiting ...\n";
80            }
81        }
82    } else {
83        if ( $posixpath =~ /^\// ) {
84            chomp( $dospath = qx{cygpath -w "$posixpath"} );
85        } else {
86            $dospath = $posixpath;
87            $dospath =~ s/\//\\/g;
88        }
89    }
90    return $dospath;
91}
92
93#----------------------------------------------------------
94# Function name: WinFormat
95# Description:   Format variables to Windows Format.
96# Arguments:     1. Variable (string) with one token
97# Return value:  Reformatted String
98#----------------------------------------------------------
99sub WinFormat {
100  my $variable = shift @_;
101  my( $d1, $d1_prefix, $d2 );
102
103  $variable =~ s/(\$\w+)/$1/eeg ; # expand the variables
104  $variable =~ s/(\$\w+)/$1/eeg ; # expand the variables twice!
105
106  # Include paths or parameters with filenames
107  if ( $variable =~ /\A(-D[\w\.]*=)[\'\"]?((?:\/?[\w\.\-\+ ~]+)+\/?)[\'\"]?\Z/ ) {
108	  # This regex evaluates -D<something>=<path>, sometimes with quotes or "/" at the end
109	  # option -> $1, filename without quotes -> $2
110	  if ( defined $debug ) { print(STDERR "WinFormat:\ninclude (-D<something>=<path>) path:\n$variable\n"); }
111	  $d1_prefix = $1;
112	  $d1 = $2;
113	  $d2 = myCygpath($2,1);
114	  if ( $d2 ne "" ) {
115             $d2 =~ s/\\/\\\\/g ;
116	  }
117  } elsif ( $variable =~ /\A(-?\w[\w\.]*=)[\'\"]?((?:\/?[\w\.\-\+ ~]+)+\/?)[\'\"]?\Z/ ) {
118	  # This regex evaluates [-]X<something>=<path>, sometimes with quotes or "/" at the end
119	  # option -> $1, filename without quotes -> $2
120	  if ( defined $debug ) { print(STDERR "WinFormat:\ninclude ([-]<something>=<path>) path:\n$variable\n"); }
121	  $d1_prefix = $1;
122	  $d1 = $2;
123	  $d2 = myCygpath($2,1);
124  } elsif ( $variable =~ /\A(--\w[\w\.\-]*=)[\'\"]?((?:\/?[\w\.\-\+ ~]+)+\/?)[\'\"]?\Z/ ) {
125	  # This regex evaluates --<something>=<path>, sometimes with quotes or "/" at the end
126	  # option -> $1, filename without quotes -> $2
127	  if ( defined $debug ) { print(STDERR "WinFormat:\ninclude (--<something>=<path>) path:\n$variable\n"); }
128	  $d1_prefix = $1;
129	  $d1 = $2;
130	  $d2 = myCygpath($2,1);
131  } elsif ( $variable =~ /\A(-\w[\w\.]*:)[\'\"]?((?:\/?[\w\.\-\+ ~]+)+\/?)[\'\"]?\Z/ ) {
132	  # This regex evaluates -X<something>:<path>, sometimes with quotes or "/" at the end
133	  # option -> $1, filename without quotes -> $2
134	  if ( defined $debug ) { print(STDERR "WinFormat:\nFound (-<something>:<path>):\n$variable\n"); }
135	  $d1_prefix = $1;
136	  $d1 = $2;
137	  $d2 = myCygpath($2,1);
138  } elsif ( $variable =~ /\A(-\w+:)(.*)\Z/ ) {
139	  # This regex evaluates -X<something>:<NO-path>, and prevents translating of these.
140	  # option -> $1, rest -> $2
141	  if ( defined $debug ) { print(STDERR "WinFormat:\nFound (-<something>:<no-path>):\n$variable\n"); }
142	  $d1_prefix = $1;
143	  $d1 = $2;
144	  $d2 = myCygpath($2,1);
145  } elsif ( $variable =~ /\A(\w+:)[\'\"]?\/\/\/((?:\/?[\w\.\-\+ ~]+)+\/?)[\'\"]?\Z/ ) {
146      # See iz35982 for the reason for the special treatment of this switch.
147      # This regex evaluates <something>:///<path>, sometimes with quotes or "/" at the end
148      # option -> $1, filename without quotes -> $2
149      if ( defined $debug ) { print(STDERR "WinFormat:\nFound (<something>:///<path>):\n$variable\n"); }
150      $d1_prefix = $1."///";
151      $d1 = $2;
152      $d2 = myCygpath($2,1);
153      $d2 =~ s/\\/\//g ;
154  } elsif ( $variable =~ /\A(-\w)[\'\"]?((?:\/[\w\.\-\+ ~]+)+\/?)[\'\"]?\Z/ ) {
155	  # This regex evaluates -X<path>, sometimes with quotes or "/" at the end
156	  # option -> $1, filename without quotes -> $2
157	  if ( defined $debug ) { print(STDERR "WinFormat:\ninclude (-X<absolute path>) path:\n$variable\n"); }
158	  $d1_prefix = $1;
159	  $d1 = $2;
160	  $d2 = myCygpath($2,1);
161  } elsif ( $variable =~ /\A(-F[ARdemopr])[\'\"]?((?:\/[\w\.\-\+ ~]+)+\/?)[\'\"]?\Z/ ) {
162	  # This regex evaluates -FX<path> (MSVC switches for output naming), sometimes with quotes or "/" at the end
163	  # option -> $1, filename without quotes -> $2
164	  if ( defined $debug ) { print(STDERR "WinFormat:\ncompiler naming (-FX<absolute path>) path:\n$variable\n"); }
165	  $d1_prefix = $1;
166	  $d1 = $2;
167	  $d2 = myCygpath($2,1);
168  } else {
169      $d2 = "";
170  }
171  if ( $d2 ne "" ) {
172      # Found a parameter
173      $d1 =~ s/\+/\\\+/ ;
174      $d1 =~ s/\./\\\./ ;
175      $variable =~ s/$d1/$d2/ ;
176  } else {
177    # Found no parameter, assume a path
178    $variable =~ s/:/;/g;
179    $variable =~ s/([;]|\A)(\w);/$1$2:/g; # get back the drives
180
181    # Search for posix path ;entry; (The regex accepts valid paths with at least one /)
182    # and replace with DOS path, accept quotes.
183    # iz28717 Accept ',' as path seperator.
184    while ( $variable =~ /(?:[;,]|\A)[\'\"]?([\w\.\-\+ ~]*(?:\/[\w\.\-\+ ~]+)+\/?)[\'\"]?(?:[;,]|\Z)/ ) {
185        # Normal paths
186        $d1 = $1;
187        $d2 = myCygpath($d1);
188        if ( defined $debug ) {
189            print(STDERR "WinFormat:\nFull path:\n$variable\nTranslated part:$d2\n");
190        }
191	$d1 =~ s/\+/\\\+/ ;
192        $variable =~ s/$d1/$d2/ ;
193    }
194  }
195
196  # Sanity check for -X<path>
197  if ( $variable =~ /-\w[\'\"]?(?:(?:\/[\w\.\-\+ ~]+)+)/ ) {
198	  print(STDERR "Error: guw.pl: WinFormat: Not converted -X/... type switch in :$variable:.\n");
199	  if ( (defined $debug_light) or (defined $debug) ) { die "\nNot processed -X/...\n"; }
200  }
201  # Sanity check for [-]X<something>(:|=)<path> case
202  if ( $variable =~ /\A-?\w[\w\.]*[=:][\'\"]?(?:\/[\w\.\-\+ ~]+)+/ ) {
203	  print(STDERR "Error: guw.pl: WinFormat: Not converted [-]X<something>(=|:)/<path> type switch in :$variable:.\n");
204	  if ( (defined $debug_light) or (defined $debug) ) { die "\nNot processed [-]X<something>(=|:)/...\n"; }
205  }
206
207  if ( defined $debug ) { print(STDERR "WinFormat:\nresult:$variable\n");};
208  return $variable;
209}
210
211#----------------------------------------------------------
212# Function name: replace_cyg
213# Description:   Process all arguments and change them to Windows Format.
214# Arguments:     Reference to array with arguments
215# Return value:  -
216#----------------------------------------------------------
217sub replace_cyg {
218	my $args = shift;
219	my( @cmd_file, @cmd_temp );
220	my $atchars;
221	foreach my $para ( @$args )
222	  {
223		if ( $para =~ "^@" ) {
224		  # it's a command file
225		  if ( defined $debug ) { print(STDERR "----------------------------\n");};
226		  # Workaround, iz28717, keep number of @'s.
227		  $para =~ s/(^\@+)//;
228		  $atchars = $1;
229		  $filename = $para;
230		  if ( defined $debug ) { print(STDERR "filename = $filename \n");};
231		  # open this command file for reading
232		  open(CMD, "$filename");
233		  while ( <CMD> ) {
234			# Remove DOS lineendings. Bug in Cygwin / Perl?
235			$_ =~ s/\r//g;
236			# Remove lineendings and trailing spaces. ( Needed by &parse_line )
237			$_ =~ s/\n$//g;
238			$_ =~ s/\s+$//g;
239			# Fill all tokens into array
240            @cmd_temp = &parse_line('\s+', 1, $_ );
241			if ( $#cmd_temp > -1 ) {
242				push( @cmd_file, @cmd_temp);
243            }
244		  }
245		  close(CMD);
246		  # reformat all tokens
247		  replace_cyg(\@cmd_file);
248		  if ( defined $debug ) { print(STDERR "Tokens processed:\n");};
249		  foreach $i (@cmd_file) {
250			if ( defined $debug ) { print(STDERR "!".$i."!\n");};
251		  }
252		  # open this filename for writing (truncate) Textmode?
253		  open(CMD, '>', $filename);
254		  # write all tokens back into this file
255		  print(CMD join(' ', @cmd_file));
256		  close(CMD);
257		  # convert '@filename' to dos style
258		  $para = WinFormat( $para );
259		  if ( defined $debug ) { print(STDERR "----------------------------\n");};
260		  if ( (defined $debug_light) or (defined $debug) ) { print(STDERR "\nParameter in File:".join(' ', @cmd_file).":\n");}
261		  $para = $atchars.$para;
262		} else {
263		  # it's just a parameter
264		  if ( defined $debug ) { print(STDERR "\nParameter:---${para}---\n");};
265		  # If $tmp1 is empty then $para is a parameter.
266		  my $is_no_para = 1;
267		  # remove .exe and convert to lower case
268		  $shortcommand = lc $command ;
269		  $shortcommand =~ s/\.exe$//;
270		  $shortcommand =~ /([^\/]+$)/;
271		  $shortcommand = $1;
272		  foreach $i (@{$knownpara{$shortcommand}}) {
273			if( $para =~ /$i/ ) {
274			  $is_no_para = 0;
275			  if ( defined $debug ) { print(STDERR "Is parameter exception for ${shortcommand}: ${para}:\n" );};
276			  last;
277			}
278		  }
279		  if( $is_no_para ) {
280			$para = WinFormat($para);
281		  }
282		  if ( defined $debug ) { print(STDERR "Converted line:${para}:\n" );};
283		} # else
284	  } # foreach loop
285}
286
287#----------------------------------------------------------
288# Function name: replace_cyg_env
289# Description:   Process selected environment variables and change
290#                them to Windows Format.
291# Arguments:     -
292# Return value:  -
293#----------------------------------------------------------
294sub replace_cyg_env {
295    @affected_vars = (
296        'SOLAR_VERSION',
297        'SOLARVERSION',
298        'SOLARVER',
299        'SRC_ROOT',
300        'LOCALINI',
301        'GLOBALINI',
302        'SOLARENV',
303        'STAR_INSTPATH',
304        'STAR_SOLARPATH',
305        'STAR_PACKMISC',
306        'STAR_SOLARENVPATH',
307        'STAR_INITROOT',
308        'STAR_STANDLST',
309        'CLASSPATH',
310        'JAVA_HOME'
311    );
312    foreach my $one_var ( @affected_vars )
313    {
314        my $this_var = $ENV{ $one_var };
315        if ( defined $this_var )
316        {
317            if ( defined $debug ) { print(STDERR "ENV $one_var before: ".$ENV{ $one_var}."\n" );};
318            $ENV{ $one_var } = WinFormat( $this_var );
319            if ( defined $debug ) { print(STDERR "ENV $one_var after : ".$ENV{ $one_var}."\n" );};
320        }
321    }
322
323}
324#---------------------------------------------------------------------------
325# main
326@params = @ARGV;
327
328$command = shift(@params);
329while ( $command =~ /^-/ )
330{
331    if ( $command eq "-env" )
332    {
333        replace_cyg_env;
334    }
335
336    $command = shift(@params);
337}
338if ( (defined $debug_light) or (defined $debug) ) { print( STDERR "Command: $command\n" ); }
339
340replace_cyg(\@params);
341if ( (defined $debug_light) or (defined $debug) ) { print(STDERR "\n---------------------\nExecute: $command @params\n----------------\n");};
342exec( "$command", @params) or die( "\nError: guw.pl: executing $command failed!\n" );
343
344