Path: blob/main/sys/tools/syscalls/scripts/libsys_h.lua
39530 views
#!/usr/libexec/flua1--2-- SPDX-License-Identifier: BSD-2-Clause3--4-- Copyright (c) 2024 SRI International5-- Copyright (c) 2024 Tyler Baxter <[email protected]>6-- Copyright (c) 2023 Warner Losh <[email protected]>7-- Copyright (c) 2019 Kyle Evans <[email protected]>8--910-- Setup to be a module, or ran as its own script.11local libsys_h = {}12local script = not pcall(debug.getlocal, 4, 1) -- TRUE if script.13if script then14-- Add library root to the package path.15local path = arg[0]:gsub("/[^/]+.lua$", "")16package.path = package.path .. ";" .. path .. "/../?.lua"17end1819local FreeBSDSyscall = require("core.freebsd-syscall")20local generator = require("tools.generator")21local util = require("tools.util")2223-- File has not been decided yet; config will decide file. Default defined as24-- /dev/null.25libsys_h.file = "/dev/null"2627function libsys_h.generate(tbl, config, fh)28-- Grab the master system calls table.29local s = tbl.syscalls3031local print_decl = function (sc)32return sc:native() and not sc.type.NODEF and33not sc.type.NOLIB and not sc.type.SYSMUX34end3536-- Bind the generator to the parameter file.37local gen = generator:new({}, fh)3839-- Write the generated preamble.40gen:preamble("Public system call stubs provided by libsys.\n" ..41"\n" ..42"Do not use directly, include <libsys.h> instead.")4344gen:write(string.format([[45#ifndef __LIBSYS_H_46#define __LIBSYS_H_4748#include <sys/_cpuset.h>49#include <sys/_domainset.h>50#include <sys/_ffcounter.h>51#include <sys/_semaphore.h>52#include <sys/_sigaltstack.h>53#include <machine/ucontext.h> /* for mcontext_t */54#include <sys/_ucontext.h>55#include <sys/wait.h>5657]]))5859for name, _ in util.pairsByKeys(tbl.structs) do60gen:write(string.format("struct %s;\n", name))61end62gen:write("union semun;\n")6364gen:write("\n__BEGIN_DECLS\n")6566for _, v in pairs(s) do67if print_decl(v) then68gen:write(string.format(69"typedef %s (__sys_%s_t)(%s);\n",70v.ret, v.name, v.argstr_type))71end72end7374gen:write("\n")7576for _, v in pairs(s) do77if print_decl(v) then78local ret_attr = "";79if v.type.NORETURN then80ret_attr = "_Noreturn "81end82gen:write(string.format("%s%s __sys_%s(%s);\n",83ret_attr, v.ret, v.name, v.argstr_type_var))84end85end8687gen:write("__END_DECLS\n")88-- End89gen:write("\n#endif /* __LIBSYS_H_ */\n")90end9192-- Entry of script:93if script then94local config = require("config")9596if #arg < 1 or #arg > 2 then97error("usage: " .. arg[0] .. " syscall.master")98end99100local sysfile, configfile = arg[1], arg[2]101102config.merge(configfile)103config.mergeCompat()104105-- The parsed syscall table.106local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}107108libsys_h.file = config.libsys_h -- change file here109libsys_h.generate(tbl, config, libsys_h.file)110end111112-- Return the module.113return libsys_h114115116