From ffec14bf49df2c3e54bff37ada68359623d3f848 Mon Sep 17 00:00:00 2001 From: Pin Date: Thu, 31 Mar 2022 00:19:41 -0400 Subject: [PATCH] wip --- Makefile | 2 +- msh.c | 129 +++++++++++++++++++++++++++++++++++++++++++++---------- msh.h | 5 ++- 3 files changed, 111 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index cf43eff..b567a2b 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ build: output_dir gcc -Wall -std=gnu99 ${RELEASE_ARGS} ${SOURCES} ${LIBRARIES} ${OUTPUT:PROG=mash} debug: output_dir - gcc -Wall -std=gnu99 ${SOURCES} ${LIBRARIES} ${OUTPUT:PROG=mash} + gcc -Wall -std=gnu99 -g ${SOURCES} ${LIBRARIES} ${OUTPUT:PROG=mash} output_dir: mkdir -p ${OUTPUT_DIR} diff --git a/msh.c b/msh.c index 8fa00dc..e96d176 100644 --- a/msh.c +++ b/msh.c @@ -1,5 +1,8 @@ +#define _GNU_SOURCE + #include #include +#include #include #include #include @@ -10,7 +13,10 @@ #include "msh.h" #define clear() printf("\033[H\033[J") -#define MATHLEN 10 +#define MATHLEN 100000 + +static char *prompt = NULL; +static bool skipMath = false; union pipe { int fileDesc[2]; @@ -23,43 +29,86 @@ union pipe { static char *builtinFunctions[] = { "exit", "author", - "cd" + "cd", + "nomorenumbers" }; int (*builtinFunc[]) (char **) = { &builtinExit, &builtinAuthor, - &builtinCD + &builtinCD, + &builtinNMN }; -int builtinExit() { +int builtinExit(char **args) { + rl_clear_history(); + free(args); exit(EXIT_SUCCESS); } -int builtinAuthor() { +int builtinAuthor(char **args) { static const char* message = "Author: Spencer\n" "Description: A shell to promote math!\n" "Ask for the source code!\n" "\n"; printf("%s", message); + free(args); return 0; } int builtinCD(char **dir) { int status = 0; - if (dir != NULL) { - if (chdir(dir[1]) != 0) { + char *oldPWD = getenv("PWD"); + char *changeDir = malloc(sizeof(char *)); + if (dir[1] != NULL) { + char c = *dir[1]; + switch (c) { + case '~': + changeDir = realloc(changeDir, strlen(getenv("HOME") + strlen(dir[1]))); + strcpy(changeDir, getenv("HOME")); + strcat(changeDir, dir[1]+1); + break; + case '-': + changeDir = realloc(changeDir, strlen(getenv("OLDPWD"))); + strcpy(changeDir, getenv("OLDPWD")); + break; + default: + changeDir = dir[1]; + break; + } + if (chdir(changeDir) != 0) { status = 1; printf("bash: cd: error\n"); + } else { + setenv("PWD", dir[1], 1); + setenv("OLDPWD", oldPWD, 1); + free(prompt); + prompt = NULL; } } else { - status = 1; - printf("bash: cd: error\n"); + if (chdir(getenv("HOME")) != 0) { + status = 1; + printf("Bash: cd: error\n"); + } else { + setenv("PWD", getenv("HOME"), 1); + setenv("OLDPWD", oldPWD, 1); + free(prompt); + prompt = NULL; + } } + free(changeDir); + changeDir = NULL; + free(dir); return status; } +int builtinNMN(char **args) { + skipMath = true; + free(args); + return 0; +} + void failedTest() { static const char* message = "\n" @@ -91,18 +140,21 @@ int runCommand(char *command) { return status; } -char **splitCMD(char *line) { +char **splitCMD(const char *line) { size_t size = 8; size_t pos = 0; - char **args = calloc(0, size * sizeof(char*)); + char **args = malloc(size * sizeof(char*)); char *arg = NULL; + char *lineTest = NULL; + lineTest = strdup(line); + if(line == NULL) { *args=NULL; return args; } - arg = strtok(line, " \t\r\n\a"); + arg = strtok(lineTest, " \t\r\n\a"); while (arg != NULL) { args[pos] = arg; pos++; @@ -126,6 +178,7 @@ int execCommand(char *command) { return (*builtinFunc[i])(commandARGS); } } + free(commandARGS); return runCommand(command); } @@ -165,7 +218,6 @@ char *getPrompt() { size_t len = 0; int status = 0; - static char *prompt = NULL; static size_t promptCap = 0; pipe(input.fileDesc); @@ -224,17 +276,51 @@ int randomNum(int limit) { int mathTest() { int status = 0; - int numAnswer = 0; + long numAnswer = 0; int num1 = randomNum(MATHLEN); int num2 = randomNum(MATHLEN); + long numSolution = 0; char *numLine = NULL; - printf("%d + %d = ", num1, num2); - numLine = readline(""); - sscanf(numLine, "%d", &numAnswer); + + const char mathOperation[] = {'+', '-', '*', '/'}; + + int randProblem = randomNum(3); + + if (skipMath) { + return 0; + } + + for(int i = 0; i < 1; i++) { + switch (mathOperation[randProblem]) { + case '+': + numSolution = num1 + num2; + break; + case '-': + numSolution = num1 - num2; + break; + case '*': + numSolution = num1 * num2; + break; + case '/': + numSolution = num1 / num2; + break; + + } + #ifndef RELEASEBUILD + printf("Answer: %ld\n", numSolution); + #endif + printf("%d %c %d = ", num1, mathOperation[randProblem], num2); + numLine = readline(""); + sscanf(numLine, "%ld", &numAnswer); + + if (numSolution != numAnswer) { // If wrong answer + status = 1; + } + } #ifdef RELEASEBUILD printf("Calculating"); - for (int i = 0; i < 3; i++){ + for (int i = 0; i < 5; i++){ printf("."); fflush(stdout); sleep(2); @@ -242,10 +328,6 @@ int mathTest() { printf("\n"); #endif - if ((num1 + num2) != numAnswer) { // If wrong answer - status = 1; - } - free(numLine); return status; } @@ -267,6 +349,8 @@ void startShell() { } free(line); } + free(prompt); + prompt = NULL; } void handleBreak(){ @@ -281,5 +365,6 @@ int main() { initShell(); signal(SIGINT, handleBreak); startShell(); + rl_clear_history(); exit(EXIT_SUCCESS); } diff --git a/msh.h b/msh.h index 4fa1224..5ac7419 100644 --- a/msh.h +++ b/msh.h @@ -1,3 +1,4 @@ -int builtinExit(); -int builtinAuthor(); +int builtinExit(char **args); +int builtinAuthor(char **args); int builtinCD(char **dir); +int builtinNMN(char **args);