summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-05-24 13:05:34 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-05-24 13:05:34 +0200
commit8f33ab3bf8b7d5a256c1d996e572fa67a5578b4f (patch)
tree44c55b4679f8546f940df7191f191ed290c57f6f
parentbed31a7634701b2c19ef8eef3ccb3039c03b1cda (diff)
parent7a9c301d10e001b92846b6b1e64136c10c4bb247 (diff)
downloadbetriebssysteme-8f33ab3bf8b7d5a256c1d996e572fa67a5578b4f.tar.gz
betriebssysteme-8f33ab3bf8b7d5a256c1d996e572fa67a5578b4f.zip
Merge remote-tracking branch 'origin/MOAR_CMAKE' into dev/niklas
-rw-r--r--.gitignore3
-rw-r--r--.gitmodules3
-rw-r--r--.vscode/c_cpp_properties.json3
-rw-r--r--02_exercise/.gitignore1
-rw-r--r--02_exercise/CMakeLists.txt35
-rw-r--r--02_exercise/Makefile2
-rw-r--r--02_exercise/process.c16
-rw-r--r--02_exercise/process_test.c26
-rw-r--r--02_exercise/prompt_utils_test.c4
-rw-r--r--02_exercise/shell.c3
-rw-r--r--CMakeLists.txt40
m---------externals/sanitizers-cmake0
12 files changed, 93 insertions, 43 deletions
diff --git a/.gitignore b/.gitignore
index 02c228d..49fc4a3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@
*_exe
*.d
cmake-build-debug/
-bootloader.bin \ No newline at end of file
+bootloader.bin
+build \ No newline at end of file
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..b49debd
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "externals/sanitizers-cmake"]
+ path = externals/sanitizers-cmake
+ url = git://github.com/arsenm/sanitizers-cmake.git
diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
index d57b434..2796da8 100644
--- a/.vscode/c_cpp_properties.json
+++ b/.vscode/c_cpp_properties.json
@@ -22,7 +22,8 @@
"-MMD",
"-MP",
"-D=_GNU_SOURCE"
- ]
+ ],
+ "configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
diff --git a/02_exercise/.gitignore b/02_exercise/.gitignore
index 5aacf99..819a501 100644
--- a/02_exercise/.gitignore
+++ b/02_exercise/.gitignore
@@ -1,2 +1,3 @@
prog
shell
+build \ No newline at end of file
diff --git a/02_exercise/CMakeLists.txt b/02_exercise/CMakeLists.txt
index ca67d1a..9cae089 100644
--- a/02_exercise/CMakeLists.txt
+++ b/02_exercise/CMakeLists.txt
@@ -6,9 +6,13 @@ set(CMAKE_C_COMPILER gcc)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)
+find_package(Sanitizers)
+
add_executable(prog prog.c)
add_executable(shell shell.c)
target_link_libraries(shell PRIVATE array prompt_utils process)
+
+add_sanitizers(shell)
add_compile_definitions(_GNU_SOURCE)
add_library(array array.c)
@@ -25,33 +29,6 @@ target_link_libraries(process PRIVATE array)
add_executable(process_test process_test.c)
target_link_libraries(process_test PRIVATE process)
-
-
-set(CLANG_WARNINGS
- -Wall
- -Wextra # reasonable and standard
- -Wshadow # warn the user if a variable declaration shadows one from a
- # parent context
- -Wcast-align # warn for potential performance problem casts
- -Wunused # warn on anything being unused
- -Wpedantic # warn if non-standard C++ is used
- -Wconversion # warn on type conversions that may lose data
- -Wsign-conversion # warn on sign conversions
- -Wnull-dereference # warn if a null dereference is detected
- -Wdouble-promotion # warn if float is implicit promoted to double
- -Wformat=2 # warn on security issues around functions that format output
- # (ie printf)
- -Werror
- )
-set(GCC_WARNINGS
- ${CLANG_WARNINGS}
- -Wmisleading-indentation # warn if indentation implies blocks where blocks
- # do not exist
- -Wduplicated-cond # warn if if / else chain has duplicated conditions
- -Wduplicated-branches # warn if if / else branches have duplicated code
- -Wlogical-op # warn about logical operations being used where bitwise were
- # probably wanted
- )
-set(PROJECT_WARNINGS ${GCC_WARNINGS})
target_compile_options(shell INTERFACE ${PROJECT_WARNINGS})
-target_compile_options(prompt_utils INTERFACE ${PROJECT_WARNINGS}) \ No newline at end of file
+target_compile_options(prompt_utils INTERFACE ${PROJECT_WARNINGS})
+target_compile_options(process INTERFACE ${PROJECT_WARNINGS})
diff --git a/02_exercise/Makefile b/02_exercise/Makefile
index 5d1b360..277b597 100644
--- a/02_exercise/Makefile
+++ b/02_exercise/Makefile
@@ -19,7 +19,7 @@ all: $(TAR)
prog: prog.o
$(CC) -o $@ $^
-shell: $(filter-out prog.o,$(OBJ))
+shell: $(filter-out prog.o prompt_utils_test.o process_test.o, $(OBJ))
$(CC) -o $@ $^
run: all
diff --git a/02_exercise/process.c b/02_exercise/process.c
index 8f6589d..fb55236 100644
--- a/02_exercise/process.c
+++ b/02_exercise/process.c
@@ -1,8 +1,8 @@
-#include <wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <wait.h>
#include "array.h"
#include "process.h"
@@ -11,29 +11,33 @@
int parse_command(char const *line, char const *end, process *p) {
char *part;
char **local_parts;
-
if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) {
fprintf(stderr, "Failed to prepare new part / parts array whilst parsing line");
return -1;
}
for (size_t i = 0; line + i < end; ++i) {
- char c = line[i];
- if (c == '\t' || c == ' ' || c == '\0') {
+ char c;
+ switch (c = line[i]) {
+ default:
+ arrayPush(part) = c;
+ break;
+ case ' ':
+ case '\t':
+ case '\0':
if (arrayLen(part) == 0)
continue;
arrayPush(part) = '\0';
arrayPush(local_parts) = part;
arrayInit(part);
- } else {
- arrayPush(part) = c;
}
}
if (arrayLen(part) == 0) {
arrayRelease(part);
} else {
+ arrayPush(part) = '\0';
arrayPush(local_parts) = part;
}
diff --git a/02_exercise/process_test.c b/02_exercise/process_test.c
index 591a06f..d7e59a4 100644
--- a/02_exercise/process_test.c
+++ b/02_exercise/process_test.c
@@ -53,11 +53,33 @@ void test_ls(){
free_processes(&processes);
}
-/*
+void test_multiparse() {
+ Process *processes = NULL;
+ parse_line("echo abcdefg", &processes);
+ assert(arrayLen(processes) == 1);
+ Process p = processes[0];
+ assert(arrayLen(p.argv)==3);
+ assert(p.argc == 2);
+ assert(strcmp(p.argv[0], "echo") == 0);
+ assert(strcmp(p.argv[1], "abcdefg") == 0);
+ assert(p.argc[p.argv] == NULL);
+ free_processes(&processes);
+ parse_line("echo abc", &processes);
+ assert(arrayLen(processes) == 1);
+ p = processes[0];
+ assert(arrayLen(p.argv)==3);
+ assert(p.argc == 2);
+ assert(strcmp(p.argv[0], "echo") == 0);
+ assert(strcmp(p.argv[1], "abc") == 0);
+ assert(p.argc[p.argv] == NULL);
+}
+
+
int main() {
test_ls();
test_simple_case();
test_detached();
test_pipe();
+ test_multiparse();
return 0;
-}*/
+}
diff --git a/02_exercise/prompt_utils_test.c b/02_exercise/prompt_utils_test.c
index 8bc29de..1be6162 100644
--- a/02_exercise/prompt_utils_test.c
+++ b/02_exercise/prompt_utils_test.c
@@ -36,10 +36,10 @@ void test_relative_path() {
test_paths("/B/C", "/", "../..");
}
-/*
+
int main(void) {
test_relative_path();
test_get_separator_indices();
return 0;
}
-*/
+
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index d1e52ec..ea1b13f 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -31,7 +31,6 @@ int main(void) {
// skip empty lines - empty being just spaces or tabs
continue;
}
-
line[length - 1] = '\0'; // cut the line feed
process *processes = NULL;
@@ -68,6 +67,8 @@ int main(void) {
clean:
free((void *)line);
+ line = NULL;
+ cap = 0;
free_processes(&processes);
}
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..41de2c8
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,40 @@
+cmake_minimum_required(VERSION 3.0.0)
+project(Betriebsysteme VERSION 0.1.0)
+
+include(CTest)
+enable_testing()
+
+set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/externals/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH})
+
+set(CPACK_PROJECT_NAME ${PROJECT_NAME})
+set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
+include(CPack)
+
+add_subdirectory(02_exercise)
+
+set(CLANG_WARNINGS
+ -Wall
+ -Wextra # reasonable and standard
+ -Wshadow # warn the user if a variable declaration shadows one from a
+ # parent context
+ -Wcast-align # warn for potential performance problem casts
+ -Wunused # warn on anything being unused
+ -Wpedantic # warn if non-standard C++ is used
+ -Wconversion # warn on type conversions that may lose data
+ -Wsign-conversion # warn on sign conversions
+ -Wnull-dereference # warn if a null dereference is detected
+ -Wdouble-promotion # warn if float is implicit promoted to double
+ -Wformat=2 # warn on security issues around functions that format output
+ # (ie printf)
+ -Werror
+ )
+set(GCC_WARNINGS
+ ${CLANG_WARNINGS}
+ -Wmisleading-indentation # warn if indentation implies blocks where blocks
+ # do not exist
+ -Wduplicated-cond # warn if if / else chain has duplicated conditions
+ -Wduplicated-branches # warn if if / else branches have duplicated code
+ -Wlogical-op # warn about logical operations being used where bitwise were
+ # probably wanted
+ )
+set(PROJECT_WARNINGS ${GCC_WARNINGS})
diff --git a/externals/sanitizers-cmake b/externals/sanitizers-cmake
new file mode 160000
+Subproject 99e159ec9bc8dd362b08d18436bd40ff0648417