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