summaryrefslogtreecommitdiff
path: root/Userland/sh.cpp
blob: 30d9526b7f2afa3e585677dabea505dedd43fedf (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
#include <LibC/stdio.h>
#include <LibC/unistd.h>
#include <LibC/process.h>
#include <LibC/errno.h>
#include <LibC/string.h>
#include <LibC/stdlib.h>

char* g_cwd = nullptr;

static void prompt()
{
    if (getuid() == 0)
        printf("# ");
    else
        printf("$ ");
}

static int sh_pwd(int, const char**)
{
    printf("cwd: %s\n", g_cwd);
}

static int sh_cd(int argc, const char** argv)
{
    if (argc == 1) {
        printf("usage: cd <path>\n");
        return 0;
    }

    char pathbuf[128];
    if (argv[1][1] == '/')
        memcpy(pathbuf, argv[1], strlen(argv[1]));
    else
        sprintf(pathbuf, "%s/%s", g_cwd, argv[1]);
    struct stat st;
    int rc = lstat(pathbuf, &st);
    if (rc < 0) {
        printf("lstat(%s) failed: %s\n", pathbuf, strerror(errno));
        return 1;
    }
    if (!S_ISDIR(st.st_mode)) {
        printf("Not a directory: %s\n", pathbuf);
        return 1;
    }
    rc = chdir(pathbuf);
    if (rc < 0) {
        printf("chdir(%s) failed: %s\n", pathbuf, strerror(errno));
        return 1;
    }
    memcpy(g_cwd, pathbuf, strlen(pathbuf));
    return 0;
}

static bool handle_builtin(int argc, const char** argv, int& retval)
{
    if (argc == 0)
        return false;
    if (!strcmp(argv[0], "cd")) {
        retval = sh_cd(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "pwd")) {
        retval = sh_pwd(argc, argv);
        return true;
    }
    return false;
}

static int runcmd(char* cmd)
{
    if (cmd[0] == 0)
        return 0;
    char buf[128];
    memcpy(buf, cmd, 128);

    const char* argv[32];
    size_t argi = 1;
    argv[0] = &buf[0];
    size_t buflen = strlen(buf);
    for (size_t i = 0; i < buflen; ++i) {
        if (buf[i] == ' ') {
            buf[i] = '\0';
            argv[argi++] = &buf[i + 1];
        }
    }
    argv[argi + 1] = nullptr;

    int retval = 0;
    if (handle_builtin(argi, argv, retval)) {
        return 0;
    }

    const char* search_path = "/bin";

    char pathbuf[128];
    sprintf(pathbuf, "%s/%s", search_path, argv[0]);
    int ret = spawn(pathbuf, argv);
    if (ret == -1) {
        printf("spawn failed: %s (%s)\n", cmd, strerror(errno));
        return 1;
    }
    // FIXME: waitpid should give us the spawned process's exit status
    waitpid(ret);
    return retval;
}

int main(int, char**)
{
    char linebuf[128];
    int linedx = 0;
    linebuf[0] = '\0';

    int fd = open("/dev/keyboard");
    if (fd == -1) {
        printf("failed to open /dev/keyboard :(\n");
        return 1;
    }
    g_cwd = (char*)malloc(1024);
    g_cwd[0] = '/';
    g_cwd[1] = '\0';
    prompt();
    for (;;) {
        char keybuf[16];
        ssize_t nread = read(fd, keybuf, sizeof(keybuf));
        if (nread < 0) {
            printf("failed to read :(\n");
            return 2;
        }
        if (nread > 2)
            printf("read %u bytes\n", nread);
        if (nread > (ssize_t)sizeof(keybuf)) {
            printf("read() overran the buffer i gave it!\n");
            return 3;
        }
        for (ssize_t i = 0; i < nread; ++i) {
            putchar(keybuf[i]);
            if (keybuf[i] != '\n') {
                linebuf[linedx++] = keybuf[i];
                linebuf[linedx] = '\0';
            } else {
                runcmd(linebuf);
                linebuf[0] = '\0';
                linedx = 0;
                prompt();
            }
        }
    }
    return 0;
}