-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_list.c
278 lines (258 loc) · 6.13 KB
/
cmd_list.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* cmd_list.c
*
* Implementation of the command list module.
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmd_list.h"
// Size of string buffers:
#define BUFFER_SIZE 256
// Line token delimiter:
#define DELIMITER " "
// List node struct:
struct cmd_node_struct
{
struct cmd_node_struct *next;
struct cmd_entry_struct *entry;
};
// List struct:
struct cmd_list_struct
{
struct cmd_node_struct *head;
struct cmd_node_struct *tail;
};
// Parser function, see below:
static struct cmd_entry_struct *parse_line(char *line);
// Free memory allocated for the node/entry, see below:
static void free_node(struct cmd_node_struct *node);
// Create command list:
struct cmd_list_struct *create_cmd_list(char *file_name)
{
FILE *input_file;
struct cmd_list_struct *list;
struct cmd_node_struct *new_node;
struct cmd_entry_struct *new_entry;
char line_buf[BUFFER_SIZE];
// Open input file:
if ((input_file = fopen(file_name, "r")) == NULL )
{
return NULL ;
}
// Allocate and init list struct:
if ((list = malloc(sizeof(struct cmd_list_struct))) == NULL )
{
return NULL ;
}
list->head = NULL;
list->tail = NULL;
// Read file line by line until EOF:
while (fgets(line_buf, BUFFER_SIZE, input_file) != NULL )
{
// If line is empty or a comment, just read the next line
if (line_buf[0] == '#' || line_buf[0] == '\n') {
continue;
}
// Parse read line:
if ((new_entry = parse_line(line_buf)) == NULL )
{
return NULL ;
}
// Allocated and init list node:
if ((new_node = malloc(sizeof(struct cmd_node_struct))) == NULL )
{
return NULL ;
}
new_node->next = NULL;
new_node->entry = new_entry;
// Insert node in list:
if (list->head != NULL )
{
list->tail->next = new_node;
list->tail = new_node;
}
else
{
list->head = new_node;
list->tail = new_node;
}
}
return list;
}
// Destroy list and free memory:
void destroy_cmd_list(struct cmd_list_struct *list)
{
struct cmd_node_struct *node, *next_node;
// Non-empty list:
if (list->head != NULL )
{
node = list->head;
// Free all nodes and command entries:
while (node != NULL )
{
next_node = node->next;
free_node(node);
node = next_node;
}
}
// Free list:
free(list);
}
// Get first entry in list:
struct cmd_entry_struct *get_first(struct cmd_list_struct *list)
{
// Return NULL if list is empty:
if (list->head == NULL )
{
return NULL ;
}
// Return first entry:
return list->head->entry;
}
// Remove first entry in list:
void remove_first(struct cmd_list_struct *list)
{
struct cmd_node_struct *new_head;
// Do nothing if list is empty:
if (list->head == NULL )
{
return;
}
// Free head node:
new_head = list->head->next;
free_node(list->head);
list->head = new_head;
// Set tail NULL if list is now empty:
if (list->head == NULL )
{
list->tail = NULL;
}
}
// Free memory allocated to the node:
static void free_node(struct cmd_node_struct *node)
{
int arg_index;
free(node->entry->dir);
free(node->entry->cmd);
// Free all arguments from argv:
arg_index = 0;
while (node->entry->argv[arg_index] != NULL )
{
free(node->entry->argv[arg_index]);
arg_index++;
}
free(node->entry->argv);
free(node->entry);
free(node);
}
// Parse contents of the specified line and create command entry:
static struct cmd_entry_struct *parse_line(char *line)
{
int token_count, arg_count, arg_index, str_length;
char *token, *line_break;
char parse_buf[BUFFER_SIZE];
struct cmd_entry_struct *new_entry;
// Copy line to parse buffer:
strcpy(parse_buf, line);
// Count number of tokens:
if (strtok(parse_buf, DELIMITER) != NULL )
{
token_count = 1;
}
else
{
return NULL ;
}
while (strtok(NULL, DELIMITER) != NULL )
{
token_count++;
}
// A properly formatted line has at least 4 (incl. class) tokens:
if (token_count < 4)
{
return NULL ;
}
// Allocate entry struct:
if ((new_entry = malloc(sizeof(struct cmd_entry_struct))) == NULL )
{
return NULL ;
}
// Copy line to parse buffer:
strcpy(parse_buf, line);
// Find and replace line break (if present):
if ((line_break = strchr(parse_buf, '\n')) != NULL )
{
*line_break = '\0';
}
// Parse start time:
token = strtok(parse_buf, DELIMITER);
new_entry->start_time = atoi(token);
// Parse class
token = strtok(NULL, DELIMITER);
new_entry->class = atoi(token);
// Parse working dir:
token = strtok(NULL, DELIMITER);
str_length = strlen(token) + 1;
if ((new_entry->dir = malloc(sizeof(char) * str_length)) == NULL )
{
return NULL ;
}
strcpy(new_entry->dir, token);
// Parse command:
token = strtok(NULL, DELIMITER);
str_length = strlen(token) + 1;
if ((new_entry->cmd = malloc(sizeof(char) * str_length)) == NULL )
{
return NULL ;
}
strcpy(new_entry->cmd, token);
// Parse argv:
arg_count = token_count - 1;
if ((new_entry->argv = malloc(sizeof(char*) * arg_count)) == NULL )
{
return NULL ;
}
// argv[0] = command:
if ((new_entry->argv[0] = malloc(sizeof(char) * str_length)) == NULL )
{
return NULL ;
}
strcpy(new_entry->argv[0], token);
// Parse remaining arguments (if any):
arg_index = 1;
while ((token = strtok(NULL, DELIMITER)) != NULL )
{
str_length = strlen(token) + 1;
if ((new_entry->argv[arg_index] = malloc(sizeof(char) * str_length))
== NULL )
{
return NULL ;
}
strcpy(new_entry->argv[arg_index], token);
arg_index++;
}
// Set NULL-pointer at last index of argv:
new_entry->argv[arg_index] = NULL;
/* This is some ugly code to redirect stdin/stdout.
* It just makes it possible to do "$ cmd < input.txt".
* Can't handle any arguments other than this.
*
* Oh and you have to choose if you want to redirect stdin OR stdout!
* */
if (*new_entry->argv[1] == '<' && new_entry->argv[2] != NULL) {
new_entry->new_stdin = new_entry->argv[2];
free(new_entry->argv[1]);
new_entry->argv[1] = NULL;
}
else if (*new_entry->argv[1] == '>' && new_entry->argv[2] != NULL) {
new_entry->new_stdout = new_entry->argv[2];
free(new_entry->argv[1]);
new_entry->argv[1] = NULL;
}
else {
new_entry->new_stdout = NULL;
new_entry->new_stdin = NULL;
}
// Return entry:
return new_entry;
}