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
|
#include "ast.h"
#include "lexer.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
int execute_command(struct AST *ast, int input_fd);
int execute_binary(struct AST *ast, int input_fd) {
char *program = ast->val.string;
struct AST *child = ast->children;
char *argv[100];
argv[0] = program;
int i = 1;
for (; child; i++, child = child->next) {
argv[i] = child->val.string;
}
argv[i] = NULL;
int in = input_fd;
int out = STDOUT_FILENO;
int slave_input = -1;
int file_out_fd;
if (ast->file_out) {
file_out_fd =
open(ast->file_out,
O_WRONLY | O_CREAT | ((ast->file_out_append) ? O_APPEND : O_TRUNC),
0666);
}
if (ast->pipe_rhs) {
int fds[2];
pipe(fds);
out = fds[1];
slave_input = fds[0];
}
int pid = fork();
if (0 == pid) {
if (slave_input >= 0)
close(slave_input);
dup2(in, STDIN_FILENO);
dup2(out, STDOUT_FILENO);
if (ast->file_out)
dup2(file_out_fd, ast->file_out_fd_to_use);
execvp(program, argv);
exit(1);
}
if (ast->file_out)
close(file_out_fd);
if (ast->pipe_rhs) {
if (out >= 0)
close(out);
return execute_command(ast->pipe_rhs, slave_input);
}
int rc;
// FIXME: Should use waitpid ... when my OS supports that
wait(&rc);
return rc;
}
int execute_command(struct AST *ast, int input_fd) {
char *program = ast->val.string;
if (0 == strcmp(program, "cd")) {
struct AST *child = ast->children;
char *directory;
if (!child)
directory = "~";
else
directory = child->val.string;
int rc = chdir(directory);
if (-1 == rc) {
perror("cd");
return 1;
}
return 0;
}
return execute_binary(ast, input_fd);
}
void execute_ast(struct AST *ast) {
int rc = -1;
for (; ast;) {
if (AST_COMMAND == ast->type) {
rc = execute_command(ast, STDIN_FILENO);
} else if (AST_CONDITIONAL_AND == ast->type) {
if (0 != rc) {
ast = ast->next;
if (!ast)
break;
}
} else if (AST_CONDITIONAL_NOT == ast->type) {
if (0 == rc) {
ast = ast->next;
if (!ast)
break;
}
}
ast = ast->next;
}
}
char *get_line(void) {
char *str = malloc(1024);
char *p = str;
int rc;
for (;;) {
if (0 == (rc = read(0, p, 1))) {
continue;
}
if (0 > rc) {
perror("read");
continue;
}
if (8 == *p) {
if (p == str)
continue;
putchar(*p);
p--;
continue;
}
putchar(*p);
if ('\n' == *p) {
break;
}
p++;
}
p++;
*p = '\0';
return str;
}
int sh_main(int argc, char **argv) {
(void)argc;
(void)argv;
for (;;) {
printf("/ : ");
char *line = get_line();
{
struct TOKEN *h = lex(line);
struct AST *ast_h = generate_ast(h);
execute_ast(ast_h);
free_tokens(h);
free_ast(ast_h);
}
free(line);
}
return 0;
}
|