-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
280 lines (248 loc) · 7.42 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <ctype.h>
#define MAX_HISTORY 100
#define MAX_COMMAND_LENGTH 1024
#define MAX_TOKENS 100
typedef struct {
char *name; // Command name
char *argv[MAX_TOKENS]; // Array of command arguments
char *input_file; // Input redirection file
char *output_file; // Output redirection file
} command;
typedef struct {
int num_commands; // Number of commands in the pipeline
command cmds[MAX_TOKENS]; // Array of commands
} commands;
char *history[MAX_HISTORY]; // History of commands
int history_count = 0; // Number of commands in history
// Add a command to the history
void add_to_history(const char *input) {
if (history_count >= MAX_HISTORY) {
free(history[0]);
memmove(history, history + 1, (MAX_HISTORY - 1) * sizeof(char *));
history_count--;
}
history[history_count++] = strdup(input);
}
// Free all allocated memory in history
void clear_history() {
for (int i = 0; i < history_count; i++) {
free(history[i]);
}
history_count = 0;
}
// Print the command history
void print_history() {
for (int i = 0; i < history_count; i++) {
printf("%d: %s\n", i + 1, history[i]);
}
}
// Read input from the user
void read_input(char *input) {
printf("[%s] > ", getcwd(NULL, 0)); // Prompt
fflush(stdout);
int pos = 0; // Position in the input buffer
input[0] = '\0';
while (1) {
char c;
if (read(STDIN_FILENO, &c, 1) == -1) {
perror("read() error");
exit(EXIT_FAILURE);
}
if (c == '\n') { // Enter key
input[pos] = '\0';
printf("\n");
break;
}
if (isprint(c)) { // Printable characters
input[pos++] = c;
input[pos] = '\0';
printf("%c", c);
} else if (c == 127 && pos > 0) { // Backspace
pos--;
input[pos] = '\0';
printf("\r\033[K[%s] > %s", getcwd(NULL, 0), input);
}
}
}
// Parse a single command into its name, arguments, and redirections
int parse_command(char *input, command *cmd) {
char *token = strtok(input, " ");
int token_index = 0;
cmd->name = NULL;
cmd->input_file = NULL;
cmd->output_file = NULL;
while (token != NULL) {
if (strcmp(token, "<") == 0) {
token = strtok(NULL, " ");
if (token == NULL) {
fprintf(stderr, "Error: Missing input file for redirection\n");
return -1;
}
cmd->input_file = token;
} else if (strcmp(token, ">") == 0) {
token = strtok(NULL, " ");
if (token == NULL) {
fprintf(stderr, "Error: Missing output file for redirection\n");
return -1;
}
cmd->output_file = token;
} else {
if (cmd->name == NULL) {
cmd->name = token;
}
cmd->argv[token_index++] = token;
}
token = strtok(NULL, " ");
}
cmd->argv[token_index] = NULL;
return 0;
}
// Parse the input string into a sequence of commands separated by pipes
int parse_commands_with_pipes(char *input, commands *cmds) {
char *token = strtok(input, "|");
int command_index = 0;
while (token != NULL) {
if (parse_command(token, &cmds->cmds[command_index]) == -1) {
return -1;
}
command_index++;
token = strtok(NULL, "|");
}
cmds->num_commands = command_index;
return command_index;
}
// Execute a single command with redirections using execvp
void exec_command(command *cmd) {
if (cmd->input_file != NULL) {
int fd_in = open(cmd->input_file, O_RDONLY);
if (fd_in == -1) {
perror("Error opening input file");
exit(EXIT_FAILURE);
}
dup2(fd_in, STDIN_FILENO);
close(fd_in);
}
if (cmd->output_file != NULL) {
int fd_out = open(cmd->output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd_out == -1) {
perror("Error opening output file");
exit(EXIT_FAILURE);
}
dup2(fd_out, STDOUT_FILENO);
close(fd_out);
}
if (execvp(cmd->name, cmd->argv) == -1) {
perror("Command execution failed");
exit(EXIT_FAILURE);
}
}
// Execute a sequence of commands with pipes and redirections
void exec_commands(commands *cmds) {
int pipefds[2 * (cmds->num_commands - 1)];
pid_t pids[cmds->num_commands];
for (int i = 0; i < cmds->num_commands - 1; i++) {
if (pipe(pipefds + i * 2) == -1) {
perror("Pipe creation failed");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < cmds->num_commands; i++) {
pids[i] = fork();
if (pids[i] == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (pids[i] == 0) {
if (i > 0) {
dup2(pipefds[(i - 1) * 2], STDIN_FILENO);
}
if (i < cmds->num_commands - 1) {
dup2(pipefds[i * 2 + 1], STDOUT_FILENO);
}
for (int j = 0; j < 2 * (cmds->num_commands - 1); j++) {
close(pipefds[j]);
}
exec_command(&cmds->cmds[i]);
}
}
for (int i = 0; i < 2 * (cmds->num_commands - 1); i++) {
close(pipefds[i]);
}
for (int i = 0; i < cmds->num_commands; i++) {
waitpid(pids[i], NULL, 0);
}
}
// Handle built-in commands such as exit, cd, and history
int handle_built_in(command *cmd) {
if (strcmp(cmd->name, "exit") == 0) {
clear_history();
exit(0);
} else if (strcmp(cmd->name, "cd") == 0) {
if (cmd->argv[1] == NULL) {
fprintf(stderr, "cd: missing argument\n");
} else {
if (chdir(cmd->argv[1]) == -1) {
perror("cd failed");
}
}
return 1;
} else if (strcmp(cmd->name, "history") == 0) {
if (cmd->argv[1] != NULL) {
int index = atoi(cmd->argv[1]) - 1;
if (index >= 0 && index < history_count) {
printf("%s\n", history[index]);
command cmd_to_run;
parse_command(history[index], &cmd_to_run);
pid_t pid = fork();
if (pid == -1) {
perror("Fork failed");
return 1;
} else if (pid == 0) {
exec_command(&cmd_to_run);
} else {
waitpid(pid, NULL, 0);
}
} else {
fprintf(stderr, "Invalid history index\n");
}
} else {
print_history();
}
return 1;
}
return 0;
}
// Handle signals such as SIGINT
void handle_signal(int sig) {
if (sig == SIGINT) {
printf("\n");
}
}
int main() {
char input[MAX_COMMAND_LENGTH];
commands cmds;
signal(SIGINT, handle_signal);
while (1) {
read_input(input);
if (strlen(input) == 0) {
continue;
}
add_to_history(input);
if (parse_commands_with_pipes(input, &cmds) == -1) {
continue;
}
if (handle_built_in(&cmds.cmds[0])) {
continue;
}
exec_commands(&cmds);
}
clear_history();
return 0;
}