summaryrefslogtreecommitdiffhomepage
path: root/src/exec.h
blob: b278ee0e54bf77f780204e29ad7cc6529787ad41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
 * @file exec.h
 * @brief Command evaluation, redirection, jobs, traps and builtins.
 */
#ifndef ZASH_EXEC_H
#define ZASH_EXEC_H

#include <signal.h>

#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,
};

/**
 * @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);
int perf_ablate(void);
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