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 42 gen:write(v.prolog); 43 44 if v:native() then 45 gen:write(string.format([[ 46 "%s", /* %d = %s */ 47]], 48 v.name, v.num, v.name)) 49 elseif c >= 3 then 50 -- Lookup the info for this specific compat option. 51 local flag, descr 52 for _, opt in pairs(config.compat_options) do 53 if opt.compatlevel == c then 54 flag = opt.flag 55 flag = flag:lower() 56 descr = opt.descr 57 break 58 end 59 end 60 61 gen:write(string.format([[ 62 "%s.%s", /* %d = %s %s */ 63]], 64 flag, v.name, v.num, descr, v.name)) 65 66 elseif v.type.RESERVED then 67 gen:write(string.format([[ 68 "#%d", /* %d = reserved for local use */ 69]], 70 v.num, v.num)) 71 72 elseif v.type.UNIMPL then 73 gen:write(string.format([[ 74 "#%d", /* %d = %s */ 75]], 76 v.num, v.num, v.alias)) 77 78 elseif v.type.OBSOL then 79 gen:write(string.format([[ 80 "obs_%s", /* %d = obsolete %s */ 81]], 82 v.name, v.num, v.name)) 83 84 end 85 end 86 gen:write(tbl.epilog) 87 -- End 88 gen:write("};\n") 89end 90 91-- Entry of script: 92if script then 93 local config = require("config") 94 95 if #arg < 1 or #arg > 2 then 96 error("usage: " .. arg[0] .. " syscall.master") 97 end 98 99 local sysfile, configfile = arg[1], arg[2] 100 101 config.merge(configfile) 102 config.mergeCompat() 103 104 -- The parsed syscall table. 105 local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config} 106 107 syscalls.file = config.sysnames -- change file here 108 syscalls.generate(tbl, config, syscalls.file) 109end 110 111-- Return the module. 112return syscalls 113