This commit is contained in:
Ayham Mamoun 2019-02-01 08:40:40 +03:00
parent 0fbb6dd5fb
commit ac6e1d2c3d
9 changed files with 191 additions and 58 deletions

2
.vscode/tasks.json vendored
View File

@ -5,6 +5,6 @@
"tasks": [{
"label": "Build",
"type": "shell",
"command": "cmake . && make"
"command": "clear && cmake . && make"
}]
}

View File

@ -9,67 +9,16 @@ set(CMAKE_MAKEFILE_DEPENDS
"CMakeCache.txt"
"CMakeFiles/3.10.2/CMakeCCompiler.cmake"
"CMakeFiles/3.10.2/CMakeSystem.cmake"
"CMakeFiles/feature_tests.c"
"CMakeLists.txt"
"/usr/share/cmake-3.10/Modules/CMakeCCompiler.cmake.in"
"/usr/share/cmake-3.10/Modules/CMakeCCompilerABI.c"
"/usr/share/cmake-3.10/Modules/CMakeCInformation.cmake"
"/usr/share/cmake-3.10/Modules/CMakeCommonLanguageInclude.cmake"
"/usr/share/cmake-3.10/Modules/CMakeCompilerIdDetection.cmake"
"/usr/share/cmake-3.10/Modules/CMakeDetermineCCompiler.cmake"
"/usr/share/cmake-3.10/Modules/CMakeDetermineCompileFeatures.cmake"
"/usr/share/cmake-3.10/Modules/CMakeDetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/CMakeDetermineCompilerABI.cmake"
"/usr/share/cmake-3.10/Modules/CMakeDetermineCompilerId.cmake"
"/usr/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake"
"/usr/share/cmake-3.10/Modules/CMakeFindBinUtils.cmake"
"/usr/share/cmake-3.10/Modules/CMakeGenericSystem.cmake"
"/usr/share/cmake-3.10/Modules/CMakeLanguageInformation.cmake"
"/usr/share/cmake-3.10/Modules/CMakeParseImplicitLinkInfo.cmake"
"/usr/share/cmake-3.10/Modules/CMakeSystem.cmake.in"
"/usr/share/cmake-3.10/Modules/CMakeSystemSpecificInformation.cmake"
"/usr/share/cmake-3.10/Modules/CMakeSystemSpecificInitialize.cmake"
"/usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake"
"/usr/share/cmake-3.10/Modules/CMakeTestCompilerCommon.cmake"
"/usr/share/cmake-3.10/Modules/CMakeUnixFindMake.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/ADSP-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/ARMCC-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/AppleClang-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Borland-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Bruce-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/CMakeCommonCompilerMacros.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Clang-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Clang-DetermineCompilerInternal.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Compaq-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Cray-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Embarcadero-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Fujitsu-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/GHS-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/GNU-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/GNU-C-FeatureTests.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/GNU-C.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/GNU-FindBinUtils.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/GNU.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/HP-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/IAR-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/IBMCPP-C-DetermineVersionInternal.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Intel-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/MIPSpro-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/MSVC-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/NVIDIA-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/OpenWatcom-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/PGI-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/PathScale-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/SCO-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/SDCC-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/SunPro-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/TI-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/TinyCC-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/VisualAge-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/Watcom-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/XL-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Compiler/zOS-C-DetermineCompiler.cmake"
"/usr/share/cmake-3.10/Modules/Internal/FeatureTesting.cmake"
"/usr/share/cmake-3.10/Modules/Platform/Linux-GNU-C.cmake"
"/usr/share/cmake-3.10/Modules/Platform/Linux-GNU.cmake"
"/usr/share/cmake-3.10/Modules/Platform/Linux.cmake"
@ -84,9 +33,6 @@ set(CMAKE_MAKEFILE_OUTPUTS
# Byproducts of CMake generate step:
set(CMAKE_MAKEFILE_PRODUCTS
"CMakeFiles/3.10.2/CMakeSystem.cmake"
"CMakeFiles/3.10.2/CMakeCCompiler.cmake"
"CMakeFiles/3.10.2/CMakeCCompiler.cmake"
"CMakeFiles/CMakeDirectoryInformation.cmake"
)

View File

@ -1 +0,0 @@
empty

View File

@ -1 +0,0 @@
empty

View File

@ -1 +0,0 @@
2

View File

@ -7,4 +7,14 @@
#IncludeRegexTransform:
/home/ayham-mamoun/Desktop/git/lsh/src/main.c
stdio.h
-
stdlib.h
-
string.h
-
sys/unistd.h
-
sys/wait.h
-

Binary file not shown.

BIN
bin/lsh Executable file

Binary file not shown.

View File

@ -0,0 +1,180 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/wait.h>
#define LSH_RL_BUFSIZE 1024
char* lsh_read_line(void) {
int bufsize = LSH_RL_BUFSIZE;
int position = 0;
char* buffer = malloc(sizeof(char) * bufsize);
int c;
if (!buffer) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace if with a null characeter and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else
buffer[position] = c;
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += LSH_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}
#define LSH_TOK_BUFSIZE 64
#define LSH_TOK_DELIM " \t\r\n\a"
char** lsh_split_line(char* line) {
int bufsize = LSH_TOK_BUFSIZE, position = 0;
char** tokens = malloc(bufsize * sizeof(char*));
char* token;
if (!tokens) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, LSH_TOK_DELIM);
while (token != NULL) {
tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += LSH_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, LSH_TOK_DELIM);
}
tokens[position] = NULL;
return tokens;
}
int lsh_launch(char** args) {
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) {
perror("lsh");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("lsh");
} else {
// Parent process
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
/*
Function Declarations for builtin shell commands:
*/
int lsh_cd(char** args);
int lsh_help(char** args);
int lsh_exit(char** args);
/*
List of builtin commands, followed by their corresponding functions
*/
char* builtin_str[] = {"cd", "help", "exit"};
int (*builtin_func[])(char**) = {
&lsh_cd,
&lsh_help,
&lsh_exit,
};
int lsh_num_builtins() { return sizeof(builtin_str) / sizeof(char*); }
int lsh_cd(char** args) {
if (args[1] == NULL) {
fprintf(stderr, "lsh: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
perror("lsh");
}
}
return 1;
}
int lsh_help(char** args) {
int i;
for (i = 0; i < lsh_num_builtins(); i++) {
printf(" %s\n", builtin_str[i]);
}
printf("Use the man command for information on other programs. \n");
return 1;
}
int lsh_exit(char** args) { return 0; }
int lsh_execute(char** args) {
int i;
if (args[0] == NULL)
// An empty command was entered.
return 1;
for (i = 0; i < lsh_num_builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0)
return (*builtin_func[i])(args);
}
return lsh_launch(args);
}
void lsh_loop(void) {
char* line;
char** args;
int status;
do {
printf("> ");
line = lsh_read_line();
args = lsh_split_line(line);
status = lsh_execute(args);
free(line);
free(args);
} while (status);
}
int main(int argc, char** argv) {
// Load config files, if amy.
// Run command loop.
lsh_loop();
// Perform any shutdown/cleanup.
return EXIT_SUCCESS;
}