wip
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
*.out
|
*.out
|
||||||
|
bin/
|
||||||
|
|||||||
17
Makefile
17
Makefile
@@ -1,9 +1,18 @@
|
|||||||
LIBRARIES = -lreadline
|
LIBRARIES = -lreadline
|
||||||
RELEASE_ARGS = -DRELEASEBUILD
|
RELEASE_ARGS = -DRELEASEBUILD
|
||||||
SOURCES = ./msh.c
|
SOURCES = ./msh.c
|
||||||
|
OUTPUT_DIR = ./bin
|
||||||
|
OUTPUT_BIN = ${OUTPUT_DIR}/PROG
|
||||||
|
OUTPUT = -o ${OUTPUT_BIN}
|
||||||
|
|
||||||
build:
|
build: output_dir
|
||||||
gcc -Wall -std=gnu99 ${RELEASE_ARGS} ${SOURCES} ${LIBRARIES}
|
gcc -Wall -std=gnu99 ${RELEASE_ARGS} ${SOURCES} ${LIBRARIES} ${OUTPUT:PROG=mash}
|
||||||
|
|
||||||
debug:
|
debug: output_dir
|
||||||
gcc -Wall -std=gnu99 ${SOURCES} ${LIBRARIES}
|
gcc -Wall -std=gnu99 ${SOURCES} ${LIBRARIES} ${OUTPUT:PROG=mash}
|
||||||
|
|
||||||
|
output_dir:
|
||||||
|
mkdir -p ${OUTPUT_DIR}
|
||||||
|
|
||||||
|
install: build
|
||||||
|
mv ${OUTPUT:PROG=mash} /usr/sbin/mash
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -0,0 +1,10 @@
|
|||||||
|
**IMPORTANT** this shell is **NOT** meant to be used for any serious applications.
|
||||||
|
This is for academic purposes only!
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```
|
||||||
|
make
|
||||||
|
make install
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
65
msh.c
65
msh.c
@@ -10,6 +10,7 @@
|
|||||||
#include "msh.h"
|
#include "msh.h"
|
||||||
|
|
||||||
#define clear() printf("\033[H\033[J")
|
#define clear() printf("\033[H\033[J")
|
||||||
|
#define MATHLEN 10
|
||||||
|
|
||||||
union pipe {
|
union pipe {
|
||||||
int fileDesc[2];
|
int fileDesc[2];
|
||||||
@@ -21,12 +22,14 @@ union pipe {
|
|||||||
|
|
||||||
static char *builtinFunctions[] = {
|
static char *builtinFunctions[] = {
|
||||||
"exit",
|
"exit",
|
||||||
"author"
|
"author",
|
||||||
|
"cd"
|
||||||
};
|
};
|
||||||
|
|
||||||
int (*builtinFunc[]) (char *) = {
|
int (*builtinFunc[]) (char **) = {
|
||||||
&builtinExit,
|
&builtinExit,
|
||||||
&builtinAuthor
|
&builtinAuthor,
|
||||||
|
&builtinCD
|
||||||
};
|
};
|
||||||
|
|
||||||
int builtinExit() {
|
int builtinExit() {
|
||||||
@@ -43,6 +46,20 @@ int builtinAuthor() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int builtinCD(char **dir) {
|
||||||
|
int status = 0;
|
||||||
|
if (dir != NULL) {
|
||||||
|
if (chdir(dir[1]) != 0) {
|
||||||
|
status = 1;
|
||||||
|
printf("bash: cd: error\n");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
status = 1;
|
||||||
|
printf("bash: cd: error\n");
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
void failedTest() {
|
void failedTest() {
|
||||||
static const char* message =
|
static const char* message =
|
||||||
"\n"
|
"\n"
|
||||||
@@ -74,10 +91,39 @@ int runCommand(char *command) {
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char **splitCMD(char *line) {
|
||||||
|
size_t size = 8;
|
||||||
|
size_t pos = 0;
|
||||||
|
char **args = calloc(0, size * sizeof(char*));
|
||||||
|
char *arg = NULL;
|
||||||
|
|
||||||
|
if(line == NULL) {
|
||||||
|
*args=NULL;
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
arg = strtok(line, " \t\r\n\a");
|
||||||
|
while (arg != NULL) {
|
||||||
|
args[pos] = arg;
|
||||||
|
pos++;
|
||||||
|
|
||||||
|
if (pos >= size) {
|
||||||
|
size += 1;
|
||||||
|
args = realloc(args, size * sizeof(char*));
|
||||||
|
|
||||||
|
}
|
||||||
|
arg = strtok(NULL, " \t\r\n\a");
|
||||||
|
}
|
||||||
|
|
||||||
|
args[pos] = NULL;
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
int execCommand(char *command) {
|
int execCommand(char *command) {
|
||||||
|
char **commandARGS = splitCMD(command);
|
||||||
for (size_t i = 0; i < (sizeof(builtinFunctions) / sizeof(char *)); i++) {
|
for (size_t i = 0; i < (sizeof(builtinFunctions) / sizeof(char *)); i++) {
|
||||||
if (strcmp(command, builtinFunctions[i]) == 0) {
|
if (strcmp(commandARGS[0], builtinFunctions[i]) == 0) {
|
||||||
return (*builtinFunc[i])(command);
|
return (*builtinFunc[i])(commandARGS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return runCommand(command);
|
return runCommand(command);
|
||||||
@@ -92,6 +138,7 @@ void initShell() {
|
|||||||
// Setting random time seed
|
// Setting random time seed
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
runCommand("uname -a");
|
runCommand("uname -a");
|
||||||
|
|
||||||
fd = fopen("/etc/motd", "r");
|
fd = fopen("/etc/motd", "r");
|
||||||
@@ -104,10 +151,10 @@ void initShell() {
|
|||||||
c = fgetc(fd);
|
c = fgetc(fd);
|
||||||
}
|
}
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
} else {
|
|
||||||
printf("MOTD is empty\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,8 +225,8 @@ int randomNum(int limit) {
|
|||||||
int mathTest() {
|
int mathTest() {
|
||||||
int status = 0;
|
int status = 0;
|
||||||
int numAnswer = 0;
|
int numAnswer = 0;
|
||||||
int num1 = randomNum(10);
|
int num1 = randomNum(MATHLEN);
|
||||||
int num2 = randomNum(10);
|
int num2 = randomNum(MATHLEN);
|
||||||
char *numLine = NULL;
|
char *numLine = NULL;
|
||||||
printf("%d + %d = ", num1, num2);
|
printf("%d + %d = ", num1, num2);
|
||||||
numLine = readline("");
|
numLine = readline("");
|
||||||
|
|||||||
Reference in New Issue
Block a user