aboutsummaryrefslogtreecommitdiff
path: root/src/eval.cc
blob: 6d16cf74042e3a485f34236c17053e431162835f (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

#include "eval.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <iostream>
#include <vector>
#include <string>

#include "magic_enum/magic_enum.hpp"

#include "error.h"
#include "lexer/lexer_class.h"
#include "lexer/ast.h"
#include "lexer/token.h"
#include "lexer/enum_type.h"
#include "lexer/symbol_property.h"
#include "handler.h"

using namespace std;

extern char **environ;

namespace ov4
{

int eval_exe(const string &s, bool is_aync)
{
    loggerln("eval_exe: receive {}", s);

    char cmdline[MAXLINE];
    strcpy(cmdline, s.c_str());
    char arg[MAXARGS][MAXLINE];
    char *argv[MAXARGS];
    for (int i = 0 ; i < MAXARGS; i++)
    {
        argv[i] = &arg[i][0];
    }
    parseline(cmdline, argv);

    if (argv == nullptr || argv[0] == nullptr) return 0;

    if (exe_bultin_command(argv)) return 0;

    // block_io signals can stop shell
    // when child is at foreground, parent shell is at background
    sigprocmask(SIG_BLOCK, &block_io, nullptr);

    sigset_t prev;
    block_all(&prev);

    string cmd = find_cmd(argv[0]);
    const char* cmd_c = cmd.c_str();

    shell_pgid = getpgid(0);
    pid_t pid = fork();
    
    if (pid == 0) // child
    {
        setpgid(0, 0);
        if (!is_aync) // foreground
            tcsetpgrp(tty_fd, getpgrp());

        sigprocmask(SIG_SETMASK, &prev, nullptr);

        // program inside execve may use SIGTTIN/SIGTTOU
        // so just restore in child
        sigprocmask(SIG_UNBLOCK, &block_io, nullptr);
        execve(cmd_c, argv, environ);

        // execve: if success, never returns

        wrap_eliminator(cmdline);
        cout << cmdline << flush;
        switch (errno)
        {
            case ENOTDIR:
                println(": A component of the path prefix is not a directory.");
                exit(errno);
                break;
            
            case ENAMETOOLONG:
                println(": A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.");
                exit(errno);
                break;
            
            case ENOENT:
                println(": The new process file does not exist.");
                exit(errno);
                break;

            case EACCES:
                println(": The new process file mode denies execute permission.");
                exit(errno);
                break;

            default:
                println(": Error, code: {}", errno);
                exit(errno);
        }
    }

    // parent

    exit_required_pid = pid;

    if (!is_aync)  // foreground
    {   
        addjob(pid, FG, cmdline); 
        sigset_t prev_with_sigchld_blocked = prev;
        sigaddset(&prev_with_sigchld_blocked, SIGCHLD);
        sigprocmask(SIG_SETMASK, &prev_with_sigchld_blocked, NULL);
        waitfg(pid);
        sigprocmask(SIG_SETMASK, &prev, NULL);

        return exit_code;
    }
    else // background
    {
        addjob(pid, BG, cmdline);
        sigprocmask(SIG_SETMASK, &prev, NULL);
        cout << "[" << pid2jid(pid) << "] (" << pid << ") " << cmdline;

        // POSIX:
        // async command returns 0 immediately
        // TODO: recurring async 
        return 0;
    }



    return 0;
}

int eval_subshell(size_t i, const T_lexer &lexer_instance)
{
    println("eval_tree_cd: processing subshell");

    // TODO: implement async
    bool is_aync = false;

    char cmdline[] = "subshell";

    // block_io signals can stop shell
    // when child is at foreground, parent shell is at background
    sigprocmask(SIG_BLOCK, &block_io, nullptr);

    sigset_t prev;
    block_all(&prev);

    shell_pgid = getpgid(0);
    pid_t pid = fork();
    
    if (pid == 0) // child
    {
        setpgid(0, 0);
        if (!is_aync) // foreground
            tcsetpgrp(tty_fd, getpgrp());

        sigprocmask(SIG_SETMASK, &prev, nullptr);

        // program inside execve may use SIGTTIN/SIGTTOU
        // so just restore in child
        sigprocmask(SIG_UNBLOCK, &block_io, nullptr);
        eval_tree_cd(i, lexer_instance, true);
        exit(0);
    }

    // parent

    exit_required_pid = pid;

    if (!is_aync)  // foreground
    {   
        addjob(pid, FG, cmdline); 
        sigset_t prev_with_sigchld_blocked = prev;
        sigaddset(&prev_with_sigchld_blocked, SIGCHLD);
        sigprocmask(SIG_SETMASK, &prev_with_sigchld_blocked, NULL);
        waitfg(pid);
        sigprocmask(SIG_SETMASK, &prev, NULL);

        return exit_code;
    }
    else // background
    {
        addjob(pid, BG, cmdline);
        sigprocmask(SIG_SETMASK, &prev, NULL);
        cout << "[" << pid2jid(pid) << "] (" << pid << ") " << cmdline;

        // POSIX:
        // async command returns 0 immediately
        // TODO: recurring async 
        return 0;
    }
}

int eval_tree_cd(size_t i, const T_lexer &lexer_instance, bool inside_subshell)
{
    // TODO: async

    int ret = -1;

    println("eval_tree_cd: processing {} subshell {}", i, (lexer_instance.ast[i].subshell ? "TRUE" : "FALSE"));

    // if it's a subshell, and current AST's subshell status is not subshell
    // (subshell should be forkerd already, then set inside_subshell = true)
    // fork itself
    if (lexer_instance.ast[i].subshell && !inside_subshell)
    {
        eval_subshell(i, lexer_instance);
        return 0;
    }

    if (lexer_instance.ast[i].token_type == TEXT)
    {
        ret = eval_exe(lexer_instance.ast[i].command_text, false);
        return ret;
    }

    if (lexer_instance.ast[i].left != string::npos)
    {
        ret = eval_tree_cd(lexer_instance.ast[i].left, lexer_instance, false);
    }

    if (lexer_instance.ast[i].right != string::npos)
    {
        if (lexer_instance.ast[i].token_type == LOGIC_AND && ret != 0) return ret;
        if (lexer_instance.ast[i].token_type == LOGIC_OR && ret == 0) return ret;
        
        ret = eval_tree_cd(lexer_instance.ast[i].right, lexer_instance, false);
    }
    return ret;
}
    
/* 
 * eval - Evaluate the command line that the user has just typed in
 * 
 * If the user has requested a built-in command (quit, jobs, bg or fg)
 * then execute it immediately. Otherwise, fork a child process and
 * run the job in the context of the child. If the job is running in
 * the foreground, wait for it to terminate and then return.  Note:
 * each child process must have a unique process group ID so that our
 * background children don't receive SIGINT (SIGTSTP) from the kernel
 * when we type ctrl-c (ctrl-z) at the keyboard.  
*/
void eval(char *cmdline) 
{
    string s = cmdline;
    T_lexer lexer_instance;

    try
    {
        lexer_instance.tokenizer(s);
        lexer_instance.parse(0, lexer_instance.token.size(), lexer_instance.alloc_ast());
    }
    catch(const T_error &e)
    {
        cerr << "Error: " << magic_enum::enum_name(e.code) << endl;
        cerr << "what: " << e.what() << endl;
        return;
    }

    eval_tree_cd(0, lexer_instance, false);
    return;
}

}