-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
86 lines (65 loc) · 1.47 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
#include "shell.h"
#include <sys/wait.h>
#include <sys/types.h>
void shell(void)
{
int status = 0; // Status of shell
int len = 0; // Current line length
short specChar = FALSE; // Bool if character is a special key
FILE *fp = NULL;
// Allocate sapce for the line
char ch, *line = (char*)malloc(MAX_LINE_BUFFER);
// Main shell Loop
prompt();
while(TRUE)
{
// Get the current char and check if it's a special character
ch = getch(fp);
if (specChar){
specChar = FALSE;
ch = switch_keypad(ch);
}
switch(ch){
// Arrow keys
case '\033':
getch(NULL);
specChar = TRUE;
continue;
break;
// Don't save tabs or spaces
case KEYLEFT:
case KEYRIGHT:
case KEYUP:
case KEYDOWN:
case '\t':
break;
// Backspacing
case DELETE:
{
if (len > 0)
delete(--len, line);
break;
}
default:
{
if (len < MAX_LINE_BUFFER){
if (ch != '\n'){
printf("%c", ch);
line[len++] = ch;
} else if (ch == '\n' && len > 0){ // Enter!
printf("%c", ch);
line[len] = '\0';
status = executeCMD(line); // send line to be executed in command.c
free(line); // Free the line space
if (status == TERMINATE) exit(SUCCESS); // If we terminate the app
line = (char*)malloc(MAX_LINE_BUFFER); // Allocate new Line Space
len = 0;
prompt(); // New Prompt for User
}
}
break;
}
}
}
fclose(fp);
}