-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.d
57 lines (54 loc) · 1.29 KB
/
shell.d
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
extern (C):
import common;
void main()
{
while(true)
{
prompt:
printf("> ");
char[12] cmdline;
for (int i = 0;; i++)
{
char ch = cast(char) getchar();
putchar(ch);
if (i == cmdline.sizeof - 1)
{
printf("command line too long\n");
goto prompt;
}
else if (ch == '\r')
{
printf("\n");
cmdline[i] = '\0';
break;
}
else
{
cmdline[i] = ch;
}
}
if (strcmp(cmdline.ptr, "hello".ptr) == 0)
{
printf("Hello, World from shell!\n");
}
else if (strcmp(cmdline.ptr, "exit".ptr) == 0)
{
exit();
}
else if (strcmp(cmdline.ptr, "readfile".ptr) == 0)
{
char[128] buf;
int len = readfile("hello.txt".ptr, buf.ptr, buf.length);
buf[len] = '\0';
printf("%s\n", buf.ptr);
}
else if (strcmp(cmdline.ptr, "writefile".ptr) == 0)
{
writefile("hello.txt".ptr, "Hello from shell!\n".ptr, 19);
}
else
{
printf("unknown command: %s\n", cmdline.ptr);
}
}
}