summaryrefslogtreecommitdiff
path: root/src/user/app/shell/shell.c
blob: 290102e7020b7d09f347bd086eaf8dbae3d165d7 (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
#include "builtins.h"
#include "shell.h"
#include <camellia/flags.h>
#include <camellia/syscalls.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <user/lib/fs/misc.h>
#include <x86intrin.h>

int main();

static void execp(char **argv) {
	if (!argv || !*argv) return;
	if (argv[0][0] == '/') {
		execv(argv[0], argv);
		return;
	}

	size_t cmdlen = strlen(argv[0]);
	char *s = malloc(cmdlen);
	if (!s) {
		eprintf("out of memory.");
		exit(1);
	}
	memcpy(s, "/bin/", 5);
	memcpy(s + 5, argv[0], cmdlen + 1);
	argv[0] = s;

	execv(s, argv);
	free(s);
}

void run_args(int argc, char **argv, struct redir *redir) {
	if (!*argv) return;

	/* "special" commands that can't be handled in a subprocess */
	if (!strcmp(argv[0], "mount")) {
		if (argc < 3) {
			eprintf("not enough arguments");
			return;
		}
		MOUNT_AT(argv[1]) {
			run_args(argc - 2, argv + 2, redir);
			exit(1);
		}
		return;
	} else if (!strcmp(argv[0], "cd")) {
		if (chdir(argc > 1 ? argv[1] : "/") < 0)
			perror("cd");
		return;
	} else if (!strcmp(argv[0], "time")) {
		uint64_t time = __rdtsc();
		uint64_t div = 3000;
		run_args(argc - 1, argv + 1, redir);
		time = __rdtsc() - time;
		printf("%u ns (assuming 3GHz)\n", time / div);
		return;
	} else if (!strcmp(argv[0], "exit")) {
		exit(0);
	}

	if (fork()) {
		_syscall_await();
		return;
	}

	if (redir && redir->stdout) {
		FILE *f = fopen(redir->stdout, redir->append ? "a" : "w");
		if (!f) {
			eprintf("couldn't open %s for redirection", redir->stdout);
			exit(1);
		}

		/* a workaround for file offsets not being preserved across exec()s.
		 * TODO document that weird behaviour of exec() */

		handle_t p[2];
		if (_syscall_pipe(p, 0) < 0) {
			eprintf("couldn't create redirection pipe", redir->stdout);
			exit(1);
		}
		if (!_syscall_fork(FORK_NOREAP, NULL)) {
			/* the child forwards data from the pipe to the file */
			const size_t buflen = 512;
			char *buf = malloc(buflen);
			if (!buf) {
				eprintf("when redirecting: malloc failure");
				exit(1);
			}
			close(p[1]);
			for (;;) {
				long len = _syscall_read(p[0], buf, buflen, 0);
				if (len < 0) exit(0);
				fwrite(buf, 1, len, f);
				if (ferror(f)) exit(0);
			}
		}

		fclose(f);
		close(p[0]);
		if (_syscall_dup(p[1], 1, 0) < 0) {
			eprintf("dup failure");
			exit(1);
		}
	}

	for (struct builtin *iter = builtins; iter->name; iter++) {
		if (!strcmp(argv[0], iter->name)) {
			iter->fn(argc, argv);
			exit(0);
		}
	}

	execp(argv);
	if (errno == EINVAL) {
		eprintf("%s isn't a valid executable\n", argv[0]);
	} else {
		eprintf("unknown command: %s\n", argv[0]);
	}
	exit(0); /* kills the subprocess */
}

static void run(char *cmd) {
	#define ARGV_MAX 16
	char *argv[ARGV_MAX];
	struct redir redir;

	int argc = parse(cmd, argv, ARGV_MAX, &redir);
	if (argc < 0) {
		eprintf("error parsing command");
		return;
	}

	run_args(argc, argv, &redir);
}


int main(int argc, char **argv) {
	static char buf[256];
	FILE *f = stdin;

	if (argc > 1) {
		f = fopen(argv[1], "r");
		if (!f) {
			eprintf("couldn't open %s\n", argv[1]);
			return 1;
		}
	}

	for (;;) {
		if (f == stdin)
			printf("%s $ ", getcwd(buf, sizeof buf));
		if (!fgets(buf, 256, f))
			return 0;
		run(buf);
	}
}