From 356b25fd9460d844a98d1a5edde6d45d3f6ea3f9 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 13 Sep 2020 21:45:30 -0400 Subject: [PATCH] Implement src/error.c Implement src/error.c to provide the patty_error type, to allow easy error string formatting, error state checking, and error clearing --- include/patty/error.h | 24 ++++++++++++++++++++++++ src/Makefile | 12 ++++++------ src/error.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 include/patty/error.h create mode 100644 src/error.c diff --git a/include/patty/error.h b/include/patty/error.h new file mode 100644 index 0000000..2724a27 --- /dev/null +++ b/include/patty/error.h @@ -0,0 +1,24 @@ +#ifndef _PATTY_ERROR_H +#define _PATTY_ERROR_H + +#define PATTY_ERROR_STRLEN 256 + +enum patty_error_state { + PATTY_ERROR_OK, + PATTY_ERROR_SET +}; + +typedef struct _patty_error { + enum patty_error_state state; + char err[PATTY_ERROR_STRLEN]; +} patty_error; + +int patty_error_fmt(patty_error *e, const char *message, ...); + +void patty_error_clear(patty_error *e); + +int patty_error_set(patty_error *e); + +char *patty_error_string(patty_error *e); + +#endif /* _PATTY_ERROR_H */ diff --git a/src/Makefile b/src/Makefile index 1774b1d..a979dcd 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,13 +7,13 @@ CC = $(CROSS)cc CFLAGS = $(CGFLAGS) -fPIC -Wall -O2 -I$(INCLUDE_PATH) LDFLAGS = -lutil -HEADERS = kiss.h ax25.h client.h ax25/if.h ax25/frame.h \ - ax25/sock.h ax25/route.h ax25/server.h daemon.h list.h \ - hash.h dict.h timer.h print.h conf.h +HEADERS = kiss.h kiss/tnc.h ax25.h client.h ax25/if.h ax25/frame.h \ + ax25/sock.h ax25/route.h ax25/server.h daemon.h \ + error.h list.h hash.h dict.h timer.h print.h conf.h -OBJS = kiss.o ax25.o client.o if.o frame.o \ - sock.o route.o server.o daemon.o list.o \ - hash.o dict.o timer.o print.o conf.o +OBJS = kiss.o tnc.o ax25.o client.o if.o frame.o \ + sock.o route.o server.o daemon.o \ + error.o list.o hash.o dict.o timer.o print.o conf.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..db34ea5 --- /dev/null +++ b/src/error.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +#include + +int patty_error_fmt(patty_error *e, const char *message, ...) { + va_list args; + + va_start(args, message); + + if (vsnprintf(e->err, sizeof(e->err)-1, message, args) < 0) { + goto error_vsnprintf; + } + + va_end(args); + + e->state = PATTY_ERROR_SET; + + return 0; + +error_vsnprintf: + va_end(args); + + return -1; +} + +void patty_error_clear(patty_error *e) { + memset(e->err, '\0', sizeof(e->err)); + + e->state = PATTY_ERROR_OK; +} + +int patty_error_isset(patty_error *e) { + return e->state == PATTY_ERROR_SET? 1: 0; +} + +char *patty_error_string(patty_error *e) { + return e->err; +}