11 idRe = "[a-zA-Z_][a-zA-Z_0-9]*"
12 tokenRe = "#?" + idRe + "|[0-9]+|."
19 line = inputFile.readline()
22 fatal("unexpected end of input")
32 if line.startswith("/*"):
36 commentEnd = line.find("*/")
41 line = line[commentEnd + 2:]
43 match = re.match(tokenRe, line)
44 token = match.group(0)
45 line = line[len(token):]
46 if token.startswith('#'):
48 elif token in macros and not inDirective:
49 line = macros[token] + line
58 line = inputFile.readline()
60 while line.endswith("\\\n"):
61 line = line[:-2] + inputFile.readline()
65 fatal("unexpected end of input")
72 sys.stderr.write("%s:%d: %s\n" % (fileName, lineNumber, msg))
85 return re.match(idRe + "$", s) != None
89 fatal("identifier expected")
92 if not re.match('[0-9]+$', token):
93 fatal("integer expected")
104 fatal("%s expected" % t)
106 def parseTaggedName():
107 assert token in ('struct', 'union')
111 name = "%s %s" % (name, token)
115 def print_enum(tag, constants, storage_class):
117 %(storage_class)sconst char *
118 %(tag)s_to_string(uint16_t value)
122 "bufferlen": len(tag) + 32,
123 "storage_class": storage_class})
124 for constant in constants:
125 print (" case %s: return \"%s\";" % (constant, constant))
133 argv0 = os.path.basename(sys.argv[0])
135 %(argv0)s, for extracting OpenFlow error codes from header files
136 usage: %(argv0)s FILE [FILE...]
138 This program reads the header files specified on the command line and
139 outputs a C source file for translating OpenFlow error codes into
140 strings, for use as lib/ofp-errors.c in the Open vSwitch source tree.
142 This program is specialized for reading lib/ofp-errors.h. It will not
143 work on arbitrary header files without extensions.\
144 ''' % {"argv0": argv0})
147 def extract_ofp_errors(filenames):
154 for domain_name in ("OF1.0", "OF1.1", "OF1.2", "OF1.3",
155 "NX1.0", "NX1.1", "NX1.2", "NX1.3"):
156 domain[domain_name] = {}
157 reverse[domain_name] = {}
163 for fileName in filenames:
166 inputFile = open(fileName)
171 if re.match('enum ofperr', line):
176 if line.startswith('/*') or not line or line.isspace():
178 elif re.match('}', line):
181 if not line.lstrip().startswith('/*'):
182 fatal("unexpected syntax between errors")
184 comment = line.lstrip()[2:].strip()
185 while not comment.endswith('*/'):
187 if line.startswith('/*') or not line or line.isspace():
188 fatal("unexpected syntax within error")
189 comment += ' %s' % line.lstrip('* \t').rstrip(' \t\r\n')
190 comment = comment[:-2].rstrip()
192 m = re.match('Expected: (.*)\.$', comment)
194 expected_errors[m.group(1)] = (fileName, lineNumber)
197 m = re.match('((?:.(?!\. ))+.)\. (.*)$', comment)
199 fatal("unexpected syntax between errors")
201 dsts, comment = m.groups()
204 m = re.match('\s+(?:OFPERR_((?:OFP|NX)[A-Z0-9_]+))(\s*=\s*OFPERR_OFS)?,',
207 fatal("syntax error expecting enum value")
211 comments.append(re.sub('\[[^]]*\]', '', comment))
214 for dst in dsts.split(', '):
215 m = re.match(r'([A-Z0-9.+]+)\((\d+)(?:,(\d+))?\)$', dst)
217 fatal("%s: syntax error in destination" % dst)
219 type_ = int(m.group(2))
221 code = int(m.group(3))
225 target_map = {"OF1.0+": ("OF1.0", "OF1.1", "OF1.2", "OF1.3"),
226 "OF1.1+": ("OF1.1", "OF1.2", "OF1.3"),
227 "OF1.2+": ("OF1.2", "OF1.3"),
228 "OF1.3+": ("OF1.3",),
233 "NX1.0+": ("OF1.0", "OF1.1", "OF1.2", "OF1.3"),
234 "NX1.1+": ("OF1.1", "OF1.2", "OF1.3"),
235 "NX1.2+": ("OF1.2", "OF1.3"),
236 "NX1.3+": ("OF1.3",),
241 if targets not in target_map:
242 fatal("%s: unknown error domain" % targets)
243 if targets.startswith('NX') and code < 0x100:
244 fatal("%s: NX domain code cannot be less than 0x100" % dst)
245 if targets.startswith('OF') and code >= 0x100:
246 fatal("%s: OF domain code cannot be greater than 0x100"
248 for target in target_map[targets]:
249 domain[target].setdefault(type_, {})
250 if code in domain[target][type_]:
251 msg = "%d,%d in %s means both %s and %s" % (
253 domain[target][type_][code][0], enum)
254 if msg in expected_errors:
255 del expected_errors[msg]
257 error("%s: %s." % (dst, msg))
258 sys.stderr.write("%s:%d: %s: Here is the location "
259 "of the previous definition.\n"
260 % (domain[target][type_][code][1],
261 domain[target][type_][code][2],
264 domain[target][type_][code] = (enum, fileName,
267 if enum in reverse[target]:
268 error("%s: %s in %s means both %d,%d and %d,%d." %
270 reverse[target][enum][0],
271 reverse[target][enum][1],
273 reverse[target][enum] = (type_, code)
277 for fn, ln in expected_errors.values():
278 sys.stderr.write("%s:%d: expected duplicate not used.\n" % (fn, ln))
285 /* Generated automatically; do not modify! -*- buffer-read-only: t -*- */
287 #define OFPERR_N_ERRORS %d
289 struct ofperr_domain {
292 enum ofperr (*decode)(uint16_t type, uint16_t code);
293 struct pair errors[OFPERR_N_ERRORS];
296 static const char *error_names[OFPERR_N_ERRORS] = {
300 static const char *error_comments[OFPERR_N_ERRORS] = {
304 '\n'.join(' "%s",' % name for name in names),
305 '\n'.join(' "%s",' % re.sub(r'(["\\])', r'\\\1', comment)
306 for comment in comments)))
308 def output_domain(map, name, description, version):
311 %s_decode(uint16_t type, uint16_t code)
313 switch ((type << 16) | code) {""" % name)
318 type_, code = map[enum]
321 value = (type_ << 16) | code
325 print (" case (%d << 16) | %d:" % (type_, code))
326 print (" return OFPERR_%s;" % enum)
334 static const struct ofperr_domain %s = {
338 {""" % (name, description, version, name))
341 type_, code = map[enum]
346 print (" { %2d, %3d }, /* %s */" % (type_, code, enum))
351 output_domain(reverse["OF1.0"], "ofperr_of10", "OpenFlow 1.0", 0x01)
352 output_domain(reverse["OF1.1"], "ofperr_of11", "OpenFlow 1.1", 0x02)
353 output_domain(reverse["OF1.2"], "ofperr_of12", "OpenFlow 1.2", 0x03)
354 output_domain(reverse["OF1.3"], "ofperr_of13", "OpenFlow 1.3", 0x04)
356 if __name__ == '__main__':
357 if '--help' in sys.argv:
359 elif len(sys.argv) < 2:
360 sys.stderr.write("at least one non-option argument required; "
361 "use --help for help\n")
364 extract_ofp_errors(sys.argv[1:])