-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
489 lines (429 loc) · 9.9 KB
/
shell.c
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*shell.c
Michael Black, 2007
Creates the shell and handles the shell commands.
The shell is automatically loaded by the kernel
*/
#include "lib.h"
void doshell();
/*the shell starts here*/
main()
{
/*enable interrupts*/
setup();
/*start the main shell routine*/
doshell();
/*terminate - this shouldn't run*/
exit();
}
/*compares command with line. if they are equal, returns 1, else 0*/
int iscommand(char* line, char* command)
{
int i=0;
/*step through until reach end of string*/
while(command[i]!='\0')
{
if (line[i]!=command[i])
return 0;
i++;
}
return 1;
}
/*returns the first argument after the command*/
char* getargument(char* line)
{
int i=0;
int j=0;
/*find the space*/
while(line[i]!=' ')
i++;
i++;
/*find the end and replace it with a \0*/
j=i;
while(line[j]!=0xd)
{
j++;
if (j-i==80)
break;
}
line[j]=0x0;
return line+i;
}
/*execute a program. fore is 1 if the shell is meant to be paused while the program runs*/
void doexecute(char* line, int fore)
{
char program[12800];
/*extract the name from the command line*/
char* name=getargument(line);
/*check that a program is loaded by putting 0 in the first byte. if it stays, wasn't loaded*/
/*I guess it's bad luck if the program starts with 0*/
program[0]=0x0;
readfile(name,program);
if (program[0]==0x0)
{
printstring("Program not found\r\n\0");
}
else
{
/*use fore to pick one of the library functions*/
if (fore==1)
executeprogram(program,12800);
else
executeprogrambackground(program,12800);
}
}
/*load the file and print out the contents. assume that's it's ASCII*/
void dotype(char* line)
{
char file[12800];
char* name=getargument(line);
file[0]=0x1;
readfile(name,file);
if (file[0]==0x1)
{
printstring("File not found\r\n\0");
}
else
{
printstring(file);
}
}
/*print out a help screen*/
void dohelp()
{
printstring("The commands are: \r\n\0");
printstring(" CLS: Clear the screen\r\n\0");
printstring(" COPY: Copy one file to another with a different name\r\n\0");
printstring(" CREATE <FILE_NAME>: Create a text file FILE_NAME\r\n\0");
printstring(" MKDIR <FILE_NAME>: Create a directory FILE_NAME\r\n\0");
printstring(" DELETE <FILE_NAME>: Delete the file FILE_NAME\r\n\0");
printstring(" DIR: Print out the directory\r\n\0");
printstring(" EXEC <PROGRAM_NAME>: Run the program PROGRAM_NAME\r\n\0");
printstring(" EXECBACK <PROGRAM_NAME>: Run PROGRAM_NAME as a background process\r\n\0");
printstring(" HELP or ?: Print this message\r\n\0");
printstring(" KILL <PROCESS_NUMBER>: Terminate the process with the id PROCESS_NUMBER\r\n\0");
printstring(" TYPE <FILE_NAME>: Print out the contents of file FILE_NAME\r\n\0");
}
void doCreateFile(char* line)
{
char file[12800];
int index=0;
char* name=getargument(line);
char c=0;
printstring("Ingrese el texto del archivo, para finalizar escriba Ctrl + C.\r\n\n\0");
while(c!=0x3)
{
c=getchar();
file[index++]=c;
if (c!=0x3)
putchar(c);
//putchar(c);
if (c==0xd)
{
file[index++]=0xa;
putchar(0xa);
}
}
file[index-1]=0x0;
guardarArchivo(name,file, index);
}
/*allow the user to create a text file*/
void docreate(char* line)
{
char file[12800];
int index=0;
char* name=getargument(line);
char c=0;
printstring("Enter your text. End the file by typing CTRL-C.\r\n\n\0");
/*terminates when user presses CTRL-C - 3) */
while(c!=0x3)
{
/*get a character and store it*/
c=getchar();
file[index++]=c;
/*echo it back if it isn't CTRL-C*/
if (c!=0x3)
putchar(c);
/*if the user presses ENTER, follow the CR with a LF*/
if (c==0xd)
{
file[index++]=0xa;
putchar(0xa);
}
}
/*terminate the file with a 0*/
file[index-1]=0x0;
/*write it to the disk. Round up the sector*/
writefile(name,file,div(index,512)+1);
printstring("\r\n\0");
}
/*delete a file*/
void dodelete(char* line)
{
char* name=getargument(line);
/*make the system call*/
deletefile(name);
}
/*print out the directory*/
void dodir()
{
char dirsector[512];
char mapsector[512];
char name[7];
int i,j,index,files;
/*read the directory sector*/
readsector(2,dirsector);
printstring("Directory:\r\n\0");
index=0;
files=0;
/*the file system allows no more than 16 files*/
for (i=0; i<16; i++)
{
/*check if the file exists*/
if (dirsector[index]!=0)
{
/*print hte name*/
for (j=0; j<6; j++)
name[j]=dirsector[index+j];
name[6]=0;
printstring(name);
/*calculate the file size in sectors from the directory entry*/
j=0;
while(dirsector[index+j+6]!=0)
j++;
printstring(" \0");
printnumber(j);
printstring(" sectors\r\n\0");
files++;
}
/*move to the next entry*/
index=index+0x20;
}
printnumber(files);
printstring(" files total (out of 16 maximum)\r\n\0");
/*read the map sector to find out how many free sectors are left*/
readsector(1,mapsector);
j=0;
for (i=0; i<256; i++)
{
if (mapsector[i]==0x0)
j++;
}
printnumber(j);
printstring(" sectors available\r\n\0");
}
/*finds where a file name exists within the directory*/
int findname(char* name, char* sector)
{
int i,j,index=0;
/*search the directory until we find name*/
/*after 16 entries, quit*/
for (i=0; i<16; i++)
{
/*not a file? continue*/
if (sector[index]==0)
{
index=index+0x20;
continue;
}
/*try to match name*/
for (j=0; j<6; j++)
{
if (name[j]=='\0' && sector[index+j]==0x20)
{
j=6;
break;
}
if (name[j]!=sector[index+j])
break;
}
/*name matches, break*/
if (j==6)
break;
index=index+0x20;
}
/*did we not find it? then return -1*/
if (i==16)
{
return -1;
}
return index;
}
/*copy a file*/
void docopy()
{
char sname[7];
char dname[7];
char line[80];
char file[12800];
char dirsector[512];
int index,i;
/*prompt for the first file name*/
printstring("Name of the source file? \0");
readstring(line);
for (i=0; i<6; i++)
{
sname[i]=line[i];
if (line[i]==0xd)
break;
}
sname[i]=0x0;
/*make sure it exists - find the directory entry*/
readsector(2,dirsector);
index=findname(sname,dirsector);
if (index==-1)
{
printstring("File not found\r\n\0");
return;
}
/*read the source file*/
readfile(sname,file);
/*prompt for the destination file name*/
printstring("Name of the destination file? \0");
readstring(line);
for (i=0; i<6; i++)
{
dname[i]=line[i];
if (line[i]==0xd)
break;
}
dname[i]=0x0;
/*figure out how long the source file is*/
i=0;
index=index+6;
while(dirsector[index]!=0x0)
{
index++;
i++;
}
/*write the file*/
writefile(dname,file,i);
}
/*kill a process*/
void dokill(char* line)
{
/*make the system call*/
char* arg=getargument(line);
int21(9,arg[0]-0x30);
}
/*clear the screen*/
void doclear()
{
clearscreen();
setcursor(0,0);
}
void domkdir(char* line)
{
char* directorio=getargument(line);
mkdir(directorio);
}
void doFormat()
{
Format();
}
void doRemove(char* line)
{
char* nombre=getargument(line);
//printstring(nombre);
Remove(nombre);
}
void doList()
{
Listar();
}
void doEcho(char* line)
{
char* nombre = getargument(line);
leerArchivo(nombre);
}
void doCount(char* line)
{
char* nombre = getargument(line);
strTok(nombre);
}
/*this is the main routine*/
void doshell()
{
char line[80];
/*run forever - the shell shouldn't end*/
while(1==1)
{
/*read in a line*/
printstring("SHELL>\0");
readstring(line);
/*match it against each possible command*/
/*if the user presses return, ignore it*/
if (line[0]==0xd)
continue;
else if (iscommand(line,"CLS\0")==1)
doclear();
else if (iscommand(line,"cls\0")==1)
doclear();
else if (iscommand(line,"COPY\0")==1)
docopy();
else if (iscommand(line,"copy\0")==1)
docopy();
else if (iscommand(line,"CREATE \0")==1)
docreate(line);
else if (iscommand(line,"create \0")==1)
docreate(line);
else if (iscommand(line,"DELETE \0")==1)
dodelete(line);
else if (iscommand(line,"delete \0")==1)
dodelete(line);
else if (iscommand(line,"DIR\0")==1)
dodir();
else if (iscommand(line,"dir\0")==1)
dodir();
else if (iscommand(line,"EXEC \0")==1)
doexecute(line,1);
else if (iscommand(line,"exec \0")==1)
doexecute(line,1);
else if (iscommand(line,"EXECBACK \0")==1)
doexecute(line,0);
else if (iscommand(line,"execback \0")==1)
doexecute(line,0);
else if (iscommand(line,"HELP\0")==1)
dohelp();
else if (iscommand(line,"help\0")==1)
dohelp();
else if (line[0]=='?')
dohelp();
else if (iscommand(line,"TYPE \0")==1)
dotype(line);
else if (iscommand(line,"type \0")==1)
dotype(line);
else if (iscommand(line,"KILL \0")==1)
dokill(line);
else if (iscommand(line,"kill \0")==1)
dokill(line);
else if (iscommand(line,"mkdir \0")==1)
domkdir(line);
else if (iscommand(line,"MKDIR\0")==1)
domkdir(line);
else if (iscommand(line,"FORMAT\0")==1)
doFormat();
else if (iscommand(line,"format\0")==1)
doFormat();
else if (iscommand(line,"remove\0")==1)
doRemove(line);
else if (iscommand(line,"REMOVE\0")==1)
doRemove(line);
else if (iscommand(line,"list\0")==1)
doList();
else if (iscommand(line,"LIST\0")==1)
doList();
else if (iscommand(line,"count\0")==1)
doCount(line);
else if (iscommand(line,"WRITE\0")==1)
doCreateFile(line);
else if (iscommand(line,"write\0")==1)
doCreateFile(line);
else if (iscommand(line,"READ\0")==1)
doEcho(line);
else if (iscommand(line,"read\0")==1)
doEcho(line);
else
printstring("Command not found\r\n\0");
printstring("\r\n\0");
}
}