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
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*$/ )
46 par2script
::remover
::remove_leading_and_ending_whitespaces
(\
$oneentry);
47 push(@
{$locallistref}, $oneentry);
52 par2script
::remover
::remove_leading_and_ending_whitespaces
(\
$list);
53 push(@
{$locallistref}, $list);
58 ############################################
59 # setting list of include paths
60 ############################################
66 # input is the comma separated list of include paths
68 my $includes = analyze_comma_separated_list
($list);
73 ############################################
74 # setting list of all par files
75 ############################################
81 # input is the name of the list file
82 $filename =~ s/\@//; # removing the leading \@
84 my $filecontent = par2script
::files
::read_file
($filename);
87 my $parfilesref = \
@parfiles;
89 foreach ( @
{$filecontent} ) { $parfilesref = analyze_comma_separated_list
($_, $parfilesref); }
94 ############################################
95 # finding the correct include path
97 ############################################
99 sub make_complete_paths_for_parfiles
101 my ($parfiles, $includes) = @_;
105 foreach $oneparfile ( @
{$parfiles} )
107 my $foundparfile = 0;
110 foreach $includepath ( @
{$includes} )
112 my $parfile = "$includepath/$oneparfile";
117 $oneparfile = $parfile;
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 = ();
143 # create empty item hashes
144 foreach $oneitem ( @par2script::globals
::allitems
) {
146 $par2script::globals
::definitions
{$oneitem} = \
%items;
149 for ( my $i = 0; $i <= $#{$parfilecontent}; $i++ )
151 my $line = ${$parfilecontent}[$i];
154 $line =~ /^\s*$/ && next; # skip blank lines
156 # lines should be well formed:
157 if ($line =~ m/^\s*(\w+)\s+(\w+)\s*$/)
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"); }
180 while (! ( ${$parfilecontent}[$i] =~ /^\s*End\s*$/i ) )
182 if ( ${$parfilecontent}[$i] =~ /^\s*(.+?)\s*\=\s*(.+?)\s*\;\s*$/ ) # only oneliner!
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;
196 my $allitemhash = \
$par2script::globals
::definitions
{$oneitem};
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}})
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" );
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 ######################################################
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); }
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};
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"}) ))
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 ######################################################
305 my ( $itemhash, $parent, $order ) = @_;
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};
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
338 foreach $parent ( @startparents ) { collect_children
($itemhash, $parent, \
@itemorder); }
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'};
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 = ();
363 foreach $modulegid (keys %{$allmodules} )
365 # print "Module $modulegid\n";
367 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\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, ",");
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
400 my @parfilecontent = ();
403 foreach $parfilename ( @
{$parfiles} )
405 my $parfile = par2script
::files
::read_file
($parfilename);
406 foreach ( @
{$parfile} ) { push(@parfilecontent, $_); }
407 push(@parfilecontent, "\n");
410 return \
@parfilecontent;