-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathluaopts
executable file
·99 lines (73 loc) · 1.74 KB
/
luaopts
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
93
94
95
96
97
98
#!/usr/bin/ruby -w
=begin
This approach doesn't work well when the options found can differ by platform.
At least for defines, there need to be ifdef guards around them. PF_ defines, for example, vary
across libc.
=end
pattern = ARGV.shift
RXDEF = %r{^\s*(?:[#]\s*define)?\s*#{pattern}_(\w+)(.*)}
RXDOC = %r{/[/*](.*)}
OPTS = []
ARGF.each do |line|
next unless line =~ RXDEF
name = $1
doc = nil
if $2 =~ RXDOC
doc = $1.gsub(/\*\/.*/, "").strip
end
#p [name, doc, line]
next if name == "MAX"
cname = pattern + "_" + name
lname = name.gsub(/_/, "-").downcase
=begin
Catch cases like:
enum
{
IPPROTO_IP = 0, /* Dummy protocol for TCP. */
#define IPPROTO_IP IPPROTO_IP
=end
if OPTS.last and OPTS.last[1] == cname
if not OPTS.last[2]
OPTS.last[2] = doc
end
next
end
OPTS << [lname, cname, doc]
#p OPTS.last
end
width = 10
OPTS.each do |lname, cname, doc|
if lname.size > width
width = lname.size + 2
end
end
puts "-- DOCUMENTATION"
OPTS.each do |lname, cname, doc|
line = " #{lname}#{" " * (width - lname.size)}-- #{cname}"
if doc
line << ", #{doc}"
end
puts line
end
puts""
puts""
puts "static const char* " + pattern + "_opts[] = {"
OPTS.each do |lname, cname, doc|
puts " #{lname.inspect},"
end
puts " NULL"
puts "};"
puts "static int " + pattern + "_vals[] = {"
OPTS.each do |lname, cname, doc|
puts " #{cname},"
end
puts "};"
puts "static int " + pattern + "_vals_size = " + OPTS.size.to_s + ";" # cvt to sizeof(pattern _vals) / sizeof(pattern _vals[0])
puts <<__
static int check_#{pattern}(lua_State* L, int argn)
{
int opt = luaL_checkoption(L, argn, NULL /* default? */, #{pattern}_opts);
int val = #{pattern}_vals[opt];
return val;
}
__