diff --git a/Makefile b/Makefile index b560c9b..8576202 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,8 @@ define \n endef C = cc -C_DEBUG_FLAGS = -ggdb -pedantic -Wall -C_COMPILE_FLAGS = -O2 -DNDEBUG -fno-stack-protector -z execstack -no-pie +C_DEBUG_FLAGS = -ggdb -pedantic -Wall -fPIC +C_COMPILE_FLAGS = -O2 -DNDEBUG -fno-stack-protector -z execstack -no-pie -fPIC C_FLAGS = $(C_DEBUG_FLAGS) N = nasm N_DEBUG_FLAGS = -g -f elf64 @@ -69,6 +69,10 @@ $(LIB_DIR): @echo Creating: $@ @mkdir -p $@ +$(UPLOAD_DIR): + @echo Creating: $@ + @mkdir -p $@ + # Create the output binary $(TEST_BIN_DIR): @echo Creating: $@ @@ -102,38 +106,19 @@ test_%.out: $(TEST_BIN_DIR)/test_%.out # To Run all the tests test: $(notdir $(TESTS)) -# To clean any compiled object -clean_$(OBJ_DIR)/%.o: - @echo Removing: $(patsubst clean_%, %, $@) - @rm $(patsubst clean_%, %, $@) - -# To clean any archive library -clean_$(LIB_DIR)/%.a: - @echo Removing: $(patsubst clean_%, %, $@) - @rm $(patsubst clean_%, %, $@) - -# To clean any compiled test -clean_$(TEST_BIN_DIR)/%.out: - @echo Removing: $(patsubst clean_%, %, $@) - @rm $(patsubst clean_%, %, $@) # Remove all the compiled dependencies and tes -clean: $(addprefix clean_, $(wildcard $(OBJ_DIR)/*.o) \ - $(wildcard $(LIB_DIR)/*.a) \ - $(wildcard $(TEST_BIN_DIR)/*.out)) +clean: ifneq ("$(wildcard $(OBJ_DIR))", "") - @echo Removing: $(OBJ_DIR) - @rmdir $(OBJ_DIR) + @rm -vr $(OBJ_DIR) endif ifneq ("$(wildcard $(TESTS_BIN_DIR))", "") - @echo Removing: $(TEST_BIN_DIR) - @rmdir $(TESTS_BIN_DIR) + @rm -vr $(TESTS_BIN_DIR) endif ifneq ("$(wildcard $(LIB_DIR))", "") - @echo Removing: $(LIB_DIR) - @rmdir $(LIB_DIR) + @rm -vr $(LIB_DIR) endif # Clean objects and libs and recompile with optimizations diff --git a/PKGBUILD b/PKGBUILD index 89dc50c..bca2ba0 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -11,8 +11,8 @@ # Maintainer: alecksandr pkgname=trycatch-c -pkgver=1.3.0 -pkgrel=2 +pkgver=1.3.1 +pkgrel=3 epoch= pkgdesc="This module offers a straightforward macro interface that facilitates seamless exception handling in the C programming language, drawing inspiration from the paradigm employed in C++." diff --git a/TODO b/TODO new file mode 100644 index 0000000..da57011 --- /dev/null +++ b/TODO @@ -0,0 +1,2 @@ +---- 2024/01/26 ---- +- Create like standard exceptions [ ] \ No newline at end of file diff --git a/include/trycatch.h b/include/trycatch.h index 432ff8e..0d0d043 100644 --- a/include/trycatch.h +++ b/include/trycatch.h @@ -24,37 +24,38 @@ struct E { typedef struct F F; struct F { - F *prev; - JmpBuf contex; + F *prev; + JmpBuf contex; const char *file; int line; const E *exception; }; -#define try \ - do { \ +#define try \ + do { \ volatile int __except_flag; \ ExceptFrame __except_frame; /* Creates the except frame */ \ - /* Link the frames */ \ - __except_frame.prev = __except_head; \ - __except_head = &__except_frame; \ - __except_flag = stackjmp(&__except_frame.contex); \ - /* Try something */ \ + /* Link the frames */ \ + __except_frame.prev = __except_head; \ + __except_head = &__except_frame; \ + __except_flag = stackjmp(&__except_frame.contex); \ + /* Try something */ \ if (__except_flag == EXCEPT_ENTERED) #define throw(e) __tc_except_raise(&(e), __FILE__, __LINE__) -#define RE_RAISE \ - __tc_except_raise(__except_frame.exception, __except_frame.file, __except_frame.line) -#define catch(e) \ +#define RE_RAISE \ + __tc_except_raise(__except_frame.exception, __except_frame.file, \ + __except_frame.line) +#define catch(e) \ else if (__except_frame.exception == &(e) && (__except_flag = EXCEPT_HANDLED)) #define otherwise else if ((__except_flag = EXCEPT_HANDLED)) -#define endtry \ - ; \ +#define endtry \ + ; \ if (__except_flag == EXCEPT_ENTERED) __except_head = __except_head->prev; \ - if (__except_flag == EXCEPT_RAISED) RE_RAISE; \ - } \ + if (__except_flag == EXCEPT_RAISED) RE_RAISE; \ + } \ while (0) -extern F *__except_head; +extern F *__except_head; extern void __tc_except_raise(const E *e, const char *file, int line); #undef E diff --git a/src/trycatch.c b/src/trycatch.c index e9f0afb..9a519c8 100644 --- a/src/trycatch.c +++ b/src/trycatch.c @@ -7,14 +7,12 @@ @license This project is released under the MIT License */ - +#include "../include/trycatch.h" #include #include #include -#include "../include/trycatch.h" - #define F ExceptFrame #define E Except @@ -27,12 +25,12 @@ void __tc_except_raise(const E *e, const char *file, int line) F *frame = __except_head; - if (frame == NULL) { + if (frame == NULL) { fprintf(stderr, "Uncaught exception: "); if (e->reason) fprintf(stderr, "%s:", e->reason); else fprintf(stderr, "at 0x%p:", (void *) e); if (file && line > 0) fprintf(stderr, " raised at %s:%i\n", file, line); - + fprintf(stderr, "Aborting....\n"); fflush(stderr); abort(); @@ -42,8 +40,8 @@ void __tc_except_raise(const E *e, const char *file, int line) frame->exception = e; frame->file = file; frame->line = line; - __except_head = __except_head->prev; - + __except_head = __except_head->prev; + jmpback(&frame->contex, EXCEPT_RAISED); } diff --git a/test/src/test_stackjmp.c b/test/src/test_stackjmp.c index 7a0c8a7..6a8aae2 100644 --- a/test/src/test_stackjmp.c +++ b/test/src/test_stackjmp.c @@ -1,5 +1,5 @@ /* TO test the setjmp */ -#include "../../include/tc/stackjmp.h" +#include "../../include/trycatch/stackjmp.h" #include #include diff --git a/test/src/test_trycatch.c b/test/src/test_trycatch.c index 1bfb1d4..d7db58f 100644 --- a/test/src/test_trycatch.c +++ b/test/src/test_trycatch.c @@ -14,16 +14,11 @@ void test_simple_try_except(void) { int i = 0; - try - throw(SomeError); /* throw_except some error */ - catch(SomeError3) - i = 5; - catch(SomeError) - i++; - catch(SomeError2) - i = 3; - otherwise - i = 2; + try throw(SomeError); /* throw_except some error */ + catch (SomeError3) i = 5; + catch (SomeError) i++; + catch (SomeError2) i = 3; + otherwise i = 2; endtry; assert(i == 1); @@ -35,8 +30,7 @@ Except NotEnoughMemory = {"Not enough memory"}, /* always is going to return null */ void *alloc_will_be_null(int num) { - if (num < 1) - throw(AllocNumShouldBePositive); + if (num < 1) throw(AllocNumShouldBePositive); return NULL; } @@ -55,9 +49,9 @@ void test_simulate_try_except(void) { void *some_instace = NULL; - try - some_instace = create_instance_of_something(10); - catch(NotEnoughMemory) { + try some_instace = create_instance_of_something(10); + catch (NotEnoughMemory) + { assert(some_instace == NULL); assert(__except_flag == EXCEPT_HANDLED); } @@ -68,16 +62,12 @@ void test_multiples_trys(void) { int i = 0; - try - throw(SomeError); - catch(SomeError) - i++; + try throw(SomeError); + catch (SomeError) i++; endtry; - try - throw(SomeError2); - catch(SomeError2) - i++; + try throw(SomeError2); + catch (SomeError2) i++; endtry; assert(i == 2); @@ -87,9 +77,8 @@ void more_try(void) { void *instance = NULL; - try - instance = create_instance_of_something(-1); - catch(AllocNumShouldBePositive) + try instance = create_instance_of_something(-1); + catch (AllocNumShouldBePositive) { assert(instance == NULL); throw(AllocNumShouldBePositive); /* Raise again an error */ @@ -99,9 +88,9 @@ void more_try(void) void test_trys_inside_trys(void) { - try - more_try(); - catch(AllocNumShouldBePositive) { + try more_try(); + catch (AllocNumShouldBePositive) + { assert(exception == AllocNumShouldBePositive.reason); printf("%s\n", exception); }