feature_to_c.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. import os, sys
  4. def writeliteral(indent, bytes):
  5. sys.stdout.write(' ' * indent)
  6. sys.stdout.write('"')
  7. quoted = True
  8. for c in bytes:
  9. if not quoted:
  10. sys.stdout.write('\n')
  11. sys.stdout.write(' ' * indent)
  12. sys.stdout.write('"')
  13. quoted = True
  14. if c == b'"'[0]:
  15. sys.stdout.write('\\"')
  16. elif c == b'\\'[0]:
  17. sys.stdout.write('\\\\')
  18. elif c == b'\n'[0]:
  19. sys.stdout.write('\\n"')
  20. quoted = False
  21. elif c >= 32 and c < 127:
  22. sys.stdout.write(c.to_bytes(1, 'big').decode())
  23. else:
  24. sys.stdout.write(f'\{c:03o}')
  25. if quoted:
  26. sys.stdout.write('"')
  27. sys.stdout.write('#include "qemu/osdep.h"\n' \
  28. '#include "exec/gdbstub.h"\n' \
  29. '\n'
  30. 'const GDBFeature gdb_static_features[] = {\n')
  31. for input in sys.argv[1:]:
  32. with open(input, 'rb') as file:
  33. read = file.read()
  34. sys.stdout.write(' {\n')
  35. writeliteral(8, bytes(os.path.basename(input), 'utf-8'))
  36. sys.stdout.write(',\n')
  37. writeliteral(8, read)
  38. sys.stdout.write('\n },\n')
  39. sys.stdout.write(' { NULL }\n};\n')