xref: /freebsd/sys/tools/syscalls/scripts/libsys_h.lua (revision 47ef2a131091508e049ab10cad7f91a3c1342cd9)
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 <agge@FreeBSD.org>
7-- Copyright (c) 2023 Warner Losh <imp@bsdimp.com>
8-- Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
9--
10
11-- Setup to be a module, or ran as its own script.
12local libsys_h = {}
13local script = not pcall(debug.getlocal, 4, 1)	-- TRUE if script.
14if script then
15	-- Add library root to the package path.
16	local path = arg[0]:gsub("/[^/]+.lua$", "")
17	package.path = package.path .. ";" .. path .. "/../?.lua"
18end
19
20local FreeBSDSyscall = require("core.freebsd-syscall")
21local generator = require("tools.generator")
22local util = require("tools.util")
23
24-- File has not been decided yet; config will decide file.  Default defined as
25-- /dev/null.
26libsys_h.file = "/dev/null"
27
28function 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			gen:write(string.format("%s __sys_%s(%s);\n",
80			    v.ret, v.name, v.argstr_type_var))
81		end
82	end
83
84	gen:write("__END_DECLS\n")
85	-- End
86	gen:write("\n#endif /* __LIBSYS_H_ */\n")
87end
88
89-- Entry of script:
90if script then
91	local config = require("config")
92
93	if #arg < 1 or #arg > 2 then
94		error("usage: " .. arg[0] .. " syscall.master")
95	end
96
97	local sysfile, configfile = arg[1], arg[2]
98
99	config.merge(configfile)
100	config.mergeCompat()
101
102	-- The parsed syscall table.
103	local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}
104
105	libsys_h.file = config.libsys_h	-- change file here
106	libsys_h.generate(tbl, config, libsys_h.file)
107end
108
109-- Return the module.
110return libsys_h
111