-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysh.c
235 lines (178 loc) · 3.32 KB
/
mysh.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
#include"headers.h"
#include"varlib.h"
#include"mysh.h"
#include"mypwd.h"
#include"redir.h"
#include"pipe.h"
#include"color.h"
//#define DEBUG
/* This is the function to read the command line */
char *read_args(FILE *fp,char *prompt)
{
char *cmd;
int cnt=0;
int ch;
int cmd_size = BUFSIZE;
cmd=(char *)malloc(cmd_size);
//printf("%s",prompt);
show_info(get_node());
printf("$ ");
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n')
break;
cmd[cnt++]=ch;
if (cnt ==BUFSIZE-1)
{
cmd_size+=BUFSIZE;
cmd=realloc(cmd,cmd_size);
}
}
if (cnt > 0)
{
cmd[cnt]='\0';
}
if (cnt == 0)
{
free(cmd);
cmd=NULL;
if (ch != EOF)
return read_args(fp,prompt);
}
return cmd;
}
/* error handling function */
void error_occur(char *msg)
{
char *p;
int len=strlen(msg)+strlen(KYEL);
p=(char*)malloc(len*sizeof(char) +1);
strcpy(p,KYEL);
strcat(p,msg);
p[len] = '\0';
perror(p);
printf(KYEL"exiting...\n");
free(p);
exit(0);
}
/* To parse the command line */
char *get_memory(char *str, int len)
{
char *return_value;
return_value =(char*)malloc(len+1);
if (return_value == NULL)
error_occur("malloc error");
for (int i=0; i<len; i++)
return_value[i]=str[i];
return_value[len]='\0';
return return_value;
}
char **parse_args(char *cmd)
{
int arg_nums=0;
int arg_arry_len=ARG_ARRAY_LEN;
char **args;
if (cmd == NULL)
return NULL;
args = (char **)malloc(arg_arry_len*sizeof(char **)); /* Make sure to add "sizeof" */
if (args == NULL)
error_occur("malloc error");
int len=0;
char *start_pos;
while (*cmd != '\0')
{
len=0;
while ((*cmd) == ' ')
cmd++;
start_pos=cmd;
while ((*cmd) != ' ' && (*cmd) != '\0')
{
len++;
cmd++;
}
if (len > 0)
{
char *p = get_memory(start_pos,len);
args[arg_nums++] = p;
if (arg_nums == arg_arry_len -2)
{
arg_arry_len+=ARG_ARRAY_LEN;
args = realloc(args, arg_arry_len*sizeof(char **));
}
}
}
args[arg_nums]=NULL;
return args;
}
/*To free the memory thar args take*/
void free_args(char **args)
{
for (int i=0; *(args+i); i++)
free(*(args+i)); //IT IS VERY EASY TO GET WRONG!
free(args);
}
/* to execute the programs */
int execute(char **args)
{
int status;
/* to handle the varible */
if (var_handler(args))
return (0);
/* the build-in command 'cd' */
if (strcmp(args[0],"cd") == 0)
{
if (!args[1])
return 1;
else
{
chdir(args[1]);
return 0;
}
}
/* quit command*/
if (args == NULL)
error_occur("args is NULL");
if (strcmp(args[0],"exit") == 0)
exit(0);
if (strcmp(args[0],"quit") == 0)
exit(0);
if (strcmp(args[0],"q") ==0)
exit(0);
int new_pid=fork();
if (new_pid == -1)
error_occur("fork");
if (new_pid == 0)
{
/* redirect:to make the stdout be the filename*/
is_redir(args);
/* to handler the pipe situation*/
int pipe_pos=is_pipe(args);
if (pipe_pos > 0)
{
char *args1[pipe_pos+1];
for (int i=0; i<pipe_pos; i++)
args1[i]=args[i];
args1[pipe_pos] = NULL;
make_pipe(args1,args+pipe_pos+1);
}
/* normal condition */
else
{
execvp(args[0],args);
error_occur("the son program has some problems");
}
}
else
{
if (wait(&status) == -1)
error_occur("while waiting");
return status;
}
}
#ifdef DEBUG
void print_args(char ** args)
{
for (int i=0; *(args+i); i++)
printf("%s\n",*(args+i));
}
#endif