You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build_errcodedb.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
  3. #
  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.
  8. #
  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.
  13. #
  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
  19. pxenv_status_files = ('../../src/include/errno.h', )
  20. errfile_files = ('../../src/include/gpxe/errfile.h',
  21. '../../src/arch/i386/include/bits/errfile.h')
  22. posix_errno_files = ('../../src/include/errno.h', )
  23. PXENV_STATUS_RE = re.compile(r'^#define\s+(PXENV_STATUS_[^\s]+)\s+(.+)$')
  24. ERRFILE_RE = re.compile(r'^#define\s+(ERRFILE_[^\s]+)\s+(.+)$')
  25. POSIX_ERRNO_RE = re.compile(r'^#define\s+(E[A-Z]+)\s+.*(0x[0-9a-f]+).*$')
  26. def err(msg):
  27. sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))
  28. sys.exit(1)
  29. def to_pxenv_status(errno):
  30. return errno & 0xff
  31. def to_errfile(errno):
  32. return (errno >> 13) & 0x7ff
  33. def to_posix_errno(errno):
  34. return (errno >> 24) & 0x7f
  35. def load_header_file(filename, regexp):
  36. defines = {}
  37. for line in open(filename, 'r'):
  38. m = regexp.match(line)
  39. if m:
  40. key, val = m.groups()
  41. defines[key] = val
  42. return defines
  43. def evaluate(defines, expr):
  44. pyexpr = ''
  45. for token in expr.split():
  46. if token in '()':
  47. pass
  48. elif token.startswith('/*') or token.startswith('//'):
  49. break
  50. elif token.startswith('0x') or token == '|':
  51. pyexpr += token
  52. else:
  53. if token in defines:
  54. pyexpr += '0x%x' % defines[token]
  55. else:
  56. return -1
  57. if not re.match(r'^[0-9a-zA-Z_|]+$', pyexpr):
  58. err('invalid expression')
  59. return eval(pyexpr)
  60. def build(filenames, regexp, selector):
  61. unevaluated = {}
  62. for filename in filenames:
  63. unevaluated.update(load_header_file(filename, regexp))
  64. evaluated = {}
  65. changed = True
  66. while changed:
  67. changed = False
  68. for key in list(unevaluated.keys()):
  69. val = evaluate(evaluated, unevaluated[key])
  70. if val != -1:
  71. del unevaluated[key]
  72. evaluated[key] = val
  73. changed = True
  74. if unevaluated:
  75. err('unable to evaluate all #defines')
  76. lookup = {}
  77. for key, val in evaluated.iteritems():
  78. lookup[selector(val)] = key
  79. return lookup
  80. print 'pxenv_status =', repr(build(pxenv_status_files, PXENV_STATUS_RE, to_pxenv_status))
  81. print 'errfile =', repr(build(errfile_files, ERRFILE_RE, to_errfile))
  82. print 'posix_errno =', repr(build(posix_errno_files, POSIX_ERRNO_RE, to_posix_errno))