| 1 |
# |
| 2 |
# This file is part of the LibreOffice project. |
| 3 |
# |
| 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/. |
| 7 |
# |
| 8 |
# This file incorporates work covered by the following license notice: |
| 9 |
# |
| 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 . |
| 17 |
# |
| 18 |
|
| 19 |
|
| 20 |
package par2script::work; |
| 21 |
|
| 22 |
use par2script::globals; |
| 23 |
use par2script::remover; |
| 24 |
|
| 25 |
############################################ |
| 26 |
# par2script working module |
| 27 |
############################################ |
| 28 |
|
| 29 |
sub analyze_comma_separated_list |
| 30 |
{ |
| 31 |
my ($list, $listref) = @_; # second parameter is optional |
| 32 |
|
| 33 |
my @list = (); |
| 34 |
my $locallistref; |
| 35 |
|
| 36 |
if (!( $listref )) { $locallistref = \@list; } |
| 37 |
else { $locallistref = $listref; } |
| 38 |
|
| 39 |
par2script::remover::remove_leading_and_ending_comma(\$list); |
| 40 |
par2script::remover::remove_leading_and_ending_whitespaces(\$list); |
| 41 |
|
| 42 |
while ( $list =~ /^\s*(.*?)\s*\,\s*(.*)\s*$/ ) |
| 43 |
{ |
| 44 |
my $oneentry = $1; |
| 45 |
$list = $2; |
| 46 |
par2script::remover::remove_leading_and_ending_whitespaces(\$oneentry); |
| 47 |
push(@{$locallistref}, $oneentry); |
| 48 |
} |
| 49 |
|
| 50 |
# the last entry |
| 51 |
|
| 52 |
par2script::remover::remove_leading_and_ending_whitespaces(\$list); |
| 53 |
push(@{$locallistref}, $list); |
| 54 |
|
| 55 |
return $locallistref; |
| 56 |
} |
| 57 |
|
| 58 |
############################################ |
| 59 |
# setting list of include paths |
| 60 |
############################################ |
| 61 |
|
| 62 |
sub setincludes |
| 63 |
{ |
| 64 |
my ($list) = @_; |
| 65 |
|
| 66 |
# input is the comma separated list of include paths |
| 67 |
|
| 68 |
my $includes = analyze_comma_separated_list($list); |
| 69 |
|
| 70 |
return $includes; |
| 71 |
} |
| 72 |
|
| 73 |
############################################ |
| 74 |
# setting list of all par files |
| 75 |
############################################ |
| 76 |
|
| 77 |
sub setparfiles |
| 78 |
{ |
| 79 |
my ($filename) = @_; |
| 80 |
|
| 81 |
# input is the name of the list file |
| 82 |
my $filecontent = par2script::files::read_file($filename); |
| 83 |
|
| 84 |
my @parfiles = (); |
| 85 |
my $parfilesref = \@parfiles; |
| 86 |
|
| 87 |
foreach ( @{$filecontent} ) { $parfilesref = analyze_comma_separated_list($_, $parfilesref); } |
| 88 |
|
| 89 |
return $parfilesref; |
| 90 |
} |
| 91 |
|
| 92 |
############################################ |
| 93 |
# finding the correct include path |
| 94 |
# for the par files |
| 95 |
############################################ |
| 96 |
|
| 97 |
sub make_complete_paths_for_parfiles |
| 98 |
{ |
| 99 |
my ($parfiles, $includes) = @_; |
| 100 |
|
| 101 |
my $oneparfile; |
| 102 |
|
| 103 |
foreach $oneparfile ( @{$parfiles} ) |
| 104 |
{ |
| 105 |
my $foundparfile = 0; |
| 106 |
my $includepath; |
| 107 |
|
| 108 |
foreach $includepath ( @{$includes} ) |
| 109 |
{ |
| 110 |
my $parfile = "$includepath/$oneparfile"; |
| 111 |
|
| 112 |
if ( -f $parfile ) |
| 113 |
{ |
| 114 |
$foundparfile = 1; |
| 115 |
$oneparfile = $parfile; |
| 116 |
last; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! $foundparfile ) |
| 121 |
{ |
| 122 |
die "ERROR: Could not find parfile ${$parfiles}[$i] in includes paths: $par2script::globals::includepathlist !\n"; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
###################################################### |
| 128 |
# collecting one special item in the par files and |
| 129 |
# including it into the "definitions" hash |
| 130 |
###################################################### |
| 131 |
|
| 132 |
sub collect_definitions |
| 133 |
{ |
| 134 |
my ($parfilecontent) = @_; |
| 135 |
|
| 136 |
my $multidefinitionerror = 0; |
| 137 |
my @multidefinitiongids = (); |
| 138 |
|
| 139 |
my %itemhash; |
| 140 |
|
| 141 |
# create empty item hashes |
| 142 |
foreach $oneitem ( @par2script::globals::allitems ) { |
| 143 |
my %items; |
| 144 |
$par2script::globals::definitions{$oneitem} = \%items; |
| 145 |
} |
| 146 |
|
| 147 |
for ( my $i = 0; $i <= $#{$parfilecontent}; $i++ ) |
| 148 |
{ |
| 149 |
my $line = ${$parfilecontent}[$i]; |
| 150 |
my $oneitem, $gid; |
| 151 |
|
| 152 |
$line =~ /^\s*$/ && next; # skip blank lines |
| 153 |
|
| 154 |
# lines should be well formed: |
| 155 |
if ($line =~ m/^\s*(\w+)\s+(\w+)\s*$/) |
| 156 |
{ |
| 157 |
$oneitem = $1; |
| 158 |
$gid = $2; |
| 159 |
} else { |
| 160 |
chomp ($line); |
| 161 |
my $invalid = $line; |
| 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"); |
| 164 |
} |
| 165 |
# print STDERR "line '$line' -> '$oneitem' '$gid'\n"; |
| 166 |
|
| 167 |
# hunt badness variously |
| 168 |
if ( ! defined $par2script::globals::definitions{$oneitem} ) |
| 169 |
{ |
| 170 |
par2script::exiter::exit_program("ERROR: invalid scp2 fragment item type '$oneitem' in line: '$line'", "test_par_syntax"); |
| 171 |
} |
| 172 |
|
| 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"); } |
| 175 |
|
| 176 |
my %oneitemhash; |
| 177 |
|
| 178 |
while (! ( ${$parfilecontent}[$i] =~ /^\s*End\s*$/i ) ) |
| 179 |
{ |
| 180 |
if ( ${$parfilecontent}[$i] =~ /^\s*(.+?)\s*\=\s*(.+?)\s*\;\s*$/ ) # only oneliner! |
| 181 |
{ |
| 182 |
$itemkey = $1; |
| 183 |
$itemvalue = $2; |
| 184 |
|
| 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 |
| 189 |
{ |
| 190 |
$itemvalue =~ /^\(([^)]*)\)$/; |
| 191 |
$itemvalue = '(' . join( ',', grep( !/^$/, split( ',', $1 ) ) ) . ')'; |
| 192 |
} |
| 193 |
|
| 194 |
$oneitemhash{$itemkey} = $itemvalue; |
| 195 |
} |
| 196 |
$i++; |
| 197 |
} |
| 198 |
|
| 199 |
# test of uniqueness |
| 200 |
if ( defined ($par2script::globals::definitions{$oneitem}->{$gid}) ) |
| 201 |
{ |
| 202 |
$multidefinitionerror = 1; |
| 203 |
push(@multidefinitiongids, $gid); |
| 204 |
} |
| 205 |
|
| 206 |
$par2script::globals::definitions{$oneitem}->{$gid} = \%oneitemhash; |
| 207 |
} |
| 208 |
|
| 209 |
if ( $multidefinitionerror ) { par2script::exiter::multidefinitionerror(\@multidefinitiongids); } |
| 210 |
|
| 211 |
# foreach $key (keys %par2script::globals::definitions) |
| 212 |
# { |
| 213 |
# print "Key: $key \n"; |
| 214 |
# |
| 215 |
# foreach $key (keys %{$par2script::globals::definitions{$key}}) |
| 216 |
# { |
| 217 |
# print "\t$key \n"; |
| 218 |
# } |
| 219 |
# } |
| 220 |
} |
| 221 |
|
| 222 |
###################################################### |
| 223 |
# Filling content into the script |
| 224 |
###################################################### |
| 225 |
|
| 226 |
sub put_oneitem_into_script |
| 227 |
{ |
| 228 |
my ( $script, $item, $itemhash, $itemkey ) = @_; |
| 229 |
|
| 230 |
push(@{$script}, "$item $itemkey\n" ); |
| 231 |
my $content = ""; |
| 232 |
foreach $content (sort keys %{$itemhash->{$itemkey}}) { push(@{$script}, "\t$content = $itemhash->{$itemkey}->{$content};\n" ); } |
| 233 |
push(@{$script}, "End\n" ); |
| 234 |
push(@{$script}, "\n" ); |
| 235 |
} |
| 236 |
|
| 237 |
###################################################### |
| 238 |
# Creating the script |
| 239 |
###################################################### |
| 240 |
|
| 241 |
sub create_script |
| 242 |
{ |
| 243 |
my @script = (); |
| 244 |
my $oneitem; |
| 245 |
|
| 246 |
foreach $oneitem ( @par2script::globals::allitems ) |
| 247 |
{ |
| 248 |
if ( exists($par2script::globals::definitions{$oneitem}) ) |
| 249 |
{ |
| 250 |
if ( $oneitem eq "Shortcut" ) { next; } # "Shortcuts" after "Files" |
| 251 |
|
| 252 |
if (( $oneitem eq "Module" ) || ( $oneitem eq "Directory" )) { write_sorted_items(\@script, $oneitem); } |
| 253 |
else { write_unsorted_items(\@script, $oneitem); } |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
return \@script; |
| 258 |
} |
| 259 |
|
| 260 |
###################################################### |
| 261 |
# Adding script content for the unsorted items |
| 262 |
###################################################### |
| 263 |
|
| 264 |
sub write_unsorted_items |
| 265 |
{ |
| 266 |
my ( $script, $oneitem ) = @_; |
| 267 |
|
| 268 |
my $itemhash = $par2script::globals::definitions{$oneitem}; |
| 269 |
|
| 270 |
my $itemkey = ""; |
| 271 |
foreach $itemkey (sort keys %{$itemhash}) |
| 272 |
{ |
| 273 |
put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey); |
| 274 |
|
| 275 |
# special handling for Shortcuts after Files |
| 276 |
if (( $oneitem eq "File" ) && ( exists($par2script::globals::definitions{"Shortcut"}) )) |
| 277 |
{ |
| 278 |
my $shortcutkey; |
| 279 |
foreach $shortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} ) |
| 280 |
{ |
| 281 |
if ( $par2script::globals::definitions{"Shortcut"}->{$shortcutkey}->{'FileID'} eq $itemkey ) |
| 282 |
{ |
| 283 |
put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $shortcutkey); |
| 284 |
|
| 285 |
# and Shortcut to Shortcut also |
| 286 |
my $internshortcutkey; |
| 287 |
foreach $internshortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} ) |
| 288 |
{ |
| 289 |
if ( $par2script::globals::definitions{"Shortcut"}->{$internshortcutkey}->{'ShortcutID'} eq $shortcutkey ) |
| 290 |
{ |
| 291 |
put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $internshortcutkey); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
###################################################### |
| 301 |
# Collecting all children of a specified parent |
| 302 |
###################################################### |
| 303 |
|
| 304 |
sub collect_children |
| 305 |
{ |
| 306 |
my ( $itemhash, $parent, $order ) = @_; |
| 307 |
|
| 308 |
my $item; |
| 309 |
foreach $item ( sort keys %{$itemhash} ) |
| 310 |
{ |
| 311 |
if ( $itemhash->{$item}->{'ParentID'} eq $parent ) |
| 312 |
{ |
| 313 |
push(@{$order}, $item); |
| 314 |
my $newparent = $item; |
| 315 |
collect_children($itemhash, $newparent, $order); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
###################################################### |
| 321 |
# Adding script content for the sorted items |
| 322 |
###################################################### |
| 323 |
|
| 324 |
sub write_sorted_items |
| 325 |
{ |
| 326 |
my ( $script, $oneitem ) = @_; |
| 327 |
|
| 328 |
my $itemhash = $par2script::globals::definitions{$oneitem}; |
| 329 |
|
| 330 |
my @itemorder = (); |
| 331 |
my @startparents = (); |
| 332 |
|
| 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"; } |
| 336 |
|
| 337 |
# supporting more than one toplevel item |
| 338 |
my $parent; |
| 339 |
foreach $parent ( @startparents ) { collect_children($itemhash, $parent, \@itemorder); } |
| 340 |
|
| 341 |
my $itemkey; |
| 342 |
foreach $itemkey ( @itemorder ) { put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey); } |
| 343 |
} |
| 344 |
|
| 345 |
####################################################################### |
| 346 |
# Collecting all assigned gids of the type "item" from the modules |
| 347 |
# in the par files. Using a hash! |
| 348 |
####################################################################### |
| 349 |
|
| 350 |
sub collect_assigned_gids |
| 351 |
{ |
| 352 |
my $allmodules = $par2script::globals::definitions{'Module'}; |
| 353 |
|
| 354 |
my $item; |
| 355 |
foreach $item ( @par2script::globals::items_assigned_at_modules ) |
| 356 |
{ |
| 357 |
if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "collect_assigned_gids"); } |
| 358 |
|
| 359 |
my $searchkey = $par2script::globals::searchkeys{$item}; |
| 360 |
|
| 361 |
my %assignitems = (); |
| 362 |
my $modulegid = ""; |
| 363 |
|
| 364 |
foreach $modulegid (keys %{$allmodules} ) |
| 365 |
{ |
| 366 |
# print "Module $modulegid\n"; |
| 367 |
# my $content = ""; |
| 368 |
# foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; } |
| 369 |
# print "End\n"; |
| 370 |
# print "\n"; |
| 371 |
|
| 372 |
if ( exists($allmodules->{$modulegid}->{$searchkey}) ) |
| 373 |
{ |
| 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, ","); |
| 378 |
|
| 379 |
my $gid; |
| 380 |
foreach $gid ( @{$allassigneditems} ) |
| 381 |
{ |
| 382 |
if ( exists($assignitems{$gid}) ) { $assignitems{$gid} = $assignitems{$gid} + 1; } |
| 383 |
else { $assignitems{$gid} = 1; } |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
$par2script::globals::assignedgids{$item} = \%assignitems; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
################################################## |
| 393 |
# Collecting the content of all par files. |
| 394 |
# Then the files do not need to be opened twice. |
| 395 |
################################################## |
| 396 |
|
| 397 |
sub read_all_parfiles |
| 398 |
{ |
| 399 |
my ($parfiles) = @_; |
| 400 |
|
| 401 |
my @parfilecontent = (); |
| 402 |
my $parfilename; |
| 403 |
|
| 404 |
foreach $parfilename ( @{$parfiles} ) |
| 405 |
{ |
| 406 |
my $parfile = par2script::files::read_file($parfilename); |
| 407 |
foreach ( @{$parfile} ) { push(@parfilecontent, $_); } |
| 408 |
push(@parfilecontent, "\n"); |
| 409 |
} |
| 410 |
|
| 411 |
return \@parfilecontent; |
| 412 |
} |
| 413 |
|
| 414 |
1; |