aboutsummaryrefslogtreecommitdiffstats
path: root/bcsh.c
blob: 08f7ac7d2377745c74941daca1e7dea70fcfd605 (plain) (blame)
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
#include "cmd.h"

extern int yyparse();
char hostname[100];
char *username;
char pwd[100];

int main(int argc, char **argv) {
    // 获取当前登陆的用户名、系统主机名、当前工作路径
    gethostname(hostname, sizeof(hostname));
    getcwd(pwd, sizeof(pwd));
    username = getpwuid(getuid())->pw_name;
    showPrompt();
    while (true) {
        yyparse();
    }
    return 0;
}

void showPrompt() {
    printf("\033[01;32m%s@%s\033[00m:\033[33m%s\033[00m$ ", username, hostname,
           pwd);
    // 强制刷新缓冲区
    fflush(stdout);
}

Command *newcmd() {
    Command *cmd = (Command *)malloc(sizeof(Command));
    cmd->argc = 0;
    cmd->args = NULL;
    cmd->type = CMD_NORMAL;
    cmd->left = NULL;
    cmd->right = NULL;
    for (int i = 0; i < 3; i++) {
        cmd->redirectFile[i] = NULL;
        cmd->redirectFD[i] = i;
    }
    cmd->background = false;
    return cmd;
}

int runcmd(Command *cmd) {
    int status;
    if (cmd == NULL) {
        fprintf(stderr, "cmd is NULL\n");
        return -1;
    }

    if (cmd->type == CMD_NORMAL) {
        pid_t pid;
        if (!strcmp(cmd->args[0], "exit")) {
            printf("Good Bye, %s!\n", username);
            exit(0);
        } else if (!strcmp(cmd->args[0], "cd")) {
            if (cmd->argc == 1) {
                chdir(getenv("HOME"));
            } else {
                chdir(cmd->args[1]);
            }
            printf("\r");
            getcwd(pwd, sizeof(pwd));
        } else {
            if ((pid = fork()) == 0) {
                // consider the redirection now
                // it's just a part of normal command
                // but not in other types
                for (int i = 0; i < 3; i++) {
                    if (cmd->redirectFD[i] != i) {
                        close(i);
                        dup2(cmd->redirectFD[i], i);
                    } else if (cmd->redirectFile[i] != NULL) {
                        int fd =
                            open(cmd->redirectFile[i], O_RDWR | O_CREAT, 0666);
                        if (fd < 0) {
                            fprintf(stderr, "open file %s failed\n",
                                    cmd->redirectFile[i]);
                            exit(-1);
                        }
                        close(i);
                        dup2(fd, i);
                        close(fd);
                    }
                }
                if (cmd->background) {
                    setpgid(0, 0);
                    // no output, redirect 1 and 2 to /dev/null
                    int fd = open("/dev/null", O_RDWR);
                    dup2(fd, 1);
                    dup2(fd, 2);
                    close(fd);
                }
                exit(execvp(cmd->args[0], cmd->args));
            } else {
                if (!cmd->background) {
                    waitpid(pid, &status, 0);
                    if (!WIFEXITED(status)) {
                        // when here, the child process exited abnormally
                        fprintf(stderr, "Process %d exited with status %d\n",
                                pid, WEXITSTATUS(status));
                    }
                }
            }
            // return the exit status of the child process
            return WEXITSTATUS(status);
        }
    } else if (cmd->type == CMD_PIPE) {
        // create a pipe file and gets two file descriptors
        // fd[0] is the read end, while fd[1] the write end
        int fd[2];
        pid_t left, right;
        pipe(fd);
        if ((left = fork()) == 0) {
            /*
             * close the read end of the pipe, so that the child process
             * can only write to it; then redirect the standard output
             * to the write end of the pipe, and close the write end
             * itself as a file descriptor.
             */
            close(fd[0]);
            dup2(fd[1], 1);
            close(fd[1]);
            exit(runcmd(cmd->left));
        }
        if ((right = fork()) == 0) {
            close(fd[1]);
            dup2(fd[0], 0);
            close(fd[0]);
            exit(runcmd(cmd->right));
        }
        // close the pipe, because the parent process doesn't need it
        // and wait for the two child processes to exit
        close(fd[0]);
        close(fd[1]);
        waitpid(left, NULL, 0);
        waitpid(right, NULL, 0);
    } else if (cmd->type == CMD_AND) {
        if (fork() == 0) {
            exit(runcmd(cmd->left));
        } else {
            waitpid(-1, &status, 0);
            if (WIFEXITED(status) && WEXITSTATUS(status) == 0 && fork() == 0) {
                status = runcmd(cmd->right);
            }
            wait(NULL);
        }
    } else if (cmd->type == CMD_OR) {
        if (fork() == 0) {
            exit(runcmd(cmd->left));
        } else {
            waitpid(-1, &status, 0);
            if (WIFEXITED(status) && WEXITSTATUS(status) != 0 && fork() == 0) {
                status = runcmd(cmd->right);
            }
            wait(NULL);
        }
    }
    freecmd(cmd);
    return status;
}

void freecmd(Command *cmd) {
    if (cmd == NULL) {
        return;
    }
    if (cmd->left != NULL) {
        freecmd(cmd->left);
    }
    if (cmd->right != NULL) {
        freecmd(cmd->right);
    }
    int i;
    for (i = 0; i < cmd->argc; i++) {
        free(cmd->args[i]);
    }
    free(cmd->args);
    for (i = 0; i < 3; i++) {
        if (cmd->redirectFile[i] != NULL) {
            free(cmd->redirectFile[i]);
        }
    }
    free(cmd);
}