summaryrefslogtreecommitdiff
path: root/userspace/foolshell.c
blob: 6baeb87f4485761c3cc6d9f62fc9e80f6db5ec17 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <string.h>
#include "syscalls.c"
#include "../fs/fs.h"

void hello() {
    puts(
        "Welcome to FoolShell v0.1"
    );
}

void prompt() {
    printf(
        "$ "
    );
}

int main(int argc, char **argv) 
{
    syscalls_init();
    hello();

    FILE *input;
    input=fopen(1,"r");
    char *buf=malloc(256);

    while(1)
    {
	prompt();
        fgets(buf,255,input);
	buf[strlen(buf)-1]=0; // remove \n
	process(buf);
    }

    return 0;
}

char **tokenize(char *buf)
{

    char **token;
    token=malloc(10*sizeof(char*));

    int l=strlen(buf);

    int i;
    int c=0;

    for(i=0;i<l;i++)
    {  
	// init space for next token
	token[c]=malloc(256);

	//skip all the whitespace
	while(buf[i]==' '&&i<l)i++;
	if(i==l)break;

	//get token
	int t=0;

	while(buf[i]!=' '&&i<l)
	{
	    token[c][t]=buf[i];
	    t++;
	    i++;
	}
	token[c][t]=0;


//	printf("token %i : <%s>\n",c, token[c]);
	c++;

    }

    return token;

}

int process(char *buf)
{
    
    char **token=tokenize(buf);
    char *command=token[0];
  //  puts(command);
    // copied from trottelshell

    if(!strcmp(command,"HELP"))
    {
	puts("foolshell: supported built-in commands: HELP, ECHO, TIME, MEM, PROC, TASKS, ALLOC, LS, SYSCALL.");
    }
    else if(!strcmp(command,"TIME"))
    {
//	uint32_t time=task_system_clock;
//	log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"%d seconds passed since system start.",(time/25));
	puts("foolshell: TIME not supported now.");
    }
    else if(!strcmp(command,"MEM"))
    {
	//mmap_show_free();	
	puts("foolshell: MEM not supported now.");
    }
    else if(!strcmp(command,"PROC"))
    {
	/*
        for(int i=0;i<SMP_MAX_PROC;i++)
	{
	    if(cpu_counter[i]!=0)
		log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"cpu: %d => %d.",i,cpu_counter[i]);
	}
	*/
	puts("foolshell: PROC not supported now.");
    }
    else if(!strcmp(command,"TASKS"))
    {
	//log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"%d %d %d",c1,c2,c2);
	puts("foolshell: TASKS not supported now.");
    }
    else if(!strcmp(command,"ALLOC"))
    {
	/*
	uint32_t *malloc= pmmngr_alloc_block();
	log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"allocated 4KB block at: %08x.",malloc);
	*/
	puts("foolshell: ALLOC not supported now.");
    }
    else if(!strcmp(command,"SYSCALL"))
    {

	puts("system call 10");
	unsigned int ebx; // will hold return value;

	// system call 
	asm("pusha");
	asm("mov $10,%eax"); // select syscall 10 (example_syscall)
	asm("mov $666,%edx");
	asm("mov $333,%ecx");
	asm("int $0x80"); // actual syscall ! interrupt
        asm("mov %%ebx, %0": "=b" (ebx));
	asm("popa");
	//

	printf("system call returned %i\n",ebx);

    }
    else if(!strcmp(command,"LS"))
    {
	//fs_dirent dirs[25];
	
	fs_dirent *dirs=malloc(sizeof(fs_dirent)*25);

	int  ls=readdir("/dupa",dirs,25);
	int i;
	for(i=0;i<ls;i++)
	{
	    printf("%i %s%c\n",dirs[i].inode, dirs[i].name, ((dirs[i].type==FS_FILE_TYPE_DIR)?'/':' '));
	}

    }
    else if(!strcmp(command,"ECHO"))
    {

	printf("%s\n",token[1]);

    }
    else
    {
	puts("foolshell: command not found");
    }

    //
}