1- # frozen_string_literal: true
1+ # frozen_string_literal: false
22=begin
33= Ruby-space definitions that completes C-space funcs for Config
44
@@ -37,7 +37,7 @@ class << self
3737 def parse ( string )
3838 c = new ( )
3939 parse_config ( StringIO . new ( string ) ) . each do |section , hash |
40- c . set_section ( section , hash )
40+ c [ section ] = hash
4141 end
4242 c
4343 end
@@ -53,8 +53,9 @@ def parse(string)
5353 def parse_config ( io )
5454 begin
5555 parse_config_lines ( io )
56- rescue => error
57- raise ConfigError , "error in line #{ io . lineno } : " + error . message
56+ rescue ConfigError => e
57+ e . message . replace ( "error in line #{ io . lineno } : " + e . message )
58+ raise
5859 end
5960 end
6061
@@ -76,44 +77,29 @@ def get_key_string(data, section, key) # :nodoc:
7677 def parse_config_lines ( io )
7778 section = 'default'
7879 data = { section => { } }
79- io_stack = [ io ]
80- while definition = get_definition ( io_stack )
80+ while definition = get_definition ( io )
8181 definition = clear_comments ( definition )
8282 next if definition . empty?
83- case definition
84- when /\A \[ /
83+ if definition [ 0 ] == ?[
8584 if /\[ ([^\] ]*)\] / =~ definition
8685 section = $1. strip
8786 data [ section ] ||= { }
8887 else
8988 raise ConfigError , "missing close square bracket"
9089 end
91- when /\A \. include (\s *=\s *)?(.+)\z /
92- path = $2
93- if File . directory? ( path )
94- files = Dir . glob ( File . join ( path , "*.{cnf,conf}" ) , File ::FNM_EXTGLOB )
95- else
96- files = [ path ]
97- end
98-
99- files . each do |filename |
100- begin
101- io_stack << StringIO . new ( File . read ( filename ) )
102- rescue
103- raise ConfigError , "could not include file '%s'" % filename
90+ else
91+ if /\A ([^:\s ]*)(?:::([^:\s ]*))?\s *=(.*)\z / =~ definition
92+ if $2
93+ section = $1
94+ key = $2
95+ else
96+ key = $1
10497 end
105- end
106- when /\A ([^:\s ]*)(?:::([^:\s ]*))?\s *=(.*)\z /
107- if $2
108- section = $1
109- key = $2
98+ value = unescape_value ( data , section , $3)
99+ ( data [ section ] ||= { } ) [ key ] = value . strip
110100 else
111- key = $1
101+ raise ConfigError , "missing equal sign"
112102 end
113- value = unescape_value ( data , section , $3)
114- ( data [ section ] ||= { } ) [ key ] = value . strip
115- else
116- raise ConfigError , "missing equal sign"
117103 end
118104 end
119105 data
@@ -226,10 +212,10 @@ def clear_comments(line)
226212 scanned . join
227213 end
228214
229- def get_definition ( io_stack )
230- if line = get_line ( io_stack )
215+ def get_definition ( io )
216+ if line = get_line ( io )
231217 while /[^\\ ]\\ \z / =~ line
232- if extra = get_line ( io_stack )
218+ if extra = get_line ( io )
233219 line += extra
234220 else
235221 break
@@ -239,12 +225,9 @@ def get_definition(io_stack)
239225 end
240226 end
241227
242- def get_line ( io_stack )
243- while io = io_stack . last
244- if line = io . gets
245- return line . gsub ( /[\r \n ]*/ , '' )
246- end
247- io_stack . pop
228+ def get_line ( io )
229+ if line = io . gets
230+ line . gsub ( /[\r \n ]*/ , '' )
248231 end
249232 end
250233 end
@@ -266,7 +249,7 @@ def initialize(filename = nil)
266249 if filename
267250 File . open ( filename . to_s ) do |file |
268251 Config . parse_config ( file ) . each do |section , hash |
269- set_section ( section , hash )
252+ self [ section ] = hash
270253 end
271254 end
272255 end
@@ -315,8 +298,6 @@ def value(arg1, arg2 = nil) # :nodoc:
315298 end
316299
317300 ##
318- # *Deprecated in v2.2.0*. This method will be removed in a future release.
319- #
320301 # Set the target _key_ with a given _value_ under a specific _section_.
321302 #
322303 # Given the following configurating file being loaded:
@@ -371,8 +352,6 @@ def section(name) # :nodoc:
371352 end
372353
373354 ##
374- # *Deprecated in v2.2.0*. This method will be removed in a future release.
375- #
376355 # Sets a specific _section_ name with a Hash _pairs_.
377356 #
378357 # Given the following configuration being created:
@@ -398,13 +377,9 @@ def section(name) # :nodoc:
398377 #
399378 def []=( section , pairs )
400379 check_modify
401- set_section ( section , pairs )
402- end
403-
404- def set_section ( section , pairs ) # :nodoc:
405- hash = @data [ section ] ||= { }
380+ @data [ section ] ||= { }
406381 pairs . each do |key , value |
407- hash [ key ] = value
382+ self . add_value ( section , key , value )
408383 end
409384 end
410385
@@ -489,8 +464,6 @@ def initialize_copy(other)
489464 end
490465
491466 def check_modify
492- warn "#{ caller ( 2 , 1 ) [ 0 ] } : warning: do not modify OpenSSL::Config; this " \
493- "method is deprecated and will be removed in a future release."
494467 raise TypeError . new ( "Insecure: can't modify OpenSSL config" ) if frozen?
495468 end
496469
0 commit comments