@@ -209,27 +209,55 @@ func setup4(args ...string) (handler.Handler4, error) {
209209// errors occur, they are gathered into errs, a slice of errors, so that they
210210// can be printed or handled.
211211func parseConfig (argv ... string ) (cfg Config , errs []error ) {
212- for idx , arg := range argv {
212+ var (
213+ idx int = 0 // Separate arg index so comments aren't counted
214+ insideComment bool = false // Track if inside a comment
215+ commentIdx int = - 1 // Track where last comment began for printing error
216+ )
217+ for argIdx , arg := range argv {
218+ // Parse comments (skip further arg parsing until out of comment)
219+ if len (arg ) >= 2 {
220+ if arg [0 :2 ] == "/*" || arg [len (arg )- 2 :] == "*/" {
221+ if arg [0 :2 ] == "/*" {
222+ insideComment = true
223+ commentIdx = argIdx + 1
224+ }
225+ if arg [len (arg )- 2 :] == "*/" {
226+ if ! insideComment {
227+ errs = append (errs , fmt .Errorf ("arg %d: comment terminator (\" */\" ) found without start of comment (\" /*\" )" , argIdx + 1 ))
228+ }
229+ insideComment = false
230+ }
231+ continue
232+ }
233+ }
234+ if insideComment {
235+ continue
236+ } else {
237+ // If not a comment, increase separate arg index
238+ idx ++
239+ }
240+
213241 opt := strings .SplitN (arg , "=" , 2 )
214242
215243 // Ensure key=val format
216244 if len (opt ) != 2 {
217- errs = append (errs , fmt .Errorf ("arg %d: invalid format '%s', should be 'key=val' (skipping)" , idx , arg ))
245+ errs = append (errs , fmt .Errorf ("non-comment arg %d: invalid format '%s', should be 'key=val' (skipping)" , idx , arg ))
218246 continue
219247 }
220248
221249 // Check that key is known and, if so, process value
222250 switch opt [0 ] {
223251 case "svc_base_uri" :
224252 if svcURI , err := url .Parse (opt [1 ]); err != nil {
225- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid URI '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
253+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid URI '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
226254 continue
227255 } else {
228256 cfg .svcBaseURI = svcURI
229257 }
230258 case "ipxe_base_uri" :
231259 if ipxeURI , err := url .Parse (opt [1 ]); err != nil {
232- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid URI '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
260+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid URI '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
233261 continue
234262 } else {
235263 cfg .ipxeBaseURI = ipxeURI
@@ -242,21 +270,21 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
242270 }
243271 case "cache_valid" :
244272 if cacheValid , err := time .ParseDuration (opt [1 ]); err != nil {
245- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid duration '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
273+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid duration '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
246274 continue
247275 } else {
248276 cfg .cacheValid = & cacheValid
249277 }
250278 case "lease_time" :
251279 if leaseTime , err := time .ParseDuration (opt [1 ]); err != nil {
252- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid duration '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
280+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid duration '%s' (skipping): %w" , idx , opt [0 ], opt [1 ], err ))
253281 continue
254282 } else {
255283 cfg .leaseTime = & leaseTime
256284 }
257285 case "single_port" :
258286 if singlePort , err := strconv .ParseBool (opt [1 ]); err != nil {
259- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid value '%s' (defaulting to false): %w" , idx , opt [0 ], opt [1 ], err ))
287+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid value '%s' (defaulting to false): %w" , idx , opt [0 ], opt [1 ], err ))
260288 continue
261289 } else {
262290 cfg .singlePort = singlePort
@@ -268,13 +296,13 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
268296 }
269297 case "tftp_port" :
270298 if tftpPort , err := strconv .ParseInt (opt [1 ], 10 , 64 ); err != nil {
271- errs = append (errs , fmt .Errorf ("arg %d: %s: invalid port '%s' (defaulting to %d): %w" , idx , opt [0 ], opt [1 ], defaultTFTPPort , err ))
299+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: invalid port '%s' (defaulting to %d): %w" , idx , opt [0 ], opt [1 ], defaultTFTPPort , err ))
272300 cfg .tftpPort = defaultTFTPPort
273301 } else {
274302 if tftpPort >= 0 && tftpPort <= 65535 {
275303 cfg .tftpPort = int (tftpPort )
276304 } else {
277- errs = append (errs , fmt .Errorf ("arg %d: %s: port '%d' out of range, must be between 0-65535 (defaulting to %d)" , idx , opt [0 ], tftpPort , defaultTFTPPort ))
305+ errs = append (errs , fmt .Errorf ("non-comment arg %d: %s: port '%d' out of range, must be between 0-65535 (defaulting to %d)" , idx , opt [0 ], tftpPort , defaultTFTPPort ))
278306 cfg .tftpPort = defaultTFTPPort
279307 }
280308 }
@@ -306,7 +334,7 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
306334 // componentType = vals[0], pattern = vals[1]
307335 vals := strings .SplitN (opt [1 ], ":" , 2 )
308336 if len (vals ) != 2 {
309- errs = append (errs , fmt .Errorf ("arg %d: invalid format for key '%s': expected hostname_by_type=<type>:<pattern>, got %s" , idx , opt [0 ], opt [1 ]))
337+ errs = append (errs , fmt .Errorf ("non-comment arg %d: invalid format for key '%s': expected hostname_by_type=<type>:<pattern>, got %s" , idx , opt [0 ], opt [1 ]))
310338 continue
311339 }
312340 if cfg .policy .ByType == nil {
@@ -315,10 +343,13 @@ func parseConfig(argv ...string) (cfg Config, errs []error) {
315343 cfg .policy .ByType [vals [0 ]] = vals [1 ]
316344 }
317345 default :
318- errs = append (errs , fmt .Errorf ("arg %d: unknown config key '%s' (skipping)" , idx , opt [0 ]))
346+ errs = append (errs , fmt .Errorf ("non-comment arg %d: unknown config key '%s' (skipping)" , idx , opt [0 ]))
319347 continue
320348 }
321349 }
350+ if insideComment {
351+ errs = append (errs , fmt .Errorf ("arg %d: unterminated comment (\" /*\" found without a \" */\" )" , commentIdx ))
352+ }
322353 return
323354}
324355
0 commit comments