summaryrefslogtreecommitdiff
path: root/src/exec.c
blob: d01b03e9a9ece683c0bf7709ae55e672c8a362fa (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>

#include "syntax.h"
#include "utils.h"
#include "stages.h"

void execute_command_pipeline(Pipeline pipeline, Channel read_cha, Channel write_cha, bool wait_here, int* pgid, ShellState* shstate);


DECLARE_MAYBE(StrPair, MaybeStrPair)

MaybeStrPair parse_assignment(char* token) {
    // TODO: ideally shouldn't mutate maybe
    if (token == NULL) return (MaybeStrPair){.some = false};
    char* eq = strchr(token, '=');
    if (eq == NULL || eq == token) return (MaybeStrPair){.some = false};
    *eq = '\0';
    if (!is_identifier(token)) exit(42);
    return (MaybeStrPair){.some = true, .value = (StrPair){.key = token, .val = eq+1}};
}


void source_script(char* fname, ShellState* shstate) {
    // TODO: add stdin/out (to other builtins too, actually)
    FILE* fp = fopen(fname, "r");        // TODO: checks
    if (fp == 0) exit(1);

    char* line = NULL;
    size_t len = 0;

    while (getline(&line, &len, fp) != -1) {
		line[strcspn(line, "\r\n")] = '\0';
        execute_string_line(line, shstate);
    }
    free(line);
    fclose(fp);

}


bool handle_builtin(ExecutableCommand cmd, ShellState* shstate) {
    MaybeStrPair m_assignment;
    assert(cmd.args[0] != NULL); 
    // TODO: handle id=value assignments
    
    m_assignment = parse_assignment(cmd.args[0]);
    if (m_assignment.some) {
        set_map_pair(&shstate->shell_vars, m_assignment.value);
        return true;
    }

    if (strcmp(cmd.args[0], "exit") == 0) {
        exit(0);
    } else if (strcmp(cmd.args[0], "cd") == 0) {
        assert(cmd.args[1] != NULL);
        chdir(cmd.args[1]);
        return true;
    } else if (strcmp(cmd.args[0], "alias") == 0) {
        // TODO: if no arg1 -> print alias list
        m_assignment = parse_assignment(cmd.args[1]);
        assert(m_assignment.some);
        set_map_pair(&shstate->aliases, m_assignment.value); 
        return true;
    } else if (strcmp(cmd.args[0], "source") == 0) {
        assert(cmd.args[1] != NULL);
        source_script(cmd.args[1], shstate);
        return true;
    } else if (strcmp(cmd.args[0], "export") == 0) {
        m_assignment = parse_assignment(cmd.args[1]);
        assert(m_assignment.some);
        setenv(m_assignment.value.key, m_assignment.value.val, 1);
        return true;
    } else if (strcmp(cmd.args[0], "unalias") == 0) { 
        assert(cmd.args[1] != NULL);
        unset_map_key(&shstate->aliases, cmd.args[1]);
        return true;
    }  else if (strcmp(cmd.args[0], "unset") == 0) {
        assert(cmd.args[1] != NULL);
        unset_map_key(&shstate->shell_vars, cmd.args[1]);
        return true;
    }
    return false;
}

void execute_cmd(ExecutableCommand cmd, int* pgid) {

    pid_t pid = fork();

    if (pid == 0) {

        if (*pgid == -1) {
            setpgid(0, 0);
        } else {
            setpgid(0, *pgid);
        }

        dup2(cmd.read_cha.fd, STDIN_FILENO);
        dup2(cmd.write_cha.fd, STDOUT_FILENO);

        if (cmd.read_cha.child_proc_closable) close(cmd.read_cha.fd);
        if (cmd.write_cha.child_proc_closable) close(cmd.write_cha.fd);

        for (int i = 0; i < cmd.redirects.count; i++) {
            FdRedirect fd_rd = *get_from_FdRedirectVec(&cmd.redirects, i);
            dup2(fd_rd.to, fd_rd.redirected);
        }

        for (int j = 0; j < cmd.child_open_fds.count; j++) {
            int fd = *get_from_IntVec(&cmd.child_open_fds, j);
            close(fd);
        }

        signal(SIGINT,  SIG_DFL);
        signal(SIGQUIT, SIG_DFL);
        signal(SIGTSTP, SIG_DFL);
        signal(SIGTTIN, SIG_DFL);
        signal(SIGTTOU, SIG_DFL);

        execvp(cmd.args[0], cmd.args);
        exit(67);
    }

    if (*pgid == -1) {
        setpgid(pid, pid);
        *pgid = pid;
    } else {
        setpgid(pid, *pgid);
    }
}


ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* shstate) {
    StrVec arg_vec = make_StrVec(256);
    FdRedirectVec fd_redirs = make_FdRedirectVec(256);
    IntVec parent_open_fds = make_IntVec(256);
    IntVec child_open_fds = make_IntVec(256);

    int ps_pipe_fds[2];

    for (int i = 0; i < pl_cmd.args.count; i++) {
        Arg arg = *get_from_ArgVec(&pl_cmd.args, i);
        switch (arg.kind) {
            case ARG_LIT:
                push_to_StrVec(&arg_vec, arg.as.lit_str);
                break;
            case ARG_PS_IN:
                pipe(ps_pipe_fds);
                Channel write_cha = {.fd = ps_pipe_fds[1], .parent_proc_closable = true, .child_proc_closable = true};
                execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_CHA, write_cha, false, pgid, shstate);
                //close(ps_pipe_fds[1]);
                char pname[64];
                snprintf(pname, sizeof(pname), "/dev/fd/%d", ps_pipe_fds[0]);
                push_to_IntVec(&parent_open_fds, ps_pipe_fds[0]);
                push_to_StrVec(&arg_vec, strdup(pname));
                break;
            case ARG_PS_OUT:
                // TODO: do waiting correctly
                pipe(ps_pipe_fds);

                // TODO: don't do fcntl?
                fcntl(ps_pipe_fds[1], F_SETFD, FD_CLOEXEC);
                Channel read_cha = {.fd = ps_pipe_fds[0], .parent_proc_closable = true, .child_proc_closable = true};
                execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, read_cha, STDOUT_CHA, false, pgid, shstate);
                //close(ps_pipe_fds[0]);
                fcntl(ps_pipe_fds[1], F_SETFD, 0);
                char name[64];
                snprintf(name, sizeof(name), "/dev/fd/%d", ps_pipe_fds[1]);
                push_to_IntVec(&parent_open_fds, ps_pipe_fds[1]);
                push_to_StrVec(&arg_vec, strdup(name));
                break;
            default:
                exit(67);
        }
    }

    for (int j = 0; j < pl_cmd.redirects.count; j++) {
        Redirect redir = *get_from_RedirectVec(&pl_cmd.redirects, j);
        FdRedirect fd_redir;
        int fd;
        switch (redir.kind) {
            case REDIR_READ:  
                fd = open(redir.as.filename, O_RDONLY, 0644);
                push_to_IntVec(&parent_open_fds, fd);
                push_to_IntVec(&child_open_fds, fd);
                fd_redir = (FdRedirect){.to = fd, .redirected = STDIN_FILENO};
                break;
            case REDIR_WRITE_APPEND:
                fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
                push_to_IntVec(&parent_open_fds, fd);
                push_to_IntVec(&child_open_fds, fd);
                fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO};
                break;
            case REDIR_WRITE_TRUNC:
                fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
                push_to_IntVec(&parent_open_fds, fd);
                push_to_IntVec(&child_open_fds, fd);
                fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO};
                break;
            default:
                exit(67);
        }
        push_to_FdRedirectVec(&fd_redirs, fd_redir);
}

    // TODO: I think dealiasing has certain preconditions, like arg0 not being expanded before(?).
    char** args = malloc(sizeof(char*) * (arg_vec.count + 1));
    for (int i = 0; i < arg_vec.count; i++) {
        if (i == 0) {
            char* arg = *get_from_StrVec(&arg_vec, 0);
            MaybeStr m_dealias = get_map_value(&shstate->aliases, arg);
            if (m_dealias.some) {
                args[0] = m_dealias.value;
            } else {
                args[0] = arg;
            }
        } else {
            args[i] = *get_from_StrVec(&arg_vec, i);
        }
    }
    args[arg_vec.count] = NULL;
    
    return (ExecutableCommand){.args = args, .redirects = fd_redirs, .parent_open_fds = parent_open_fds, .child_open_fds = child_open_fds};
}


