-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
158 lines (139 loc) · 4.32 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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "builtIn.h"
#include "parse.h"
char *print_prompt();
void executeCommand(int in, int out, commandType *input_command, parseInfo *result);
int MAX_PATH = 1024;
job *first_job = NULL;
char input_copy[MAX_COM_SIZE];
// Readline template source: https://en.wikipedia.org/wiki/GNU_Readline
int main() {
// Configure readline to disable tab completion
rl_bind_key('\t', rl_insert);
using_history();
while (1) {
// Display prompt and read input
char *buffer = print_prompt();
char *input = readline(buffer);
check_and_free(buffer)
// Check for EOF.
if (!input)
break;
// make a copy of command for use in jobs
strcpy(input_copy, input);
parseInfo *result = parse(input);
if (result == NULL) {
goto free;
}
// add input to readline history.
add_history(input_copy);
time_t t;
time(&t);
add_history_time(ctime(&t));
commandType *input_command = &result->CommArray[0];
// execute builtin command in parent process
int commType = isBuiltInCommand(input_command->command);
if (commType == EXIT) {
copy_running_jobs(&first_job);
if (first_job != NULL) {
printf("Can't exit as there are background jobs running! Please kill them first.\n");
} else {
free_info(result);
free_jobs(first_job);
check_and_free(input)
exit(0);
}
} else if (commType != NO_SUCH_BUILTIN) {
executeBuiltInCommand(input_command, commType, history_get_history_state(), first_job);
} else {
// create a child process for each | or & separated command
int in, pipe_file_descriptor[2];
// The first process should get its input from the original file descriptor 0
in = STDIN_FILENO;
for (int i = 0; i < result->pipeNum - 1; i++) {
// Source: https://stackoverflow.com/questions/8082932/connecting-n-commands-with-pipes-in-a-shell
pipe(pipe_file_descriptor);
executeCommand(in, pipe_file_descriptor[1], &result->CommArray[i], result);
/* No need for the write end of the pipe, the child will write here. */
close(pipe_file_descriptor[1]);
/* Keep the read end of the pipe, the next child will read from there. */
in = pipe_file_descriptor[0];
}
executeCommand(in, STDOUT_FILENO, &result->CommArray[result->pipeNum - 1], result);
}
// Free buffer that was allocated by readline
free:
free_info(result);
check_and_free(input)
}
return 0;
}
void executeCommand(int in, int out, commandType *input_command, parseInfo *result) {
pid_t childPid;
int status;
childPid = fork();
// inside child...
if (childPid == 0) {
if (input_command->boolInfile || input_command->boolOutfile) {
if (input_command->boolInfile) {
in = open(input_command->inFile, O_RDONLY);
dup2(in, STDIN_FILENO);
close(in);
}
if (input_command->boolOutfile) {
out = open(input_command->outFile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
dup2(out, STDOUT_FILENO);
close(out);
}
} else if (!result->boolBackground) {
if (in != STDIN_FILENO) {
dup2(in, STDIN_FILENO);
close(in);
}
if (out != STDOUT_FILENO) {
dup2(out, STDOUT_FILENO);
close(out);
}
}
execvp(input_command->command, input_command->VarList);
perror("Error! execvp");
free_info(result);
exit(1);
}
// inside parent...
else if (childPid > 0) {
if (result->boolBackground) {
// record in list of background jobs
copy_running_jobs(&first_job);
add_job(&first_job, childPid, input_copy);
waitpid(childPid, &status, WNOHANG);
} else {
waitpid(childPid, &status, NO_MATCH);
}
} else {
perror("Error! fork");
}
}
char *print_prompt() {
char *buffer, *cwd, *host = NULL;
size_t allocSize = sizeof(char) * MAX_PATH;
buffer = (char *) malloc(allocSize);
// Source: https://stackoverflow.com/questions/504810/how-do-i-find-the-current-machines-full-hostname-in-c-hostname-and-domain-info
// Gets hostname of machine
host = (char *) malloc(allocSize);
gethostname(host, allocSize);
// Source: https://stackoverflow.com/questions/298510/how-to-get-the-current-directory-in-a-c-program
// Gets current path of user
cwd = (char *) malloc(allocSize);
getcwd(cwd, allocSize);
snprintf(buffer, MAX_PATH, "%s@%s:%s$ ", getenv("USER"), host, cwd);
free_and_null(cwd)
free_and_null(host)
return buffer;
}