Commit 56a8de21 authored by Matthew Burket's avatar Matthew Burket

Basic shell stuff with readline

parent c4f75935
cmake_minimum_required(VERSION 3.9)
cmake_minimum_required(VERSION 3.3)
project(coms352_shell C)
set(CMAKE_C_STANDARD 11)
add_executable(coms352_shell main.c)
\ No newline at end of file
# https://stackoverflow.com/a/33451936
include_directories(/usr/include/readline)
add_executable(coms352_shell main.c tokenize.c tokenize.h)
target_link_libraries(coms352_shell readline)
......@@ -150,6 +150,33 @@ main.c.s:
$(MAKE) -f CMakeFiles/coms352_shell.dir/build.make CMakeFiles/coms352_shell.dir/main.c.s
.PHONY : main.c.s
tokenize.o: tokenize.c.o
.PHONY : tokenize.o
# target to build an object file
tokenize.c.o:
$(MAKE) -f CMakeFiles/coms352_shell.dir/build.make CMakeFiles/coms352_shell.dir/tokenize.c.o
.PHONY : tokenize.c.o
tokenize.i: tokenize.c.i
.PHONY : tokenize.i
# target to preprocess a source file
tokenize.c.i:
$(MAKE) -f CMakeFiles/coms352_shell.dir/build.make CMakeFiles/coms352_shell.dir/tokenize.c.i
.PHONY : tokenize.c.i
tokenize.s: tokenize.c.s
.PHONY : tokenize.s
# target to generate assembly for a file
tokenize.c.s:
$(MAKE) -f CMakeFiles/coms352_shell.dir/build.make CMakeFiles/coms352_shell.dir/tokenize.c.s
.PHONY : tokenize.c.s
# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
......@@ -162,6 +189,9 @@ help:
@echo "... main.o"
@echo "... main.i"
@echo "... main.s"
@echo "... tokenize.o"
@echo "... tokenize.i"
@echo "... tokenize.s"
.PHONY : help
......
......@@ -44,6 +44,7 @@
<Option type="1"/>
<Option compiler="gcc"/>
<Compiler>
<Add directory="/usr/include/readline"/>
<Add directory="/usr/lib/gcc/x86_64-redhat-linux/7/include"/>
<Add directory="/usr/local/include"/>
<Add directory="/usr/include"/>
......@@ -62,6 +63,7 @@
<Option type="1"/>
<Option compiler="gcc"/>
<Compiler>
<Add directory="/usr/include/readline"/>
<Add directory="/usr/lib/gcc/x86_64-redhat-linux/7/include"/>
<Add directory="/usr/local/include"/>
<Add directory="/usr/include"/>
......@@ -77,6 +79,12 @@
<Unit filename="/home/mburket/CLionProjects/coms352_shell/main.c">
<Option target="coms352_shell"/>
</Unit>
<Unit filename="/home/mburket/CLionProjects/coms352_shell/tokenize.c">
<Option target="coms352_shell"/>
</Unit>
<Unit filename="/home/mburket/CLionProjects/coms352_shell/tokenize.h">
<Option target="coms352_shell"/>
</Unit>
<Unit filename="/home/mburket/CLionProjects/coms352_shell/CMakeLists.txt">
<Option virtualFolder="CMake Files\"/>
</Unit>
......
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "tokenize.h"
#define TRUE 0
int main() {
printf("Hello, World!\n");
return 0;
// Read line examples: https://eli.thegreenplace.net/2016/basics-of-using-the-readline-library/
int should_run = 1;
char *readBuff;
fprintf(stdout, "Welcome to Burket Shell (bsh)\n");
fprintf(stdout, "Copyright 2018 Matthew Burket\n");
fprintf(stdout, "Released under the GPL v2 License\n");
while (should_run) {
while ((readBuff = readline("bsh >> ")) != NULL) {
if (strlen(readBuff) > 0) {
add_history(readBuff);
}
if (strcmp("exit", readBuff) == TRUE) {
should_run = 0;
exit(EXIT_SUCCESS);
}
char **tokens = malloc(sizeof(char) * 5000);
tokenize(readBuff, tokens);
if (fork() == 0) {
int returnCode = execvp(tokens[0], tokens);
printf("Return code %d\n", returnCode);
} else {
wait(NULL);
}
free(readBuff);
}
}
}
\ No newline at end of file
//
// Created by mburket on 2/26/18.
//
#include "tokenize.h"
char **tokenize(char *string, char **output) {
char *strtok_save;
char *del = " ";
output[0] = strtok_r(string, del, &strtok_save);
char *token = output[0];
int i = 1;
while (token != NULL) {
token = strtok_r(NULL, del, &strtok_save);
output[i] = token;
i++;
}
return output;
}
//
// Created by mburket on 2/26/18.
//
#ifndef COMS352_SHELL_TOKENIZE_H
#define COMS352_SHELL_TOKENIZE_H
#include <string.h>
char **tokenize(char *string, char **output);
#endif //COMS352_SHELL_TOKENIZE_H
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