-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
183 lines (167 loc) · 5.91 KB
/
Copy pathmain.c
File metadata and controls
183 lines (167 loc) · 5.91 KB
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
//Loading all the necessary header files
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/utsname.h>
#include<signal.h>
#include<ctype.h>
#include "LLS.h"
#include "utils.h"
#include "dirProc.h"
#include "exec.h"
//Driver function
int main(){
//Instructions on how to use this shell
purple();
printHelp();
reset();
printf("\n");
getShellName(); //Getting the name of the shell
//Defining the various signals
signal(SIGCHLD, sigHandler);
signal(SIGINT, sigHandler);
char dir[maxLength]; //Defining variable to get the preset working directory
//Some variables that help in getting the input
size_t len = 0;
char* command = NULL;
//Starting the shells
while(1){
strcpy(dir, "");
yellow();
bold();
printf("%s", shellName); //Shell prompt
cyan();
bold();
printf("%s-> ", getcwd(dir, maxLength)); //Shell prompt
reset();
//Getting the input command
if(getline(&command, &len, stdin) == -1){
printf("\n");
return 0;
}
//Removing trailing '\n'
if(strlen(command) > 0 && command[strlen(command) - 1]=='\n'){
command[strlen(command) - 1] = '\0';
}
//Removing leading and trailing spaces
command = trim(command);
//Creating copy of the command
char* command_ = strndup(command, strlen(command));
//Checking for redirection and calling the necessary function
if(check(addChars(command_, '\|'), "|") == 1 || check(addChars(command_, '\>'), ">") == 1 || check(addChars(command_, '\<'), "<") == 1){
execRedirectionAndPipe(command);
}
//Checking for change directory command and calling the necessary function
else if(strlen(command) > 3 && command[0] == 'c' && command[1] == 'd' && (command[2] == ' ' || command[2] == '\t')){
push(&histHead, command, getpid());
changeDir(trim(command + 3));
}
//Printing the last 'N' commands executed using HISTN command
else if(command[0] != '!' && strstr(command, "HIST")){
if(isNumber(command + 4) == 1){
printHistory(histHead, atoi(command + 4));
}else{
red();
printf("[!!]Error while executing command. To print the last ");
yellow();
printf("\'N\' ");
red();
printf(", commands run ");
yellow();
printf("\'!HISTN\'\n");
red();
printf(" (where N is the number commands)");
reset();
}
}
//Executing the Nth command using !HISTN command
else if(strstr(command, "HIST")){
if(isNumber(command+5) == 1 && numWords(command) == 1){
execCommandN(histHead, atoi(command + 5));
}else{
red();
printf("[!!]Error while executing command. To execute the ");
yellow();
printf("\'Nth\' ");
red();
printf(", command, run ");
yellow();
printf("\'!HISTN\'");
red();
printf(" (where N is the command number)\n");
reset();
}
}
//Command to print the pid of the current shell
else if(strcmp(command, "pid") == 0){
purple();
printf("\n Process name : ");
yellow();
printf("%-30s", "./shell");
purple();
printf("process id : ");
yellow();
printf("%-5d\n", getpid());
reset();
}
//Command to print the pid of all the spawned processes from the shell
else if((strstr(command, "pid") != NULL) && (strstr(command, "all") != NULL) && (numWords(command) == 2)){
if(histHead == NULL){
purple();
printf("\n*No process spawned from this shell so far*\n");
reset();
}
else{
printPidAll(histHead);
}
}
//Command to print all the active processes spanwed from the shell
else if((strstr(command, "pid") != NULL) && (strstr(command, "current") != NULL) && (numWords(command) == 2)){
if(currProc == NULL){
purple();
printf("\n*No process currently active*\n");
reset();
}
else{
printPidAll(currProc);
}
}
//Command to exit the shell
else if(strcmp(command, "STOP") == 0){
killBackgroundProcesses(&currProc); //Killing background processes, if any
purple();
printf("\n*Exiting from this shell, bye*\n\n");
reset();
free(command);
break;
}
else if(strcmp(command, "HELP") == 0){
purple();
printHelp();
reset();
}
//Execution of normal command (background and foreground)
else{
int flag = 0;
for(int i = 0; i < strlen(command); i++){
if(isalpha(command[i]) || isdigit(command[i])){
flag = 1;
break;
}
}
if(flag == 1){
char* command__ = strdup(command);
if(command[strlen(command) - 1] != '&'){ //Checking if the process is background process or not
char** arr = tokenize(command);
execCommand(arr, 0);
}else{
command[strlen(command) - 1] = '\0';
char** arr = tokenize(command);
execCommand(arr, 1);
}
}
}
command = NULL;
printf("\n");
}
}