summaryrefslogtreecommitdiffstats
path: root/03_exercise/srv/shell.c
blob: 792a26c5b9f64bbdca80b6cce47f468a4383644d (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <wait.h>
#include <errno.h>

#include "array.h"
#include "process.h"
#include "prompt_utils.h"
#include "builtins.h"

#define BUF_SIZE 2048

process *processes;

int write_all(int sock, char *buffer, int size) {
    int nwrite, sent = 0;

    while (sent < size) {
        if ((nwrite = write(sock, buffer + sent, size - sent)) < 0) {
            perror("Write");
            exit(1);
        }

        sent += nwrite;
    }

    return sent;
}

void receive_file(int sock, char *path, int size) {
    char buffer[512];
    int  file_fd;
    int  nread, received;

    if ((file_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { perror("Open"); }

    while (true) {
        received = 0;

        /* Read all data */
        while (received < size) {
            if ((nread = read(sock, buffer + received, size - received)) < 0) {
                perror("Read");
                //pthread_exit(NULL);
                exit(1);
            }

            received += nread;
        }

        if (strncmp(buffer, "end", 4) == 0) { break; }

        write_all(file_fd, buffer, strlen(buffer) + 1);
    }
}

const char *const get_current_dir_name();

void signal_handler(int signal) {
    if (signal == SIGINT) {
        for (size_t i = 0; i < arrayLen(processes); ++i) {
            pid_t pid = processes[i].pid;
            if (pid != 0) {
                kill(pid, SIGINT);
            }
        }
    }
}

int shell(int in_fd) {
    setvbuf(stderr, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);

    dup2(in_fd, 1);
    dup2(in_fd, 2);

    // I don't think the shell should exit on SIG_TERM
    if (signal(SIGINT, SIG_IGN) == SIG_ERR) {
        perror("Couldn't ignore sigterm");
        exit(errno);
    }

    printf("Welcome! Available built-ins are:\n"
           "cd: `cd <path>` - if no path is given, return to the current dir\n"
           "wait: `wait pid1 ... pidN` - wait on the processes and report their exit conditions\n"
           "fg: `fg pid` - pulls a process from the background back in the foreground\n"
           "\n"
           "You can put processes in the background using `&`\n"
           "And with `|` you can pipe the output from one process to the input of another\n"
    );

    char const *const original_wd = get_current_dir_name();

    bool done = false;
    while (!done) {
        char      line[BUF_SIZE];
        __ssize_t length;

        if ((length = read(in_fd, line, BUF_SIZE)) < 0) {
            fprintf(stderr, "Failed to read from STDIN");
            fflush(stderr);
            exit(-1);
        }

        if (strspn(line, " \n\t") == strlen(line)) {
            // skip empty lines - empty being just spaces or tabs
            continue;
        }
        line[length - 1] = '\0'; // cut the line feed

        processes = NULL;
        parse_line(line, &processes);

        if (strcmp(processes[0].argv[0], "cd") == 0) {
            if (arrayLen(processes) != 1) {
                perror("Can't chain cd with other processes");
            }
            int ret;
            switch (arrayLen(processes[0].argv)) {
                case 3:
                    ret = chdir(processes[0].argv[1]);
                    break;
                case 2:
                    ret = chdir(original_wd);
                    break;
                default:
                    fprintf(stderr, "usage: cd <path>\n");
                    fflush(stderr);
                    ret = -1;
            }

            if (ret)
                printf("[%i] ", ret);
            else
                printf("%s\n", get_current_dir_name());

        } else if (strcmp(processes[0].argv[0], "exit") == 0) {
            done = true;
        } else if (strcmp(processes[0].argv[0], "wait") == 0) {
            builtin_wait(processes[0], false);
        } else if (strcmp(processes[0].argv[0], "fg") == 0) {
            // same behaviour as wait, just bind to shell again (i.e. terminate process on ctrl-c)
            builtin_wait(processes[0], true);
        } else {
            if (arrayLen(processes) != 0 && processes[arrayLen(processes) - 1].blocking) {
                signal(SIGINT, signal_handler);
            }
            for (size_t i = 0; i < arrayLen(processes); ++i) {
                /*int ret = */exec_command(processes[i]);
                /*if (ret)
                    printf("[%i] ", ret);*/
            }
            signal(SIGINT, SIG_IGN);
        }

        free_processes(&processes);
    }

    printf("Disconnecting...");

    free((void *) original_wd);

    return 0;
}