/
newmain.c
348 lines (329 loc) · 14.1 KB
/
newmain.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
PROGRAM #2
*/
// define libraries
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "defines.h"
#include "node.h"
int main() {
// boolean values
bool needToExecuteProgram, extendVariable, extendAtBack, extendAtFront, didComplete, checkAlias;
// counters
int wordCount, loopCount, tempCount, arrayPosition_x, arrayPosition_y;
// strings
char commandInput[100],tempWord[20],userCommand[10][30], temp[50], temp2[50], temp3[50];
// commands & variables
char *command, *arg1, *arg2, *arg3, *extending;
// declare list heads
struct node *varHP;
struct node *aliasHP;
// init to null
varHP = NULL;
aliasHP = NULL;
char mem[1000];
initialize_globals(mem, 1000);
memset(mem, 0, 1000); //all values need to be initialized to 0.
// get command input
needToExecuteProgram = TRUE;
while (needToExecuteProgram)
{
// print shell prompt
wordCount = 0;
prompt;
fgets(commandInput, 100, stdin);
// For @var to display
if(commandInput[0] == '@') {
int temporary = strlen(commandInput) -2; //to ignore "@" and "\n" in input
char temporary2[temporary];
int x = 1,y = 0;; //skip the @
while(y < temporary) {
if(commandInput[x] == '\n') //break at the newline, we don't want that included
break;
temporary2[y] = commandInput[x];
x++; //temporary[2] will hold the variable name.
y++;
}
temporary2[y] = '\0';
nshFind(varHP, temporary2);
continue;
}
//end of @var display
// break up input string from user into an array of strings to interpret
tempCount = arrayPosition_x = arrayPosition_y = 0;
// loop through each character of the string entered from the command line
for (loopCount=0; loopCount<strlen(commandInput)-1; loopCount++)
{
// if a space is found
if (commandInput[loopCount] == space)
{
// end the temp word
tempWord[tempCount] = EOS;
// place it in the command array
arrayPosition_y = 0;
wordCount++;
while (TRUE)
{
if (tempWord[arrayPosition_y] == EOS)
{
userCommand[arrayPosition_x][arrayPosition_y] = EOS;
break;
}
userCommand[arrayPosition_x][arrayPosition_y] = tempWord[arrayPosition_y];
arrayPosition_y++;
}
// move to get the next word
arrayPosition_x++;
tempCount = 0;
}
// while there are no spaces
else
{
// put the characters in a temporary word until one is found
tempWord[tempCount] = commandInput[loopCount];
tempCount++;
// if there is no space, but its the end of the user's input
if (loopCount == strlen(commandInput)-2)
{
tempWord[tempCount] = EOS;
arrayPosition_y = 0;
wordCount++;
while (TRUE)
{
if (tempWord[arrayPosition_y] == EOS)
{
userCommand[arrayPosition_x][arrayPosition_y] = EOS;
break;
}
userCommand[arrayPosition_x][arrayPosition_y] = tempWord[arrayPosition_y];
arrayPosition_y++;
}
}
}
}
// Implement whatever command was given or print the errors
// if there was input
if (wordCount > 0)
{
// get the command
command = &userCommand[0][0];
// if the command is "set"
if (strcmp(command,"set") == 0 || (matchAlias(aliasHP, "set", command))==TRUE )
{
// check for the correct amount of arguments
if (wordCount-1 <= 2)
{
// display all variables
if (wordCount == 1)
{
printVarList(nshHead(varHP));
}
// display a variable's specific value
else if (wordCount == 2)
{
arg1 = &userCommand[1][0];
nshFind(nshHead(varHP),arg1);
}
// set OR reset a variable
else
{
arg1 = &userCommand[1][0];
arg2 = &userCommand[2][0];
strcpy(temp3,userCommand[2]);
extendVariable = extendAtFront = extendAtBack = FALSE;
// if wanting to extend a variable with the new value on the back
if (temp3[0] == '@')
{
extendAtBack = TRUE;
extendVariable = FALSE;
// look for the '!' ending variable character
for(loopCount=0; loopCount < strlen(temp3); loopCount++)
{
if (temp3[loopCount] == '!')
{
extendVariable = TRUE;
temp[loopCount-1] = EOS;
// get the rest of the string to use to extend variable with
loopCount++;
int i = 0;
for (loopCount; loopCount < strlen(temp3); loopCount++)
{
temp2[i] = temp3[loopCount];
i++;
if (loopCount == (strlen(temp3) -1))
{
temp2[i] = EOS;
arg2 = temp;
arg3 = temp2;
break;
}
}
}
else if (extendVariable == TRUE)
break;
else
{
if (loopCount != 0)
temp[loopCount-1] = temp3[loopCount];
}
}
}
// test to see if user wants to extend the variable on the front of it with the new value
if (extendAtBack == FALSE)
{
extendAtFront = FALSE;
for(loopCount=0; loopCount < strlen(temp3); loopCount++)
{
if (temp3[loopCount] == '@' && temp3[loopCount-1] == '!')
{
int tempSpot, j;
j = 0;
tempSpot = loopCount + 1;
for (tempSpot; tempSpot < strlen(temp3); tempSpot++)
{
temp[j] = temp3[tempSpot];
j++;
}
temp[j] = EOS;
for (tempSpot = 0; tempSpot < (loopCount-1); tempSpot++)
{
temp2[tempSpot] = temp3[tempSpot];
}
temp2[tempSpot] = EOS;
extendAtFront = TRUE;
extendVariable = TRUE;
arg2 = temp;
arg3 = temp2;
break;
}
}
}
// Set or reset a variable
if (extendVariable == FALSE)
{
// if the head is null
if (varHP == NULL){
//varHP = (node*)nshMalloc(sizeof(node));
varHP = (node*) malloc (sizeof(node));
strcpy(varHP->name,&userCommand[1][0]);
strcpy(varHP->value, arg2);
varHP->next = NULL;
}
// if need to find its place in the list
else {
nshInsert(nshHead(varHP), arg1,arg2, VARIABLE);
}
}
else if (extendAtFront == TRUE)
{
// extendVarValue (node *head, char *set_resetValue, char *getVarValue, char *extendWith, int extendingPosition)
didComplete = extendVarValue(nshHead(varHP), arg1, arg2, arg3, varValueInBack);
if (didComplete == FALSE)
printf("Couldn't extend variable, it doesn't exist\n");
}
else {
// extendVarValue (node *head, char *set_resetValue, char *getVarValue, char *extendWith, int extendingPosition)
didComplete = extendVarValue(nshHead(varHP), arg1, arg2, arg3, varValueInFront);
if (didComplete == FALSE)
printf("Couldn't extend variable, it doesn't exist\n");
}
// reset bool values
extendVariable = extendAtFront = extendAtBack = FALSE;
}
}
else
{
printf("Error - To many arguments for the \"set\" command\n");
continue;
}
}
// if the command is "tes"
else if (strcmp(command,"tes") == 0 || (matchAlias(aliasHP, "tes", command))==TRUE )
{
// if there are not to many OR to few arguments for this command
if (wordCount-1 == 1)
{
arg1 = &userCommand[1][0];
int status;
status = nshRemove(varHP,arg1);
if (status == onlyOneNode){
varHP = NULL;
}
else if (status == popHead){
varHP = (node*) nshNext(varHP);
}
}
else
{
printf("Error - To many or to few arguments for the \"tes\" command\n");
}
}
// if the command is "alias"
else if (strcmp(command,"alias") == 0 || (matchAlias(aliasHP, "alias", command))==TRUE )
{
// if wordCount is 1 - display all aliases (External & Internal)
if (wordCount == 1)
{
printVarList(nshHead(aliasHP));
}
// wordCount = 2 - display a specific command alias
else if (wordCount == 2)
{
arg1 = &userCommand[1][0];
nshFind(nshHead(aliasHP),arg1);
}
// wordCout =3 - set/reset a command alias
else
{
// if the new alias is an internal command
arg1 = &userCommand[1][0];
arg2 = &userCommand[2][0];
// if the head is null
if (aliasHP == NULL){
//aliasHP = (node*)nshMalloc(sizeof(node));
aliasHP = (node*) malloc(sizeof(node));
strcpy(aliasHP->name,&userCommand[1][0]);
strcpy(aliasHP->value, arg2);
aliasHP->next = NULL;
}
// if need to find its place in the list
else {
nshInsert(nshHead(aliasHP), arg1,arg2, ALIAS);
}
}
}
// if the command is "saila"
else if (strcmp(command,"saila") == 0 || (matchAlias(aliasHP, "saila", command))==TRUE )
{
if (wordCount == 2)
{
arg1 = &userCommand[1][0];
int status;
status = nshRemove(nshHead(aliasHP),arg1);
if (status == onlyOneNode){
aliasHP = NULL;
}
else if (status == popHead){
aliasHP = (node*) nshNext(aliasHP);
}
}
else {
printf("To many arguments for 'saila' command\n");
}
}
// if the command is "exit"
else if (strcmp(command,"exit") == 0 || (matchAlias(aliasHP, "exit", command))==TRUE )
{
needToExecuteProgram = FALSE;
continue;
}
// if the command is not supported or found
else
{
printf("-bash: %s: external command not supported\n",command);
}
}
}
return END_PROGRAM;
}