-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
170 lines (160 loc) · 5.52 KB
/
Copy pathmain.c
File metadata and controls
170 lines (160 loc) · 5.52 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
#include "headers.h"
void execute_com(char *commands)
{
char *delim = " \n\t\r";
char *s = (char*) malloc(MX_L2 * sizeof(char));
strcpy(s, commands);
int n_com = 0;
check_redirect = 0;
check_pipe =0;
char *temp = strtok(s, delim);
char **tokens = malloc(256 * sizeof(char*));
tokens[0] = temp;
while(temp != NULL) // get number of arguments
{
n_com++;
temp = strtok(NULL, delim);
tokens[n_com] = temp;
}
check_pipe = check_piping(commands);
check_redirect = check_redirection(commands);
if(check_pipe)
piping(commands);
else if(check_redirect)
redirection(commands);
else
{
char* com = strtok(commands, delim); // get the command
if (com == NULL) // check which command and execute accordingly
return;
else if (strcmp(com, "cd") == 0)
{
if(n_com > 2)
{
printf("cd: too many arguments\n");
strcpy(emoji,":'(");
return;
}
com = strtok(NULL, delim);
cd(com);
}
else if (strcmp(com, "pwd") == 0)
pwd();
else if (strcmp(com, "ls") == 0)
check_ls(com);
else if (strcmp(com, "echo") == 0)
echo(com);
else if (strcmp(com, "jobs") == 0)
print_jobs();
else if (strcmp(com, "kjob") == 0)
kjob(com, n_com);
else if (strcmp(com, "fg") == 0)
fg(com);
else if (strcmp(com, "bg") == 0)
bg(com);
else if (strcmp(com, "setenv") == 0)
set_env(com, n_com);
else if (strcmp(com, "unsetenv") == 0)
unset_env(com, n_com);
else if (strcmp(com, "history") == 0)
history_print(com);
else if (strcmp(com, "nightswatch") == 0)
nightswatch(com);
else if (strcmp(com, "overkill") == 0)
overkill();
else if (strcmp(com, "exit") == 0 || strcmp(com, "quit") == 0)
{
history_write();
overkill();
printf(YELLOW "\n\t Thank you for using myCShell. See you soon.\n\n" YELLOW_BOLD "\t\t\t Sayonara!\n\n" DFLT); // exit message
exit(EXIT_SUCCESS);
}
else if (strcmp(com, "pinfo") == 0)
{
pid_t p_id;
if(n_com > 2)
{
printf("pinfo error\n");
strcpy(emoji,":'(");
return;
}
if(n_com == 1)
{
p_id = getpid();
pinfo(p_id);
}
else
{
com = strtok(NULL, delim);
p_id = atoi(com);
pinfo(p_id);
}
}
else if (strcmp(tokens[n_com - 1], "&") == 0) // background process
{
tokens[n_com - 1] = NULL;
back(tokens);
}
else
fore(tokens);
}
free(tokens);
free(s);
}
int main()
{
getcwd(HOME, sizeof(HOME)); // get pseudo home directory where we run the shell
strcpy(LWD, HOME);
strcpy(CWD1, HOME);
getlogin_r(USER, sizeof(USER)); // get user
gethostname(HOST, sizeof(HOST)); // get host
SHELL_ID = getpid();
strcpy(emoji,":')"); // get shell pid
printf(YELLOW_BOLD "\n\t\t:(: Welcome to myCShell :):\t\t\n\n" DFLT); // welcome message
printf(YELLOW "\t\t It's pretty shelly out here !\n\n" DFLT);
size_t buffer_size = 0, inp_sz = 0;
char* inp = NULL; // taking command input
int num;
ll emoji_flag = 0;
history_init();
while (true)
{
free(inp); // free input buffer
signal(SIGCHLD, bg_handler);
signal(SIGTSTP, stphandler);
signal(SIGINT, ctrl_c);
inp_sz = 0;
buffer_size = 0;
num = 0;
f_current.pid = -1;
if(emoji_flag)
printf(DFLT "%s",emoji);
strcpy(emoji,":')");
prompt(); // display prompt
inp_sz = getline(&inp, &buffer_size, stdin); // get semi-colon separated commands as input
if(inp_sz == EOF)
{
printf(YELLOW "\n\t Thank you for using myCShell. See you soon.\n\n" YELLOW_BOLD "\t\t\t Sayonara!\n\n" DFLT); // exit message
overkill();
exit(EXIT_SUCCESS);
}
if(inp[0] != '\n')
history_update(inp);
char *token = strtok (inp, ";");
char **commands = malloc(256 * sizeof(char*));
char *delim = " \n\t\r";
while (token != NULL)
{
commands[num] = token;
token = strtok (NULL, ";");
num++;
}
for(int i = 0; i < num; i++) // executing the commands one by one
{
emoji_flag = 1;
strcpy(emoji,":')");
execute_com(commands[i]);
}
history_write(); // writing current history back to hist.txt
}
}