-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
196 lines (173 loc) · 4.54 KB
/
parse.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "parse.h"
int main()
{
Param_t * param = createParam(); //Create new Parameter
param = stringtokenizer(param); //get commands and tokenize
deleteParam(param);
return 0;
}
Param_t * createParam()
{
Param_t * param = (Param_t *)malloc(sizeof(Param_t) +1); //Allocate enough space for new parameter (+1 byte for error reasons?)
param = clearParam(param); //clear/reset parameter values
return param; //return new cleared/allocated parameter
}
Param_t * clearParam(Param_t * param)
{
param->inputRedirect = '\0'; //set filename '\0', don't use NULL because you will lose the only reference you had to memory and can't free it.
param->outputRedirect = '\0'; //set filename to an empty string
param->background = 0; //clear bg
param->argumentCount = 0; //reset argc
int i;
//loop through all argv's
for(i = 0; i < MAXARGS; i++)
{
param->argumentVector[i] = '\0'; //set all argv's in array to empty strings
}
return param; //return cleared param
}
Param_t * deleteParam(Param_t * param)
{
free(param);
return param;
}
Param_t * stringtokenizer(Param_t * param)
{
int streamch, buff = 8, currsize = 0;
prompt(); //display prompt sequence
currsize = buff;
char *inputstr = (char*)malloc(sizeof(char)*currsize); //allocate enough initial space for the input char array
if(inputstr!=NULL)//Test to see if the space was allocated
{
int i = 0;
while((streamch=getchar())!='\n')
{
*(inputstr+i++)=(char)streamch; //convert streamch to char and save it in memory
//reallocate more space until we're done scanning
if(i==currsize)
{
currsize+=buff;
inputstr = (char*)realloc(inputstr,sizeof(char)*currsize);
}
}
inputstr[i] = '\0'; //add end of sequence character
}
printf("Testing input string: %s\n",inputstr); //check the input string
param = stringbreaker(inputstr,param); //tokenize the input string
return param;
}
//display prompt sequence
void prompt()
{
printf("$$$ ");
}
Param_t * stringbreaker(char*str, Param_t *param)
{
char *t; //create new temporary character array
char *stringcount[MAXARGS]; //create array of character pointers
int i=0;
t=strtok(str," \n\t"); //tokenize first argument based on where " ", "\t", and "\n" are located
printf("Temp String: %s\n",t); //print the temporary string
//tokenize the rest of the string
while(t != NULL)
{
printf("\nThe token is: %s", t);
stringcount[i] = t; //add temp char to string array
param->argumentCount++; //increment argc
t = strtok(NULL," \n\t"); //tokenize
i++;
}
parseParams(stringcount,param); //
printParams(param); //print params, returns nothing
//theshell(param) //fork to new process id, show child process and wait
param = clearParam(param);
return param;
}
void printParams(Param_t * param)
{
int i;
//not started
printf ("InputRedirect: [%s]\n", (param->inputRedirect != NULL) ? param->inputRedirect:"NULL");
//not started
printf ("OutputRedirect: [%s]\n", (param->outputRedirect != NULL) ? param->outputRedirect:"NULL");
//not started
printf ("Background: [%d]\n", param->background);
//finished
printf ("ArgumentCount: [%d]\n", param->argumentCount);
for (i = 0; i < param->argumentCount; i++)
printf("ArgumentVector[%2d]: [%s]\n", i, param->argumentVector[i]);
}
Param_t *parseParams(char **strings, Param_t *param)
{
char *t;
int i;
for(i = 0; i < param->argumentCount; i++)
{
t = strings[i];
param = parseStrings(t, param); //parse each string command to see what it does
}
//param = setArgV(param);
return param;
}
Param_t *parseStrings(char* str, Param_t *param)
{
//test input redirect
if(isInputRedirect(str))
{
if(param->inputRedirect != NULL)
printf("input redirect is already filled");
else
param->inputRedirect = str;
}
//test output redirect
else if(isOutputRedirect(str))
{
if(param->outputRedirect != NULL)
printf("output redirect is already filled");
else
param->outputRedirect = str;
}
//test background
else if(isBackground(str))
{
param->background = 1;
}
else
{
//add all other parameters to the argument vector
//param->argumentvector[i]
}
return param;
}
//test to see if there is an input redirect
int isInputRedirect(char *t)
{
if(t[0]=='>' && t[1] != '\0')
return 1;
else
return 0;
}
//test to see if there is an output redirect
int isOutputRedirect(char *t)
{
if(t[0]=='<' && t[1] != '\0')
return 1;
else
return 0;
}
//test to see if there is a backgrounding
int isBackground(char *t)
{
int i=0;
while(t[i++]!='\0')
{
if(t[i] == '&')
return 1;
else
return 0;
}
}