summaryrefslogtreecommitdiff
path: root/userspace/foolshell.c
blob: e010c18ee07e569a5f12a20b83b3290b4fc871d0 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

extern char **environ;
//
void hello()
{
    // ascci art: http://patorjk.com/software/taag/#p=testall&f=Cards&t=Fool%20OS
    puts(
                    
    "       ______            __   ____  _____      \n"
    "      / ____/___  ____  / /  / __ \\/ ___/     \n"
    "     / /_  / __ \\/ __ \\/ /  / / / /\\__ \\   \n"
    "    / __/ / /_/ / /_/ / /  / /_/ /___/ /       \n"
    "   /_/    \\____/\\____/_/   \\____//____/     \n"
    "                                               \n"
    "Welcome to FoolShell v0.4 (Compiled on " __DATE__ " at " __TIME__ "\n"
    "------------------------------------------------------------------\n\n"
    "Please type 'help' anytime, to show shell \"built-ins\" or execute \n"
    "user programms that are in you $PATH directory by simply typing \n"
    "their filenames. You can get additional information for many commands\n"
    "by invking them with the --help or -h flag (e.g. ls --help) \n"
    );

    printf("Your $PATH is currently set to: %s\n\n",getenv("PATH"));
}

void prompt() 
{
    printf("%s%s",getenv("PWD"),getenv("PS1"));
}

int main(int argc, char **argv) 
{

    bool silent=false;
    for(int i=0;i<argc;i++)
    {
	if(!strcmp(argv[i],"--silent"))silent=true;
    }
    
    if(!silent)hello();

    char *buf=malloc(256);

    while(1)
    {
	prompt();
        fgets(buf,255,stdin);
	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++;
	token[c]=NULL;

    }

    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 [string]', 'malloc [bytes]', 'free [address]', 'getenv [var]', 'putenv/setenv [var] [val]', 'env' 'cd [dir]'");
    }
    else if(!strcmp(command,"cd"))
    {
	char *dir=getenv("PWD");

	char buf[256];

	if(token[1][0]!='/')
	{
	    sprintf(buf,"%s%s%s",dir,dir[0]=='/'&&dir[1]==0?"":"/",token[1]);
	}
	else
	{
	    sprintf(buf,"%s",token[1]);
	}

	char token[10];

	//adjust pwd (resolve '..' and '.')
////	printf("adjusting pwd: '%s'\n",buf);
	int left=1;
	int right=0;

	do{

	    int t=0;
	    do
	    {
		right++;
		token[t]=buf[right];

		t++;
	    }
	    while(buf[right]!=0&&buf[right]!='/');
	    token[t-1]=0;
	    
//	    printf("path token: '%s'\n",token);

	    

	    if(!strcmp(token,".."))
	    {
		left--;
		while(buf[left]!='/')left--;
	    }
	    else if(!strcmp(token,"."))
	    {
	    }
	    else
	    {
		int i=0;
		if(left!=1)buf[left++]='/';
		do{buf[left++]=token[i++];}while(token[i]!=0);

	    }

	}while(buf[right]!=0);
	buf[left]=0;
//	printf("adjusted: '%s'\n",buf);
	
	setenv("PWD",buf,1);

    }
    else if(!strcmp(command,"echo"))
    {
	printf("\"%s\"\n",token[1]);

    }
    else if(!strcmp(command,"malloc"))
    {
	uint8_t *mall=malloc(atoi(token[1]));
	printf("allocated %d bytes at 0x%08X (%i).\n",atoi(token[1]),mall,mall);
    }
    else if(!strcmp(command,"free"))
    {
	free(atoi(token[1]));
	printf("called free(0x%08X).\n",atoi(token[1]));
    }
    else if(!strcmp(command,"getenv"))
    {
	printf("(0x%08X) get: '%s' = '%s'(0x%08X) \n",environ,token[1],getenv(token[1]),getenv(token[1]));
    }
    else if(!strcmp(command,"putenv")||!strcmp(command,"setenv"))
    {
	char buf[256];
	sprintf(buf,"%s=%s",token[1],token[2]);
	putenv(buf);
	printf("(0x%08X) set: '%s' = '%s' \n",environ,token[1],getenv(token[1]));
    }
    else if(!strcmp(command,"env"))
    {
	int i=0;
	printf("env: 0x%08X\n",environ);
	while(environ[i]!=NULL)
	{
	    printf("envvar %s (0x%08X)\n" ,environ[i],environ[i]);
	    i++;
	}
    }
    else
    {
	char buf[256];
	sprintf(buf,"%s/%s",getenv("PATH"),token[0]);

	asm("mov $0x05bff,%esp"); // set stack pointer
	execve(buf,token,environ);
	asm("mov $0x07000,%esp"); // set stack pointer

	puts("foolshell: command not found");

	asm("mov $0x05bff,%esp"); // set stack pointer
	static char *argv[]={"shell","--silent",NULL};
	execve("/bin/foolshell",argv,environ); // start shell
	


    }
    return 0;

}