Repository navigation
Expand file tree
/
Copy pathparser.c
More file actions
121 lines (97 loc) · 2.63 KB
/
Copy pathparser.c
File metadata and controls
121 lines (97 loc) · 2.63 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
#include "k2sh.h"
char* input_string(FILE* fp, size_t size) {
int buf_size = BUF_SIZE;
int pos = 0;
int ch;
char* buffer = malloc(sizeof(char) * buf_size);
if (!buffer) {
fprintf(stderr, "input_string: allocation error\n");
exit(EXIT_FAILURE);
}
while (1) {
ch = getchar();
if (ch == EOF || ch == '\n') {
buffer[pos] = '\0';
break;
} else {
buffer[pos] = ch;
pos++;
}
if (pos >= buf_size) {
buf_size += BUF_SIZE;
buffer = realloc(buffer, buf_size);
if (!buffer) {
fprintf(stderr, "input_string: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
return buffer;
}
char **split_args(char *input) {
char **args = malloc(TOKEN_BUF_SIZE * sizeof(char *));
int arg_index = 0;
const char *p = input;
while (*p) {
while (isspace(*p)) p++;
if (*p == '\0') break;
char quote = 0;
const char *start = p;
char *token = malloc(1024);
int k = 0;
if (*p == '"' || *p == '\'') {
quote = *p++;
while (*p && *p != quote) token[k++] = *p++;
p++; // skip closing quote
} else {
while (*p && !isspace(*p)) token[k++] = *p++;
}
token[k] = '\0';
args[arg_index++] = token;
}
args[arg_index] = NULL;
return args;
}
char*** parse_pipe(int* num_cmds, char* line) {
int cur_num = NUM_CMDS;
char** commands = malloc(cur_num * sizeof(char*));
if (!commands) {
perror("allocation error");
exit(EXIT_FAILURE);
}
int count = 0;
char* token = strtok(line, "|");
while (token != NULL) {
while (*token == ' ') {
token++;
}
commands[count++] = token;
if (count >= cur_num) {
cur_num += NUM_CMDS;
commands = realloc(commands, cur_num * sizeof(char*));
if (!commands) {
perror("allocation error");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, "|");
}
*num_cmds = count;
char*** split_commands = malloc(cur_num * sizeof(char**));
for (int i = 0; i < count; i++) {
split_commands[i] = split_args(commands[i]);
}
free(commands);
return split_commands;
}
void free_commands(char*** commands, int num_cmds) {
if (!commands) {
return;
}
for (int i = 0; i < num_cmds; i++) {
if (commands[1]) {
free(commands[i]);
}
}
free(commands);
}