/** * @file exec.h * @brief Command evaluation, redirection, jobs, traps and builtins. */ #ifndef ZASH_EXEC_H #define ZASH_EXEC_H #include #include "ast.h" #include "sys.h" #include "var.h" /* ---- evaluation (exec.c) --------------------------------------------- */ /** * @brief Flags for eval_node(). */ enum { EV_EXIT = 1u << 0, /**< the shell exits after this: exec is allowed */ EV_TESTED = 1u << 1, /**< status is being tested: errexit does not apply */ }; /** * @brief Flags for run_input(). */ enum { RI_TOP = 1u << 0, /**< the main loop: recover from errors if a tty */ RI_EXEC_LAST = 1u << 1, /**< the last command may replace the shell */ RI_RECOVER = 1u << 2, /**< a startup file: recover, but no prompt */ }; /** * @brief Kinds of non-local exit handled without exceptions. */ typedef enum skip_type { SKIP_NONE, SKIP_BREAK, SKIP_CONTINUE, SKIP_RETURN, } skip_type; extern skip_type skip_kind; /**< a break/continue/return is in progress */ extern int skip_count; /**< loop levels left for break/continue */ extern int loop_depth; extern int func_depth; extern int dot_depth; extern pid_t last_bg_pid; /**< $! */ int eval_node(node *n, unsigned flags); int eval_string(const char *s, unsigned flags); int run_input(unsigned flags); char *cmdsub_run(node *n, size_t *len); void exec_init(void); [[noreturn]] void shell_exec(char **argv, char **envp); /* ---- command table --------------------------------------------------- */ /** * @brief Builtin properties. */ enum { BI_SPECIAL = 1u << 0, /**< POSIX special builtin */ BI_PURE = 1u << 1, BI_CD = 1u << 2, /**< changes directory state; see builtin_cd_isolated() */ }; /** * @brief A builtin command. */ typedef struct builtin { const char *name; int (*fn)(int argc, char **argv); unsigned flags; } builtin; extern const builtin builtins[]; extern const size_t nbuiltins; /** * @brief A shell function. */ typedef struct func { node *body; arena *arena; int refs; } func; /** * @brief What a command name can refer to. */ typedef enum cmdkind { C_NOTFOUND, C_BUILTIN, C_FUNC, C_EXTERNAL } cmdkind; /** * @brief The result of looking a command name up. */ typedef struct cmdinfo { cmdkind kind; const builtin *bi; func *fn; const char *path; } cmdinfo; /** * @brief Flags for find_command(). */ enum { FC_NOFUNC = 1u << 0, /**< skip functions (the `command` builtin) */ FC_DEFPATH = 1u << 1, /**< search the default PATH (command -p) */ }; void find_command(const char *name, cmdinfo *ci, unsigned flags); void builtin_cd_isolated(int argc, char **argv, int *status); void func_define(const char *name, node *body, arena *owner); bool func_unset(const char *name); void func_each(void (*fn)(const char *name, void *arg), void *arg); void hash_clear(void); void hash_print(void); char *path_search(const char *name, const char *path, bool executable); int call_function(func *fn, int argc, char **argv, unsigned flags); /* ---- redirection (redir.c) ------------------------------------------- */ /** * @brief The descriptors a redir_push() replaced, for redir_pop(). */ typedef struct redirsave { struct redirsave *prev; int n; struct { int fd; int copy; /**< where the old descriptor was moved; -1 if it was closed */ } *e; } redirsave; extern redirsave *redir_top; extern unsigned fd_open_mask; [[nodiscard]] bool redir_push(const redir *list, redirsave *sv); void redir_push_fd(redirsave *sv, int fd, int src); void redir_pop(bool keep); void redir_unwind(redirsave *to); /** * @brief The descriptors a child should start with, expressed without changing * the shell's own: child descriptor i is to be the shell's map[i]. */ typedef struct fdplan { int map[SYS_NFD]; int *temps; /**< descriptors opened for the plan, to close afterwards */ int ntemps; } fdplan; void plan_init(fdplan *p, int in, int out); [[nodiscard]] bool plan_redirs(fdplan *p, const redir *list); void plan_close(fdplan *p); void plan_apply(fdplan *p); /* ---- processes (exec.c) ---------------------------------------------- */ pid_t fork_shell(void); /* ---- jobs (jobs.c) --------------------------------------------------- */ typedef struct job job; extern job *job_list; extern job *job_launching; int wait_fg(pid_t pid); void jobs_add(pid_t pid, const node *n); void jobs_reap(void); void jobs_notify(void); void jobs_forget(void); int jobs_stopped(void); bool jc_active(void); void jc_sync(void); void jc_exit(void); int job_tty(void); job *job_begin(const node *n, bool fg); pid_t job_launch_pgid(void); void job_child_setup(void); void job_add_proc(job *j, pid_t pid); int job_wait_fg(job *j); int job_status_of(const job *j, pid_t pid); void job_release(job *j); void job_end_bg(job *j); int job_signal(const char *spec, int sig); int builtin_wait(int argc, char **argv); int builtin_jobs(int argc, char **argv); int builtin_fg(int argc, char **argv); int builtin_bg(int argc, char **argv); /* ---- unparse.c ------------------------------------------------------- */ char *node_text(const node *n); /* ---- unwinding ------------------------------------------------------- */ /* Defined in parser.h. */ typedef struct input_src input_src; /** * @brief State that an exception handler must put back before continuing. */ typedef struct unwind { varscope *scope; redirsave *redir; input_src *input; } unwind; void unwind_save(unwind *u); void unwind_restore(const unwind *u); /* ---- traps (trap.c) -------------------------------------------------- */ extern volatile sig_atomic_t trap_pending; extern bool trap_err_set; void trap_jobctl(bool on); void trap_note(int sig); void trap_run_err(void); void trap_init(void); void trap_poll(void); void trap_reset_child(void); bool trap_has_exit(void); void trap_run_exit(void); const sigset_t *trap_spawn_defaults(void); int builtin_trap(int argc, char **argv); int builtin_kill(int argc, char **argv); /* ---- builtins in their own files ------------------------------------- */ int builtin_test(int argc, char **argv); bool test_unary(char op, const char *arg); bool test_files(const char *a, char op, const char *b); int eval_cond(const cond *c); int builtin_printf(int argc, char **argv); int builtin_command(int argc, char **argv); int builtin_exec(int argc, char **argv); int builtin_print(int argc, char **argv); int builtin_whence(int argc, char **argv); int builtin_setvar(int argc, char **argv); int builtin_typeset(int argc, char **argv); int builtin_let(int argc, char **argv); int builtin_ulimit(int argc, char **argv); int builtin_tag(int argc, char **argv); int builtin_alloc(int argc, char **argv); int builtin_free(int argc, char **argv); int builtin_fc(int argc, char **argv); int builtin_alias(int argc, char **argv); int builtin_unalias(int argc, char **argv); int describe_command(const char *name, bool verbose); void getopts_reset(const char *value); bool out_escaped(output *o, const char *s, bool echo_style); #endif