-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
185 lines (170 loc) · 3.52 KB
/
shell.cpp
File metadata and controls
185 lines (170 loc) · 3.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include<readline/readline.h>
#include<readline/history.h>
#include <signal.h>
#define RED "\001\033[0;31m\002"
#define YELLOW "\001\033[0;33m\002"
#define CYAN "\001\033[0;36m\002"
#define GREEN "\001\033[0;32m\002"
#define BLUE "\001\033[0;34m\002"
#define INVERT "\001\033[0;7m\002"
#define RESET "\001\e[0m\002"
#define BOLD "\001\e[1m\002"
#define ITALICS "\001\e[3m\002"
#define TOK_BUFSIZE 64
#define TOK_DELIM " \t\r\n\a"
#define BUILT_NUM 3
using namespace std;
void shell_cd(char **args);
void shell_help(char **args);
void shell_exit(char **args);
void shell_loop();
char *shell_readline(char* path);
char **parse_line(char *line);
void shell_execute(char **args);
void shell_launch(char **args);
void sigintHandler(int sig_num)
{
cout<<'\b'<<'\b'<<" ";
//cout<<endl;
return;
}
int main(){
shell_loop();
return 0;
}
void shell_loop(){
char *line;
char **args;
char path[FILENAME_MAX];
signal(SIGINT, sigintHandler);
while(1){
getcwd(path,FILENAME_MAX);
line = shell_readline(path);
args = parse_line(line);
shell_execute(args);
free(line);
free(args);
}
}
char *shell_readline(char* path){
char *buf;
char *line = (char*)malloc(1000*sizeof(char));
char str[200];
strcpy(str, RED);
strcat(str, BOLD);
strcat(str, getenv("USER"));
strcat(str, RESET);
strcat(str, ":");
strcat(str, CYAN);
strcat(str, BOLD);
strcat(str, path);
strcat(str, RESET);
strcat(str, "> ");
buf = readline(str);
if (strlen(buf) != 0) {
add_history(buf);
strcpy(line, buf);
free(buf);
return line;
} else {
free(buf);
free(line);
shell_readline(path);
}
}
char **parse_line(char *line){
int bufsize = TOK_BUFSIZE, position = 0;
char **args = (char**)malloc(bufsize*sizeof(char*));
char *token = strtok(line, TOK_DELIM);
while(token != NULL){
args[position]=token;
position++;
if(position>=bufsize){
bufsize+=TOK_BUFSIZE;
args = (char**)realloc(args, bufsize * sizeof(char*));
}
token = strtok(NULL, TOK_DELIM);
}
if(!strcmp(args[0],"ls")){
args[position]=(char*)"--color";
position++;
args[position]=0;
}
else{
args[position]=0;
}
return args;
}
const char *builtins_str[]={"cd","help","exit"};
void (*builtin_func[]) (char **) = {
&shell_cd,
&shell_help,
&shell_exit
};
void shell_execute(char **args){
if(args[0]==NULL){
return;
}
for(int i=0; i<BUILT_NUM; i++){
if(strcmp(args[0], builtins_str[i])==0){
(*builtin_func[i])(args);
return;
}
}
shell_launch(args);
return;
}
void shell_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "shell: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
perror("rshell");
}
}
return;
}
void shell_help(char **args)
{
int i;
cout<<"Type program names and arguments, and hit enter.\n";
cout<<"The following in-built commands are supported:\n";
for (i = 0; i < BUILT_NUM; i++) {
cout<<builtins_str[i]<<"\n";
}
cout<<"Use the man command for information on other programs.\n";
return ;
}
void shell_exit(char **args)
{
free(args);
exit(0);
}
void shell_launch(char **args){
pid_t pid, wpid;
int status;
pid = fork();
if(pid == 0){
//child
if(execvp(args[0],args)==-1){
perror("rshell");
}
exit(1);
}
else if(pid == -1){
perror("rshell");
}
else{
//parent
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return;
}