bump product version to 4.1.6.2
[LibreOffice.git] / solenv / bin / modules / par2script / work.pm
blobe46f8d82d7030a1b6b7fcf769ec55e6a1f16f5c0
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package par2script::work;
22 use par2script::globals;
23 use par2script::remover;
25 ############################################
26 # par2script working module
27 ############################################
29 sub analyze_comma_separated_list
31 my ($list, $listref) = @_; # second parameter is optional
33 my @list = ();
34 my $locallistref;
36 if (!( $listref )) { $locallistref = \@list; }
37 else { $locallistref = $listref; }
39 par2script::remover::remove_leading_and_ending_comma(\$list);
40 par2script::remover::remove_leading_and_ending_whitespaces(\$list);
42 while ( $list =~ /^\s*(.*?)\s*\,\s*(.*)\s*$/ )
44 my $oneentry = $1;
45 $list = $2;
46 par2script::remover::remove_leading_and_ending_whitespaces(\$oneentry);
47 push(@{$locallistref}, $oneentry);
50 # the last entry
52 par2script::remover::remove_leading_and_ending_whitespaces(\$list);
53 push(@{$locallistref}, $list);
55 return $locallistref;
58 ############################################
59 # setting list of include paths
60 ############################################
62 sub setincludes
64 my ($list) = @_;
66 # input is the comma separated list of include paths
68 my $includes = analyze_comma_separated_list($list);
70 return $includes;
73 ############################################
74 # setting list of all par files
75 ############################################
77 sub setparfiles
79 my ($filename) = @_;
81 # input is the name of the list file
82 $filename =~ s/\@//; # removing the leading \@
84 my $filecontent = par2script::files::read_file($filename);
86 my @parfiles = ();
87 my $parfilesref = \@parfiles;
89 foreach ( @{$filecontent} ) { $parfilesref = analyze_comma_separated_list($_, $parfilesref); }
91 return $parfilesref;
94 ############################################
95 # finding the correct include path
96 # for the par files
97 ############################################
99 sub make_complete_paths_for_parfiles
101 my ($parfiles, $includes) = @_;
103 my $oneparfile;
105 foreach $oneparfile ( @{$parfiles} )
107 my $foundparfile = 0;
108 my $includepath;
110 foreach $includepath ( @{$includes} )
112 my $parfile = "$includepath/$oneparfile";
114 if ( -f $parfile )
116 $foundparfile = 1;
117 $oneparfile = $parfile;
118 last;
122 if ( ! $foundparfile )
124 die "ERROR: Could not find parfile ${$parfiles}[$i] in includes paths: $par2script::globals::includepathlist !\n";
129 ######################################################
130 # collecting one special item in the par files and
131 # including it into the "definitions" hash
132 ######################################################
134 sub collect_definitions
136 my ($parfilecontent) = @_;
138 my $multidefinitionerror = 0;
139 my @multidefinitiongids = ();
141 my %itemhash;
143 # create empty item hashes
144 foreach $oneitem ( @par2script::globals::allitems ) {
145 my %items;
146 $par2script::globals::definitions{$oneitem} = \%items;
149 for ( my $i = 0; $i <= $#{$parfilecontent}; $i++ )
151 my $line = ${$parfilecontent}[$i];
152 my $oneitem, $gid;
154 $line =~ /^\s*$/ && next; # skip blank lines
156 # lines should be well formed:
157 if ($line =~ m/^\s*(\w+)\s+(\w+)\s*$/)
159 $oneitem = $1;
160 $gid = $2;
161 } else {
162 chomp ($line);
163 my $invalid = $line;
164 $invalid =~ s/[\s\w]*//g;
165 par2script::exiter::exit_program("ERROR: malformed par file, invalid character '$invalid', expecting <token> <gid> but saw '$line'", "test_par_syntax");
167 # print STDERR "line '$line' -> '$oneitem' '$gid'\n";
169 # hunt badness variously
170 if ( ! defined $par2script::globals::definitions{$oneitem} )
172 par2script::exiter::exit_program("ERROR: invalid scp2 fragment item type '$oneitem' in line: '$line'", "test_par_syntax");
175 # no hyphen allowed in gids -> cannot happen here because (\w+) is required for gids
176 if ( $gid =~ /-/ ) { par2script::exiter::exit_program("ERROR: No hyphen allowed in global id: $gid", "test_of_hyphen"); }
178 my %oneitemhash;
180 while (! ( ${$parfilecontent}[$i] =~ /^\s*End\s*$/i ) )
182 if ( ${$parfilecontent}[$i] =~ /^\s*(.+?)\s*\=\s*(.+?)\s*\;\s*$/ ) # only oneliner!
184 $itemkey = $1;
185 $itemvalue = $2;
187 if ( $oneitem eq "Directory" ) { if ( $itemkey =~ "DosName" ) { $itemkey =~ s/DosName/HostName/; } }
188 if (( $oneitem eq "Directory" ) || ( $oneitem eq "File" ) || ( $oneitem eq "Unixlink" )) { if ( $itemvalue eq "PD_PROGDIR" ) { $itemvalue = "PREDEFINED_PROGDIR"; }}
189 if (( $itemkey eq "Styles" ) && ( $itemvalue =~ /^\s*(\w+)(\s*\;\s*)$/ )) { $itemvalue = "($1)$2"; }
191 $oneitemhash{$itemkey} = $itemvalue;
193 $i++;
196 my $allitemhash = \$par2script::globals::definitions{$oneitem};
198 # test of uniqueness
199 if ( defined ($par2script::globals::definitions{$oneitem}->{$gid}) )
201 $multidefinitionerror = 1;
202 push(@multidefinitiongids, $gid);
205 $par2script::globals::definitions{$oneitem}->{$gid} = \%oneitemhash;
208 if ( $multidefinitionerror ) { par2script::exiter::multidefinitionerror(\@multidefinitiongids); }
210 # foreach $key (keys %par2script::globals::definitions)
212 # print "Key: $key \n";
214 # foreach $key (keys %{$par2script::globals::definitions{$key}})
216 # print "\t$key \n";
221 ######################################################
222 # Filling content into the script
223 ######################################################
225 sub put_oneitem_into_script
227 my ( $script, $item, $itemhash, $itemkey ) = @_;
229 push(@{$script}, "$item $itemkey\n" );
230 my $content = "";
231 foreach $content (sort keys %{$itemhash->{$itemkey}}) { push(@{$script}, "\t$content = $itemhash->{$itemkey}->{$content};\n" ); }
232 push(@{$script}, "End\n" );
233 push(@{$script}, "\n" );
236 ######################################################
237 # Creating the script
238 ######################################################
240 sub create_script
242 my @script = ();
243 my $oneitem;
245 foreach $oneitem ( @par2script::globals::allitems )
247 if ( exists($par2script::globals::definitions{$oneitem}) )
249 if ( $oneitem eq "Shortcut" ) { next; } # "Shortcuts" after "Files"
251 if (( $oneitem eq "Module" ) || ( $oneitem eq "Directory" )) { write_sorted_items(\@script, $oneitem); }
252 else { write_unsorted_items(\@script, $oneitem); }
256 return \@script;
259 ######################################################
260 # Adding script content for the unsorted items
261 ######################################################
263 sub write_unsorted_items
265 my ( $script, $oneitem ) = @_;
267 my $itemhash = $par2script::globals::definitions{$oneitem};
269 my $itemkey = "";
270 foreach $itemkey (sort keys %{$itemhash})
272 put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey);
274 # special handling for Shortcuts after Files
275 if (( $oneitem eq "File" ) && ( exists($par2script::globals::definitions{"Shortcut"}) ))
277 my $shortcutkey;
278 foreach $shortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} )
280 if ( $par2script::globals::definitions{"Shortcut"}->{$shortcutkey}->{'FileID'} eq $itemkey )
282 put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $shortcutkey);
284 # and Shortcut to Shortcut also
285 my $internshortcutkey;
286 foreach $internshortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} )
288 if ( $par2script::globals::definitions{"Shortcut"}->{$internshortcutkey}->{'ShortcutID'} eq $shortcutkey )
290 put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $internshortcutkey);
299 ######################################################
300 # Collecting all children of a specified parent
301 ######################################################
303 sub collect_children
305 my ( $itemhash, $parent, $order ) = @_;
307 my $item;
308 foreach $item ( sort keys %{$itemhash} )
310 if ( $itemhash->{$item}->{'ParentID'} eq $parent )
312 push(@{$order}, $item);
313 my $newparent = $item;
314 collect_children($itemhash, $newparent, $order);
319 ######################################################
320 # Adding script content for the sorted items
321 ######################################################
323 sub write_sorted_items
325 my ( $script, $oneitem ) = @_;
327 my $itemhash = $par2script::globals::definitions{$oneitem};
329 my @itemorder = ();
330 my @startparents = ();
332 if ( $oneitem eq "Module" ) { push(@startparents, ""); }
333 elsif ( $oneitem eq "Directory" ) { push(@startparents, "PREDEFINED_PROGDIR"); }
334 else { die "ERROR: No root parent defined for item type $oneitem !\n"; }
336 # supporting more than one toplevel item
337 my $parent;
338 foreach $parent ( @startparents ) { collect_children($itemhash, $parent, \@itemorder); }
340 my $itemkey;
341 foreach $itemkey ( @itemorder ) { put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey); }
344 #######################################################################
345 # Collecting all assigned gids of the type "item" from the modules
346 # in the par files. Using a hash!
347 #######################################################################
349 sub collect_assigned_gids
351 my $allmodules = $par2script::globals::definitions{'Module'};
353 my $item;
354 foreach $item ( @par2script::globals::items_assigned_at_modules )
356 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "collect_assigned_gids"); }
358 my $searchkey = $par2script::globals::searchkeys{$item};
360 my %assignitems = ();
361 my $modulegid = "";
363 foreach $modulegid (keys %{$allmodules} )
365 # print "Module $modulegid\n";
366 # my $content = "";
367 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
368 # print "End\n";
369 # print "\n";
371 if ( exists($allmodules->{$modulegid}->{$searchkey}) )
373 my $list = $allmodules->{$modulegid}->{$searchkey};
374 if ( $list =~ /^\s*\((.*?)\)\s*(.*?)\s*$/ ) { $list = $1; }
375 else { par2script::exiter::exit_program("ERROR: Invalid module list: $list", "collect_assigned_gids"); }
376 my $allassigneditems = par2script::converter::convert_stringlist_into_array_2($list, ",");
378 my $gid;
379 foreach $gid ( @{$allassigneditems} )
381 if ( exists($assignitems{$gid}) ) { $assignitems{$gid} = $assignitems{$gid} + 1; }
382 else { $assignitems{$gid} = 1; }
387 $par2script::globals::assignedgids{$item} = \%assignitems;
391 ##################################################
392 # Collecting the content of all par files.
393 # Then the files do not need to be opened twice.
394 ##################################################
396 sub read_all_parfiles
398 my ($parfiles) = @_;
400 my @parfilecontent = ();
401 my $parfilename;
403 foreach $parfilename ( @{$parfiles} )
405 my $parfile = par2script::files::read_file($parfilename);
406 foreach ( @{$parfile} ) { push(@parfilecontent, $_); }
407 push(@parfilecontent, "\n");
410 return \@parfilecontent;