File: instructions.rb

package info (click to toggle)
zonecheck 3.0.2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,312 kB
  • ctags: 836
  • sloc: ruby: 6,664; xml: 693; sh: 518; python: 301; makefile: 75
file content (164 lines) | stat: -rw-r--r-- 3,985 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# $Id: instructions.rb,v 1.15 2010/06/07 08:51:25 chabannf Exp $

# 
# CONTACT     : zonecheck@nic.fr
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED     : 2002/07/19 07:28:13
# REVISION    : $Revision: 1.15 $ 
# DATE        : $Date: 2010/06/07 08:51:25 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE     : GPL v3
# COPYRIGHT   : AFNIC (c) 2003
#
# This file is part of ZoneCheck.
#
# ZoneCheck is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# ZoneCheck is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZoneCheck; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

require 'dbg'


##
##
##
module Instruction
    class InstructionError < StandardError
    end

    class PreevalError < InstructionError
    end


    ##
    ## Abstract Node class
    ##
    class Node
    end


    ##
    ## Instruction block
    ##
    class Block < Node
	def initialize(instr=[])
	    @instr		= instr
	end
	attr_reader :instr
	
	def [](idx)   ; @instr[idx]     ; end
	def size      ; @instr.size     ; end
	def <<(instr) ; @instr << instr ; end
	
	def validate(testmanager)
	    @instr.each { |i| i.validate(testmanager) }
	end
	
	def preeval(testmanager, args)
	    count = 0
	    @instr.each { |i| count += i.preeval(testmanager, args) }
	    count
	end
	
	def eval(testmanager, args)
	    @instr.each { |i| i.eval(testmanager, args) }
	end
    end


    ##
    ## Check
    ##
    class Check < Node
      def initialize(checkname, severity, category, block=nil)
	    @checkname		= checkname
	    @severity		= severity
	    @category		= category
	    @block = block
	end
	attr_reader :checkname, :severity, :category, :block
	
	def validate(testmanager)
	    unless testmanager.has_check?(@checkname)
		raise StandardError, $mc.get('config:check_unknown') % [
		    @checkname ]
	    end
	end
	
	def preeval(testmanager, args)
	    testmanager.wanted_check?(@checkname, category) ? @block.preeval(testmanager, args) + 1 : 0;
	end
	
  def eval(testmanager, args)
      if testmanager.wanted_check?(@checkname, category)
  	    result = testmanager.check1(@checkname, severity, *args)
  	    
  	    if result == Test::Succeed
  	      unless @block.nil?
  	        @block.eval(testmanager,args)
  	      end
        end
        
	    end
  end
    end
    
    
    ##
    ## Case switch
    ##
    class Switch < Node
	def initialize(testname, when_stmt, else_stmt)
	    @testname		= testname
	    @when		= when_stmt
	    @else		= else_stmt
	end
	attr_reader :testname, :when, :else
	
	def validate(testmanager)
	    unless testmanager.has_test?(@testname)
		raise StandardError, $mc.get('config:check_unknown') % [
		    @testname ]
	    end
	    @when.each_value { |b| b.validate(testmanager) }
	    @else.validate(testmanager) if @else
	end
	
	def preeval(testmanager, args)
	    choice = testmanager.test1(@testname, false, *args)
	    raise PreevalError if choice.kind_of?(Exception)

	    $dbg.msg(DBG::TESTS) { "preeval: #{@testname} = #{choice}" }
	    block = @when[choice] || @else
	    block.nil? ? 0 : block.preeval(testmanager, args) 
	end
	
	def eval(testmanager, args)
	    choice = testmanager.test1(@testname, true, *args)
	    $dbg.msg(DBG::TESTS) {"switching to: #{@testname} = #{choice}"}
	    block = @when[choice] || @else
	    if block
		$dbg.msg(DBG::TESTS) { "leaving switch: #{@testname}" }
		block.eval(testmanager, args)
	    else
		$dbg.msg(DBG::TESTS) {
		    "switch no choice: #{@testname} = #{choice}" }
	    end
	end
    end
end