-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_errno.lua
executable file
·70 lines (59 loc) · 1.32 KB
/
build_errno.lua
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
#!/usr/bin/luajit
local strings = require('useful.strings')
local format = string.format
local join = table.concat
local template = [=[
--
-- p o s i x . e r r n o
--
-- auto-generated by: build_errno.lua
local errno = { }
local ffi = require('ffi')
local C = ffi.C
ffi.cdef [[
enum {
%s
};
]]
errno.names = {
%s
}
return setmetatable(errno, {
__index = function(t, n)
t[n] = C[n]
return t[n]
end,
})
]=]
local entries = { }
local f = io.popen('errno -l')
for line in f:lines() do
local fields = { line:match('(%S+)%s+(%S+)%s(.*)') }
table.insert(entries, { name=fields[1], errno=tonumber(fields[2]) })
end
table.sort(entries, function(a,b) return a.errno < b.errno end)
local enums = { }
local names = { }
local last = 0
for _,entry in ipairs(entries) do
local len8 = math.floor(#entry.name / 8)
local tabs = string.rep('\t', 3-len8)
local enum = format('\t%s%s= %d,', entry.name, tabs, entry.errno)
table.insert(enums, enum)
local name
if entry.errno == last then
-- ignore
else
name = format('\t"%s",\t-- %d', entry.name, entry.errno)
name = format('\t[%d] = "%s",\t-- %d',
entry.errno, entry.name, entry.errno)
end
last = entry.errno
table.insert(names, name)
end
if arg[1] == '-' then
f = io.stdout
else
f = io.open(arg[1], 'w')
end
f:write(format(template, join(enums, '\n'), join(names, '\n')))