1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#!/usr/bin/env python
import re
import sys
import os
from pprint import pformat
def token_is_variable(line_number, token):
if token.isdigit():
return True
if token.startswith('('):
assert token.endswith(')'), "%d: token %s should end with )" % (line_number, token)
return True
if token.startswith('['):
assert token.endswith(']'), "%d: token %s should end with ]" % (line_number, token)
return True
if token.startswith('{'):
# I don't really care about checking for this I just put
# these asserts in here to bug sharpd
assert token.endswith('}'), "%d: token %s should end with }" % (line_number, token)
return True
assert '|' not in token, "%d: Weird token %s has a | but does not start with [ or (" % (line_number, token)
if token in ('WORD',
'.LINE', # where is this defined?
'PATH',
'A.B.C.D',
'A.B.C.D/M',
'X:X::X:X',
'X:X::X:X/M',
'ASN:nn_or_IP-address:nn'): # where is this defined?
return True
re_number_range = re.search('^<\d+-\d+>$', token)
if re_number_range:
return True
return False
def get_argv_translator(line_number, line):
table = {}
line = line.strip()
assert line.startswith('"'), "%d: line does not start with \"\n%s" % (line_number, line)
assert line.endswith('",'), "%d: line does not end with \",\n%s" % (line_number, line)
line = line[1:-2]
funky_chars = ('+', '"')
for char in funky_chars:
if char in line:
raise Exception("%d: Add support for tokens in\n%s\n\nsee BGP_INSTANCE_CMD down below" % (line_number, line))
old_style_index = 0
for (token_index, token) in enumerate(line.split()):
if token_is_variable(line_number, token):
# print "%s is a token" % token
table[old_style_index] = token_index
old_style_index += 1
else:
# print "%s is NOT a token" % token
pass
return table
def update_argvs(filename):
lines = []
with open(filename, 'r') as fh:
state = None
defun_line_number = None
cmd_string = None
argv_translator = {}
print_translator = False
for (line_number, line) in enumerate(fh.readlines()):
new_line = line
if state is None:
if line.startswith('DEFUN ('):
assert line.count(',') == 1, "Too many commas in\n%s" % line
state = 'DEFUN_HEADER'
defun_line_number = line_number
elif state == 'DEFUN_HEADER':
if line.startswith('DEFUN'):
raise Exception("ERROR on line %d, found DEFUN inside DEFUN" % line_number)
elif line.startswith('ALIAS'):
raise Exception("ERROR on line %d, found ALIAS inside DEFUN" % line_number)
elif line.strip() == '{':
state = 'DEFUN_BODY'
elif line_number == defun_line_number + 2:
# in the middle
line = line.replace('" CMD_AS_RANGE "', '<1-4294967295>')
line = line.replace('" DYNAMIC_NEIGHBOR_LIMIT_RANGE "', '<1-5000>')
line = line.replace('" BGP_INSTANCE_CMD "', '(view|vrf) WORD')
line = line.replace('" BGP_INSTANCE_ALL_CMD "', '(view|vrf) all')
line = line.replace('" CMD_RANGE_STR(1, MULTIPATH_NUM) "', '<1-255>')
line = line.replace('" QUAGGA_IP_REDIST_STR_BGPD "', '(kernel|connected|static|rip|ospf|isis|pim|table)')
line = line.replace('" QUAGGA_IP6_REDIST_STR_BGPD "', '(kernel|connected|static|ripng|ospf6|isis|table)')
# endswith
line = line.replace('" CMD_AS_RANGE,', ' <1-4294967295>",')
line = line.replace('" DYNAMIC_NEIGHBOR_LIMIT_RANGE,', ' <1-5000>",')
line = line.replace('" BGP_INSTANCE_CMD,', ' (view|vrf) WORD",')
line = line.replace('" BGP_INSTANCE_ALL_CMD,', ' (view|vrf) all",')
line = line.replace('" CMD_RANGE_STR(1, MULTIPATH_NUM),', '<1-255>",')
line = line.replace('" CMD_RANGE_STR(1, MAXTTL),', '<1-255>",')
line = line.replace('" BGP_UPDATE_SOURCE_REQ_STR,', ' (A.B.C.D|X:X::X:X|WORD)",')
line = line.replace('" BGP_UPDATE_SOURCE_OPT_STR,', ' {A.B.C.D|X:X::X:X|WORD}",')
line = line.replace('" QUAGGA_IP_REDIST_STR_BGPD,', ' (kernel|connected|static|rip|ospf|isis|pim|table)",')
line = line.replace('" QUAGGA_IP6_REDIST_STR_BGPD,', ' (kernel|connected|static|ripng|ospf6|isis|table)",')
# startswith
line = line.replace('LISTEN_RANGE_CMD "', '"bgp listen range (A.B.C.D/M|X:X::X:X/M) ')
line = line.replace('NO_NEIGHBOR_CMD2 "', '"no neighbor (A.B.C.D|X:X::X:X|WORD) ')
line = line.replace('NEIGHBOR_CMD2 "', '"neighbor (A.B.C.D|X:X::X:X|WORD) ')
line = line.replace('NO_NEIGHBOR_CMD "', '"no neighbor (A.B.C.D|X:X::X:X) ')
line = line.replace('NEIGHBOR_CMD "', '"neighbor (A.B.C.D|X:X::X:X) ')
# solo
line = line.replace('NO_NEIGHBOR_CMD2,', '"no neighbor (A.B.C.D|X:X::X:X|WORD)",')
line = line.replace('NEIGHBOR_CMD2,', '"neighbor (A.B.C.D|X:X::X:X|WORD)",')
line = line.replace('NO_NEIGHBOR_CMD,', '"no neighbor (A.B.C.D|X:X::X:X)",')
line = line.replace('NEIGHBOR_CMD,', '"neighbor (A.B.C.D|X:X::X:X)",')
if line.rstrip().endswith('" ,'):
line = line.replace('" ,', '",')
argv_translator = get_argv_translator(line_number, line)
print_translator = True
elif state == 'DEFUN_BODY':
if line.rstrip() == '}':
state = None
defun_line_number = None
cmd_string = None
argv_translator = {}
elif 'argv[' in new_line:
for index in reversed(argv_translator.keys()):
old_argv = "argv[%d]" % index
new_argv = "argv[%d]->arg" % argv_translator[index]
new_line = new_line.replace(old_argv, new_argv)
# uncomment to debug state machine
print "%5d %12s: %s" % (line_number, state, new_line.rstrip())
if print_translator:
print "%s\n" % pformat(argv_translator)
print_translator = False
lines.append(new_line)
with open(filename, 'w') as fh:
for line in lines:
fh.write(line)
if __name__ == '__main__':
filename = sys.argv[1]
if os.path.exists(filename):
update_argvs(filename)
else:
print "ERROR: could not find file %s" % filename
|