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.
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+(.+)$')
26 ERRFILE_RE = re.compile(r'^#define\s+(ERRFILE_[^\s]+)\s+(.+)$')
27 POSIX_ERRNO_RE = re.compile(r'^#define\s+(E[A-Z]+)\s+.*(0x[0-9a-f]+).*$')
30 sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))
33 def to_pxenv_status(errno):
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):
44 for line in open(filename, 'r'):
45 m = regexp.match(line)
51 def evaluate(defines, expr):
53 for token in expr.split():
56 elif token.startswith('/*') or token.startswith('//'):
58 elif token.startswith('0x') or token == '|':
62 pyexpr += '0x%x' % defines[token]
65 if not re.match(r'^[0-9a-zA-Z_|]+$', pyexpr):
66 err('invalid expression')
69 def build(filenames, regexp, selector):
71 for filename in filenames:
72 unevaluated.update(load_header_file(filename, regexp))
78 for key in list(unevaluated.keys()):
79 val = evaluate(evaluated, unevaluated[key])
85 err('unable to evaluate all #defines')
88 for key, val in evaluated.iteritems():
89 lookup[selector(val)] = key
92 print 'pxenv_status =', repr(build(pxenv_status_files, PXENV_STATUS_RE, to_pxenv_status))
93 print 'errfile =', repr(build(errfile_files, ERRFILE_RE, to_errfile))
94 print 'posix_errno =', repr(build(posix_errno_files, POSIX_ERRNO_RE, to_posix_errno))