Skip to content

Commit 8de932f

Browse files
committed
Add ability to quote command arguments and escape equals signs
1 parent f3eaeb2 commit 8de932f

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Arguments may also be specified by name in order to skip optional arguments, e.g
141141

142142
{{tlsa:443:ttl=300}}
143143

144-
If an argument value needs to contain a colon, it can be escaped with a backslash, e.g. `\:`.
144+
If an argument value needs to contain a colon, equals sign, or quote, those symbols can be escaped with a backslash, e.g. `\:` or the value may be enclosed in double quotes.
145145

146146
Records can be disabled by prepending the record type with `-`.
147147

bindtool

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class BindTool(object):
121121
self.script_name = os.path.basename(__file__)
122122

123123
argparser = argparse.ArgumentParser(description='Preprocess bind zone files')
124-
argparser.add_argument('--version', action='version', version='%(prog)s 1.0.1')
124+
argparser.add_argument('--version', action='version', version='%(prog)s 1.0.2')
125125
argparser.add_argument('zone_file_path')
126126
argparser.add_argument('out_file_path', nargs='?')
127127
argparser.add_argument('-d', '--debug',
@@ -331,37 +331,46 @@ class BindTool(object):
331331

332332
def _split_command(self, command):
333333
parts = []
334-
part = ''
334+
name = ''
335+
value = ''
335336
count = len(command)
336337
index = 0
337338
while (index < count):
338-
if ('\\' == command[index]):
339+
if ('"' == command[index]):
340+
index += 1
341+
while (index < count):
342+
if ('"' == command[index]):
343+
break
344+
value += command[index]
345+
index += 1
346+
elif ('\\' == command[index]):
339347
index += 1
340348
if (index < count):
341-
part += command[index]
349+
value += command[index]
350+
elif ('=' == command[index]):
351+
name = value
352+
value = ''
342353
elif (':' == command[index]):
343-
parts.append(part)
344-
part = ''
354+
parts.append((name.strip(), value.strip()))
355+
name = ''
356+
value = ''
345357
else:
346-
part += command[index]
358+
value += command[index]
347359
index += 1
348-
parts.append(part)
349-
return parts
360+
parts.append((name.strip(), value.strip()))
361+
return (parts[0][1], parts[1:])
350362

351363
def _parse_params(self, type, params, names, defaults={}, prefixes={}):
352364
out = self._defaults(type, defaults)
353365
while (0 < len(params)):
354-
param = params.pop(0)
355-
if ('=' in param):
356-
name, value = param.split('=', 1)
357-
name = name.strip()
366+
name, value = params.pop(0)
367+
if (name):
358368
if (name in names):
359369
names.remove(name)
360370
else:
361371
name = names.pop(0)
362-
value = param
363372
if (value):
364-
out[name] = value.strip()
373+
out[name] = value
365374
for name in prefixes:
366375
if ((name in out) and out[name]):
367376
out[name] = prefixes[name] + out[name]
@@ -621,6 +630,7 @@ class BindTool(object):
621630
self._error('caa record must specify tag {{', command, '}}\n')
622631
if ('caname' not in params):
623632
self._error('caa record must specify caname {{', command, '}}\n')
633+
self._validate_numeric(params, command, 'flag')
624634

625635
return self._caa_rr(params, '@', params['flag'], params['tag'], params['caname'])
626636

@@ -698,7 +708,7 @@ class BindTool(object):
698708
elif (re.match(r'^[a-z]+:', command)):
699709
if (((0 == len(output)) or ('\n' == output[-1])) and ('\n' == input[0:1])):
700710
input = input[1:]
701-
record, *params = self._split_command(command)
711+
record, params = self._split_command(command)
702712
if ('soa' == record):
703713
output = self._append(output, self.soa_record(params, command, zone_name))
704714
has_soa = True

0 commit comments

Comments
 (0)