@@ -154,12 +154,40 @@ func setup4(args ...string) (handler.Handler4, error) {
154154// errors occur, they are gathered into errs, a slice of errors, so that they
155155// can be printed or handled.
156156func parseConfig (argv ... string ) (cfg Config , errs []error ) {
157- for idx , arg := range argv {
157+ var (
158+ idx int = 0 // Separate arg index so comments aren't counted
159+ insideComment bool = false // Track if inside a comment
160+ commentIdx int = - 1 // Track where last comment began for printing error
161+ )
162+ for argIdx , arg := range argv {
163+ // Parse comments (skip further arg parsing until out of comment)
164+ if len (arg ) >= 2 {
165+ if arg [0 :2 ] == "/*" || arg [len (arg )- 2 :] == "*/" {
166+ if arg [0 :2 ] == "/*" {
167+ insideComment = true
168+ commentIdx = argIdx + 1
169+ }
170+ if arg [len (arg )- 2 :] == "*/" {
171+ if ! insideComment {
172+ errs = append (errs , fmt .Errorf ("arg %d: comment terminator (\" */\" ) found without start of comment (\" /*\" )" , argIdx + 1 ))
173+ }
174+ insideComment = false
175+ }
176+ continue
177+ }
178+ }
179+ if insideComment {
180+ continue
181+ } else {
182+ // If not a comment, increase separate arg index
183+ idx ++
184+ }
185+
158186 opt := strings .SplitN (arg , "=" , 2 )
159187
160188 // Ensure key=val format
161189 if len (opt ) != 2 {
162- errs = append (errs , fmt .Errorf ("arg %d: invalid format '%s', should be 'key=val' (skipping)" , idx , arg ))
190+ errs = append (errs , fmt .Errorf ("non-comment arg %d: invalid format '%s', should be 'key=val' (skipping)" , idx , arg ))
163191 continue
164192 }
165193
@@ -168,48 +196,51 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
168196 case "lease_file" :
169197 leaseFile := strings .Trim (opt [1 ], `"'` )
170198 if leaseFile == "" {
171- errs = append (errs , fmt .Errorf ("arg %d: %s: empty (skipping)" , idx , opt [0 ]))
199+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: empty (skipping)" , idx , opt [0 ]))
172200 continue
173201 } else {
174202 cfg .leaseFile = leaseFile
175203 }
176204 case "script_path" :
177205 scriptPath := strings .Trim (opt [1 ], `"'` )
178206 if scriptPath == "" {
179- errs = append (errs , fmt .Errorf ("arg %d: %s: empty (setting to default script)" , idx , opt [0 ]))
207+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: empty (setting to default script)" , idx , opt [0 ]))
180208 cfg .scriptPath = defaultScriptPath
181209 continue
182210 } else {
183211 cfg .scriptPath = scriptPath
184212 }
185213 case "lease_time" :
186214 if leaseTime , err := time .ParseDuration (opt [1 ]); err != nil {
187- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid duration '%s' (skipping)" , idx , opt [0 ], opt [1 ]))
215+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid duration '%s' (skipping)" , idx , opt [0 ], opt [1 ]))
188216 continue
189217 } else {
190218 cfg .leaseTime = & leaseTime
191219 }
192220 case "ipv4_start" :
193221 ipv4Start := net .ParseIP (opt [1 ])
194222 if ipv4Start .To4 () == nil {
195- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid ip address '%s' (skipping)" , idx , opt [0 ], opt [1 ]))
223+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid ip address '%s' (skipping)" , idx , opt [0 ], opt [1 ]))
196224 continue
197225 } else {
198226 cfg .ipv4Start = & ipv4Start
199227 }
200228 case "ipv4_end" :
201229 ipv4End := net .ParseIP (opt [1 ])
202230 if ipv4End .To4 () == nil {
203- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid ip address '%s' (skipping)" , idx , opt [0 ], opt [1 ]))
231+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid ip address '%s' (skipping)" , idx , opt [0 ], opt [1 ]))
204232 continue
205233 } else {
206234 cfg .ipv4End = & ipv4End
207235 }
208236 default :
209- errs = append (errs , fmt .Errorf ("arg %d: unknown config key '%s' (skipping)" , idx , opt [0 ]))
237+ errs = append (errs , fmt .Errorf ("non-comment arg %d: unknown config key '%s' (skipping)" , idx , opt [0 ]))
210238 continue
211239 }
212240 }
241+ if insideComment {
242+ errs = append (errs , fmt .Errorf ("arg %d: unterminated comment (\" /*\" found without a \" */\" )" , commentIdx ))
243+ }
213244 return
214245}
215246
0 commit comments