-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.cpp
More file actions
159 lines (141 loc) · 4.87 KB
/
process.cpp
File metadata and controls
159 lines (141 loc) · 4.87 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
#include "shell.h"
void process_input(char *cmd, char *alias_copy) {
char *args[MAX_ARGS];
char input_copy[1024];
strncpy(input_copy, cmd, sizeof(input_copy));
input_copy[1023] = '\0';
parse(input_copy, args);
expand_tilde(args);
expand_variables(args);
if (args[0] == NULL) return;
int anzahl_args = 0;
while (args[anzahl_args] != NULL) anzahl_args++;
// Builtins (cd, echo, export, alias, history, config, exit)
if (run_builtin(args, anzahl_args, alias_copy)) return;
// echo mit $VAR Support und Redirect
if (strcmp(args[0], "echo") == 0) {
char *datei = NULL;
char type;
for (int i = 1; i < anzahl_args; i++) {
if (args[i] && args[i][0] == '$') {
char *value = getenv(args[i] + 1);
args[i] = value ? value : (char *)"";
}
}
if (find_redirect(args, anzahl_args, &datei, &type))
execute_redirect(args, datei, type);
else {
for (int i = 1; args[i] != NULL; i++)
printf("%s ", args[i]);
printf("\n");
}
return;
}
// ls separat wegen Redirect und Flags
if (strcmp(args[0], "ls") == 0) {
bool show_all = false;
bool show_long = false;
const char *path = NULL;
char *datei = NULL;
char type;
for (int i = 1; i < anzahl_args; i++) {
if (strcmp(args[i], "-la") == 0 || strcmp(args[i], "-al") == 0) { show_all = true; show_long = true; }
else if (strcmp(args[i], "-l") == 0) show_long = true;
else if (strcmp(args[i], "-a") == 0) show_all = true;
else path = args[i];
}
if (find_redirect(args, anzahl_args, &datei, &type))
execute_redirect(args, datei, type);
else
my_ls(path, show_all, show_long);
return;
}
// Alias auflösen falls vorhanden
char *alias_value = find_alias(args[0]);
if (alias_value) {
parse(alias_value, args);
expand_tilde(args);
expand_variables(args);
anzahl_args = 0;
while (args[anzahl_args] != NULL) anzahl_args++;
}
if (anzahl_args > 0 && strcmp(args[anzahl_args - 1], "&") == 0) {
background = true;
args[anzahl_args - 1] = NULL;
anzahl_args--;
}
// Redirect oder normale Ausführung
char *datei = NULL;
char type;
if (find_redirect(args, anzahl_args, &datei, &type))
execute_redirect(args, datei, type);
else{
execute(args, background);
background = false;
}
}
void run_command(char *input, char *alias_copy) {
// Erst Semikolon, dann && / ||, dann Pipes
char *semi_befehle[MAX_ARGS];
int anzahl_semi = split_semicolon(input, semi_befehle);
for (int s = 0; s < anzahl_semi; s++) {
// && — nächster Befehl nur bei Erfolg
if (strstr(semi_befehle[s], "&&")) {
char *and_befehle[MAX_ARGS];
int anzahl_and = split_and(semi_befehle[s], and_befehle);
int last_exit = 0;
for (int a = 0; a < anzahl_and; a++) {
if (last_exit != 0) break;
char tmp[1024]; strncpy(tmp, and_befehle[a], 1024);
char *args[MAX_ARGS];
parse(tmp, args);
expand_tilde(args);
expand_variables(args);
if (args[0]) last_exit = execute(args, background);
background = false;
}
continue;
}
// || — nächster Befehl nur bei Fehler
if (strstr(semi_befehle[s], "||")) {
char *or_befehle[MAX_ARGS];
int anzahl_or = split_or(semi_befehle[s], or_befehle);
int last_exit = 1;
for (int o = 0; o < anzahl_or; o++) {
if (last_exit == 0) break;
char tmp[1024]; strncpy(tmp, or_befehle[o], 1024);
char *args[MAX_ARGS];
parse(tmp, args);
expand_tilde(args);
expand_variables(args);
if (args[0]) last_exit = execute(args, background);
background = false;
}
continue;
}
// Pipes
char *pipes[MAX_ARGS];
int anzahl_pipes = split_pipes(semi_befehle[s], pipes);
if (anzahl_pipes > 1) {
execute_pipe(pipes, anzahl_pipes);
continue;
}
process_input(semi_befehle[s], alias_copy);
}
}
void print_jobs(){
for (int i = 0; i < job_count; i++) {
printf("[%d] %d running %s\n",
jobs[i].id, jobs[i].pid, jobs[i].command);
}
}
void remove_finished_jobs(){
int wstatus;
for (int i = 0; i < job_count; i++){
if (waitpid(jobs[i].pid, &wstatus, WNOHANG) > 0) {
memmove(&jobs[i], &jobs[i+1], (job_count - i - 1) * sizeof(Job));
i--;
job_count--;
}
}
}