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