/
main.c
225 lines (190 loc) · 5.86 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
#include "defines.h"
#include "utils.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
/*
* Interpret the user input as either
* a shell command or a program to be run
*/
void exec_command(char **tokens, int bg) {
char *path, *first, **firstparsed, **checkenv, *pager;
char *pathenv, *pathcp, **pathtokens;
char *tmp_str;
int i;
pid_t parent;
/* copy the first command string and handle
* this case specially - it might be a shell command */
first = malloc(sizeof(char)*strlen(tokens[0])+1);
strcpy(first, tokens[0]);
firstparsed = tokenize(first, ' ');
if (strcmp(firstparsed[0], "") == 0) {
free(first);
free(firstparsed);
free(tokens);
return;
}
if (strcmp(firstparsed[0], "exit") == 0) {
free(first);
free(firstparsed);
free(tokens);
/* try to terminate all childs before exiting */
/* send term to whole group, but ignore in parent */
register_sighandler(SIGTERM, SIG_IGN);
parent = getpid();
sighold(SIGCHLD);
/* give processes a chance to finish */
if (kill(-parent, SIGTERM) == -1) {
printf("Error: %s\n", strerror(errno));
exit(1);
}
sleep(1);
poll_childs();
sigrelse(SIGCHLD);
printf("Bye!\n");
exit(0);
}
if (strcmp(firstparsed[0], "cd") == 0) {
if (firstparsed[1] == '\0') {
/* default dir to change to */
path = getenv("HOME");
} else {
path = firstparsed[1];
}
if (DEBUG) {
printf("Change Directory to %s\n", path);
}
if (chdir(path) == -1) {
fprintf(stderr, "Error: %s\n", strerror(errno));
}
free(first);
free(firstparsed);
free(tokens);
return;
}
if (strcmp(firstparsed[0], "checkEnv") == 0) {
pager = getenv("PAGER");
if (pager == NULL) {
pathenv = getenv("PATH");
pathcp = malloc(strlen(pathenv)+1);
strcpy(pathcp, pathenv);
pathtokens = tokenize(pathcp, ':');
for (i = 0; i < tokens_length(pathtokens); i++) {
if((tmp_str = malloc(strlen(pathtokens[i])+strlen("/less")+1)) != NULL){
tmp_str[0] = '\0';
strcat(tmp_str,pathtokens[i]);
strcat(tmp_str,"/less");
if (access(tmp_str, X_OK) != -1) {
pager = "less";
free(tmp_str);
break;
}
free(tmp_str);
}
}
if (pager == NULL) pager = "more";
free(pathcp);
free(pathtokens);
}
if (DEBUG) {
printf("Pager: %s\n", pager);
}
/* As our fork_and_run function takes an array of commands, we prepare an
array with these commands in the checkenv array */
/* tmp_str will become "grep <arguments>" if any arguments are passed to
checkEnv */
tmp_str = malloc(sizeof(char*) * (LIMIT + 1));
if (tokens_length(firstparsed) > 1) {
checkenv = malloc(sizeof(char*) * (4 + 1));
tmp_str[0] = '\0';
strcat(tmp_str, "grep ");
for (i = 1; firstparsed[i] != NULL; i++) {
strcat(tmp_str, firstparsed[i]);
strcat(tmp_str, " ");
}
checkenv[0] = "printenv";
checkenv[1] = tmp_str;
checkenv[2] = "sort";
checkenv[3] = pager;
checkenv[4] = NULL;
} else {
checkenv = malloc(sizeof(char*) * (3 + 1));
checkenv[0] = "printenv";
checkenv[1] = "sort";
checkenv[2] = pager;
checkenv[3] = NULL;
}
fork_and_run(checkenv, bg);
free(tmp_str);
free(checkenv);
free(first);
free(firstparsed);
free(tokens);
return;
}
/* what was wrote wasn't "cd", "exit" or "checkEnv". We still execute
the command. */
free(first);
free(firstparsed);
fork_and_run(tokens, bg);
free(tokens);
}
int main(int argc, const char *argv[]) {
char linebuf[LIMIT+1];
char *read;
char **tokens;
int len, bg;
#if SIGDET == 0
/*pid_t pid;
int status;*/
#endif
register_sighandler(SIGINT, parent_sigint);
register_sighandler(SIGTSTP, parent_sigtstp);
/* SIGDET = 1 means that the child is responsible for reporting to
its parent when it's done so we register a sighandler for that */
#if SIGDET == 1
register_sighandler(SIGCHLD, parent_sigchld);
#endif
while (TRUE) {
read = NULL;
tokens = 0;
bg = FALSE;
#if SIGDET == 0
poll_childs();
#endif
type_prompt();
read = fgets(linebuf, LIMIT, stdin);
if (read != linebuf) {
/* end of file - quit as with exit command */
if (feof(stdin)) {
printf("^D\n");
strcpy(linebuf, "exit\n");
} else {
/* some interrupt, proceed to next read */
continue;
}
}
/* trim whitespace right (in order to read flag later) */
len = strlen(linebuf);
while ((len > 0) &&
(linebuf[len-1] == '\n' || linebuf[len-1] == ' ')) {
linebuf[len-1] = '\0';
len -= 1;
}
/* check for background flag */
if (len > 0 && linebuf[len-1] == '&') {
bg = TRUE;
linebuf[len-1] = '\0';
len -= 1;
}
tokens = tokenize(linebuf, '|');
exec_command(tokens, bg);
}
return 0;
}