[release] Update version to 0.9.6+ post release
[gpxe.git] / contrib / errcode / build_errcodedb.py
blob1be9d8532e706f95d1af208bd8344a8f3d49519c
1 #!/usr/bin/env python
2 # Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 import sys
18 import re
20 pxenv_status_files = ('../../src/include/errno.h', )
21 errfile_files = ('../../src/include/gpxe/errfile.h',
22 '../../src/arch/i386/include/bits/errfile.h')
23 posix_errno_files = ('../../src/include/errno.h', )
25 PXENV_STATUS_RE = re.compile(r'^#define\s+(PXENV_STATUS_[^\s]+)\s+(.+)$', re.M)
26 ERRFILE_RE = re.compile(r'^#define\s+(ERRFILE_[^\s]+)\s+(.+)$', re.M)
27 POSIX_ERRNO_RE = re.compile(r'^#define\s+(E[A-Z0-9]+)\s+(?:\\\n)?.*(0x[0-9a-f]+).*$', re.M)
29 def err(msg):
30 sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))
31 sys.exit(1)
33 def to_pxenv_status(errno):
34 return errno & 0xff
36 def to_errfile(errno):
37 return (errno >> 13) & 0x7ff
39 def to_posix_errno(errno):
40 return (errno >> 24) & 0x7f
42 def load_header_file(filename, regexp):
43 defines = {}
44 data = open(filename, 'r').read()
45 for m in regexp.finditer(data):
46 key, val = m.groups()
47 defines[key] = val
48 return defines
50 def evaluate(defines, expr):
51 pyexpr = ''
52 for token in expr.split():
53 if token in '()':
54 pass
55 elif token.startswith('/*') or token.startswith('//'):
56 break
57 elif token.startswith('0x') or token == '|':
58 pyexpr += token
59 else:
60 if token in defines:
61 pyexpr += '0x%x' % defines[token]
62 else:
63 return -1
64 if not re.match(r'^[0-9a-zA-Z_|]+$', pyexpr):
65 err('invalid expression')
66 return eval(pyexpr)
68 def build(filenames, regexp, selector):
69 unevaluated = {}
70 for filename in filenames:
71 unevaluated.update(load_header_file(filename, regexp))
73 evaluated = {}
74 changed = True
75 while changed:
76 changed = False
77 for key in list(unevaluated.keys()):
78 val = evaluate(evaluated, unevaluated[key])
79 if val != -1:
80 del unevaluated[key]
81 evaluated[key] = val
82 changed = True
83 if unevaluated:
84 err('unable to evaluate all #defines')
86 lookup = {}
87 for key, val in evaluated.iteritems():
88 lookup[selector(val)] = key
89 return lookup
91 print 'pxenv_status =', repr(build(pxenv_status_files, PXENV_STATUS_RE, to_pxenv_status))
92 print 'errfile =', repr(build(errfile_files, ERRFILE_RE, to_errfile))
93 print 'posix_errno =', repr(build(posix_errno_files, POSIX_ERRNO_RE, to_posix_errno))