1#************************************************************** 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20#************************************************************** 21 22 23 24package par2script::module; 25 26use par2script::converter; 27use par2script::exiter; 28 29########################################### 30# Removing undefined gids 31# from modules 32########################################### 33 34sub remove_from_modules 35{ 36 my ($gid, $item) = @_; 37 38 my $counter = 0; 39 40 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); } 41 my $searchkey = $par2script::globals::searchkeys{$item}; 42 43 my $allmodules = $par2script::globals::definitions{'Module'}; 44 45 my $onemodule; 46 foreach $onemodule (keys %{$allmodules}) 47 { 48 if (( exists($allmodules->{$onemodule}->{$searchkey}) ) && ( $allmodules->{$onemodule}->{$searchkey} =~ /\b$gid\b/ )) 49 { 50 my $infoline = "WARNING: Removing $gid because of missing definition\n"; 51 # print $infoline; 52 push(@par2script::globals::logfileinfo, $infoline); 53 54 $allmodules->{$onemodule}->{$searchkey} =~ s/\b$gid\b//; 55 $allmodules->{$onemodule}->{$searchkey} =~ s/\,\s*\,/\,/; 56 $allmodules->{$onemodule}->{$searchkey} =~ s/\(\s*\,\s*/\(/; 57 $allmodules->{$onemodule}->{$searchkey} =~ s/\s*\,\s*\)/\)/; 58 59 if (( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\,\s*\)/ ) || 60 ( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\)/ )) 61 { 62 delete($allmodules->{$onemodule}->{$searchkey}); 63 } 64 65 $counter++; 66 } 67 } 68 69 return $counter; 70} 71 72########################################### 73# Removing undefined gids automatically 74# from modules 75########################################### 76 77sub remove_undefined_gids_from_modules 78{ 79 # If assigned gids for "File", "Directory" or "Unixlink" are not defined, 80 # they are automatically removed from the module 81 82 foreach $item ( @par2script::globals::items_assigned_at_modules ) 83 { 84 my $assignedgids = $par2script::globals::assignedgids{$item}; 85 my $definedgids = $par2script::globals::definitions{$item}; 86 87 my $gid; 88 foreach $gid ( keys %{$assignedgids} ) 89 { 90 if ( ! exists( $definedgids->{$gid} )) 91 { 92 # deleting entry in module definition 93 my $number_of_removals = remove_from_modules($gid, $item); 94 # decreasing counter in assignments 95 if ( $assignedgids->{$gid} > $number_of_removals ) { $assignedgids->{$gid} = $assignedgids->{$gid} - $number_of_removals; } 96 else { delete($assignedgids->{$gid}); } 97 } 98 } 99 } 100} 101 102############################################ 103# Getting the gid of the root module. The 104# root module has no ParentID or an empty 105# ParentID. 106############################################ 107 108sub get_rootmodule_gid 109{ 110 my $rootgid = ""; 111 my $foundroot = 0; 112 113 my $allmodules = $par2script::globals::definitions{'Module'}; 114 115 my $modulegid = ""; 116 foreach $modulegid (keys %{$allmodules} ) 117 { 118 # print "Module $modulegid\n"; 119 # my $content = ""; 120 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; } 121 # print "End\n"; 122 # print "\n"; 123 124 if (( ! exists($allmodules->{$modulegid}->{'ParentID'})) || ( $allmodules->{$modulegid}->{'ParentID'} eq "" )) 125 { 126 if ( $foundroot ) { par2script::exiter::exit_program("ERROR: More than one Root module. Only one module without ParentID or with empty ParentID allowed ($rootgid and $modulegid).", "get_rootmodule_gid"); } 127 $rootgid = $modulegid; 128 $foundroot = 1; 129 } 130 } 131 132 if ( ! $foundroot ) { par2script::exiter::exit_program("ERROR: Could not find Root module. Did not find module without ParentID or with empty ParentID.", "get_rootmodule_gid"); } 133 134 return $rootgid; 135} 136 137#################################### 138# Adding defined items without 139# assignment to the root module. 140#################################### 141 142sub add_to_root_module 143{ 144 # If defined gids for "File", "Directory" or "Unixlink" are not assigned, 145 # they are automatically assigned to the root module 146 147 my $rootmodulegid = get_rootmodule_gid(); 148 149 my $item; 150 foreach $item ( @par2script::globals::items_assigned_at_modules ) 151 { 152 my $assignedgids = $par2script::globals::assignedgids{$item}; 153 my $definedgids = $par2script::globals::definitions{$item}; 154 155 my $gidstring = ""; 156 157 # Perhaps there are already items assigned to the root 158 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); } 159 my $modulekey = $par2script::globals::searchkeys{$item}; 160 if ( exists($par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey}) ) 161 { 162 $gidstring = $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey}; 163 $gidstring =~ s/\(//; 164 $gidstring =~ s/\)//; 165 } 166 167 my $gid; 168 foreach $gid ( keys %{$definedgids} ) 169 { 170 if ( ! exists( $assignedgids->{$gid} )) 171 { 172 if ( $gidstring eq "" ) 173 { 174 $gidstring = $gid; 175 } 176 else 177 { 178 $gidstring = "$gidstring,$gid"; 179 } 180 181 $assignedgids->{$gid} = 1; 182 } 183 } 184 185 if ( $gidstring ne "" ) 186 { 187 $gidstring = "\($gidstring\)"; 188 $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey} = $gidstring; 189 } 190 } 191} 192 193################################################### 194# Including \n in a very long string 195################################################### 196 197sub include_linebreaks 198{ 199 my ($allgidstring) = @_; 200 201 my $newline = ""; 202 my $newlength = 0; 203 204 $allgidstring =~ s/\(//; 205 $allgidstring =~ s/\)//; 206 207 my $allgids = par2script::converter::convert_stringlist_into_array_2($allgidstring, ","); 208 209 if ( $#{$allgids} > -1 ) 210 { 211 my $onegid; 212 foreach $onegid ( @{$allgids} ) 213 { 214 $newline = "$newline$onegid,"; 215 $newlength = $newlength + length($onegid) + 1; # +1 for the comma 216 217 if ( $newlength > 80 ) 218 { 219 $newline = $newline . "\n\t\t\t\t"; 220 $newlength = 0; 221 } 222 } 223 } 224 225 $newline =~ s/,\s*$//; 226 $newline = "($newline)"; 227 228 return $newline; 229} 230 231################################################### 232# Shorten the lines that belong to modules, if 233# the length of the line is greater 100 234################################################### 235 236sub shorten_lines_at_modules 237{ 238 my $item; 239 foreach $item ( @par2script::globals::items_assigned_at_modules ) 240 { 241 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "shorten_lines_at_modules"); } 242 my $searchkey = $par2script::globals::searchkeys{$item}; 243 244 my $allmodules = $par2script::globals::definitions{'Module'}; 245 246 my $onemodule; 247 foreach $onemodule (keys %{$allmodules}) 248 { 249 if (( exists($allmodules->{$onemodule}->{$searchkey}) ) && 250 ( length($allmodules->{$onemodule}->{$searchkey}) > 100 )) 251 { 252 # including "\n\t\t\t\t" 253 my $newstring = include_linebreaks($allmodules->{$onemodule}->{$searchkey}); 254 $allmodules->{$onemodule}->{$searchkey} = $newstring; 255 } 256 } 257 } 258} 259 2601; 261