xref: /freebsd/sys/tools/syscalls/scripts/syscalls.lua (revision 7ab1a32cd43cbae61ad4dd435d6a482bbf61cb52)
1#!/usr/libexec/flua
2--
3-- SPDX-License-Identifier: BSD-2-Clause
4--
5-- Copyright (c) 2024 Tyler Baxter <agge@FreeBSD.org>
6-- Copyright (c) 2023 Warner Losh <imp@bsdimp.com>
7-- Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
8--
9
10-- Setup to be a module, or ran as its own script.
11local syscalls = {}
12local script = not pcall(debug.getlocal, 4, 1)	-- TRUE if script.
13if script then
14	-- Add library root to the package path.
15	local path = arg[0]:gsub("/[^/]+.lua$", "")
16	package.path = package.path .. ";" .. path .. "/../?.lua"
17end
18
19local FreeBSDSyscall = require("core.freebsd-syscall")
20local generator = require("tools.generator")
21
22-- File has not been decided yet; config will decide file.  Default defined as
23-- /dev/null.
24syscalls.file = "/dev/null"
25
26function syscalls.generate(tbl, config, fh)
27	-- Grab the master system calls table.
28	local s = tbl.syscalls
29
30	-- Bind the generator to the parameter file.
31	local gen = generator:new({}, fh)
32
33	-- Write the generated preamble.
34	gen:preamble("System call names.")
35
36	gen:write(string.format("const char *%s[] = {\n", config.namesname))
37
38	for _, v in pairs(s) do
39		--print("num " .. v.num .. " name " .. v.name)
40		local c = v:compatLevel()
41		if v:native() then
42			gen:write(string.format([[
43	"%s",			/* %d = %s */
44]],
45			    v.name, v.num, v.name))
46		elseif c >= 3 then
47			-- Lookup the info for this specific compat option.
48			local flag, descr
49			for _, opt in pairs(config.compat_options) do
50				if opt.compatlevel == c then
51					flag = opt.flag
52					flag = flag:lower()
53					descr = opt.descr
54					break
55				end
56			end
57
58			gen:write(string.format([[
59	"%s.%s",		/* %d = %s %s */
60]],
61			    flag, v.name, v.num, descr, v.name))
62
63		elseif v.type.RESERVED then
64			gen:write(string.format([[
65	"#%d",			/* %d = reserved for local use */
66]],
67			    v.num, v.num))
68
69		elseif v.type.UNIMPL then
70			gen:write(string.format([[
71	"#%d",			/* %d = %s */
72]],
73			    v.num, v.num, v.alias))
74
75		elseif v.type.OBSOL then
76			gen:write(string.format([[
77	"obs_%s",			/* %d = obsolete %s */
78]],
79			    v.name, v.num, v.name))
80
81		end
82	end
83	-- End
84	gen:write("};\n")
85end
86
87-- Entry of script:
88if script then
89	local config = require("config")
90
91	if #arg < 1 or #arg > 2 then
92		error("usage: " .. arg[0] .. " syscall.master")
93	end
94
95	local sysfile, configfile = arg[1], arg[2]
96
97	config.merge(configfile)
98	config.mergeCompat()
99
100	-- The parsed syscall table.
101	local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}
102
103	syscalls.file = config.sysnames	-- change file here
104	syscalls.generate(tbl, config, syscalls.file)
105end
106
107-- Return the module.
108return syscalls
109