1- # frozen_string_literal: false
1+ # frozen_string_literal: true
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 [ section ] = hash
40+ c . set_section ( section , hash )
4141 end
4242 c
4343 end
@@ -53,9 +53,8 @@ def parse(string)
5353 def parse_config ( io )
5454 begin
5555 parse_config_lines ( io )
56- rescue ConfigError => e
57- e . message . replace ( "error in line #{ io . lineno } : " + e . message )
58- raise
56+ rescue => error
57+ raise ConfigError , "error in line #{ io . lineno } : " + error . message
5958 end
6059 end
6160
@@ -77,29 +76,44 @@ def get_key_string(data, section, key) # :nodoc:
7776 def parse_config_lines ( io )
7877 section = 'default'
7978 data = { section => { } }
80- while definition = get_definition ( io )
79+ io_stack = [ io ]
80+ while definition = get_definition ( io_stack )
8181 definition = clear_comments ( definition )
8282 next if definition . empty?
83- if definition [ 0 ] == ?[
83+ case definition
84+ when /\A \[ /
8485 if /\[ ([^\] ]*)\] / =~ definition
8586 section = $1. strip
8687 data [ section ] ||= { }
8788 else
8889 raise ConfigError , "missing close square bracket"
8990 end
90- else
91- if /\A ([^:\s ]*)(?:::([^:\s ]*))?\s *=(.*)\z / =~ definition
92- if $2
93- section = $1
94- key = $2
95- else
96- key = $1
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
97104 end
98- value = unescape_value ( data , section , $3)
99- ( data [ section ] ||= { } ) [ key ] = value . strip
105+ end
106+ when /\A ([^:\s ]*)(?:::([^:\s ]*))?\s *=(.*)\z /
107+ if $2
108+ section = $1
109+ key = $2
100110 else
101- raise ConfigError , "missing equal sign"
111+ key = $1
102112 end
113+ value = unescape_value ( data , section , $3)
114+ ( data [ section ] ||= { } ) [ key ] = value . strip
115+ else
116+ raise ConfigError , "missing equal sign"
103117 end
104118 end
105119 data
@@ -212,10 +226,10 @@ def clear_comments(line)
212226 scanned . join
213227 end
214228
215- def get_definition ( io )
216- if line = get_line ( io )
229+ def get_definition ( io_stack )
230+ if line = get_line ( io_stack )
217231 while /[^\\ ]\\ \z / =~ line
218- if extra = get_line ( io )
232+ if extra = get_line ( io_stack )
219233 line += extra
220234 else
221235 break
@@ -225,9 +239,12 @@ def get_definition(io)
225239 end
226240 end
227241
228- def get_line ( io )
229- if line = io . gets
230- line . gsub ( /[\r \n ]*/ , '' )
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
231248 end
232249 end
233250 end
@@ -249,7 +266,7 @@ def initialize(filename = nil)
249266 if filename
250267 File . open ( filename . to_s ) do |file |
251268 Config . parse_config ( file ) . each do |section , hash |
252- self [ section ] = hash
269+ set_section ( section , hash )
253270 end
254271 end
255272 end
@@ -298,6 +315,8 @@ def value(arg1, arg2 = nil) # :nodoc:
298315 end
299316
300317 ##
318+ # *Deprecated in v2.2.0*. This method will be removed in a future release.
319+ #
301320 # Set the target _key_ with a given _value_ under a specific _section_.
302321 #
303322 # Given the following configurating file being loaded:
@@ -352,6 +371,8 @@ def section(name) # :nodoc:
352371 end
353372
354373 ##
374+ # *Deprecated in v2.2.0*. This method will be removed in a future release.
375+ #
355376 # Sets a specific _section_ name with a Hash _pairs_.
356377 #
357378 # Given the following configuration being created:
@@ -377,9 +398,13 @@ def section(name) # :nodoc:
377398 #
378399 def []=( section , pairs )
379400 check_modify
380- @data [ section ] ||= { }
401+ set_section ( section , pairs )
402+ end
403+
404+ def set_section ( section , pairs ) # :nodoc:
405+ hash = @data [ section ] ||= { }
381406 pairs . each do |key , value |
382- self . add_value ( section , key , value )
407+ hash [ key ] = value
383408 end
384409 end
385410
@@ -464,6 +489,8 @@ def initialize_copy(other)
464489 end
465490
466491 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."
467494 raise TypeError . new ( "Insecure: can't modify OpenSSL config" ) if frozen?
468495 end
469496
0 commit comments