-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.c
207 lines (195 loc) · 4.72 KB
/
command.c
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
#include <glob.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "command.h"
#include "prompt.h"
void clear_cmd(Command *c)
{
c->argc = 0;
c->stdout_append = 0;
c->stderr_append = 0;
c->stdout_redir = 0;
c->stderr_redir = 0;
memset(c->stdin_redir_f, 0, sizeof(c->stdin_redir_f));
memset(c->stdout_redir_f, 0, sizeof(c->stdout_redir_f));
memset(c->stderr_redir_f, 0, sizeof(c->stderr_redir_f));
c->dont_wait = 0;
c->pipe_in = 0;
c->pipe_out = 0;
c->pipe_to = 0;
c->piped_from = 0;
c->and_to = 0;
c->and_from = 0;
c->or_to = 0;
c->or_from = 0;
c->next_cmd = 0;
c->prev_cmd = 0;
c->condition = 0;
c->cond_cmd = 0;
c->abort = 0;
}
void handle_redirects(Command *c)
{
if (*c->stdin_redir_f)
{
int fd = open(c->stdin_redir_f, O_RDONLY);
dup2(fd, STDIN_FILENO);
close(fd);
}
if (c->stdout_redir)
{
dup2(c->stdout_redir, STDOUT_FILENO);
}
if (*c->stdout_redir_f)
{
int append = c->stdout_append ? O_APPEND : 0;
int fd = open(c->stdout_redir_f, O_WRONLY|O_CREAT|append, 0644);
dup2(fd, STDOUT_FILENO);
close(fd);
}
if (c->stderr_redir)
{
dup2(c->stderr_redir, STDERR_FILENO);
}
if (*c->stderr_redir_f)
{
int append = c->stderr_append ? O_APPEND : 0;
int fd = open(c->stderr_redir_f, O_WRONLY|O_CREAT|append, 0644);
dup2(fd, STDERR_FILENO);
close(fd);
}
}
Command *next_cmd(Command *c)
{
if (c->pipe_to)
return c->pipe_to;
else if (c->and_to)
{
if (get_exit_code() == 0)
return c->and_to;
else
return next_cmd(c->and_to);
}
else if (c->or_to)
{
if (get_exit_code() == 0)
return next_cmd(c->or_to);
else
return c->or_to;
}
else if (c->next_cmd)
return c->next_cmd;
else
return NULL;
}
void expand(Command *c)
{
glob_t globbuf;
int i, j;
int argc = 0;
int flags = GLOB_TILDE | GLOB_NOCHECK;
char **copy = (char **) calloc(sizeof(c->argv), sizeof(char *));
memcpy(copy, c->argv, sizeof(c->argv));
for (i = 0; copy[i]; ++i)
{
glob(copy[i], flags, NULL, &globbuf);
for (j = 0; j < globbuf.gl_pathc; ++j)
{
free(c->argv[argc]);
c->argv[argc++] = strdup(globbuf.gl_pathv[j]);
}
globfree(&globbuf);
}
c->argc = argc;
free(copy);
}
void handle_pipes(Command *c)
{
if (c->pipe_out)
{
dup2(c->pipe_out, STDOUT_FILENO);
}
if (c->pipe_in)
{
dup2(c->pipe_in, STDIN_FILENO);
}
}
void free_cmds(Command *c)
{
if (c == NULL)
return;
free_cmds(c->pipe_to);
free_cmds(c->and_to);
free_cmds(c->or_to);
free_cmds(c->next_cmd);
free_cmds(c->condition);
free_cmds(c->cond_cmd);
int i;
for (i = 0; i < c->argc; ++i)
{
free(c->argv[i]);
}
free(c);
}
void print_cmd(Command *c)
{
printf("Command at %p\n", c);
int i;
for (i = 0; i < c->argc; ++i)
{
printf("argv[%d]: %s\n", i, c->argv[i]);
}
printf("argc: %d\n", c->argc);
printf("stdin_redir_f: %s\n"
"stdout_redir: %d\n"
"stderr_redir: %d\n"
"stdout_redir_f: %s\n"
"stderr_redir_f: %s\n"
"stdout_append: %d\n"
"stderr_append: %d\n",
c->stdin_redir_f, c->stdout_redir, c->stderr_redir,
c->stdout_redir_f, c->stderr_redir_f,
c->stdout_append, c->stderr_append);
printf("Don't wait: %d\n", c->dont_wait);
printf("Pipes to: %p\n", c->pipe_to);
printf("Piped from: %p\n", c->piped_from);
printf("And to: %p\n", c->and_to);
printf("And from: %p\n", c->and_from);
printf("Or to: %p\n", c->or_to);
printf("Or from: %p\n", c->or_from);
printf("Next cmd: %p\n", c->next_cmd);
printf("Prev cmd: %p\n", c->prev_cmd);
printf("Abort: %d\n", c->abort);
printf("Condition: %p\n", c->condition);
printf("Cond Cmd: %p\n", c->cond_cmd);
printf("-----------------------------\n");
}
void print_cmds(Command *c)
{
if (c == NULL)
return;
print_cmd(c);
print_cmds(c->pipe_to);
print_cmds(c->and_to);
print_cmds(c->or_to);
print_cmds(c->next_cmd);
print_cmds(c->condition);
print_cmds(c->cond_cmd);
}
void apply_dont_wait(Command *c)
{
if (c == NULL)
return;
c->dont_wait = 1;
apply_dont_wait(c->pipe_to);
apply_dont_wait(c->and_to);
apply_dont_wait(c->or_to);
apply_dont_wait(c->next_cmd);
apply_dont_wait(c->condition);
apply_dont_wait(c->cond_cmd);
}