void execute_command_pipeline(Pipeline pipeline, Channel read_cha, Channel write_cha, bool wait_here, int* pgid, ShellState* shstate) {

    Channel trunk_read_cha = read_cha;

    for (int i = 0; i < pipeline.count; i++) {
        PlCommand cmd = *get_from_Pipeline(&pipeline, i);
        ExecutableCommand exec_cmd = process_pl_command(cmd, pgid, shstate);

        exec_cmd.read_cha = trunk_read_cha;

        exec_cmd.write_cha = write_cha;
        
        if (cmd.conn == CONN_PIPE) {
            int pipe_fds[2];
            pipe(pipe_fds);
            exec_cmd.write_cha = (Channel){.fd = pipe_fds[1], .parent_proc_closable = true, .child_proc_closable = true};
            trunk_read_cha = (Channel){.fd = pipe_fds[0], .parent_proc_closable = true, .child_proc_closable = true};
        }

        if (!handle_builtin(exec_cmd, shstate)) {
            execute_cmd(exec_cmd, pgid);
        }

        if (exec_cmd.read_cha.parent_proc_closable) close(exec_cmd.read_cha.fd);
        if (exec_cmd.write_cha.parent_proc_closable) close(exec_cmd.write_cha.fd);

        for (int j = 0; j < exec_cmd.parent_open_fds.count; j++) {
            int fd = *get_from_IntVec(&exec_cmd.parent_open_fds, j);
            close(fd);
        }
    }

    if (wait_here) {
        tcsetpgrp(STDIN_FILENO, *pgid); 
        while (waitpid(-*pgid, NULL, 0) > 0); 
        tcsetpgrp(STDIN_FILENO, getpgrp());
    }
}

void execute_pipeline_tree(Pipeline root_pl, ShellState* shstate, int stdin_fd, int stdout_fd) {
    int pgid = -1;
    Channel read_cha = {.fd = stdin_fd, .parent_proc_closable = false, .child_proc_closable = false};
    Channel write_cha = {.fd = stdout_fd, .parent_proc_closable = false, .child_proc_closable = false};
    execute_command_pipeline(root_pl, read_cha, write_cha, true, &pgid, shstate); 
}