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