-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
63 lines (57 loc) · 1.79 KB
/
parser.c
File metadata and controls
63 lines (57 loc) · 1.79 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
#include "parser.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static char exit_buf[16];
void expand_variables(char **args) {
for (int i = 0; args[i] != NULL; i++) {
if (args[i][0] != '$') continue;
char *name = args[i] + 1;
if (strcmp(name, "?") == 0) {
snprintf(exit_buf, sizeof(exit_buf), "%d", last_exit_status);
args[i] = exit_buf;
} else {
char *val = getenv(name);
args[i] = val ? val : "";
}
}
}
int tokenize(char *input, char **args, Redirect *redir) {
redir->infile = NULL;
redir->outfile = NULL;
redir->append = 0;
redir->background = 0;
int count = 0;
char *saveptr;
char *token = strtok_r(input, " \t", &saveptr);
while (token != NULL && count < MAX_ARGS - 1) {
if (strcmp(token, ">>") == 0) {
redir->outfile = strtok_r(NULL, " \t", &saveptr);
redir->append = 1;
} else if (strcmp(token, ">") == 0) {
redir->outfile = strtok_r(NULL, " \t", &saveptr);
} else if (strcmp(token, "<") == 0) {
redir->infile = strtok_r(NULL, " \t", &saveptr);
} else {
args[count++] = token;
}
token = strtok_r(NULL, " \t", &saveptr);
}
if (count > 0 && strcmp(args[count-1], "&") == 0) {
redir->background = 1;
args[--count] = NULL;
}
args[count] = NULL;
return count;
}
int parse_pipeline(char *input, Command cmds[]) {
int num_cmds = 0;
char *saveptr;
char *segment = strtok_r(input, "|", &saveptr);
while (segment != NULL && num_cmds < MAX_PIPES) {
tokenize(segment, cmds[num_cmds].args, &cmds[num_cmds].redir);
num_cmds++;
segment = strtok_r(NULL, "|", &saveptr);
}
return num_cmds;
}