-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecho_implement.c
More file actions
166 lines (158 loc) · 3.38 KB
/
Copy pathecho_implement.c
File metadata and controls
166 lines (158 loc) · 3.38 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
155
156
157
158
159
160
161
162
163
164
165
166
#include "echo_implement.h"
#include <string.h>
void checkEcho(char *buffer)
{
unsigned long bufsize = 0;
char *iter = buffer;
iter = iter + 5;
int pbufsize = 10;
int *statePtr = malloc(sizeof(int));
*statePtr = 0;
char *printBuffer;
printBuffer = echoOutLine(statePtr, iter);
// printf("%s", printBuffer);
char **allOut = (char**)malloc(sizeof(char*)*2);
int all_bufsize = 2;
int position = 0;
allOut[position++] = printBuffer;
// printf("%d\n", state);
while(*statePtr == 2 || *statePtr == 3)
{
printf(">");
getline(&buffer, &bufsize, stdin);
printBuffer = echoOutLine(statePtr, buffer);
allOut[position++] = printBuffer;
if(position >= all_bufsize)
{
all_bufsize = position + all_bufsize;
// printf("%d - Updated All buf\n", all_bufsize );
allOut = (char **)realloc(allOut, all_bufsize * sizeof(char*));
if (!allOut) {
fprintf(stderr, "Shell: allOut reallocation error\n");
exit(EXIT_FAILURE);
}
}
}
int i;
for (i = 0; i < position; ++i)
{
printf("%s", allOut[i]);
free(allOut[i]);
}
}
char* echoOutLine(int *statePtr, char *iter)
{
char *printBuffer = (char*)malloc(sizeof(char)*1000);
int *position = (int *)malloc(sizeof(int));
int state = *statePtr;
*position = 0;
char now = *(iter);
while(now != '\0')
{
switch(state)
{
case 0:
switch(now)
{
case 34:
state = 2;
break;
case 39:
state = 3;
break;
case 32:
break;
default:
printBuffer = addToPrintBuf(printBuffer, now, position);
// printf("%c", now);
state = 1;
break;
}
break;
case 1:
switch(now)
{
case 34:
state = 2;
break;
case 39:
state = 3;
break;
case 32:
printBuffer = addToPrintBuf(printBuffer, now, position);
// printf("%c", now);
state = 0;
break;
default:
printBuffer = addToPrintBuf(printBuffer, now, position);
// printf("%c",now);
break;
}
break;
case 2:
switch(now)
{
case 34:
state = 1;
break;
default:
printBuffer = addToPrintBuf(printBuffer, now, position);
// printf("%c", now);
break;
}
break;
case 3:
switch(now)
{
case 39:
state = 1;
break;
default:
printBuffer = addToPrintBuf(printBuffer, now, position);
// printf("%c", now);
break;
}
break;
}
iter = iter + 1;
now = *(iter);
// printf("%d\n", strlen(printBuffer) );
}
// return state;
*(statePtr) = state;
printBuffer[*(position)] = '\0';
return printBuffer;
}
char* addToPrintBuf(char *printBuffer, char now, int *position)
{
int pbufsize = 1000;
char c = now;
printBuffer[*(position)] = c;
*(position) = *(position) + 1;
// If we have exceeded the buffer, reallocate.
if (*(position) >= pbufsize) {
pbufsize = *(position) + pbufsize;
printBuffer = (char*)realloc(printBuffer, pbufsize*sizeof(char));
if (!printBuffer) {
fprintf(stderr, "Shell: printBuffer reallocation error\n");
exit(EXIT_FAILURE);
}
}
return printBuffer;
}
int run_echo(char **args)
{
int i;
// printf("Testing2\n");
char *buffer = malloc(sizeof(char) * 1000);
// printf("Testing3\n");
strcpy(buffer,"echo");
for ( i = 1; args[i] != NULL; ++i)
{
strcat(buffer," ");
strcat(buffer,args[i]);
}
strcat(buffer,"\n");
checkEcho(buffer);
return 1;
}