diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-13 02:24:39 +0000 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-13 02:24:39 +0000 |
| commit | 8cb846c0ad784c4ce249958113d59a331dd207e8 (patch) | |
| tree | 328f83d7e19710fcb52c7659962a1363492cb907 /src/exec.h | |
| parent | c3b270f82dd62a56e9d75cdd652c55a694a2da64 (diff) | |
| download | zash-8cb846c0ad784c4ce249958113d59a331dd207e8.tar.gz | |
Add extensions, job control and the interactive layer; convert to C11
Language: $'...', aliases, function keyword, (( )) and let, typeset and
integer, print, whence, setvar, ulimit, read -t, RANDOM, SECONDS, the
ERR trap, trap -p, chdir, and more set -o names.
Job control: a job table, process groups, terminal hand-over through
the spawn layer, jobs/fg/bg, wait %n and kill %n.
Interactive: prompt expansion, a built-in line editor with emacs and vi
modes, completion, history with fc and HISTFILE, and libedit as a
compile-time alternative (make EDIT=libedit LDLIBS=-ledit).
The source is now C11 (was C23), uses typedef'd structs and enums, and
has Doxygen comments throughout. Adds .clang-tidy, .clangd and a
Doxyfile.
Diffstat (limited to 'src/exec.h')
| -rw-r--r-- | src/exec.h | 217 |
1 files changed, 152 insertions, 65 deletions
@@ -1,5 +1,6 @@ -/* - * exec.h: command evaluation, redirection, jobs, traps and builtins. +/** + * @file exec.h + * @brief Command evaluation, redirection, jobs, traps and builtins. */ #ifndef ZASH_EXEC_H #define ZASH_EXEC_H @@ -12,135 +13,212 @@ /* ---- evaluation (exec.c) --------------------------------------------- */ -enum : unsigned { - 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 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 */ }; -enum : unsigned { - 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 */ +/** + * @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 */ }; -enum skip : unsigned char { SKIP_NONE, SKIP_BREAK, SKIP_CONTINUE, SKIP_RETURN }; +/** + * @brief Kinds of non-local exit handled without exceptions. + */ +typedef enum skip_type { + SKIP_NONE, + SKIP_BREAK, + SKIP_CONTINUE, + SKIP_RETURN +} skip_type; -extern enum skip skip_kind; /* a break/continue/return is in progress */ -extern int skip_count; /* loop levels left for break/continue */ +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; /* $! */ +extern pid_t last_bg_pid; /**< $! */ -int eval_node(struct node *n, unsigned flags); +int eval_node(node *n, unsigned flags); int eval_string(const char *s, unsigned flags); int run_input(unsigned flags); -char *cmdsub_run(struct node *n, size_t *len); +char *cmdsub_run(node *n, size_t *len); void exec_init(void); -[[noreturn]] void shell_exec(char **argv, char **envp); +_Noreturn void shell_exec(char **argv, char **envp); /* ---- command table --------------------------------------------------- */ -enum : unsigned { - BI_SPECIAL = 1u << 0, /* POSIX special builtin */ - BI_PURE = 1u << 1, /* only writes to out1/out2: safe to run in the +/** + * @brief Builtin properties. + */ +enum { + BI_SPECIAL = 1u << 0, /**< POSIX special builtin */ + BI_PURE = 1u << 1, /**< only writes to out1/out2: safe to run in the shell in place of a subshell */ }; -struct builtin { +/** + * @brief A builtin command. + */ +typedef struct builtin { const char *name; int (*fn)(int argc, char **argv); unsigned flags; -}; +} builtin; -extern const struct builtin builtins[]; +extern const builtin builtins[]; extern const size_t nbuiltins; -struct func { - struct node *body; - struct arena *arena; +/** + * @brief A shell function. + */ +typedef struct func { + node *body; + arena *arena; int refs; -}; +} func; -enum cmdkind : unsigned char { C_NOTFOUND, C_BUILTIN, C_FUNC, C_EXTERNAL }; +/** + * @brief What a command name can refer to. + */ +typedef enum cmdkind { C_NOTFOUND, C_BUILTIN, C_FUNC, C_EXTERNAL } cmdkind; -struct cmdinfo { - enum cmdkind kind; - const struct builtin *bi; - struct func *fn; +/** + * @brief The result of looking a command name up. + */ +typedef struct cmdinfo { + cmdkind kind; + const builtin *bi; + func *fn; const char *path; -}; +} cmdinfo; -enum : unsigned { - FC_NOFUNC = 1u << 0, /* skip functions (the `command` builtin) */ - FC_DEFPATH = 1u << 1, /* search the default PATH (command -p) */ +/** + * @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, struct cmdinfo *ci, unsigned flags); -void func_define(const char *name, struct node *body, struct arena *arena); +void find_command(const char *name, cmdinfo *ci, unsigned flags); +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(struct func *fn, int argc, char **argv, unsigned flags); +int call_function(func *fn, int argc, char **argv, unsigned flags); /* ---- redirection (redir.c) ------------------------------------------- */ -struct redirsave { +/** + * @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 */ + int copy; /**< where the old descriptor was moved; -1 if it was closed + */ } *e; -}; +} redirsave; -extern struct redirsave *redir_top; +extern redirsave *redir_top; extern unsigned fd_open_mask; -[[nodiscard]] bool redir_push(const struct redir *list, struct redirsave *sv); -void redir_push_fd(struct redirsave *sv, int fd, int src); +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(struct redirsave *to); +void redir_unwind(redirsave *to); -/* - * The descriptors a child should start with, expressed without changing +/** + * @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]. */ -struct fdplan { +typedef struct fdplan { int map[SYS_NFD]; - int *temps; /* descriptors opened for the plan, to close afterwards */ + int *temps; /**< descriptors opened for the plan, to close afterwards */ int ntemps; -}; +} fdplan; -void plan_init(struct fdplan *p, int in, int out); -[[nodiscard]] bool plan_redirs(struct fdplan *p, const struct redir *list); -void plan_close(struct fdplan *p); -void plan_apply(struct fdplan *p); +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); +void jobs_add(pid_t pid, const node *n); void jobs_reap(void); -int jobs_wait(pid_t pid); -int jobs_wait_all(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 ------------------------------------------------------- */ -/* State that an exception handler must put back before continuing. */ -struct unwind { - struct varscope *scope; - struct redirsave *redir; - struct input *input; -}; +/* 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(struct unwind *u); -void unwind_restore(const struct unwind *u); +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); @@ -157,8 +235,17 @@ int builtin_test(int argc, char **argv); 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_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(struct output *o, const char *s, bool echo_style); +bool out_escaped(output *o, const char *s, bool echo_style); #endif |
