Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/tools/syscalls/scripts/libsys_h.lua
39530 views
1
#!/usr/libexec/flua
2
--
3
-- SPDX-License-Identifier: BSD-2-Clause
4
--
5
-- Copyright (c) 2024 SRI International
6
-- Copyright (c) 2024 Tyler Baxter <[email protected]>
7
-- Copyright (c) 2023 Warner Losh <[email protected]>
8
-- Copyright (c) 2019 Kyle Evans <[email protected]>
9
--
10
11
-- Setup to be a module, or ran as its own script.
12
local libsys_h = {}
13
local script = not pcall(debug.getlocal, 4, 1) -- TRUE if script.
14
if script then
15
-- Add library root to the package path.
16
local path = arg[0]:gsub("/[^/]+.lua$", "")
17
package.path = package.path .. ";" .. path .. "/../?.lua"
18
end
19
20
local FreeBSDSyscall = require("core.freebsd-syscall")
21
local generator = require("tools.generator")
22
local util = require("tools.util")
23
24
-- File has not been decided yet; config will decide file. Default defined as
25
-- /dev/null.
26
libsys_h.file = "/dev/null"
27
28
function libsys_h.generate(tbl, config, fh)
29
-- Grab the master system calls table.
30
local s = tbl.syscalls
31
32
local print_decl = function (sc)
33
return sc:native() and not sc.type.NODEF and
34
not sc.type.NOLIB and not sc.type.SYSMUX
35
end
36
37
-- Bind the generator to the parameter file.
38
local gen = generator:new({}, fh)
39
40
-- Write the generated preamble.
41
gen:preamble("Public system call stubs provided by libsys.\n" ..
42
"\n" ..
43
"Do not use directly, include <libsys.h> instead.")
44
45
gen:write(string.format([[
46
#ifndef __LIBSYS_H_
47
#define __LIBSYS_H_
48
49
#include <sys/_cpuset.h>
50
#include <sys/_domainset.h>
51
#include <sys/_ffcounter.h>
52
#include <sys/_semaphore.h>
53
#include <sys/_sigaltstack.h>
54
#include <machine/ucontext.h> /* for mcontext_t */
55
#include <sys/_ucontext.h>
56
#include <sys/wait.h>
57
58
]]))
59
60
for name, _ in util.pairsByKeys(tbl.structs) do
61
gen:write(string.format("struct %s;\n", name))
62
end
63
gen:write("union semun;\n")
64
65
gen:write("\n__BEGIN_DECLS\n")
66
67
for _, v in pairs(s) do
68
if print_decl(v) then
69
gen:write(string.format(
70
"typedef %s (__sys_%s_t)(%s);\n",
71
v.ret, v.name, v.argstr_type))
72
end
73
end
74
75
gen:write("\n")
76
77
for _, v in pairs(s) do
78
if print_decl(v) then
79
local ret_attr = "";
80
if v.type.NORETURN then
81
ret_attr = "_Noreturn "
82
end
83
gen:write(string.format("%s%s __sys_%s(%s);\n",
84
ret_attr, v.ret, v.name, v.argstr_type_var))
85
end
86
end
87
88
gen:write("__END_DECLS\n")
89
-- End
90
gen:write("\n#endif /* __LIBSYS_H_ */\n")
91
end
92
93
-- Entry of script:
94
if script then
95
local config = require("config")
96
97
if #arg < 1 or #arg > 2 then
98
error("usage: " .. arg[0] .. " syscall.master")
99
end
100
101
local sysfile, configfile = arg[1], arg[2]
102
103
config.merge(configfile)
104
config.mergeCompat()
105
106
-- The parsed syscall table.
107
local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}
108
109
libsys_h.file = config.libsys_h -- change file here
110
libsys_h.generate(tbl, config, libsys_h.file)
111
end
112
113
-- Return the module.
114
return libsys_h
115
116