-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.c
More file actions
154 lines (131 loc) · 3.83 KB
/
Copy pathexpr.c
File metadata and controls
154 lines (131 loc) · 3.83 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
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <stdbool.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
#include "expr.h"
#include "redirect.h"
#include "memory.h"
#include "error.h"
#define INITIAL_ARGV_SIZE 10
#define MAX_ARG_LENGTH 4096
extern CommandList* command_list;
Command* create_command(void) {
Command* cmd = rmalloc(sizeof(Command));
if (cmd == NULL) {
print_error(_SLIT("Command creation failed"));
exit(EXIT_FAILURE);
}
memset(cmd, 0, sizeof(Command));
cmd->redirects.data = rcalloc(1, sizeof(Redirect));
cmd->redirects.capacity = 1;
cmd->argv = create_array(sizeof(string));
return cmd;
}
bool add_argument(Command* cmd, const string arg) {
if (cmd == NULL || string__is_null_or_empty(arg)) return false;
if (cmd->argv.size >= INT_MAX - 1) {
print_error(_SLIT("Maximum number of arguments exceeded"));
return false;
}
array_push(&cmd->argv, (string*)&arg);
return true;
}
bool add_redirect(Command* cmd, RedirectType type, int fd, const string target) {
if (cmd == NULL || string__is_null_or_empty(target) || cmd->redirects.size >= MAX_REDIRECTS) return false;
if (cmd->redirects.size >= cmd->redirects.capacity) {
size_t new_capacity = cmd->redirects.capacity * 2;
cmd->redirects.data = rrealloc(cmd->redirects.data, new_capacity * sizeof(Redirect));
cmd->redirects.capacity = new_capacity;
}
Redirect* redirect = &cmd->redirects.data[cmd->redirects.size];
redirect->type = type;
redirect->fd = fd;
redirect->target = string__from(target);
redirect->is_fd = (target.str[0] >= '0' && target.str[0] <= '9' && target.str[1] == '\0');
cmd->redirects.size++;
return true;
}
bool add_pipeline(Command* __restrict cmd, Command* __restrict next) {
if (cmd == NULL || next == NULL) return false;
Command* last = cmd;
while (last->next != NULL) {
last = last->next;
}
last->next = next;
last->pipline_next = true;
return true;
}
CommandList* create_command_list(void) {
CommandList* list = rcalloc(1, sizeof(CommandList));
if (list == NULL) {
print_error(_SLIT("Command list creation failed"));
exit(EXIT_FAILURE);
}
return list;
}
bool add_command(CommandList* list, Command* cmd) {
if (list == NULL || cmd == NULL) return false;
if (list->head == NULL) {
list->head = cmd;
list->tail = cmd;
} else {
list->tail->next = cmd;
list->tail = cmd;
}
return true;
}
bool append_command_list(CommandList* __restrict dest, CommandList* __restrict src) {
if (dest == NULL || src == NULL) return false;
if (dest->head == NULL) {
dest->head = src->head;
dest->tail = src->tail;
} else if (src->head != NULL) {
dest->tail->next = src->head;
dest->tail = src->tail;
}
src->head = NULL;
src->tail = NULL;
return true;
}
void free_command(Command* cmd) {
if (cmd == NULL) return;
for (size_t i = 0; i < cmd->argv.size; i++)
string__free(*(string*)array_checked_get(cmd->argv, i));
array_free(&cmd->argv);
for (size_t i = 0; i < cmd->redirects.size; i++)
string__free(cmd->redirects.data[i].target);
rfree(cmd->redirects.data);
memset(cmd, 0, sizeof(Command));
rfree(cmd);
}
void free_command_list(CommandList* list) {
if (list == NULL) return;
Command* current = list->head;
while (current != NULL) {
Command* next = current->next;
free_command(current);
current = next;
}
memset(list, 0, sizeof(CommandList));
rfree(list);
}
void set_pipeline_background(CommandList* list) {
if (list == NULL) return;
for (Command* cmd = list->head; cmd != NULL; cmd = cmd->next) {
cmd->background = true;
}
}
void set_command_background(Command* cmd) {
if (cmd != NULL) {
cmd->background = true;
}
}