---@meta spawn.posix

--------------------------------------------------------------------------------
-- spawn.posix C module
--------------------------------------------------------------------------------

---@class spawn_posix_sigset*
local spawn_posix_sigset = {}

---@class spawn_posix_attr_flags*
---@field resetids? boolean
---@field setpgroup? boolean
---@field setsigdef? boolean
---@field setsigmask? boolean
---@field setsid? boolean
---@field cloexec_default? boolean
---@field setexec? boolean
---@field start_suspended? boolean
---@field setscheduler? boolean
---@field setschedparam? boolean
---@field usevfork? boolean

---@class spawn_posix_open_flags*
---@field rdwr? boolean
---@field wronly? boolean
---@field rdonly? boolean
---@field append? boolean
---@field creat? boolean
---@field excl? boolean
---@field noctty? boolean
---@field nonblock? boolean
---@field trunc? boolean
---@field sync? boolean
---@field dsync? boolean
---@field rsync? boolean
---@field async? boolean
---@field cloexec? boolean
---@field direct? boolean
---@field directory? boolean
---@field largefile? boolean
---@field ndelay? boolean
---@field noatime? boolean
---@field nofollow? boolean
---@field path? boolean
---@field tmpfile? boolean

---@class spawn_posix_attr*
local spawn_posix_attr = {}

--- Get the default signal collection.
---@param set? spawn_posix_sigset* Reuse sigset
---@return spawn_posix_sigset*? set signal collection
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:getsigdefault(set) end

--- Set the default signal collection.
---@param set spawn_posix_sigset* signal collection
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:setsigdefault(set) end

--- Get the signal mask.
---@param set? spawn_posix_sigset* Reuse sigset
---@return spawn_posix_sigset*? set signal collection
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:getsigmask(set) end

--- Set signal mask.
---@param set spawn_posix_sigset* signal collection
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:setsigmask(set) end

--- Get spawn flags.
---@return spawn_posix_attr_flags*? flags flags table
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:getflags() end

--- Set spawn flags.
---@param flags spawn_posix_attr_flags* flags table
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:setflags(flags) end

--- Get the process group.
---@return integer? pgroup process group
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:getpgroup() end

--- Set process group.
---@param pgroup integer process group
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:setpgroup(pgroup) end

--- Get the scheduling policy.
---@return integer? policy Scheduling strategy
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:getschedpolicy() end

--- Set scheduling policy.
---@param policy integer Scheduling strategy
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_attr:setschedpolicy(policy) end

---@class spawn_posix_file_actions*
local spawn_posix_file_actions = {}

--- Add open file action.
---@param fd integer target fd
---@param path string file path
---@param oflag integer|spawn_posix_open_flags* open flags or flags table
---@param mode? integer file mode
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_file_actions:addopen(fd, path, oflag, mode) end

--- Add close file action.
---@param fd integer fd
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_file_actions:addclose(fd) end

--- Add dup2 file action.
---@param fd integer Source fd
---@param newfd integer target fd
---@return boolean? ok true on success
---@return string? errMsg error message
---@return integer? errno error code
function spawn_posix_file_actions:adddup2(fd, newfd) end

---@class spawn_posix*
---@type spawn_posix*
local M = {}

--- Call `posix_spawn`.
---@param path string Program path
---@param fileActions? spawn_posix_file_actions* File action
---@param attr? spawn_posix_attr* spawn Properties
---@param argv string[] parameter array
---@param envp? string[] Array of environment variables; if not passed, the current environment will be inherited.
---@return integer? pid child process pid
---@return string? errMsg error message
---@return integer? errno error code
function M.spawn(path, fileActions, attr, argv, envp) end

--- Call `posix_spawnp`, find the program by PATH.
---@param file string Program name
---@param fileActions? spawn_posix_file_actions* File action
---@param attr? spawn_posix_attr* spawn Properties
---@param argv string[] parameter array
---@param envp? string[] Array of environment variables; if not passed, the current environment will be inherited.
---@return integer? pid child process pid
---@return string? errMsg error message
---@return integer? errno error code
function M.spawnp(file, fileActions, attr, argv, envp) end

--- Create a spawn property object.
---@return spawn_posix_attr*? attr property object
---@return string? errMsg error message
---@return integer? errno error code
function M.new_attr() end

--- Create a spawn file action object.
---@return spawn_posix_file_actions*? fileActions file action object
---@return string? errMsg error message
---@return integer? errno error code
function M.new_file_actions() end

return M
