-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
223 lines (199 loc) · 5.6 KB
/
parse.cpp
File metadata and controls
223 lines (199 loc) · 5.6 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
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
#include "shell.h"
#include <cstdlib>
#include <cstring>
#include <cctype>
void parse(char *input, char **args) {
int i = 0;
char *p = input;
while (*p != '\0') {
while (*p == ' ' || *p == '\t' || *p == '\n') {
p++;
if (*p == '\0') break;
}
if (*p == '\0') break;
if (i >= MAX_ARGS - 1) {
fprintf(stderr, "Zu viele Argumente (max %d)\n", MAX_ARGS - 1);
break;
}
if (*p == '"') {
p++;
args[i++] = p;
while (*p != '"' && *p != '\0') p++;
if (*p == '"') *p++ = '\0';
} else {
args[i++] = p;
while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') p++;
if (*p != '\0') *p++ = '\0';
}
}
args[i] = NULL;
}
int split_pipes(char *input, char **befehle) {
int anzahl = 0;
befehle[anzahl] = strtok(input, "|");
while (befehle[anzahl] != NULL) {
anzahl++;
befehle[anzahl] = strtok(NULL, "|");
}
return anzahl;
}
int find_redirect(char **args, int anzahl, char **datei, char *type) {
for (int i = 0; i < anzahl; i++) {
if (strcmp(args[i], ">>") == 0) {
*type = 'a';
*datei = args[i + 1];
args[i] = NULL;
return 1;
} else if (strcmp(args[i], ">") == 0) {
*type = 'o';
*datei = args[i + 1];
args[i] = NULL;
return 1;
} else if (strcmp(args[i], "<") == 0) {
*type = 'i';
*datei = args[i + 1];
args[i] = NULL;
return 1;
}
}
return 0;
}
void parse_alias(char *input) {
input += 6;
input[strcspn(input, "\n")] = '\0';
char *eq = strchr(input, '=');
if (eq == NULL) {
printf(RED "alias: falsches Format. Beispiel: alias ll=\"ls -la\"\n" RESET);
return;
}
if (alias_count >= MAX_ALIASES) {
fprintf(stderr, "Maximale Anzahl Aliases (%d) erreicht\n", MAX_ALIASES);
return;
}
*eq = '\0';
char *name = input;
char *value = eq + 1;
if (strlen(name) > 64) {
fprintf(stderr, "Alias-Name zu lang (max 64 Zeichen)\n");
return;
}
if (strlen(value) > 512) {
fprintf(stderr, "Alias-Wert zu lang (max 512 Zeichen)\n");
return;
}
if (value[0] == '"') {
value++;
value[strlen(value) - 1] = '\0';
}
for (int i = 0; i < alias_count; i++) {
if (strcmp(aliases[i].name, name) == 0) {
free(aliases[i].value);
aliases[i].value = strdup(value);
return;
}
}
aliases[alias_count].name = strdup(name);
aliases[alias_count].value = strdup(value);
alias_count++;
}
char *find_alias(char *name) {
for (int i = 0; i < alias_count; i++) {
if (strcmp(aliases[i].name, name) == 0)
return aliases[i].value;
}
return NULL;
}
int split_semicolon(char *input, char **befehle) {
int anzahl = 0;
befehle[anzahl] = strtok(input, ";");
while (befehle[anzahl] != NULL) {
anzahl++;
befehle[anzahl] = strtok(NULL, ";");
}
return anzahl;
}
int split_and(char *input, char **befehle) {
int anzahl = 0;
char *p = input;
befehle[anzahl++] = p;
while ((p = strstr(p, "&&")) != NULL) {
*p = '\0';
*(p+1) = '\0';
p += 2;
befehle[anzahl++] = p;
}
return anzahl;
}
int split_or(char *input, char **befehle) {
int anzahl = 0;
char *p = input;
befehle[anzahl++] = p;
while ((p = strstr(p, "||")) != NULL) {
*p = '\0';
*(p+1) = '\0';
p += 2;
befehle[anzahl++] = p;
}
return anzahl;
}
// ~ durch HOME ersetzen
void expand_tilde(char **args) {
char *home = getenv("HOME");
if (!home) return;
static char bufs[MAX_ARGS][1024];
for (int i = 0; args[i] != NULL; i++) {
if (args[i][0] == '~') {
snprintf(bufs[i], sizeof(bufs[i]), "%s%s", home, args[i] + 1);
args[i] = bufs[i];
}
}
}
// Splittet einen String nur by Space – keine Glob Expansion wie parse()
void parse_simple(char *cmd, char **args) {
int i = 0;
char *token = strtok(cmd, " ");
while (token != NULL) {
args[i++] = token;
token = strtok(NULL, " ");
}
// execvp erwartet NULL am Ende
args[i] = NULL;
}
void expand_variables(char **args){
static char buffer[MAX_ARGS][1024];
for (int i = 0; args[i] != NULL; i++){
const char* source = args[i];
if (!strchr(source, '$')) continue;
char* destination = buffer[i];
size_t remaining = sizeof(buffer[i]) -1;
while (*source && remaining > 0){
if (*source == '$'){
// '$' überspringen
source++;
char name[256];
size_t n = 0;
while (*source && (isalnum((unsigned char)*source) || *source == '_')) {
if (n + 1 < sizeof(name)) {
name[n++] = *source;
}
source++;
}
name[n] = '\0';
// Wert holen, Wenn !Wert -> leerer String
const char* val = getenv(name);
if(!val) val = "";
while(*val && remaining > 0){
*destination++ = *val++;
remaining--;
}
}
else{
*destination++ = *source++;
remaining--;
}
}
// String terminieren
*destination = '\0';
args[i] = buffer[i];
}
}