lib/snmp/open/parser.rb in snmp-open-0.6.0 vs lib/snmp/open/parser.rb in snmp-open-0.6.1
- old
+ new
@@ -12,12 +12,42 @@
end
# convert SNMP command output into arrays
class Parser
include SNMP::Open::Parser::Constants
- OID_RE = Regexp.union(/\S+-MIB::\S+/, /[0-9\.]+/)
+ OID_RE = Regexp.union(/\S+-MIB::\S+/, /[0-9.]+/)
+ EMPTY_STRING_RE =
+ /^(#{OID_RE}) # capture group 1: OID
+ \s+=\s+
+ (Opaque|STRING) # capture group 2: Type
+ :\s*\n # value is always empty string
+ /x.freeze
+
+ STRING_RE =
+ /^(#{OID_RE}) # capture group 1: OID
+ \s+=\s+
+ (Opaque|STRING):\s+ # capture group 2: Type
+
+ ( # capture group 3: Value
+
+ (?!") # this pattern is for finding strings in need of
+ # quoting, so reject any strings that are already
+ # quoted
+
+ [^\n]* # first line of value
+
+ (\n # newline before each additional line of value
+ (?!
+ #{OID_RE} # additional lines of value are identified by not
+ \s+=\s+ # starting with "<OID> ="
+ )
+ [^\n]+ # additional lines of value
+ )*
+ )\n
+ /x.freeze
+
def initialize(oids)
@oids = oids
end
def parse(texts)
@@ -45,14 +75,12 @@
end
def clean_input_text(text)
text
.gsub(/\r\n|\n\r|\r/, "\n")
- .gsub(/^(#{OID_RE})\s*=\s*(Opaque|STRING):\s*\n/,
- %(\\1 = \\2: ""\n))
- .gsub(/^(#{OID_RE}) = (Opaque|STRING): ((?!")[^\n]*)\n/,
- %(\\1 = \\2: "\\3"\n))
+ .gsub(EMPTY_STRING_RE, %(\\1 = \\2: ""\n))
+ .gsub(STRING_RE, %(\\1 = \\2: "\\3"\n))
.gsub(Static::ANY_MESSAGE, Static::QUOTED_MESSAGES)
end
def index_using_first_oid(value)
base = @oids.first
@@ -80,11 +108,13 @@
end
def parse_next_object(tokens)
oid = tokens.next.sub(/\A\./, '')
raise "Parse error at #{oid}" unless oid =~ OID_RE
+
equals = tokens.next
raise "Parse error after #{oid}" unless equals == '='
+
type, value = parse_type(tokens)
Value.new(oid, type, value)
end
def parse_type(tokens)