Newer
Older
/*
** ast_cmd.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Fri Aug 18 22:13:51 2006 Seblu
** Last update Fri Sep 1 00:44:22 2006 Seblu
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
*/
#include "ast.h"
s_ast_node *ast_cmd_create(void)
{
s_ast_node *node;
secmalloc(node, sizeof (s_ast_node));
node->type = T_CMD;
node->body.child_cmd.argv = NULL;
node->body.child_cmd.prefix = NULL;
node->body.child_cmd.redirs = NULL;
return node;
}
void ast_cmd_add_redir(s_ast_node *node,
e_redir_type type,
int fd,
char *word)
{
s_redir *red;
s_redir **this;
if (node->type != T_CMD)
return;
secmalloc(red, sizeof (s_redir));
red->type = type;
red->fd = fd;
red->word = word;
red->next = NULL;
for (this = &node->body.child_cmd.redirs; *this; *this = (*this)->next)
; //do nothing
*this = red;
}
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
void ast_cmd_add_prefix(s_ast_node *node, char *assignment_word)
{
if (node->type != T_CMD)
return;
size_t size = 0;
if (node->body.child_cmd.prefix)
while (node->body.child_cmd.prefix[size])
++size;
secrealloc(node->body.child_cmd.prefix, node->body.child_cmd.prefix,
(++size + 1) * sizeof (char *));
node->body.child_cmd.prefix[size - 1] = assignment_word;
node->body.child_cmd.prefix[size] = NULL;
}
void ast_cmd_add_argv(s_ast_node *node, char *argv)
{
if (node->type != T_CMD)
return;
size_t size = 0;
if (node->body.child_cmd.argv)
while (node->body.child_cmd.argv[size])
++size;
secrealloc(node->body.child_cmd.argv, node->body.child_cmd.argv,
(++size + 1) * sizeof (char *));
node->body.child_cmd.argv[size - 1] = argv;
node->body.child_cmd.argv[size] = NULL;
}
void ast_cmd_print(s_ast_node *node, FILE *fs, unsigned int *node_id)
{
unsigned cur_id = *node_id;
if (node->type != T_CMD)
return;
fprintf(fs, "%u [label = \"Command\"];\n", *node_id);
//prefix
char **prefix = node->body.child_cmd.prefix;
if (prefix && prefix[0]) {
++*node_id;
fprintf(fs, "%u [label = \"", *node_id);
for (int i = 0; prefix && prefix[i]; ++i) {
fprintf(fs, "prefix[%d]=%s\\n", i, prefix[i]);
}
fprintf(fs, "\"];\n");
fprintf(fs, "%u -> %u\n", cur_id, *node_id);
}
//arguments
char **argv = node->body.child_cmd.argv;
if (argv && argv[0]) {
++*node_id;
fprintf(fs, "%u [label = \"", *node_id);
for (int i = 0; argv && argv[i]; ++i)
fprintf(fs, "argv[%d]=%s\\n", i, argv[i]);
fprintf(fs, "\"];\n");
fprintf(fs, "%u -> %u\n", cur_id, *node_id);
}
//redirs
if (node->body.child_cmd.redirs) {
int i = 0;
++*node_id;
fprintf(fs, "%u [label = \"", *node_id);
for (s_redir *this = node->body.child_cmd.redirs; this; this = this->next, ++i)
fprintf(fs, "redirs[%d]: fd=%d, type=%d, word=%s\\n", i, this->fd, this->type, this->word);
fprintf(fs, "\"];\n");
fprintf(fs, "%u -> %u\n", cur_id, *node_id);
}
}
void ast_cmd_destruct(s_ast_node *node)
{
s_redir *this, *buf;
if (node->type != T_CMD)
return;
if (node->body.child_cmd.argv) {
for (register int i = 0; node->body.child_cmd.argv[i]; ++i)
free(node->body.child_cmd.argv[i]);
free(node->body.child_cmd.argv);
}
if (node->body.child_cmd.prefix) {
for (register int i = 0; node->body.child_cmd.prefix[i]; ++i)
free(node->body.child_cmd.prefix[i]);
free(node->body.child_cmd.prefix);
}