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 my $filecontent = par2script
::files
::read_file
($filename);
85 my $parfilesref = \
@parfiles;
87 foreach ( @
{$filecontent} ) { $parfilesref = analyze_comma_separated_list
($_, $parfilesref); }
92 ############################################
93 # finding the correct include path
95 ############################################
97 sub make_complete_paths_for_parfiles
99 my ($parfiles, $includes) = @_;
103 foreach $oneparfile ( @
{$parfiles} )
105 my $foundparfile = 0;
108 foreach $includepath ( @
{$includes} )
110 my $parfile = "$includepath/$oneparfile";
115 $oneparfile = $parfile;
120 if ( ! $foundparfile )
122 die "ERROR: Could not find parfile ${$parfiles}[$i] in includes paths: $par2script::globals::includepathlist !\n";
127 ######################################################
128 # collecting one special item in the par files and
129 # including it into the "definitions" hash
130 ######################################################
132 sub collect_definitions
134 my ($parfilecontent) = @_;
136 my $multidefinitionerror = 0;
137 my @multidefinitiongids = ();
141 # create empty item hashes
142 foreach $oneitem ( @par2script::globals
::allitems
) {
144 $par2script::globals
::definitions
{$oneitem} = \
%items;
147 for ( my $i = 0; $i <= $#{$parfilecontent}; $i++ )
149 my $line = ${$parfilecontent}[$i];
152 $line =~ /^\s*$/ && next; # skip blank lines
154 # lines should be well formed:
155 if ($line =~ m/^\s*(\w+)\s+(\w+)\s*$/)
162 $invalid =~ s/[\s\w]*//g;
163 par2script
::exiter
::exit_program
("ERROR: malformed par file, invalid character '$invalid', expecting <token> <gid> but saw '$line'", "test_par_syntax");
165 # print STDERR "line '$line' -> '$oneitem' '$gid'\n";
167 # hunt badness variously
168 if ( ! defined $par2script::globals
::definitions
{$oneitem} )
170 par2script
::exiter
::exit_program
("ERROR: invalid scp2 fragment item type '$oneitem' in line: '$line'", "test_par_syntax");
173 # no hyphen allowed in gids -> cannot happen here because (\w+) is required for gids
174 if ( $gid =~ /-/ ) { par2script
::exiter
::exit_program
("ERROR: No hyphen allowed in global id: $gid", "test_of_hyphen"); }
178 while (! ( ${$parfilecontent}[$i] =~ /^\s*End\s*$/i ) )
180 if ( ${$parfilecontent}[$i] =~ /^\s*(.+?)\s*\=\s*(.+?)\s*\;\s*$/ ) # only oneliner!
185 if ( $oneitem eq "Directory" ) { if ( $itemkey =~ "DosName" ) { $itemkey =~ s/DosName/HostName/; } }
186 if (( $oneitem eq "Directory" ) || ( $oneitem eq "File" ) || ( $oneitem eq "Unixlink" )) { if ( $itemvalue eq "PD_PROGDIR" ) { $itemvalue = "PREDEFINED_PROGDIR"; }}
187 if (( $itemkey eq "Styles" ) && ( $itemvalue =~ /^\s*(\w+)(\s*\;\s*)$/ )) { $itemvalue = "($1)$2"; }
188 elsif ( $itemkey eq "Files" ) # filter out empty file records, as they mess up assignment to modules
190 $itemvalue =~ /^\(([^)]*)\)$/;
191 $itemvalue = '(' . join( ',', grep( !/^$/, split( ',', $1 ) ) ) . ')';
194 $oneitemhash{$itemkey} = $itemvalue;
200 if ( defined ($par2script::globals
::definitions
{$oneitem}->{$gid}) )
202 $multidefinitionerror = 1;
203 push(@multidefinitiongids, $gid);
206 $par2script::globals
::definitions
{$oneitem}->{$gid} = \
%oneitemhash;
209 if ( $multidefinitionerror ) { par2script
::exiter
::multidefinitionerror
(\
@multidefinitiongids); }
211 # foreach $key (keys %par2script::globals::definitions)
213 # print "Key: $key \n";
215 # foreach $key (keys %{$par2script::globals::definitions{$key}})
222 ######################################################
223 # Filling content into the script
224 ######################################################
226 sub put_oneitem_into_script
228 my ( $script, $item, $itemhash, $itemkey ) = @_;
230 push(@
{$script}, "$item $itemkey\n" );
232 foreach $content (sort keys %{$itemhash->{$itemkey}}) { push(@
{$script}, "\t$content = $itemhash->{$itemkey}->{$content};\n" ); }
233 push(@
{$script}, "End\n" );
234 push(@
{$script}, "\n" );
237 ######################################################
238 # Creating the script
239 ######################################################
246 foreach $oneitem ( @par2script::globals
::allitems
)
248 if ( exists($par2script::globals
::definitions
{$oneitem}) )
250 if ( $oneitem eq "Shortcut" ) { next; } # "Shortcuts" after "Files"
252 if (( $oneitem eq "Module" ) || ( $oneitem eq "Directory" )) { write_sorted_items
(\
@script, $oneitem); }
253 else { write_unsorted_items
(\
@script, $oneitem); }
260 ######################################################
261 # Adding script content for the unsorted items
262 ######################################################
264 sub write_unsorted_items
266 my ( $script, $oneitem ) = @_;
268 my $itemhash = $par2script::globals
::definitions
{$oneitem};
271 foreach $itemkey (sort keys %{$itemhash})
273 put_oneitem_into_script
($script, $oneitem, $itemhash, $itemkey);
275 # special handling for Shortcuts after Files
276 if (( $oneitem eq "File" ) && ( exists($par2script::globals
::definitions
{"Shortcut"}) ))
279 foreach $shortcutkey ( keys %{$par2script::globals
::definitions
{"Shortcut"}} )
281 if ( $par2script::globals
::definitions
{"Shortcut"}->{$shortcutkey}->{'FileID'} eq $itemkey )
283 put_oneitem_into_script
($script, "Shortcut", $par2script::globals
::definitions
{"Shortcut"}, $shortcutkey);
285 # and Shortcut to Shortcut also
286 my $internshortcutkey;
287 foreach $internshortcutkey ( keys %{$par2script::globals
::definitions
{"Shortcut"}} )
289 if ( $par2script::globals
::definitions
{"Shortcut"}->{$internshortcutkey}->{'ShortcutID'} eq $shortcutkey )
291 put_oneitem_into_script
($script, "Shortcut", $par2script::globals
::definitions
{"Shortcut"}, $internshortcutkey);
300 ######################################################
301 # Collecting all children of a specified parent
302 ######################################################
306 my ( $itemhash, $parent, $order ) = @_;
309 foreach $item ( sort keys %{$itemhash} )
311 if ( $itemhash->{$item}->{'ParentID'} eq $parent )
313 push(@
{$order}, $item);
314 my $newparent = $item;
315 collect_children
($itemhash, $newparent, $order);
320 ######################################################
321 # Adding script content for the sorted items
322 ######################################################
324 sub write_sorted_items
326 my ( $script, $oneitem ) = @_;
328 my $itemhash = $par2script::globals
::definitions
{$oneitem};
331 my @startparents = ();
333 if ( $oneitem eq "Module" ) { push(@startparents, ""); }
334 elsif ( $oneitem eq "Directory" ) { push(@startparents, "PREDEFINED_PROGDIR"); }
335 else { die "ERROR: No root parent defined for item type $oneitem !\n"; }
337 # supporting more than one toplevel item
339 foreach $parent ( @startparents ) { collect_children
($itemhash, $parent, \
@itemorder); }
342 foreach $itemkey ( @itemorder ) { put_oneitem_into_script
($script, $oneitem, $itemhash, $itemkey); }
345 #######################################################################
346 # Collecting all assigned gids of the type "item" from the modules
347 # in the par files. Using a hash!
348 #######################################################################
350 sub collect_assigned_gids
352 my $allmodules = $par2script::globals
::definitions
{'Module'};
355 foreach $item ( @par2script::globals
::items_assigned_at_modules
)
357 if ( ! exists($par2script::globals
::searchkeys
{$item}) ) { par2script
::exiter
::exit_program
("ERROR: Unknown type \"$item\" at modules.", "collect_assigned_gids"); }
359 my $searchkey = $par2script::globals
::searchkeys
{$item};
361 my %assignitems = ();
364 foreach $modulegid (keys %{$allmodules} )
366 # print "Module $modulegid\n";
368 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
372 if ( exists($allmodules->{$modulegid}->{$searchkey}) )
374 my $list = $allmodules->{$modulegid}->{$searchkey};
375 if ( $list =~ /^\s*\((.*?)\)\s*(.*?)\s*$/ ) { $list = $1; }
376 else { par2script
::exiter
::exit_program
("ERROR: Invalid module list: $list", "collect_assigned_gids"); }
377 my $allassigneditems = par2script
::converter
::convert_stringlist_into_array_2
($list, ",");
380 foreach $gid ( @
{$allassigneditems} )
382 if ( exists($assignitems{$gid}) ) { $assignitems{$gid} = $assignitems{$gid} + 1; }
383 else { $assignitems{$gid} = 1; }
388 $par2script::globals
::assignedgids
{$item} = \
%assignitems;
392 ##################################################
393 # Collecting the content of all par files.
394 # Then the files do not need to be opened twice.
395 ##################################################
397 sub read_all_parfiles
401 my @parfilecontent = ();
404 foreach $parfilename ( @
{$parfiles} )
406 my $parfile = par2script
::files
::read_file
($parfilename);
407 foreach ( @
{$parfile} ) { push(@parfilecontent, $_); }
408 push(@parfilecontent, "\n");
411 return \
@parfilecontent;