ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Runtime.pm
Revision: 1.4.4.1
Committed: Fri Feb 27 16:06:24 2004 UTC (21 years, 2 months ago) by sashby
Content type: text/plain
Branch: SCRAM_V1_BRANCH
CVS Tags: V1_pre0
Branch point for: V1_pre1
Changes since 1.4: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #
2 #
3 # Runtime.pm
4 #
5 # Written by Christopher Williams
6 #
7 # Description
8 # -----------
9 # Prepare a runtime environment
10 #
11 # Interface
12 # ---------
13 # new() : A new Runtime object
14 # file(file) : Specify an environment description file
15 # doc(var) : Print out any documentation for the runtime env
16 # list() : return a list of all variables set
17 # sethash(hashref) : Set the environment in the hash provided
18 # printenv(shell) : output environment to stdout in shell format
19 # getvalue(var) : return a string - modified by type for the var specified
20 # addvar(name,value[,type]) : add a variable - if necessary specify a type
21 # unshiftvar(name,value{,type]) : unshift a variable
22
23 package Runtime;
24 require 5.004;
25 use ActiveDoc::SimpleDoc;
26
27 sub new {
28 my $class=shift;
29 $self={};
30 $self->{Arch}=1;
31 $self->{validtypes}={ 'path'=>1 };
32 push @{$self->{ARCHBLOCK}}, $self->{Arch};
33 bless $self, $class;
34 return $self;
35 }
36
37 sub file {
38 my $self=shift;
39 my $filename=shift;
40 $self->_parsefile($filename);
41 }
42
43 sub sethash {
44 my $self=shift;
45 my $hashref=shift;
46
47 foreach $var ( @{$self->{'varlist'}} ) {
48 $$hashref{$var}=$self->_expandvar($self->getvalue($var));
49 }
50 }
51
52 sub printenv {
53 my $self=shift;
54 my $shell=shift;
55
56 foreach $var ( @{$self->{'varlist'}} ) {
57 $self->_printoutenv($shell,$var,
58 $self->_expandvar($self->getvalue($var)));
59 }
60 }
61
62 sub list {
63 my $self=shift;
64 return @{$self->{'varlist'}};
65 }
66
67 sub doc {
68 my $self=shift;
69 my $var=shift;
70 return $self->{vardoc}{$var};
71 }
72
73 sub getvalue {
74 my $self=shift;
75 my $name=shift;
76
77 my $string;
78 my @vals=@{$self->{vars}{$name}};
79
80 if ( defined $self->{'vartype'}{$name} ) { #any type proceesing
81 $string=&{"_".$self->{'vartype'}{$name}}($self,@vals);
82 }
83 else {
84 $string=$vals[0];
85 }
86 return $string;
87 }
88
89 sub addvar {
90 my $self=shift;
91 my $name=shift;
92 my $val=shift;
93
94 push @{$self->{'varlist'}}, $name;
95 push @{$self->{'vars'}{$name}}, $val;
96 if ( @_ ) {
97 $self->{'vartype'}{$name}=shift;;
98 }
99 else {
100 $self->{'vartype'}{$name}=undef;
101 }
102 }
103
104 sub unshiftvar {
105 my $self=shift;
106 my $name=shift;
107 my $val=shift;
108
109 if ( exists $self->{'vars'}{$name} ) {
110 unshift @{$self->{'vars'}{$name}}, $val;
111 }
112 else {
113 $self->addvar($name,$val,@_);
114 }
115 }
116
117 # ---- Support Routines
118 sub _expandvar {
119 my $self=shift;
120 my $string=shift;
121
122 return "" , if ( ! defined $string );
123 $string=~s{
124 \$\((\w+)\)
125 }{
126 if (defined $ENV{$1}) {
127 $self->_expandvar($ENV{$1});
128 } else {
129 "\$".$1;
130 }
131 }egx;
132 $string=~s{
133 \$(\w+)
134 }{
135 if (defined $ENV{$1}) {
136 $self->_expandvar($ENV{$1});
137 } else {
138 "\$".$1;
139 }
140 }egx;
141 return $string;
142 }
143
144 sub _path {
145 my $self=shift;
146 my @vals=@_;
147
148 @vals=$self->_removedoubles(@vals);
149 my $string=join ":", @vals;
150 return $string;
151 }
152
153 #
154 # removes doubles and whitespace
155 #
156 sub _removedoubles {
157 my $self=shift;
158 my @red=();
159
160 foreach $elem ( @_ ) {
161 $elem=~s/^\s+//; # chop leading whitespace
162 if ( (! grep { $_ eq $elem } @red ) && ( $elem ne "")) {
163 push @red,$elem;
164 }
165 }
166 return @red;
167 }
168
169 sub _parsefile {
170 my $self=shift;
171 my $filename=shift;
172
173 $self->{switch}=ActiveDoc::SimpleDoc->new();
174 $self->{switch}->newparse("runtime");
175 $self->{switch}->addignoretags("runtime");
176 $self->{switch}->filetoparse($filename);
177 $self->{switch}->addtag("runtime","Runtime",
178 \&Runtime_start, $self,
179 \&Runtime_text, $self,
180 \&Runtime_end, $self);
181 $self->{switch}->addtag("runtime","Architecture",
182 \&Arch_Start, $self, "",$self, \&Arch_End, $self);
183 $self->{Runtimecontext}=0;
184 $self->{switch}->parse("runtime");
185 }
186
187 sub _printoutenv {
188 my $self=shift;
189 my $shell=shift;
190 my $variable=shift;
191 my $value=shift;
192
193 if ( $shell eq "csh" ) {
194 print "setenv $variable \"$value\";\n";
195 }
196 elsif ( $shell eq "sh" ) {
197 print "$variable=\"$value\";\n";
198 print "export $variable;\n";
199 }
200 }
201
202 # ------ Tag Routines
203
204 sub Runtime_start {
205 my $self=shift;
206 my $name=shift;
207 my $hashref=shift;
208
209 $self->{switch}->checktag($name,$hashref,'name');
210 $self->{switch}->checktag($name,$hashref,'value');
211 if ( $self->{Arch} ) {
212 if ( $self->{Runtimecontext} == 1 ) {
213 $self->{switch}->parseerror("No </$name> before new tag");
214 }
215 $self->{Runtimecontext}=1;
216
217 push @{$self->{'varlist'}}, $$hashref{'name'};
218 push @{$self->{'vars'}{$$hashref{'name'}}}, $$hashref{'value'};
219 if ( defined $$hashref{'type'} ) {
220 if ( ( exists $self->{'vartype'}{$$hashref{'name'}} ) &&
221 ( $self->{'vartype'}{$$hashref{'name'}} ne $$hashref{'type'}) ){
222 $self->{switch}->parseerror("Redefinition of Variable".
223 " type for ".$$hashref{'name'});
224 }
225 $self->{'vartype'}{$$hashref{'name'}}=$$hashref{'type'};
226 if ( ! defined $self->{validtypes}{$$hashref{'type'}} ) {
227 $self->{switch}->parseerror(
228 "Unknown type ".$$hashref{'type'});
229 }
230 }
231 else {
232 $self->{'vartype'}{$$hashref{'name'}}=undef;
233 }
234 }
235 }
236
237 sub Runtime_text {
238 my $self=shift;
239 my $name=shift;
240 my @vars=@_;
241
242 if ( $self->{Arch} ) {
243 my $var=$self->{varlist}[$#{$self->{varlist}}];
244 $self->{'vardoc'}{$var}=$self->{'vardoc'}{$var}.
245 (join "", @vars);
246 }
247 }
248
249 sub Runtime_end {
250 my $self=shift;
251 my $name=shift;
252 if ( $self->{Arch} ) {
253 $self->{Runtimecontext}=0;
254 }
255 }
256
257 sub Arch_Start {
258 my $self=shift;
259 my $name=shift;
260 my $hashref=shift;
261
262 $toolswitch->checktag($name, $hashref,'name');
263 ( ($ENV{SCRAM_ARCH}=~/$$hashref{name}.*/) )? ($self->{Arch}=1)
264 : ($self->{Arch}=0);
265 push @{$self->{ARCHBLOCK}}, $self->{Arch};
266 }
267
268 sub Arch_End {
269 my $self=shift;
270 my $name=shift;
271
272 pop @{$self->{ARCHBLOCK}};
273 $self->{Arch}=$self->{ARCHBLOCK}[$#{$self->{ARCHBLOCK}}];
274 }
275