Subversion Repositories lagranto.wrf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 michaesp 1
#!/usr/bin/perl
2
 
3
# --------------------------------------------------------
4
# Separate different commands out (delimiter & and |)
5
# --------------------------------------------------------
6
 
7
# Get input command and remove all spaces
8
$cmd = $ARGV[0];
9
$_   = $cmd;s/\s+//g;$cmd=$_;
10
$len = length $cmd; 
11
 
12
# Split the command string according to logical operators
13
$nline = 0;
14
while ( $len > 0 )
15
{
16
  # Get the length of the (remaining) command string
17
  $len = length $cmd; 
18
 
19
  # Get the position of the next command separator
20
  $and  = index($cmd,'&');
21
  $or   = index($cmd,'|' );
22
  if ( ($and == -1) && ($or == -1) )
23
     { $next = $len+1; }
24
  elsif ( ($and == -1) && ($or >= 0) )
25
     { $next = $or;  @logop[$nline] = 'OR'; }
26
  elsif ( ($and >= 0) && ($or == -1) )
27
     { $next = $and; @logop[$nline] = 'AND'; }
28
  elsif ($and > $or)
29
     { $next = $or; @logop[$nline] = 'OR';  }
30
  else
31
      { $next = $and; @logop[$nline] = 'AND'; }
32
 
33
  # A logical operator is not allowed to be at position 0
34
  if ( $next == 0) 
35
   {    
36
      die('Invalid expression... Check logical operators & and |');
37
   }   
38
 
39
  # Extract the next substring
40
  $sub = substr($cmd,0,$next);
41
  $cmd = substr($cmd,$next+1,$len-$next-1);
42
  $len = length $cmd; 
43
 
44
  # Save the command in a new line
45
  @field[$nline] = $sub;
46
  $nline = $nline + 1;
47
}
48
 
49
# --------------------------------------------------------
50
# Handle each command line separately
51
# --------------------------------------------------------
52
 
53
# Write start marker
54
print "BEGIN \n";
55
 
56
foreach ( $i = 0; $i < $nline; $i++  )
57
{
58
    # Split the command into its four components
59
    @entry  = split /:/, @field[$i];
60
    $nentry = @entry;
61
 
62
    # Either four or three elements are needed
63
    if ( ($nentry < 3) | ($nentry > 4) )     
64
     {    
65
      die('Each expression needs either 3 or 4 fields...');
66
     }   
67
 
68
    # Write the command
69
    print "@entry[0] \n";
70
 
71
    # Get the variable and the 'mode' for this variable
72
    $left  = index(@entry[1],'(');
73
    $right = index(@entry[1],')');
74
    if ( ($left == -1) && ($right == -1) ) 
75
      { 
76
       $var  = @entry[1];
77
       $mode = 'VALUE';
78
      }
79
    elsif ( ($left > 0) && ($right > $left) ) 
80
      { 
81
       $var  = substr(@entry[1],0,$left);  
82
       $mode = substr(@entry[1],$left+1,$right-$left-1);
83
      }
84
    else
85
      {    
86
       die('Invalid variable specification...');
87
      }   
88
    print "$var  $mode \n";
89
 
90
    # Get the parameter list for this command
91
    @param  = split /,/, @entry[2];
92
    $nparam = @param;
93
    print "$nparam \n";
94
    print "@param \n";
95
 
96
    # If only three parameters are given, the time is assumed to be the first one
97
    if ( $nentry == 3 )
98
    {
99
	@entry[3]='FIRST';
100
    }    
101
 
102
    # Get the variable and the 'mode' for this variable
103
    $left  = index(@entry[3],'(');
104
    $right = index(@entry[3],')');
105
    if ( ($left == -1) && ($right == -1) ) 
106
      { 
107
       $time = @entry[3];
108
       $mode = 'ALL';
109
      }
110
    elsif ( ($left > 0) && ($right > $left) ) 
111
      { 
112
       $time = substr(@entry[3],0,$left);  
113
       $mode = substr(@entry[3],$left+1,$right-$left-1);
114
      }
115
    else
116
      {    
117
       die('Invalid time specification...');
118
      }   
119
 
120
    # Get the time list for this command
121
    if ( $time eq 'ALL' )
122
    {
123
	$time = -999;
124
        print "1 \n";
125
        print "$time \n";
126
    }
127
    elsif ( $time eq 'FIRST' )
128
    {
129
        $time = -996;
130
        print "1 \n";
131
	print "$time \n";
132
    }
133
    elsif ( $time eq 'LAST' )
134
    {
135
        $time = -995;
136
        print "1 \n";
137
	print "$time \n";
138
    }
139
    else
140
    {
141
        @times  = split /,/, $time;
142
        $outstr = "";
143
	$outlen = 0;
144
	foreach $j ( @times )
145
        {
146
	    $to = index($j,'to');
147
            if ( $to == -1 )
148
               { 
149
		if ( $j eq 'FIRST' )
150
		  {
151
                      $outlen=$outlen+1;
152
		      $outstr=$outstr . " -996 ";
153
                  }
154
                elsif ( $j eq 'LAST' )
155
		  {
156
                      $outlen=$outlen+1;
157
		      $outstr=$outstr . " -995 ";
158
                  }
159
                else
160
		  {
161
                   $outlen=$outlen+1;
162
                   $outstr=$outstr . "$j "; 
163
	          }
164
               }
165
            else
166
	       { 
167
                $outlen = $outlen+3;
168
                $t1 = substr($j,0,$to);
169
		$t2 = substr($j,$to+2,length($j)-$to+1);
170
		if ( $t1 eq 'FIRST' ) 
171
		    { $t1='-996'; };
172
		if ( $t2 eq 'FIRST' ) 
173
		    { $t2='-996'; };
174
		if ( $t1 eq 'LAST'  ) 
175
		    { $t1='-995'; };
176
		if ( $t2 eq 'LAST'  ) 
177
		    { $t2='-995'; };
178
	        $outstr=$outstr . $t1 . " -994 " . $t2 . " "; 
179
	       }
180
        }
181
	print "$outlen \n";
182
        print "$outstr \n";
183
    }
184
 
185
    # Write the time mode
186
    if ( $mode eq 'ALL' )
187
    {
188
        print "$mode \n";
189
    }
190
    elsif ( $mode eq 'ANY' )
191
    {
192
        print "$mode \n";
193
    }
194
    elsif ( $mode eq 'NONE' )
195
    {
196
        print "$mode \n";
197
    }
198
    elsif ( $mode eq 'TRIGGER' )
199
    {
200
        print "$mode \n";
201
    }
202
    else
203
    {    
204
       die('Invalid time mode...');
205
    }   
206
 
207
 
208
    # Write the logical operator
209
    if ( $i < $nline-1 )
210
    {
211
      print "@logop[$i] \n";
212
    } 
213
}
214
 
215
# Write end marker
216
print "END \n";