1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#!/usr/bin/python
import string, sys, os.path
escapes = {}
escapes['\a'] = r'\a'
escapes['\b'] = r'\b'
escapes['\f'] = r'\f'
escapes['\n'] = r'\n'
escapes['\r'] = r'\r'
#escapes['\t'] = r'\t'
# Let's replace tabs with four spaces
# so the text looks nicely indented in the C source
escapes['\t'] = r' '
escapes['\v'] = r'\v'
# escapes['\''] = r'\''
escapes['\"'] = r'\"'
escapes['\\'] = r'\\'
def escape(c):
if c in escapes:
return escapes[c]
elif c not in string.printable:
return "\\x%x" % ord(c)
else:
return c
def stringize(line):
newline = '"'
for c in line:
newline += escape(c)
newline += '"'
return newline
class cfunc:
def __init__(self, name, lines):
self.name = name
self.lines = lines
def cdef(self):
result = ""
result += "static const char * const %s = \n\t" % self.cfunc_name()
result += '\n\t'.join(self.lines)
result += ';\n'
return result
def cfunc_name(self):
munged_name = string.replace(self.name, '-', '_')
return "function_%s" % munged_name
funcs = []
for file in sys.argv[1:]:
fd = open(file, 'r')
newlines = []
for line in fd:
newlines.append(stringize(line))
fd.close()
name = os.path.basename(file)
name, ext = os.path.splitext(name)
funcs.append(cfunc(name, newlines))
# Sort our functions by name
funcs.sort(key=cfunc.cfunc_name)
# Output our header
fd = open('builtin_scripts.h', 'w')
fd.write('/* This file is generated by internalize_scripts.py */\n\n')
fd.write("""struct builtin_script_t {
const char *name;
const char *def;
};""")
fd.write('\n')
fd.write('\n')
fd.write('extern const struct builtin_script_t internal_function_scripts[%d];\n' % len(funcs))
fd.write('\n')
fd.close()
# Output the function definitions
fd = open('builtin_scripts.cpp', 'w')
fd.write('/* This file is generated by internalize_scripts.py */\n\n')
fd.write('#include "builtin_scripts.h"\n\n')
for func in funcs:
fd.write(func.cdef())
fd.write('\n')
# Output the refs
func_refs = ["{%s, %s}" % (stringize(func.name), func.cfunc_name()) for func in funcs]
fd.write('const struct builtin_script_t internal_function_scripts[%d] =\n' % len(funcs))
fd.write('{\n\t')
fd.write(',\n\t'.join(func_refs))
fd.write('\n};\n')
|