This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-command-backup.c
executable file
·288 lines (218 loc) · 5.44 KB
/
read-command-backup.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
279
280
281
282
283
284
285
286
287
288
// UCLA CS 111 Lab 1 command reading
#include "command.h"
#include "command-internals.h"
#include "alloc.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
//Structure Definitions
struct command_stream {
command_t command;
command_stream_t next;
command_stream_t prev;
};
struct token {
token_type type;
char* word;
};
struct token_list {
token t;
token_list_t next;
token_list_t prev;
};
///////////////
/* Create a command stream from LABEL, GETBYTE, and ARG. A reader of
the command stream will invoke GETBYTE (ARG) to get the next byte.
GETBYTE will return the next input byte, or a negative number
(setting errno) on failure. */
bool isValidChar(char c)
{
return (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9'));
}
bool isValidOp(char c)
{
return (c == '|' || c == '&' || c == '(' || c == ')' || c == '<' || c == '>' );
/*(c == '!' || c == '%' || c == '+' || c == ',' || c == '-' || c == '.' || c == '/' ||
c == ':' || c == '@' || c == '^' || c == '-' ||
c == ';' || c == '|' || c == '&' || c == '(' || c == ')' || c == '<' || c == '>' ||
c == '\n' || c == '\t' || c == ' ' || c == '#') ; */
}
token_list_t create_token_list(char* buffer)
{
token_list_t head = NULL;
head = (token_list_t)checked_malloc(sizeof(token_list_t));
head->next = NULL;
head->prev = NULL;
token_list_t traverse = (token_list_t)checked_malloc(sizeof(token_list_t));
token_type type;
int iter = 0;
char current;
char next;
while ((buffer[iter] != '\0') && (buffer[iter] != EOF))
{
current = buffer[iter];
next = buffer[iter + 1];
switch (current)
{
//Skip blank spaces and new lines
case '\t':
case ' ':
type = OTHER;
//iter++;
continue;
case ';':
type = SEMICOLON;
break;
case '\n':
type = NEWLINE;
break;
case '|':
if (current == next)
type = OR;
else
type = PIPE;
break;
case '&':
if (current == next)
{
type = AND;
iter++;
}
else
type = OTHER;
break;
case '(':
type = RIGHT_PAREN;
break;
case ')':
type = LEFT_PAREN;
break;
case '<':
type = LEFT_ARROW;
break;
case '>':
type = RIGHT_ARROW;
break;
default:
type = OTHER;
break;
}
int len = 0;
token temp;
temp.type = NULL;
//Case that the token is a word
if (isValidChar(current))
{
type = WORD;
//Gets the length of the word within the buffer.
while (isValidChar(buffer[iter + len]) && buffer[iter + len++] != EOF);
//Allocates memory for the string stored in the token, accounting for NULL byte at end.
temp.word = (char *)checked_malloc((len * sizeof(char)) + 1);
int i;
//Stores the word from the buffer into the token
for (i = 0; i < len; i++)
{
temp.word[i] = buffer[iter + i];
}
//NULL terminates the token
temp.word[len] = '\0';
//Increments iter by the length of the string
iter += len - 1;
}
else if (type == OTHER)
{
error(1, 0, "Unable to tokenize string");
exit(1);
}
temp.type = type;
if ( (head->next) == NULL)
{
//Store the temp token
traverse->t = temp;
//
traverse->next = (token_list_t)checked_malloc(sizeof(token_list_t));
traverse = traverse->next;
traverse->prev = head;
head->next = traverse;
}
else
{
traverse->t = temp;
traverse->next = (token_list_t)checked_malloc(sizeof(token_list_t));
traverse->prev = traverse;
traverse = traverse->next;
}
iter++;
}
traverse->next = NULL;
return head;
}
char* create_buffer(int(*get_next_byte) (void *), void *get_next_byte_argument)
{
size_t iter = 0;
size_t buffer_size = 1024;
char *buffer = (char *)checked_malloc(buffer_size);
char current_byte;
//Iterates through entire file
while ((current_byte = get_next_byte(get_next_byte_argument)) != EOF)
{
//Skips over characters included in a comment
if (current_byte == '#')
{
while ((current_byte = get_next_byte(get_next_byte_argument)) != '\n')
{
if (current_byte == EOF)
break;
//fprintf(stderr, "%d", current_byte);
//Continue iterating until the end of the comment is reached.
}
continue;
}
buffer[iter++] = current_byte;
if (iter == buffer_size)
buffer = (char *)checked_grow_alloc(buffer, &buffer_size);
}
buffer[iter] = '\0';
fprintf(stderr, "%s", buffer);
return buffer;
}
command_stream_t
make_command_stream(int(*get_next_byte) (void *),
void *get_next_byte_argument)
{
/* Method of Implementation
1.Read the file into a Buffer
2.Process the buffer into a linked list of commands and operators
3. Convert list into command trees.
4. Print Tree's into correct format.
*/
//Read the file into the buffer
char *buffer;
buffer = create_buffer(get_next_byte, get_next_byte_argument);
//Process the buffer into a linked list of commands and operators.
token_list_t list = create_token_list(buffer);
int iter = 0;
//Test Output
/*while (list->next != NULL)
{
if (list->t.type == WORD)
printf("%s", list->t.word);
list = list->next;
}
*/
//while (isValidChar(buffer[iter++]));
//fprintf(stderr, "%d", buffer[--iter]);
error(1, 0, "command reading not yet implemented");
return 0;
}
//TODO LATER
/* Read a command from STREAM; return it, or NULL on EOF. If there is
an error, report the error and exit instead of returning. */
command_t
read_command_stream(command_stream_t s)
{
/* FIXME: Replace this with your implementation too. */
error(1, 0, "command reading not yet implemented");
return 0;
}