summaryrefslogtreecommitdiff
path: root/src/user/test/main.c
blob: 81753bda9a35d41787feed6293277c7aa1f96db8 (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
#include <tce/syscall.h>
#include <stdlib.h>
#include <stdio.h>

int threads = 0;

void thread_cascade(void* d) {
	int n = (int)d;

	threads ++;

	if (d == 0) {
		//printk("{#} 0 cascade element started => end\n");
		printk("*");
	} else {
		if (n < 0) {
			//printk("{#} - cascade element started\n");
			printk("-");
			n = 0 - n;
		} else {
			//printk("{#} + cascade element started\n");
			printk("+");
		}
		//printk("{#} FORK + ...\n");
		printk(">");
		thread_new(thread_cascade, (void*)(n - 1));
		//printk("{#} FORK - ...\n");
		printk("<");
		thread_new(thread_cascade, (void*)(1 - n));
		//printk("{#} Thread cascade element finished.\n");
		printk(".");
	}

	threads--;
}

void list_dir(FILE f, int lv) {
	char buf[256];
	int i = 0, k;
	int r;
	file_info info;
	
	while ((r = read(f, i, 256, buf)) > 0) {
		if (strcmp(buf, ".") == 0 || strcmp(buf, "..") == 0) {
			i++;
			continue;
		}
		for (k = 0; k < lv; k++) printk("  ");
		printk(buf);
		stat_relative(f, buf, &info);
		if (info.type & FT_DIR) printk("/");
		printk("    \t");

		if (info.type & FT_FILE) printk("file ");
		if (info.type & FT_DIR) printk("dir ");
		if (info.type & FT_SYMLINK) printk("symlink ");
		if (info.type & FT_DEV) printk("dev ");
		if (info.type & FT_TERMINAL) printk("term ");

		if (info.type & FT_DIR) {
			printk("  \t");
			FILE ff = open_relative(f, buf, 0);
			if (ff <= 0) {
				printk("error: "); printk_int(ff); printk("\n"); 
			} else {
				printk("fd: "); printk_int(ff); printk("\n");
				list_dir(ff, lv+1);
				close(ff);
			}
		} else {
			printk("\n");
		}
		i++;
	}
}

void fprint(FILE f, char *s) {
	write(f, 0, strlen(s), s);
}

int main(char** args) {
	char**a;
	if (args != 0) {
		printk("args");
		for (a = args; *a != 0; a++) {
			printk(" - ");
			printk(*a);
		}
		printk("\n");
	}

	printk("(test app) malloc(42) = ");
	printk_hex((uint32_t)malloc(42));
	printk("\n");

	printk(" -> Creating thread cascade (total 2**4 = 16 threads)\n");
	thread_new(thread_cascade, (void*)4);

	printk(" -> Main thread now sleeping...\n");
	while (1) {
		thread_sleep(100);
		if (threads == 0) break;
	}
	printk("\n -> Ok, let's try something else.\n");

	FILE f = open("/", 0);
	if (f <= 0) {
		printk(" -> Could not open '/', error #");
		printk_int(f);
		printk("...\n");
	} else {
		printk("Now enumerating '/' (fd "); printk_int(f); printk(") :\n");
		list_dir(f, 1);
		close(f);
	}

	f = open("/.ui/klog", 0);
	if (f <= 0) {
		printk(" -> Error #"); printk_int(f);  printk(" - too bad. Exiting.\n");
	} else {
		fprint(f, " -> Now reading && writing from/to virtual terminal '/.ui/klog'\n");
		while (1) {
			fprint(f, " > ");

			char buffer[256];
			int l = read(f, 0, 255, buffer);
			buffer[l] = 0;
			if (buffer[l-1] == '\n') {
				buffer[l-1] = 0; l--;

				if (strcmp(buffer, "about") == 0) {
					fprint(f, "Trivial/Computing Environment v0.1.0 - useless shell, first ediiton.\n");
				} else if (strcmp(buffer, "help") == 0) {
					fprint(f, "Available commands: about, help, exit.\n");
				} else if (strcmp(buffer, "exit") == 0) {
					fprint(f, "Exiting the shell. I think the system is pretty likely to panic.\n");
					break;
				} else if (strcmp(buffer, "gotosleep") == 0) {
					while (1) thread_sleep(1000);
				} else {
					fprint(f, "Unknown command. ");
					fprint(f, buffer);
				}
			} else {
				fprint(f, " - - - oops\n");
			}
		}
	}

	return 0;
}