-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject2b.c
193 lines (184 loc) · 4.42 KB
/
project2b.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
/* CS370 - Ross Rydman - Project 2 */
/*
Full submission
*/
#include<termios.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#define BUFF_SIZE 256
#define ARG_COUNT 20
#define space ' '
#define bs '\b'
void forkPro(char **command){
unsigned int pid = fork();
int status;
int errno;
if (pid == -1){
printf("Could not fork!");
} else if (pid == 0){
close(2);
dup(1);
int success = execvp(command[0], command);
if (success == -1)
printf("Command not found!\n");
exit(errno);
} else {
waitpid(pid, &status, WUNTRACED);
}
}
void splitString(char *str, char **splitstr)
{
char *p;
int i = 0;
p = strtok(str," \n");
while(p != NULL)
{
splitstr[i] = malloc(strlen(p) + 1);
if (splitstr[i]){
strcpy(splitstr[i], p);
}
i++;
p = strtok (NULL, " \n");
}
}
int main(void)
{
int exited = 0;
int broken = 0;
int printedHistory = 0;
char* history[10]; // command history array
int histCounter = 0;
// fill history array
int i;
for (i=0; i<10; i++){
history[i] = (char*)malloc(BUFF_SIZE);
}
struct termios origConfig;
// Get original config
tcgetattr(0, &origConfig);
// Copy config
struct termios newConfig = origConfig;
// Adjust config
newConfig.c_lflag &= ~(ICANON|ECHO);
newConfig.c_cc[VERASE] = 0;
tcflush(0, TCIFLUSH);
// Set the new config
tcsetattr(0, TCSANOW, &newConfig);
while (exited != 1) {
broken = 0;
// print cwd shell prompt
if (printedHistory == 0){
char *cwdbuf = (char *)malloc(100);
getcwd(cwdbuf,100);
printf("%s -> ", cwdbuf);
fflush(stdout);
free(cwdbuf);
}
char inbuffer[BUFF_SIZE];
int bytesRead = 0;
char *c = malloc(sizeof(char));
while (broken == 0){
read(0, c, 1); // Read one character
if (*c == 8 | *c == 127) { // Handle bs and del keys correctly
if (bytesRead == 0) continue; // Dont touch prompt
write(2, c, 1);
write(2, space, 1);
write(2, c, 1);
} else if (*c == 27){ // Handle updownleftright correctly
read(0, c, 1);
if (*c == 91){
read(0,c,1);
if (*c == 65){
broken = 3; // up arrow
while (bytesRead > 0){
write(2, bs, 1);
write(2, space, 1);
write(2, bs, 1);
bytesRead--;
}
}
if (*c == 66) {
broken = 4; // down arrow
while (bytesRead > 0){
write(2, bs, 1);
write(2, space, 1);
write(2, bs, 1);
bytesRead--;
}
}
if (*c == 67) continue; // left arrow
if (*c == 68) continue; // right arrow
continue;
}
} else {
write(2, c, 1);
}
// Break on line feed
if (*c == '\n'){
broken = 2;
}
// Write or delete char to line buffer
if (*c == 8 | *c == 127){
--bytesRead;
inbuffer[bytesRead] = '\0';
} else {
inbuffer[bytesRead] = *c;
bytesRead++;
inbuffer[bytesRead] = '\0';
}
}
// Create tokenized command
char *command[ARG_COUNT] = {NULL};
splitString(inbuffer, command);
if (command[0] == NULL) continue; // Nothing typed - loop again
// store buffer to history
if (histCounter == 9){
for(i=1;i<10;i++){
history[i-1] = history[i];
strcpy(history[histCounter], *command);
}
} else {
strcpy(history[histCounter], *command);
histCounter++;
}
if (broken > 2){
// Up arrow
if (broken == 3){
printf("%s", history[0]);
fflush(stdout);
//forkPro(history[0]);
}
// Down arrow
if (broken == 4){
}
printf("\nCould not get history to work right in time. So close, yet so far.\n");
fflush(stdout);
//printedHistory = 1;
}
else {
// if cd command then change working directory
if (strcmp(command[0], "cd") == 0) {
int error = chdir(command[1]);
if (error == -1)
perror("Error changing directory.\n");
}
// if exit command, break
else if (strcmp(command[0], "exit") == 0) {
exited = 1;
continue;
}
// if not cd or exit, execute command
else {
forkPro(command);
}
printedHistory = 0;
}
}
// restore input config
tcsetattr(0, TCSANOW, &origConfig);
return 0;
}