Commit 67fa60fd authored by Matthew Burket's avatar Matthew Burket

Merge branch 'master' of git.assignitapp.com:coms352/shell

parents bcd47a9f f16d1ade
Pipeline #322 failed with stage
......@@ -13,7 +13,7 @@
#define READ 0
void storeHist(char *readBuff, int* historyPos, char historyBuff[50][BUFFSIZE]);
void storeHist(char **readBuff, int *historyPos, char historyBuff[50][5000], int numTokens);
int main() {
// Setup Some Vars
......@@ -52,7 +52,7 @@ int main() {
if (tokens[1] == NULL) {
fprintf(stderr, "bsh: expected one argument for cd");
free(tokens);
storeHist(readBuff, &historyPos, historyBuff);
storeHist(tokens, &historyPos, historyBuff, numTokens);
continue;
} else {
if (strcmp(tokens[1], "~") == 0) {
......@@ -70,52 +70,52 @@ int main() {
if (strcmp(tokens[0], "history") == 0) {
if (historyPos == -1) {
fprintf(stderr, "\nNo commands in history\n");
storeHist(readBuff, &historyPos, historyBuff);
storeHist(tokens, &historyPos, historyBuff, numTokens);
free(tokens);
continue;
}
for (int i = historyPos; i >= 0; i--) {
fprintf(stdout, "%2d %s\n", i + 1, historyBuff[i]);
}
storeHist(readBuff, &historyPos, historyBuff);
storeHist(tokens, &historyPos, historyBuff, numTokens);
free(tokens);
continue;
}
char *ptr = malloc(sizeof(char) * 10);
if (*tokens[0] == '!') {
errno = 0;
// Using strtol allows us to do error checking
long cmdNum = strtol(tokens[0] + 1, &ptr, 10);
if (errno != 0) {
printf("Invalid history command lookup!");
if (strcmp(tokens[0], "!!") == 0) {
if (historyPos == -1) {
fprintf(stderr, "\nbsh: No such command in history.\n");
storeHist(tokens, &historyPos, historyBuff, numTokens);
free(tokens);
free(ptr);
continue;
} else {
strcpy(readBuff, historyBuff[historyPos]);
}
if (cmdNum <= 0 || cmdNum > historyPos) {
printf("Invalid history command lookup!");
}
char *ptr;
if (*tokens[0] == '!' && strcmp(tokens[0], "!!") != 0) {
errno = 0;
// Using strtol allows us to do error checking
*tokens[0] = '0';
long cmdNum = strtol(tokens[0], &ptr, 10);
if (errno != 0) {
printf("\nInvalid history command lookup!\n");
free(tokens);
free(ptr);
continue;
}
free(ptr);
}
if (strcmp(tokens[0], "!!") == 0) {
if (historyPos == -1) {
fprintf(stderr, "\nbsh: No such command in history.\n");
storeHist(readBuff, &historyPos, historyBuff);
if (cmdNum <= 0 || cmdNum-1 > historyPos) {
printf("\nInvalid history command lookup!\n");
free(tokens);
continue;
} else {
strcpy(readBuff, historyBuff[historyPos]);
numTokens = tokenize(historyBuff[cmdNum - 1], tokens);
}
}
storeHist(readBuff, &historyPos, historyBuff);
storeHist(tokens, &historyPos, historyBuff, numTokens);
int runInBackground = 0;
int doMoreCheck = 0;
......@@ -168,12 +168,17 @@ int main() {
}
}
void storeHist(char *readBuff, int* historyPos, char historyBuff[50][BUFFSIZE]) {
void storeHist(char **readBuff, int *historyPos, char historyBuff[50][5000], int numTokens) {
// History store stuff
if (strlen(readBuff) > 0) {
if (*historyPos < 49) {
*historyPos = *historyPos + 1;
strcpy(historyBuff[*historyPos], readBuff);
if (*historyPos < 49) {
*historyPos = *historyPos + 1;
for (int i = 0; i < numTokens - 1; i++) {
strcat(historyBuff[*historyPos], readBuff[i]);
int nextIter = i + 1;
if (nextIter < numTokens - 1) {
strcat(historyBuff[*historyPos], " ");
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment